PowerManagerService中的核心方法(一)

一:

1587      /**
1588       * Updates the global power state based on dirty bits recorded in mDirty.
1589       *
1590       * This is the main function that performs power state transitions.
1591       * We centralize them here so that we can recompute the power state completely
1592       * each time something important changes, and ensure that we do it the same
1593       * way each time.  The point is to gather all of the transition logic here.
1594       */
1595      private void updatePowerStateLocked() {
1596          if (!mSystemReady || mDirty == 0) {
1597              return;
1598          }
1599          if (!Thread.holdsLock(mLock)) {
1600              Slog.wtf(TAG, "Power manager lock was not held when calling updatePowerStateLocked");
1601          }
1602  
1603          Trace.traceBegin(Trace.TRACE_TAG_POWER, "updatePowerState");
1604          try {
1605              // Phase 0: Basic state updates.
                  /**
                   *如果mIsPowered的值或者充电类型一旦发生了改变,都会执行mDirty |=                                       DIRTY_IS_POWERED.
                   *同时判断是否需要插拔usb时有亮屏操作
                   *更新省电模式
                   */
1606              updateIsPoweredLocked(mDirty);
                  /*更新mStayOn的值,如果为true,设备将保持唤醒状态,同时将mDirty置位mDirty |=     DIRTY_STAY_ON;*/
1607              updateStayOnLocked(mDirty);
                  /*屏幕亮度相关*/
1608              updateScreenBrightnessBoostLocked(mDirty);
1609  
1610              // Phase 1: Update wakefulness.
1611              // Loop because the wake lock and user activity computations are influenced
1612              // by changes in wakefulness.
1613              final long now = SystemClock.uptimeMillis();
1614              int dirtyPhase2 = 0;
1615              for (;;) {
1616                  int dirtyPhase1 = mDirty;
1617                  dirtyPhase2 |= dirtyPhase1;
1618                  mDirty = 0;
1619                  /*更新mWakeLockSummary的值以汇总所有活动唤醒锁的状态
                       *系统处于睡眠状态时忽略大多数唤醒锁
                       */
1620                  updateWakeLockSummaryLocked(dirtyPhase1);
                      /* 更新mUserActivitySummary的值以汇总用户请求的系统状态,例如屏幕应该是        亮还是暗。
                       * 系统处于休眠状态时将忽略用户活动。
                       */ 
1621                  updateUserActivitySummaryLocked(now, dirtyPhase1);

                       /**根据当前唤醒锁定和用户活动状态决定设备是否应该开启屏保的功能
                        *如果wakefulness改变并且我们需要重新启动电源状态计算,则返回true
                        */
1622                  if (!updateWakefulnessLocked(dirtyPhase1)) {
1623                      break;
1624                  }
1625              }
1626  
1627              // Phase 2: Lock profiles that became inactive/not kept awake.
1628              updateProfilesLocked(now);
1629  
1630              // Phase 3: Update display power state.
1631              final boolean displayBecameReady = updateDisplayPowerStateLocked(dirtyPhase2);
1632  
1633              // Phase 4: Update dream state (depends on display ready signal).
1634              updateDreamLocked(dirtyPhase2, displayBecameReady);
1635  
1636              // Phase 5: Send notifications, if needed.
1637              finishWakefulnessChangeIfNeededLocked();
1638  
1639              // Phase 6: Update suspend blocker.
1640              // Because we might release the last suspend blocker here, we need to make sure
1641              // we finished everything else first!
1642              updateSuspendBlockerLocked();
1643          } finally {
1644              Trace.traceEnd(Trace.TRACE_TAG_POWER);
1645          }
1646      }

updateIsPoweredLocked()

