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

Commit 5373a2e6 authored by Sudheer Shanka's avatar Sudheer Shanka Committed by Android (Google) Code Review
Browse files

Merge changes Idde09e4c,I0337b400

* changes:
  Inform UsageStatsManager about notification related events.
  Allow reporting certain events to UsageStatsManager.
parents cdf01410 4d1f3862
Loading
Loading
Loading
Loading
+50 −0
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package android.app.usage;

import android.annotation.CurrentTimeMillisLong;
import android.annotation.ElapsedRealtimeLong;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.UserIdInt;
@@ -25,6 +26,7 @@ import android.content.ComponentName;
import android.content.LocusId;
import android.content.res.Configuration;
import android.os.IBinder;
import android.os.SystemClock;
import android.os.UserHandle;
import android.os.UserManager;

@@ -362,4 +364,52 @@ public abstract class UsageStatsManagerInternal {
    /** Unregister a listener from being notified of every estimated launch time change. */
    public abstract void unregisterLaunchTimeChangedListener(
            @NonNull EstimatedLaunchTimeChangedListener listener);

    /**
     * Reports a broadcast dispatched event to the UsageStatsManager.
     *
     * @param sourceUid uid of the package that sent the broadcast.
     * @param targetPackage name of the package that the broadcast is targeted to.
     * @param targetUser user that {@code targetPackage} belongs to.
     * @param idForResponseEvent ID to be used for recording any response events corresponding
     *                           to this broadcast.
     * @param timestampMs time (in millis) when the broadcast was dispatched, in
     *                    {@link SystemClock#elapsedRealtime()} timebase.
     */
    public abstract void reportBroadcastDispatched(int sourceUid, @NonNull String targetPackage,
            @NonNull UserHandle targetUser, long idForResponseEvent,
            @ElapsedRealtimeLong long timestampMs);

    /**
     * Reports a notification posted event to the UsageStatsManager.
     *
     * @param packageName name of the package which posted the notification.
     * @param user user that {@code packageName} belongs to.
     * @param timestampMs time (in millis) when the notification was posted, in
     *                    {@link SystemClock#elapsedRealtime()} timebase.
     */
    public abstract void reportNotificationPosted(@NonNull String packageName,
            @NonNull UserHandle user, @ElapsedRealtimeLong long timestampMs);

    /**
     * Reports a notification updated event to the UsageStatsManager.
     *
     * @param packageName name of the package which updated the notification.
     * @param user user that {@code packageName} belongs to.
     * @param timestampMs time (in millis) when the notification was updated, in
     *                    {@link SystemClock#elapsedRealtime()} timebase.
     */
    public abstract void reportNotificationUpdated(@NonNull String packageName,
            @NonNull UserHandle user, @ElapsedRealtimeLong long timestampMs);

    /**
     * Reports a notification removed event to the UsageStatsManager.
     *
     * @param packageName name of the package which removed the notification.
     * @param user user that {@code packageName} belongs to.
     * @param timestampMs time (in millis) when the notification was removed, in
     *                    {@link SystemClock#elapsedRealtime()} timebase.
     */
    public abstract void reportNotificationRemoved(@NonNull String packageName,
            @NonNull UserHandle user, @ElapsedRealtimeLong long timestampMs);
}
+12 −2
Original line number Diff line number Diff line
@@ -499,6 +499,7 @@ public class NotificationManagerService extends SystemService {
    private IPlatformCompat mPlatformCompat;
    private ShortcutHelper mShortcutHelper;
    private PermissionHelper mPermissionHelper;
    private UsageStatsManagerInternal mUsageStatsManagerInternal;

    final IBinder mForegroundToken = new Binder();
    private WorkerHandler mHandler;
@@ -2092,7 +2093,8 @@ public class NotificationManagerService extends SystemService {
            UserManager userManager,
            NotificationHistoryManager historyManager, StatsManager statsManager,
            TelephonyManager telephonyManager, ActivityManagerInternal ami,
            MultiRateLimiter toastRateLimiter, PermissionHelper permissionHelper) {
            MultiRateLimiter toastRateLimiter, PermissionHelper permissionHelper,
            UsageStatsManagerInternal usageStatsManagerInternal) {
        mHandler = handler;
        Resources resources = getContext().getResources();
        mMaxPackageEnqueueRate = Settings.Global.getFloat(getContext().getContentResolver(),
@@ -2110,6 +2112,7 @@ public class NotificationManagerService extends SystemService {
        mPackageManagerClient = packageManagerClient;
        mPackageManagerInternal = LocalServices.getService(PackageManagerInternal.class);
        mPermissionPolicyInternal = LocalServices.getService(PermissionPolicyInternal.class);
        mUsageStatsManagerInternal = usageStatsManagerInternal;
        mAppOps = appOps;
        mAppOpsService = iAppOps;
        try {
@@ -2411,7 +2414,8 @@ public class NotificationManagerService extends SystemService {
                LocalServices.getService(ActivityManagerInternal.class),
                createToastRateLimiter(), new PermissionHelper(LocalServices.getService(
                        PermissionManagerServiceInternal.class), AppGlobals.getPackageManager(),
                        AppGlobals.getPermissionManager(), mEnableAppSettingMigration));
                        AppGlobals.getPermissionManager(), mEnableAppSettingMigration),
                LocalServices.getService(UsageStatsManagerInternal.class));

        publishBinderService(Context.NOTIFICATION_SERVICE, mService, /* allowIsolated= */ false,
                DUMP_FLAG_PRIORITY_CRITICAL | DUMP_FLAG_PRIORITY_NORMAL);
@@ -7259,6 +7263,8 @@ public class NotificationManagerService extends SystemService {
                    if (index < 0) {
                        mNotificationList.add(r);
                        mUsageStats.registerPostedByApp(r);
                        mUsageStatsManagerInternal.reportNotificationPosted(r.getSbn().getOpPkg(),
                                r.getSbn().getUser(), SystemClock.elapsedRealtime());
                        final boolean isInterruptive = isVisuallyInterruptive(null, r);
                        r.setInterruptive(isInterruptive);
                        r.setTextChanged(isInterruptive);
@@ -7266,6 +7272,8 @@ public class NotificationManagerService extends SystemService {
                        old = mNotificationList.get(index);  // Potentially *changes* old
                        mNotificationList.set(index, r);
                        mUsageStats.registerUpdatedByApp(r, old);
                        mUsageStatsManagerInternal.reportNotificationUpdated(r.getSbn().getOpPkg(),
                                r.getSbn().getUser(), SystemClock.elapsedRealtime());
                        // Make sure we don't lose the foreground service state.
                        notification.flags |=
                                old.getNotification().flags & FLAG_FOREGROUND_SERVICE;
@@ -8748,6 +8756,8 @@ public class NotificationManagerService extends SystemService {
            case REASON_APP_CANCEL:
            case REASON_APP_CANCEL_ALL:
                mUsageStats.registerRemovedByApp(r);
                mUsageStatsManagerInternal.reportNotificationRemoved(r.getSbn().getOpPkg(),
                        r.getUser(), SystemClock.elapsedRealtime());
                break;
        }

+1 −1
Original line number Diff line number Diff line
@@ -483,7 +483,7 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
                mAppUsageStats, mock(DevicePolicyManagerInternal.class), mUgm, mUgmInternal,
                mAppOpsManager, mAppOpsService, mUm, mHistoryManager, mStatsManager,
                mock(TelephonyManager.class),
                mAmi, mToastRateLimiter, mPermissionHelper);
                mAmi, mToastRateLimiter, mPermissionHelper, mock(UsageStatsManagerInternal.class));
        // Return first true for RoleObserver main-thread check
        when(mMainLooper.isCurrentThread()).thenReturn(true).thenReturn(false);
        mService.onBootPhase(SystemService.PHASE_SYSTEM_SERVICES_READY, mMainLooper);
+2 −1
Original line number Diff line number Diff line
@@ -371,7 +371,8 @@ public class NotificationPermissionMigrationTest extends UiServiceTestCase {
                mSnoozeHelper, mUsageStats, mPolicyFile, mActivityManager, mGroupHelper, mAm, mAtm,
                mAppUsageStats, mock(DevicePolicyManagerInternal.class), mUgm, mUgmInternal,
                mAppOpsManager, mock(IAppOpsService.class), mUm, mHistoryManager, mStatsManager,
                mock(TelephonyManager.class), mAmi, mToastRateLimiter, mPermissionHelper);
                mock(TelephonyManager.class), mAmi, mToastRateLimiter, mPermissionHelper,
                mock(UsageStatsManagerInternal.class));
        // Return first true for RoleObserver main-thread check
        when(mMainLooper.isCurrentThread()).thenReturn(true).thenReturn(false);
        mService.onBootPhase(SystemService.PHASE_SYSTEM_SERVICES_READY, mMainLooper);
+2 −1
Original line number Diff line number Diff line
@@ -166,7 +166,8 @@ public class RoleObserverTest extends UiServiceTestCase {
                    mUm, mock(NotificationHistoryManager.class),
                    mock(StatsManager.class), mock(TelephonyManager.class),
                    mock(ActivityManagerInternal.class),
                    mock(MultiRateLimiter.class), mock(PermissionHelper.class));
                    mock(MultiRateLimiter.class), mock(PermissionHelper.class),
                    mock(UsageStatsManagerInternal.class));
        } catch (SecurityException e) {
            if (!e.getMessage().contains("Permission Denial: not allowed to send broadcast")) {
                throw e;
Loading