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

Commit d1e5faba authored by Sudheer Shanka's avatar Sudheer Shanka
Browse files

Track broadcast dispatched event only when the app is in the bg.

Bug: 206518114
Test: atest tests/tests/app.usage/src/android/app/usage/cts/UsageStatsTest.java
Change-Id: Ic017fff35d800a1ea9b4288421e021859f443b30
parent 3578c8f2
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import android.annotation.ElapsedRealtimeLong;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.UserIdInt;
import android.app.ActivityManager.ProcessState;
import android.app.usage.UsageStatsManager.StandbyBuckets;
import android.content.ComponentName;
import android.content.LocusId;
@@ -375,10 +376,11 @@ public abstract class UsageStatsManagerInternal {
     *                           to this broadcast.
     * @param timestampMs time (in millis) when the broadcast was dispatched, in
     *                    {@link SystemClock#elapsedRealtime()} timebase.
     * @param targetUidProcState process state of the uid that the broadcast is targeted to.
     */
    public abstract void reportBroadcastDispatched(int sourceUid, @NonNull String targetPackage,
            @NonNull UserHandle targetUser, long idForResponseEvent,
            @ElapsedRealtimeLong long timestampMs);
            @ElapsedRealtimeLong long timestampMs, @ProcessState int targetUidProcState);

    /**
     * Reports a notification posted event to the UsageStatsManager.
+5 −6
Original line number Diff line number Diff line
@@ -334,7 +334,7 @@ public final class BroadcastQueue {
        mService.updateOomAdjPendingTargetsLocked(OomAdjuster.OOM_ADJ_REASON_START_RECEIVER);

        // Tell the application to launch this receiver.
        maybeReportBroadcastDispatchedEventLocked(r);
        maybeReportBroadcastDispatchedEventLocked(r, r.curReceiver.applicationInfo.uid);
        r.intent.setComponent(r.curComponent);

        boolean started = false;
@@ -927,7 +927,7 @@ public final class BroadcastQueue {
                r.receiverTime = SystemClock.uptimeMillis();
                maybeAddAllowBackgroundActivityStartsToken(filter.receiverList.app, r);
                maybeScheduleTempAllowlistLocked(filter.owningUid, r, r.options);
                maybeReportBroadcastDispatchedEventLocked(r);
                maybeReportBroadcastDispatchedEventLocked(r, filter.owningUid);
                performReceiveLocked(filter.receiverList.app, filter.receiverList.receiver,
                        new Intent(r.intent), r.resultCode, r.resultData,
                        r.resultExtras, r.ordered, r.initialSticky, r.userId);
@@ -1856,7 +1856,7 @@ public final class BroadcastQueue {
        return null;
    }

    private void maybeReportBroadcastDispatchedEventLocked(BroadcastRecord r) {
    private void maybeReportBroadcastDispatchedEventLocked(BroadcastRecord r, int targetUid) {
        final String targetPackage = getTargetPackage(r);
        // Ignore non-explicit broadcasts
        if (targetPackage == null) {
@@ -1867,11 +1867,10 @@ public final class BroadcastQueue {
        if (r.options == null || r.options.getIdForResponseEvent() <= 0) {
            return;
        }
        // TODO (206518114): Only report this event when the broadcast is dispatched while the app
        // is in the background state.
        getUsageStatsManagerInternal().reportBroadcastDispatched(
                r.callingUid, targetPackage, UserHandle.of(r.userId),
                r.options.getIdForResponseEvent(), SystemClock.elapsedRealtime());
                r.options.getIdForResponseEvent(), SystemClock.elapsedRealtime(),
                mService.getUidStateLocked(targetUid));
    }

    @NonNull
+14 −4
Original line number Diff line number Diff line
@@ -16,6 +16,9 @@

package com.android.server.usage;

import static android.app.ActivityManager.PROCESS_STATE_TOP;
import static android.app.ActivityManager.procStateToString;

import static com.android.server.usage.UsageStatsService.DEBUG_RESPONSE_STATS;

import android.annotation.ElapsedRealtimeLong;
@@ -23,6 +26,7 @@ import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.UserIdInt;
import android.app.ActivityManager.ProcessState;
import android.app.usage.BroadcastResponseStats;
import android.os.UserHandle;
import android.text.TextUtils;
@@ -78,12 +82,18 @@ class BroadcastResponseStatsTracker {
    // TODO (206518114): Move all callbacks handling to a handler thread.
    void reportBroadcastDispatchEvent(int sourceUid, @NonNull String targetPackage,
            UserHandle targetUser, long idForResponseEvent,
            @ElapsedRealtimeLong long timestampMs) {
            @ElapsedRealtimeLong long timestampMs, @ProcessState int targetUidProcState) {
        if (DEBUG_RESPONSE_STATS) {
            Slog.d(TAG, TextUtils.formatSimple(
                    "reportBroadcastDispatchEvent; srcUid=%d, tgtPkg=%s, tgtUsr=%d, id=%d, ts=%s",
            Slog.d(TAG, TextUtils.formatSimple("reportBroadcastDispatchEvent; "
                            + "srcUid=%d, tgtPkg=%s, tgtUsr=%d, id=%d, ts=%s, state=%s",
                    sourceUid, targetPackage, targetUser, idForResponseEvent,
                    TimeUtils.formatDuration(timestampMs)));
                    TimeUtils.formatDuration(timestampMs), procStateToString(targetUidProcState)));
        }
        // TODO (206518114): Make the fg threshold state configurable.
        if (targetUidProcState <= PROCESS_STATE_TOP) {
            // No need to track the broadcast response state while the target app is
            // in the foreground.
            return;
        }
        synchronized (mLock) {
            final LongSparseArray<BroadcastEvent> broadcastEvents =
+3 −2
Original line number Diff line number Diff line
@@ -42,6 +42,7 @@ import android.annotation.Nullable;
import android.annotation.SuppressLint;
import android.annotation.UserIdInt;
import android.app.ActivityManager;
import android.app.ActivityManager.ProcessState;
import android.app.AppOpsManager;
import android.app.IUidObserver;
import android.app.PendingIntent;
@@ -3042,9 +3043,9 @@ public class UsageStatsService extends SystemService implements
        @Override
        public void reportBroadcastDispatched(int sourceUid, @NonNull String targetPackage,
                @NonNull UserHandle targetUser, long idForResponseEvent,
                @ElapsedRealtimeLong long timestampMs) {
                @ElapsedRealtimeLong long timestampMs, @ProcessState int targetUidProcState) {
            mResponseStatsTracker.reportBroadcastDispatchEvent(sourceUid, targetPackage,
                    targetUser, idForResponseEvent, timestampMs);
                    targetUser, idForResponseEvent, timestampMs, targetUidProcState);
        }

        @Override