[关联唤醒拦截]AMS的启动流程

0. 流程图

SystemServer->AMS->PowerController->BackgroundCleanHelper

1. SystemServer.java

  • frameworks/base/services/java/com/android/server/SystemServer.java
    很多framewrok层的服务都是从这里定义启动的

1.1 SystemServer.startBootstrapServices()

package com.android.server;

public final class SystemServer {
    private static final String TAG = "SystemServer";
    
    /**
     * Starts the small tangle of critical services that are needed to get
     * the system off the ground.  These services have complex mutual dependencies
     * which is why we initialize them all in one place here.  Unless your service
     * is also entwined in these dependencies, it should be initialized in one of
     * the other functions.
     */
    private void startBootstrapServices() {
        ...
        // 启动 AMS
        // Activity manager runs the show.
        traceBeginAndSlog("StartActivityManager");
        mActivityManagerService = mSystemServiceManager.startService(
                ActivityManagerService.Lifecycle.class).getService();
        mActivityManagerService.setSystemServiceManager(mSystemServiceManager);
        mActivityManagerService.setInstaller(installer);
        traceEnd();

1.2 初始化关联唤醒服务

// inone add start by suhuazhi powersave
import com.android.server.power.PowerController;
//inone add end by suhuazhi powersave

    /**
     * Starts a miscellaneous grab bag of stuff that has yet to be refactored
     * and organized.
     */
    private void startOtherServices() {
        ...
        // inone add start by suhuazhi for powersave
        PowerController powerctl = null;
        // inone add end by suhuazhi for powersave
        ...

        // inone add start by suhuazhi powersave
        try {
            Slog.i(TAG, "PowerController Service");
            // 实例化关联唤醒服务
            powerctl = new PowerController(context, mActivityManagerService);
        } catch (Throwable e) {
            reportWtf("starting PowerController", e);
        }
        // inone add end by suhuazhi powersave
        ...
    }

2. AMS启动-ActivityManagerService.Lifecycle

  • frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
    public static final class Lifecycle extends SystemService {
        private final ActivityManagerService mService;

        public Lifecycle(Context context) {
            super(context);
            // 启动入口
            mService = new ActivityManagerServiceEx(context);
        }

        @Override
        public void onStart() {
            mService.start();
        }

        @Override
        public void onCleanupUser(int userId) {
            mService.mBatteryStatsService.onCleanupUser(userId);
        }

        public ActivityManagerService getService() {
            return mService;
        }
    }

3. AMS相关初始化

package com.android.server.am;

public class ActivityManagerService extends ActivityManagerServiceExAbs
        implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback {
        
public class ActivityManagerServiceEx extends ActivityManagerService {

3.1 ActivityManagerService.systemReady()

package com.android.server;

public final class SystemServer {
    private static final String TAG = "SystemServer";

    // We now tell the activity manager it is okay to run third party
    // code.  It will call back into us once it has gotten to the state
    // where third party code can really run (but before it has actually
    // started launching the initial applications), for us to complete our
    // initialization.
    mActivityManagerService.systemReady(() -> {
        ...
        // inone add start by suhuazhi for power policy
        try {
            powerctlF.setWindowManager(windowManagerF);
            Slog.i(TAG, "PowerController Service systemReady");
            powerctlF.systemReady();
        } catch (Throwable e) {
            reportWtf("starting PowerController", e);
        }
        // inone add end by suhuazhi for power policy
        ...
    }

4. PowerController 初始化

4.1 构造器

package com.android.server.power;

    // SystemServer.startOtherServices()
    public PowerController(Context context, IActivityManager activityManager) {
        mContext = context;
        mActivityManager = activityManager;
    }

4.2 AMS.systemReady()

    // call before AMS.SystemReadby
    // 系统触摸事件监听
    public void setWindowManager(WindowManagerService wm) {
        mWindowManagerFuncs = wm;
    }

    // called when system is AMS.ready ( that is all the service is started)
    public void systemReady() {
        HandlerThread handlerThread = new HandlerThread(TAG);
        handlerThread.start();
        msgHandler = new MyHandler(handlerThread.getLooper());
        
        // to init Data first
        msgHandler.sendMessage(msgHandler.obtainMessage(MSG_INIT));
    }

4.3 AMS.initData()

    private void initData() {
        createPowerSaveHelpers();
    }

    // create helpers
    private void createPowerSaveHelpers() {
        mBackgroundCleanHelper = new BackgroundCleanHelper(mContext, mActivityManager, msgHandler);
    }

至此打通SystemServer->AMS->PowerController->BackgroundCleanHelper

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

法迪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值