Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | |
| 18 | package com.android.internal.app; |
| 19 | |
| 20 | import android.app.ActivityManagerNative; |
| 21 | import android.app.IActivityManager; |
| 22 | import android.app.ProgressDialog; |
| 23 | import android.app.AlertDialog; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 24 | import android.bluetooth.BluetoothAdapter; |
| 25 | import android.bluetooth.IBluetooth; |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 26 | import android.content.BroadcastReceiver; |
| 27 | import android.content.Context; |
| 28 | import android.content.DialogInterface; |
| 29 | import android.content.Intent; |
| 30 | import android.os.Handler; |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 31 | import android.os.Power; |
Dianne Hackborn | f99ae76 | 2010-03-08 12:43:51 -0800 | [diff] [blame] | 32 | import android.os.PowerManager; |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 33 | import android.os.RemoteException; |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 34 | import android.os.ServiceManager; |
| 35 | import android.os.SystemClock; |
Mike Lockwood | a717f64 | 2010-04-01 20:01:44 -0700 | [diff] [blame^] | 36 | import android.os.Vibrator; |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 37 | import android.os.storage.IMountService; |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 38 | import android.os.storage.IMountShutdownObserver; |
Dianne Hackborn | 568cae5 | 2009-10-07 16:13:39 -0700 | [diff] [blame] | 39 | |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 40 | import com.android.internal.telephony.ITelephony; |
| 41 | import android.util.Log; |
| 42 | import android.view.WindowManager; |
| 43 | |
| 44 | public final class ShutdownThread extends Thread { |
| 45 | // constants |
| 46 | private static final String TAG = "ShutdownThread"; |
| 47 | private static final int MAX_NUM_PHONE_STATE_READS = 16; |
| 48 | private static final int PHONE_STATE_POLL_SLEEP_MSEC = 500; |
| 49 | // maximum time we wait for the shutdown broadcast before going on. |
| 50 | private static final int MAX_BROADCAST_TIME = 10*1000; |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 51 | private static final int MAX_SHUTDOWN_WAIT_TIME = 20*1000; |
Mike Lockwood | a717f64 | 2010-04-01 20:01:44 -0700 | [diff] [blame^] | 52 | |
| 53 | // length of vibration before shutting down |
| 54 | private static final int SHUTDOWN_VIBRATE_MS = 500; |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 55 | |
| 56 | // state tracking |
| 57 | private static Object sIsStartedGuard = new Object(); |
| 58 | private static boolean sIsStarted = false; |
| 59 | |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 60 | private static boolean mReboot; |
| 61 | private static String mRebootReason; |
| 62 | |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 63 | // static instance of this thread |
| 64 | private static final ShutdownThread sInstance = new ShutdownThread(); |
| 65 | |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 66 | private final Object mActionDoneSync = new Object(); |
| 67 | private boolean mActionDone; |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 68 | private Context mContext; |
Dianne Hackborn | f99ae76 | 2010-03-08 12:43:51 -0800 | [diff] [blame] | 69 | private PowerManager mPowerManager; |
| 70 | private PowerManager.WakeLock mWakeLock; |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 71 | private Handler mHandler; |
| 72 | |
| 73 | private ShutdownThread() { |
| 74 | } |
| 75 | |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 76 | /** |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 77 | * Request a clean shutdown, waiting for subsystems to clean up their |
| 78 | * state etc. Must be called from a Looper thread in which its UI |
| 79 | * is shown. |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 80 | * |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 81 | * @param context Context used to display the shutdown progress dialog. |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 82 | * @param confirm true if user confirmation is needed before shutting down. |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 83 | */ |
| 84 | public static void shutdown(final Context context, boolean confirm) { |
| 85 | // ensure that only one thread is trying to power down. |
| 86 | // any additional calls are just returned |
| 87 | synchronized (sIsStartedGuard){ |
| 88 | if (sIsStarted) { |
| 89 | Log.d(TAG, "Request to shutdown already running, returning."); |
| 90 | return; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | Log.d(TAG, "Notifying thread to start radio shutdown"); |
| 95 | |
| 96 | if (confirm) { |
| 97 | final AlertDialog dialog = new AlertDialog.Builder(context) |
| 98 | .setIcon(android.R.drawable.ic_dialog_alert) |
| 99 | .setTitle(com.android.internal.R.string.power_off) |
| 100 | .setMessage(com.android.internal.R.string.shutdown_confirm) |
| 101 | .setPositiveButton(com.android.internal.R.string.yes, new DialogInterface.OnClickListener() { |
| 102 | public void onClick(DialogInterface dialog, int which) { |
| 103 | beginShutdownSequence(context); |
| 104 | } |
| 105 | }) |
| 106 | .setNegativeButton(com.android.internal.R.string.no, null) |
| 107 | .create(); |
| 108 | dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG); |
Dianne Hackborn | 568cae5 | 2009-10-07 16:13:39 -0700 | [diff] [blame] | 109 | if (!context.getResources().getBoolean( |
| 110 | com.android.internal.R.bool.config_sf_slowBlur)) { |
| 111 | dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND); |
| 112 | } |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 113 | dialog.show(); |
| 114 | } else { |
| 115 | beginShutdownSequence(context); |
| 116 | } |
| 117 | } |
| 118 | |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 119 | /** |
| 120 | * Request a clean shutdown, waiting for subsystems to clean up their |
| 121 | * state etc. Must be called from a Looper thread in which its UI |
| 122 | * is shown. |
| 123 | * |
| 124 | * @param context Context used to display the shutdown progress dialog. |
| 125 | * @param reason code to pass to the kernel (e.g. "recovery"), or null. |
| 126 | * @param confirm true if user confirmation is needed before shutting down. |
| 127 | */ |
| 128 | public static void reboot(final Context context, String reason, boolean confirm) { |
| 129 | mReboot = true; |
| 130 | mRebootReason = reason; |
| 131 | shutdown(context, confirm); |
| 132 | } |
| 133 | |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 134 | private static void beginShutdownSequence(Context context) { |
| 135 | synchronized (sIsStartedGuard) { |
| 136 | sIsStarted = true; |
| 137 | } |
| 138 | |
| 139 | // throw up an indeterminate system dialog to indicate radio is |
| 140 | // shutting down. |
| 141 | ProgressDialog pd = new ProgressDialog(context); |
| 142 | pd.setTitle(context.getText(com.android.internal.R.string.power_off)); |
| 143 | pd.setMessage(context.getText(com.android.internal.R.string.shutdown_progress)); |
| 144 | pd.setIndeterminate(true); |
| 145 | pd.setCancelable(false); |
| 146 | pd.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG); |
Dianne Hackborn | 568cae5 | 2009-10-07 16:13:39 -0700 | [diff] [blame] | 147 | if (!context.getResources().getBoolean( |
| 148 | com.android.internal.R.bool.config_sf_slowBlur)) { |
| 149 | pd.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND); |
| 150 | } |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 151 | |
| 152 | pd.show(); |
| 153 | |
| 154 | // start the thread that initiates shutdown |
| 155 | sInstance.mContext = context; |
Dianne Hackborn | f99ae76 | 2010-03-08 12:43:51 -0800 | [diff] [blame] | 156 | sInstance.mPowerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE); |
| 157 | sInstance.mWakeLock = null; |
| 158 | if (sInstance.mPowerManager.isScreenOn()) { |
| 159 | try { |
| 160 | sInstance.mWakeLock = sInstance.mPowerManager.newWakeLock( |
| 161 | PowerManager.FULL_WAKE_LOCK, "Shutdown"); |
| 162 | sInstance.mWakeLock.acquire(); |
| 163 | } catch (SecurityException e) { |
| 164 | Log.w(TAG, "No permission to acquire wake lock", e); |
| 165 | sInstance.mWakeLock = null; |
| 166 | } |
| 167 | } |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 168 | sInstance.mHandler = new Handler() { |
| 169 | }; |
| 170 | sInstance.start(); |
| 171 | } |
| 172 | |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 173 | void actionDone() { |
| 174 | synchronized (mActionDoneSync) { |
| 175 | mActionDone = true; |
| 176 | mActionDoneSync.notifyAll(); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 177 | } |
| 178 | } |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 179 | |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 180 | /** |
| 181 | * Makes sure we handle the shutdown gracefully. |
| 182 | * Shuts off power regardless of radio and bluetooth state if the alloted time has passed. |
| 183 | */ |
| 184 | public void run() { |
| 185 | boolean bluetoothOff; |
| 186 | boolean radioOff; |
| 187 | |
| 188 | BroadcastReceiver br = new BroadcastReceiver() { |
| 189 | @Override public void onReceive(Context context, Intent intent) { |
| 190 | // We don't allow apps to cancel this, so ignore the result. |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 191 | actionDone(); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 192 | } |
| 193 | }; |
| 194 | |
| 195 | Log.i(TAG, "Sending shutdown broadcast..."); |
| 196 | |
| 197 | // First send the high-level shut down broadcast. |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 198 | mActionDone = false; |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 199 | mContext.sendOrderedBroadcast(new Intent(Intent.ACTION_SHUTDOWN), null, |
| 200 | br, mHandler, 0, null, null); |
| 201 | |
| 202 | final long endTime = System.currentTimeMillis() + MAX_BROADCAST_TIME; |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 203 | synchronized (mActionDoneSync) { |
| 204 | while (!mActionDone) { |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 205 | long delay = endTime - System.currentTimeMillis(); |
| 206 | if (delay <= 0) { |
| 207 | Log.w(TAG, "Shutdown broadcast timed out"); |
| 208 | break; |
| 209 | } |
| 210 | try { |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 211 | mActionDoneSync.wait(delay); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 212 | } catch (InterruptedException e) { |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | Log.i(TAG, "Shutting down activity manager..."); |
| 218 | |
| 219 | final IActivityManager am = |
| 220 | ActivityManagerNative.asInterface(ServiceManager.checkService("activity")); |
| 221 | if (am != null) { |
| 222 | try { |
| 223 | am.shutdown(MAX_BROADCAST_TIME); |
| 224 | } catch (RemoteException e) { |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | final ITelephony phone = |
| 229 | ITelephony.Stub.asInterface(ServiceManager.checkService("phone")); |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 230 | final IBluetooth bluetooth = |
| 231 | IBluetooth.Stub.asInterface(ServiceManager.checkService( |
Nick Pelly | f242b7b | 2009-10-08 00:12:45 +0200 | [diff] [blame] | 232 | BluetoothAdapter.BLUETOOTH_SERVICE)); |
San Mehat | 9f7f7ca | 2010-01-07 11:34:59 -0800 | [diff] [blame] | 233 | |
| 234 | final IMountService mount = |
| 235 | IMountService.Stub.asInterface( |
| 236 | ServiceManager.checkService("mount")); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 237 | |
| 238 | try { |
| 239 | bluetoothOff = bluetooth == null || |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 240 | bluetooth.getBluetoothState() == BluetoothAdapter.STATE_OFF; |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 241 | if (!bluetoothOff) { |
| 242 | Log.w(TAG, "Disabling Bluetooth..."); |
| 243 | bluetooth.disable(false); // disable but don't persist new state |
| 244 | } |
| 245 | } catch (RemoteException ex) { |
| 246 | Log.e(TAG, "RemoteException during bluetooth shutdown", ex); |
| 247 | bluetoothOff = true; |
| 248 | } |
| 249 | |
| 250 | try { |
| 251 | radioOff = phone == null || !phone.isRadioOn(); |
| 252 | if (!radioOff) { |
| 253 | Log.w(TAG, "Turning off radio..."); |
| 254 | phone.setRadio(false); |
| 255 | } |
| 256 | } catch (RemoteException ex) { |
| 257 | Log.e(TAG, "RemoteException during radio shutdown", ex); |
| 258 | radioOff = true; |
| 259 | } |
| 260 | |
| 261 | Log.i(TAG, "Waiting for Bluetooth and Radio..."); |
| 262 | |
| 263 | // Wait a max of 32 seconds for clean shutdown |
| 264 | for (int i = 0; i < MAX_NUM_PHONE_STATE_READS; i++) { |
| 265 | if (!bluetoothOff) { |
| 266 | try { |
| 267 | bluetoothOff = |
Nick Pelly | de893f5 | 2009-09-08 13:15:33 -0700 | [diff] [blame] | 268 | bluetooth.getBluetoothState() == BluetoothAdapter.STATE_OFF; |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 269 | } catch (RemoteException ex) { |
| 270 | Log.e(TAG, "RemoteException during bluetooth shutdown", ex); |
| 271 | bluetoothOff = true; |
| 272 | } |
| 273 | } |
| 274 | if (!radioOff) { |
| 275 | try { |
| 276 | radioOff = !phone.isRadioOn(); |
| 277 | } catch (RemoteException ex) { |
| 278 | Log.e(TAG, "RemoteException during radio shutdown", ex); |
| 279 | radioOff = true; |
| 280 | } |
| 281 | } |
| 282 | if (radioOff && bluetoothOff) { |
| 283 | Log.i(TAG, "Radio and Bluetooth shutdown complete."); |
| 284 | break; |
| 285 | } |
| 286 | SystemClock.sleep(PHONE_STATE_POLL_SLEEP_MSEC); |
| 287 | } |
| 288 | |
San Mehat | 9f7f7ca | 2010-01-07 11:34:59 -0800 | [diff] [blame] | 289 | // Shutdown MountService to ensure media is in a safe state |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 290 | IMountShutdownObserver observer = new IMountShutdownObserver.Stub() { |
| 291 | public void onShutDownComplete(int statusCode) throws RemoteException { |
| 292 | Log.w(TAG, "Result code " + statusCode + " from MountService.shutdown"); |
| 293 | actionDone(); |
San Mehat | 9f7f7ca | 2010-01-07 11:34:59 -0800 | [diff] [blame] | 294 | } |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 295 | }; |
| 296 | |
| 297 | Log.i(TAG, "Shutting down MountService"); |
| 298 | // Set initial variables and time out time. |
| 299 | mActionDone = false; |
| 300 | final long endShutTime = System.currentTimeMillis() + MAX_SHUTDOWN_WAIT_TIME; |
| 301 | synchronized (mActionDoneSync) { |
| 302 | try { |
| 303 | if (mount != null) { |
| 304 | mount.shutdown(observer); |
| 305 | } else { |
| 306 | Log.w(TAG, "MountService unavailable for shutdown"); |
| 307 | } |
| 308 | } catch (Exception e) { |
| 309 | Log.e(TAG, "Exception during MountService shutdown", e); |
| 310 | } |
| 311 | while (!mActionDone) { |
| 312 | long delay = endShutTime - System.currentTimeMillis(); |
| 313 | if (delay <= 0) { |
| 314 | Log.w(TAG, "Shutdown wait timed out"); |
| 315 | break; |
| 316 | } |
| 317 | try { |
| 318 | mActionDoneSync.wait(delay); |
| 319 | } catch (InterruptedException e) { |
| 320 | } |
| 321 | } |
San Mehat | 9f7f7ca | 2010-01-07 11:34:59 -0800 | [diff] [blame] | 322 | } |
| 323 | |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 324 | if (mReboot) { |
| 325 | Log.i(TAG, "Rebooting, reason: " + mRebootReason); |
| 326 | try { |
| 327 | Power.reboot(mRebootReason); |
| 328 | } catch (Exception e) { |
| 329 | Log.e(TAG, "Reboot failed, will attempt shutdown instead", e); |
| 330 | } |
Mike Lockwood | a717f64 | 2010-04-01 20:01:44 -0700 | [diff] [blame^] | 331 | } else if (SHUTDOWN_VIBRATE_MS > 0) { |
| 332 | // vibrate before shutting down |
| 333 | Vibrator vibrator = new Vibrator(); |
| 334 | vibrator.vibrate(SHUTDOWN_VIBRATE_MS); |
| 335 | // vibrator is asynchronous so we need to wait to avoid shutting down too soon. |
| 336 | try { |
| 337 | Thread.sleep(SHUTDOWN_VIBRATE_MS); |
| 338 | } catch (InterruptedException e) { |
| 339 | } |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | // Shutdown power |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 343 | Log.i(TAG, "Performing low-level shutdown..."); |
| 344 | Power.shutdown(); |
| 345 | } |
| 346 | } |