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

Commit 651def48 authored by Jeff Brown's avatar Jeff Brown Committed by Android Git Automerger
Browse files

am 3f51958b: Merge "Fix window manager policy state when waking from doze." into lmp-dev

* commit '3f51958b1911ffa49ca3d7eb2a9304348ccbf04e':
  Fix window manager policy state when waking from doze.
parents 47b32c56 55fa0207
Loading
Loading
Loading
Loading
+7 −2
Original line number Original line Diff line number Diff line
@@ -955,14 +955,19 @@ public interface WindowManagerPolicy {
     */
     */
    public void screenTurningOn(ScreenOnListener screenOnListener);
    public void screenTurningOn(ScreenOnListener screenOnListener);


    /**
     * Called when the device has turned the screen off.
     */
    public void screenTurnedOff();

    public interface ScreenOnListener {
    public interface ScreenOnListener {
        void onScreenOn();
        void onScreenOn();
    }
    }


    /**
    /**
     * Return whether the system is awake.
     * Return whether the default display is on and not blocked by a black surface.
     */
     */
    public boolean isAwake();
    public boolean isScreenOn();


    /**
    /**
     * Tell the policy that the lid switch has changed state.
     * Tell the policy that the lid switch has changed state.
+128 −100
Original line number Original line Diff line number Diff line
@@ -199,7 +199,7 @@ public class PhoneWindowManager implements WindowManagerPolicy {
     */
     */
    private WindowState mKeyguardScrim;
    private WindowState mKeyguardScrim;
    private boolean mKeyguardHidden;
    private boolean mKeyguardHidden;
    private boolean mKeyguardDrawn;
    private boolean mKeyguardDrawnOnce;


    /* Table of Application Launch keys.  Maps from key codes to intent categories.
    /* Table of Application Launch keys.  Maps from key codes to intent categories.
     *
     *
@@ -284,10 +284,6 @@ public class PhoneWindowManager implements WindowManagerPolicy {


    boolean mBootMessageNeedsHiding;
    boolean mBootMessageNeedsHiding;
    KeyguardServiceDelegate mKeyguardDelegate;
    KeyguardServiceDelegate mKeyguardDelegate;
    // The following are only accessed on the mHandler thread.
    boolean mKeyguardDrawComplete;
    boolean mWindowManagerDrawComplete;
    ScreenOnListener mScreenOnListener;
    final Runnable mWindowManagerDrawCallback = new Runnable() {
    final Runnable mWindowManagerDrawCallback = new Runnable() {
        @Override
        @Override
        public void run() {
        public void run() {
@@ -351,8 +347,12 @@ public class PhoneWindowManager implements WindowManagerPolicy {
    boolean mLidControlsSleep;
    boolean mLidControlsSleep;
    int mShortPressOnPowerBehavior = -1;
    int mShortPressOnPowerBehavior = -1;
    int mLongPressOnPowerBehavior = -1;
    int mLongPressOnPowerBehavior = -1;
    boolean mAwakeEarly = false;
    boolean mAwake;
    boolean mAwakeFully = false;
    boolean mScreenOnEarly;
    boolean mScreenOnFully;
    ScreenOnListener mScreenOnListener;
    boolean mKeyguardDrawComplete;
    boolean mWindowManagerDrawComplete;
    boolean mOrientationSensorEnabled = false;
    boolean mOrientationSensorEnabled = false;
    int mCurrentAppOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
    int mCurrentAppOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
    boolean mHasSoftInput = false;
    boolean mHasSoftInput = false;
@@ -543,12 +543,10 @@ public class PhoneWindowManager implements WindowManagerPolicy {
    private static final int MSG_KEYGUARD_DRAWN_COMPLETE = 5;
    private static final int MSG_KEYGUARD_DRAWN_COMPLETE = 5;
    private static final int MSG_KEYGUARD_DRAWN_TIMEOUT = 6;
    private static final int MSG_KEYGUARD_DRAWN_TIMEOUT = 6;
    private static final int MSG_WINDOW_MANAGER_DRAWN_COMPLETE = 7;
    private static final int MSG_WINDOW_MANAGER_DRAWN_COMPLETE = 7;
    private static final int MSG_WAKING_UP = 8;
    private static final int MSG_DISPATCH_SHOW_RECENTS = 9;
    private static final int MSG_DISPATCH_SHOW_RECENTS = 9;
    private static final int MSG_DISPATCH_SHOW_GLOBAL_ACTIONS = 10;
    private static final int MSG_DISPATCH_SHOW_GLOBAL_ACTIONS = 10;
    private static final int MSG_HIDE_BOOT_MESSAGE = 11;
    private static final int MSG_HIDE_BOOT_MESSAGE = 11;
    private static final int MSG_LAUNCH_VOICE_ASSIST_WITH_WAKE_LOCK = 12;
    private static final int MSG_LAUNCH_VOICE_ASSIST_WITH_WAKE_LOCK = 12;
    private static final int MSG_SCREEN_TURNING_ON = 13;


    private class PolicyHandler extends Handler {
    private class PolicyHandler extends Handler {
        @Override
        @Override
@@ -584,18 +582,12 @@ public class PhoneWindowManager implements WindowManagerPolicy {
                    if (DEBUG_WAKEUP) Slog.w(TAG, "Setting mWindowManagerDrawComplete");
                    if (DEBUG_WAKEUP) Slog.w(TAG, "Setting mWindowManagerDrawComplete");
                    finishWindowsDrawn();
                    finishWindowsDrawn();
                    break;
                    break;
                case MSG_WAKING_UP:
                    handleWakingUp();
                    break;
                case MSG_HIDE_BOOT_MESSAGE:
                case MSG_HIDE_BOOT_MESSAGE:
                    handleHideBootMessage();
                    handleHideBootMessage();
                    break;
                    break;
                case MSG_LAUNCH_VOICE_ASSIST_WITH_WAKE_LOCK:
                case MSG_LAUNCH_VOICE_ASSIST_WITH_WAKE_LOCK:
                    launchVoiceAssistWithWakeLock(msg.arg1 != 0);
                    launchVoiceAssistWithWakeLock(msg.arg1 != 0);
                    break;
                    break;
                case MSG_SCREEN_TURNING_ON:
                    handleScreenTurningOn((ScreenOnListener)msg.obj);
                    break;
            }
            }
        }
        }
    }
    }
@@ -766,11 +758,11 @@ public class PhoneWindowManager implements WindowManagerPolicy {
        }
        }
        //Could have been invoked due to screen turning on or off or
        //Could have been invoked due to screen turning on or off or
        //change of the currently visible window's orientation
        //change of the currently visible window's orientation
        if (localLOGV) Slog.v(TAG, "Screen status="+mAwakeEarly+
        if (localLOGV) Slog.v(TAG, "mScreenOnEarly=" + mScreenOnEarly
                ", current orientation="+mCurrentAppOrientation+
                + ", mAwake=" + mAwake + ", mCurrentAppOrientation=" + mCurrentAppOrientation
                ", SensorEnabled="+mOrientationSensorEnabled);
                + ", mOrientationSensorEnabled=" + mOrientationSensorEnabled);
        boolean disable = true;
        boolean disable = true;
        if (mAwakeEarly) {
        if (mScreenOnEarly && mAwake) {
            if (needSensorRunningLp()) {
            if (needSensorRunningLp()) {
                disable = false;
                disable = false;
                //enable listener if not already enabled
                //enable listener if not already enabled
@@ -1332,7 +1324,7 @@ public class PhoneWindowManager implements WindowManagerPolicy {
    }
    }


    private boolean shouldEnableWakeGestureLp() {
    private boolean shouldEnableWakeGestureLp() {
        return mWakeGestureEnabledSetting && !mAwakeEarly
        return mWakeGestureEnabledSetting && !mAwake
                && (!mLidControlsSleep || mLidState != LID_CLOSED)
                && (!mLidControlsSleep || mLidState != LID_CLOSED)
                && mWakeGestureListener.isSupported();
                && mWakeGestureListener.isSupported();
    }
    }
@@ -4728,45 +4720,51 @@ public class PhoneWindowManager implements WindowManagerPolicy {
        }
        }
    }
    }


    // Called on the PowerManager's Notifier thread.
    @Override
    @Override
    public void goingToSleep(int why) {
    public void goingToSleep(int why) {
        EventLog.writeEvent(70000, 0);
        EventLog.writeEvent(70000, 0);
        if (DEBUG_WAKEUP) Slog.i(TAG, "Going to sleep...");
        if (DEBUG_WAKEUP) Slog.i(TAG, "Going to sleep...");

        // We must get this work done here because the power manager will drop
        // the wake lock and let the system suspend once this function returns.
        synchronized (mLock) {
        synchronized (mLock) {
            mAwakeEarly = false;
            mAwake = false;
            mAwakeFully = false;
            mKeyguardDrawComplete = false;
        }
        if (mKeyguardDelegate != null) {
            mKeyguardDelegate.onScreenTurnedOff(why);
        }
        synchronized (mLock) {
            updateWakeGestureListenerLp();
            updateWakeGestureListenerLp();
            updateOrientationListenerLp();
            updateOrientationListenerLp();
            updateLockScreenTimeout();
            updateLockScreenTimeout();
        }
        }

        if (mKeyguardDelegate != null) {
            mKeyguardDelegate.onScreenTurnedOff(why);
        }
    }
    }


    // Called on the PowerManager's Notifier thread.
    @Override
    @Override
    public void wakingUp() {
    public void wakingUp() {
        EventLog.writeEvent(70000, 1);
        EventLog.writeEvent(70000, 1);
        if (DEBUG_WAKEUP) Slog.i(TAG, "Waking up...");
        if (DEBUG_WAKEUP) Slog.i(TAG, "Waking up...");
        mHandler.obtainMessage(MSG_WAKING_UP).sendToTarget();
    }


    // Called on the mHandler thread.
        // Since goToSleep performs these functions synchronously, we must
    private void handleWakingUp() {
        // do the same here.  We cannot post this work to a handler because
        // that might cause it to become reordered with respect to what
        // may happen in a future call to goToSleep.
        synchronized (mLock) {
        synchronized (mLock) {
            mAwakeEarly = true;
            mAwake = true;
            mKeyguardDrawComplete = false;
            if (mKeyguardDelegate != null) {
                mHandler.removeMessages(MSG_KEYGUARD_DRAWN_TIMEOUT);
                mHandler.sendEmptyMessageDelayed(MSG_KEYGUARD_DRAWN_TIMEOUT, 1000);
            }

            updateWakeGestureListenerLp();
            updateWakeGestureListenerLp();
            updateOrientationListenerLp();
            updateOrientationListenerLp();
            updateLockScreenTimeout();
            updateLockScreenTimeout();
        }
        }


        mKeyguardDrawComplete = false;
        mWindowManagerDrawComplete = false; // wait for later call to screenTurningOn
        if (mKeyguardDelegate != null) {
        if (mKeyguardDelegate != null) {
            mHandler.removeMessages(MSG_KEYGUARD_DRAWN_TIMEOUT);
            mHandler.sendEmptyMessageDelayed(MSG_KEYGUARD_DRAWN_TIMEOUT, 1000);
            mKeyguardDelegate.onScreenTurnedOn(mKeyguardDelegateCallback);
            mKeyguardDelegate.onScreenTurnedOn(mKeyguardDelegateCallback);
            // ... eventually calls finishKeyguardDrawn
            // ... eventually calls finishKeyguardDrawn
        } else {
        } else {
@@ -4775,94 +4773,130 @@ public class PhoneWindowManager implements WindowManagerPolicy {
        }
        }
    }
    }


    // Called on the mHandler thread.
    private void finishKeyguardDrawn() {
    private void finishKeyguardDrawn() {
        if (!mKeyguardDrawComplete) {
        synchronized (mLock) {
            if (!mAwake || mKeyguardDrawComplete) {
                return; // spurious
            }

            mKeyguardDrawComplete = true;
            mKeyguardDrawComplete = true;
            if (mKeyguardDelegate != null) {
                mHandler.removeMessages(MSG_KEYGUARD_DRAWN_TIMEOUT);
                mHandler.removeMessages(MSG_KEYGUARD_DRAWN_TIMEOUT);
            }
        }

        finishScreenTurningOn();
        finishScreenTurningOn();
    }
    }

    // Called on the DisplayManager's DisplayPowerController thread.
    @Override
    public void screenTurnedOff() {
        if (DEBUG_WAKEUP) Slog.i(TAG, "Screen turned off...");

        synchronized (mLock) {
            mScreenOnEarly = false;
            mScreenOnFully = false;
            mWindowManagerDrawComplete = false;
            mScreenOnListener = null;
            updateOrientationListenerLp();
        }
    }
    }


    // Called on the DisplayManager's DisplayPowerController thread.
    @Override
    @Override
    public void screenTurningOn(final ScreenOnListener screenOnListener) {
    public void screenTurningOn(final ScreenOnListener screenOnListener) {
        EventLog.writeEvent(70000, 1);
        if (DEBUG_WAKEUP) Slog.i(TAG, "Screen turning on...");
        if (DEBUG_WAKEUP) Slog.i(TAG, "Screen turning on...");
        mHandler.obtainMessage(MSG_SCREEN_TURNING_ON, screenOnListener).sendToTarget();
    }


    // Called on the mHandler thread.
        synchronized (mLock) {
    private void handleScreenTurningOn(ScreenOnListener screenOnListener) {
            mScreenOnEarly = true;
            mScreenOnFully = false;
            mWindowManagerDrawComplete = false;
            mScreenOnListener = screenOnListener;
            mScreenOnListener = screenOnListener;
            updateOrientationListenerLp();
        }


        mWindowManagerDrawComplete = false;
        mWindowManagerInternal.waitForAllWindowsDrawn(mWindowManagerDrawCallback,
        mWindowManagerInternal.waitForAllWindowsDrawn(mWindowManagerDrawCallback,
                WAITING_FOR_DRAWN_TIMEOUT);
                WAITING_FOR_DRAWN_TIMEOUT);
        // ... eventually calls finishWindowsDrawn
        // ... eventually calls finishWindowsDrawn
    }
    }


    // Called on the mHandler thread.
    private void finishWindowsDrawn() {
    private void finishWindowsDrawn() {
        if (!mWindowManagerDrawComplete) {
        synchronized (mLock) {
            if (!mScreenOnEarly || mWindowManagerDrawComplete) {
                return; // spurious
            }

            mWindowManagerDrawComplete = true;
            mWindowManagerDrawComplete = true;
            finishScreenTurningOn();
        }
        }

        finishScreenTurningOn();
    }
    }


    // Called on the mHandler thread.
    private void finishScreenTurningOn() {
    private void finishScreenTurningOn() {
        if (DEBUG_WAKEUP) Slog.d(TAG,
        final ScreenOnListener listener;
                "finishScreenTurningOn: mAwakeEarly=" + mAwakeEarly
        final boolean enableScreen;
                        + " mKeyguardDrawComplete=" + mKeyguardDrawComplete
                        + " mWindowManagerDrawComplete=" + mWindowManagerDrawComplete);
        boolean awake;
        synchronized (mLock) {
        synchronized (mLock) {
            if ((mAwakeEarly && !mKeyguardDrawComplete)
            if (DEBUG_WAKEUP) Slog.d(TAG,
                    || !mWindowManagerDrawComplete) {
                    "finishScreenTurningOn: mAwake=" + mAwake
                return;
                            + ", mScreenOnEarly=" + mScreenOnEarly
            }
                            + ", mScreenOnFully=" + mScreenOnFully
                            + ", mKeyguardDrawComplete=" + mKeyguardDrawComplete
                            + ", mWindowManagerDrawComplete=" + mWindowManagerDrawComplete);


            if (mAwakeEarly) {
            if (mScreenOnFully || !mScreenOnEarly || !mWindowManagerDrawComplete
                mAwakeFully = true;
                    || (mAwake && !mKeyguardDrawComplete)) {
            }
                return; // spurious or not ready yet
            awake = mAwakeFully;
            }
            }


            if (DEBUG_WAKEUP) Slog.i(TAG, "Finished screen turning on...");
            if (DEBUG_WAKEUP) Slog.i(TAG, "Finished screen turning on...");

            listener = mScreenOnListener;
        if (mScreenOnListener != null) {
            mScreenOnListener.onScreenOn();
            mScreenOnListener = null;
            mScreenOnListener = null;
        }
            mScreenOnFully = true;

        if (awake) {
            setKeyguardDrawnFirstTime();


            // Remember the first time we draw the keyguard so we know when we're done with
            // the main part of booting and can enable the screen and hide boot messages.
            if (!mKeyguardDrawnOnce && mAwake) {
                mKeyguardDrawnOnce = true;
                enableScreen = true;
                if (mBootMessageNeedsHiding) {
                if (mBootMessageNeedsHiding) {
                handleHideBootMessage();
                    mBootMessageNeedsHiding = false;
                    mBootMessageNeedsHiding = false;
                    hideBootMessages();
                }
                }
            } else {
                enableScreen = false;
            }
            }
        }
        }


    private void handleHideBootMessage() {
        if (listener != null) {
        if (mBootMsgDialog == null) {
            listener.onScreenOn();
            if (DEBUG_WAKEUP) Slog.d(TAG, "handleHideBootMessage: boot message not up");
        }
            return;

        if (enableScreen) {
            try {
                mWindowManager.enableScreenIfNeeded();
            } catch (RemoteException unhandled) {
            }
            }
        if (!mKeyguardDrawComplete || !mWindowManagerDrawComplete) {
        }
            if (DEBUG_WAKEUP) Slog.d(TAG, "handleHideBootMessage: deferring until keyguard ready");
    }

    private void handleHideBootMessage() {
        synchronized (mLock) {
            if (!mKeyguardDrawnOnce) {
                mBootMessageNeedsHiding = true;
                mBootMessageNeedsHiding = true;
            return;
                return; // keyguard hasn't drawn the first time yet, not done booting
            }
            }
        }

        if (mBootMsgDialog != null) {
            if (DEBUG_WAKEUP) Slog.d(TAG, "handleHideBootMessage: dismissing");
            if (DEBUG_WAKEUP) Slog.d(TAG, "handleHideBootMessage: dismissing");
            mBootMsgDialog.dismiss();
            mBootMsgDialog.dismiss();
            mBootMsgDialog = null;
            mBootMsgDialog = null;
        }
        }
    }


    @Override
    @Override
    public boolean isAwake() {
    public boolean isScreenOn() {
        return mAwakeFully;
        return mScreenOnFully;
    }
    }


    /** {@inheritDoc} */
    /** {@inheritDoc} */
@@ -4936,20 +4970,10 @@ public class PhoneWindowManager implements WindowManagerPolicy {
        }
        }
    }
    }


    private void setKeyguardDrawnFirstTime() {
        synchronized (mLock) {
            mKeyguardDrawn = true;
        }
        try {
            mWindowManager.enableScreenIfNeeded();
        } catch (RemoteException unhandled) {
        }
    }

    @Override
    @Override
    public boolean isKeyguardDrawnLw() {
    public boolean isKeyguardDrawnLw() {
        synchronized (mLock) {
        synchronized (mLock) {
            return mKeyguardDrawn;
            return mKeyguardDrawnOnce;
        }
        }
    }
    }


