Merge "Screen Resolution: add space for 4-digit values" into main
This commit is contained in:
committed by
Android (Google) Code Review
commit
accbe2362a
@@ -28,6 +28,7 @@ import android.text.TextUtils;
|
|||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.view.Display;
|
import android.view.Display;
|
||||||
|
|
||||||
|
import androidx.annotation.NonNull;
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
import androidx.annotation.VisibleForTesting;
|
import androidx.annotation.VisibleForTesting;
|
||||||
import androidx.preference.PreferenceScreen;
|
import androidx.preference.PreferenceScreen;
|
||||||
@@ -52,11 +53,15 @@ import java.util.concurrent.atomic.AtomicInteger;
|
|||||||
/** Preference fragment used for switch screen resolution */
|
/** Preference fragment used for switch screen resolution */
|
||||||
@SearchIndexable
|
@SearchIndexable
|
||||||
public class ScreenResolutionFragment extends RadioButtonPickerFragment {
|
public class ScreenResolutionFragment extends RadioButtonPickerFragment {
|
||||||
|
public static final String NON_BREAKING_SPACE = "\u00A0";
|
||||||
|
|
||||||
private static final String TAG = "ScreenResolution";
|
private static final String TAG = "ScreenResolution";
|
||||||
|
|
||||||
private Resources mResources;
|
private Resources mResources;
|
||||||
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 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 Display mDefaultDisplay;
|
||||||
private String[] mScreenResolutionOptions;
|
private String[] mScreenResolutionOptions;
|
||||||
private Set<Point> mResolutions;
|
private Set<Point> mResolutions;
|
||||||
@@ -94,12 +99,45 @@ public class ScreenResolutionFragment extends RadioButtonPickerFragment {
|
|||||||
|
|
||||||
|
|
||||||
private SpannableString getResolutionSpannable(int width, int height) {
|
private SpannableString getResolutionSpannable(int width, int height) {
|
||||||
String resolutionString = width + " x " + height;
|
String resolutionString = getResolutionString(width, height);
|
||||||
String accessibleText = mResources.getString(
|
String accessibleText = mResources.getString(
|
||||||
R.string.screen_resolution_delimiter_a11y, width, height);
|
R.string.screen_resolution_delimiter_a11y, width, height);
|
||||||
return Utils.createAccessibleSequence(resolutionString, accessibleText);
|
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
|
@Override
|
||||||
protected int getPreferenceScreenResId() {
|
protected int getPreferenceScreenResId() {
|
||||||
return R.xml.screen_resolution_settings;
|
return R.xml.screen_resolution_settings;
|
||||||
|
@@ -110,4 +110,28 @@ public class ScreenResolutionFragmentTest {
|
|||||||
|
|
||||||
assertThat(preference.getSummary().toString().contentEquals(summary)).isTrue();
|
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");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user