Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e87ebbe2 authored by Ned Burns's avatar Ned Burns
Browse files

NotificationEntryBuilder can modify existing entries

Modifies NotificationEntryBuilder to, instead of creating a new
NotifEntry, also be able to modify an existing entry.

Test: atest SystemUITests
Change-Id: I3d4f434eaaa9c6e63278529a06d0fc044096d20e
parent d9736c20
Loading
Loading
Loading
Loading
+32 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.statusbar.notification.collection

/**
 * Modifies a NotificationEntry
 *
 * The [modifier] function will be passed an instance of a NotificationEntryBuilder. Any
 * modifications made to the builder will be applied to the [entry].
 */
inline fun modifyEntry(
    entry: NotificationEntry,
    modifier: NotificationEntryBuilder.() -> Unit
) {
    val builder = NotificationEntryBuilder(entry)
    modifier(builder)
    builder.apply(entry)
}
+47 −7
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import android.app.NotificationManager;
import android.content.Context;
import android.content.pm.ShortcutInfo;
import android.os.UserHandle;
import android.service.notification.NotificationListenerService.Ranking;
import android.service.notification.SnoozeCriterion;
import android.service.notification.StatusBarNotification;

@@ -33,6 +34,8 @@ import com.android.systemui.util.time.FakeSystemClock;

import java.util.ArrayList;

import kotlin.Unit;

/**
 * Combined builder for constructing a NotificationEntry and its associated StatusBarNotification
 * and Ranking. Is largely a proxy for the SBN and Ranking builders, but does a little extra magic
@@ -43,8 +46,8 @@ import java.util.ArrayList;
 * Only for use in tests.
 */
public class NotificationEntryBuilder {
    private final SbnBuilder mSbnBuilder = new SbnBuilder();
    private final RankingBuilder mRankingBuilder = new RankingBuilder();
    private final SbnBuilder mSbnBuilder;
    private final RankingBuilder mRankingBuilder;
    private final FakeSystemClock mClock = new FakeSystemClock();
    private StatusBarNotification mSbn = null;

@@ -55,12 +58,49 @@ public class NotificationEntryBuilder {
    /* If set, use this creation time instead of mClock.uptimeMillis */
    private long mCreationTime = -1;

    public NotificationEntryBuilder() {
        mSbnBuilder = new SbnBuilder();
        mRankingBuilder = new RankingBuilder();
    }

    public NotificationEntryBuilder(NotificationEntry source) {
        mSbnBuilder = new SbnBuilder(source.getSbn());
        mRankingBuilder = new RankingBuilder(source.getRanking());

        mParent = source.getParent();
        mSection = source.getSection();
        mCreationTime = source.getCreationTime();
    }

    /** Build a new instance of NotificationEntry */
    public NotificationEntry build() {
        StatusBarNotification sbn = mSbn != null ? mSbn : mSbnBuilder.build();
        mRankingBuilder.setKey(sbn.getKey());
        long creationTime = mCreationTime != -1 ? mCreationTime : mClock.uptimeMillis();
        final NotificationEntry entry = new NotificationEntry(
                sbn, mRankingBuilder.build(), mClock.uptimeMillis());
        return buildOrApply(null);
    }

    /** Modifies [target] to match the contents of this builder */
    public void apply(NotificationEntry target) {
        buildOrApply(target);
    }

    /** Convenience method for Kotlin callbacks that are passed a builder and need to return Unit */
    public Unit done() {
        return Unit.INSTANCE;
    }

    private NotificationEntry buildOrApply(NotificationEntry target) {
        final StatusBarNotification sbn = mSbn != null ? mSbn : mSbnBuilder.build();
        final Ranking ranking = mRankingBuilder.setKey(sbn.getKey()).build();
        final long creationTime = mCreationTime != -1 ? mCreationTime : mClock.uptimeMillis();

        final NotificationEntry entry;
        if (target == null) {
            entry = new NotificationEntry(sbn, ranking, creationTime);
        } else {
            entry = target;
            entry.setSbn(sbn);
            entry.setRanking(ranking);
            // Note: we can't modify the creation time as it's immutable
        }

        /* ListEntry properties */
        entry.setParent(mParent);