Minor change for CaptionAppearanceFragment

1. Move unnecessary public variables into local variables
2. Use %s to update locale preference summary
3. Correct the test case name of CaptionPreviewPreferenceControllerTest

Bug: 197695932
Test: make RunSettingsRoboTests ROBOTEST_FILTER=com.android.settings.accessibility
Change-Id: I58d4d590b9b6d46c27d389217f846a780d755891
This commit is contained in:
menghanli
2022-07-29 08:26:44 +08:00
parent 6544dee983
commit b9194c4eb8
10 changed files with 54 additions and 56 deletions

View File

@@ -22,6 +22,7 @@
<com.android.settings.accessibility.LocalePreference <com.android.settings.accessibility.LocalePreference
android:key="captioning_locale" android:key="captioning_locale"
android:summary="%s"
android:title="@string/captioning_locale" android:title="@string/captioning_locale"
settings:controller="com.android.settings.accessibility.CaptionLocalePreferenceController"/> settings:controller="com.android.settings.accessibility.CaptionLocalePreferenceController"/>

View File

@@ -42,7 +42,6 @@ public class CaptionCustomController extends BasePreferenceController
private Preference mCustom; private Preference mCustom;
private final CaptionHelper mCaptionHelper; private final CaptionHelper mCaptionHelper;
private final ContentResolver mContentResolver; private final ContentResolver mContentResolver;
private final Handler mHandler = new Handler(Looper.getMainLooper());
@VisibleForTesting @VisibleForTesting
AccessibilitySettingsContentObserver mSettingsContentObserver; AccessibilitySettingsContentObserver mSettingsContentObserver;
@VisibleForTesting @VisibleForTesting
@@ -54,7 +53,8 @@ public class CaptionCustomController extends BasePreferenceController
super(context, preferenceKey); super(context, preferenceKey);
mCaptionHelper = new CaptionHelper(context); mCaptionHelper = new CaptionHelper(context);
mContentResolver = context.getContentResolver(); mContentResolver = context.getContentResolver();
mSettingsContentObserver = new AccessibilitySettingsContentObserver(mHandler); mSettingsContentObserver = new AccessibilitySettingsContentObserver(
new Handler(Looper.getMainLooper()));
mSettingsContentObserver.registerKeysToObserverCallback(CAPTIONING_FEATURE_KEYS, mSettingsContentObserver.registerKeysToObserverCallback(CAPTIONING_FEATURE_KEYS,
key -> refreshShowingCustom()); key -> refreshShowingCustom());
} }

View File

@@ -33,7 +33,6 @@ public class CaptionFontSizeController extends BasePreferenceController
private final CaptioningManager mCaptioningManager; private final CaptioningManager mCaptioningManager;
private final CaptionHelper mCaptionHelper; private final CaptionHelper mCaptionHelper;
private ListPreference mPreference;
public CaptionFontSizeController(Context context, String preferenceKey) { public CaptionFontSizeController(Context context, String preferenceKey) {
super(context, preferenceKey); super(context, preferenceKey);
@@ -49,19 +48,19 @@ public class CaptionFontSizeController extends BasePreferenceController
@Override @Override
public void displayPreference(PreferenceScreen screen) { public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen); super.displayPreference(screen);
mPreference = screen.findPreference(getPreferenceKey()); final ListPreference listPreference = screen.findPreference(getPreferenceKey());
final float fontSize = mCaptioningManager.getFontScale(); final float fontSize = mCaptioningManager.getFontScale();
mPreference.setValue(Float.toString(fontSize)); listPreference.setValue(Float.toString(fontSize));
} }
@Override @Override
public boolean onPreferenceChange(Preference preference, Object newValue) { public boolean onPreferenceChange(Preference preference, Object newValue) {
final ListPreference listPreference = (ListPreference) preference;
final ContentResolver cr = mContext.getContentResolver(); final ContentResolver cr = mContext.getContentResolver();
Settings.Secure.putFloat( Settings.Secure.putFloat(
cr, Settings.Secure.ACCESSIBILITY_CAPTIONING_FONT_SCALE, cr, Settings.Secure.ACCESSIBILITY_CAPTIONING_FONT_SCALE,
Float.parseFloat((String) newValue)); Float.parseFloat((String) newValue));
mPreference.setValue((String) newValue); listPreference.setValue((String) newValue);
mCaptionHelper.setEnabled(true); mCaptionHelper.setEnabled(true);
return false; return false;
} }

