Refactor CaptionAppearanceFragment to improve maintainability (2/n)

Root cause: There is a bunch of different logic of preferences in CaptionAppearanceFragment. It’s hard to implement new features and hard to maintain and hard to be testable.
Solution: Move out font size and type face preference logic of CaptionAppearanceFragment into controllers to reduce the complexity of the relationship between preference and fragment.

Bug: 197695932
Test: make RunSettingsRoboTests ROBOTEST_FILTER=com.android.settings.accessibility
Change-Id: Ia52cd272495d49a772c981f51e190ff7d29ee14f
This commit is contained in:
menghanli
2022-07-05 17:54:18 +08:00
parent 591e44bb99
commit 3a591f9a34
9 changed files with 431 additions and 69 deletions

View File

@@ -16,8 +16,6 @@
package com.android.settings.accessibility;
import static com.android.settings.accessibility.AccessibilityUtil.State.ON;
import android.app.settings.SettingsEnums;
import android.content.ContentResolver;
import android.content.Context;
@@ -27,9 +25,6 @@ import android.os.Bundle;
import android.provider.Settings;
import android.view.accessibility.CaptioningManager;
import androidx.preference.ListPreference;
import androidx.preference.Preference;
import androidx.preference.Preference.OnPreferenceChangeListener;
import androidx.preference.PreferenceCategory;
import com.android.settings.R;
@@ -38,13 +33,10 @@ import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settingslib.search.SearchIndexable;
import java.util.ArrayList;
import java.util.List;
/** Settings fragment containing font style of captioning properties. */
@SearchIndexable(forTarget = SearchIndexable.ALL & ~SearchIndexable.ARC)
public class CaptionAppearanceFragment extends DashboardFragment
implements OnPreferenceChangeListener, OnValueChangedListener {
implements OnValueChangedListener {
private static final String TAG = "CaptionAppearanceFragment";
private static final String PREF_BACKGROUND_COLOR = "captioning_background_color";
@@ -55,19 +47,16 @@ public class CaptionAppearanceFragment extends DashboardFragment
private static final String PREF_WINDOW_OPACITY = "captioning_window_opacity";
private static final String PREF_EDGE_COLOR = "captioning_edge_color";
private static final String PREF_EDGE_TYPE = "captioning_edge_type";
private static final String PREF_FONT_SIZE = "captioning_font_size";
private static final String PREF_TYPEFACE = "captioning_typeface";
private static final String PREF_PRESET = "captioning_preset";
private static final String PREF_CUSTOM = "custom";
private CaptioningManager mCaptioningManager;
private CaptionHelper mCaptionHelper;
// Standard options.
private ListPreference mFontSize;
private PresetPreference mPreset;
// Custom options.
private ListPreference mTypeface;
private ColorPreference mForegroundColor;
private ColorPreference mForegroundOpacity;
private EdgeTypePreference mEdgeType;
@@ -80,8 +69,6 @@ public class CaptionAppearanceFragment extends DashboardFragment
private boolean mShowingCustom;
private final List<Preference> mPreferenceList = new ArrayList<>();
@Override
public int getMetricsCategory() {
return SettingsEnums.ACCESSIBILITY_CAPTION_APPEARANCE;
@@ -92,6 +79,7 @@ public class CaptionAppearanceFragment extends DashboardFragment
super.onCreatePreferences(savedInstanceState, rootKey);
mCaptioningManager = (CaptioningManager) getSystemService(Context.CAPTIONING_SERVICE);
mCaptionHelper = new CaptionHelper(getContext());
initializeAllPreferences();
updateAllPreferences();
@@ -118,12 +106,6 @@ public class CaptionAppearanceFragment extends DashboardFragment
mPreset.setValues(presetValues);
mPreset.setTitles(presetTitles);
mFontSize = (ListPreference) findPreference(PREF_FONT_SIZE);
// Initialize the preference list
mPreferenceList.add(mFontSize);
mPreferenceList.add(mPreset);
mCustom = (PreferenceCategory) findPreference(PREF_CUSTOM);
mShowingCustom = true;
@@ -168,7 +150,6 @@ public class CaptionAppearanceFragment extends DashboardFragment
mWindowOpacity.setValues(opacityValues);
mEdgeType = (EdgeTypePreference) mCustom.findPreference(PREF_EDGE_TYPE);
mTypeface = (ListPreference) mCustom.findPreference(PREF_TYPEFACE);
}
private void installUpdateListeners() {
@@ -181,18 +162,12 @@ public class CaptionAppearanceFragment extends DashboardFragment
mWindowColor.setOnValueChangedListener(this);
mWindowOpacity.setOnValueChangedListener(this);
mEdgeType.setOnValueChangedListener(this);
mTypeface.setOnPreferenceChangeListener(this);
mFontSize.setOnPreferenceChangeListener(this);
}
private void updateAllPreferences() {
final int preset = mCaptioningManager.getRawUserStyle();
mPreset.setValue(preset);
final float fontSize = mCaptioningManager.getFontScale();
mFontSize.setValue(Float.toString(fontSize));
final ContentResolver cr = getContentResolver();
final CaptioningManager.CaptionStyle attrs = CaptioningManager.CaptionStyle.getCustomStyle(
cr);
@@ -210,9 +185,6 @@ public class CaptionAppearanceFragment extends DashboardFragment
final int windowColor = attrs.hasWindowColor() ? attrs.windowColor
: CaptioningManager.CaptionStyle.COLOR_UNSPECIFIED;
parseColorOpacity(mWindowColor, mWindowOpacity, windowColor);
final String rawTypeface = attrs.mRawTypeface;
mTypeface.setValue(rawTypeface == null ? "" : rawTypeface);
}
/**
@@ -297,32 +269,7 @@ public class CaptionAppearanceFragment extends DashboardFragment
} else if (mEdgeType == preference) {
Settings.Secure.putInt(cr, Settings.Secure.ACCESSIBILITY_CAPTIONING_EDGE_TYPE, value);
}
enableCaptioningManager();
}
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
final ContentResolver cr = getActivity().getContentResolver();
if (mTypeface == preference) {
Settings.Secure.putString(
cr, Settings.Secure.ACCESSIBILITY_CAPTIONING_TYPEFACE, (String) value);
enableCaptioningManager();
} else if (mFontSize == preference) {
Settings.Secure.putFloat(
cr, Settings.Secure.ACCESSIBILITY_CAPTIONING_FONT_SCALE,
Float.parseFloat((String) value));
enableCaptioningManager();
}
return true;
}
private void enableCaptioningManager() {
if (mCaptioningManager.isEnabled()) {
return;
}
Settings.Secure.putInt(getContentResolver(),
Settings.Secure.ACCESSIBILITY_CAPTIONING_ENABLED, ON);
mCaptionHelper.setEnabled(true);
}
@Override

View File

@@ -0,0 +1,68 @@
/*
* Copyright (C) 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.accessibility;
import android.content.ContentResolver;
import android.content.Context;
import android.provider.Settings;
import android.view.accessibility.CaptioningManager;
import androidx.preference.ListPreference;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.core.BasePreferenceController;
/** Preference controller for caption font size. */
public class CaptionFontSizeController extends BasePreferenceController
implements Preference.OnPreferenceChangeListener {
private final CaptioningManager mCaptioningManager;
private final CaptionHelper mCaptionHelper;
private ListPreference mPreference;
public CaptionFontSizeController(Context context, String preferenceKey) {
super(context, preferenceKey);
mCaptioningManager = context.getSystemService(CaptioningManager.class);
mCaptionHelper = new CaptionHelper(context);
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mPreference = screen.findPreference(getPreferenceKey());
final float fontSize = mCaptioningManager.getFontScale();
mPreference.setValue(Float.toString(fontSize));
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
final ContentResolver cr = mContext.getContentResolver();
Settings.Secure.putFloat(
cr, Settings.Secure.ACCESSIBILITY_CAPTIONING_FONT_SCALE,
Float.parseFloat((String) newValue));
mPreference.setValue((String) newValue);
mCaptionHelper.setEnabled(true);
return false;
}
}

View File

@@ -16,7 +16,11 @@
package com.android.settings.accessibility;
import static com.android.settings.accessibility.AccessibilityUtil.State.OFF;
import static com.android.settings.accessibility.AccessibilityUtil.State.ON;
import android.content.Context;
import android.provider.Settings;
import android.view.View;
import android.view.accessibility.CaptioningManager;
@@ -45,6 +49,29 @@ public class CaptionHelper {
mCaptioningManager = context.getSystemService(CaptioningManager.class);
}
/**
* Sets the user's preferred captioning enabled state.
*
* @param enabled Whether to enable or disable captioning manager.
*/
public void setEnabled(boolean enabled) {
if (isEnabled() == enabled) {
return;
}
Settings.Secure.putInt(mContext.getContentResolver(),
Settings.Secure.ACCESSIBILITY_CAPTIONING_ENABLED, enabled ? ON : OFF);
}
/**
* Gets if the captioning manager is enabled.
*
* @return True if the captioning manager is enabled, false otherwise.
*/
public boolean isEnabled() {
return mCaptioningManager.isEnabled();
}
/**
* Updates font style of captioning properties for preview screen.
*

View File

@@ -16,12 +16,7 @@
package com.android.settings.accessibility;
import static com.android.settings.accessibility.AccessibilityUtil.State.OFF;
import static com.android.settings.accessibility.AccessibilityUtil.State.ON;
import android.content.Context;
import android.provider.Settings;
import android.view.accessibility.CaptioningManager;
import android.widget.Switch;
import androidx.preference.PreferenceScreen;
@@ -35,11 +30,11 @@ import com.android.settingslib.widget.OnMainSwitchChangeListener;
public class CaptionTogglePreferenceController extends TogglePreferenceController
implements OnMainSwitchChangeListener {
private final CaptioningManager mCaptioningManager;
private final CaptionHelper mCaptionHelper;
public CaptionTogglePreferenceController(Context context, String preferenceKey) {
super(context, preferenceKey);
mCaptioningManager = context.getSystemService(CaptioningManager.class);
mCaptionHelper = new CaptionHelper(context);
}
@Override
@@ -49,13 +44,12 @@ public class CaptionTogglePreferenceController extends TogglePreferenceControlle
@Override
public boolean isChecked() {
return mCaptioningManager.isEnabled();
return mCaptionHelper.isEnabled();
}
@Override
public boolean setChecked(boolean isChecked) {
Settings.Secure.putInt(mContext.getContentResolver(),
Settings.Secure.ACCESSIBILITY_CAPTIONING_ENABLED, isChecked ? ON : OFF);
mCaptionHelper.setEnabled(isChecked);
return true;
}

View File

@@ -0,0 +1,67 @@
/*
* Copyright (C) 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.accessibility;
import android.content.ContentResolver;
import android.content.Context;
import android.provider.Settings;
import android.view.accessibility.CaptioningManager.CaptionStyle;
import androidx.preference.ListPreference;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.core.BasePreferenceController;
/** Preference controller for caption type face. */
public class CaptionTypefaceController extends BasePreferenceController
implements Preference.OnPreferenceChangeListener {
private final CaptionHelper mCaptionHelper;
private ListPreference mPreference;
public CaptionTypefaceController(Context context, String preferenceKey) {
super(context, preferenceKey);
mCaptionHelper = new CaptionHelper(context);
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mPreference = screen.findPreference(getPreferenceKey());
final ContentResolver cr = mContext.getContentResolver();
final CaptionStyle attrs = CaptionStyle.getCustomStyle(cr);
final String rawTypeface = attrs.mRawTypeface;
mPreference.setValue(rawTypeface == null ? "" : rawTypeface);
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
final ContentResolver cr = mContext.getContentResolver();
Settings.Secure.putString(
cr, Settings.Secure.ACCESSIBILITY_CAPTIONING_TYPEFACE, (String) newValue);
mPreference.setValue((String) newValue);
mCaptionHelper.setEnabled(true);
return false;
}
}