1671      /**
1672       * Updates the value of mIsPowered.
1673       * Sets DIRTY_IS_POWERED if a change occurred.
1674       */
1675      private void updateIsPoweredLocked(int dirty) {
1676          if ((dirty & DIRTY_BATTERY_STATE) != 0) {
1677              final boolean wasPowered = mIsPowered;
1678              final int oldPlugType = mPlugType;
1679              final boolean oldLevelLow = mBatteryLevelLow;
1680              mIsPowered = mBatteryManagerInternal.isPowered(BatteryManager.BATTERY_PLUGGED_ANY);
1681              mPlugType = mBatteryManagerInternal.getPlugType();
1682              mBatteryLevel = mBatteryManagerInternal.getBatteryLevel();
1683              mBatteryLevelLow = mBatteryManagerInternal.getBatteryLevelLow();
1684  
1685              if (DEBUG_SPEW) {
1686                  Slog.d(TAG, "updateIsPoweredLocked: wasPowered=" + wasPowered
1687                          + ", mIsPowered=" + mIsPowered
1688                          + ", oldPlugType=" + oldPlugType
1689                          + ", mPlugType=" + mPlugType
1690                          + ", mBatteryLevel=" + mBatteryLevel);
1691              }
1692              /*充电状态发生改变 mDirty |= DIRTY_IS_POWERED
1693              if (wasPowered != mIsPowered || oldPlugType != mPlugType) {
1694                  mDirty |= DIRTY_IS_POWERED;
1695  
1696                  // Update wireless dock detection state.
1697                  final boolean dockedOnWirelessCharger = mWirelessChargerDetector.update(
1698                          mIsPowered, mPlugType);
1699  
1700                  // Treat plugging and unplugging the devices as a user activity.
1701                  // Users find it disconcerting when they plug or unplug the device
1702                  // and it shuts off right away.
1703                  // Some devices also wake the device when plugged or unplugged because
1704                  // they don't have a charging LED.
1705                  final long now = SystemClock.uptimeMillis();
                       //插拔usb是否唤醒屏幕
1706                  if (shouldWakeUpWhenPluggedOrUnpluggedLocked(wasPowered, oldPlugType,
1707                          dockedOnWirelessCharger)) {
1708                      wakeUpNoUpdateLocked(now, "android.server.power:POWER", Process.SYSTEM_UID,
1709                              mContext.getOpPackageName(), Process.SYSTEM_UID);
1710                  }
                      //更新用户活动
1711                  userActivityNoUpdateLocked(
1712                          now, PowerManager.USER_ACTIVITY_EVENT_OTHER, 0, Process.SYSTEM_UID);
1713  
1714                  // only play charging sounds if boot is completed so charging sounds don't play
1715                  // with potential notification sounds
                      //如果开机完成,充电时只播放充电声不播放通知声
1716                  if (mBootCompleted) {
1717                      if (mIsPowered && !BatteryManager.isPlugWired(oldPlugType)
1718                              && BatteryManager.isPlugWired(mPlugType)) {
1719                          mNotifier.onWiredChargingStarted();
1720                      } else if (dockedOnWirelessCharger) {
1721                          mNotifier.onWirelessChargingStarted(mBatteryLevel);
1722                      }
1723                  }
1724              }
1725              //更新省电模式
1726              mBatterySaverStateMachine.setBatteryStatus(mIsPowered, mBatteryLevel, mBatteryLevelLow);
1727          }
1728      }

updateStayOnLocked()

1771      /**
1772       * Updates the value of mStayOn.
1773       * Sets DIRTY_STAY_ON if a change occurred.
1774       */
1775      private void updateStayOnLocked(int dirty) {
1776          if ((dirty & (DIRTY_BATTERY_STATE | DIRTY_SETTINGS)) != 0) {
1777              final boolean wasStayOn = mStayOn;
                  /**
                   *mStayOnWhilePluggedInSetting 是数据库    Settings.Global.STAY_ON_WHILE_PLUGGED_IN中获取的,充电时是否保持屏幕唤醒?
                   *&& DevicePolicyManager中未设置最大关闭时间
                   */
1778              if (mStayOnWhilePluggedInSetting != 0
1779                      && !isMaximumScreenOffTimeoutFromDeviceAdminEnforcedLocked()) {
                   /*是否在电池状态未知的情况下充电*/
1780                  mStayOn = mBatteryManagerInternal.isPowered(mStayOnWhilePluggedInSetting);
1781              } else {
1782                  mStayOn = false;
1783              }
1784              //如果 mStayOn 的值发生了改变,那么mDirty 置位
1785              if (mStayOn != wasStayOn) {
1786                  mDirty |= DIRTY_STAY_ON;
1787              }
1788          }
1789      }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值