Skip to content

Commit 2aaa1ce

Browse files
Jean-Baptiste QueruAndroid Code Review
authored andcommitted
Merge "Hold partial wakelock during shutdown to avoid entering sleep"
2 parents 0e430cc + cd4e427 commit 2aaa1ce

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

core/java/com/android/internal/app/ShutdownThread.java

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ public final class ShutdownThread extends Thread {
6767
private boolean mActionDone;
6868
private Context mContext;
6969
private PowerManager mPowerManager;
70-
private PowerManager.WakeLock mWakeLock;
70+
private PowerManager.WakeLock mCpuWakeLock;
71+
private PowerManager.WakeLock mScreenWakeLock;
7172
private Handler mHandler;
7273

7374
private ShutdownThread() {
@@ -155,20 +156,36 @@ private static void beginShutdownSequence(Context context) {
155156

156157
pd.show();
157158

158-
// start the thread that initiates shutdown
159159
sInstance.mContext = context;
160160
sInstance.mPowerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
161-
sInstance.mWakeLock = null;
161+
162+
// make sure we never fall asleep again
163+
sInstance.mCpuWakeLock = null;
164+
try {
165+
sInstance.mCpuWakeLock = sInstance.mPowerManager.newWakeLock(
166+
PowerManager.PARTIAL_WAKE_LOCK, TAG + "-cpu");
167+
sInstance.mCpuWakeLock.setReferenceCounted(false);
168+
sInstance.mCpuWakeLock.acquire();
169+
} catch (SecurityException e) {
170+
Log.w(TAG, "No permission to acquire wake lock", e);
171+
sInstance.mCpuWakeLock = null;
172+
}
173+
174+
// also make sure the screen stays on for better user experience
175+
sInstance.mScreenWakeLock = null;
162176
if (sInstance.mPowerManager.isScreenOn()) {
163177
try {
164-
sInstance.mWakeLock = sInstance.mPowerManager.newWakeLock(
165-
PowerManager.FULL_WAKE_LOCK, "Shutdown");
166-
sInstance.mWakeLock.acquire();
178+
sInstance.mScreenWakeLock = sInstance.mPowerManager.newWakeLock(
179+
PowerManager.FULL_WAKE_LOCK, TAG + "-screen");
180+
sInstance.mScreenWakeLock.setReferenceCounted(false);
181+
sInstance.mScreenWakeLock.acquire();
167182
} catch (SecurityException e) {
168183
Log.w(TAG, "No permission to acquire wake lock", e);
169-
sInstance.mWakeLock = null;
184+
sInstance.mScreenWakeLock = null;
170185
}
171186
}
187+
188+
// start the thread that initiates shutdown
172189
sInstance.mHandler = new Handler() {
173190
};
174191
sInstance.start();

0 commit comments

Comments
 (0)