[RRS] show resolution option on Settings page.

Due to the special width/ height of the device, we have to modify the
available condition otherwise the resolution option cannot be shown on Settings.

Bug: 258346214
Test: Lunch Settings and check the Display page
      atest SettingsUnitTests:ScreenResolutionControllerTest
      atest SettingsUnitTests:ScreenResolutionFragmentTest
Change-Id: Ie22df7dff6e31a7697c128eabe69dfe4bba29746
This commit is contained in:
Amy Hsu
2022-11-30 16:29:04 +00:00
parent c54584127b
commit be19785b51
6 changed files with 142 additions and 89 deletions

View File

@@ -578,13 +578,7 @@
<!-- The option list for switch screen resolution --> <!-- The option list for switch screen resolution -->
<string-array name="config_screen_resolution_options_strings" translatable="false"> <string-array name="config_screen_resolution_options_strings" translatable="false">
<item>@string/screen_resolution_option_high</item> <item>@string/screen_resolution_option_high</item>
<item>@string/screen_resolution_option_highest</item> <item>@string/screen_resolution_option_full</item>
</string-array>
<!-- The option summary list for screen resolution -->
<string-array name="config_screen_resolution_summaries_strings" translatable="false">
<item>@string/screen_resolution_summary_high</item>
<item>@string/screen_resolution_summary_highest</item>
</string-array> </string-array>
<!-- Whether to aggregate for network selection list--> <!-- Whether to aggregate for network selection list-->

View File

@@ -2201,14 +2201,10 @@
<!-- Display settings screen, screen resolution settings title [CHAR LIMIT=30] --> <!-- Display settings screen, screen resolution settings title [CHAR LIMIT=30] -->
<string name="screen_resolution_title">Screen resolution</string> <string name="screen_resolution_title">Screen resolution</string>
<!-- Display settings screen, screen resolution option for "FHD+" [CHAR LIMIT=45] --> <!-- Display settings screen, screen resolution option for high resolution [CHAR LIMIT=45] -->
<string name="screen_resolution_option_high">High resolution</string> <string name="screen_resolution_option_high">High resolution</string>
<!-- Display settings screen, screen resolution option for "QHD+" [CHAR LIMIT=45] --> <!-- Display settings screen, screen resolution option for full resolution [CHAR LIMIT=45] -->
<string name="screen_resolution_option_highest">Full resolution</string> <string name="screen_resolution_option_full">Full resolution</string>
<!-- Display settings screen, "FHD+" screen resolution summary [CHAR LIMIT=NONE] -->
<string name="screen_resolution_summary_high">1080p FHD+</string>
<!-- Display settings screen, "QHD+" screen resolution summary [CHAR LIMIT=NONE] -->
<string name="screen_resolution_summary_highest">1440p QHD+</string>
<!-- The footer message for switch screen resolution [CHAR LIMIT=NONE] --> <!-- The footer message for switch screen resolution [CHAR LIMIT=NONE] -->
<string name="screen_resolution_footer">Full resolution uses more of your battery. Switching your resolution may cause some apps to restart.</string> <string name="screen_resolution_footer">Full resolution uses more of your battery. Switching your resolution may cause some apps to restart.</string>
<!-- Message announced to a11y users when they selected one resolution [CHAR LIMIT=NONE] --> <!-- Message announced to a11y users when they selected one resolution [CHAR LIMIT=NONE] -->

View File

