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