View File

@@ -188,17 +188,27 @@ public class CaptionHelper {
} }
/** /**
* Sets the caption raw user style. * Sets the captioning raw user style.
* *
* @param type The caption raw user style * @param type The captioning raw user style
*/ */
public void setRawUserStyle(int type) { public void setRawUserStyle(int type) {
Settings.Secure.putInt(mContentResolver, Settings.Secure.putInt(mContentResolver,
Settings.Secure.ACCESSIBILITY_CAPTIONING_PRESET, type); Settings.Secure.ACCESSIBILITY_CAPTIONING_PRESET, type);
} }
/** Returns the caption raw user style.*/ /** Returns the captioning raw preset number.*/
public int getRawUserStyle() { public int getRawUserStyle() {
return mCaptioningManager.getRawUserStyle(); return mCaptioningManager.getRawUserStyle();
} }
/** Returns the captioning visual properties.*/
public CaptionStyle getUserStyle() {
return mCaptioningManager.getUserStyle();
}
/** Returns the captioning locale language.*/
public Locale getLocale() {
return mCaptioningManager.getLocale();
}
} }

View File

@@ -30,7 +30,6 @@ public class CaptionLocalePreferenceController extends BasePreferenceController
implements Preference.OnPreferenceChangeListener { implements Preference.OnPreferenceChangeListener {
private final CaptioningManager mCaptioningManager; private final CaptioningManager mCaptioningManager;
private LocalePreference mPreference;
public CaptionLocalePreferenceController(Context context, String preferenceKey) { public CaptionLocalePreferenceController(Context context, String preferenceKey) {
super(context, preferenceKey); super(context, preferenceKey);
@@ -45,22 +44,17 @@ public class CaptionLocalePreferenceController extends BasePreferenceController
@Override @Override
public void displayPreference(PreferenceScreen screen) { public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen); super.displayPreference(screen);
mPreference = screen.findPreference(getPreferenceKey()); final LocalePreference localePreference = screen.findPreference(getPreferenceKey());
final String rawLocale = mCaptioningManager.getRawLocale(); final String rawLocale = mCaptioningManager.getRawLocale();
mPreference.setValue(rawLocale == null ? "" : rawLocale); localePreference.setValue(rawLocale == null ? "" : rawLocale);
}
@Override
public CharSequence getSummary() {
return mPreference.getEntry();
} }
@Override @Override
public boolean onPreferenceChange(Preference preference, Object newValue) { public boolean onPreferenceChange(Preference preference, Object newValue) {
final LocalePreference localePreference = (LocalePreference) preference;
Settings.Secure.putString(mContext.getContentResolver(), Settings.Secure.putString(mContext.getContentResolver(),
Settings.Secure.ACCESSIBILITY_CAPTIONING_LOCALE, (String) newValue); Settings.Secure.ACCESSIBILITY_CAPTIONING_LOCALE, (String) newValue);
mPreference.setValue((String) newValue); localePreference.setValue((String) newValue);
mPreference.setSummary(mPreference.getEntry());
return true; return true;
} }
} }

View File

