PowerManagerService(一)

PowerManagerService(一),第1张

概述极力推荐Android 开发大总结文章:欢迎收藏 程序员Android 力荐 ,Android 开发者需要的必备技能 PowerManagerService 提供Android系统的电源管理服务,主要功能是控制系统待机状态,屏幕显示,亮度调节,光线/距离传感器的控制等。 相关代码在以下文件中: frameworks/base/services/java/com/android/server/Sys

极力推荐AndroID 开发大总结文章:欢迎收藏
程序员Android 力荐 ,Android 开发者需要的必备技能

PowerManagerService 提供AndroID系统的电源管理服务,主要功能是控制系统待机状态,屏幕显示,亮度调节,光线/距离传感器的控制等。

相关代码在以下文件中:

frameworks/base/services/java/com/androID/server/SystemServer.javaframeworks/base/core/java/androID/os/PowerManager.javaframeworks/base/services/core/java/com/androID/server/power/PowerManagerService.javaframeworks/base/services/core/jni/com_androID_server_power_PowerManagerService.cppframeworks/base/core/java/androID/os/PowerManagerInternal.javaframeworks/base/services/core/java/com/androID/server/power/NotifIEr.javadevice/qcom/common/power/power.csystem/core/libsuspend/autosuspend.charDWare/libharDWare_legacy/power/power.c

初始化流程

跟其他系统服务一样,PowerManagerService也是继承于SystemService并通过SystemServer启动。
SystemServer
frameworks/base/services/java/com/androID/server/SystemServer.java

private voID startbootstrapServices() {  ......  mPowerManagerService = mSystemServiceManager.startService(PowerManagerService.class);  ......}

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

public final class PowerManagerService extends SystemService        implements Watchdog.Monitor {    ......}

在SystemServer的startbootstrapServices中,通过SystemServiceManager.startService启动了PowerManagerService,下面首先来看PowerManagerService构造方法。
frameworks/base/services/core/java/com/androID/server/power/PowerManagerService.java

