Monkey test shutdown/restart problem analysis (3)

previous articles

Android shutdown/restart reason analysis
monkey test shutdown/restart problem analysis (1)
monkey test shutdown/restart problem analysis (2)
monkey test shutdown/restart problem analysis (3)

shutdown process

For the shutdown process, please refer to the following articles by other people https://blog.csdn.net/qq_29413633/article/details/120848128

The trigger process may be different, such as long press to shutdown, click the dialog shutdown button to shutdown, and so on.
1. Upper layer shutdown process

Eventually back to ShutdowmThread.

This question follows the monkey test shutdown/restart problem analysis (2) and continues down, calling mBarService.shutdown() from systemui; finally go to frameworks/base/services/core/java/com/android/server/statusbar /StatusBarManagerService.java

1.1、StatusBarManagerService

    public void shutdown() {
    
    
        enforceStatusBarService();
        String reason = PowerManager.SHUTDOWN_USER_REQUESTED;
        ShutdownCheckPoints.recordCheckPoint(Binder.getCallingPid(), reason);//这里就是ShutdownCheckPoints log打印的地方,用来跟踪是哪个进程调用了关机
        final long identity = Binder.clearCallingIdentity();
        try {
    
    
            mNotificationDelegate.prepareForPossibleShutdown();
            // ShutdownThread displays UI, so give it a UI context.
            mHandler.post(() ->
                    ShutdownThread.shutdown(getUiContext(), reason, false));//这里调用到ShutdownThread
        } finally {
    
    
            Binder.restoreCallingIdentity(identity);
        }
    }

1.2、ShutdowmThread.run()

    public void run() {
    
    
        TimingsTraceLog shutdownTimingLog = newTimingsLog();
        shutdownTimingLog.traceBegin("SystemServerShutdown");
        ...
        //关机之前关掉其它service等一系列操作。
        ...
        // Remaining work will be done by init, including vold shutdown
        rebootOrShutdown(mContext, mReboot, mReason);
    }

1.3、ShutdownThread.rebootOrShutdown()

    public static void rebootOrShutdown(final Context context, boolean reboot, String reason) {
    
    
        //add for sreen off earlier
        if (sInstance.mPowerManager != null) {
    
    
            Log.i(TAG, "rebootOrShutdown:goToSleep");
            sInstance.mPowerManager.goToSleep(SystemClock.uptimeMillis());
        }

        if (reboot) {
    
    
            Log.i(TAG, "Rebooting, reason: " + reason);
            PowerManagerService.lowLevelReboot(reason);//重启
            Log.e(TAG, "Reboot failed, will attempt shutdown instead");
            reason = null;
        } else if (SHUTDOWN_VIBRATE_MS > 0 && context != null) {
    
    
            // vibrate before shutting down
            Vibrator vibrator = new SystemVibrator(context);
            try {
    
    
                vibrator.vibrate(SHUTDOWN_VIBRATE_MS, VIBRATION_ATTRIBUTES);
            } catch (Exception e) {
    
    
                // Failure to vibrate shouldn't interrupt shutdown.  Just log it.
                Log.w(TAG, "Failed to vibrate during shutdown.", e);
            }

            // vibrator is asynchronous so we need to wait to avoid shutting down too soon.
            try {
    
    
                Thread.sleep(SHUTDOWN_VIBRATE_MS);
            } catch (InterruptedException unused) {
    
    
            }
        }
        // Shutdown power
        Log.i(TAG, "Performing low-level shutdown...");
        PowerManagerService.lowLevelShutdown(reason);//关机
    }

1.4、PowerManagerService.lowLevelShutdown(reason);

    public static void lowLevelShutdown(String reason) {
    
    
        if (reason == null) {
    
    
            reason = "";
        }
        String chargeTypeString = "";
        if(mChargeType) {
    
    
            chargeTypeString  = ",charging";
        }
        //通过设置系统属性关机
        SystemProperties.set("sys.powerctl", "shutdown," + reason + chargeTypeString);
    }

The upper-level Java code is here. After shutdown and restart are set system properties, the underlying c-code listens to the node to perform real operations.

SystemProperties.set("sys.powerctl", "reboot," + reason);//重启
SystemProperties.set("sys.powerctl", "shutdown," + reason + chargeTypeString);//关机

2. The underlying shutdown process

Guess you like

Origin blog.csdn.net/a396604593/article/details/131195882