blob: 83614a87ea2ce1b858e6025b070fa818dd068745 [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;
21import android.app.IActivityManager;
22import android.app.ProgressDialog;
23import android.app.AlertDialog;
Nick Pellybd022f42009-08-14 18:33:38 -070024import android.bluetooth.BluetoothAdapter;
25import android.bluetooth.IBluetooth;
Dianne Hackborn55280a92009-05-07 15:53:46 -070026import android.content.BroadcastReceiver;
27import android.content.Context;
28import android.content.DialogInterface;
29import android.content.Intent;
30import android.os.Handler;
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -080031import android.os.Power;
Dianne Hackbornf99ae762010-03-08 12:43:51 -080032import android.os.PowerManager;
Dianne Hackborn55280a92009-05-07 15:53:46 -070033import android.os.RemoteException;
Dianne Hackborn55280a92009-05-07 15:53:46 -070034import android.os.ServiceManager;
35import android.os.SystemClock;
Mike Lockwooda717f642010-04-01 20:01:44 -070036import android.os.Vibrator;
San Mehatb1043402010-02-05 08:26:50 -080037import android.os.storage.IMountService;
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -080038import android.os.storage.IMountShutdownObserver;
Dianne Hackborn568cae52009-10-07 16:13:39 -070039
Dianne Hackborn55280a92009-05-07 15:53:46 -070040import com.android.internal.telephony.ITelephony;
41import android.util.Log;
42import android.view.WindowManager;
43
44public 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 Amalapurapu6ffce2e2010-03-08 14:48:40 -080051 private static final int MAX_SHUTDOWN_WAIT_TIME = 20*1000;
Mike Lockwooda717f642010-04-01 20:01:44 -070052
53 // length of vibration before shutting down
54 private static final int SHUTDOWN_VIBRATE_MS = 500;
Dianne Hackborn55280a92009-05-07 15:53:46 -070055
56 // state tracking
57 private static Object sIsStartedGuard = new Object();
58 private static boolean sIsStarted = false;
59
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -080060 private static boolean mReboot;
61 private static String mRebootReason;
62
Dianne Hackborn55280a92009-05-07 15:53:46 -070063 // static instance of this thread
64 private static final ShutdownThread sInstance = new ShutdownThread();
65
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -080066 private final Object mActionDoneSync = new Object();
67 private boolean mActionDone;
Dianne Hackborn55280a92009-05-07 15:53:46 -070068 private Context mContext;
Dianne Hackbornf99ae762010-03-08 12:43:51 -080069 private PowerManager mPowerManager;
70 private PowerManager.WakeLock mWakeLock;
Dianne Hackborn55280a92009-05-07 15:53:46 -070071 private Handler mHandler;
72
73 private ShutdownThread() {
74 }
75
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -080076 /**
Dianne Hackborn55280a92009-05-07 15:53:46 -070077 * 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 Amalapurapu6ffce2e2010-03-08 14:48:40 -080080 *
Dianne Hackborn55280a92009-05-07 15:53:46 -070081 * @param context Context used to display the shutdown progress dialog.
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -080082 * @param confirm true if user confirmation is needed before shutting down.
Dianne Hackborn55280a92009-05-07 15:53:46 -070083 */
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 Hackborn568cae52009-10-07 16:13:39 -0700109 if (!context.getResources().getBoolean(
110 com.android.internal.R.bool.config_sf_slowBlur)) {
111 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
112 }
Dianne Hackborn55280a92009-05-07 15:53:46 -0700113 dialog.show();
114 } else {
115 beginShutdownSequence(context);
116 }
117 }
118
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800119 /**
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 Hackborn55280a92009-05-07 15:53:46 -0700134 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 Hackborn568cae52009-10-07 16:13:39 -0700147 if (!context.getResources().getBoolean(
148 com.android.internal.R.bool.config_sf_slowBlur)) {
149 pd.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
150 }
Dianne Hackborn55280a92009-05-07 15:53:46 -0700151
152 pd.show();
153
154 // start the thread that initiates shutdown
155 sInstance.mContext = context;
Dianne Hackbornf99ae762010-03-08 12:43:51 -0800156 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 Hackborn55280a92009-05-07 15:53:46 -0700168 sInstance.mHandler = new Handler() {
169 };
170 sInstance.start();
171 }
172
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800173 void actionDone() {
174 synchronized (mActionDoneSync) {
175 mActionDone = true;
176 mActionDoneSync.notifyAll();
Dianne Hackborn55280a92009-05-07 15:53:46 -0700177 }
178 }
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800179
Dianne Hackborn55280a92009-05-07 15:53:46 -0700180 /**
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 Amalapurapu6ffce2e2010-03-08 14:48:40 -0800191 actionDone();
Dianne Hackborn55280a92009-05-07 15:53:46 -0700192 }
193 };
194
195 Log.i(TAG, "Sending shutdown broadcast...");
196
197 // First send the high-level shut down broadcast.
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800198 mActionDone = false;
Dianne Hackborn55280a92009-05-07 15:53:46 -0700199 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 Amalapurapu6ffce2e2010-03-08 14:48:40 -0800203 synchronized (mActionDoneSync) {
204 while (!mActionDone) {
Dianne Hackborn55280a92009-05-07 15:53:46 -0700205 long delay = endTime - System.currentTimeMillis();
206 if (delay <= 0) {
207 Log.w(TAG, "Shutdown broadcast timed out");
208 break;
209 }
210 try {
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800211 mActionDoneSync.wait(delay);
Dianne Hackborn55280a92009-05-07 15:53:46 -0700212 } 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 Pellybd022f42009-08-14 18:33:38 -0700230 final IBluetooth bluetooth =
231 IBluetooth.Stub.asInterface(ServiceManager.checkService(
Nick Pellyf242b7b2009-10-08 00:12:45 +0200232 BluetoothAdapter.BLUETOOTH_SERVICE));
San Mehat9f7f7ca2010-01-07 11:34:59 -0800233
234 final IMountService mount =
235 IMountService.Stub.asInterface(
236 ServiceManager.checkService("mount"));
Dianne Hackborn55280a92009-05-07 15:53:46 -0700237
238 try {
239 bluetoothOff = bluetooth == null ||
Nick Pellyde893f52009-09-08 13:15:33 -0700240 bluetooth.getBluetoothState() == BluetoothAdapter.STATE_OFF;
Dianne Hackborn55280a92009-05-07 15:53:46 -0700241 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 Pellyde893f52009-09-08 13:15:33 -0700268 bluetooth.getBluetoothState() == BluetoothAdapter.STATE_OFF;
Dianne Hackborn55280a92009-05-07 15:53:46 -0700269 } 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 Mehat9f7f7ca2010-01-07 11:34:59 -0800289 // Shutdown MountService to ensure media is in a safe state
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800290 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 Mehat9f7f7ca2010-01-07 11:34:59 -0800294 }
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800295 };
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 Mehat9f7f7ca2010-01-07 11:34:59 -0800322 }
323
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800324 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 Lockwooda717f642010-04-01 20:01:44 -0700331 } 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 Amalapurapu6ffce2e2010-03-08 14:48:40 -0800340 }
341
342 // Shutdown power
Dianne Hackborn55280a92009-05-07 15:53:46 -0700343 Log.i(TAG, "Performing low-level shutdown...");
344 Power.shutdown();
345 }
346}