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

Commit b5cbecb1 authored by Santos Cordon's avatar Santos Cordon Committed by Craig Donner
Browse files

Add keyguard state as a gate for VR Mode.

Problem: When the user toggles power button while in VR, they will be
taken to a black VR screen that occludes the keyguard.

VRManager previously used wakefulness and screen off as gates for VR Mode. However,
the keyguard state can be on or off when the device is awake so we need
to listen to the keyguard state as well.

Change:
1) Have ActivityManagerService tell VrManagerService about the keyguard
state the same as it does with wakefulness.
2) Stop applying any pending states when the device comes out of
sleep/screen-off/keyguard to force user into putting-headset-on (DON) UX
in those situations.

Bug: 66696304
Test: Manual - toggle device's power button and see logs of keyguard.
Test with all keyguard types.

Change-Id: I4ae03cb1c8c13acdc8cd11839f178bc8d8f6f515
parent a2fbb123
Loading
Loading
Loading
Loading
+18 −0
Original line number Diff line number Diff line
@@ -1691,6 +1691,7 @@ public class ActivityManagerService extends IActivityManager.Stub
    static final int DISPATCH_OOM_ADJ_OBSERVER_MSG = 70;
    static final int START_USER_SWITCH_FG_MSG = 712;
    static final int TOP_APP_KILLED_BY_LMK_MSG = 73;
    static final int NOTIFY_VR_KEYGUARD_MSG = 74;
    static final int FIRST_ACTIVITY_STACK_MSG = 100;
    static final int FIRST_BROADCAST_QUEUE_MSG = 200;
@@ -2426,6 +2427,9 @@ public class ActivityManagerService extends IActivityManager.Stub
            case NOTIFY_VR_SLEEPING_MSG: {
                notifyVrManagerOfSleepState(msg.arg1 != 0);
            } break;
            case NOTIFY_VR_KEYGUARD_MSG: {
                notifyVrManagerOfKeyguardState(msg.arg1 != 0);
            } break;
            case HANDLE_TRUST_STORAGE_UPDATE_MSG: {
                synchronized (ActivityManagerService.this) {
                    for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) {
@@ -3281,6 +3285,19 @@ public class ActivityManagerService extends IActivityManager.Stub
        vrService.onSleepStateChanged(isSleeping);
    }
    private void sendNotifyVrManagerOfKeyguardState(boolean isShowing) {
        mHandler.sendMessage(
                mHandler.obtainMessage(NOTIFY_VR_KEYGUARD_MSG, isShowing ? 1 : 0, 0));
    }
    private void notifyVrManagerOfKeyguardState(boolean isShowing) {
        final VrManagerInternal vrService = LocalServices.getService(VrManagerInternal.class);
        if (vrService == null) {
            return;
        }
        vrService.onKeyguardStateChanged(isShowing);
    }
    final void showAskCompatModeDialogLocked(ActivityRecord r) {
        Message msg = Message.obtain();
        msg.what = SHOW_COMPAT_MODE_DIALOG_UI_MSG;
@@ -12653,6 +12670,7 @@ public class ActivityManagerService extends IActivityManager.Stub
                Binder.restoreCallingIdentity(ident);
            }
        }
        sendNotifyVrManagerOfKeyguardState(showing);
    }
    @Override
