PowerManagerService启动-Android12

Posted xhBruce

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PowerManagerService启动-Android12相关的知识,希望对你有一定的参考价值。

PowerManagerService启动-android12

android12-release


1. 在SystemServer的startBootstrapServices中启动

  • mSystemServiceManager.startService(PowerManagerService.class)先调用CameraServiceProxy构造方法constructor.newInstance(mContext),在调用CameraServiceProxy的service.onStart()方法
  • mPowerManagerService.systemReady() 获取各类本地服务、远程服务以及相关联的功能类;注册SettingsObserver监听 等。

frameworks/base/services/java/com/android/server/SystemServer.java

private void startBootstrapServices(@NonNull TimingsTraceAndSlog t) 
	// ... ...
    t.traceBegin("StartPowerManager");
    mPowerManagerService = mSystemServiceManager.startService(PowerManagerService.class);
    t.traceEnd();
    // ... ...


private void startOtherServices(@NonNull TimingsTraceAndSlog t) 
	// ... ...
    t.traceBegin("MakePowerManagerServiceReady");
    try 
        // TODO: use boot phase
        mPowerManagerService.systemReady(mActivityManagerService.getAppOpsService());
     catch (Throwable e) 
        reportWtf("making Power Manager Service ready", e);
    
    t.traceEnd();
    // ... ...

2. PowerManagerService初始化并调用onStart()方法

2.1 初始化

  • mBinderService = new BinderService()作为Binder IPC的服务端
  • mLocalService = new LocalService()为同进程的其它模块提供支持
  • mNativeWrapper = injector.createNativeWrapper()是对调用native方法提供的一层包装
  • 了解Native层:
    mNativeWrapper.nativeInit(this); native方法初始化并连接HAL层PowerHalController.init()
    mNativeWrapper.nativeSetAutoSuspend(false); 关闭自动休眠
    mNativeWrapper.nativeSetPowerMode(Mode.INTERACTIVE, true); 保持屏幕点亮
    mNativeWrapper.nativeSetPowerMode(Mode.DOUBLE_TAP_TO_WAKE, false); 禁用双击屏幕唤醒系统的功能

frameworks/base/services/core/java/com/android/server/power/PowerManagerService.java