@@ -17,7 +17,9 @@
package com.android.settings.display; package com.android.settings.display;
import android.content.Context; import android.content.Context;
import android.graphics.Point;
import android.hardware.display.DisplayManager; import android.hardware.display.DisplayManager;
import android.util.Log;
import android.view.Display; import android.view.Display;
import androidx.annotation.VisibleForTesting; import androidx.annotation.VisibleForTesting;
@@ -25,32 +27,63 @@ import androidx.annotation.VisibleForTesting;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.core.BasePreferenceController; import com.android.settings.core.BasePreferenceController;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/** Controller that switch the screen resolution. */ /** Controller that switch the screen resolution. */
public class ScreenResolutionController extends BasePreferenceController { public class ScreenResolutionController extends BasePreferenceController {
private static final String TAG = "ScreenResolutionController";
static final int FHD_WIDTH = 1080; static final int HIGHRESOLUTION_IDX = 0;
static final int QHD_WIDTH = 1440; static final int FULLRESOLUTION_IDX = 1;
private Display mDisplay; private Display mDisplay;
private Set<Point> mSupportedResolutions = null;
private int mHighWidth = 0;
private int mFullWidth = 0;
private int mHighHeight = 0;
private int mFullHeight = 0;
public ScreenResolutionController(Context context, String key) { public ScreenResolutionController(Context context, String key) {
super(context, key); super(context, key);
mDisplay = mDisplay =
mContext.getSystemService(DisplayManager.class).getDisplay(Display.DEFAULT_DISPLAY); mContext.getSystemService(DisplayManager.class).getDisplay(Display.DEFAULT_DISPLAY);
initSupportedResolutionData();
} }
/** Check if the width is supported by the display. */ /**
private boolean isSupportedMode(int width) { * Initialize the resolution data. So far, we support two resolution switching. Save the width
* and the height for high resolution and full resolution.
*/
private void initSupportedResolutionData() {
// Collect and filter the resolutions
Set<Point> resolutions = new HashSet<>();
for (Display.Mode mode : getSupportedModes()) { for (Display.Mode mode : getSupportedModes()) {
if (mode.getPhysicalWidth() == width) return true; resolutions.add(new Point(mode.getPhysicalWidth(), mode.getPhysicalHeight()));
} }
return false; mSupportedResolutions = resolutions;
// Get the width and height for high resolution and full resolution
List<Point> resolutionList = new ArrayList<>(resolutions);
if (resolutionList == null || resolutionList.size() != 2) {
Log.e(TAG, "No support");
return;
}
Collections.sort(resolutionList, (p1, p2) -> p1.x * p1.y - p2.x * p2.y);
mHighWidth = resolutionList.get(HIGHRESOLUTION_IDX).x;
mHighHeight = resolutionList.get(HIGHRESOLUTION_IDX).y;
mFullWidth = resolutionList.get(FULLRESOLUTION_IDX).x;
mFullHeight = resolutionList.get(FULLRESOLUTION_IDX).y;
} }
/** Return true if the device contains two (or more) resolutions. */ /** Return true if the device contains two (or more) resolutions. */
protected boolean checkSupportedResolutions() { protected boolean checkSupportedResolutions() {
return isSupportedMode(FHD_WIDTH) && isSupportedMode(QHD_WIDTH); return getHighWidth() != 0 && getFullWidth() != 0;
} }
@Override @Override
@@ -61,20 +94,43 @@ public class ScreenResolutionController extends BasePreferenceController {
@Override @Override
public CharSequence getSummary() { public CharSequence getSummary() {
String summary = null; String summary = null;
switch (getDisplayWidth()) { int width = getDisplayWidth();
case FHD_WIDTH: if (width == mHighWidth) {
summary = mContext.getString(R.string.screen_resolution_summary_high); summary = mContext.getString(R.string.screen_resolution_option_high);
break; } else if (width == mFullWidth) {
case QHD_WIDTH: summary = mContext.getString(R.string.screen_resolution_option_full);
summary = mContext.getString(R.string.screen_resolution_summary_highest); } else {
break;
default:
summary = mContext.getString(R.string.screen_resolution_title); summary = mContext.getString(R.string.screen_resolution_title);
} }
return summary; return summary;
} }
/** Return all supported resolutions of the device. */
public Set<Point> getAllSupportedResolutions() {
return this.mSupportedResolutions;
}
/** Return the high resolution width of the device. */
public int getHighWidth() {
return this.mHighWidth;
}
/** Return the full resolution width of the device. */
public int getFullWidth() {
return this.mFullWidth;
}
/** Return the high resolution height of the device. */
public int getHighHeight() {
return this.mHighHeight;
}
/** Return the full resolution height of the device. */
public int getFullHeight() {
return this.mFullHeight;
}
@VisibleForTesting @VisibleForTesting
public int getDisplayWidth() { public int getDisplayWidth() {
return mDisplay.getMode().getPhysicalWidth(); return mDisplay.getMode().getPhysicalWidth();

View File

@@ -16,9 +16,6 @@
package com.android.settings.display; package com.android.settings.display;
import static com.android.settings.display.ScreenResolutionController.FHD_WIDTH;
import static com.android.settings.display.ScreenResolutionController.QHD_WIDTH;
import android.annotation.Nullable; import android.annotation.Nullable;
import android.app.settings.SettingsEnums; import android.app.settings.SettingsEnums;
import android.content.Context; import android.content.Context;
@@ -48,7 +45,6 @@ import com.android.settingslib.widget.IllustrationPreference;
import com.android.settingslib.widget.SelectorWithWidgetPreference; import com.android.settingslib.widget.SelectorWithWidgetPreference;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
@@ -59,9 +55,8 @@ public class ScreenResolutionFragment extends RadioButtonPickerFragment {
private static final String TAG = "ScreenResolution"; private static final String TAG = "ScreenResolution";
private Resources mResources; private Resources mResources;
private static final int FHD_INDEX = 0;
private static final int QHD_INDEX = 1;
private static final String SCREEN_RESOLUTION = "user_selected_resolution"; private static final String SCREEN_RESOLUTION = "user_selected_resolution";
private static final String SCREEN_RESOLUTION_KEY = "screen_resolution";
private Display mDefaultDisplay; private Display mDefaultDisplay;
private String[] mScreenResolutionOptions; private String[] mScreenResolutionOptions;
private Set<Point> mResolutions; private Set<Point> mResolutions;
@@ -71,6 +66,9 @@ public class ScreenResolutionFragment extends RadioButtonPickerFragment {
private DisplayObserver mDisplayObserver; private DisplayObserver mDisplayObserver;
private AccessibilityManager mAccessibilityManager; private AccessibilityManager mAccessibilityManager;
private int mHighWidth;
private int mFullWidth;
@Override @Override
public void onAttach(Context context) { public void onAttach(Context context) {
super.onAttach(context); super.onAttach(context);
@@ -81,11 +79,19 @@ public class ScreenResolutionFragment extends RadioButtonPickerFragment {
mResources = context.getResources(); mResources = context.getResources();
mScreenResolutionOptions = mScreenResolutionOptions =
mResources.getStringArray(R.array.config_screen_resolution_options_strings); mResources.getStringArray(R.array.config_screen_resolution_options_strings);
mScreenResolutionSummaries =
mResources.getStringArray(R.array.config_screen_resolution_summaries_strings);
mResolutions = getAllSupportedResolution();
mImagePreference = new IllustrationPreference(context); mImagePreference = new IllustrationPreference(context);
mDisplayObserver = new DisplayObserver(context); mDisplayObserver = new DisplayObserver(context);
ScreenResolutionController controller =
new ScreenResolutionController(context, SCREEN_RESOLUTION_KEY);
mResolutions = controller.getAllSupportedResolutions();
mHighWidth = controller.getHighWidth();
mFullWidth = controller.getFullWidth();
Log.i(TAG, "mHighWidth:" + mHighWidth + "mFullWidth:" + mFullWidth);
mScreenResolutionSummaries =
new String[] {
mHighWidth + " x " + controller.getHighHeight(),
mFullWidth + " x " + controller.getFullHeight()
};
} }
@Override @Override
@@ -133,16 +139,6 @@ public class ScreenResolutionFragment extends RadioButtonPickerFragment {
return candidates; return candidates;
} }
/** Get all supported resolutions on the device. */
private Set<Point> getAllSupportedResolution() {
Set<Point> resolutions = new HashSet<>();
for (Display.Mode mode : mDefaultDisplay.getSupportedModes()) {
resolutions.add(new Point(mode.getPhysicalWidth(), mode.getPhysicalHeight()));
}
return resolutions;
}
/** Get prefer display mode. */ /** Get prefer display mode. */
private Display.Mode getPreferMode(int width) { private Display.Mode getPreferMode(int width) {
for (Point resolution : mResolutions) { for (Point resolution : mResolutions) {
@@ -177,6 +173,7 @@ public class ScreenResolutionFragment extends RadioButtonPickerFragment {
try { try {
/** Apply the resolution change. */ /** Apply the resolution change. */
Log.i(TAG, "setUserPreferredDisplayMode: " + mode);
mDefaultDisplay.setUserPreferredDisplayMode(mode); mDefaultDisplay.setUserPreferredDisplayMode(mode);
} catch (Exception e) { } catch (Exception e) {
Log.e(TAG, "setUserPreferredDisplayMode() failed", e); Log.e(TAG, "setUserPreferredDisplayMode() failed", e);
@@ -194,16 +191,20 @@ public class ScreenResolutionFragment extends RadioButtonPickerFragment {
/** Get the key corresponding to the resolution. */ /** Get the key corresponding to the resolution. */
@VisibleForTesting @VisibleForTesting
String getKeyForResolution(int width) { String getKeyForResolution(int width) {
return width == FHD_WIDTH return width == mHighWidth
? mScreenResolutionOptions[FHD_INDEX] ? mScreenResolutionOptions[ScreenResolutionController.HIGHRESOLUTION_IDX]
: width == QHD_WIDTH ? mScreenResolutionOptions[QHD_INDEX] : null; : width == mFullWidth
? mScreenResolutionOptions[ScreenResolutionController.FULLRESOLUTION_IDX]
: null;
} }
/** Get the width corresponding to the resolution key. */ /** Get the width corresponding to the resolution key. */
int getWidthForResoluitonKey(String key) { int getWidthForResoluitonKey(String key) {
return mScreenResolutionOptions[FHD_INDEX].equals(key) return mScreenResolutionOptions[ScreenResolutionController.HIGHRESOLUTION_IDX].equals(key)
? FHD_WIDTH ? mHighWidth
: mScreenResolutionOptions[QHD_INDEX].equals(key) ? QHD_WIDTH : -1; : mScreenResolutionOptions[ScreenResolutionController.FULLRESOLUTION_IDX].equals(
key)
? mFullWidth : -1;
} }
@Override @Override
@@ -248,9 +249,11 @@ public class ScreenResolutionFragment extends RadioButtonPickerFragment {
private void updateIllustrationImage(IllustrationPreference preference) { private void updateIllustrationImage(IllustrationPreference preference) {
String key = getDefaultKey(); String key = getDefaultKey();
if (TextUtils.equals(mScreenResolutionOptions[FHD_INDEX], key)) { if (TextUtils.equals(
mScreenResolutionOptions[ScreenResolutionController.HIGHRESOLUTION_IDX], key)) {
preference.setLottieAnimationResId(R.drawable.screen_resolution_1080p); preference.setLottieAnimationResId(R.drawable.screen_resolution_1080p);
} else if (TextUtils.equals(mScreenResolutionOptions[QHD_INDEX], key)) { } else if (TextUtils.equals(
mScreenResolutionOptions[ScreenResolutionController.FULLRESOLUTION_IDX], key)) {
preference.setLottieAnimationResId(R.drawable.screen_resolution_1440p); preference.setLottieAnimationResId(R.drawable.screen_resolution_1440p);
} }
} }
@@ -300,7 +303,7 @@ public class ScreenResolutionFragment extends RadioButtonPickerFragment {
@Override @Override
protected boolean isPageSearchEnabled(Context context) { protected boolean isPageSearchEnabled(Context context) {
ScreenResolutionController mController = ScreenResolutionController mController =
new ScreenResolutionController(context, "fragment"); new ScreenResolutionController(context, SCREEN_RESOLUTION_KEY);
return mController.checkSupportedResolutions(); return mController.checkSupportedResolutions();
} }
}; };
@@ -408,6 +411,8 @@ public class ScreenResolutionFragment extends RadioButtonPickerFragment {
return false; return false;
} }
Log.i(TAG,
"resolution changed from " + mPreviousWidth.get() + " to " + getCurrentWidth());
return true; return true;
} }
} }

View File

@@ -35,22 +35,23 @@ import org.junit.runner.RunWith;
@RunWith(AndroidJUnit4.class) @RunWith(AndroidJUnit4.class)
public class ScreenResolutionControllerTest { public class ScreenResolutionControllerTest {
private static final int FHD_WIDTH = 1080;
private static final int QHD_WIDTH = 1440;
private ScreenResolutionController mController; private ScreenResolutionController mController;
private int mHighWidth;
private int mFullWidth;
@Before @Before
public void setUp() { public void setUp() {
Context context = spy(ApplicationProvider.getApplicationContext()); Context context = spy(ApplicationProvider.getApplicationContext());
mController = spy(new ScreenResolutionController(context, "test")); mController = spy(new ScreenResolutionController(context, "test"));
mHighWidth = mController.getHighWidth();
mFullWidth = mController.getFullWidth();
} }
@Test @Test
public void getAvailabilityStatus_hasFhdAndQhdModes_returnAvailable() { public void getAvailabilityStatus_hasFhdAndQhdModes_returnAvailable() {
Display.Mode modeA = new Display.Mode(0, FHD_WIDTH, 0, 0); Display.Mode modeA = new Display.Mode(0, mHighWidth, 0, 0);
Display.Mode modeB = new Display.Mode(0, QHD_WIDTH, 0, 0); Display.Mode modeB = new Display.Mode(0, mFullWidth, 0, 0);
Display.Mode[] modes = {modeA, modeB}; Display.Mode[] modes = {modeA, modeB};
doReturn(modes).when(mController).getSupportedModes(); doReturn(modes).when(mController).getSupportedModes();
@@ -60,27 +61,25 @@ public class ScreenResolutionControllerTest {
@Test @Test
public void getAvailabilityStatus_hasOneMode_returnUnsupported() { public void getAvailabilityStatus_hasOneMode_returnUnsupported() {
Display.Mode modeA = new Display.Mode(0, FHD_WIDTH, 0, 0); doReturn(0).when(mController).getHighWidth();
Display.Mode[] modes = {modeA};
doReturn(modes).when(mController).getSupportedModes();
assertThat(mController.getAvailabilityStatus()) assertThat(mController.getAvailabilityStatus())
.isEqualTo(BasePreferenceController.UNSUPPORTED_ON_DEVICE); .isEqualTo(BasePreferenceController.UNSUPPORTED_ON_DEVICE);
} }
@Test @Test
public void updateState_screenResolutionFHD_shouldSetSummaryToFHD() { public void updateState_HighResolution_shouldSetSummaryToHighResolution() {
int width = FHD_WIDTH; int width = mHighWidth;
doReturn(width).when(mController).getDisplayWidth(); doReturn(width).when(mController).getDisplayWidth();
assertThat(mController.getSummary().toString()).isEqualTo("1080p FHD+"); assertThat(mController.getSummary().toString()).isEqualTo("High resolution");
} }
@Test @Test
public void updateState_screenResolutionQHD_shouldSetSummaryToQHD() { public void updateState_FullResolution_shouldSetSummaryToFullResolution() {
int width = QHD_WIDTH; int width = mFullWidth;
doReturn(width).when(mController).getDisplayWidth(); doReturn(width).when(mController).getDisplayWidth();
assertThat(mController.getSummary().toString()).isEqualTo("1440p QHD+"); assertThat(mController.getSummary().toString()).isEqualTo("Full resolution");
} }
} }

View File

@@ -37,59 +37,62 @@ public class ScreenResolutionFragmentTest {
private Context mContext; private Context mContext;
private ScreenResolutionFragment mFragment; private ScreenResolutionFragment mFragment;
private ScreenResolutionController mController;
private static final int FHD_WIDTH = 1080; private int mHighWidth;
private static final int QHD_WIDTH = 1440; private int mFullWidth;
@Before @Before
@UiThreadTest @UiThreadTest
public void setup() { public void setup() {
mContext = spy(ApplicationProvider.getApplicationContext()); mContext = spy(ApplicationProvider.getApplicationContext());
mFragment = spy(new ScreenResolutionFragment()); mFragment = spy(new ScreenResolutionFragment());
mController = spy(new ScreenResolutionController(mContext, "test"));
mHighWidth = mController.getHighWidth();
mFullWidth = mController.getFullWidth();
} }
@Test @Test
@UiThreadTest @UiThreadTest
public void getDefaultKey_FHD() { public void getDefaultKey_highResolution() {
Display.Mode mode = new Display.Mode(0, FHD_WIDTH, 0, 0); Display.Mode mode = new Display.Mode(0, mHighWidth, 0, 0);
doReturn(mode).when(mFragment).getDisplayMode(); doReturn(mode).when(mFragment).getDisplayMode();
doReturn(mContext).when(mFragment).getContext(); doReturn(mContext).when(mFragment).getContext();
mFragment.onAttach(mContext); mFragment.onAttach(mContext);
assertThat(mFragment.getDefaultKey()).isEqualTo(mFragment.getKeyForResolution(FHD_WIDTH)); assertThat(mFragment.getDefaultKey()).isEqualTo(mFragment.getKeyForResolution(mHighWidth));
} }
@Test @Test
@UiThreadTest @UiThreadTest
public void getDefaultKey_QHD() { public void getDefaultKey_fullResolution() {
Display.Mode mode = new Display.Mode(0, QHD_WIDTH, 0, 0); Display.Mode mode = new Display.Mode(0, mFullWidth, 0, 0);
doReturn(mode).when(mFragment).getDisplayMode(); doReturn(mode).when(mFragment).getDisplayMode();
doReturn(mContext).when(mFragment).getContext(); doReturn(mContext).when(mFragment).getContext();
mFragment.onAttach(mContext); mFragment.onAttach(mContext);
assertThat(mFragment.getDefaultKey()).isEqualTo(mFragment.getKeyForResolution(QHD_WIDTH)); assertThat(mFragment.getDefaultKey()).isEqualTo(mFragment.getKeyForResolution(mFullWidth));
} }
@Test @Test
@UiThreadTest @UiThreadTest
public void setDefaultKey_FHD() { public void setDefaultKey_highResolution() {
doReturn(mContext).when(mFragment).getContext(); doReturn(mContext).when(mFragment).getContext();
mFragment.onAttach(mContext); mFragment.onAttach(mContext);
mFragment.setDefaultKey(mFragment.getKeyForResolution(FHD_WIDTH)); mFragment.setDefaultKey(mFragment.getKeyForResolution(mHighWidth));
verify(mFragment).setDisplayMode(FHD_WIDTH); verify(mFragment).setDisplayMode(mHighWidth);
} }
@Test @Test
@UiThreadTest @UiThreadTest
public void setDefaultKey_QHD() { public void setDefaultKey_fullResolution() {
doReturn(mContext).when(mFragment).getContext(); doReturn(mContext).when(mFragment).getContext();
mFragment.onAttach(mContext); mFragment.onAttach(mContext);
mFragment.setDefaultKey(mFragment.getKeyForResolution(QHD_WIDTH)); mFragment.setDefaultKey(mFragment.getKeyForResolution(mFullWidth));
verify(mFragment).setDisplayMode(QHD_WIDTH); verify(mFragment).setDisplayMode(mFullWidth);
} }
@Test @Test