+7 −0
Original line number Diff line number Diff line
@@ -73,6 +73,13 @@ public abstract class VrManagerInternal {
     */
    public abstract void onScreenStateChanged(boolean isScreenOn);

    /**
     * Set whether the keyguard is currently active/showing.
     *
     * @param isShowing is {@code true} if the keyguard is active/showing.
     */
    public abstract void onKeyguardStateChanged(boolean isShowing);

    /**
     * Return NO_ERROR if the given package is installed on the device and enabled as a
     * VrListenerService for the given current user, or a negative error code indicating a failure.
+37 −19
Original line number Diff line number Diff line
@@ -115,11 +115,13 @@ public class VrManagerService extends SystemService implements EnabledComponentC
    /** Null set of sleep sleep flags. */
    private static final int FLAG_NONE = 0;
    /** Flag set when the device is not sleeping. */
    private static final int FLAG_AWAKE = 1;
    private static final int FLAG_AWAKE = 1 << 0;
    /** Flag set when the screen has been turned on. */
    private static final int FLAG_SCREEN_ON = 2;
    private static final int FLAG_SCREEN_ON = 1 << 1;
    /** Flag set when the keyguard is not active. */
    private static final int FLAG_KEYGUARD_UNLOCKED = 1 << 2;
    /** Flag indicating that all system sleep flags have been set.*/
    private static final int FLAG_ALL = FLAG_AWAKE | FLAG_SCREEN_ON;
    private static final int FLAG_ALL = FLAG_AWAKE | FLAG_SCREEN_ON | FLAG_KEYGUARD_UNLOCKED;

    private static native void initializeNative();
    private static native void setVrModeNative(boolean enabled);
@@ -155,10 +157,11 @@ public class VrManagerService extends SystemService implements EnabledComponentC
    private final NotificationAccessManager mNotifAccessManager = new NotificationAccessManager();
    private INotificationManager mNotificationManager;
    /** Tracks the state of the screen and keyguard UI.*/
    private int mSystemSleepFlags = FLAG_AWAKE;
    private int mSystemSleepFlags = FLAG_AWAKE | FLAG_KEYGUARD_UNLOCKED;
    /**
     * Set when ACTION_USER_UNLOCKED is fired. We shouldn't try to bind to the
     * vr service before then.
     * vr service before then. This gets set only once the first time the user unlocks the device
     * and stays true thereafter.
     */
    private boolean mUserUnlocked;
    private Vr2dDisplay mVr2dDisplay;
@@ -208,7 +211,6 @@ public class VrManagerService extends SystemService implements EnabledComponentC
                if (mBootsToVr) {
                    setPersistentVrModeEnabled(true);
                }
                consumeAndApplyPendingStateLocked();
                if (mBootsToVr && !mVrModeEnabled) {
                  setVrMode(true, mDefaultVrService, 0, -1, null);
                }
@@ -230,28 +232,39 @@ public class VrManagerService extends SystemService implements EnabledComponentC
    }

    private void setSleepState(boolean isAsleep) {
        synchronized(mLock) {

            if (!isAsleep) {
                mSystemSleepFlags |= FLAG_AWAKE;
            } else {
                mSystemSleepFlags &= ~FLAG_AWAKE;
        setSystemState(FLAG_AWAKE, !isAsleep);
    }

            updateVrModeAllowedLocked();
    private void setScreenOn(boolean isScreenOn) {
        setSystemState(FLAG_SCREEN_ON, isScreenOn);
    }

    private void setKeyguardShowing(boolean isShowing) {
        setSystemState(FLAG_KEYGUARD_UNLOCKED, !isShowing);
    }

    private void setScreenOn(boolean isScreenOn) {
    private void setSystemState(int flags, boolean isOn) {
        synchronized(mLock) {
            if (isScreenOn) {
                mSystemSleepFlags |= FLAG_SCREEN_ON;
            int oldState = mSystemSleepFlags;
            if (isOn) {
                mSystemSleepFlags |= flags;
            } else {
                mSystemSleepFlags &= ~FLAG_SCREEN_ON;
                mSystemSleepFlags &= ~flags;
            }
            if (oldState != mSystemSleepFlags) {
                if (DBG) Slog.d(TAG, "System state: " + getStateAsString());
                updateVrModeAllowedLocked();
            }
        }
    }

    private String getStateAsString() {
        return new StringBuilder()
                .append((mSystemSleepFlags & FLAG_AWAKE) != 0 ? "awake, " : "")
                .append((mSystemSleepFlags & FLAG_SCREEN_ON) != 0 ? "screen_on, " : "")
                .append((mSystemSleepFlags & FLAG_KEYGUARD_UNLOCKED) != 0 ? "keyguard_off" : "")
                .toString();
    }

    private void setUserUnlocked() {
        synchronized(mLock) {
@@ -671,6 +684,11 @@ public class VrManagerService extends SystemService implements EnabledComponentC
            VrManagerService.this.setScreenOn(isScreenOn);
        }

        @Override
        public void onKeyguardStateChanged(boolean isShowing) {
            VrManagerService.this.setKeyguardShowing(isShowing);
        }

        @Override
        public boolean isCurrentVrListener(String packageName, int userId) {
            return VrManagerService.this.isCurrentVrListener(packageName, userId);