@@ -5380,7 +5404,7 @@ public class PhoneWindowManager implements WindowManagerPolicy {


    private void updateLockScreenTimeout() {
    private void updateLockScreenTimeout() {
        synchronized (mScreenLockTimeout) {
        synchronized (mScreenLockTimeout) {
            boolean enable = (mAllowLockscreenWhenOn && mAwakeEarly &&
            boolean enable = (mAllowLockscreenWhenOn && mAwake &&
                    mKeyguardDelegate != null && mKeyguardDelegate.isSecure());
                    mKeyguardDelegate != null && mKeyguardDelegate.isSecure());
            if (mLockScreenTimerActive != enable) {
            if (mLockScreenTimerActive != enable) {
                if (enable) {
                if (enable) {
@@ -5918,9 +5942,13 @@ public class PhoneWindowManager implements WindowManagerPolicy {
                pw.print("mShortPressOnPowerBehavior="); pw.print(mShortPressOnPowerBehavior);
                pw.print("mShortPressOnPowerBehavior="); pw.print(mShortPressOnPowerBehavior);
                pw.print(" mLongPressOnPowerBehavior="); pw.println(mLongPressOnPowerBehavior);
                pw.print(" mLongPressOnPowerBehavior="); pw.println(mLongPressOnPowerBehavior);
        pw.print(prefix); pw.print("mHasSoftInput="); pw.println(mHasSoftInput);
        pw.print(prefix); pw.print("mHasSoftInput="); pw.println(mHasSoftInput);
        pw.print(prefix); pw.print("mAwakeEarly="); pw.print(mAwakeEarly);
        pw.print(prefix); pw.print("mAwake="); pw.println(mAwake);
                pw.print(" mAwakeFully="); pw.print(mAwakeFully);
        pw.print(prefix); pw.print("mScreenOnEarly="); pw.print(mScreenOnEarly);
                pw.print(" mOrientationSensorEnabled="); pw.println(mOrientationSensorEnabled);
                pw.print(" mScreenOnFully="); pw.println(mScreenOnFully);
        pw.print(prefix); pw.print("mKeyguardDrawComplete="); pw.print(mKeyguardDrawComplete);
                pw.print(" mWindowManagerDrawComplete="); pw.println(mWindowManagerDrawComplete);
        pw.print(prefix); pw.print("mOrientationSensorEnabled=");
                pw.println(mOrientationSensorEnabled);
        pw.print(prefix); pw.print("mOverscanScreen=("); pw.print(mOverscanScreenLeft);
        pw.print(prefix); pw.print("mOverscanScreen=("); pw.print(mOverscanScreenLeft);
                pw.print(","); pw.print(mOverscanScreenTop);
                pw.print(","); pw.print(mOverscanScreenTop);
                pw.print(") "); pw.print(mOverscanScreenWidth);
                pw.print(") "); pw.print(mOverscanScreenWidth);
+90 −29

File changed.

Preview size limit exceeded, changes collapsed.

+1 −42
Original line number Original line Diff line number Diff line
@@ -84,7 +84,6 @@ final class Notifier {
    private final IBatteryStats mBatteryStats;
    private final IBatteryStats mBatteryStats;
    private final IAppOpsService mAppOps;
    private final IAppOpsService mAppOps;
    private final SuspendBlocker mSuspendBlocker;
    private final SuspendBlocker mSuspendBlocker;
    private final ScreenOnBlocker mScreenOnBlocker;
    private final WindowManagerPolicy mPolicy;
    private final WindowManagerPolicy mPolicy;
    private final ActivityManagerInternal mActivityManagerInternal;
    private final ActivityManagerInternal mActivityManagerInternal;
    private final InputManagerInternal mInputManagerInternal;
    private final InputManagerInternal mInputManagerInternal;
@@ -110,18 +109,13 @@ final class Notifier {
    // True if a user activity message should be sent.
    // True if a user activity message should be sent.
    private boolean mUserActivityPending;
    private boolean mUserActivityPending;


    // The currently active screen on listener. This field is non-null whenever the
    // ScreenOnBlocker has been acquired and we are awaiting a callback to release it.
    private ScreenOnUnblocker mPendingScreenOnUnblocker;

    public Notifier(Looper looper, Context context, IBatteryStats batteryStats,
    public Notifier(Looper looper, Context context, IBatteryStats batteryStats,
            IAppOpsService appOps, SuspendBlocker suspendBlocker, ScreenOnBlocker screenOnBlocker,
            IAppOpsService appOps, SuspendBlocker suspendBlocker,
            WindowManagerPolicy policy) {
            WindowManagerPolicy policy) {
        mContext = context;
        mContext = context;
        mBatteryStats = batteryStats;
        mBatteryStats = batteryStats;
        mAppOps = appOps;
        mAppOps = appOps;
        mSuspendBlocker = suspendBlocker;
        mSuspendBlocker = suspendBlocker;
        mScreenOnBlocker = screenOnBlocker;
        mPolicy = policy;
        mPolicy = policy;
        mActivityManagerInternal = LocalServices.getService(ActivityManagerInternal.class);
        mActivityManagerInternal = LocalServices.getService(ActivityManagerInternal.class);
        mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
        mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
@@ -332,21 +326,6 @@ final class Notifier {
        }
        }
    }
    }


    /**
     * Notifies that screen is about to be turned on (any state other than off).
     */
    public void onScreenTurningOn() {
        synchronized (mLock) {
            final ScreenOnUnblocker unblocker = blockScreenOnLocked();
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    mPolicy.screenTurningOn(unblocker);
                }
            });
        }
    }

    /**
    /**
     * Called when there has been user activity.
     * Called when there has been user activity.
     */
     */
@@ -470,26 +449,6 @@ final class Notifier {
        }
        }
    }
    }


    private ScreenOnUnblocker blockScreenOnLocked() {
        if (mPendingScreenOnUnblocker == null) {
            mScreenOnBlocker.acquire();
        }
        mPendingScreenOnUnblocker = new ScreenOnUnblocker();
        return mPendingScreenOnUnblocker;
    }

    private final class ScreenOnUnblocker implements WindowManagerPolicy.ScreenOnListener {
        @Override
        public void onScreenOn() {
            synchronized (mLock) {
                if (mPendingScreenOnUnblocker == this) {
                    mPendingScreenOnUnblocker = null;
                    mScreenOnBlocker.release();
                }
            }
        }
    }

    private final BroadcastReceiver mWakeUpBroadcastDone = new BroadcastReceiver() {
    private final BroadcastReceiver mWakeUpBroadcastDone = new BroadcastReceiver() {
        @Override
        @Override
        public void onReceive(Context context, Intent intent) {
        public void onReceive(Context context, Intent intent) {
+3 −93
Original line number Original line Diff line number Diff line
@@ -88,8 +88,6 @@ public final class PowerManagerService extends SystemService
    private static final int MSG_USER_ACTIVITY_TIMEOUT = 1;
    private static final int MSG_USER_ACTIVITY_TIMEOUT = 1;
    // Message: Sent when the device enters or exits a dreaming or dozing state.
    // Message: Sent when the device enters or exits a dreaming or dozing state.
    private static final int MSG_SANDMAN = 2;
    private static final int MSG_SANDMAN = 2;
    // Message: Sent when the screen on blocker is released.
    private static final int MSG_SCREEN_ON_BLOCKER_RELEASED = 3;


    // Dirty bit: mWakeLocks changed
    // Dirty bit: mWakeLocks changed
    private static final int DIRTY_WAKE_LOCKS = 1 << 0;
    private static final int DIRTY_WAKE_LOCKS = 1 << 0;
@@ -111,10 +109,8 @@ public final class PowerManagerService extends SystemService
    private static final int DIRTY_BATTERY_STATE = 1 << 8;
    private static final int DIRTY_BATTERY_STATE = 1 << 8;
    // Dirty bit: proximity state changed
    // Dirty bit: proximity state changed
    private static final int DIRTY_PROXIMITY_POSITIVE = 1 << 9;
    private static final int DIRTY_PROXIMITY_POSITIVE = 1 << 9;
    // Dirty bit: screen on blocker state became held or unheld
    private static final int DIRTY_SCREEN_ON_BLOCKER_RELEASED = 1 << 10;
    // Dirty bit: dock state changed
    // Dirty bit: dock state changed
    private static final int DIRTY_DOCK_STATE = 1 << 11;
    private static final int DIRTY_DOCK_STATE = 1 << 10;


    // Wakefulness: The device is asleep and can only be awoken by a call to wakeUp().
    // Wakefulness: The device is asleep and can only be awoken by a call to wakeUp().
    // The screen should be off or in the process of being turned off by the display controller.
    // The screen should be off or in the process of being turned off by the display controller.
@@ -244,10 +240,6 @@ public final class PowerManagerService extends SystemService
    // True if the display suspend blocker has been acquired.
    // True if the display suspend blocker has been acquired.
    private boolean mHoldingDisplaySuspendBlocker;
    private boolean mHoldingDisplaySuspendBlocker;


    // The screen on blocker used to keep the screen from turning on while the lock
    // screen is coming up.
    private final ScreenOnBlockerImpl mScreenOnBlocker;

    // True if systemReady() has been called.
    // True if systemReady() has been called.
    private boolean mSystemReady;
    private boolean mSystemReady;


@@ -451,7 +443,6 @@ public final class PowerManagerService extends SystemService
            mHalAutoSuspendModeEnabled = false;
            mHalAutoSuspendModeEnabled = false;
            mHalInteractiveModeEnabled = true;
            mHalInteractiveModeEnabled = true;


            mScreenOnBlocker = new ScreenOnBlockerImpl();
            mWakefulness = WAKEFULNESS_AWAKE;
            mWakefulness = WAKEFULNESS_AWAKE;
            mInteractive = true;
            mInteractive = true;


@@ -505,7 +496,7 @@ public final class PowerManagerService extends SystemService
            mBatteryStats = BatteryStatsService.getService();
            mBatteryStats = BatteryStatsService.getService();
            mNotifier = new Notifier(Looper.getMainLooper(), mContext, mBatteryStats,
            mNotifier = new Notifier(Looper.getMainLooper(), mContext, mBatteryStats,
                    mAppOps, createSuspendBlockerLocked("PowerManagerService.Broadcasts"),
                    mAppOps, createSuspendBlockerLocked("PowerManagerService.Broadcasts"),
                    mScreenOnBlocker, mPolicy);
                    mPolicy);


            mWirelessChargerDetector = new WirelessChargerDetector(sensorManager,
            mWirelessChargerDetector = new WirelessChargerDetector(sensorManager,
                    createSuspendBlockerLocked("PowerManagerService.WirelessChargerDetector"),
                    createSuspendBlockerLocked("PowerManagerService.WirelessChargerDetector"),
@@ -1745,13 +1736,6 @@ public final class PowerManagerService extends SystemService
        return true;
        return true;
    }
    }


    private void handleScreenOnBlockerReleased() {
        synchronized (mLock) {
            mDirty |= DIRTY_SCREEN_ON_BLOCKER_RELEASED;
            updatePowerStateLocked();
        }
    }

    /**
    /**
     * Updates the display power state asynchronously.
     * Updates the display power state asynchronously.
     * When the update is finished, mDisplayReady will be set to true.  The display
     * When the update is finished, mDisplayReady will be set to true.  The display
@@ -1766,8 +1750,7 @@ public final class PowerManagerService extends SystemService
        final boolean oldDisplayReady = mDisplayReady;
        final boolean oldDisplayReady = mDisplayReady;
        if ((dirty & (DIRTY_WAKE_LOCKS | DIRTY_USER_ACTIVITY | DIRTY_WAKEFULNESS
        if ((dirty & (DIRTY_WAKE_LOCKS | DIRTY_USER_ACTIVITY | DIRTY_WAKEFULNESS
                | DIRTY_ACTUAL_DISPLAY_POWER_STATE_UPDATED | DIRTY_BOOT_COMPLETED
                | DIRTY_ACTUAL_DISPLAY_POWER_STATE_UPDATED | DIRTY_BOOT_COMPLETED
                | DIRTY_SETTINGS | DIRTY_SCREEN_ON_BLOCKER_RELEASED)) != 0) {
                | DIRTY_SETTINGS)) != 0) {
            boolean wasBlockerNeeded = isScreenOnBlockerNeededLocked(mDisplayPowerRequest);
            mDisplayPowerRequest.policy = getDesiredScreenPolicyLocked();
            mDisplayPowerRequest.policy = getDesiredScreenPolicyLocked();


            int screenBrightness = mScreenBrightnessSettingDefault;
            int screenBrightness = mScreenBrightnessSettingDefault;
@@ -1815,12 +1798,6 @@ public final class PowerManagerService extends SystemService
                mDisplayPowerRequest.dozeScreenBrightness = PowerManager.BRIGHTNESS_DEFAULT;
                mDisplayPowerRequest.dozeScreenBrightness = PowerManager.BRIGHTNESS_DEFAULT;
            }
            }


            if (!wasBlockerNeeded && isScreenOnBlockerNeededLocked(mDisplayPowerRequest)
                    && !mScreenOnBlocker.isHeld()) {
                mNotifier.onScreenTurningOn();
            }
            mDisplayPowerRequest.blockScreenOn = mScreenOnBlocker.isHeld();

            mDisplayReady = mDisplayManagerInternal.requestPowerState(mDisplayPowerRequest,
            mDisplayReady = mDisplayManagerInternal.requestPowerState(mDisplayPowerRequest,
                    mRequestWaitForNegativeProximity);
                    mRequestWaitForNegativeProximity);
            mRequestWaitForNegativeProximity = false;
            mRequestWaitForNegativeProximity = false;
@@ -1837,17 +1814,6 @@ public final class PowerManagerService extends SystemService
        return mDisplayReady && !oldDisplayReady;
        return mDisplayReady && !oldDisplayReady;
    }
    }


    private static boolean isScreenOnBlockerNeededLocked(DisplayPowerRequest req) {
        switch (req.policy) {
            case DisplayPowerRequest.POLICY_OFF:
                return false;
            case DisplayPowerRequest.POLICY_DOZE:
                return req.dozeScreenState != Display.STATE_OFF;
            default:
                return true;
        }
    }

    private static boolean isValidBrightness(int value) {
    private static boolean isValidBrightness(int value) {
        return value >= 0 && value <= 255;
        return value >= 0 && value <= 255;
    }
    }
@@ -2419,9 +2385,6 @@ public final class PowerManagerService extends SystemService
                pw.println("  " + sb);
                pw.println("  " + sb);
            }
            }


            pw.println();
            pw.println("Screen On Blocker: " + mScreenOnBlocker);

            pw.println();
            pw.println();
            pw.println("Display Power: " + mDisplayPowerCallbacks);
            pw.println("Display Power: " + mDisplayPowerCallbacks);


@@ -2530,9 +2493,6 @@ public final class PowerManagerService extends SystemService
                case MSG_SANDMAN:
                case MSG_SANDMAN:
                    handleSandman();
                    handleSandman();
                    break;
                    break;
                case MSG_SCREEN_ON_BLOCKER_RELEASED:
                    handleScreenOnBlockerReleased();
                    break;
            }
            }
        }
        }
    }
    }
@@ -2709,56 +2669,6 @@ public final class PowerManagerService extends SystemService
        }
        }
    }
    }


    private final class ScreenOnBlockerImpl implements ScreenOnBlocker {
        private static final String TRACE_NAME = "ScreenOnBlocker";

        private int mNestCount;

        public boolean isHeld() {
            synchronized (this) {
                return mNestCount != 0;
            }
        }

        @Override
        public void acquire() {
            synchronized (this) {
                mNestCount += 1;
                if (DEBUG || true) {
                    Slog.d(TAG, "Screen on blocked: mNestCount=" + mNestCount);
                }
                if (mNestCount == 1) {
                    Trace.asyncTraceBegin(Trace.TRACE_TAG_POWER, TRACE_NAME, 0);
                }
            }
        }

        @Override
        public void release() {
            synchronized (this) {
                mNestCount -= 1;
                if (mNestCount == 0) {
                    if (DEBUG || true) {
                        Slog.d(TAG, "Screen on unblocked: mNestCount=" + mNestCount);
                    }
                    mHandler.sendEmptyMessage(MSG_SCREEN_ON_BLOCKER_RELEASED);
                    Trace.asyncTraceEnd(Trace.TRACE_TAG_POWER, TRACE_NAME, 0);
                } else if (mNestCount < 0) {
                    Slog.wtf(TAG, "Screen on blocker was released without being acquired!",
                            new Throwable());
                    mNestCount = 0;
                }
            }
        }

        @Override
        public String toString() {
            synchronized (this) {
                return "held=" + (mNestCount != 0) + ", mNestCount=" + mNestCount;
            }
        }
    }

    private final class BinderService extends IPowerManager.Stub {
    private final class BinderService extends IPowerManager.Stub {
        @Override // Binder call
        @Override // Binder call
        public void acquireWakeLockWithUid(IBinder lock, int flags, String tag,
        public void acquireWakeLockWithUid(IBinder lock, int flags, String tag,
Loading