From cd4e4279804288a941c308b88df8bafa4f3f7458 Mon Sep 17 00:00:00 2001 From: Mattias Larsson Date: Tue, 28 Sep 2010 14:34:15 +0200 Subject: 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 --- .../com/android/internal/app/ShutdownThread.java | 31 +++++++++++++++++----- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/core/java/com/android/internal/app/ShutdownThread.java b/core/java/com/android/internal/app/ShutdownThread.java index e35edc3400b0..37d98beb41d7 100644 --- a/core/java/com/android/internal/app/ShutdownThread.java +++ b/core/java/com/android/internal/app/ShutdownThread.java @@ -67,7 +67,8 @@ public final class ShutdownThread extends Thread { private boolean mActionDone; private Context mContext; private PowerManager mPowerManager; - private PowerManager.WakeLock mWakeLock; + private PowerManager.WakeLock mCpuWakeLock; + private PowerManager.WakeLock mScreenWakeLock; private Handler mHandler; private ShutdownThread() { @@ -155,20 +156,36 @@ public final class ShutdownThread extends Thread { pd.show(); - // start the thread that initiates shutdown sInstance.mContext = context; sInstance.mPowerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE); - sInstance.mWakeLock = null; + + // make sure we never fall asleep again + sInstance.mCpuWakeLock = null; + try { + sInstance.mCpuWakeLock = sInstance.mPowerManager.newWakeLock( + PowerManager.PARTIAL_WAKE_LOCK, TAG + "-cpu"); + sInstance.mCpuWakeLock.setReferenceCounted(false); + sInstance.mCpuWakeLock.acquire(); + } catch (SecurityException e) { + Log.w(TAG, "No permission to acquire wake lock", e); + sInstance.mCpuWakeLock = null; + } + + // also make sure the screen stays on for better user experience + sInstance.mScreenWakeLock = null; if (sInstance.mPowerManager.isScreenOn()) { try { - sInstance.mWakeLock = sInstance.mPowerManager.newWakeLock( - PowerManager.FULL_WAKE_LOCK, "Shutdown"); - sInstance.mWakeLock.acquire(); + sInstance.mScreenWakeLock = sInstance.mPowerManager.newWakeLock( + PowerManager.FULL_WAKE_LOCK, TAG + "-screen"); + sInstance.mScreenWakeLock.setReferenceCounted(false); + sInstance.mScreenWakeLock.acquire(); } catch (SecurityException e) { Log.w(TAG, "No permission to acquire wake lock", e); - sInstance.mWakeLock = null; + sInstance.mScreenWakeLock = null; } } + + // start the thread that initiates shutdown sInstance.mHandler = new Handler() { }; sInstance.start(); -- cgit v1.2.3-59-g8ed1b