1.
BatteryService.java
private void shutdownIfOverTempLocked() {
// shut down gracefully if temperature is too high (> 68.0C by default)
// wait until the system has booted before attempting to display the
// shutdown dialog.
if (mHealthInfo.batteryTemperature > mShutdownBatteryTemperature) {
mHandler.post(new Runnable() {
@Override
public void run() {
if (mActivityManagerInternal.isSystemReady()) {
if (DEBUG) {
Slog.d(TAG, "ready to shutdown battery temp high");
RuntimeException e = new RuntimeException("error is here");
e.fillInStackTrace();
System.err.println("Failure starting apk: " + e);
Slog.i("run stack", "xxx" + this, e);
}
//debug_stamp
Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
intent.putExtra(Intent.EXTRA_REASON,
PowerManager.SHUTDOWN_BATTERY_THERMAL_STATE);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivityAsUser(intent, UserHandle.CURRENT);
}
}
});
}
}
监测到温度超过68摄氏度,调用关机Activity
2.
ShutdownActivity.java
Thread thr = new Thread("ShutdownActivity") {
@Override
public void run() {
IPowerManager pm = IPowerManager.Stub.asInterface(
ServiceManager.getService(Context.POWER_SERVICE));
try {
if (mReboot) {
pm.reboot(mConfirm, null, false);
} else {
Slog.i(TAG, "onCreate(): pm.shutdown");
pm.shutdown(mConfirm, reason, false);
}
} catch (RemoteException e) {
}
}
};
新建了一个线程 设为 2858
3.PowerManagerService.java
/**
* Shuts down the device.
*
* @param confirm If true, shows a shutdown confirmation dialog.
* @param wait If true, this call waits for the shutdown to complete and does not return.
*/
@Override // Binder call
public void shutdown(boolean confirm, String reason, boolean wait) {
mContext.enforceCallingOrSelfPermission(android.Manifest.permission.REBOOT, null);
Slog.w(TAG, "Binder call shutdown1"); 2858
final long ident = Binder.clearCallingIdentity();
try {
shutdownOrRebootInternal(HALT_MODE_SHUTDOWN, confirm, reason, wait);
Slog.w(TAG, "Binder call shutdown12");
} finally {
Slog.w(TAG, "Binder call shutdown1t");
Binder.restoreCallingIdentity(ident);
Slog.w(TAG, "Binder call shutdown13");
}
}
注意此处经过binder,
刚开始时,线程号还是 2858
2008是system_server进程号
08-08 16:02:48.104 2008 2008 W ActivityThread: handleWindowVisibility: no activity for token Token{1db3e02 ActivityRecord{554f34d u0 android/com.android.internal.app.ShutdownActivity t12}}
08-08 16:02:48.129 2008 2008 I ShutdownActivity: onCreate(): confirm=false
08-08 16:02:48.134 2008 2858 I ShutdownActivity: onCreate(): pm.shutdown
08-08 16:02:48.134 2008 2858 W PowerManagerService: Binder call shutdown1
08-08 16:02:48.134 2008 2858 W PowerManagerService: Binder call.0 shutdownOrRebootInternal
//新建了一个线程
08-08 16:02:48.136 2008 2076 W PowerManagerService: Binder call.2 shutdownOrRebootInternal
08-08 16:02:48.137 2008 2076 D ShutdownThread: shutdown
08-08 16:02:48.137 2008 2076 W System.err: Failure starting apk: java.lang.RuntimeException: error is here
08-08 16:02:48.138 2008 2076 D run stack: xxx
08-08 16:02:48.138 2008 2076 D run stack: java.lang.RuntimeException: error is here
08-08 16:02:48.138 2008 2076 D run stack: at com.android.server.power.ShutdownThread.shutdown(ShutdownThread.java:152)
08-08 16:02:48.138 2008 2076 D run stack: at com.android.server.power.PowerManagerService$2.run(PowerManagerService.java:2745)
08-08 16:02:48.138 2008 2076 D run stack: at android.os.Handler.handleCallback(Handler.java:873)
08-08 16:02:48.138 2008 2076 D run stack: at android.os.Handler.dispatchMessage(Handler.java:99)
08-08 16:02:48.138 2008 2076 D run stack: at android.os.Looper.loop(Looper.java:193)
08-08 16:02:48.138 2008 2076 D run stack: at android.os.HandlerThread.run(HandlerThread.java:65)
08-08 16:02:48.138 2008 2076 D run stack: at com.android.server.ServiceThread.run(ServiceThread.java:44)
08-08 16:02:48.138 2008 2076 D run stack: at com.android.server.UiThread.run(UiThread.java:43)
08-08 16:02:48.138 2008 2076 D ShutdownThread: Notifying thread to start shutdown longPressBehavior=1
08-08 16:02:48.140 2008 2076 D ShutdownThread: Attempting to use SysUI shutdown UI
08-08 16:02:48.141 2008 2076 D ShutdownThread: SysUI handling shutdown UI
08-08 16:02:48.145 2008 2858 W PowerManagerService: Binder call shutdown12
08-08 16:02:48.145 2008 2858 W PowerManagerService: Binder call shutdown1t
08-08 16:02:48.145 2008 2858 W PowerManagerService: Binder call shutdown13
08-08 16:02:48.212 2008 2859 I ShutdownThread: Sending shutdown broadcast...
08-08 16:02:48.222 2008 2008 W Looper : Slow dispatch took 118ms main h=android.app.ActivityThread$H c=null m=159
08-08 16:02:48.257 2583 2583 I Launcher: in launcher onResume.0
08-08 16:02:48.320 2008 2008 W SyncManager: Writing sync state before shutdown...
08-08 16:02:48.336 2008 2084 I EntropyMixer: Writing entropy...
08-08 16:02:48.355 2008 2008 I AlarmManager: AlarmManagerService receive shutting down set rtc alarm time: 0
08-08 16:02:48.363 2008 2008 I StatsCompanionService: StatsCompanionService noticed a shutdown.
08-08 16:02:48.393 2008 2859 I ShutdownThread: Shutting down activity manager...
08-08 16:02:48.405 2583 2583 I Launcher: in launcher onPause.
08-08 16:02:48.422 2583 2583 I Launcher: in launcher onStop.
08-08 16:02:48.424 2583 2583 E Launcher: in launcher handleMessage2.
08-08 16:02:48.444 2008 2859 W AppOps : Writing app ops before shutdown...
08-08 16:02:48.457 2008 2859 I UsageStatsService: User[0] Flushing usage stats to disk
08-08 16:02:48.513 2008 2859 W BatteryStats: Writing battery stats before shutdown...
08-08 16:02:48.540 2008 2080 E BatteryExternalStatsWorker: no controller energy info supplied for wifi
08-08 16:02:48.541 2008 2080 E BatteryExternalStatsWorker: no controller energy info supplied for bluetooth
08-08 16:02:48.558 2008 2080 W KernelCpuProcReader: File not exist: /proc/uid_cpupower/time_in_state
08-08 16:02:48.559 2008 2080 W KernelCpuProcReader: File not exist: /proc/uid_cpupower/concurrent_active_time
08-08 16:02:48.560 2008 2080 W KernelCpuProcReader: File not exist: /proc/uid_cpupower/concurrent_policy_time
08-08 16:02:48.571 2008 2080 W BatteryExternalStatsWorker: modem info is invalid: ModemActivityInfo{ mTimestamp=0 mSleepTimeMs=0 mIdleTimeMs=0 mTxTimeMs[]=[0, 0, 0, 0, 0] mRxTimeMs=0 mEnergyUsed=0}
08-08 16:02:48.603 2008 2859 W ProcessStatsService: Writing process stats before shutdown...
08-08 16:02:48.618 2008 2859 I ProcessStatsService: Pruning old procstats: /data/system/procstats/state-2020-08-07-23-26-29.bin
08-08 16:02:48.621 2008 2859 I ShutdownThread: Shutting down package manager...
08-08 16:02:48.657 2298 2319 V PhoneInterfaceManager: [PhoneIntfMgr] 1 Phones are shutdown.
08-08 16:02:48.660 2008 2864 I ShutdownThread: Waiting for Radio...
08-08 16:02:48.660 2008 2864 I ShutdownThread: Radio shutdown complete.
08-08 16:02:48.665 2008 2859 E VibratorService: vibratorOff command failed (1).
08-08 16:02:48.668 1657 1657 W hwservicemanager: getTransport: Cannot find entry android.hardware.vibrator@1.0::IVibrator/default in either framework or device manifest.
08-08 16:02:48.669 2008 2859 E VibratorService: vibratorOn command failed (1).
08-08 16:02:48.763 2008 2019 I system_server: Background concurrent copying GC freed 50705(3MB) AllocSpace objects, 58(2MB) LOS objects, 49% free, 6MB/13MB, paused 359us total 182.378ms
08-08 16:02:49.170 2008 2008 E VibratorService: vibratorOff command failed (1).
08-08 16:02:49.170 2008 2859 I ShutdownThread: Performing low-level shutdown...
08-08 16:02:49.171 2008 2859 E PowerManagerService: lowLevelShutdown
4.ShutdownThread.java
public static void shutdown(final Context context, String reason, boolean confirm) {
mReboot = false;
mRebootSafeMode = false;
mReason = reason;
Log.d(TAG, "shutdown");
RuntimeException e = new RuntimeException("error is here");
e.fillInStackTrace();
System.err.println("Failure starting apk: " + e);
Log.d("run stack", "xxx", e);
//debug_stamp
shutdownInner(context, confirm);
}
private static void shutdownInner(final Context context, boolean confirm) {
// ShutdownThread is called from many places, so best to verify here that the context passed
// in is themed.
context.assertRuntimeOverlayThemable();
// ensure that only one thread is trying to power down.
// any additional calls are just returned
synchronized (sIsStartedGuard) {
if (sIsStarted) {
Log.d(TAG, "Request to shutdown already running, returning.");
return;
}
}
final int longPressBehavior = context.getResources().getInteger(
com.android.internal.R.integer.config_longPressOnPowerBehavior);
final int resourceId = mRebootSafeMode
? com.android.internal.R.string.reboot_safemode_confirm
: (longPressBehavior == 2
? com.android.internal.R.string.shutdown_confirm_question
: com.android.internal.R.string.shutdown_confirm);
Log.d(TAG, "Notifying thread to start shutdown longPressBehavior=" + longPressBehavior);
if (confirm) {
//有确认对话框 关机之前先确认
final CloseDialogReceiver closer = new CloseDialogReceiver(context);
if (sConfirmDialog != null) {
sConfirmDialog.dismiss();
}
sConfirmDialog = new AlertDialog.Builder(context)
.setTitle(mRebootSafeMode
? com.android.internal.R.string.reboot_safemode_title
: com.android.internal.R.string.power_off)
.setMessage(resourceId)
.setPositiveButton(com.android.internal.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
beginShutdownSequence(context);
}
})
.setNegativeButton(com.android.internal.R.string.no, null)
.create();
closer.dialog = sConfirmDialog;
sConfirmDialog.setOnDismissListener(closer);
sConfirmDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
sConfirmDialog.show();
} else {
beginShutdownSequence(context);
//没有确认 直接关机
}
}
关机动作分成2部分完成
beginShutdownSequence
java端的关机动作 包管理 等的关闭
// Remaining work will be done by init, including vold shutdown
rebootOrShutdown(mContext, mReboot, mReason);
// Shutdown power
Log.i(TAG, "Performing low-level shutdown...");
PowerManagerService.lowLevelShutdown(reason);
//其实就是写一个系统属性
java SystemProperties.set("sys.powerctl", "shutdown," + reason);
类似于windows的注册表机制
当写入的时候,会调用init的set方法 通过jni本地方法
最终被init 守护进程所感知
低级关机动作 由init守护进程完成
包括文件系统的脏数据同步,卸载等