Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 3a591f9a authored by menghanli's avatar menghanli
Browse files

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
parent 591e44bb
Loading
Loading
Loading
Loading
+4 −2
Original line number Diff line number Diff line
@@ -33,7 +33,8 @@
        android:entryValues="@array/captioning_font_size_selector_values"
        android:key="captioning_font_size"
        android:summary="%s"
        android:title="@string/captioning_text_size"/>
        android:title="@string/captioning_text_size"
        settings:controller="com.android.settings.accessibility.CaptionFontSizeController"/>

    <com.android.settings.accessibility.PresetPreference
        android:key="captioning_preset"
@@ -48,7 +49,8 @@
            android:entryValues="@array/captioning_typeface_selector_values"
            android:key="captioning_typeface"
            android:summary="%s"
            android:title="@string/captioning_typeface"/>
            android:title="@string/captioning_typeface"
            settings:controller="com.android.settings.accessibility.CaptionTypefaceController"/>

        <com.android.settings.accessibility.ColorPreference
            android:key="captioning_foreground_color"
+4 −57
Original line number Diff line number Diff line
@@ -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
+68 −0
Original line number Diff line number Diff line
/*
 * 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;
    }
}
+27 −0
Original line number Diff line number Diff line
@@ -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.
     *
+4 −10
Original line number Diff line number Diff line
@@ -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;
    }

Loading