@@ -21,7 +21,7 @@ import android.os.Handler;
import android.os.Looper; import android.os.Looper;
import android.provider.Settings; import android.provider.Settings;
import android.view.View; import android.view.View;
import android.view.accessibility.CaptioningManager; import android.view.accessibility.CaptioningManager.CaptionStyle;
import androidx.annotation.VisibleForTesting; import androidx.annotation.VisibleForTesting;
import androidx.preference.PreferenceScreen; import androidx.preference.PreferenceScreen;
@@ -57,16 +57,11 @@ public class CaptionPreviewPreferenceController extends BasePreferenceController
private final Handler mHandler = new Handler(Looper.getMainLooper()); private final Handler mHandler = new Handler(Looper.getMainLooper());
@VisibleForTesting @VisibleForTesting
AccessibilitySettingsContentObserver mSettingsContentObserver; AccessibilitySettingsContentObserver mSettingsContentObserver;
private CaptioningManager mCaptioningManager;
private CaptionHelper mCaptionHelper; private CaptionHelper mCaptionHelper;
private LayoutPreference mPreference; private LayoutPreference mPreference;
private SubtitleView mPreviewText;
private View mPreviewWindow;
private View mPreviewViewport;
public CaptionPreviewPreferenceController(Context context, String preferenceKey) { public CaptionPreviewPreferenceController(Context context, String preferenceKey) {
super(context, preferenceKey); super(context, preferenceKey);
mCaptioningManager = context.getSystemService(CaptioningManager.class);
mCaptionHelper = new CaptionHelper(context); mCaptionHelper = new CaptionHelper(context);
mSettingsContentObserver = new AccessibilitySettingsContentObserver(mHandler); mSettingsContentObserver = new AccessibilitySettingsContentObserver(mHandler);
mSettingsContentObserver.registerKeysToObserverCallback(CAPTIONING_FEATURE_KEYS, mSettingsContentObserver.registerKeysToObserverCallback(CAPTIONING_FEATURE_KEYS,
@@ -92,16 +87,14 @@ public class CaptionPreviewPreferenceController extends BasePreferenceController
public void displayPreference(PreferenceScreen screen) { public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen); super.displayPreference(screen);
mPreference = screen.findPreference(getPreferenceKey()); mPreference = screen.findPreference(getPreferenceKey());
mPreviewText = mPreference.findViewById(R.id.preview_text); final View previewViewport = mPreference.findViewById(R.id.preview_viewport);
mPreviewWindow = mPreference.findViewById(R.id.preview_window); previewViewport.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
mPreviewViewport = mPreference.findViewById(R.id.preview_viewport);
mPreviewViewport.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override @Override
public void onLayoutChange(View v, int left, int top, int right, public void onLayoutChange(View v, int left, int top, int right,
int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) { int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
if ((oldRight - oldLeft) != (right - left)) { if ((oldRight - oldLeft) != (right - left)) {
// Remove the listener once the callback is triggered. // Remove the listener once the callback is triggered.
mPreviewViewport.removeOnLayoutChangeListener(this); previewViewport.removeOnLayoutChangeListener(this);
mHandler.post(() -> refreshPreviewText()); mHandler.post(() -> refreshPreviewText());
} }
} }
@@ -109,26 +102,28 @@ public class CaptionPreviewPreferenceController extends BasePreferenceController
} }
private void refreshPreviewText() { private void refreshPreviewText() {
if (mPreviewText != null) { final SubtitleView previewText = mPreference.findViewById(R.id.preview_text);
final int styleId = mCaptioningManager.getRawUserStyle(); if (previewText != null) {
mCaptionHelper.applyCaptionProperties(mPreviewText, mPreviewViewport, styleId); final View previewViewport = mPreference.findViewById(R.id.preview_viewport);
final int styleId = mCaptionHelper.getRawUserStyle();
mCaptionHelper.applyCaptionProperties(previewText, previewViewport, styleId);
final Locale locale = mCaptioningManager.getLocale(); final Locale locale = mCaptionHelper.getLocale();
if (locale != null) { if (locale != null) {
final CharSequence localizedText = AccessibilityUtils.getTextForLocale( final CharSequence localizedText = AccessibilityUtils.getTextForLocale(
mContext, locale, R.string.captioning_preview_text); mContext, locale, R.string.captioning_preview_text);
mPreviewText.setText(localizedText); previewText.setText(localizedText);
} else { } else {
mPreviewText.setText(R.string.captioning_preview_text); previewText.setText(R.string.captioning_preview_text);
} }
final CaptioningManager.CaptionStyle style = mCaptioningManager.getUserStyle(); final View previewWindow = mPreference.findViewById(R.id.preview_window);
final CaptionStyle style = mCaptionHelper.getUserStyle();
if (style.hasWindowColor()) { if (style.hasWindowColor()) {
mPreviewWindow.setBackgroundColor(style.windowColor); previewWindow.setBackgroundColor(style.windowColor);
} else { } else {
final CaptioningManager.CaptionStyle defStyle = final CaptionStyle defStyle = CaptionStyle.DEFAULT;
CaptioningManager.CaptionStyle.DEFAULT; previewWindow.setBackgroundColor(defStyle.windowColor);
mPreviewWindow.setBackgroundColor(defStyle.windowColor);
} }
} }
} }

View File

@@ -57,9 +57,9 @@ public class CaptionTogglePreferenceController extends TogglePreferenceControlle
public void displayPreference(PreferenceScreen screen) { public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen); super.displayPreference(screen);
SettingsMainSwitchPreference pref = screen.findPreference(getPreferenceKey()); SettingsMainSwitchPreference preference = screen.findPreference(getPreferenceKey());
pref.addOnSwitchChangeListener(this); preference.addOnSwitchChangeListener(this);
pref.setChecked(isChecked()); preference.setChecked(isChecked());
} }
@Override @Override

