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