android 9.0关于屏幕亮度的整理

时序图
上图是关于亮度调节的时序图

BrightnessController

  private final DisplayManager mDisplayManager;

private void setBrightness(int brightness) {
   
        mDisplayManager.setTemporaryBrightness(brightness);
    }

DisplayManager

  private final DisplayManagerGlobal mGlobal;
  
 public void setTemporaryBrightness(int brightness) {
   
        mGlobal.setTemporaryBrightness(brightness);
    }

DispalyManagerGlobal

private final IDisplayManager mDm;

 public void setTemporaryBrightness(int brightness) {
   
        try {
   
            mDm.setTemporaryBrightness(brightness);
        } catch (RemoteException ex) {
   
            throw ex.rethrowFromSystemServer();
        }
    }

这里的IDisplayManager是一个aidl,具体的调用直接看DisplayManagerService

DisplayManagerService

它里面的BinderService继承了IDisplayManager,具体实现如下


        @Override // Binder call
        public void setTemporaryBrightness(int brightness) {
   
            mContext.enforceCallingOrSelfPermission(
                    Manifest.permission.CONTROL_DISPLAY_BRIGHTNESS,
                    "Permission required to set the display's brightness");
            final long token = Binder.clearCallingIdentity();
            try {
   
                synchronized (mSyncRoot) {
   
                    mDisplayPowerController.setTemporaryBrightness(brightness);//在这里调用了设置亮度的方法
                }
            } finally {
   
                Binder.restoreCallingIdentity(token);
            }
        }

DisplayPowerController

 public void setTemporaryBrightness(int brightness) {
   
        Message msg = mHandler.obtainMessage(MSG_SET_TEMPORARY_BRIGHTNESS,
                brightness, 0 /*unused*/);
        msg.sendToTarget();
    }

看一下handMessage,把亮度值赋给了mTemporaryScreenBrightness 然后执行updatePowerState()方法

  case MSG_SET_TEMPORARY_BRIGHTNESS:
                    // TODO: Should we have a a timeout for the temporary brightness?
                    mTemporaryScreenBrightness = msg.arg1;
                    updatePowerState();
                    break;

updatePowerState()中对于亮度的设置在这里

         //在屏幕打开或打zing睡时对屏幕亮度进行动画处理。
         //当屏幕关闭,暂停或切换到VR或从VR切换时,跳过动画。
        if (!mPendingScreenOff) {
   
            if (mSkipScreenOnBrightnessRamp) {
   
                if (state == Display.STATE_ON) {
   
                    if (mSkipRampState == RAMP_STATE_SKIP_NONE && mDozing) {
   
                        mInitialAutoBrightness = brightness;
                        mSkipRampState = RAMP_STATE_SKIP_INITIAL;
                    } else if (mSkipRampState == RAMP_STATE_SKIP_INITIAL
                            && mUseSoftwareAutoBrightnessConfig
                            && brightness != mInitialAutoBrightness) {
   
                        mSkipRampState = RAMP_STATE_SKIP_AUTOBRIGHT;
                    } else if (mSkipRampState == RAMP_STATE_SKIP_AUTOBRIGHT) {
   
                        mSkipRampState = RAMP_STATE_SKIP_NONE;
                    }
                } else {
   
                    mSkipRampState = RAMP_STATE_SKIP_NONE;
                }
            }

            final boolean wasOrWillBeInVr =
                    (state == Display.STATE_VR || oldState == Display.STATE_VR);
            final boolean initialRampSkip =
                    state == Display.STATE_ON && mSkipRampState != RAMP_STATE_SKIP_NONE;
            // While dozing, sometimes the brightness is split into buckets. Rather than animating
            // through the buckets, which is unlikely to be smooth in the first place, just jump
            // right to the suggested brightness.
            final boolean hasBrightnessBuckets =
                    Display.isDozeState(state) && mBrightnessBucketsInDozeConfig;
            // If the color fade is totally covering the screen then we can change the backlight
            // level without it being a noticeable jump since any actual content isn't yet visible.
            final boolean isDisplayContentVisible =
                    mColorFadeEnabled && mPowerState.getColorFadeLevel() == 1.0f;
            final boolean brightnessIsTemporary =
                    mAppliedTemporaryBrightness || mAppliedTemporaryAutoBrightnessAdjustment;
            if (initialRampSkip || hasBrightnessBuckets
                    || wasOrWillBeInVr || !isDisplayContentVisible || brightnessIsTemporary) {
   
                animateScreenBrightness(brightness, 0);//动画处理
            } else {
   
                animateScreenBrightness(brightness,
                        slowChange ? mBrightnessRampRateSlow : mBrightnessRampRateFast);//动画处理
            }

            if (!brightnessIsTemporary) {
   
                notifyBrightnessChanged(brightness, userInitiatedChange, hadUserBrightnessPoint);
            }

        }

然后是animateScreenBrightness(int target, int rate)方法


    private void animateScreenBrightness(int target, int rate) {
   
        if (DEBUG) {
   
            Slog.d(TAG, "Animating brightness: target=" + target +", rate=" + rate);
        }
        if (mScreenBrightnessRampAnimator.animateTo(target, rate)) {
   //这里调用了动画调节
            Trace.traceCounter(Trace.TRACE_TAG_POWER, "TargetScreenBrightness", target);
            try {
   
                mBatteryStats.noteScreenBrightness(target);
            } catch (RemoteException ex) {
   
                // same process
            }
        }
    }

animateTo(target, rate)方法由mScreenBrightnessRampAnimator调用,mScreenBrightnessRampAnimator是RampAnimator类型的,这里对它的初始化在DisplayPowerController的initialize()里

 mScreenBrightnessRampAnimator = new RampAnimator<DisplayPowerState>(
                mPowerState, DisplayPowerState.SCREEN_BRIGHTNESS);
        mScreenBrightnessRampAnimator.setListener(mRampAnimatorListener);

RampAnimator

看一下RampAnimator的animateTo方法


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

    public boolean animateTo(int target, int rate) {
   
        if (!this.mFirstTime && rate > 0) {
   
            if (!this.mAnimating || rate > this.mRate || target <= this.mCurrentValue && this.mCurrentValue <= this.mTargetValue || this.mTargetValue <= this.mCurrentValue && this.mCurrentValue <= target) {
   
                this.mRate = rate;
            }

            boolean changed = this
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值