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

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

Merge "new screen session observer for log Related Doc:...

Merge "new screen session observer for log Related Doc: go/telemetry-screen-session-metrics-collection" into main
parents 4c9ddfa6 62812241
Loading
Loading
Loading
Loading
+23 −0
Original line number Diff line number Diff line
@@ -129,6 +129,7 @@ public class Notifier {
    private final WindowManagerPolicy mPolicy;
    private final FaceDownDetector mFaceDownDetector;
    private final ScreenUndimDetector mScreenUndimDetector;
    private final WakefulnessSessionObserver mWakefulnessSessionObserver;
    private final ActivityManagerInternal mActivityManagerInternal;
    private final InputManagerInternal mInputManagerInternal;
    private final InputMethodManagerInternal mInputMethodManagerInternal;
@@ -197,6 +198,7 @@ public class Notifier {
        mPolicy = policy;
        mFaceDownDetector = faceDownDetector;
        mScreenUndimDetector = screenUndimDetector;
        mWakefulnessSessionObserver = new WakefulnessSessionObserver(mContext, null);
        mActivityManagerInternal = LocalServices.getService(ActivityManagerInternal.class);
        mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
        mInputMethodManagerInternal = LocalServices.getService(InputMethodManagerInternal.class);
@@ -286,6 +288,8 @@ public class Notifier {
        }

        mWakeLockLog.onWakeLockAcquired(tag, ownerUid, flags);

        mWakefulnessSessionObserver.onWakeLockAcquired(flags);
    }

    public void onLongPartialWakeLockStart(String tag, int ownerUid, WorkSource workSource,
@@ -386,6 +390,16 @@ public class Notifier {
    public void onWakeLockReleased(int flags, String tag, String packageName,
            int ownerUid, int ownerPid, WorkSource workSource, String historyTag,
            IWakeLockCallback callback) {
        onWakeLockReleased(flags, tag, packageName, ownerUid, ownerPid, workSource, historyTag,
                callback, ScreenTimeoutOverridePolicy.RELEASE_REASON_UNKNOWN);
    }

    /**
     * Called when a wake lock is released.
     */
    public void onWakeLockReleased(int flags, String tag, String packageName,
            int ownerUid, int ownerPid, WorkSource workSource, String historyTag,
            IWakeLockCallback callback, int releaseReason) {
        if (DEBUG) {
            Slog.d(TAG, "onWakeLockReleased: flags=" + flags + ", tag=\"" + tag
                    + "\", packageName=" + packageName
@@ -409,6 +423,8 @@ public class Notifier {
            }
        }
        mWakeLockLog.onWakeLockReleased(tag, ownerUid);

        mWakefulnessSessionObserver.onWakeLockReleased(flags, releaseReason);
    }

    /** Shows the keyguard without requesting the device to immediately lock. */
@@ -670,6 +686,8 @@ public class Notifier {
            interactivity.changeStartTime = eventTime;
            interactivity.isChanging = true;
            handleEarlyInteractiveChange(groupId);
            mWakefulnessSessionObserver.onWakefulnessChangeStarted(groupId, wakefulness,
                    changeReason, eventTime);
        }
    }

@@ -680,6 +698,7 @@ public class Notifier {
     */
    public void onGroupRemoved(int groupId) {
        mInteractivityByGroupId.remove(groupId);
        mWakefulnessSessionObserver.removePowerGroup(groupId);
    }

    /**
@@ -693,6 +712,8 @@ public class Notifier {

        try {
            mBatteryStats.noteUserActivity(uid, event);
            mWakefulnessSessionObserver.notifyUserActivity(
                    SystemClock.uptimeMillis(), displayGroupId, event);
        } catch (RemoteException ex) {
            // Ignore
        }
@@ -798,6 +819,8 @@ public class Notifier {
        if (mWakeLockLog != null) {
            mWakeLockLog.dump(pw);
        }

        mWakefulnessSessionObserver.dump(pw);
    }

    private void updatePendingBroadcastLocked() {
+19 −7
Original line number Diff line number Diff line
@@ -231,7 +231,7 @@ public final class PowerManagerService extends SystemService

    // Default timeout in milliseconds.  This is only used until the settings
    // provider populates the actual default value (R.integer.def_screen_off_timeout).
    private static final int DEFAULT_SCREEN_OFF_TIMEOUT = 15 * 1000;
    static final int DEFAULT_SCREEN_OFF_TIMEOUT = 15 * 1000;
    private static final int DEFAULT_SLEEP_TIMEOUT = -1;

    // Screen brightness boost timeout.
@@ -1417,8 +1417,9 @@ public final class PowerManagerService extends SystemService
            updateSettingsLocked();
            if (mFeatureFlags.isEarlyScreenTimeoutDetectorEnabled()) {
                mScreenTimeoutOverridePolicy = new ScreenTimeoutOverridePolicy(mContext,
                        mMinimumScreenOffTimeoutConfig, () -> {
                        mMinimumScreenOffTimeoutConfig, (releaseReason) -> {
                    Message msg = mHandler.obtainMessage(MSG_RELEASE_ALL_OVERRIDE_WAKE_LOCKS);
                    msg.arg1 = releaseReason;
                    mHandler.sendMessageAtTime(msg, mClock.uptimeMillis());
                });
            }
@@ -1827,6 +1828,12 @@ public final class PowerManagerService extends SystemService

    @GuardedBy("mLock")
    private void removeWakeLockNoUpdateLocked(WakeLock wakeLock, int index) {
        removeWakeLockNoUpdateLocked(wakeLock, index,
                ScreenTimeoutOverridePolicy.RELEASE_REASON_UNKNOWN);
    }

    @GuardedBy("mLock")
    private void removeWakeLockNoUpdateLocked(WakeLock wakeLock, int index, int releaseReason) {
        mWakeLocks.remove(index);
        UidState state = wakeLock.mUidState;
        state.mNumWakeLocks--;
@@ -1835,7 +1842,7 @@ public final class PowerManagerService extends SystemService
            mUidState.remove(state.mUid);
        }

        notifyWakeLockReleasedLocked(wakeLock);
        notifyWakeLockReleasedLocked(wakeLock, releaseReason);
        applyWakeLockFlagsOnReleaseLocked(wakeLock);
        mDirty |= DIRTY_WAKE_LOCKS;
    }
@@ -2001,12 +2008,17 @@ public final class PowerManagerService extends SystemService

    @GuardedBy("mLock")
    private void notifyWakeLockReleasedLocked(WakeLock wakeLock) {
        notifyWakeLockReleasedLocked(wakeLock, ScreenTimeoutOverridePolicy.RELEASE_REASON_UNKNOWN);
    }

    @GuardedBy("mLock")
    private void notifyWakeLockReleasedLocked(WakeLock wakeLock, int releaseReason) {
        if (mSystemReady && wakeLock.mNotifiedAcquired) {
            wakeLock.mNotifiedAcquired = false;
            wakeLock.mAcquireTime = 0;
            mNotifier.onWakeLockReleased(wakeLock.mFlags, wakeLock.mTag,
                    wakeLock.mPackageName, wakeLock.mOwnerUid, wakeLock.mOwnerPid,
                    wakeLock.mWorkSource, wakeLock.mHistoryTag, wakeLock.mCallback);
                    wakeLock.mWorkSource, wakeLock.mHistoryTag, wakeLock.mCallback, releaseReason);
            notifyWakeLockLongFinishedLocked(wakeLock);
        }
    }
@@ -5345,7 +5357,7 @@ public final class PowerManagerService extends SystemService
                    handleAttentiveTimeout();
                    break;
                case MSG_RELEASE_ALL_OVERRIDE_WAKE_LOCKS:
                    releaseAllOverrideWakeLocks();
                    releaseAllOverrideWakeLocks(msg.arg1);
                    break;
            }

@@ -7269,7 +7281,7 @@ public final class PowerManagerService extends SystemService
        return false;
    }

    private void releaseAllOverrideWakeLocks() {
    private void releaseAllOverrideWakeLocks(int releaseReason) {
        synchronized (mLock) {
            final int size = mWakeLocks.size();
            boolean change = false;
@@ -7277,7 +7289,7 @@ public final class PowerManagerService extends SystemService
                final WakeLock wakeLock = mWakeLocks.get(i);
                if ((wakeLock.mFlags & PowerManager.WAKE_LOCK_LEVEL_MASK)
                        == PowerManager.SCREEN_TIMEOUT_OVERRIDE_WAKE_LOCK) {
                    removeWakeLockNoUpdateLocked(wakeLock, i);
                    removeWakeLockNoUpdateLocked(wakeLock, i, releaseReason);
                    change = true;
                }
            }
+58 −9
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import static com.android.server.power.PowerManagerService.WAKE_LOCK_SCREEN_BRIG
import static com.android.server.power.PowerManagerService.WAKE_LOCK_SCREEN_DIM;
import static com.android.server.power.PowerManagerService.WAKE_LOCK_SCREEN_TIMEOUT_OVERRIDE;

import android.annotation.IntDef;
import android.content.Context;
import android.os.PowerManager;
import android.util.IndentingPrintWriter;
@@ -31,6 +32,8 @@ import android.util.Slog;
import com.android.internal.annotations.VisibleForTesting;

import java.io.PrintWriter;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
  * Policy that handle the screen timeout override wake lock behavior.
@@ -50,25 +53,62 @@ final class ScreenTimeoutOverridePolicy {
    public static final int RELEASE_REASON_NON_INTERACTIVE = 1;

    /**
     * Release reason code: Release because user activity occurred.
     * Release reason code: Release because a screen lock is acquired.
     */
    public static final int RELEASE_REASON_USER_ACTIVITY = 2;
    public static final int RELEASE_REASON_SCREEN_LOCK = 2;

    /**
     * Release reason code: Release because a screen lock is acquired.
     * Release reason code: Release because user activity attention occurs.
     */
    public static final int RELEASE_REASON_USER_ACTIVITY_ATTENTION = 3;

    /**
     * Release reason code: Release because user activity other occurs.
     */
    public static final int RELEASE_REASON_USER_ACTIVITY_OTHER = 4;

    /**
     * Release reason code: Release because user activity button occurs.
     */
    public static final int RELEASE_REASON_USER_ACTIVITY_BUTTON = 5;

    /**
     * Release reason code: Release because user activity touch occurs.
     */
    public static final int RELEASE_REASON_SCREEN_LOCK = 3;
    public static final int RELEASE_REASON_USER_ACTIVITY_TOUCH = 6;

    /**
     * Release reason code: Release because user activity accessibility occurs.
     */
    public static final int RELEASE_REASON_USER_ACTIVITY_ACCESSIBILITY = 7;

    /**
     * @hide
     */
    @IntDef(prefix = { "RELEASE_REASON_" }, value = {
            RELEASE_REASON_UNKNOWN,
            RELEASE_REASON_NON_INTERACTIVE,
            RELEASE_REASON_SCREEN_LOCK,
            RELEASE_REASON_USER_ACTIVITY_ATTENTION,
            RELEASE_REASON_USER_ACTIVITY_OTHER,
            RELEASE_REASON_USER_ACTIVITY_BUTTON,
            RELEASE_REASON_USER_ACTIVITY_TOUCH,
            RELEASE_REASON_USER_ACTIVITY_ACCESSIBILITY
    })
    @Retention(RetentionPolicy.SOURCE)
    public @interface ReleaseReason{}

    // The screen timeout override config in milliseconds.
    private long mScreenTimeoutOverrideConfig;

    // The last reason that wake locks had been released by service.
    private int mLastAutoReleaseReason = RELEASE_REASON_UNKNOWN;
    private @ReleaseReason int mLastAutoReleaseReason = RELEASE_REASON_UNKNOWN;

    interface PolicyCallback {
        /**
         * Notify {@link PowerManagerService} to release all override wake locks.
         */
        void releaseAllScreenTimeoutOverrideWakelocks();
        void releaseAllScreenTimeoutOverrideWakelocks(@ReleaseReason int reason);
    }
    private PolicyCallback mPolicyCallback;

@@ -110,11 +150,20 @@ final class ScreenTimeoutOverridePolicy {

        switch (event) {
            case PowerManager.USER_ACTIVITY_EVENT_ATTENTION:
                releaseAllWakeLocks(RELEASE_REASON_USER_ACTIVITY_ATTENTION);
                return;
            case PowerManager.USER_ACTIVITY_EVENT_OTHER:
                releaseAllWakeLocks(RELEASE_REASON_USER_ACTIVITY_OTHER);
                return;
            case PowerManager.USER_ACTIVITY_EVENT_BUTTON:
                releaseAllWakeLocks(RELEASE_REASON_USER_ACTIVITY_BUTTON);
                return;
            case PowerManager.USER_ACTIVITY_EVENT_TOUCH:
                releaseAllWakeLocks(RELEASE_REASON_USER_ACTIVITY_TOUCH);
                return;
            case PowerManager.USER_ACTIVITY_EVENT_ACCESSIBILITY:
                releaseAllWakeLocks(RELEASE_REASON_USER_ACTIVITY);
                releaseAllWakeLocks(RELEASE_REASON_USER_ACTIVITY_ACCESSIBILITY);
                return;
        }
    }

@@ -154,8 +203,8 @@ final class ScreenTimeoutOverridePolicy {
                + " (reason=" + mLastAutoReleaseReason + ")");
    }

    private void releaseAllWakeLocks(int reason) {
        mPolicyCallback.releaseAllScreenTimeoutOverrideWakelocks();
    private void releaseAllWakeLocks(@ReleaseReason int reason) {
        mPolicyCallback.releaseAllScreenTimeoutOverrideWakelocks(reason);
        mLastAutoReleaseReason = reason;
        logReleaseReason();
    }
+595 −0

File added.

Preview size limit exceeded, changes collapsed.

+5 −5
Original line number Diff line number Diff line
@@ -1067,7 +1067,7 @@ public class PowerManagerServiceTest {
            wakelockMap.remove((String) inv.getArguments()[1]);
            return null;
        }).when(mNotifierMock).onWakeLockReleased(anyInt(), anyString(), anyString(), anyInt(),
                anyInt(), any(), any(), any());
                anyInt(), any(), any(), any(), anyInt());

        //
        // TEST STARTS HERE
@@ -2777,7 +2777,7 @@ public class PowerManagerServiceTest {

        mService.getBinderServiceInstance().releaseWakeLock(token, 0);
        verify(mNotifierMock).onWakeLockReleased(anyInt(), eq(tag), eq(packageName), anyInt(),
                anyInt(), any(), any(), same(callback));
                anyInt(), any(), any(), same(callback), anyInt());
    }

    /**
@@ -2796,8 +2796,8 @@ public class PowerManagerServiceTest {
        when(callback1.asBinder()).thenReturn(callbackBinder1);
        mService.getBinderServiceInstance().acquireWakeLock(token, flags, tag, packageName,
                null /* workSource */, null /* historyTag */, Display.INVALID_DISPLAY, callback1);
        verify(mNotifierMock).onWakeLockAcquired(anyInt(), eq(tag), eq(packageName), anyInt(),
                anyInt(), any(), any(), same(callback1));
        verify(mNotifierMock).onWakeLockAcquired(anyInt(), eq(tag), eq(packageName),
                anyInt(), anyInt(), any(), any(), same(callback1));

        final IWakeLockCallback callback2 = Mockito.mock(IWakeLockCallback.class);
        final IBinder callbackBinder2 = Mockito.mock(Binder.class);
@@ -2810,7 +2810,7 @@ public class PowerManagerServiceTest {

        mService.getBinderServiceInstance().releaseWakeLock(token, 0);
        verify(mNotifierMock).onWakeLockReleased(anyInt(), eq(tag), eq(packageName), anyInt(),
                anyInt(), any(), any(), same(callback2));
                anyInt(), any(), any(), same(callback2), anyInt());
    }

    @Test
Loading