Android7.0 PowerManagerService亮灭屏分析(三)

本文深入分析了Android 7.0中PowerManagerService的亮屏与灭屏过程。从animateScreenBrightness函数的亮度渐变动画到RampAnimator的实现,再到requestDisplayStateLocked中的runnable机制,详细阐述了亮屏操作。同时,灭屏流程中,从输入事件处理到PhoneWindowManager的角色,再到AMS和WMS的交互,展示了灭屏的独特步骤,包括AMS的comeOutOfSleepIfNeedLocked函数、InputManagerService的屏幕状态记录以及灭屏动画的执行。文章最后总结了亮灭屏的完整流程。
摘要由CSDN通过智能技术生成

在前面两部分已经对绘制windows与设置设备状态进行了详细讲解. 之后接着就该对亮度值进行设置, 实现亮屏动作了.

在DisplayPowerController中的animateScreenBrightness函数通过亮度渐变动画来将亮度设置到目标亮度.

    // Brightness animation ramp rate in brightness units per second.
    private static final int BRIGHTNESS_RAMP_RATE_SLOW = 40;     //亮度渐变动画较慢的速率, 每秒变化40个亮度单位 

        mBrightnessRampRateFast = resources.getInteger(
                com.android.internal.R.integer.config_brightness_ramp_rate_fast);   //从配置文件中获取较快的亮度速率

        // Animate the screen brightness when the screen is on or dozing.
        // Skip the animation when the screen is off or suspended.
        if (!mPendingScreenOff) {
            if (state == Display.STATE_ON || state == Display.STATE_DOZE) {
                animateScreenBrightness(brightness,      //当亮屏或doze状态时有亮度渐变动画
                        slowChange ? BRIGHTNESS_RAMP_RATE_SLOW : mBrightnessRampRateFast);
            } else {
                animateScreenBrightness(brightness, 0);   //灭屏时没有亮度渐变动画,直接将亮度设置为0
            }
        }
在animateScreenBrightness函数中调用动画mScreenBrightnessRampAnimator对亮度值处理, 而mScreenBrightnessRampAnimator是在initialize()函数中进行初始化的, 在构造函数中将DisplayPowerState和DisplayPowerState.SCREEN_BRIGHTNESS传输过去.

  private void initialize() {
      //....
        mScreenBrightnessRampAnimator = new RampAnimator<DisplayPowerState>(  //泛型为DisplayPowerState
                mPowerState, DisplayPowerState.SCREEN_BRIGHTNESS);
        mScreenBrightnessRampAnimator.setListener(mRampAnimatorListener);
     //...
}

    private void animateScreenBrightness(int target, int rate) {
        if (DEBUG) {
            Slog.d(TAG, "Animating brightness: target=" + target +", rate=" + rate);
        }
        if (mScreenBrightnessRampAnimator.animateTo(target, rate)) {    //动画处理亮度值
            try {
                mBatteryStats.noteScreenBrightness(target);
            } catch (RemoteException ex) {
                // same process
            }
        }
    }

在RampAnimator的构造函数中将DisplayPowerState赋值给mObject, DisplayPowerState.SCREEN_BRIGHTNESS赋值给mProperty, 并且创建mChoreographer实例.

    public RampAnimator(T object, IntProperty<T> property) {
        mObject = object;
        mProperty = property;
        mChoreographer = Choreographer.getInstance();
    }

下面重点讲解animateTo函数.

    public boolean animateTo(int target, int rate) {
        // Immediately jump to the target the first time.
        if (mFirstTime || rate <= 0) {   //当第一次调用animateTo, 或者rate小于等于0时直接设置目标亮度.
            if (mFirstTime || target != mCurrentValue) {
                mFirstTime = false;   //之后就不是第一次调用该函数
                mRate = 0;              //设置mRate为0
                mTargetValue = target;   //设置目标亮度为target
                mCurrentValue = target;
                mProperty.setValue(mObject, target);   //调用DisplayPowerState.SCREEN_BRIGHTNESS设置亮度值
                if (mAnimating) {
                    mAnimating = false;
                    cancelAnimationCallback();
                }
                if (mListener != null) {
                    mListener.onAnimationEnd();  //动画结束
                }
                return true;
            }
            return false;
        }

        // Adjust the rate based on the closest target.
        // If a faster rate is specified, then use the new rate so that we converge
        // more rapidly based on the new request.
        // If a slower rate is specified, then use the new rate only if the current
        // value is somewhere in between the new and the old target meaning that
        // we will be ramping in a different direction to get there.
        // Otherwise, continue at the previous rate.
        if (!mAnimating
                || rate > mRate
                || (target <= mCurrentValue && mCurrentValue <= mTargetValue)
                || (mTargetValue <= mCurrentValue && mCurrentValue <= target)) {
            mRate = rate;   //重新调节亮度速率
        }

        final boolean changed = (mTargetValue != target);  //如果当前亮度值不等于目标亮度值,说明亮度改变了
        mTargetValue = target;    //重新设置mTargetValue

        // Start animating.  开始动画
        if (!mAnimating && target != mCurrentValue) {  
            mAnimating = true;
            mAnimatedValue = mCurrentValue;
            mLastFrameTimeNanos = System.nanoTime();
            postAnimationCallback();
        }

        return changed; 
    }
在postAnimationCallback中调用Choreographer的postCallback函数处理, 调用完成后回调回mAnimationCallback的run函数

    private void postAnimationCallback() {
        mChoreographer.postCallback(Choreographer.CALLBACK_ANIMATION, mAnimationCallback, null);
    }

    private final Runnable mAnima
  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值