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

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

Merge changes from topics "fg-threshold-config", "track-bg-state"

* changes:
  Make broadcast response fg threshold state configurable.
  Track broadcast dispatched event only when the app is in the bg.
parents 7e968758 918d7243
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@ package com.android.server.usage;
import android.annotation.CurrentTimeMillisLong;
import android.annotation.NonNull;
import android.annotation.UserIdInt;
import android.app.ActivityManager.ProcessState;
import android.app.usage.AppStandbyInfo;
import android.app.usage.UsageStatsManager.ForcedReasons;
import android.app.usage.UsageStatsManager.StandbyBuckets;
@@ -223,4 +224,12 @@ public interface AppStandbyInternal {
     * a broadcast.
     */
    long getBroadcastResponseWindowDurationMs();

    /**
     * Returns the process state threshold that should be used for deciding whether or not an app
     * is in the background in the context of recording broadcast response stats. Apps whose
     * process state is higher than this threshold state should be considered to be in background.
     */
    @ProcessState
    int getBroadcastResponseFgThresholdState();
}
+26 −0
Original line number Diff line number Diff line
@@ -355,6 +355,14 @@ public class AppStandbyController
    volatile long mBroadcastResponseWindowDurationMillis =
            ConstantsObserver.DEFAULT_BROADCAST_RESPONSE_WINDOW_DURATION_MS;

    /**
     * Process state threshold that is used for deciding whether or not an app is in the background
     * in the context of recording broadcast response stats. Apps whose process state is higher
     * than this threshold state will be considered to be in background.
     */
    volatile int mBroadcastResponseFgThresholdState =
            ConstantsObserver.DEFAULT_BROADCAST_RESPONSE_FG_THRESHOLD_STATE;

    /**
     * Whether we should allow apps into the
     * {@link android.app.usage.UsageStatsManager#STANDBY_BUCKET_RESTRICTED} bucket or not.
@@ -1787,6 +1795,11 @@ public class AppStandbyController
        return mBroadcastResponseWindowDurationMillis;
    }

    @Override
    public int getBroadcastResponseFgThresholdState() {
        return mBroadcastResponseFgThresholdState;
    }

    @Override
    public void flushToDisk() {
        synchronized (mAppIdleLock) {
@@ -2058,6 +2071,10 @@ public class AppStandbyController
        TimeUtils.formatDuration(mBroadcastResponseWindowDurationMillis, pw);
        pw.println();

        pw.print("  mBroadcastResponseFgThresholdState=");
        pw.print(ActivityManager.procStateToString(mBroadcastResponseFgThresholdState));
        pw.println();

        pw.println();
        pw.print("mAppIdleEnabled="); pw.print(mAppIdleEnabled);
        pw.print(" mAllowRestrictedBucket=");
@@ -2491,6 +2508,8 @@ public class AppStandbyController
        };
        private static final String KEY_BROADCAST_RESPONSE_WINDOW_DURATION_MS =
                "broadcast_response_window_timeout_ms";
        private static final String KEY_BROADCAST_RESPONSE_FG_THRESHOLD_STATE =
                "broadcast_response_fg_threshold_state";
        public static final long DEFAULT_CHECK_IDLE_INTERVAL_MS =
                COMPRESS_TIME ? ONE_MINUTE : 4 * ONE_HOUR;
        public static final long DEFAULT_STRONG_USAGE_TIMEOUT =
@@ -2522,6 +2541,8 @@ public class AppStandbyController
        public static final boolean DEFAULT_CROSS_PROFILE_APPS_SHARE_STANDBY_BUCKETS = true;
        public static final long DEFAULT_BROADCAST_RESPONSE_WINDOW_DURATION_MS =
                2 * ONE_MINUTE;
        public static final int DEFAULT_BROADCAST_RESPONSE_FG_THRESHOLD_STATE =
                ActivityManager.PROCESS_STATE_TOP;

        ConstantsObserver(Handler handler) {
            super(handler);
@@ -2644,6 +2665,11 @@ public class AppStandbyController
                                    KEY_BROADCAST_RESPONSE_WINDOW_DURATION_MS,
                                    DEFAULT_BROADCAST_RESPONSE_WINDOW_DURATION_MS);
                            break;
                        case KEY_BROADCAST_RESPONSE_FG_THRESHOLD_STATE:
                            mBroadcastResponseFgThresholdState = properties.getInt(
                                    KEY_BROADCAST_RESPONSE_FG_THRESHOLD_STATE,
                                    DEFAULT_BROADCAST_RESPONSE_FG_THRESHOLD_STATE);
                            break;
                        default:
                            if (!timeThresholdsUpdated
                                    && (name.startsWith(KEY_PREFIX_SCREEN_TIME_THRESHOLD)
+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
+12 −4
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

package com.android.server.usage;

import static android.app.ActivityManager.procStateToString;

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

import android.annotation.ElapsedRealtimeLong;
@@ -23,6 +25,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 +81,17 @@ 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)));
        }
        if (targetUidProcState <= mAppStandby.getBroadcastResponseFgThresholdState()) {
            // No need to track the broadcast response state while the target app is
            // in the foreground.
            return;
        }
        synchronized (mLock) {
            final LongSparseArray<BroadcastEvent> broadcastEvents =
Loading