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 | |
Tao Bao | 90237f7 | 2015-05-21 16:25:19 -0700 | [diff] [blame] | 17 | |
Jeff Brown | 4f8ecd8 | 2012-06-18 18:29:13 -0700 | [diff] [blame] | 18 | package com.android.server.power; |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 19 | |
Joe Onorato | d208e70 | 2010-10-08 16:22:43 -0400 | [diff] [blame] | 20 | import android.app.AlertDialog; |
| 21 | import android.app.Dialog; |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 22 | import android.app.IActivityManager; |
Lucas Dupin | 527aad1 | 2017-07-21 13:50:25 -0700 | [diff] [blame] | 23 | import android.app.KeyguardManager; |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 24 | import android.app.ProgressDialog; |
Lucas Dupin | 527aad1 | 2017-07-21 13:50:25 -0700 | [diff] [blame] | 25 | import android.app.WallpaperColors; |
| 26 | import android.app.WallpaperManager; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 27 | import android.bluetooth.BluetoothAdapter; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 28 | import android.bluetooth.IBluetoothManager; |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 29 | import android.content.BroadcastReceiver; |
| 30 | import android.content.Context; |
| 31 | import android.content.DialogInterface; |
| 32 | import android.content.Intent; |
Joe Onorato | d208e70 | 2010-10-08 16:22:43 -0400 | [diff] [blame] | 33 | import android.content.IntentFilter; |
Jason Monk | fd27966 | 2017-06-29 19:37:48 -0400 | [diff] [blame] | 34 | import android.graphics.Color; |
| 35 | import android.graphics.drawable.ColorDrawable; |
| 36 | import android.media.AudioAttributes; |
Tao Bao | e8a403d | 2015-12-31 07:44:55 -0800 | [diff] [blame] | 37 | import android.os.FileUtils; |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 38 | import android.os.Handler; |
Dianne Hackborn | f99ae76 | 2010-03-08 12:43:51 -0800 | [diff] [blame] | 39 | import android.os.PowerManager; |
Tao Bao | e8a403d | 2015-12-31 07:44:55 -0800 | [diff] [blame] | 40 | import android.os.RecoverySystem; |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 41 | import android.os.RemoteException; |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 42 | import android.os.ServiceManager; |
| 43 | import android.os.SystemClock; |
Kenny Root | f547d67 | 2010-09-22 10:36:48 -0700 | [diff] [blame] | 44 | import android.os.SystemProperties; |
Jason Monk | fd27966 | 2017-06-29 19:37:48 -0400 | [diff] [blame] | 45 | import android.os.SystemVibrator; |
Fyodor Kupolov | 6e3461b | 2017-08-10 17:00:43 -0700 | [diff] [blame] | 46 | import android.os.Trace; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 47 | import android.os.UserHandle; |
Benjamin Franz | bff46ba | 2015-03-05 18:33:51 +0000 | [diff] [blame] | 48 | import android.os.UserManager; |
Mike Lockwood | a717f64 | 2010-04-01 20:01:44 -0700 | [diff] [blame] | 49 | import android.os.Vibrator; |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 50 | import android.os.storage.IStorageManager; |
Jason Monk | fd27966 | 2017-06-29 19:37:48 -0400 | [diff] [blame] | 51 | import android.os.storage.IStorageShutdownObserver; |
Fyodor Kupolov | 291f63b | 2017-08-25 16:59:08 -0700 | [diff] [blame] | 52 | import android.util.ArrayMap; |
Jason Monk | fd27966 | 2017-06-29 19:37:48 -0400 | [diff] [blame] | 53 | import android.util.Log; |
Fyodor Kupolov | 6e3461b | 2017-08-10 17:00:43 -0700 | [diff] [blame] | 54 | import android.util.TimingsTraceLog; |
Jason Monk | fd27966 | 2017-06-29 19:37:48 -0400 | [diff] [blame] | 55 | import android.view.WindowManager; |
Lucas Dupin | 527aad1 | 2017-07-21 13:50:25 -0700 | [diff] [blame] | 56 | |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 57 | import com.android.internal.telephony.ITelephony; |
Jeff Sharkey | b4f3697 | 2017-08-02 14:33:04 -0600 | [diff] [blame] | 58 | import com.android.server.RescueParty; |
Jason Monk | b430218 | 2017-08-04 13:39:17 -0400 | [diff] [blame] | 59 | import com.android.server.LocalServices; |
Brian Carlstrom | ff1ec4d | 2014-03-17 15:21:35 -0700 | [diff] [blame] | 60 | import com.android.server.pm.PackageManagerService; |
Jason Monk | b430218 | 2017-08-04 13:39:17 -0400 | [diff] [blame] | 61 | import com.android.server.statusbar.StatusBarManagerInternal; |
Jeff Brown | 7304c34 | 2012-05-11 18:42:42 -0700 | [diff] [blame] | 62 | |
Tao Bao | 90237f7 | 2015-05-21 16:25:19 -0700 | [diff] [blame] | 63 | import java.io.File; |
Fyodor Kupolov | 291f63b | 2017-08-25 16:59:08 -0700 | [diff] [blame] | 64 | import java.io.FileOutputStream; |
Tao Bao | 90237f7 | 2015-05-21 16:25:19 -0700 | [diff] [blame] | 65 | import java.io.IOException; |
Fyodor Kupolov | 291f63b | 2017-08-25 16:59:08 -0700 | [diff] [blame] | 66 | import java.nio.charset.StandardCharsets; |
Tao Bao | 90237f7 | 2015-05-21 16:25:19 -0700 | [diff] [blame] | 67 | |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 68 | public final class ShutdownThread extends Thread { |
| 69 | // constants |
| 70 | private static final String TAG = "ShutdownThread"; |
Fyodor Kupolov | 231d57d | 2017-11-14 17:41:53 -0800 | [diff] [blame] | 71 | private static final int ACTION_DONE_POLL_WAIT_MS = 500; |
| 72 | private static final int RADIOS_STATE_POLL_SLEEP_MS = 100; |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 73 | // maximum time we wait for the shutdown broadcast before going on. |
| 74 | private static final int MAX_BROADCAST_TIME = 10*1000; |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 75 | private static final int MAX_SHUTDOWN_WAIT_TIME = 20*1000; |
Jeff Brown | b820371 | 2012-05-31 17:39:13 -0700 | [diff] [blame] | 76 | private static final int MAX_RADIO_WAIT_TIME = 12*1000; |
Adam Seaton | b57e656 | 2016-08-30 22:14:47 +0000 | [diff] [blame] | 77 | private static final int MAX_UNCRYPT_WAIT_TIME = 15*60*1000; |
Tao Bao | 81dce66 | 2015-06-03 11:42:31 -0700 | [diff] [blame] | 78 | // constants for progress bar. the values are roughly estimated based on timeout. |
| 79 | private static final int BROADCAST_STOP_PERCENT = 2; |
| 80 | private static final int ACTIVITY_MANAGER_STOP_PERCENT = 4; |
| 81 | private static final int PACKAGE_MANAGER_STOP_PERCENT = 6; |
| 82 | private static final int RADIO_STOP_PERCENT = 18; |
| 83 | private static final int MOUNT_SERVICE_STOP_PERCENT = 20; |
Mike Lockwood | a717f64 | 2010-04-01 20:01:44 -0700 | [diff] [blame] | 84 | |
| 85 | // length of vibration before shutting down |
| 86 | private static final int SHUTDOWN_VIBRATE_MS = 500; |
Tao Bao | 90237f7 | 2015-05-21 16:25:19 -0700 | [diff] [blame] | 87 | |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 88 | // state tracking |
Adam Lesinski | a82b626 | 2017-03-21 16:56:17 -0700 | [diff] [blame] | 89 | private static final Object sIsStartedGuard = new Object(); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 90 | private static boolean sIsStarted = false; |
Tao Bao | 90237f7 | 2015-05-21 16:25:19 -0700 | [diff] [blame] | 91 | |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 92 | private static boolean mReboot; |
Dianne Hackborn | 19caadc | 2012-04-20 17:49:10 -0700 | [diff] [blame] | 93 | private static boolean mRebootSafeMode; |
Tao Bao | e8a403d | 2015-12-31 07:44:55 -0800 | [diff] [blame] | 94 | private static boolean mRebootHasProgressBar; |
Yusuke Sato | 705ffd1 | 2015-07-21 15:52:11 -0700 | [diff] [blame] | 95 | private static String mReason; |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 96 | |
Kenny Root | f547d67 | 2010-09-22 10:36:48 -0700 | [diff] [blame] | 97 | // Provides shutdown assurance in case the system_server is killed |
| 98 | public static final String SHUTDOWN_ACTION_PROPERTY = "sys.shutdown.requested"; |
| 99 | |
Dianne Hackborn | 19caadc | 2012-04-20 17:49:10 -0700 | [diff] [blame] | 100 | // Indicates whether we are rebooting into safe mode |
| 101 | public static final String REBOOT_SAFEMODE_PROPERTY = "persist.sys.safemode"; |
dcashman | f44f5a4 | 2016-03-02 16:46:40 -0800 | [diff] [blame] | 102 | public static final String RO_SAFEMODE_PROPERTY = "ro.sys.safemode"; |
Dianne Hackborn | 19caadc | 2012-04-20 17:49:10 -0700 | [diff] [blame] | 103 | |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 104 | // static instance of this thread |
| 105 | private static final ShutdownThread sInstance = new ShutdownThread(); |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 106 | |
| 107 | private static final AudioAttributes VIBRATION_ATTRIBUTES = new AudioAttributes.Builder() |
| 108 | .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) |
| 109 | .setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION) |
| 110 | .build(); |
| 111 | |
Fyodor Kupolov | 291f63b | 2017-08-25 16:59:08 -0700 | [diff] [blame] | 112 | // Metrics that will be reported to tron after reboot |
| 113 | private static final ArrayMap<String, Long> TRON_METRICS = new ArrayMap<>(); |
| 114 | |
| 115 | // File to use for save metrics |
| 116 | private static final String METRICS_FILE_BASENAME = "/data/system/shutdown-metrics"; |
| 117 | |
| 118 | // Metrics names to be persisted in shutdown-metrics file |
| 119 | private static String METRIC_SYSTEM_SERVER = "shutdown_system_server"; |
| 120 | private static String METRIC_SEND_BROADCAST = "shutdown_send_shutdown_broadcast"; |
| 121 | private static String METRIC_AM = "shutdown_activity_manager"; |
| 122 | private static String METRIC_PM = "shutdown_package_manager"; |
| 123 | private static String METRIC_RADIOS = "shutdown_radios"; |
| 124 | private static String METRIC_BT = "shutdown_bt"; |
| 125 | private static String METRIC_RADIO = "shutdown_radio"; |
Fyodor Kupolov | 291f63b | 2017-08-25 16:59:08 -0700 | [diff] [blame] | 126 | private static String METRIC_SM = "shutdown_storage_manager"; |
| 127 | |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 128 | private final Object mActionDoneSync = new Object(); |
| 129 | private boolean mActionDone; |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 130 | private Context mContext; |
Dianne Hackborn | f99ae76 | 2010-03-08 12:43:51 -0800 | [diff] [blame] | 131 | private PowerManager mPowerManager; |
Mattias Larsson | cd4e427 | 2010-09-28 14:34:15 +0200 | [diff] [blame] | 132 | private PowerManager.WakeLock mCpuWakeLock; |
| 133 | private PowerManager.WakeLock mScreenWakeLock; |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 134 | private Handler mHandler; |
Jean-Baptiste Queru | 6e85ad7 | 2012-06-11 12:20:36 -0700 | [diff] [blame] | 135 | |
| 136 | private static AlertDialog sConfirmDialog; |
Tao Bao | 90237f7 | 2015-05-21 16:25:19 -0700 | [diff] [blame] | 137 | private ProgressDialog mProgressDialog; |
| 138 | |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 139 | private ShutdownThread() { |
| 140 | } |
Tao Bao | 90237f7 | 2015-05-21 16:25:19 -0700 | [diff] [blame] | 141 | |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 142 | /** |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 143 | * Request a clean shutdown, waiting for subsystems to clean up their |
| 144 | * state etc. Must be called from a Looper thread in which its UI |
| 145 | * is shown. |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 146 | * |
Adam Lesinski | a82b626 | 2017-03-21 16:56:17 -0700 | [diff] [blame] | 147 | * @param context Context used to display the shutdown progress dialog. This must be a context |
| 148 | * suitable for displaying UI (aka Themable). |
Yusuke Sato | 705ffd1 | 2015-07-21 15:52:11 -0700 | [diff] [blame] | 149 | * @param reason code to pass to android_reboot() (e.g. "userrequested"), or null. |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 150 | * @param confirm true if user confirmation is needed before shutting down. |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 151 | */ |
Yusuke Sato | 705ffd1 | 2015-07-21 15:52:11 -0700 | [diff] [blame] | 152 | public static void shutdown(final Context context, String reason, boolean confirm) { |
Dianne Hackborn | 19caadc | 2012-04-20 17:49:10 -0700 | [diff] [blame] | 153 | mReboot = false; |
| 154 | mRebootSafeMode = false; |
Yusuke Sato | 705ffd1 | 2015-07-21 15:52:11 -0700 | [diff] [blame] | 155 | mReason = reason; |
Dianne Hackborn | 19caadc | 2012-04-20 17:49:10 -0700 | [diff] [blame] | 156 | shutdownInner(context, confirm); |
| 157 | } |
| 158 | |
Adam Lesinski | a82b626 | 2017-03-21 16:56:17 -0700 | [diff] [blame] | 159 | private static void shutdownInner(final Context context, boolean confirm) { |
| 160 | // ShutdownThread is called from many places, so best to verify here that the context passed |
| 161 | // in is themed. |
| 162 | context.assertRuntimeOverlayThemable(); |
| 163 | |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 164 | // ensure that only one thread is trying to power down. |
| 165 | // any additional calls are just returned |
Mike Lockwood | d67b236 | 2010-07-26 07:18:21 -0400 | [diff] [blame] | 166 | synchronized (sIsStartedGuard) { |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 167 | if (sIsStarted) { |
| 168 | Log.d(TAG, "Request to shutdown already running, returning."); |
| 169 | return; |
| 170 | } |
| 171 | } |
| 172 | |
Joe Onorato | d208e70 | 2010-10-08 16:22:43 -0400 | [diff] [blame] | 173 | final int longPressBehavior = context.getResources().getInteger( |
| 174 | com.android.internal.R.integer.config_longPressOnPowerBehavior); |
Dianne Hackborn | 19caadc | 2012-04-20 17:49:10 -0700 | [diff] [blame] | 175 | final int resourceId = mRebootSafeMode |
| 176 | ? com.android.internal.R.string.reboot_safemode_confirm |
| 177 | : (longPressBehavior == 2 |
| 178 | ? com.android.internal.R.string.shutdown_confirm_question |
| 179 | : com.android.internal.R.string.shutdown_confirm); |
Joe Onorato | d208e70 | 2010-10-08 16:22:43 -0400 | [diff] [blame] | 180 | |
| 181 | Log.d(TAG, "Notifying thread to start shutdown longPressBehavior=" + longPressBehavior); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 182 | |
| 183 | if (confirm) { |
Joe Onorato | d208e70 | 2010-10-08 16:22:43 -0400 | [diff] [blame] | 184 | final CloseDialogReceiver closer = new CloseDialogReceiver(context); |
Jean-Baptiste Queru | 6e85ad7 | 2012-06-11 12:20:36 -0700 | [diff] [blame] | 185 | if (sConfirmDialog != null) { |
| 186 | sConfirmDialog.dismiss(); |
| 187 | } |
| 188 | sConfirmDialog = new AlertDialog.Builder(context) |
Dianne Hackborn | 19caadc | 2012-04-20 17:49:10 -0700 | [diff] [blame] | 189 | .setTitle(mRebootSafeMode |
| 190 | ? com.android.internal.R.string.reboot_safemode_title |
| 191 | : com.android.internal.R.string.power_off) |
Joe Onorato | d208e70 | 2010-10-08 16:22:43 -0400 | [diff] [blame] | 192 | .setMessage(resourceId) |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 193 | .setPositiveButton(com.android.internal.R.string.yes, new DialogInterface.OnClickListener() { |
| 194 | public void onClick(DialogInterface dialog, int which) { |
| 195 | beginShutdownSequence(context); |
| 196 | } |
| 197 | }) |
| 198 | .setNegativeButton(com.android.internal.R.string.no, null) |
| 199 | .create(); |
Jean-Baptiste Queru | 6e85ad7 | 2012-06-11 12:20:36 -0700 | [diff] [blame] | 200 | closer.dialog = sConfirmDialog; |
| 201 | sConfirmDialog.setOnDismissListener(closer); |
| 202 | sConfirmDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG); |
| 203 | sConfirmDialog.show(); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 204 | } else { |
| 205 | beginShutdownSequence(context); |
| 206 | } |
| 207 | } |
| 208 | |
Joe Onorato | d208e70 | 2010-10-08 16:22:43 -0400 | [diff] [blame] | 209 | private static class CloseDialogReceiver extends BroadcastReceiver |
| 210 | implements DialogInterface.OnDismissListener { |
| 211 | private Context mContext; |
| 212 | public Dialog dialog; |
| 213 | |
| 214 | CloseDialogReceiver(Context context) { |
| 215 | mContext = context; |
| 216 | IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); |
| 217 | context.registerReceiver(this, filter); |
| 218 | } |
| 219 | |
| 220 | @Override |
| 221 | public void onReceive(Context context, Intent intent) { |
| 222 | dialog.cancel(); |
| 223 | } |
| 224 | |
| 225 | public void onDismiss(DialogInterface unused) { |
| 226 | mContext.unregisterReceiver(this); |
| 227 | } |
| 228 | } |
| 229 | |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 230 | /** |
| 231 | * Request a clean shutdown, waiting for subsystems to clean up their |
| 232 | * state etc. Must be called from a Looper thread in which its UI |
| 233 | * is shown. |
| 234 | * |
Adam Lesinski | a82b626 | 2017-03-21 16:56:17 -0700 | [diff] [blame] | 235 | * @param context Context used to display the shutdown progress dialog. This must be a context |
| 236 | * suitable for displaying UI (aka Themable). |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 237 | * @param reason code to pass to the kernel (e.g. "recovery"), or null. |
| 238 | * @param confirm true if user confirmation is needed before shutting down. |
| 239 | */ |
| 240 | public static void reboot(final Context context, String reason, boolean confirm) { |
| 241 | mReboot = true; |
Dianne Hackborn | 19caadc | 2012-04-20 17:49:10 -0700 | [diff] [blame] | 242 | mRebootSafeMode = false; |
Tao Bao | e8a403d | 2015-12-31 07:44:55 -0800 | [diff] [blame] | 243 | mRebootHasProgressBar = false; |
Yusuke Sato | 705ffd1 | 2015-07-21 15:52:11 -0700 | [diff] [blame] | 244 | mReason = reason; |
Dianne Hackborn | 19caadc | 2012-04-20 17:49:10 -0700 | [diff] [blame] | 245 | shutdownInner(context, confirm); |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Request a reboot into safe mode. Must be called from a Looper thread in which its UI |
| 250 | * is shown. |
| 251 | * |
Adam Lesinski | a82b626 | 2017-03-21 16:56:17 -0700 | [diff] [blame] | 252 | * @param context Context used to display the shutdown progress dialog. This must be a context |
| 253 | * suitable for displaying UI (aka Themable). |
Dianne Hackborn | 19caadc | 2012-04-20 17:49:10 -0700 | [diff] [blame] | 254 | * @param confirm true if user confirmation is needed before shutting down. |
| 255 | */ |
| 256 | public static void rebootSafeMode(final Context context, boolean confirm) { |
Benjamin Franz | bff46ba | 2015-03-05 18:33:51 +0000 | [diff] [blame] | 257 | UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE); |
| 258 | if (um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) { |
| 259 | return; |
| 260 | } |
| 261 | |
Dianne Hackborn | 19caadc | 2012-04-20 17:49:10 -0700 | [diff] [blame] | 262 | mReboot = true; |
| 263 | mRebootSafeMode = true; |
Tao Bao | e8a403d | 2015-12-31 07:44:55 -0800 | [diff] [blame] | 264 | mRebootHasProgressBar = false; |
Yusuke Sato | 705ffd1 | 2015-07-21 15:52:11 -0700 | [diff] [blame] | 265 | mReason = null; |
Dianne Hackborn | 19caadc | 2012-04-20 17:49:10 -0700 | [diff] [blame] | 266 | shutdownInner(context, confirm); |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 267 | } |
| 268 | |
Jason Monk | fd27966 | 2017-06-29 19:37:48 -0400 | [diff] [blame] | 269 | private static ProgressDialog showShutdownDialog(Context context) { |
Tao Bao | 81dce66 | 2015-06-03 11:42:31 -0700 | [diff] [blame] | 270 | // Throw up a system dialog to indicate the device is rebooting / shutting down. |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 271 | ProgressDialog pd = new ProgressDialog(context); |
Tao Bao | 81dce66 | 2015-06-03 11:42:31 -0700 | [diff] [blame] | 272 | |
Tao Bao | e8a403d | 2015-12-31 07:44:55 -0800 | [diff] [blame] | 273 | // Path 1: Reboot to recovery for update |
Tianjie Xu | 3a66fc4 | 2017-06-09 15:50:14 -0700 | [diff] [blame] | 274 | // Condition: mReason startswith REBOOT_RECOVERY_UPDATE |
Tao Bao | e8a403d | 2015-12-31 07:44:55 -0800 | [diff] [blame] | 275 | // |
| 276 | // Path 1a: uncrypt needed |
| 277 | // Condition: if /cache/recovery/uncrypt_file exists but |
| 278 | // /cache/recovery/block.map doesn't. |
| 279 | // UI: determinate progress bar (mRebootHasProgressBar == True) |
| 280 | // |
| 281 | // * Path 1a is expected to be removed once the GmsCore shipped on |
| 282 | // device always calls uncrypt prior to reboot. |
| 283 | // |
| 284 | // Path 1b: uncrypt already done |
| 285 | // UI: spinning circle only (no progress bar) |
Tao Bao | 81dce66 | 2015-06-03 11:42:31 -0700 | [diff] [blame] | 286 | // |
| 287 | // Path 2: Reboot to recovery for factory reset |
Yusuke Sato | 705ffd1 | 2015-07-21 15:52:11 -0700 | [diff] [blame] | 288 | // Condition: mReason == REBOOT_RECOVERY |
Tao Bao | 81dce66 | 2015-06-03 11:42:31 -0700 | [diff] [blame] | 289 | // UI: spinning circle only (no progress bar) |
| 290 | // |
| 291 | // Path 3: Regular reboot / shutdown |
| 292 | // Condition: Otherwise |
| 293 | // UI: spinning circle only (no progress bar) |
Tianjie Xu | 3a66fc4 | 2017-06-09 15:50:14 -0700 | [diff] [blame] | 294 | |
| 295 | // mReason could be "recovery-update" or "recovery-update,quiescent". |
| 296 | if (mReason != null && mReason.startsWith(PowerManager.REBOOT_RECOVERY_UPDATE)) { |
Tao Bao | e8a403d | 2015-12-31 07:44:55 -0800 | [diff] [blame] | 297 | // We need the progress bar if uncrypt will be invoked during the |
| 298 | // reboot, which might be time-consuming. |
| 299 | mRebootHasProgressBar = RecoverySystem.UNCRYPT_PACKAGE_FILE.exists() |
| 300 | && !(RecoverySystem.BLOCK_MAP_FILE.exists()); |
| 301 | pd.setTitle(context.getText(com.android.internal.R.string.reboot_to_update_title)); |
| 302 | if (mRebootHasProgressBar) { |
Tao Bao | 81dce66 | 2015-06-03 11:42:31 -0700 | [diff] [blame] | 303 | pd.setMax(100); |
Tao Bao | 81dce66 | 2015-06-03 11:42:31 -0700 | [diff] [blame] | 304 | pd.setProgress(0); |
| 305 | pd.setIndeterminate(false); |
Tao Bao | e8a403d | 2015-12-31 07:44:55 -0800 | [diff] [blame] | 306 | pd.setProgressNumberFormat(null); |
| 307 | pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); |
Tao Bao | 81dce66 | 2015-06-03 11:42:31 -0700 | [diff] [blame] | 308 | pd.setMessage(context.getText( |
Tao Bao | e8a403d | 2015-12-31 07:44:55 -0800 | [diff] [blame] | 309 | com.android.internal.R.string.reboot_to_update_prepare)); |
| 310 | } else { |
Jason Monk | b430218 | 2017-08-04 13:39:17 -0400 | [diff] [blame] | 311 | if (showSysuiReboot()) { |
| 312 | return null; |
| 313 | } |
Tao Bao | 81dce66 | 2015-06-03 11:42:31 -0700 | [diff] [blame] | 314 | pd.setIndeterminate(true); |
Tao Bao | e8a403d | 2015-12-31 07:44:55 -0800 | [diff] [blame] | 315 | pd.setMessage(context.getText( |
| 316 | com.android.internal.R.string.reboot_to_update_reboot)); |
Tao Bao | 81dce66 | 2015-06-03 11:42:31 -0700 | [diff] [blame] | 317 | } |
Tianjie Xu | 3a66fc4 | 2017-06-09 15:50:14 -0700 | [diff] [blame] | 318 | } else if (mReason != null && mReason.equals(PowerManager.REBOOT_RECOVERY)) { |
Jeff Sharkey | b4f3697 | 2017-08-02 14:33:04 -0600 | [diff] [blame] | 319 | if (RescueParty.isAttemptingFactoryReset()) { |
| 320 | // We're not actually doing a factory reset yet; we're rebooting |
| 321 | // to ask the user if they'd like to reset, so give them a less |
| 322 | // scary dialog message. |
| 323 | pd.setTitle(context.getText(com.android.internal.R.string.power_off)); |
| 324 | pd.setMessage(context.getText(com.android.internal.R.string.shutdown_progress)); |
| 325 | pd.setIndeterminate(true); |
| 326 | } else { |
| 327 | // Factory reset path. Set the dialog message accordingly. |
| 328 | pd.setTitle(context.getText(com.android.internal.R.string.reboot_to_reset_title)); |
| 329 | pd.setMessage(context.getText( |
| 330 | com.android.internal.R.string.reboot_to_reset_message)); |
| 331 | pd.setIndeterminate(true); |
| 332 | } |
Tao Bao | 90237f7 | 2015-05-21 16:25:19 -0700 | [diff] [blame] | 333 | } else { |
Jason Monk | b430218 | 2017-08-04 13:39:17 -0400 | [diff] [blame] | 334 | if (showSysuiReboot()) { |
| 335 | return null; |
| 336 | } |
Tao Bao | 90237f7 | 2015-05-21 16:25:19 -0700 | [diff] [blame] | 337 | pd.setTitle(context.getText(com.android.internal.R.string.power_off)); |
Tao Bao | 81dce66 | 2015-06-03 11:42:31 -0700 | [diff] [blame] | 338 | pd.setMessage(context.getText(com.android.internal.R.string.shutdown_progress)); |
| 339 | pd.setIndeterminate(true); |
Tao Bao | 90237f7 | 2015-05-21 16:25:19 -0700 | [diff] [blame] | 340 | } |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 341 | pd.setCancelable(false); |
| 342 | pd.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 343 | |
| 344 | pd.show(); |
Jason Monk | fd27966 | 2017-06-29 19:37:48 -0400 | [diff] [blame] | 345 | return pd; |
| 346 | } |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 347 | |
Jason Monk | b430218 | 2017-08-04 13:39:17 -0400 | [diff] [blame] | 348 | private static boolean showSysuiReboot() { |
| 349 | Log.d(TAG, "Attempting to use SysUI shutdown UI"); |
| 350 | try { |
| 351 | StatusBarManagerInternal service = LocalServices.getService( |
| 352 | StatusBarManagerInternal.class); |
| 353 | if (service.showShutdownUi(mReboot, mReason)) { |
| 354 | // Sysui will handle shutdown UI. |
| 355 | Log.d(TAG, "SysUI handling shutdown UI"); |
| 356 | return true; |
| 357 | } |
| 358 | } catch (Exception e) { |
| 359 | // If anything went wrong, ignore it and use fallback ui |
| 360 | } |
| 361 | Log.d(TAG, "SysUI is unavailable"); |
| 362 | return false; |
| 363 | } |
| 364 | |
Jason Monk | fd27966 | 2017-06-29 19:37:48 -0400 | [diff] [blame] | 365 | private static void beginShutdownSequence(Context context) { |
| 366 | synchronized (sIsStartedGuard) { |
| 367 | if (sIsStarted) { |
| 368 | Log.d(TAG, "Shutdown sequence already running, returning."); |
| 369 | return; |
| 370 | } |
| 371 | sIsStarted = true; |
| 372 | } |
| 373 | |
| 374 | sInstance.mProgressDialog = showShutdownDialog(context); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 375 | sInstance.mContext = context; |
Dianne Hackborn | f99ae76 | 2010-03-08 12:43:51 -0800 | [diff] [blame] | 376 | sInstance.mPowerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE); |
Mattias Larsson | cd4e427 | 2010-09-28 14:34:15 +0200 | [diff] [blame] | 377 | |
| 378 | // make sure we never fall asleep again |
| 379 | sInstance.mCpuWakeLock = null; |
| 380 | try { |
| 381 | sInstance.mCpuWakeLock = sInstance.mPowerManager.newWakeLock( |
| 382 | PowerManager.PARTIAL_WAKE_LOCK, TAG + "-cpu"); |
| 383 | sInstance.mCpuWakeLock.setReferenceCounted(false); |
| 384 | sInstance.mCpuWakeLock.acquire(); |
| 385 | } catch (SecurityException e) { |
| 386 | Log.w(TAG, "No permission to acquire wake lock", e); |
| 387 | sInstance.mCpuWakeLock = null; |
| 388 | } |
| 389 | |
| 390 | // also make sure the screen stays on for better user experience |
| 391 | sInstance.mScreenWakeLock = null; |
Dianne Hackborn | f99ae76 | 2010-03-08 12:43:51 -0800 | [diff] [blame] | 392 | if (sInstance.mPowerManager.isScreenOn()) { |
| 393 | try { |
Mattias Larsson | cd4e427 | 2010-09-28 14:34:15 +0200 | [diff] [blame] | 394 | sInstance.mScreenWakeLock = sInstance.mPowerManager.newWakeLock( |
| 395 | PowerManager.FULL_WAKE_LOCK, TAG + "-screen"); |
| 396 | sInstance.mScreenWakeLock.setReferenceCounted(false); |
| 397 | sInstance.mScreenWakeLock.acquire(); |
Dianne Hackborn | f99ae76 | 2010-03-08 12:43:51 -0800 | [diff] [blame] | 398 | } catch (SecurityException e) { |
| 399 | Log.w(TAG, "No permission to acquire wake lock", e); |
Mattias Larsson | cd4e427 | 2010-09-28 14:34:15 +0200 | [diff] [blame] | 400 | sInstance.mScreenWakeLock = null; |
Dianne Hackborn | f99ae76 | 2010-03-08 12:43:51 -0800 | [diff] [blame] | 401 | } |
| 402 | } |
Mattias Larsson | cd4e427 | 2010-09-28 14:34:15 +0200 | [diff] [blame] | 403 | |
| 404 | // start the thread that initiates shutdown |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 405 | sInstance.mHandler = new Handler() { |
| 406 | }; |
| 407 | sInstance.start(); |
| 408 | } |
| 409 | |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 410 | void actionDone() { |
| 411 | synchronized (mActionDoneSync) { |
| 412 | mActionDone = true; |
| 413 | mActionDoneSync.notifyAll(); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 414 | } |
| 415 | } |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 416 | |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 417 | /** |
| 418 | * Makes sure we handle the shutdown gracefully. |
| 419 | * Shuts off power regardless of radio and bluetooth state if the alloted time has passed. |
| 420 | */ |
| 421 | public void run() { |
Fyodor Kupolov | 58815ea | 2017-08-29 12:12:03 -0700 | [diff] [blame] | 422 | TimingsTraceLog shutdownTimingLog = newTimingsLog(); |
| 423 | shutdownTimingLog.traceBegin("SystemServerShutdown"); |
| 424 | metricStarted(METRIC_SYSTEM_SERVER); |
| 425 | |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 426 | BroadcastReceiver br = new BroadcastReceiver() { |
| 427 | @Override public void onReceive(Context context, Intent intent) { |
| 428 | // We don't allow apps to cancel this, so ignore the result. |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 429 | actionDone(); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 430 | } |
| 431 | }; |
Kenny Root | f547d67 | 2010-09-22 10:36:48 -0700 | [diff] [blame] | 432 | |
| 433 | /* |
| 434 | * Write a system property in case the system_server reboots before we |
| 435 | * get to the actual hardware restart. If that happens, we'll retry at |
| 436 | * the beginning of the SystemServer startup. |
| 437 | */ |
| 438 | { |
Yusuke Sato | 705ffd1 | 2015-07-21 15:52:11 -0700 | [diff] [blame] | 439 | String reason = (mReboot ? "1" : "0") + (mReason != null ? mReason : ""); |
Kenny Root | f547d67 | 2010-09-22 10:36:48 -0700 | [diff] [blame] | 440 | SystemProperties.set(SHUTDOWN_ACTION_PROPERTY, reason); |
| 441 | } |
| 442 | |
Dianne Hackborn | 19caadc | 2012-04-20 17:49:10 -0700 | [diff] [blame] | 443 | /* |
| 444 | * If we are rebooting into safe mode, write a system property |
| 445 | * indicating so. |
| 446 | */ |
| 447 | if (mRebootSafeMode) { |
| 448 | SystemProperties.set(REBOOT_SAFEMODE_PROPERTY, "1"); |
| 449 | } |
| 450 | |
Fyodor Kupolov | 291f63b | 2017-08-25 16:59:08 -0700 | [diff] [blame] | 451 | metricStarted(METRIC_SEND_BROADCAST); |
Fyodor Kupolov | 58815ea | 2017-08-29 12:12:03 -0700 | [diff] [blame] | 452 | shutdownTimingLog.traceBegin("SendShutdownBroadcast"); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 453 | Log.i(TAG, "Sending shutdown broadcast..."); |
Tao Bao | 90237f7 | 2015-05-21 16:25:19 -0700 | [diff] [blame] | 454 | |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 455 | // First send the high-level shut down broadcast. |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 456 | mActionDone = false; |
Martin Wallgren | a81d7da | 2013-02-04 14:26:51 +0100 | [diff] [blame] | 457 | Intent intent = new Intent(Intent.ACTION_SHUTDOWN); |
Fyodor Kupolov | 06aea9d | 2017-10-06 16:28:49 -0700 | [diff] [blame] | 458 | intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); |
Martin Wallgren | a81d7da | 2013-02-04 14:26:51 +0100 | [diff] [blame] | 459 | mContext.sendOrderedBroadcastAsUser(intent, |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 460 | UserHandle.ALL, null, br, mHandler, 0, null, null); |
Tao Bao | 90237f7 | 2015-05-21 16:25:19 -0700 | [diff] [blame] | 461 | |
Mike Lockwood | 098e58d | 2010-05-13 16:29:49 -0400 | [diff] [blame] | 462 | final long endTime = SystemClock.elapsedRealtime() + MAX_BROADCAST_TIME; |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 463 | synchronized (mActionDoneSync) { |
| 464 | while (!mActionDone) { |
Mike Lockwood | 098e58d | 2010-05-13 16:29:49 -0400 | [diff] [blame] | 465 | long delay = endTime - SystemClock.elapsedRealtime(); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 466 | if (delay <= 0) { |
| 467 | Log.w(TAG, "Shutdown broadcast timed out"); |
| 468 | break; |
Tao Bao | e8a403d | 2015-12-31 07:44:55 -0800 | [diff] [blame] | 469 | } else if (mRebootHasProgressBar) { |
Tao Bao | 81dce66 | 2015-06-03 11:42:31 -0700 | [diff] [blame] | 470 | int status = (int)((MAX_BROADCAST_TIME - delay) * 1.0 * |
| 471 | BROADCAST_STOP_PERCENT / MAX_BROADCAST_TIME); |
| 472 | sInstance.setRebootProgress(status, null); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 473 | } |
| 474 | try { |
Fyodor Kupolov | 231d57d | 2017-11-14 17:41:53 -0800 | [diff] [blame] | 475 | mActionDoneSync.wait(Math.min(delay, ACTION_DONE_POLL_WAIT_MS)); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 476 | } catch (InterruptedException e) { |
| 477 | } |
| 478 | } |
| 479 | } |
Tao Bao | e8a403d | 2015-12-31 07:44:55 -0800 | [diff] [blame] | 480 | if (mRebootHasProgressBar) { |
Tao Bao | 81dce66 | 2015-06-03 11:42:31 -0700 | [diff] [blame] | 481 | sInstance.setRebootProgress(BROADCAST_STOP_PERCENT, null); |
| 482 | } |
Fyodor Kupolov | 58815ea | 2017-08-29 12:12:03 -0700 | [diff] [blame] | 483 | shutdownTimingLog.traceEnd(); // SendShutdownBroadcast |
Fyodor Kupolov | 291f63b | 2017-08-25 16:59:08 -0700 | [diff] [blame] | 484 | metricEnded(METRIC_SEND_BROADCAST); |
Tao Bao | 90237f7 | 2015-05-21 16:25:19 -0700 | [diff] [blame] | 485 | |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 486 | Log.i(TAG, "Shutting down activity manager..."); |
Fyodor Kupolov | 58815ea | 2017-08-29 12:12:03 -0700 | [diff] [blame] | 487 | shutdownTimingLog.traceBegin("ShutdownActivityManager"); |
Fyodor Kupolov | 291f63b | 2017-08-25 16:59:08 -0700 | [diff] [blame] | 488 | metricStarted(METRIC_AM); |
Tao Bao | 90237f7 | 2015-05-21 16:25:19 -0700 | [diff] [blame] | 489 | |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 490 | final IActivityManager am = |
Sudheer Shanka | fc46e9b | 2016-10-21 17:55:27 -0700 | [diff] [blame] | 491 | IActivityManager.Stub.asInterface(ServiceManager.checkService("activity")); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 492 | if (am != null) { |
| 493 | try { |
| 494 | am.shutdown(MAX_BROADCAST_TIME); |
| 495 | } catch (RemoteException e) { |
| 496 | } |
| 497 | } |
Tao Bao | e8a403d | 2015-12-31 07:44:55 -0800 | [diff] [blame] | 498 | if (mRebootHasProgressBar) { |
Tao Bao | 81dce66 | 2015-06-03 11:42:31 -0700 | [diff] [blame] | 499 | sInstance.setRebootProgress(ACTIVITY_MANAGER_STOP_PERCENT, null); |
| 500 | } |
Fyodor Kupolov | 58815ea | 2017-08-29 12:12:03 -0700 | [diff] [blame] | 501 | shutdownTimingLog.traceEnd();// ShutdownActivityManager |
Fyodor Kupolov | 291f63b | 2017-08-25 16:59:08 -0700 | [diff] [blame] | 502 | metricEnded(METRIC_AM); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 503 | |
Brian Carlstrom | ff1ec4d | 2014-03-17 15:21:35 -0700 | [diff] [blame] | 504 | Log.i(TAG, "Shutting down package manager..."); |
Fyodor Kupolov | 58815ea | 2017-08-29 12:12:03 -0700 | [diff] [blame] | 505 | shutdownTimingLog.traceBegin("ShutdownPackageManager"); |
Fyodor Kupolov | 291f63b | 2017-08-25 16:59:08 -0700 | [diff] [blame] | 506 | metricStarted(METRIC_PM); |
Brian Carlstrom | ff1ec4d | 2014-03-17 15:21:35 -0700 | [diff] [blame] | 507 | |
| 508 | final PackageManagerService pm = (PackageManagerService) |
| 509 | ServiceManager.getService("package"); |
| 510 | if (pm != null) { |
| 511 | pm.shutdown(); |
| 512 | } |
Tao Bao | e8a403d | 2015-12-31 07:44:55 -0800 | [diff] [blame] | 513 | if (mRebootHasProgressBar) { |
Tao Bao | 81dce66 | 2015-06-03 11:42:31 -0700 | [diff] [blame] | 514 | sInstance.setRebootProgress(PACKAGE_MANAGER_STOP_PERCENT, null); |
| 515 | } |
Fyodor Kupolov | 58815ea | 2017-08-29 12:12:03 -0700 | [diff] [blame] | 516 | shutdownTimingLog.traceEnd(); // ShutdownPackageManager |
Fyodor Kupolov | 291f63b | 2017-08-25 16:59:08 -0700 | [diff] [blame] | 517 | metricEnded(METRIC_PM); |
Brian Carlstrom | ff1ec4d | 2014-03-17 15:21:35 -0700 | [diff] [blame] | 518 | |
Matthew Xie | 9631314 | 2012-06-29 16:57:31 -0700 | [diff] [blame] | 519 | // Shutdown radios. |
Fyodor Kupolov | 58815ea | 2017-08-29 12:12:03 -0700 | [diff] [blame] | 520 | shutdownTimingLog.traceBegin("ShutdownRadios"); |
Fyodor Kupolov | 291f63b | 2017-08-25 16:59:08 -0700 | [diff] [blame] | 521 | metricStarted(METRIC_RADIOS); |
Matthew Xie | 9631314 | 2012-06-29 16:57:31 -0700 | [diff] [blame] | 522 | shutdownRadios(MAX_RADIO_WAIT_TIME); |
Tao Bao | e8a403d | 2015-12-31 07:44:55 -0800 | [diff] [blame] | 523 | if (mRebootHasProgressBar) { |
Tao Bao | 81dce66 | 2015-06-03 11:42:31 -0700 | [diff] [blame] | 524 | sInstance.setRebootProgress(RADIO_STOP_PERCENT, null); |
| 525 | } |
Fyodor Kupolov | 58815ea | 2017-08-29 12:12:03 -0700 | [diff] [blame] | 526 | shutdownTimingLog.traceEnd(); // ShutdownRadios |
Fyodor Kupolov | 291f63b | 2017-08-25 16:59:08 -0700 | [diff] [blame] | 527 | metricEnded(METRIC_RADIOS); |
Matthew Xie | 9631314 | 2012-06-29 16:57:31 -0700 | [diff] [blame] | 528 | |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 529 | // Shutdown StorageManagerService to ensure media is in a safe state |
| 530 | IStorageShutdownObserver observer = new IStorageShutdownObserver.Stub() { |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 531 | public void onShutDownComplete(int statusCode) throws RemoteException { |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 532 | Log.w(TAG, "Result code " + statusCode + " from StorageManagerService.shutdown"); |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 533 | actionDone(); |
San Mehat | 9f7f7ca | 2010-01-07 11:34:59 -0800 | [diff] [blame] | 534 | } |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 535 | }; |
| 536 | |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 537 | Log.i(TAG, "Shutting down StorageManagerService"); |
Fyodor Kupolov | 58815ea | 2017-08-29 12:12:03 -0700 | [diff] [blame] | 538 | shutdownTimingLog.traceBegin("ShutdownStorageManager"); |
Fyodor Kupolov | 291f63b | 2017-08-25 16:59:08 -0700 | [diff] [blame] | 539 | metricStarted(METRIC_SM); |
Jeff Brown | b820371 | 2012-05-31 17:39:13 -0700 | [diff] [blame] | 540 | |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 541 | // Set initial variables and time out time. |
| 542 | mActionDone = false; |
Mike Lockwood | 098e58d | 2010-05-13 16:29:49 -0400 | [diff] [blame] | 543 | final long endShutTime = SystemClock.elapsedRealtime() + MAX_SHUTDOWN_WAIT_TIME; |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 544 | synchronized (mActionDoneSync) { |
| 545 | try { |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 546 | final IStorageManager storageManager = IStorageManager.Stub.asInterface( |
Jeff Brown | b820371 | 2012-05-31 17:39:13 -0700 | [diff] [blame] | 547 | ServiceManager.checkService("mount")); |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 548 | if (storageManager != null) { |
| 549 | storageManager.shutdown(observer); |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 550 | } else { |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 551 | Log.w(TAG, "StorageManagerService unavailable for shutdown"); |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 552 | } |
| 553 | } catch (Exception e) { |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 554 | Log.e(TAG, "Exception during StorageManagerService shutdown", e); |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 555 | } |
| 556 | while (!mActionDone) { |
Mike Lockwood | 098e58d | 2010-05-13 16:29:49 -0400 | [diff] [blame] | 557 | long delay = endShutTime - SystemClock.elapsedRealtime(); |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 558 | if (delay <= 0) { |
Fyodor Kupolov | 6e3461b | 2017-08-10 17:00:43 -0700 | [diff] [blame] | 559 | Log.w(TAG, "StorageManager shutdown wait timed out"); |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 560 | break; |
Tao Bao | e8a403d | 2015-12-31 07:44:55 -0800 | [diff] [blame] | 561 | } else if (mRebootHasProgressBar) { |
Tao Bao | 81dce66 | 2015-06-03 11:42:31 -0700 | [diff] [blame] | 562 | int status = (int)((MAX_SHUTDOWN_WAIT_TIME - delay) * 1.0 * |
| 563 | (MOUNT_SERVICE_STOP_PERCENT - RADIO_STOP_PERCENT) / |
| 564 | MAX_SHUTDOWN_WAIT_TIME); |
| 565 | status += RADIO_STOP_PERCENT; |
| 566 | sInstance.setRebootProgress(status, null); |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 567 | } |
| 568 | try { |
Fyodor Kupolov | 231d57d | 2017-11-14 17:41:53 -0800 | [diff] [blame] | 569 | mActionDoneSync.wait(Math.min(delay, ACTION_DONE_POLL_WAIT_MS)); |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 570 | } catch (InterruptedException e) { |
| 571 | } |
| 572 | } |
San Mehat | 9f7f7ca | 2010-01-07 11:34:59 -0800 | [diff] [blame] | 573 | } |
Fyodor Kupolov | 58815ea | 2017-08-29 12:12:03 -0700 | [diff] [blame] | 574 | shutdownTimingLog.traceEnd(); // ShutdownStorageManager |
Fyodor Kupolov | 291f63b | 2017-08-25 16:59:08 -0700 | [diff] [blame] | 575 | metricEnded(METRIC_SM); |
Fyodor Kupolov | 6e3461b | 2017-08-10 17:00:43 -0700 | [diff] [blame] | 576 | |
Tao Bao | e8a403d | 2015-12-31 07:44:55 -0800 | [diff] [blame] | 577 | if (mRebootHasProgressBar) { |
Tao Bao | 81dce66 | 2015-06-03 11:42:31 -0700 | [diff] [blame] | 578 | sInstance.setRebootProgress(MOUNT_SERVICE_STOP_PERCENT, null); |
San Mehat | 9f7f7ca | 2010-01-07 11:34:59 -0800 | [diff] [blame] | 579 | |
Tao Bao | e8a403d | 2015-12-31 07:44:55 -0800 | [diff] [blame] | 580 | // If it's to reboot to install an update and uncrypt hasn't been |
| 581 | // done yet, trigger it now. |
Tao Bao | 90237f7 | 2015-05-21 16:25:19 -0700 | [diff] [blame] | 582 | uncrypt(); |
| 583 | } |
| 584 | |
Fyodor Kupolov | 58815ea | 2017-08-29 12:12:03 -0700 | [diff] [blame] | 585 | shutdownTimingLog.traceEnd(); // SystemServerShutdown |
| 586 | metricEnded(METRIC_SYSTEM_SERVER); |
| 587 | saveMetrics(mReboot); |
Yusuke Sato | 705ffd1 | 2015-07-21 15:52:11 -0700 | [diff] [blame] | 588 | rebootOrShutdown(mContext, mReboot, mReason); |
Kenny Root | f547d67 | 2010-09-22 10:36:48 -0700 | [diff] [blame] | 589 | } |
| 590 | |
Fyodor Kupolov | 58815ea | 2017-08-29 12:12:03 -0700 | [diff] [blame] | 591 | private static TimingsTraceLog newTimingsLog() { |
| 592 | return new TimingsTraceLog("ShutdownTiming", Trace.TRACE_TAG_SYSTEM_SERVER); |
| 593 | } |
| 594 | |
Fyodor Kupolov | 291f63b | 2017-08-25 16:59:08 -0700 | [diff] [blame] | 595 | private static void metricStarted(String metricKey) { |
Fyodor Kupolov | 58815ea | 2017-08-29 12:12:03 -0700 | [diff] [blame] | 596 | synchronized (TRON_METRICS) { |
| 597 | TRON_METRICS.put(metricKey, -1 * SystemClock.elapsedRealtime()); |
| 598 | } |
Fyodor Kupolov | 291f63b | 2017-08-25 16:59:08 -0700 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | private static void metricEnded(String metricKey) { |
Fyodor Kupolov | 58815ea | 2017-08-29 12:12:03 -0700 | [diff] [blame] | 602 | synchronized (TRON_METRICS) { |
| 603 | TRON_METRICS |
| 604 | .put(metricKey, SystemClock.elapsedRealtime() + TRON_METRICS.get(metricKey)); |
| 605 | } |
Fyodor Kupolov | 291f63b | 2017-08-25 16:59:08 -0700 | [diff] [blame] | 606 | } |
| 607 | |
Tao Bao | 81dce66 | 2015-06-03 11:42:31 -0700 | [diff] [blame] | 608 | private void setRebootProgress(final int progress, final CharSequence message) { |
Tao Bao | 90237f7 | 2015-05-21 16:25:19 -0700 | [diff] [blame] | 609 | mHandler.post(new Runnable() { |
| 610 | @Override |
| 611 | public void run() { |
| 612 | if (mProgressDialog != null) { |
| 613 | mProgressDialog.setProgress(progress); |
Tao Bao | 81dce66 | 2015-06-03 11:42:31 -0700 | [diff] [blame] | 614 | if (message != null) { |
| 615 | mProgressDialog.setMessage(message); |
| 616 | } |
Tao Bao | 90237f7 | 2015-05-21 16:25:19 -0700 | [diff] [blame] | 617 | } |
| 618 | } |
| 619 | }); |
| 620 | } |
| 621 | |
Tao Bao | 81dce66 | 2015-06-03 11:42:31 -0700 | [diff] [blame] | 622 | private void shutdownRadios(final int timeout) { |
Jeff Brown | b820371 | 2012-05-31 17:39:13 -0700 | [diff] [blame] | 623 | // If a radio is wedged, disabling it may hang so we do this work in another thread, |
| 624 | // just in case. |
| 625 | final long endTime = SystemClock.elapsedRealtime() + timeout; |
| 626 | final boolean[] done = new boolean[1]; |
| 627 | Thread t = new Thread() { |
| 628 | public void run() { |
Fyodor Kupolov | 58815ea | 2017-08-29 12:12:03 -0700 | [diff] [blame] | 629 | TimingsTraceLog shutdownTimingsTraceLog = newTimingsLog(); |
Wei Wang | 4d28254 | 2017-08-07 15:35:13 -0700 | [diff] [blame] | 630 | boolean bluetoothReadyForShutdown; |
Jeff Brown | b820371 | 2012-05-31 17:39:13 -0700 | [diff] [blame] | 631 | boolean radioOff; |
| 632 | |
Jeff Brown | b820371 | 2012-05-31 17:39:13 -0700 | [diff] [blame] | 633 | final ITelephony phone = |
| 634 | ITelephony.Stub.asInterface(ServiceManager.checkService("phone")); |
Matthew Xie | 9631314 | 2012-06-29 16:57:31 -0700 | [diff] [blame] | 635 | final IBluetoothManager bluetooth = |
| 636 | IBluetoothManager.Stub.asInterface(ServiceManager.checkService( |
| 637 | BluetoothAdapter.BLUETOOTH_MANAGER_SERVICE)); |
Jeff Brown | b820371 | 2012-05-31 17:39:13 -0700 | [diff] [blame] | 638 | |
| 639 | try { |
Wei Wang | 4d28254 | 2017-08-07 15:35:13 -0700 | [diff] [blame] | 640 | bluetoothReadyForShutdown = bluetooth == null || |
Christine Hallstrom | 995c90a | 2016-05-25 15:49:08 -0700 | [diff] [blame] | 641 | bluetooth.getState() == BluetoothAdapter.STATE_OFF; |
Wei Wang | 4d28254 | 2017-08-07 15:35:13 -0700 | [diff] [blame] | 642 | if (!bluetoothReadyForShutdown) { |
Jeff Brown | b820371 | 2012-05-31 17:39:13 -0700 | [diff] [blame] | 643 | Log.w(TAG, "Disabling Bluetooth..."); |
Fyodor Kupolov | 291f63b | 2017-08-25 16:59:08 -0700 | [diff] [blame] | 644 | metricStarted(METRIC_BT); |
Svetoslav Ganov | ac69be5 | 2016-06-29 17:31:44 -0700 | [diff] [blame] | 645 | bluetooth.disable(mContext.getPackageName(), false); // disable but don't persist new state |
Jeff Brown | b820371 | 2012-05-31 17:39:13 -0700 | [diff] [blame] | 646 | } |
| 647 | } catch (RemoteException ex) { |
| 648 | Log.e(TAG, "RemoteException during bluetooth shutdown", ex); |
Wei Wang | 4d28254 | 2017-08-07 15:35:13 -0700 | [diff] [blame] | 649 | bluetoothReadyForShutdown = true; |
Jeff Brown | b820371 | 2012-05-31 17:39:13 -0700 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | try { |
Naveen Kalla | bd77236 | 2014-08-02 01:03:42 -0700 | [diff] [blame] | 653 | radioOff = phone == null || !phone.needMobileRadioShutdown(); |
Jeff Brown | b820371 | 2012-05-31 17:39:13 -0700 | [diff] [blame] | 654 | if (!radioOff) { |
Naveen Kalla | bd77236 | 2014-08-02 01:03:42 -0700 | [diff] [blame] | 655 | Log.w(TAG, "Turning off cellular radios..."); |
Fyodor Kupolov | 291f63b | 2017-08-25 16:59:08 -0700 | [diff] [blame] | 656 | metricStarted(METRIC_RADIO); |
Naveen Kalla | bd77236 | 2014-08-02 01:03:42 -0700 | [diff] [blame] | 657 | phone.shutdownMobileRadios(); |
Jeff Brown | b820371 | 2012-05-31 17:39:13 -0700 | [diff] [blame] | 658 | } |
| 659 | } catch (RemoteException ex) { |
| 660 | Log.e(TAG, "RemoteException during radio shutdown", ex); |
| 661 | radioOff = true; |
| 662 | } |
| 663 | |
Ruchi Kandoi | 20bc30f | 2017-10-18 13:06:40 -0700 | [diff] [blame] | 664 | Log.i(TAG, "Waiting for Bluetooth and Radio..."); |
Jeff Brown | b820371 | 2012-05-31 17:39:13 -0700 | [diff] [blame] | 665 | |
Tao Bao | 81dce66 | 2015-06-03 11:42:31 -0700 | [diff] [blame] | 666 | long delay = endTime - SystemClock.elapsedRealtime(); |
| 667 | while (delay > 0) { |
Tao Bao | e8a403d | 2015-12-31 07:44:55 -0800 | [diff] [blame] | 668 | if (mRebootHasProgressBar) { |
Tao Bao | 81dce66 | 2015-06-03 11:42:31 -0700 | [diff] [blame] | 669 | int status = (int)((timeout - delay) * 1.0 * |
| 670 | (RADIO_STOP_PERCENT - PACKAGE_MANAGER_STOP_PERCENT) / timeout); |
| 671 | status += PACKAGE_MANAGER_STOP_PERCENT; |
| 672 | sInstance.setRebootProgress(status, null); |
| 673 | } |
| 674 | |
Wei Wang | 4d28254 | 2017-08-07 15:35:13 -0700 | [diff] [blame] | 675 | if (!bluetoothReadyForShutdown) { |
Jeff Brown | b820371 | 2012-05-31 17:39:13 -0700 | [diff] [blame] | 676 | try { |
Wei Wang | 4d28254 | 2017-08-07 15:35:13 -0700 | [diff] [blame] | 677 | // BLE only mode can happen when BT is turned off |
| 678 | // We will continue shutting down in such case |
| 679 | bluetoothReadyForShutdown = |
| 680 | bluetooth.getState() == BluetoothAdapter.STATE_OFF || |
| 681 | bluetooth.getState() == BluetoothAdapter.STATE_BLE_TURNING_OFF || |
| 682 | bluetooth.getState() == BluetoothAdapter.STATE_BLE_ON; |
Jeff Brown | b820371 | 2012-05-31 17:39:13 -0700 | [diff] [blame] | 683 | } catch (RemoteException ex) { |
| 684 | Log.e(TAG, "RemoteException during bluetooth shutdown", ex); |
Wei Wang | 4d28254 | 2017-08-07 15:35:13 -0700 | [diff] [blame] | 685 | bluetoothReadyForShutdown = true; |
Jeff Brown | b820371 | 2012-05-31 17:39:13 -0700 | [diff] [blame] | 686 | } |
Wei Wang | 4d28254 | 2017-08-07 15:35:13 -0700 | [diff] [blame] | 687 | if (bluetoothReadyForShutdown) { |
Jeff Brown | b820371 | 2012-05-31 17:39:13 -0700 | [diff] [blame] | 688 | Log.i(TAG, "Bluetooth turned off."); |
Fyodor Kupolov | 291f63b | 2017-08-25 16:59:08 -0700 | [diff] [blame] | 689 | metricEnded(METRIC_BT); |
Fyodor Kupolov | 58815ea | 2017-08-29 12:12:03 -0700 | [diff] [blame] | 690 | shutdownTimingsTraceLog |
Fyodor Kupolov | 291f63b | 2017-08-25 16:59:08 -0700 | [diff] [blame] | 691 | .logDuration("ShutdownBt", TRON_METRICS.get(METRIC_BT)); |
Jeff Brown | b820371 | 2012-05-31 17:39:13 -0700 | [diff] [blame] | 692 | } |
| 693 | } |
| 694 | if (!radioOff) { |
| 695 | try { |
Naveen Kalla | bd77236 | 2014-08-02 01:03:42 -0700 | [diff] [blame] | 696 | radioOff = !phone.needMobileRadioShutdown(); |
Jeff Brown | b820371 | 2012-05-31 17:39:13 -0700 | [diff] [blame] | 697 | } catch (RemoteException ex) { |
| 698 | Log.e(TAG, "RemoteException during radio shutdown", ex); |
| 699 | radioOff = true; |
| 700 | } |
| 701 | if (radioOff) { |
| 702 | Log.i(TAG, "Radio turned off."); |
Fyodor Kupolov | 291f63b | 2017-08-25 16:59:08 -0700 | [diff] [blame] | 703 | metricEnded(METRIC_RADIO); |
Fyodor Kupolov | 58815ea | 2017-08-29 12:12:03 -0700 | [diff] [blame] | 704 | shutdownTimingsTraceLog |
Fyodor Kupolov | 291f63b | 2017-08-25 16:59:08 -0700 | [diff] [blame] | 705 | .logDuration("ShutdownRadio", TRON_METRICS.get(METRIC_RADIO)); |
Jeff Brown | b820371 | 2012-05-31 17:39:13 -0700 | [diff] [blame] | 706 | } |
| 707 | } |
Jeff Brown | b820371 | 2012-05-31 17:39:13 -0700 | [diff] [blame] | 708 | |
Ruchi Kandoi | 20bc30f | 2017-10-18 13:06:40 -0700 | [diff] [blame] | 709 | if (radioOff && bluetoothReadyForShutdown) { |
| 710 | Log.i(TAG, "Radio and Bluetooth shutdown complete."); |
Jeff Brown | b820371 | 2012-05-31 17:39:13 -0700 | [diff] [blame] | 711 | done[0] = true; |
| 712 | break; |
| 713 | } |
Fyodor Kupolov | 231d57d | 2017-11-14 17:41:53 -0800 | [diff] [blame] | 714 | SystemClock.sleep(RADIOS_STATE_POLL_SLEEP_MS); |
Tao Bao | 81dce66 | 2015-06-03 11:42:31 -0700 | [diff] [blame] | 715 | delay = endTime - SystemClock.elapsedRealtime(); |
Jeff Brown | b820371 | 2012-05-31 17:39:13 -0700 | [diff] [blame] | 716 | } |
| 717 | } |
| 718 | }; |
| 719 | |
| 720 | t.start(); |
| 721 | try { |
| 722 | t.join(timeout); |
| 723 | } catch (InterruptedException ex) { |
| 724 | } |
| 725 | if (!done[0]) { |
Ruchi Kandoi | 20bc30f | 2017-10-18 13:06:40 -0700 | [diff] [blame] | 726 | Log.w(TAG, "Timed out waiting for Radio and Bluetooth shutdown."); |
Jeff Brown | b820371 | 2012-05-31 17:39:13 -0700 | [diff] [blame] | 727 | } |
| 728 | } |
| 729 | |
Kenny Root | f547d67 | 2010-09-22 10:36:48 -0700 | [diff] [blame] | 730 | /** |
| 731 | * Do not call this directly. Use {@link #reboot(Context, String, boolean)} |
Fyodor Kupolov | 291f63b | 2017-08-25 16:59:08 -0700 | [diff] [blame] | 732 | * or {@link #shutdown(Context, String, boolean)} instead. |
Kenny Root | f547d67 | 2010-09-22 10:36:48 -0700 | [diff] [blame] | 733 | * |
riddle_hsu | d3b3717 | 2015-03-19 01:11:55 +0800 | [diff] [blame] | 734 | * @param context Context used to vibrate or null without vibration |
Kenny Root | f547d67 | 2010-09-22 10:36:48 -0700 | [diff] [blame] | 735 | * @param reboot true to reboot or false to shutdown |
Yusuke Sato | 705ffd1 | 2015-07-21 15:52:11 -0700 | [diff] [blame] | 736 | * @param reason reason for reboot/shutdown |
Kenny Root | f547d67 | 2010-09-22 10:36:48 -0700 | [diff] [blame] | 737 | */ |
riddle_hsu | d3b3717 | 2015-03-19 01:11:55 +0800 | [diff] [blame] | 738 | public static void rebootOrShutdown(final Context context, boolean reboot, String reason) { |
Kenny Root | f547d67 | 2010-09-22 10:36:48 -0700 | [diff] [blame] | 739 | if (reboot) { |
| 740 | Log.i(TAG, "Rebooting, reason: " + reason); |
Nick Kralevich | dbcf2d7 | 2013-04-18 14:41:40 -0700 | [diff] [blame] | 741 | PowerManagerService.lowLevelReboot(reason); |
| 742 | Log.e(TAG, "Reboot failed, will attempt shutdown instead"); |
Yusuke Sato | 705ffd1 | 2015-07-21 15:52:11 -0700 | [diff] [blame] | 743 | reason = null; |
riddle_hsu | d3b3717 | 2015-03-19 01:11:55 +0800 | [diff] [blame] | 744 | } else if (SHUTDOWN_VIBRATE_MS > 0 && context != null) { |
Mike Lockwood | a717f64 | 2010-04-01 20:01:44 -0700 | [diff] [blame] | 745 | // vibrate before shutting down |
riddle_hsu | d3b3717 | 2015-03-19 01:11:55 +0800 | [diff] [blame] | 746 | Vibrator vibrator = new SystemVibrator(context); |
Brad Fitzpatrick | 26e9cf3 | 2010-10-19 09:33:09 -0700 | [diff] [blame] | 747 | try { |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 748 | vibrator.vibrate(SHUTDOWN_VIBRATE_MS, VIBRATION_ATTRIBUTES); |
Brad Fitzpatrick | 26e9cf3 | 2010-10-19 09:33:09 -0700 | [diff] [blame] | 749 | } catch (Exception e) { |
| 750 | // Failure to vibrate shouldn't interrupt shutdown. Just log it. |
| 751 | Log.w(TAG, "Failed to vibrate during shutdown.", e); |
| 752 | } |
| 753 | |
Mike Lockwood | a717f64 | 2010-04-01 20:01:44 -0700 | [diff] [blame] | 754 | // vibrator is asynchronous so we need to wait to avoid shutting down too soon. |
| 755 | try { |
| 756 | Thread.sleep(SHUTDOWN_VIBRATE_MS); |
Brad Fitzpatrick | e331644 | 2010-10-14 19:40:56 -0700 | [diff] [blame] | 757 | } catch (InterruptedException unused) { |
Mike Lockwood | a717f64 | 2010-04-01 20:01:44 -0700 | [diff] [blame] | 758 | } |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 759 | } |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 760 | // Shutdown power |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 761 | Log.i(TAG, "Performing low-level shutdown..."); |
Yusuke Sato | 705ffd1 | 2015-07-21 15:52:11 -0700 | [diff] [blame] | 762 | PowerManagerService.lowLevelShutdown(reason); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 763 | } |
Tao Bao | 90237f7 | 2015-05-21 16:25:19 -0700 | [diff] [blame] | 764 | |
Fyodor Kupolov | 291f63b | 2017-08-25 16:59:08 -0700 | [diff] [blame] | 765 | private static void saveMetrics(boolean reboot) { |
| 766 | StringBuilder metricValue = new StringBuilder(); |
| 767 | metricValue.append("reboot:"); |
| 768 | metricValue.append(reboot ? "y" : "n"); |
| 769 | final int metricsSize = TRON_METRICS.size(); |
| 770 | for (int i = 0; i < metricsSize; i++) { |
| 771 | final String name = TRON_METRICS.keyAt(i); |
| 772 | final long value = TRON_METRICS.valueAt(i); |
| 773 | if (value < 0) { |
| 774 | Log.e(TAG, "metricEnded wasn't called for " + name); |
| 775 | continue; |
| 776 | } |
| 777 | metricValue.append(',').append(name).append(':').append(value); |
| 778 | } |
| 779 | File tmp = new File(METRICS_FILE_BASENAME + ".tmp"); |
| 780 | boolean saved = false; |
| 781 | try (FileOutputStream fos = new FileOutputStream(tmp)) { |
| 782 | fos.write(metricValue.toString().getBytes(StandardCharsets.UTF_8)); |
| 783 | saved = true; |
| 784 | } catch (IOException e) { |
| 785 | Log.e(TAG,"Cannot save shutdown metrics", e); |
| 786 | } |
| 787 | if (saved) { |
| 788 | tmp.renameTo(new File(METRICS_FILE_BASENAME + ".txt")); |
| 789 | } |
| 790 | } |
| 791 | |
Tao Bao | 90237f7 | 2015-05-21 16:25:19 -0700 | [diff] [blame] | 792 | private void uncrypt() { |
| 793 | Log.i(TAG, "Calling uncrypt and monitoring the progress..."); |
| 794 | |
Tao Bao | e8a403d | 2015-12-31 07:44:55 -0800 | [diff] [blame] | 795 | final RecoverySystem.ProgressListener progressListener = |
| 796 | new RecoverySystem.ProgressListener() { |
| 797 | @Override |
| 798 | public void onProgress(int status) { |
| 799 | if (status >= 0 && status < 100) { |
| 800 | // Scale down to [MOUNT_SERVICE_STOP_PERCENT, 100). |
| 801 | status = (int)(status * (100.0 - MOUNT_SERVICE_STOP_PERCENT) / 100); |
| 802 | status += MOUNT_SERVICE_STOP_PERCENT; |
| 803 | CharSequence msg = mContext.getText( |
| 804 | com.android.internal.R.string.reboot_to_update_package); |
| 805 | sInstance.setRebootProgress(status, msg); |
| 806 | } else if (status == 100) { |
| 807 | CharSequence msg = mContext.getText( |
| 808 | com.android.internal.R.string.reboot_to_update_reboot); |
| 809 | sInstance.setRebootProgress(status, msg); |
| 810 | } else { |
| 811 | // Ignored |
| 812 | } |
| 813 | } |
| 814 | }; |
| 815 | |
Tao Bao | 90237f7 | 2015-05-21 16:25:19 -0700 | [diff] [blame] | 816 | final boolean[] done = new boolean[1]; |
| 817 | done[0] = false; |
| 818 | Thread t = new Thread() { |
| 819 | @Override |
| 820 | public void run() { |
Tao Bao | e8a403d | 2015-12-31 07:44:55 -0800 | [diff] [blame] | 821 | RecoverySystem rs = (RecoverySystem) mContext.getSystemService( |
| 822 | Context.RECOVERY_SERVICE); |
| 823 | String filename = null; |
Tao Bao | 90237f7 | 2015-05-21 16:25:19 -0700 | [diff] [blame] | 824 | try { |
Tao Bao | e8a403d | 2015-12-31 07:44:55 -0800 | [diff] [blame] | 825 | filename = FileUtils.readTextFile(RecoverySystem.UNCRYPT_PACKAGE_FILE, 0, null); |
| 826 | rs.processPackage(mContext, new File(filename), progressListener); |
| 827 | } catch (IOException e) { |
| 828 | Log.e(TAG, "Error uncrypting file", e); |
Tao Bao | 90237f7 | 2015-05-21 16:25:19 -0700 | [diff] [blame] | 829 | } |
| 830 | done[0] = true; |
| 831 | } |
| 832 | }; |
| 833 | t.start(); |
| 834 | |
| 835 | try { |
| 836 | t.join(MAX_UNCRYPT_WAIT_TIME); |
| 837 | } catch (InterruptedException unused) { |
| 838 | } |
| 839 | if (!done[0]) { |
| 840 | Log.w(TAG, "Timed out waiting for uncrypt."); |
Tianjie Xu | 036d086 | 2016-09-24 15:39:56 -0700 | [diff] [blame] | 841 | final int uncryptTimeoutError = 100; |
| 842 | String timeoutMessage = String.format("uncrypt_time: %d\n" + "uncrypt_error: %d\n", |
| 843 | MAX_UNCRYPT_WAIT_TIME / 1000, uncryptTimeoutError); |
| 844 | try { |
| 845 | FileUtils.stringToFile(RecoverySystem.UNCRYPT_STATUS_FILE, timeoutMessage); |
| 846 | } catch (IOException e) { |
| 847 | Log.e(TAG, "Failed to write timeout message to uncrypt status", e); |
| 848 | } |
Tao Bao | 90237f7 | 2015-05-21 16:25:19 -0700 | [diff] [blame] | 849 | } |
| 850 | } |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 851 | } |