Use new getBrighntessInfo() API for brightness
The slider and settings need to use the new brightness api so that: 1) It can work for mutiple-displays that support brightness 2) Can work with high-brightness mode which can dynamically adjust the brightness min and max. Bug: 168210311 Test: Verify that slider can go to 100% with HBM on or off. Test: atest com.android.settings.display Change-Id: I01029e211f64f0a8598b1388dd3bb535eb0beb69
This commit is contained in:
@@ -20,13 +20,15 @@ import static com.android.settingslib.display.BrightnessUtils.convertLinearToGam
|
|||||||
import android.content.ContentResolver;
|
import android.content.ContentResolver;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.database.ContentObserver;
|
import android.database.ContentObserver;
|
||||||
|
import android.hardware.display.BrightnessInfo;
|
||||||
|
import android.hardware.display.DisplayManager;
|
||||||
|
import android.hardware.display.DisplayManager.DisplayListener;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.os.Looper;
|
import android.os.Looper;
|
||||||
import android.os.PowerManager;
|
import android.os.PowerManager;
|
||||||
import android.os.RemoteException;
|
import android.os.RemoteException;
|
||||||
import android.os.ServiceManager;
|
import android.os.ServiceManager;
|
||||||
import android.provider.Settings;
|
|
||||||
import android.provider.Settings.System;
|
import android.provider.Settings.System;
|
||||||
import android.service.vr.IVrManager;
|
import android.service.vr.IVrManager;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
@@ -49,42 +51,53 @@ public class BrightnessLevelPreferenceController extends AbstractPreferenceContr
|
|||||||
|
|
||||||
private static final String TAG = "BrightnessPrefCtrl";
|
private static final String TAG = "BrightnessPrefCtrl";
|
||||||
private static final String KEY_BRIGHTNESS = "brightness";
|
private static final String KEY_BRIGHTNESS = "brightness";
|
||||||
private static final Uri BRIGHTNESS_URI;
|
|
||||||
private static final Uri BRIGHTNESS_FOR_VR_URI;
|
private static final Uri BRIGHTNESS_FOR_VR_URI;
|
||||||
private static final Uri BRIGHTNESS_ADJ_URI;
|
private static final Uri BRIGHTNESS_ADJ_URI;
|
||||||
|
|
||||||
private final float mMinBrightness;
|
|
||||||
private final float mMaxBrightness;
|
|
||||||
private final float mMinVrBrightness;
|
private final float mMinVrBrightness;
|
||||||
private final float mMaxVrBrightness;
|
private final float mMaxVrBrightness;
|
||||||
private final ContentResolver mContentResolver;
|
private final ContentResolver mContentResolver;
|
||||||
|
private final Handler mHandler = new Handler(Looper.getMainLooper());
|
||||||
|
private final DisplayManager mDisplayManager;
|
||||||
|
|
||||||
private Preference mPreference;
|
private Preference mPreference;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
BRIGHTNESS_URI = System.getUriFor(System.SCREEN_BRIGHTNESS_FLOAT);
|
|
||||||
BRIGHTNESS_FOR_VR_URI = System.getUriFor(System.SCREEN_BRIGHTNESS_FOR_VR);
|
BRIGHTNESS_FOR_VR_URI = System.getUriFor(System.SCREEN_BRIGHTNESS_FOR_VR);
|
||||||
BRIGHTNESS_ADJ_URI = System.getUriFor(System.SCREEN_AUTO_BRIGHTNESS_ADJ);
|
BRIGHTNESS_ADJ_URI = System.getUriFor(System.SCREEN_AUTO_BRIGHTNESS_ADJ);
|
||||||
}
|
}
|
||||||
|
|
||||||
private ContentObserver mBrightnessObserver =
|
private ContentObserver mBrightnessObserver =
|
||||||
new ContentObserver(new Handler(Looper.getMainLooper())) {
|
new ContentObserver(mHandler) {
|
||||||
@Override
|
@Override
|
||||||
public void onChange(boolean selfChange) {
|
public void onChange(boolean selfChange) {
|
||||||
updatedSummary(mPreference);
|
updatedSummary(mPreference);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
private final DisplayListener mDisplayListener = new DisplayListener() {
|
||||||
|
@Override
|
||||||
|
public void onDisplayAdded(int displayId) {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDisplayRemoved(int displayId) {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDisplayChanged(int displayId) {
|
||||||
|
updatedSummary(mPreference);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
public BrightnessLevelPreferenceController(Context context, Lifecycle lifecycle) {
|
public BrightnessLevelPreferenceController(Context context, Lifecycle lifecycle) {
|
||||||
super(context);
|
super(context);
|
||||||
|
mDisplayManager = context.getSystemService(DisplayManager.class);
|
||||||
|
|
||||||
if (lifecycle != null) {
|
if (lifecycle != null) {
|
||||||
lifecycle.addObserver(this);
|
lifecycle.addObserver(this);
|
||||||
}
|
}
|
||||||
PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
|
final PowerManager powerManager = context.getSystemService(PowerManager.class);
|
||||||
mMinBrightness = powerManager.getBrightnessConstraint(
|
|
||||||
PowerManager.BRIGHTNESS_CONSTRAINT_TYPE_MINIMUM);
|
|
||||||
mMaxBrightness = powerManager.getBrightnessConstraint(
|
|
||||||
PowerManager.BRIGHTNESS_CONSTRAINT_TYPE_MAXIMUM);
|
|
||||||
mMinVrBrightness = powerManager.getBrightnessConstraint(
|
mMinVrBrightness = powerManager.getBrightnessConstraint(
|
||||||
PowerManager.BRIGHTNESS_CONSTRAINT_TYPE_MINIMUM_VR);
|
PowerManager.BRIGHTNESS_CONSTRAINT_TYPE_MINIMUM_VR);
|
||||||
mMaxVrBrightness = powerManager.getBrightnessConstraint(
|
mMaxVrBrightness = powerManager.getBrightnessConstraint(
|
||||||
@@ -115,14 +128,16 @@ public class BrightnessLevelPreferenceController extends AbstractPreferenceContr
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onStart() {
|
public void onStart() {
|
||||||
mContentResolver.registerContentObserver(BRIGHTNESS_URI, false, mBrightnessObserver);
|
|
||||||
mContentResolver.registerContentObserver(BRIGHTNESS_FOR_VR_URI, false, mBrightnessObserver);
|
mContentResolver.registerContentObserver(BRIGHTNESS_FOR_VR_URI, false, mBrightnessObserver);
|
||||||
mContentResolver.registerContentObserver(BRIGHTNESS_ADJ_URI, false, mBrightnessObserver);
|
mContentResolver.registerContentObserver(BRIGHTNESS_ADJ_URI, false, mBrightnessObserver);
|
||||||
|
mDisplayManager.registerDisplayListener(mDisplayListener, mHandler,
|
||||||
|
DisplayManager.EVENT_FLAG_DISPLAY_BRIGHTNESS);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onStop() {
|
public void onStop() {
|
||||||
mContentResolver.unregisterContentObserver(mBrightnessObserver);
|
mContentResolver.unregisterContentObserver(mBrightnessObserver);
|
||||||
|
mDisplayManager.unregisterDisplayListener(mDisplayListener);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updatedSummary(Preference preference) {
|
private void updatedSummary(Preference preference) {
|
||||||
@@ -132,15 +147,17 @@ public class BrightnessLevelPreferenceController extends AbstractPreferenceContr
|
|||||||
}
|
}
|
||||||
|
|
||||||
private double getCurrentBrightness() {
|
private double getCurrentBrightness() {
|
||||||
final int value;
|
int value = 0;
|
||||||
if (isInVrMode()) {
|
if (isInVrMode()) {
|
||||||
value = convertLinearToGammaFloat(System.getFloat(mContentResolver,
|
value = convertLinearToGammaFloat(System.getFloat(mContentResolver,
|
||||||
System.SCREEN_BRIGHTNESS_FOR_VR_FLOAT, mMaxBrightness),
|
System.SCREEN_BRIGHTNESS_FOR_VR_FLOAT, mMaxVrBrightness),
|
||||||
mMinVrBrightness, mMaxVrBrightness);
|
mMinVrBrightness, mMaxVrBrightness);
|
||||||
} else {
|
} else {
|
||||||
value = convertLinearToGammaFloat(Settings.System.getFloat(mContentResolver,
|
final BrightnessInfo info = mContext.getDisplay().getBrightnessInfo();
|
||||||
System.SCREEN_BRIGHTNESS_FLOAT, mMinBrightness),
|
if (info != null) {
|
||||||
mMinBrightness, mMaxBrightness);
|
value = convertLinearToGammaFloat(info.brightness, info.brightnessMinimum,
|
||||||
|
info.brightnessMaximum);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return getPercentage(value, GAMMA_SPACE_MIN, GAMMA_SPACE_MAX);
|
return getPercentage(value, GAMMA_SPACE_MIN, GAMMA_SPACE_MAX);
|
||||||
}
|
}
|
||||||
|
@@ -22,15 +22,16 @@ import static com.google.common.truth.Truth.assertThat;
|
|||||||
|
|
||||||
import static org.mockito.ArgumentMatchers.anyString;
|
import static org.mockito.ArgumentMatchers.anyString;
|
||||||
import static org.mockito.Mockito.doReturn;
|
import static org.mockito.Mockito.doReturn;
|
||||||
import static org.mockito.Mockito.reset;
|
|
||||||
import static org.mockito.Mockito.spy;
|
import static org.mockito.Mockito.spy;
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
import android.content.ContentResolver;
|
import android.content.ContentResolver;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.hardware.display.BrightnessInfo;
|
||||||
import android.os.PowerManager;
|
import android.os.PowerManager;
|
||||||
import android.provider.Settings.System;
|
import android.provider.Settings.System;
|
||||||
|
import android.view.Display;
|
||||||
|
|
||||||
import androidx.preference.Preference;
|
import androidx.preference.Preference;
|
||||||
import androidx.preference.PreferenceScreen;
|
import androidx.preference.PreferenceScreen;
|
||||||
@@ -52,6 +53,8 @@ public class BrightnessLevelPreferenceControllerTest {
|
|||||||
@Mock
|
@Mock
|
||||||
private PowerManager mPowerManager;
|
private PowerManager mPowerManager;
|
||||||
@Mock
|
@Mock
|
||||||
|
private Display mDisplay;
|
||||||
|
@Mock
|
||||||
private PreferenceScreen mScreen;
|
private PreferenceScreen mScreen;
|
||||||
@Mock
|
@Mock
|
||||||
private Preference mPreference;
|
private Preference mPreference;
|
||||||
@@ -65,7 +68,7 @@ public class BrightnessLevelPreferenceControllerTest {
|
|||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
MockitoAnnotations.initMocks(this);
|
MockitoAnnotations.initMocks(this);
|
||||||
mContext = RuntimeEnvironment.application;
|
mContext = spy(RuntimeEnvironment.application);
|
||||||
mContentResolver = mContext.getContentResolver();
|
mContentResolver = mContext.getContentResolver();
|
||||||
when(mPowerManager.getBrightnessConstraint(
|
when(mPowerManager.getBrightnessConstraint(
|
||||||
PowerManager.BRIGHTNESS_CONSTRAINT_TYPE_MINIMUM)).thenReturn(0.0f);
|
PowerManager.BRIGHTNESS_CONSTRAINT_TYPE_MINIMUM)).thenReturn(0.0f);
|
||||||
@@ -78,6 +81,7 @@ public class BrightnessLevelPreferenceControllerTest {
|
|||||||
ShadowApplication.getInstance().setSystemService(POWER_SERVICE,
|
ShadowApplication.getInstance().setSystemService(POWER_SERVICE,
|
||||||
mPowerManager);
|
mPowerManager);
|
||||||
when(mScreen.findPreference(anyString())).thenReturn(mPreference);
|
when(mScreen.findPreference(anyString())).thenReturn(mPreference);
|
||||||
|
when(mContext.getDisplay()).thenReturn(mDisplay);
|
||||||
mController = spy(new BrightnessLevelPreferenceController(mContext, null));
|
mController = spy(new BrightnessLevelPreferenceController(mContext, null));
|
||||||
doReturn(false).when(mController).isInVrMode();
|
doReturn(false).when(mController).isInVrMode();
|
||||||
}
|
}
|
||||||
@@ -101,8 +105,6 @@ public class BrightnessLevelPreferenceControllerTest {
|
|||||||
|
|
||||||
controller.onStart();
|
controller.onStart();
|
||||||
|
|
||||||
assertThat(shadowContentResolver.getContentObservers(
|
|
||||||
System.getUriFor(System.SCREEN_BRIGHTNESS_FLOAT))).isNotEmpty();
|
|
||||||
assertThat(shadowContentResolver.getContentObservers(
|
assertThat(shadowContentResolver.getContentObservers(
|
||||||
System.getUriFor(System.SCREEN_BRIGHTNESS_FOR_VR))).isNotEmpty();
|
System.getUriFor(System.SCREEN_BRIGHTNESS_FOR_VR))).isNotEmpty();
|
||||||
assertThat(shadowContentResolver.getContentObservers(
|
assertThat(shadowContentResolver.getContentObservers(
|
||||||
@@ -119,8 +121,6 @@ public class BrightnessLevelPreferenceControllerTest {
|
|||||||
controller.onStart();
|
controller.onStart();
|
||||||
controller.onStop();
|
controller.onStop();
|
||||||
|
|
||||||
assertThat(shadowContentResolver.getContentObservers(
|
|
||||||
System.getUriFor(System.SCREEN_BRIGHTNESS_FLOAT))).isEmpty();
|
|
||||||
assertThat(shadowContentResolver.getContentObservers(
|
assertThat(shadowContentResolver.getContentObservers(
|
||||||
System.getUriFor(System.SCREEN_BRIGHTNESS_FOR_VR_FLOAT))).isEmpty();
|
System.getUriFor(System.SCREEN_BRIGHTNESS_FOR_VR_FLOAT))).isEmpty();
|
||||||
assertThat(shadowContentResolver.getContentObservers(
|
assertThat(shadowContentResolver.getContentObservers(
|
||||||
@@ -143,7 +143,8 @@ public class BrightnessLevelPreferenceControllerTest {
|
|||||||
System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS_MODE,
|
System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS_MODE,
|
||||||
System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
|
System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
|
||||||
|
|
||||||
System.putFloat(mContentResolver, System.SCREEN_BRIGHTNESS_FLOAT, 0.1f);
|
when(mDisplay.getBrightnessInfo()).thenReturn(
|
||||||
|
new BrightnessInfo(0.1f, 0.0f, 1.0f, BrightnessInfo.HIGH_BRIGHTNESS_MODE_OFF));
|
||||||
|
|
||||||
mController.updateState(mPreference);
|
mController.updateState(mPreference);
|
||||||
|
|
||||||
@@ -156,7 +157,8 @@ public class BrightnessLevelPreferenceControllerTest {
|
|||||||
System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS_MODE,
|
System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS_MODE,
|
||||||
System.SCREEN_BRIGHTNESS_MODE_MANUAL);
|
System.SCREEN_BRIGHTNESS_MODE_MANUAL);
|
||||||
|
|
||||||
System.putFloat(mContentResolver, System.SCREEN_BRIGHTNESS_FLOAT, 0.5f);
|
when(mDisplay.getBrightnessInfo()).thenReturn(
|
||||||
|
new BrightnessInfo(0.5f, 0.0f, 1.0f, BrightnessInfo.HIGH_BRIGHTNESS_MODE_OFF));
|
||||||
|
|
||||||
mController.updateState(mPreference);
|
mController.updateState(mPreference);
|
||||||
|
|
||||||
@@ -175,32 +177,5 @@ public class BrightnessLevelPreferenceControllerTest {
|
|||||||
System.putFloat(mContentResolver, System.SCREEN_BRIGHTNESS_FOR_VR_FLOAT, -20f);
|
System.putFloat(mContentResolver, System.SCREEN_BRIGHTNESS_FOR_VR_FLOAT, -20f);
|
||||||
mController.updateState(mPreference);
|
mController.updateState(mPreference);
|
||||||
verify(mPreference).setSummary("0%");
|
verify(mPreference).setSummary("0%");
|
||||||
|
|
||||||
// Auto mode
|
|
||||||
doReturn(false).when(mController).isInVrMode();
|
|
||||||
System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS_MODE,
|
|
||||||
System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
|
|
||||||
|
|
||||||
reset(mPreference);
|
|
||||||
System.putFloat(mContentResolver, System.SCREEN_BRIGHTNESS_FLOAT, 1.15f);
|
|
||||||
mController.updateState(mPreference);
|
|
||||||
verify(mPreference).setSummary("100%");
|
|
||||||
|
|
||||||
System.putFloat(mContentResolver, System.SCREEN_BRIGHTNESS_FLOAT, -10f);
|
|
||||||
mController.updateState(mPreference);
|
|
||||||
verify(mPreference).setSummary("0%");
|
|
||||||
|
|
||||||
// Manual mode
|
|
||||||
System.putInt(mContentResolver, System.SCREEN_BRIGHTNESS_MODE,
|
|
||||||
System.SCREEN_BRIGHTNESS_MODE_MANUAL);
|
|
||||||
|
|
||||||
reset(mPreference);
|
|
||||||
System.putFloat(mContentResolver, System.SCREEN_BRIGHTNESS_FLOAT, 1.15f);
|
|
||||||
mController.updateState(mPreference);
|
|
||||||
verify(mPreference).setSummary("100%");
|
|
||||||
|
|
||||||
System.putFloat(mContentResolver, System.SCREEN_BRIGHTNESS_FLOAT, -10f);
|
|
||||||
mController.updateState(mPreference);
|
|
||||||
verify(mPreference).setSummary("0%");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user