Screen Resolution: add space for 4-digit values

Add logic that composes screen resolution string with non-breaking
spaces. Now setting page will show "1 080 x 1 280" instead of "1080 x
1280"

Test: atest -c <SETTINGS_APP_PATH>/tests/unit/src/com/android/settings/display/ScreenResolutionFragmentTest.java
Test: verified on device
Flag: EXEMPT no API change
Bug: 385895191
Change-Id: I9a4bbd7c20e8c700dee6e997a36d4fda69f4d780
This commit is contained in:
Vadym Omelnytskyi
2025-03-13 11:29:13 +00:00
parent 67b21fb238
commit 14050c89f5
2 changed files with 63 additions and 1 deletions

View File

@@ -28,6 +28,7 @@ import android.text.TextUtils;
import android.util.Log;
import android.view.Display;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.preference.PreferenceScreen;
@@ -52,11 +53,15 @@ import java.util.concurrent.atomic.AtomicInteger;
/** Preference fragment used for switch screen resolution */
@SearchIndexable
public class ScreenResolutionFragment extends RadioButtonPickerFragment {
public static final String NON_BREAKING_SPACE = "\u00A0";
private static final String TAG = "ScreenResolution";
private Resources mResources;
private static final String SCREEN_RESOLUTION = "user_selected_resolution";
private static final String SCREEN_RESOLUTION_KEY = "screen_resolution";
private static final int RESOLUTION_SPACE_THRESHOLD = 1_000;
private static final int RESOLUTION_SPACE_THRESHOLD_DIGIT_AMOUNT = 3;
private Display mDefaultDisplay;
private String[] mScreenResolutionOptions;
private Set<Point> mResolutions;
@@ -94,12 +99,45 @@ public class ScreenResolutionFragment extends RadioButtonPickerFragment {
private SpannableString getResolutionSpannable(int width, int height) {
String resolutionString = width + " x " + height;
String resolutionString = getResolutionString(width, height);
String accessibleText = mResources.getString(
R.string.screen_resolution_delimiter_a11y, width, height);
return Utils.createAccessibleSequence(resolutionString, accessibleText);
}
/**
* Formats the given width and height into a resolution string, inserting non-breaking
* spaces as thousand separators if the values exceed a specified threshold.
*
* @param width The width value.
* @param height The height value.
* @return A formatted string representing the resolution (e.g., "1 080 x 1 280").
* Non-breaking spaces are used to ensure the numbers and 'x' stay together.
* If the width or height is less than RESOLUTION_SPACE_THRESHOLD, no spaces are added.
*/
@NonNull
@VisibleForTesting
static String getResolutionString(int width, int height) {
StringBuilder resolutionStrBldr = new StringBuilder(String.valueOf(width));
if (width >= RESOLUTION_SPACE_THRESHOLD) {
int insertWidthPosition =
resolutionStrBldr.length() - RESOLUTION_SPACE_THRESHOLD_DIGIT_AMOUNT;
resolutionStrBldr.insert(insertWidthPosition, NON_BREAKING_SPACE);
}
resolutionStrBldr.append(NON_BREAKING_SPACE);
resolutionStrBldr.append("x");
resolutionStrBldr.append(NON_BREAKING_SPACE);
resolutionStrBldr.append(String.valueOf(height));
if (height >= RESOLUTION_SPACE_THRESHOLD) {
int insertHeightPosition =
resolutionStrBldr.length() - RESOLUTION_SPACE_THRESHOLD_DIGIT_AMOUNT;
resolutionStrBldr.insert(insertHeightPosition, NON_BREAKING_SPACE);
}
return resolutionStrBldr.toString();
}
@Override
protected int getPreferenceScreenResId() {
return R.xml.screen_resolution_settings;

View File

@@ -110,4 +110,28 @@ public class ScreenResolutionFragmentTest {
assertThat(preference.getSummary().toString().contentEquals(summary)).isTrue();
}
@Test
public void testResolutionString_widthAndHeightAboveThreshold() {
String result = ScreenResolutionFragment.getResolutionString(1080, 1280);
assertThat(result).isEqualTo("1\u00A0080\u00A0x\u00A01\u00A0280");
}
@Test
public void testResolutionString_widthAboveThreshold_heightBelowThreshold() {
String result = ScreenResolutionFragment.getResolutionString(1080, 980);
assertThat(result).isEqualTo("1\u00A0080\u00A0x\u00A0980");
}
@Test
public void testResolutionString_widthBelowThreshold_heightBelowThreshold() {
String result = ScreenResolutionFragment.getResolutionString(980, 980);
assertThat(result).isEqualTo("980\u00A0x\u00A0980");
}
@Test
public void testResolutionString_widthBelowThreshold_heightAboveThreshold() {
String result = ScreenResolutionFragment.getResolutionString(980, 1080);
assertThat(result).isEqualTo("980\u00A0x\u00A01\u00A0080");
}
}