Skip to content

Commit cd4e427

Browse files
Mattias LarssonJohan Redestig
authored andcommitted
Hold partial wakelock during shutdown to avoid entering sleep
The ShutdownThread can get suspended while in progress if the device enters sleep by the user pressing the power-key, or if it is started (in sleep) from the BatteryService upon a dead battery notification. If the device is woken up before the battery is drained, the ShutdownThread will resume and finally complete the shutdown, but if not the phone will stay in sleep until the battery level is so low that the power is ruthlessly cut. Change-Id: If64429fd0c98a9136141942be6c337b5c79cf4f1
1 parent 7d9c73f commit cd4e427

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)