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
android:key="captioning_locale"
android:summary="%s"
android:title="@string/captioning_locale"
settings:controller="com.android.settings.accessibility.CaptionLocalePreferenceController"/>

View File

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

View File

@@ -33,7 +33,6 @@ public class CaptionFontSizeController extends BasePreferenceController
private final CaptioningManager mCaptioningManager;
private final CaptionHelper mCaptionHelper;
private ListPreference mPreference;
public CaptionFontSizeController(Context context, String preferenceKey) {
super(context, preferenceKey);
@@ -49,19 +48,19 @@ public class CaptionFontSizeController extends BasePreferenceController
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mPreference = screen.findPreference(getPreferenceKey());
final ListPreference listPreference = screen.findPreference(getPreferenceKey());
final float fontSize = mCaptioningManager.getFontScale();
mPreference.setValue(Float.toString(fontSize));
listPreference.setValue(Float.toString(fontSize));
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
final ListPreference listPreference = (ListPreference) preference;
final ContentResolver cr = mContext.getContentResolver();
Settings.Secure.putFloat(
cr, Settings.Secure.ACCESSIBILITY_CAPTIONING_FONT_SCALE,
Float.parseFloat((String) newValue));
mPreference.setValue((String) newValue);
listPreference.setValue((String) newValue);
mCaptionHelper.setEnabled(true);
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) {
Settings.Secure.putInt(mContentResolver,
Settings.Secure.ACCESSIBILITY_CAPTIONING_PRESET, type);
}
/** Returns the caption raw user style.*/
/** Returns the captioning raw preset number.*/
public int 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 {
private final CaptioningManager mCaptioningManager;
private LocalePreference mPreference;
public CaptionLocalePreferenceController(Context context, String preferenceKey) {
super(context, preferenceKey);
@@ -45,22 +44,17 @@ public class CaptionLocalePreferenceController extends BasePreferenceController
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mPreference = screen.findPreference(getPreferenceKey());
final LocalePreference localePreference = screen.findPreference(getPreferenceKey());
final String rawLocale = mCaptioningManager.getRawLocale();
mPreference.setValue(rawLocale == null ? "" : rawLocale);
}
@Override
public CharSequence getSummary() {
return mPreference.getEntry();
localePreference.setValue(rawLocale == null ? "" : rawLocale);
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
final LocalePreference localePreference = (LocalePreference) preference;
Settings.Secure.putString(mContext.getContentResolver(),
Settings.Secure.ACCESSIBILITY_CAPTIONING_LOCALE, (String) newValue);
mPreference.setValue((String) newValue);
mPreference.setSummary(mPreference.getEntry());
localePreference.setValue((String) newValue);
return true;
}
}

View File

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

View File

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

View File

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

View File

@@ -64,21 +64,21 @@ public class CaptionLocalePreferenceControllerTest {
}
@Test
public void getSummary_byDefault_shouldReturnDefault() {
public void displayPreference_byDefault_shouldReturnDefault() {
mController.displayPreference(mScreen);
assertThat(mController.getSummary().toString()).isEqualTo(
assertThat(mPreference.getEntry().toString()).isEqualTo(
mContext.getResources().getString(R.string.locale_default));
}
@Test
public void getSummary_byArabicLocale_shouldReturnArabic() {
public void displayPreference_byArabicLocale_shouldReturnArabic() {
Settings.Secure.putString(mContext.getContentResolver(),
Settings.Secure.ACCESSIBILITY_CAPTIONING_LOCALE, "af_ZA");
mController.displayPreference(mScreen);
assertThat(mController.getSummary().toString()).isEqualTo("Afrikaans");
assertThat(mPreference.getEntry().toString()).isEqualTo("Afrikaans");
}
@Test
@@ -87,6 +87,6 @@ public class CaptionLocalePreferenceControllerTest {
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
public void onPause_unregisterContentObserver() {
public void onStop_unregisterContentObserver() {
mController.onStop();
verify(mContentResolver).unregisterContentObserver(mController.mSettingsContentObserver);