PowerManagerService(Context context, Injector injector) 
    super(context);

    mContext = context;
    mBinderService = new BinderService();
    mLocalService = new LocalService();
    mNativeWrapper = injector.createNativeWrapper();
    mSystemProperties = injector.createSystemPropertiesWrapper();
    mClock = injector.createClock();
    mInjector = injector;

    mHandlerThread = new ServiceThread(TAG,
            Process.THREAD_PRIORITY_DISPLAY, false /*allowIo*/);
    mHandlerThread.start();
    mHandler = injector.createHandler(mHandlerThread.getLooper(),
            new PowerManagerHandlerCallback());
    mConstants = new Constants(mHandler);
    mAmbientDisplayConfiguration = mInjector.createAmbientDisplayConfiguration(context);
    mAmbientDisplaySuppressionController =
            mInjector.createAmbientDisplaySuppressionController(context);
    mAttentionDetector = new AttentionDetector(this::onUserAttention, mLock);
    mFaceDownDetector = new FaceDownDetector(this::onFlip);

    mBatterySavingStats = new BatterySavingStats(mLock);
    mBatterySaverPolicy =
            mInjector.createBatterySaverPolicy(mLock, mContext, mBatterySavingStats);
    mBatterySaverController = mInjector.createBatterySaverController(mLock, mContext,
            mBatterySaverPolicy, mBatterySavingStats);
    mBatterySaverStateMachine = mInjector.createBatterySaverStateMachine(mLock, mContext,
            mBatterySaverController);

    mInattentiveSleepWarningOverlayController =
            mInjector.createInattentiveSleepWarningController();

    // Save brightness values:
    // Get float values from config.
    // Store float if valid
    // Otherwise, get int values and convert to float and then store.
    final float min = mContext.getResources().getFloat(com.android.internal.R.dimen
            .config_screenBrightnessSettingMinimumFloat);
    final float max = mContext.getResources().getFloat(com.android.internal.R.dimen
            .config_screenBrightnessSettingMaximumFloat);
    final float def = mContext.getResources().getFloat(com.android.internal.R.dimen
            .config_screenBrightnessSettingDefaultFloat);
    final float doze = mContext.getResources().getFloat(com.android.internal.R.dimen
            .config_screenBrightnessDozeFloat);
    final float dim = mContext.getResources().getFloat(com.android.internal.R.dimen
            .config_screenBrightnessDimFloat);

    if (min == INVALID_BRIGHTNESS_IN_CONFIG || max == INVALID_BRIGHTNESS_IN_CONFIG
            || def == INVALID_BRIGHTNESS_IN_CONFIG) 
        mScreenBrightnessMinimum = BrightnessSynchronizer.brightnessIntToFloat(
                mContext.getResources().getInteger(com.android.internal.R.integer
                        .config_screenBrightnessSettingMinimum));
        mScreenBrightnessMaximum = BrightnessSynchronizer.brightnessIntToFloat(
                mContext.getResources().getInteger(com.android.internal.R.integer
                        .config_screenBrightnessSettingMaximum));
        mScreenBrightnessDefault = BrightnessSynchronizer.brightnessIntToFloat(
                mContext.getResources().getInteger(com.android.internal.R.integer
                        .config_screenBrightnessSettingDefault));
     else 
        mScreenBrightnessMinimum = min;
        mScreenBrightnessMaximum = max;
        mScreenBrightnessDefault = def;
    
    if (doze == INVALID_BRIGHTNESS_IN_CONFIG) 
        mScreenBrightnessDoze = BrightnessSynchronizer.brightnessIntToFloat(
                mContext.getResources().getInteger(com.android.internal.R.integer
                        .config_screenBrightnessDoze));
     else 
        mScreenBrightnessDoze = doze;
    
    if (dim == INVALID_BRIGHTNESS_IN_CONFIG) 
        mScreenBrightnessDim = BrightnessSynchronizer.brightnessIntToFloat(
                mContext.getResources().getInteger(com.android.internal.R.integer
                        .config_screenBrightnessDim));
     else 
        mScreenBrightnessDim = dim;
    

    final float vrMin = mContext.getResources().getFloat(com.android.internal.R.dimen
            .config_screenBrightnessSettingForVrMinimumFloat);
    final float vrMax = mContext.getResources().getFloat(com.android.internal.R.dimen
            .config_screenBrightnessSettingForVrMaximumFloat);
    final float vrDef = mContext.getResources().getFloat(com.android.internal.R.dimen
            .config_screenBrightnessSettingForVrDefaultFloat);
    if (vrMin == INVALID_BRIGHTNESS_IN_CONFIG || vrMax == INVALID_BRIGHTNESS_IN_CONFIG
            || vrDef == INVALID_BRIGHTNESS_IN_CONFIG) 
        mScreenBrightnessMinimumVr = BrightnessSynchronizer.brightnessIntToFloat(
                mContext.getResources().getInteger(com.android.internal.R.integer
                        .config_screenBrightnessForVrSettingMinimum));
        mScreenBrightnessMaximumVr = BrightnessSynchronizer.brightnessIntToFloat(
                mContext.getResources().getInteger(com.android.internal.R.integer
                        .config_screenBrightnessForVrSettingMaximum));
        mScreenBrightnessDefaultVr = BrightnessSynchronizer.brightnessIntToFloat(
                mContext.getResources().getInteger(com.android.internal.R.integer
                        .config_screenBrightnessForVrSettingDefault));
     else 
        mScreenBrightnessMinimumVr = vrMin;
        mScreenBrightnessMaximumVr = vrMax;
        mScreenBrightnessDefaultVr = vrDef;
    

    synchronized (mLock) 
        mWakeLockSuspendBlocker =
                mInjector.createSuspendBlocker(this, "PowerManagerService.WakeLocks");
        mDisplaySuspendBlocker =
                mInjector.createSuspendBlocker(this, "PowerManagerService.Display");
        if (mDisplaySuspendBlocker != null) 
            mDisplaySuspendBlocker.acquire();
            mHoldingDisplaySuspendBlocker = true;
        
        mHalAutoSuspendModeEnabled = false;
        mHalInteractiveModeEnabled = true;

        mWakefulnessRaw = WAKEFULNESS_AWAKE;
        sQuiescent = mSystemProperties.get(SYSTEM_PROPERTY_QUIESCENT, "0").equals("1")
                || InitProperties.userspace_reboot_in_progress().orElse(false);

        mNativeWrapper.nativeInit(this);
        mNativeWrapper.nativeSetAutoSuspend(false);
        mNativeWrapper.nativeSetPowerMode(Mode.INTERACTIVE, true);
        mNativeWrapper.nativeSetPowerMode(Mode.DOUBLE_TAP_TO_WAKE, false);
        mInjector.invalidateIsInteractiveCaches();
    

