Android 9.0 (P版本) 亮度控制接口变更

1. Android 9.0 之前的亮度控制接口

import android.os.IPowerManager;
import android.provider.Settings;
import android.content.Context;


    /**
     * Used by the settings application and brightness control widgets to
     * temporarily override the current screen brightness setting so that the
     * user can observe the effect of an intended settings change without
     * applying it immediately.
     * 

* The override will be canceled when the setting value is next updated. * * brightness The overridden brightness. * @see android.provider.Settings.System#SCREEN_BRIGHTNESS */ public static void setBrightnessValue(Context mContext, int value) { try { IPowerManager power = IPowerManager.Stub.asInterface(ServiceManager.getService(Context.POWER_SERVICE)); if (power != null) { power.setTemporaryScreenBrightnessSettingOverride(value); } } catch (RemoteException doe) { Log.w(TAG, doe); } Settings.System.putInt(mContext.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, value); } public static void setBrightnessValueBg(Context mContext, int value) { try { IPowerManager power = IPowerManager.Stub.asInterface(ServiceManager.getService(Context.POWER_SERVICE)); if (power != null) { power.setTemporaryScreenBrightnessSettingOverride(value); } } catch (RemoteException doe) { Log.w(TAG, doe); } }

1.1 参见Android 8.0的SystemUI代码

frameworks/base/packages/SystemUI/src/com/android/systemui/settings/BrightnessController.java:419: mPower.setTemporaryScreenBrightnessSettingOverride(brightness);

package com.android.systemui.settings;

public class BrightnessController implements ToggleSlider.Listener {

    private void setBrightness(int brightness) {
        try {
            mPower.setTemporaryScreenBrightnessSettingOverride(brightness);
        } catch (RemoteException ex) {
        }
    }

2. Android 9.0 的亮度控制接口

2.1 参考Android 9.0的SystemUI代码

package com.android.systemui.settings;

import android.hardware.display.DisplayManager;


public class BrightnessController implements ToggleSlider.Listener {

    private final DisplayManager mDisplayManager;


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

2.2 查看 DisplayManager.setTemporaryBrightness源码

  • 源码路径

frameworks/base/core/java/android/hardware/display/DisplayManager.java

root@69959bbb90c6:/home/suhuazhi/9.1# find frameworks/ -name  "DisplayManager.java"
frameworks/base/core/java/android/hardware/display/DisplayManager.java

  • 查看实现方式 -DisplayManager.setTemporaryBrightness
package android.hardware.display;

@SystemService(Context.DISPLAY_SERVICE)
public final class DisplayManager {

    private final DisplayManagerGlobal mGlobal;


    /**
     * Temporarily sets the brightness of the display.
     * 

* Requires the {@link android.Manifest.permission#CONTROL_DISPLAY_BRIGHTNESS} permission. *

* * @param brightness The brightness value from 0 to 255. * * @hide Requires signature permission. */ public void setTemporaryBrightness(int brightness) { mGlobal.setTemporaryBrightness(brightness); }
  • 源码路径
    frameworks/base/core/java/android/hardware/display/DisplayManagerGlobal.java
root@69959bbb90c6:/home/suhuazhi/9.1# find frameworks/ -name  "DisplayManagerGlobal.java"
frameworks/base/core/java/android/hardware/display/DisplayManagerGlobal.java
  • 查看实现方式 -DisplayManagerGlobal.setTemporaryBrightness
package android.hardware.display;


public final class DisplayManagerGlobal {

    private final IDisplayManager mDm;


    /**
     * Temporarily sets the brightness of the display.
     * 

* Requires the {@link android.Manifest.permission#CONTROL_DISPLAY_BRIGHTNESS} permission. *

* * @param brightness The brightness value from 0 to 255. * * @hide Requires signature permission. */ public void setTemporaryBrightness(int brightness) { try { mDm.setTemporaryBrightness(brightness); } catch (RemoteException ex) { throw ex.rethrowFromSystemServer(); } }
  • 查看实现方式 -DisplayManagerService.setTemporaryBrightness
  • ./frameworks/base/services/core/java/com/android/server/display/DisplayManagerService.java

上述 IDisplayManager 是一个aidl接口,对应找DisplayManagerService即可

package com.android.server.display;

public final class DisplayManagerService extends SystemService {


        @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);
            }
        }

上述需要权限

    
    

3. 综合上述Android 9.0 的亮度控制接口

  • 权限
    
    
  • 软件接口
import android.hardware.display.DisplayManager;
import android.content.Context;
import android.os.ServiceManager;
import android.os.IPowerManager;

    /**
     * Used by the settings application and brightness control widgets to
     * temporarily override the current screen brightness setting so that the
     * user can observe the effect of an intended settings change without
     * applying it immediately.
     * 

* The override will be canceled when the setting value is next updated. * * brightness The overridden brightness. * @see android.provider.Settings.System#SCREEN_BRIGHTNESS */ public static void setBrightnessValue(Context mContext, int value) { setBrightnessValueBg(mContext, value); Settings.System.putInt(mContext.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, value); } public static void setBrightnessValueBg(Context mContext, int value) { if (android.os.Build.VERSION.SDK_INT >= 27) { // DisplayManager mDisplayManager = mContext.getSystemService(DisplayManager.class); mDisplayManager.setTemporaryBrightness(value); } else { /*try { IPowerManager power = IPowerManager.Stub.asInterface(ServiceManager.getService(Context.POWER_SERVICE)); if (power != null) { power.setTemporaryScreenBrightnessSettingOverride(value); } } catch (RemoteException doe) { Log.w(TAG, doe); }*/ } }

你可能感兴趣的:(Android,P,Android系统)