public PowerManagerService(Context context) {    super(context);    // mContext赋值为SystemContext    mContext = context;    // 创建消息处理线程并启动,创建关联消息处理线程的handler对象    mHandlerThread = new ServiceThread(TAG,Process.THREAD_PRIORITY_disPLAY,false /*allowIo*/);    mHandlerThread.start();    mHandler = new PowerManagerHandler(mHandlerThread.getLooper());    qcNsrmPowExt = new QCNsrmPowerExtension(this);    synchronized (mlock) {        // 创建"PowerManagerService.WakeLocks"的SuspendBlocker        mWakeLockSuspendBlocker = createSuspendBlockerLocked("PowerManagerService.WakeLocks");        // 创建"PowerManagerService.display"的SuspendBlocker        mdisplaySuspendBlocker = createSuspendBlockerLocked("PowerManagerService.display");        // 请求displaySuspendBlocker,禁止系统进入休眠        mdisplaySuspendBlocker.acquire();        mHoldingdisplaySuspendBlocker = true;        mHalautoSuspendModeEnabled = false;        mHalinteractiveModeEnabled = true;        // 设置mWakefulness为唤醒状态        mWakefulness = WAKEFulnesS_AWAKE;        // 进入到native层初始化        nativeInit();        nativeSetautoSuspend(false);        nativeSetInteractive(true);        nativeSetFeature(POWER_FEATURE_DOUBLE_TAP_TO_WAKE,0);    }}

PowerManagerService构造函数中首先创建了处理消息的进程及对应的handler对象以进行消息处理,然后创建SuspendBlocker对象,用于WakeLocks与display,并设置mWakefulness的初始状态为WAKEFulnesS_AWAKE,最后进入到native层初始化。下面先看一下关于mWakefulness的定义。

>>> frameworks/base/core/java/androID/os/PowerManagerInternal.java/** * 设备处于休眠状态,只能被wakeUp()唤醒. */public static final int WAKEFulnesS_ASLEEP = 0;/** * 设备处于正常工作(fully awake)状态. */public static final int WAKEFulnesS_AWAKE = 1;/** * 设备处于播放屏保状态. */public static final int WAKEFulnesS_DREAMING = 2;/** * 设备处于doze状态,只有低耗电的屏保可以运行,其他应用被挂起. */public static final int WAKEFulnesS_DOZING = 3;

继续回到PowerManagerService构造函数的native初始化中,首先来看nativeInit的实现。
frameworks/base/services/core/jni/com_androID_server_power_PowerManagerService.cpp

static const JNINativeMethod gPowerManagerServiceMethods[] = {    /* name,signature,funcPtr */    { "nativeInit","()V",(voID*) nativeInit },{ "nativeAcquireSuspendBlocker","(Ljava/lang/String;)V",(voID*) nativeAcquireSuspendBlocker },{ "nativeReleaseSuspendBlocker",(voID*) nativeReleaseSuspendBlocker },{ "nativeSetInteractive","(Z)V",(voID*) nativeSetInteractive },{ "nativeSetautoSuspend",(voID*) nativeSetautoSuspend },{ "nativeSendPowerHint","(II)V",(voID*) nativeSendPowerHint },{ "nativeSetFeature",(voID*) nativeSetFeature },};

PowerManagerService中的native方法定义如上,nativeInit即调用nativeInit()。
frameworks/base/services/core/jni/com_androID_server_power_PowerManagerService.cpp

static voID nativeInit(jnienv* env,jobject obj) {    // 创建一个全局对象,引用PMS    gPowerManagerServiceObj = env->NewGlobalRef(obj);    // 利用hw_get_module加载power模块    status_t err = hw_get_module(POWER_HARDWARE_MODulE_ID,(hw_module_t const**)&gPowerModule);    if (!err) {        gPowerModule->init(gPowerModule);    } else {        ALOGE("Couldn't load %s module (%s)",POWER_HARDWARE_MODulE_ID,strerror(-err));    }}

nativeInit的主要任务时装载power模块,该模块由厂商实现,以高通为例,如下。
device/qcom/common/power/power.c

tatic struct hw_module_methods_t power_module_methods = {    .open = NulL,};struct power_module HAL_MODulE_INFO_SYM = {    .common = {        .tag = HARDWARE_MODulE_TAG,.module_API_version = POWER_MODulE_API_VERSION_0_2,.hal_API_version = HARDWARE_HAL_API_VERSION,.ID = POWER_HARDWARE_MODulE_ID,.name = "QCOM Power HAL",.author = "Qualcomm",.methods = &power_module_methods,},.init = power_init,.powerHint = power_hint,.setInteractive = set_interactive,};

power_module中实现了init,powerHint,setInteractive,nativeInit最终调用到HAL power模块的power_init具体实现中。接着看native初始化nativeSetautoSuspend的实现。

frameworks/base/services/core/jni/com_androID_server_power_PowerManagerService.cpp

static voID nativeSetautoSuspend(jnienv* /* env */,jclass /* clazz */,jboolean enable) {    if (enable) {        ALOGD_IF_SLOW(100,"Excessive delay in autosuspend_enable() while turning screen off");        autosuspend_enable();    } else {        ALOGD_IF_SLOW(100,"Excessive delay in autosuspend_disable() while turning screen on");        autosuspend_disable();    }}

system/core/libsuspend/autosuspend.c

int autosuspend_disable(voID){    int ret;    ret = autosuspend_init();    if (ret) {        return ret;    }    ALOGV("autosuspend_disable\n");    if (!autosuspend_enabled) {        return 0;    }    ret = autosuspend_ops->disable();    if (ret) {        return ret;    }    autosuspend_enabled = false;    return 0;}

nativeSetautoSuspend最终调用到libsuspend(参考AndroID电源管理系列之libsuspend)的autosuspend_disable禁止系统休眠。继续看native初始化nativeSetInteractive,nativeSetFeature的实现
frameworks/base/services/core/jni/com_androID_server_power_PowerManagerService.cpp

static voID nativeSetInteractive(jnienv* /* env */,jboolean enable) {    if (gPowerModule) {        if (enable) {            ALOGD_IF_SLOW(20,"Excessive delay in setInteractive(true) while turning screen on");            gPowerModule->setInteractive(gPowerModule,true);        } else {            ALOGD_IF_SLOW(20,"Excessive delay in setInteractive(false) while turning screen off");            gPowerModule->setInteractive(gPowerModule,false);        }    }}static voID nativeSetFeature(jnienv *env,jclass clazz,jint featureID,jint data) {    int data_param = data;    if (gPowerModule && gPowerModule->setFeature) {        gPowerModule->setFeature(gPowerModule,(feature_t)featureID,data_param);    }}

同nativeInit一样,最终都是调用到HAL power模块的具体实现中。以上是构造函数的分析流程,下面继续看PowerManagerService在系统启动过程中回调onStart(),onBootPhase(),systemReady()的实现。
frameworks/base/services/core/java/com/androID/server/power/PowerManagerService.java

public voID onStart() {    publishBinderService(Context.POWER_SERVICE,new BinderService());    publishLocalService(PowerManagerInternal.class,new LocalService());    Watchdog.getInstance().addMonitor(this);    Watchdog.getInstance().addThread(mHandler);}private final class BinderService extends IPowerManager.Stub {    ......}private final class LocalService extends PowerManagerInternal {    ......}

onStart()中发布了BinderService,LocalService分别供其他进程,进程内其他服务调用,并将PowerManagerService加入到Watchdog监控中。

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

public voID onBootPhase(int phase) {    synchronized (mlock) {        if (phase == PHASE_THIRD_PARTY_APPS_CAN_START) {            ......        } else if (phase == PHASE_BOOT_COMPLETED) {            final long Now = SystemClock.uptimeMillis();            // 设置mBootCompleted状态            mBootCompleted = true;            mDirty |= DIRTY_BOOT_COMPLETED;            // 更新userActivity及PowerState,后面分析            userActivityNoUpdateLocked(                    Now,PowerManager.USER_ACTIVITY_EVENT_OTHER,Process.SYstem_UID);            updatePowerStateLocked();            // 执行mBootCompletedRunnables中的runnable方法            if (!ArrayUtils.isEmpty(mBootCompletedRunnables)) {                Slog.d(TAG,"Posting " + mBootCompletedRunnables.length + " delayed runnables");                for (Runnable r : mBootCompletedRunnables) {                    BackgroundThread.getHandler().post(r);                }            }            mBootCompletedRunnables = null;        }    }}

onBootPhase中主要设置mBootCompleted状态,更新PowerState状态,并执行mBootCompletedRunnables中的runnables方法(低电量模式会设置)。
frameworks/base/services/core/java/com/androID/server/power/PowerManagerService.java

public voID systemReady(IAppOpsService appOps) {    synchronized (mlock) {        mSystemReady = true;        // 获取AppOpsService        mAppOps = appOps;        // 获取DreamManager        mDreamManager = getLocalService(DreamManagerInternal.class);        // 获取displayManagerService        mdisplayManagerInternal = getLocalService(displayManagerInternal.class);        mPolicy = getLocalService(WindowManagerPolicy.class);        // 获取mBatteryService        mBatteryManagerInternal = getLocalService(BatteryManagerInternal.class);        PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);        // 获取屏幕默认,最大,最小亮度        mScreenBrightnessSettingMinimum = pm.getMinimumScreenBrightnessSetting();        mScreenBrightnessSettingMaximum = pm.getMaximumScreenBrightnessSetting();        mScreenBrightnessSettingDefault = pm.getDefaultScreenBrightnessSetting();        // 获取SensorManager        SensorManager sensorManager = new SystemSensorManager(mContext,mHandler.getLooper());        mBatteryStats = BatteryStatsService.getService();        // 创建NotifIEr对象,用于广播power state的变化        mNotifIEr = new NotifIEr(Looper.getMainLooper(),mContext,mBatteryStats,mAppOps,createSuspendBlockerLocked("PowerManagerService.broadcasts"),mPolicy);        // 无线充电检测        mWirelessChargerDetector = new WirelessChargerDetector(sensorManager,createSuspendBlockerLocked("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);        // Register for settings changes.        final ContentResolver resolver = mContext.getContentResolver();        resolver.registerContentObserver(Settings.Secure.getUriFor(                Settings.Secure.SCREENSAVER_ENABLED),......        IVrManager vrManager =                (IVrManager) getBinderService(VrManagerService.VR_MANAGER_BINDER_SERVICE);        try {            vrManager.registerListener(mVrStateCallbacks);        } catch (remoteexception e) {            Slog.e(TAG,"Failed to register VR mode state Listener: " + e);        }        // 读取配置        readConfigurationLocked();        updateSettingsLocked();        mDirty |= DIRTY_BATTERY_STATE;        updatePowerStateLocked();    }    // Register for broadcasts from other components of the system.    IntentFilter filter = new IntentFilter();    filter.addAction(Intent.ACTION_BATTERY_CHANGED);    filter.setPriority(IntentFilter.SYstem_HIGH_PRIORITY);    mContext.registerReceiver(new BatteryReceiver(),filter,null,mHandler);    filter = new IntentFilter();    filter.addAction(Intent.ACTION_DREAMING_STARTED);    filter.addAction(Intent.ACTION_DREAMING_StopPED);    mContext.registerReceiver(new DreamReceiver(),mHandler);    filter = new IntentFilter();    filter.addAction(Intent.ACTION_USER_SWITCHED);    mContext.registerReceiver(new UserSwitchedReceiver(),mHandler);    filter = new IntentFilter();    filter.addAction(Intent.ACTION_DOCK_EVENT);    mContext.registerReceiver(new DockReceiver(),mHandler);}

userActivity
userActivity是定义在PowerManager中的SystemAPI,用户向PowerManagerService报告用户活动,以更新PowerManagerService内部时间/状态值,推迟系统休眠的时间。下面首先来看userActivity的定义。

frameworks/base/core/java/androID/os/PowerManager.java

/** * User activity event type: UnspecifIEd event type. */public static final int USER_ACTIVITY_EVENT_OTHER = 0;/** * User activity event type: button or key pressed or released. */public static final int USER_ACTIVITY_EVENT_button = 1;/** * User activity event type: touch down,move or up. */public static final int USER_ACTIVITY_EVENT_touch = 2;/** * User activity event type: Accessibility taking action on behalf of user. */public static final int USER_ACTIVITY_EVENT_ACCESSIBIliTY = 3;@SystemAPIpublic voID userActivity(long when,int event,int flags) {    try {        mService.userActivity(when,event,flags);    } catch (remoteexception e) {        throw e.rethrowFromSystemServer();    }}

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

private final class BinderService extends IPowerManager.Stub {  ......  public voID userActivity(long eventTime,int flags) {      final long Now = SystemClock.uptimeMillis();      ......      if (eventTime > Now) {          throw new IllegalArgumentException("event time must not be in the future");      }      final int uID = Binder.getCallingUID();      final long IDent = Binder.clearCallingIDentity();      try {          userActivityInternal(eventTime,flags,uID);      } finally {          Binder.restoreCallingIDentity(IDent);      }  }  ......}

PowerManager中userActivity请求调用服务端PowerManagerService BinderService的userActivity,即调用内部方法userActivityInternal。

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

private voID userActivityInternal(long eventTime,int flags,int uID) {    synchronized (mlock) {        if (userActivityNoUpdateLocked(eventTime,uID)) {            updatePowerStateLocked();        }    }}

userActivityInternal中首先调用userActivityNoUpdateLocked更新相关数据及状态(***NoUpdateLocked仅仅更新内部状态并不采取任何 *** 作),然后调用updatePowerStateLocked更新所有PowerState,下面分析userActivityNoUpdateLocked的实现,updatePowerStateLocked是PowerManagerService的核心方法,在最后进行分析。

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

private boolean userActivityNoUpdateLocked(long eventTime,int uID) {    // 如果发生时间是上一次休眠或唤醒前,或当前没有开机完成到systemReady,不采取 *** 作直接返回    if (eventTime < mLastSleepTime || eventTime < mLastWakeTime            || !mBootCompleted || !mSystemReady) {        return false;    }    try {        // 更新mLastInteractivePowerHintTime时间        if (eventTime > mLastInteractivePowerHintTime) {            powerHintInternal(POWER_HINT_INteraCTION,0);            mLastInteractivePowerHintTime = eventTime;        }        // 通过mNotifIEr通知BatteryStats UserActivity事件        mNotifIEr.onUserActivity(event,uID);        if (mUserInactiveOverrIDeFromWindowManager) {            mUserInactiveOverrIDeFromWindowManager = false;            mOverrIDdenTimeout = -1;        }        // 如果系统处于休眠状态,不进行处理        if (mWakefulness == WAKEFulnesS_ASLEEP                || mWakefulness == WAKEFulnesS_DOZING                || (flags & PowerManager.USER_ACTIVITY_FLAG_INDIRECT) != 0) {            return false;        }        // 根据flag是否在已变暗的情况下是否重启活动超时更新mLastUserActivityTimeNoChangelights或mLastUserActivityTime        // 并且设置mDirty DIRTY_USER_ACTIVITY        if ((flags & PowerManager.USER_ACTIVITY_FLAG_NO_CHANGE_liGHTS) != 0) {            if (eventTime > mLastUserActivityTimeNoChangelights                    && eventTime > mLastUserActivityTime) {                mLastUserActivityTimeNoChangelights = eventTime;                mDirty |= DIRTY_USER_ACTIVITY;                return true;            }        } else {            if (eventTime > mLastUserActivityTime) {                mLastUserActivityTime = eventTime;                mDirty |= DIRTY_USER_ACTIVITY;                return true;            }        }    } finally {        Trace.traceEnd(Trace.TRACE_TAG_POWER);    }    return false;}

gotoSleep
gotoSleep在PowerManager中的定义如下:
frameworks/base/core/java/androID/os/PowerManager.java

public voID goToSleep(long time) {    goToSleep(time,GO_TO_SLEEP_REASON_APPliCATION,0);}public voID goToSleep(long time,int reason,int flags) {    try {        mService.goToSleep(time,reason,flags);    } catch (remoteexception e) {        throw e.rethrowFromSystemServer();    }}

与userActivity一样,gotoSleep最终将调用到goToSleepInternal。
frameworks/base/services/core/java/com/androID/server/power/PowerManagerService.java

private final class BinderService extends IPowerManager.Stub {  ......  public voID goToSleep(long eventTime,int flags) {      if (eventTime > SystemClock.uptimeMillis()) {          throw new IllegalArgumentException("event time must not be in the future");      }      mContext.enforceCallingOrSelfPermission(              androID.Manifest.permission.DEVICE_POWER,null);      final int uID = Binder.getCallingUID();      final long IDent = Binder.clearCallingIDentity();      try {          goToSleepInternal(eventTime,uID);      } finally {          Binder.restoreCallingIDentity(IDent);      }  }  ......}private voID goToSleepInternal(long eventTime,int uID) {    synchronized (mlock) {        if (goToSleepNoUpdateLocked(eventTime,uID)) {            updatePowerStateLocked();        }    }}

goToSleepInternal中将执行goToSleepNoUpdateLocked更新内部状态,同样在updatePowerStateLocked中更新PowerState的 *** 作。
frameworks/base/services/core/java/com/androID/server/power/PowerManagerService.java

private boolean goToSleepNoUpdateLocked(long eventTime,int uID) {    // 当不处于awake状态或未开机systemReady,不处理    if (eventTime < mLastWakeTime            || mWakefulness == WAKEFulnesS_ASLEEP            || mWakefulness == WAKEFulnesS_DOZING            || !mBootCompleted || !mSystemReady) {        return false;    }    try {        ......        // 更新mLastSleepTime时间,设置DIRTY_WAKEFulnesS标志位        mLastSleepTime = eventTime;        mSandmanSummoned = true;        setWakefulnessLocked(WAKEFulnesS_DOZING,reason);        // Report the number of wake locks that will be cleared by going to sleep.        int numWakeLocksCleared = 0;        final int numWakeLocks = mWakeLocks.size();        for (int i = 0; i < numWakeLocks; i++) {            final WakeLock wakeLock = mWakeLocks.get(i);            switch (wakeLock.mFlags & PowerManager.WAKE_LOCK_LEVEL_MASK) {                case PowerManager.FulL_WAKE_LOCK:                case PowerManager.SCREEN_BRIGHT_WAKE_LOCK:                case PowerManager.SCREEN_DIM_WAKE_LOCK:                    numWakeLocksCleared += 1;                    break;            }        }        // Skip dozing if requested.        if ((flags & PowerManager.GO_TO_SLEEP_FLAG_NO_DOZE) != 0) {            reallyGoToSleepNoUpdateLocked(eventTime,uID);        }    } finally {        Trace.traceEnd(Trace.TRACE_TAG_POWER);    }    return true;}

goToSleepNoUpdateLocked中更新mLastSleepTime,mWakefulness,mDirty状态。

至此,本篇已结束,如有不对的地方,欢迎您的建议与指正。同时期待您的关注,感谢您的阅读,谢谢!

总结

以上是内存溢出为你收集整理的PowerManagerService(一)全部内容,希望文章能够帮你解决PowerManagerService(一)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1123357.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存