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

Commit afde246e authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Call SmartActionsHelpers in a worker thread"

parents 3e14606e dab427e9
Loading
Loading
Loading
Loading
+24 −19
Original line number Diff line number Diff line
@@ -65,6 +65,8 @@ import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Notification assistant that provides guidance on notification channel blocking
@@ -80,6 +82,7 @@ public class Assistant extends NotificationAssistantService {
    private static final String ATT_KEY = "key";
    private static final int DB_VERSION = 1;
    private static final String ATTR_VERSION = "version";
    private final ExecutorService mSingleThreadExecutor = Executors.newSingleThreadExecutor();

    private static final ArrayList<Integer> PREJUDICAL_DISMISSALS = new ArrayList<>();
    static {
@@ -233,15 +236,20 @@ public class Assistant extends NotificationAssistantService {
        if (!isForCurrentUser(sbn)) {
            return null;
        }
        mSingleThreadExecutor.submit(() -> {
            NotificationEntry entry =
                    new NotificationEntry(mPackageManager, sbn, channel, mSmsHelper);
            SmartActionsHelper.SmartSuggestions suggestions = mSmartActionsHelper.suggest(entry);
            if (DEBUG) {
            Log.d(TAG, String.format("Creating Adjustment for %s, with %d actions, and %d replies.",
                Log.d(TAG, String.format(
                        "Creating Adjustment for %s, with %d actions, and %d replies.",
                        sbn.getKey(), suggestions.actions.size(), suggestions.replies.size()));
            }
        return createEnqueuedNotificationAdjustment(
            Adjustment adjustment = createEnqueuedNotificationAdjustment(
                    entry, suggestions.actions, suggestions.replies);
            adjustNotification(adjustment);
        });
        return null;
    }

    /** A convenience helper for creating an adjustment for an SBN. */
@@ -386,15 +394,15 @@ public class Assistant extends NotificationAssistantService {
        NotificationEntry entry = mLiveNotifications.get(key);

        if (entry != null) {
            entry.setExpanded(isExpanded);
            mSmartActionsHelper.onNotificationExpansionChanged(entry, isUserAction, isExpanded);
            mSingleThreadExecutor.submit(
                    () -> mSmartActionsHelper.onNotificationExpansionChanged(entry, isExpanded));
        }
    }

    @Override
    public void onNotificationDirectReplied(@NonNull String key) {
        if (DEBUG) Log.i(TAG, "onNotificationDirectReplied " + key);
        mSmartActionsHelper.onNotificationDirectReplied(key);
        mSingleThreadExecutor.submit(() -> mSmartActionsHelper.onNotificationDirectReplied(key));
    }

    @Override
@@ -404,7 +412,8 @@ public class Assistant extends NotificationAssistantService {
            Log.d(TAG, "onSuggestedReplySent() called with: key = [" + key + "], reply = [" + reply
                    + "], source = [" + source + "]");
        }
        mSmartActionsHelper.onSuggestedReplySent(key, reply, source);
        mSingleThreadExecutor.submit(
                () -> mSmartActionsHelper.onSuggestedReplySent(key, reply, source));
    }

    @Override
@@ -415,7 +424,8 @@ public class Assistant extends NotificationAssistantService {
                    "onActionInvoked() called with: key = [" + key + "], action = [" + action.title
                            + "], source = [" + source + "]");
        }
        mSmartActionsHelper.onActionClicked(key, action, source);
        mSingleThreadExecutor.submit(
                () -> mSmartActionsHelper.onActionClicked(key, action, source));
    }

    @Override
@@ -493,11 +503,6 @@ public class Assistant extends NotificationAssistantService {
        mPackageManager = pm;
    }

    @VisibleForTesting
    public void setSmartActionsHelper(SmartActionsHelper smartActionsHelper) {
        mSmartActionsHelper = smartActionsHelper;
    }

    @VisibleForTesting
    public ChannelImpressions getImpressions(String key) {
        synchronized (mkeyToImpressions) {
+20 −15
Original line number Diff line number Diff line
@@ -47,17 +47,18 @@ import java.util.Objects;
public class NotificationEntry {
    static final String TAG = "NotificationEntry";

    private StatusBarNotification mSbn;
    private final StatusBarNotification mSbn;
    private final IPackageManager mPackageManager;
    private int mTargetSdkVersion = Build.VERSION_CODES.N_MR1;
    private boolean mPreChannelsNotification = true;
    private AudioAttributes mAttributes;
    private NotificationChannel mChannel;
    private int mImportance;
    private final boolean mPreChannelsNotification;
    private final AudioAttributes mAttributes;
    private final NotificationChannel mChannel;
    private final int mImportance;
    private boolean mSeen;
    private boolean mExpanded;
    private boolean mIsShowActionEventLogged;
    private SmsHelper mSmsHelper;
    private final SmsHelper mSmsHelper;

    private final Object mLock = new Object();

    public NotificationEntry(IPackageManager packageManager, StatusBarNotification sbn,
            NotificationChannel channel, SmsHelper smsHelper) {
@@ -233,24 +234,28 @@ public class NotificationEntry {
    }

    public void setSeen() {
        synchronized (mLock) {
            mSeen = true;
        }

    public void setExpanded(boolean expanded) {
        mExpanded = expanded;
    }

    public void setShowActionEventLogged() {
        synchronized (mLock) {
            mIsShowActionEventLogged = true;
        }
    }

    public boolean hasSeen() {
        synchronized (mLock) {
            return mSeen;
        }
    }

    public boolean isShowActionEventLogged() {
        synchronized (mLock) {
            return mIsShowActionEventLogged;
        }
    }

    public StatusBarNotification getSbn() {
        return mSbn;
+6 −2
Original line number Diff line number Diff line
@@ -53,6 +53,11 @@ import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * Generates suggestions from incoming notifications.
 *
 * Methods in this class should be called in a single worker thread.
 */
public class SmartActionsHelper {
    static final String ENTITIES_EXTRAS = "entities-extras";
    static final String KEY_ACTION_TYPE = "action_type";
@@ -287,8 +292,7 @@ public class SmartActionsHelper {
        return conversationActions;
    }

    void onNotificationExpansionChanged(NotificationEntry entry, boolean isUserAction,
            boolean isExpanded) {
    void onNotificationExpansionChanged(NotificationEntry entry, boolean isExpanded) {
        if (!isExpanded) {
            return;
        }
+3 −3
Original line number Diff line number Diff line
@@ -382,7 +382,7 @@ public class SmartActionsHelperTest {
        when(mStatusBarNotification.getNotification()).thenReturn(notification);

        mSmartActionsHelper.suggest(createNotificationEntry());
        mSmartActionsHelper.onNotificationExpansionChanged(createNotificationEntry(), true, true);
        mSmartActionsHelper.onNotificationExpansionChanged(createNotificationEntry(), true);

        ArgumentCaptor<TextClassifierEvent> argumentCaptor =
                ArgumentCaptor.forClass(TextClassifierEvent.class);
@@ -398,7 +398,7 @@ public class SmartActionsHelperTest {
        when(mStatusBarNotification.getNotification()).thenReturn(notification);

        mSmartActionsHelper.suggest(createNotificationEntry());
        mSmartActionsHelper.onNotificationExpansionChanged(createNotificationEntry(), false, false);
        mSmartActionsHelper.onNotificationExpansionChanged(createNotificationEntry(), false);

        verify(mTextClassifier, never()).onTextClassifierEvent(
                argThat(new TextClassifierEventMatcher(TextClassifierEvent.TYPE_ACTIONS_SHOWN)));
@@ -410,7 +410,7 @@ public class SmartActionsHelperTest {
        when(mStatusBarNotification.getNotification()).thenReturn(notification);

        mSmartActionsHelper.suggest(createNotificationEntry());
        mSmartActionsHelper.onNotificationExpansionChanged(createNotificationEntry(), false, true);
        mSmartActionsHelper.onNotificationExpansionChanged(createNotificationEntry(), true);

        ArgumentCaptor<TextClassifierEvent> argumentCaptor =
                ArgumentCaptor.forClass(TextClassifierEvent.class);