blob: 8104ece7b7906367df65d4ec41e9922250c84eef [file] [log] [blame]
Dianne Hackborn55280a92009-05-07 15:53:46 -07001/*
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
18package com.android.internal.app;
19
20import android.app.ActivityManagerNative;
Joe Onoratod208e702010-10-08 16:22:43 -040021import android.app.AlertDialog;
22import android.app.Dialog;
Dianne Hackborn55280a92009-05-07 15:53:46 -070023import android.app.IActivityManager;
24import android.app.ProgressDialog;
Nick Pellybd022f42009-08-14 18:33:38 -070025import android.bluetooth.BluetoothAdapter;
26import android.bluetooth.IBluetooth;
Dianne Hackborn55280a92009-05-07 15:53:46 -070027import android.content.BroadcastReceiver;
28import android.content.Context;
29import android.content.DialogInterface;
30import android.content.Intent;
Joe Onoratod208e702010-10-08 16:22:43 -040031import android.content.IntentFilter;
Dianne Hackborn55280a92009-05-07 15:53:46 -070032import android.os.Handler;
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -080033import android.os.Power;
Dianne Hackbornf99ae762010-03-08 12:43:51 -080034import android.os.PowerManager;
Dianne Hackborn55280a92009-05-07 15:53:46 -070035import android.os.RemoteException;
Dianne Hackborn55280a92009-05-07 15:53:46 -070036import android.os.ServiceManager;
37import android.os.SystemClock;
Mike Lockwooda717f642010-04-01 20:01:44 -070038import android.os.Vibrator;
San Mehatb1043402010-02-05 08:26:50 -080039import android.os.storage.IMountService;
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -080040import android.os.storage.IMountShutdownObserver;
Dianne Hackborn568cae52009-10-07 16:13:39 -070041
Dianne Hackborn55280a92009-05-07 15:53:46 -070042import com.android.internal.telephony.ITelephony;
43import android.util.Log;
44import android.view.WindowManager;
45
46public 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 Amalapurapu6ffce2e2010-03-08 14:48:40 -080053 private static final int MAX_SHUTDOWN_WAIT_TIME = 20*1000;
Mike Lockwooda717f642010-04-01 20:01:44 -070054
55 // length of vibration before shutting down
56 private static final int SHUTDOWN_VIBRATE_MS = 500;
Dianne Hackborn55280a92009-05-07 15:53:46 -070057
58 // state tracking
59 private static Object sIsStartedGuard = new Object();
60 private static boolean sIsStarted = false;
61
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -080062 private static boolean mReboot;
63 private static String mRebootReason;
64
Dianne Hackborn55280a92009-05-07 15:53:46 -070065 // static instance of this thread
66 private static final ShutdownThread sInstance = new ShutdownThread();
67
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -080068 private final Object mActionDoneSync = new Object();
69 private boolean mActionDone;
Dianne Hackborn55280a92009-05-07 15:53:46 -070070 private Context mContext;
Dianne Hackbornf99ae762010-03-08 12:43:51 -080071 private PowerManager mPowerManager;
72 private PowerManager.WakeLock mWakeLock;
Dianne Hackborn55280a92009-05-07 15:53:46 -070073 private Handler mHandler;
74
75 private ShutdownThread() {
76 }
77
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -080078 /**
Dianne Hackborn55280a92009-05-07 15:53:46 -070079 * 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 Amalapurapu6ffce2e2010-03-08 14:48:40 -080082 *
Dianne Hackborn55280a92009-05-07 15:53:46 -070083 * @param context Context used to display the shutdown progress dialog.
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -080084 * @param confirm true if user confirmation is needed before shutting down.
Dianne Hackborn55280a92009-05-07 15:53:46 -070085 */
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 Lockwoodd67b2362010-07-26 07:18:21 -040089 synchronized (sIsStartedGuard) {
Dianne Hackborn55280a92009-05-07 15:53:46 -070090 if (sIsStarted) {
91 Log.d(TAG, "Request to shutdown already running, returning.");
92 return;
93 }
94 }
95
Joe Onoratod208e702010-10-08 16:22:43 -040096 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 Hackborn55280a92009-05-07 15:53:46 -0700103
104 if (confirm) {
Joe Onoratod208e702010-10-08 16:22:43 -0400105 final CloseDialogReceiver closer = new CloseDialogReceiver(context);
Dianne Hackborn55280a92009-05-07 15:53:46 -0700106 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 Onoratod208e702010-10-08 16:22:43 -0400109 .setMessage(resourceId)
Dianne Hackborn55280a92009-05-07 15:53:46 -0700110 .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 Onoratod208e702010-10-08 16:22:43 -0400117 closer.dialog = dialog;
118 dialog.setOnDismissListener(closer);
Dianne Hackborn55280a92009-05-07 15:53:46 -0700119 dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
Dianne Hackborn568cae52009-10-07 16:13:39 -0700120 if (!context.getResources().getBoolean(
121 com.android.internal.R.bool.config_sf_slowBlur)) {
122 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
123 }
Dianne Hackborn55280a92009-05-07 15:53:46 -0700124 dialog.show();
125 } else {
126 beginShutdownSequence(context);
127 }
128 }
129
Joe Onoratod208e702010-10-08 16:22:43 -0400130 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 Amalapurapu6ffce2e2010-03-08 14:48:40 -0800151 /**
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 Hackborn55280a92009-05-07 15:53:46 -0700166 private static void beginShutdownSequence(Context context) {
167 synchronized (sIsStartedGuard) {
Mike Lockwoodd67b2362010-07-26 07:18:21 -0400168 if (sIsStarted) {
Mathias Jeppsson8534a8e2010-08-17 13:33:09 +0200169 Log.d(TAG, "Shutdown sequence already running, returning.");
Mike Lockwoodd67b2362010-07-26 07:18:21 -0400170 return;
171 }
Dianne Hackborn55280a92009-05-07 15:53:46 -0700172 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 Hackborn568cae52009-10-07 16:13:39 -0700183 if (!context.getResources().getBoolean(
184 com.android.internal.R.bool.config_sf_slowBlur)) {
185 pd.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
186 }
Dianne Hackborn55280a92009-05-07 15:53:46 -0700187
188 pd.show();
189
190 // start the thread that initiates shutdown
191 sInstance.mContext = context;
Dianne Hackbornf99ae762010-03-08 12:43:51 -0800192 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 Hackborn55280a92009-05-07 15:53:46 -0700204 sInstance.mHandler = new Handler() {
205 };
206 sInstance.start();
207 }
208
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800209 void actionDone() {
210 synchronized (mActionDoneSync) {
211 mActionDone = true;
212 mActionDoneSync.notifyAll();
Dianne Hackborn55280a92009-05-07 15:53:46 -0700213 }
214 }
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800215
Dianne Hackborn55280a92009-05-07 15:53:46 -0700216 /**
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 Amalapurapu6ffce2e2010-03-08 14:48:40 -0800227 actionDone();
Dianne Hackborn55280a92009-05-07 15:53:46 -0700228 }
229 };
230
231 Log.i(TAG, "Sending shutdown broadcast...");
232
233 // First send the high-level shut down broadcast.
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800234 mActionDone = false;
Dianne Hackborn55280a92009-05-07 15:53:46 -0700235 mContext.sendOrderedBroadcast(new Intent(Intent.ACTION_SHUTDOWN), null,
236 br, mHandler, 0, null, null);
237
Mike Lockwood098e58d2010-05-13 16:29:49 -0400238 final long endTime = SystemClock.elapsedRealtime() + MAX_BROADCAST_TIME;
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800239 synchronized (mActionDoneSync) {
240 while (!mActionDone) {
Mike Lockwood098e58d2010-05-13 16:29:49 -0400241 long delay = endTime - SystemClock.elapsedRealtime();
Dianne Hackborn55280a92009-05-07 15:53:46 -0700242 if (delay <= 0) {
243 Log.w(TAG, "Shutdown broadcast timed out");
244 break;
245 }
246 try {
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800247 mActionDoneSync.wait(delay);
Dianne Hackborn55280a92009-05-07 15:53:46 -0700248 } 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 Pellybd022f42009-08-14 18:33:38 -0700266 final IBluetooth bluetooth =
267 IBluetooth.Stub.asInterface(ServiceManager.checkService(
Nick Pellyf242b7b2009-10-08 00:12:45 +0200268 BluetoothAdapter.BLUETOOTH_SERVICE));
San Mehat9f7f7ca2010-01-07 11:34:59 -0800269
270 final IMountService mount =
271 IMountService.Stub.asInterface(
272 ServiceManager.checkService("mount"));
Dianne Hackborn55280a92009-05-07 15:53:46 -0700273
274 try {
275 bluetoothOff = bluetooth == null ||
Nick Pellyde893f52009-09-08 13:15:33 -0700276 bluetooth.getBluetoothState() == BluetoothAdapter.STATE_OFF;
Dianne Hackborn55280a92009-05-07 15:53:46 -0700277 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 Pellyde893f52009-09-08 13:15:33 -0700304 bluetooth.getBluetoothState() == BluetoothAdapter.STATE_OFF;
Dianne Hackborn55280a92009-05-07 15:53:46 -0700305 } 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 Mehat9f7f7ca2010-01-07 11:34:59 -0800325 // Shutdown MountService to ensure media is in a safe state
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800326 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 Mehat9f7f7ca2010-01-07 11:34:59 -0800330 }
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800331 };
332
333 Log.i(TAG, "Shutting down MountService");
334 // Set initial variables and time out time.
335 mActionDone = false;
Mike Lockwood098e58d2010-05-13 16:29:49 -0400336 final long endShutTime = SystemClock.elapsedRealtime() + MAX_SHUTDOWN_WAIT_TIME;
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800337 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 Lockwood098e58d2010-05-13 16:29:49 -0400348 long delay = endShutTime - SystemClock.elapsedRealtime();
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800349 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 Mehat9f7f7ca2010-01-07 11:34:59 -0800358 }
359
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800360 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 Lockwooda717f642010-04-01 20:01:44 -0700367 } 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 Amalapurapu6ffce2e2010-03-08 14:48:40 -0800376 }
377
378 // Shutdown power
Dianne Hackborn55280a92009-05-07 15:53:46 -0700379 Log.i(TAG, "Performing low-level shutdown...");
380 Power.shutdown();
381 }
382}