2.2 onStart()方法

  • publishBinderService() 添加名为POWER_SERVICE = "power"的Binder IPC的服务端
  • publishLocalService(PowerManagerInternal.class, mLocalService) 添加到LocalServices,为同进程的其它模块提供支持
public void onStart() 
    publishBinderService(Context.POWER_SERVICE, mBinderService, /* allowIsolated= */ false,
            DUMP_FLAG_PRIORITY_DEFAULT | DUMP_FLAG_PRIORITY_CRITICAL);
    publishLocalService(PowerManagerInternal.class, mLocalService);

    Watchdog.getInstance().addMonitor(this);
    Watchdog.getInstance().addThread(mHandler);

3. PowerManagerService.systemReady

  1. 获取各类本地服务、远程服务以及相关联的功能类,如屏保(DreamManagerInternal)、窗口(WindowManagerPolicy)、电池状态监听服务(BatteryManagerInternal)、屏显服务(DisplayManagerInternal)、背光服务(LightsManager)、无线充电(WirelessChargerDetector)等
  2. mSettingsObserver = new SettingsObserver(mHandler) 注册SettingsObserver监听,监听Settings中的值的变化
  3. readConfigurationLocked()读取配置文件中的默认值,如 插拔USB是否亮屏 、是否支持屏保 、充电和睡眠时屏保是否激活 、是否支持双击唤醒屏幕 等等
  4. updateSettingsLocked() 查询Settings中配置的值,如 是否打开屏保、休眠时是否启用屏保、设备在一段时间不活动后进入休眠或者屏保状态的时间等待
  5. 注册广播接收者,监听特定的广播事件,如 电池状态的改变mContext.registerReceiver(new BatteryReceiver(), filter, null, mHandler) 、进入屏保mContext.registerReceiver(new DreamReceiver(), filter, null, mHandler)、切换用户mContext.registerReceiver(new UserSwitchedReceiver(), filter, null, mHandler)、设备物理插槽状态的改变(通常用于监听是否接入了有线充电器)mContext.registerReceiver(new DockReceiver(), filter, null, mHandler)
