Android AMS关机流程分析

2021-12-20  本文已影响0人  小阿牛的爸爸

1. ams关机入口

入口位于安卓的ActivityManagerService中
从下面代码中可以看到,总共做了这么几件事:

public class ActivityManagerService
    public boolean shutdown(int timeout) {
        if (checkCallingPermission(android.Manifest.permission.SHUTDOWN)
                != PackageManager.PERMISSION_GRANTED) {
            throw new SecurityException("Requires permission "
                    + android.Manifest.permission.SHUTDOWN);
        }

        // 1. 任务相关的数据持久化
        final boolean timedout = mAtmInternal.shuttingDown(mBooted, timeout);

        // 2. 应用权限持久化
        mAppOpsService.shutdown();
        // 3. 用户使用数据持久化
        if (mUsageStatsService != null) {
            mUsageStatsService.prepareShutdown();
        }
        // 4. 电源状态持久化
        mBatteryStatsService.shutdown();
        synchronized (this) {
            // 5. 进程数据持久化
            mProcessStats.shutdownLocked();
        }

        return timedout;
    }
}

2. mAtmInternal.shuttingDown

public boolean shuttingDown(boolean booted, int timeout) {
    synchronized(mGlobalLock) {
        mShuttingDown = true;
        // 给所有的屏幕设置休眠原因并加入mSleepTokens
        mRootActivityContainer.prepateForShutdown();
        // 通知wms
        updateEventDispatchingLocked();
        // 任务持久化
        notifyTaskPersisterLocked(null, true);
        return mStackSupervisor.shutdownLocked(timeout);
    }
}

mStackSupervisor.shutdownLocked:

    boolean shutdownLocked(int timeout) {
        goingToSleepLocked();

        boolean timedout = false;
        final long endTime = System.currentTimeMillis() + timeout;
        while (true) {
            if (!putStacksToSleepLocked(true /* allowDelay */, true /* shuttingDown */)) {
                long timeRemaining = endTime - System.currentTimeMillis();
                if (timeRemaining > 0) {
                    try {
                        mService.wait(timeRemaining);
                    } catch (InterruptedException e) {
                    }
                } else {
                    Slog.w(TAG, "Activity manager shutdown timed out");
                    timedout = true;
                    break;
                }
            } else {
                break;
            }
        }

        // Force checkReadyForSleep to complete.
        checkReadyForSleepLocked(false /* allowDelay */);

        return timedout;
    }

3. mAppOpsService.shutdown

管理应用权限的,在关机前对数据进行持久化。

public void shutdown() {
    Slog.w(TAG, "Writing app ops before shutdown...");
    boolean doWrite = false;
    synchronized (this) {
        if (mWriteScheduled) {
            mWriteScheduled = false;
            doWrite = true;
        }
    }
    if (doWrite) {
        writeState();
    }
}

4. mUsageStatsService.prepareShutdown

这是一个Android私有service,主要作用是收集用户使用每一个APP的频率、使用时常。
结束的时候也是对数据进行持久化。

void shutdown() {
    synchronized (mLock) {
        mHandler.removeMessages(MSG_REPORT_EVENT);
        flushToDiskLocked();
    }
}

5. mBatteryStatsService.shutdown

BatteryStasService的主要功能是收集系统中各模块和应用进程的用电情况。关机的时候就是把相应的信息持久化。

public void shutdown() {
    Slog.w("BatteryStats", "Writing battery stats before shutdown...");

    syncStats("shutdown", BatteryExternalStatsWorker.UPDATE_ALL);

    synchronized (mStats) {
        mStats.shutdownLocked();
    }

    // Shutdown the thread we made.
    mWorker.shutdown();
}

6. mProcessStats.shutdownLocked

进程管理服务,主要是监控进程的内存、CPU等使用,也是做了一个持久化。

public void shutdownLocked() {
    Slog.w(TAG, "Writing process stats before shutdown...");
    mProcessStats.mFlags |= ProcessStats.FLAG_SHUTDOWN;
    writeStateSyncLocked();
    mShuttingDown = true;
}
上一篇下一篇

猜你喜欢

热点阅读