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

Commit 3f39de97 authored by Julia Reynolds's avatar Julia Reynolds Committed by Android (Google) Code Review
Browse files

Merge changes from topic "jr-hist-snooze"

* changes:
  Add tracing for notification history actions
  Optionally exclude snoozed notifs from dismissal archive
parents 00712e26 bbee4670
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -123,7 +123,7 @@ interface INotificationManager
    @UnsupportedAppUsage
    StatusBarNotification[] getActiveNotifications(String callingPkg);
    @UnsupportedAppUsage
    StatusBarNotification[] getHistoricalNotifications(String callingPkg, int count);
    StatusBarNotification[] getHistoricalNotifications(String callingPkg, int count, boolean includeSnoozed);

    NotificationHistory getNotificationHistory(String callingPkg);

+56 −27
Original line number Diff line number Diff line
@@ -185,6 +185,7 @@ import android.os.ServiceManager;
import android.os.ShellCallback;
import android.os.SystemClock;
import android.os.SystemProperties;
import android.os.Trace;
import android.os.UserHandle;
import android.os.UserManager;
import android.os.VibrationEffect;
@@ -527,11 +528,11 @@ public class NotificationManagerService extends SystemService {

    private static class Archive {
        final int mBufferSize;
        final ArrayDeque<StatusBarNotification> mBuffer;
        final ArrayDeque<Pair<StatusBarNotification, Integer>> mBuffer;

        public Archive(int size) {
            mBufferSize = size;
            mBuffer = new ArrayDeque<StatusBarNotification>(mBufferSize);
            mBuffer = new ArrayDeque<>(mBufferSize);
        }

        public String toString() {
@@ -544,7 +545,7 @@ public class NotificationManagerService extends SystemService {
            return sb.toString();
        }

        public void record(StatusBarNotification nr) {
        public void record(StatusBarNotification nr, int reason) {
            if (mBuffer.size() == mBufferSize) {
                mBuffer.removeFirst();
            }
@@ -552,21 +553,24 @@ public class NotificationManagerService extends SystemService {
            // We don't want to store the heavy bits of the notification in the archive,
            // but other clients in the system process might be using the object, so we
            // store a (lightened) copy.
            mBuffer.addLast(nr.cloneLight());
            mBuffer.addLast(new Pair<>(nr.cloneLight(), reason));
        }

        public Iterator<StatusBarNotification> descendingIterator() {
        public Iterator<Pair<StatusBarNotification, Integer>> descendingIterator() {
            return mBuffer.descendingIterator();
        }

        public StatusBarNotification[] getArray(int count) {
        public StatusBarNotification[] getArray(int count, boolean includeSnoozed) {
            if (count == 0) count = mBufferSize;
            final StatusBarNotification[] a
                    = new StatusBarNotification[Math.min(count, mBuffer.size())];
            Iterator<StatusBarNotification> iter = descendingIterator();
            Iterator<Pair<StatusBarNotification, Integer>> iter = descendingIterator();
            int i=0;
            while (iter.hasNext() && i < count) {
                a[i++] = iter.next();
                Pair<StatusBarNotification, Integer> pair = iter.next();
                if (pair.second != REASON_SNOOZED || includeSnoozed) {
                    a[i++] = pair.first;
                }
            }
            return a;
        }
@@ -2260,12 +2264,26 @@ public class NotificationManagerService extends SystemService {

    @Override
    public void onUnlockUser(@NonNull UserInfo userInfo) {
        mHandler.post(() -> mHistoryManager.onUserUnlocked(userInfo.id));
        mHandler.post(() -> {
            Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "notifHistoryUnlockUser");
            try {
                mHistoryManager.onUserUnlocked(userInfo.id);
            } finally {
                Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
            }
        });
    }

    @Override
    public void onStopUser(@NonNull UserInfo userInfo) {
        mHandler.post(() -> mHistoryManager.onUserStopped(userInfo.id));
        mHandler.post(() -> {
            Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "notifHistoryStopUser");
            try {
                mHistoryManager.onUserStopped(userInfo.id);
            } finally {
                Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
            }
        });
    }

    @GuardedBy("mNotificationLock")
@@ -2592,6 +2610,8 @@ public class NotificationManagerService extends SystemService {
            mAppUsageStats.reportInterruptiveNotification(r.getSbn().getPackageName(),
                    r.getChannel().getId(),
                    getRealUserId(r.getSbn().getUserId()));
            Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "notifHistoryAddItem");
            try {
                mHistoryManager.addNotification(new HistoricalNotification.Builder()
                        .setPackage(r.getSbn().getPackageName())
                        .setUid(r.getSbn().getUid())
@@ -2603,6 +2623,9 @@ public class NotificationManagerService extends SystemService {
                                r.getSbn().getPackageContext(getContext()), r.getNotification()))
                        .setIcon(r.getNotification().getSmallIcon())
                        .build());
            } finally {
                Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
            }
            r.setRecordedInterruption(true);
        }
    }
@@ -3638,7 +3661,8 @@ public class NotificationManagerService extends SystemService {
         */
        @Override
        @RequiresPermission(android.Manifest.permission.ACCESS_NOTIFICATIONS)
        public StatusBarNotification[] getHistoricalNotifications(String callingPkg, int count) {
        public StatusBarNotification[] getHistoricalNotifications(String callingPkg, int count,
                boolean includeSnoozed) {
            // enforce() will ensure the calling uid has the correct permission
            getContext().enforceCallingOrSelfPermission(
                    android.Manifest.permission.ACCESS_NOTIFICATIONS,
@@ -3651,7 +3675,7 @@ public class NotificationManagerService extends SystemService {
            if (mAppOps.noteOpNoThrow(AppOpsManager.OP_ACCESS_NOTIFICATIONS, uid, callingPkg)
                    == AppOpsManager.MODE_ALLOWED) {
                synchronized (mArchive) {
                    tmp = mArchive.getArray(count);
                    tmp = mArchive.getArray(count, includeSnoozed);
                }
            }
            return tmp;
@@ -3675,7 +3699,12 @@ public class NotificationManagerService extends SystemService {
            if (mAppOps.noteOpNoThrow(AppOpsManager.OP_ACCESS_NOTIFICATIONS, uid, callingPkg)
                    == AppOpsManager.MODE_ALLOWED) {
                IntArray currentUserIds = mUserProfiles.getCurrentProfileIds();
                Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "notifHistoryReadHistory");
                try {
                    return mHistoryManager.readNotificationHistory(currentUserIds.toArray());
                } finally {
                    Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
                }
            }
            return new NotificationHistory();
        }
@@ -5199,10 +5228,10 @@ public class NotificationManagerService extends SystemService {
                    pw.println("  mMaxPackageEnqueueRate=" + mMaxPackageEnqueueRate);
                }
                pw.println("  mArchive=" + mArchive.toString());
                Iterator<StatusBarNotification> iter = mArchive.descendingIterator();
                Iterator<Pair<StatusBarNotification, Integer>> iter = mArchive.descendingIterator();
                int j=0;
                while (iter.hasNext()) {
                    final StatusBarNotification sbn = iter.next();
                    final StatusBarNotification sbn = iter.next().first;
                    if (filter != null && !filter.matches(sbn)) continue;
                    pw.println("    " + sbn);
                    if (++j >= 5) {
@@ -7575,7 +7604,7 @@ public class NotificationManagerService extends SystemService {
        }

        // Save it for users of getHistoricalNotifications()
        mArchive.record(r.getSbn());
        mArchive.record(r.getSbn(), reason);

        final long now = System.currentTimeMillis();
        final LogMaker logMaker = r.getItemLogMaker()