View File

@@ -32,7 +32,6 @@ public class CaptionTypefaceController extends BasePreferenceController
implements Preference.OnPreferenceChangeListener { implements Preference.OnPreferenceChangeListener {
private final CaptionHelper mCaptionHelper; private final CaptionHelper mCaptionHelper;
private ListPreference mPreference;
public CaptionTypefaceController(Context context, String preferenceKey) { public CaptionTypefaceController(Context context, String preferenceKey) {
super(context, preferenceKey); super(context, preferenceKey);
@@ -47,20 +46,20 @@ public class CaptionTypefaceController extends BasePreferenceController
@Override @Override
public void displayPreference(PreferenceScreen screen) { public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen); super.displayPreference(screen);
mPreference = screen.findPreference(getPreferenceKey()); final ListPreference listPreference = screen.findPreference(getPreferenceKey());
final ContentResolver cr = mContext.getContentResolver(); final ContentResolver cr = mContext.getContentResolver();
final CaptionStyle attrs = CaptionStyle.getCustomStyle(cr); final CaptionStyle attrs = CaptionStyle.getCustomStyle(cr);
final String rawTypeface = attrs.mRawTypeface; final String rawTypeface = attrs.mRawTypeface;
mPreference.setValue(rawTypeface == null ? "" : rawTypeface); listPreference.setValue(rawTypeface == null ? "" : rawTypeface);
} }
@Override @Override
public boolean onPreferenceChange(Preference preference, Object newValue) { public boolean onPreferenceChange(Preference preference, Object newValue) {
final ListPreference listPreference = (ListPreference) preference;
final ContentResolver cr = mContext.getContentResolver(); final ContentResolver cr = mContext.getContentResolver();
Settings.Secure.putString( Settings.Secure.putString(
cr, Settings.Secure.ACCESSIBILITY_CAPTIONING_TYPEFACE, (String) newValue); cr, Settings.Secure.ACCESSIBILITY_CAPTIONING_TYPEFACE, (String) newValue);
mPreference.setValue((String) newValue); listPreference.setValue((String) newValue);
mCaptionHelper.setEnabled(true); mCaptionHelper.setEnabled(true);
return false; return false;
} }

View File

@@ -64,21 +64,21 @@ public class CaptionLocalePreferenceControllerTest {
} }
@Test @Test
public void getSummary_byDefault_shouldReturnDefault() { public void displayPreference_byDefault_shouldReturnDefault() {
mController.displayPreference(mScreen); mController.displayPreference(mScreen);
assertThat(mController.getSummary().toString()).isEqualTo( assertThat(mPreference.getEntry().toString()).isEqualTo(
mContext.getResources().getString(R.string.locale_default)); mContext.getResources().getString(R.string.locale_default));
} }
@Test @Test
public void getSummary_byArabicLocale_shouldReturnArabic() { public void displayPreference_byArabicLocale_shouldReturnArabic() {
Settings.Secure.putString(mContext.getContentResolver(), Settings.Secure.putString(mContext.getContentResolver(),
Settings.Secure.ACCESSIBILITY_CAPTIONING_LOCALE, "af_ZA"); Settings.Secure.ACCESSIBILITY_CAPTIONING_LOCALE, "af_ZA");
mController.displayPreference(mScreen); mController.displayPreference(mScreen);
assertThat(mController.getSummary().toString()).isEqualTo("Afrikaans"); assertThat(mPreference.getEntry().toString()).isEqualTo("Afrikaans");
} }
@Test @Test
@@ -87,6 +87,6 @@ public class CaptionLocalePreferenceControllerTest {
mController.onPreferenceChange(mPreference, "af_ZA"); mController.onPreferenceChange(mPreference, "af_ZA");
assertThat(mPreference.getSummary().toString()).isEqualTo("Afrikaans"); assertThat(mPreference.getEntry().toString()).isEqualTo("Afrikaans");
} }
} }

View File

@@ -84,7 +84,7 @@ public class CaptionPreviewPreferenceControllerTest {
} }
@Test @Test
public void onPause_unregisterContentObserver() { public void onStop_unregisterContentObserver() {
mController.onStop(); mController.onStop();
verify(mContentResolver).unregisterContentObserver(mController.mSettingsContentObserver); verify(mContentResolver).unregisterContentObserver(mController.mSettingsContentObserver);