From 0d9e5d600c3577bfae1af4009ac04ccbad38946b Mon Sep 17 00:00:00 2001 From: Amy Hsu Date: Wed, 30 Nov 2022 16:29:04 +0000 Subject: [PATCH] [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 Change-Id: I577e0e1227727aab75787c1f6115091e0c6158e1 --- res/values/config.xml | 8 +- res/values/strings.xml | 10 +-- .../display/ScreenResolutionController.java | 90 +++++++++++++++---- .../display/ScreenResolutionFragment.java | 56 ++++++------ 4 files changed, 106 insertions(+), 58 deletions(-) diff --git a/res/values/config.xml b/res/values/config.xml index bbacc5c23ca..4d0c8914290 100755 --- a/res/values/config.xml +++ b/res/values/config.xml @@ -562,13 +562,7 @@ @string/screen_resolution_option_high - @string/screen_resolution_option_highest - - - - - @string/screen_resolution_summary_high - @string/screen_resolution_summary_highest + @string/screen_resolution_option_full diff --git a/res/values/strings.xml b/res/values/strings.xml index 927f84d046f..fe0cd87fbdd 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -2102,14 +2102,10 @@ Screen resolution - + High resolution - - Full resolution - - 1080p FHD+ - - 1440p QHD+ + + Full resolution Full resolution uses more of your battery. Switching your resolution may cause some apps to restart. diff --git a/src/com/android/settings/display/ScreenResolutionController.java b/src/com/android/settings/display/ScreenResolutionController.java index dca12757e18..a3433d405a5 100644 --- a/src/com/android/settings/display/ScreenResolutionController.java +++ b/src/com/android/settings/display/ScreenResolutionController.java @@ -17,7 +17,9 @@ package com.android.settings.display; import android.content.Context; +import android.graphics.Point; import android.hardware.display.DisplayManager; +import android.util.Log; import android.view.Display; import androidx.annotation.VisibleForTesting; @@ -25,32 +27,63 @@ import androidx.annotation.VisibleForTesting; import com.android.settings.R; 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. */ public class ScreenResolutionController extends BasePreferenceController { - - static final int FHD_WIDTH = 1080; - static final int QHD_WIDTH = 1440; + private static final String TAG = "ScreenResolutionController"; + static final int HIGHRESOLUTION_IDX = 0; + static final int FULLRESOLUTION_IDX = 1; private Display mDisplay; + private Set mSupportedResolutions = null; + private int mHighWidth = 0; + private int mFullWidth = 0; + private int mHighHeight = 0; + private int mFullHeight = 0; public ScreenResolutionController(Context context, String key) { super(context, key); mDisplay = 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 resolutions = new HashSet<>(); 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 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. */ protected boolean checkSupportedResolutions() { - return isSupportedMode(FHD_WIDTH) && isSupportedMode(QHD_WIDTH); + return getHighWidth() != 0 && getFullWidth() != 0; } @Override @@ -61,20 +94,43 @@ public class ScreenResolutionController extends BasePreferenceController { @Override public CharSequence getSummary() { String summary = null; - switch (getDisplayWidth()) { - case FHD_WIDTH: - summary = mContext.getString(R.string.screen_resolution_summary_high); - break; - case QHD_WIDTH: - summary = mContext.getString(R.string.screen_resolution_summary_highest); - break; - default: - summary = mContext.getString(R.string.screen_resolution_title); + int width = getDisplayWidth(); + if (width == mHighWidth) { + summary = mContext.getString(R.string.screen_resolution_option_high); + } else if (width == mFullWidth) { + summary = mContext.getString(R.string.screen_resolution_option_full); + } else { + summary = mContext.getString(R.string.screen_resolution_title); } return summary; } + /** Return all supported resolutions of the device. */ + public Set 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 public int getDisplayWidth() { return mDisplay.getMode().getPhysicalWidth(); diff --git a/src/com/android/settings/display/ScreenResolutionFragment.java b/src/com/android/settings/display/ScreenResolutionFragment.java index 665f6b08d69..687fdb88fea 100644 --- a/src/com/android/settings/display/ScreenResolutionFragment.java +++ b/src/com/android/settings/display/ScreenResolutionFragment.java @@ -16,9 +16,6 @@ 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.app.settings.SettingsEnums; import android.content.Context; @@ -48,7 +45,6 @@ import com.android.settingslib.widget.IllustrationPreference; import com.android.settingslib.widget.SelectorWithWidgetPreference; import java.util.ArrayList; -import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.concurrent.atomic.AtomicInteger; @@ -59,8 +55,6 @@ public class ScreenResolutionFragment extends RadioButtonPickerFragment { private static final String TAG = "ScreenResolution"; 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 Display mDefaultDisplay; private String[] mScreenResolutionOptions; @@ -71,6 +65,9 @@ public class ScreenResolutionFragment extends RadioButtonPickerFragment { private DisplayObserver mDisplayObserver; private AccessibilityManager mAccessibilityManager; + private int mHighWidth; + private int mFullWidth; + @Override public void onAttach(Context context) { super.onAttach(context); @@ -81,11 +78,18 @@ public class ScreenResolutionFragment extends RadioButtonPickerFragment { mResources = context.getResources(); mScreenResolutionOptions = 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); mDisplayObserver = new DisplayObserver(context); + ScreenResolutionController mController = + new ScreenResolutionController(context, "fragment"); + mResolutions = mController.getAllSupportedResolutions(); + mHighWidth = mController.getHighWidth(); + mFullWidth = mController.getFullWidth(); + mScreenResolutionSummaries = + new String[] { + mHighWidth + " x " + mController.getHighHeight(), + mFullWidth + " x " + mController.getFullHeight() + }; } @Override @@ -133,16 +137,6 @@ public class ScreenResolutionFragment extends RadioButtonPickerFragment { return candidates; } - /** Get all supported resolutions on the device. */ - private Set getAllSupportedResolution() { - Set resolutions = new HashSet<>(); - for (Display.Mode mode : mDefaultDisplay.getSupportedModes()) { - resolutions.add(new Point(mode.getPhysicalWidth(), mode.getPhysicalHeight())); - } - - return resolutions; - } - /** Get prefer display mode. */ private Display.Mode getPreferMode(int width) { for (Point resolution : mResolutions) { @@ -177,6 +171,7 @@ public class ScreenResolutionFragment extends RadioButtonPickerFragment { try { /** Apply the resolution change. */ + Log.i(TAG, "setUserPreferredDisplayMode: " + mode); mDefaultDisplay.setUserPreferredDisplayMode(mode); } catch (Exception e) { Log.e(TAG, "setUserPreferredDisplayMode() failed", e); @@ -194,16 +189,19 @@ public class ScreenResolutionFragment extends RadioButtonPickerFragment { /** Get the key corresponding to the resolution. */ @VisibleForTesting String getKeyForResolution(int width) { - return width == FHD_WIDTH - ? mScreenResolutionOptions[FHD_INDEX] - : width == QHD_WIDTH ? mScreenResolutionOptions[QHD_INDEX] : null; + return width == mHighWidth + ? mScreenResolutionOptions[ScreenResolutionController.HIGHRESOLUTION_IDX] + : width == mFullWidth + ? mScreenResolutionOptions[ScreenResolutionController.FULLRESOLUTION_IDX] + : null; } /** Get the width corresponding to the resolution key. */ int getWidthForResoluitonKey(String key) { - return mScreenResolutionOptions[FHD_INDEX].equals(key) - ? FHD_WIDTH - : mScreenResolutionOptions[QHD_INDEX].equals(key) ? QHD_WIDTH : -1; + return mScreenResolutionOptions[ScreenResolutionController.HIGHRESOLUTION_IDX].equals(key) + ? mHighWidth + : mScreenResolutionOptions[ScreenResolutionController.FULLRESOLUTION_IDX].equals( + key) ? mFullWidth : -1; } @Override @@ -248,9 +246,11 @@ public class ScreenResolutionFragment extends RadioButtonPickerFragment { private void updateIllustrationImage(IllustrationPreference preference) { 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); - } else if (TextUtils.equals(mScreenResolutionOptions[QHD_INDEX], key)) { + } else if (TextUtils.equals( + mScreenResolutionOptions[ScreenResolutionController.FULLRESOLUTION_IDX], key)) { preference.setLottieAnimationResId(R.drawable.screen_resolution_1440p); } } @@ -407,6 +407,8 @@ public class ScreenResolutionFragment extends RadioButtonPickerFragment { return false; } + Log.i(TAG, + "resolution changed from " + mPreviousWidth.get() + " to " + getCurrentWidth()); return true; } }