public void systemReady(IAppOpsService appOps) 
    synchronized (mLock) 
        mSystemReady = true;
        mAppOps = appOps;
        mDreamManager = getLocalService(DreamManagerInternal.class);
        mDisplayManagerInternal = getLocalService(DisplayManagerInternal.class);
        mPolicy = getLocalService(WindowManagerPolicy.class);
        mBatteryManagerInternal = getLocalService(BatteryManagerInternal.class);
        mAttentionDetector.systemReady(mContext);
        mDisplayGroupPowerStateMapper = mInjector.createDisplayPowerRequestMapper(mLock,
                mDisplayManagerInternal, new DisplayGroupPowerChangeListener());

        SensorManager sensorManager = new SystemSensorManager(mContext, mHandler.getLooper());

        // The notifier runs on the system server's main looper so as not to interfere
        // with the animations and other critical functions of the power manager.
        mBatteryStats = BatteryStatsService.getService();
        mNotifier = mInjector.createNotifier(Looper.getMainLooper(), mContext, mBatteryStats,
                mInjector.createSuspendBlocker(this, "PowerManagerService.Broadcasts"),
                mPolicy, mFaceDownDetector);

        mWirelessChargerDetector = mInjector.createWirelessChargerDetector(sensorManager,
                mInjector.createSuspendBlocker(
                        this, "PowerManagerService.WirelessChargerDetector"),
                mHandler);
        mSettingsObserver = new SettingsObserver(mHandler);

        mLightsManager = getLocalService(LightsManager.class);
        mAttentionLight = mLightsManager.getLight(LightsManager.LIGHT_ID_ATTENTION);

        // Initialize display power management.
        mDisplayManagerInternal.initPowerManagement(
                mDisplayPowerCallbacks, mHandler, sensorManager);

        try 
            final ForegroundProfileObserver observer = new ForegroundProfileObserver();
            ActivityManager.getService().registerUserSwitchObserver(observer, TAG);
         catch (RemoteException e) 
            // Shouldn't happen since in-process.
        

        // Go.
        readConfigurationLocked();
        updateSettingsLocked();
        mDirty |= DIRTY_BATTERY_STATE;
        updatePowerStateLocked();
    

    final ContentResolver resolver = mContext.getContentResolver();
    mConstants.start(resolver);

    mBatterySaverController.systemReady();
    mBatterySaverPolicy.systemReady();
    mFaceDownDetector.systemReady(mContext);

    // Register for settings changes.
    resolver.registerContentObserver(Settings.Secure.getUriFor(
            Settings.Secure.SCREENSAVER_ENABLED),
            false, mSettingsObserver, UserHandle.USER_ALL);
    resolver.registerContentObserver(Settings.Secure.getUriFor(
            Settings.Secure.SCREENSAVER_ACTIVATE_ON_SLEEP),
            false, mSettingsObserver, UserHandle.USER_ALL);
    resolver.registerContentObserver(Settings.Secure.getUriFor(
            Settings.Secure.SCREENSAVER_ACTIVATE_ON_DOCK),
            false, mSettingsObserver, UserHandle.USER_ALL);
    resolver.registerContentObserver(Settings.System.getUriFor(
            Settings.System.SCREEN_OFF_TIMEOUT),
            false, mSettingsObserver, UserHandle.USER_ALL);
    resolver.registerContentObserver(Settings.Secure.getUriFor(
            Settings.Secure.SLEEP_TIMEOUT),
            false, mSettingsObserver, UserHandle.USER_ALL);
    resolver.registerContentObserver(Settings.Secure.getUriFor(
            Settings.Secure.ATTENTIVE_TIMEOUT),
            false, mSettingsObserver, UserHandle.USER_ALL);
    resolver.registerContentObserver(Settings.Global.getUriFor(
            Settings.Global.STAY_ON_WHILE_PLUGGED_IN),
            false, mSettingsObserver, UserHandle.USER_ALL);
    resolver.registerContentObserver(Settings.System.getUriFor(
            Settings.System.SCREEN_BRIGHTNESS_MODE),
            false, mSettingsObserver, UserHandle.USER_ALL);
    resolver.registerContentObserver(Settings.System.getUriFor(
            Settings.System.SCREEN_AUTO_BRIGHTNESS_ADJ),
            false, mSettingsObserver, UserHandle.USER_ALL);
    resolver.registerContentObserver(Settings.Global.getUriFor(
            Settings.Global.THEATER_MODE_ON),
            false, mSettingsObserver, UserHandle.USER_ALL);
    resolver.registerContentObserver(Settings.Secure.getUriFor(
            Settings.Secure.DOZE_ALWAYS_ON),
            false, mSettingsObserver, UserHandle.USER_ALL);
    resolver.registerContentObserver(Settings.Secure.getUriFor(
            Settings.Secure.DOUBLE_TAP_TO_WAKE),
            false, mSettingsObserver, UserHandle.USER_ALL);
    resolver.registerContentObserver(Settings.Global.getUriFor(
            Settings.Global.DEVICE_DEMO_MODE),
            false, mSettingsObserver, UserHandle.USER_SYSTEM);
    IVrManager vrManager = IVrManager.Stub.asInterface(getBinderService(Context.VR_SERVICE));
    if (vrManager != nullPowerManagerService的启动流程

Android7.0 PowerManagerService WakeLock的使用及流程

PowerManagerService流程分析

PowerManagerService 电源管理架构初识

[RK3568][Android11]PowerManagerService息屏控制

[RK3568][Android11]PowerManagerService息屏控制