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

Commit 16fbfe4e authored by menghanli's avatar menghanli
Browse files

Refactor CaptionAppearanceFragment to improve maintainability (3/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 color and edge 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: I8ed80b445a0d376a7e9dda87feda82420a78a9da
parent 2675dbf5
Loading
Loading
Loading
Loading
+16 −9
Original line number Original line Diff line number Diff line
@@ -54,40 +54,47 @@


        <com.android.settings.accessibility.ColorPreference
        <com.android.settings.accessibility.ColorPreference
            android:key="captioning_foreground_color"
            android:key="captioning_foreground_color"
            android:title="@string/captioning_foreground_color"/>
            android:title="@string/captioning_foreground_color"
            settings:controller="com.android.settings.accessibility.CaptionForegroundColorController"/>


        <com.android.settings.accessibility.ColorPreference
        <com.android.settings.accessibility.ColorPreference
            android:dependency="captioning_foreground_color"
            android:dependency="captioning_foreground_color"
            android:key="captioning_foreground_opacity"
            android:key="captioning_foreground_opacity"
            android:title="@string/captioning_foreground_opacity"/>
            android:title="@string/captioning_foreground_opacity"
            settings:controller="com.android.settings.accessibility.CaptionForegroundOpacityController"/>


        <com.android.settings.accessibility.EdgeTypePreference
        <com.android.settings.accessibility.EdgeTypePreference
            android:key="captioning_edge_type"
            android:key="captioning_edge_type"
            android:title="@string/captioning_edge_type"/>
            android:title="@string/captioning_edge_type"
            settings:controller="com.android.settings.accessibility.CaptionEdgeTypeController"/>


        <com.android.settings.accessibility.ColorPreference
        <com.android.settings.accessibility.ColorPreference
            android:dependency="captioning_edge_type"
            android:dependency="captioning_edge_type"
            android:key="captioning_edge_color"
            android:key="captioning_edge_color"
            android:title="@string/captioning_edge_color"/>
            android:title="@string/captioning_edge_color"
            settings:controller="com.android.settings.accessibility.CaptionEdgeColorController"/>


        <com.android.settings.accessibility.ColorPreference
        <com.android.settings.accessibility.ColorPreference
            android:key="captioning_background_color"
            android:key="captioning_background_color"
            android:title="@string/captioning_background_color"/>
            android:title="@string/captioning_background_color"
            settings:controller="com.android.settings.accessibility.CaptionBackgroundColorController"/>


        <com.android.settings.accessibility.ColorPreference
        <com.android.settings.accessibility.ColorPreference
            android:dependency="captioning_background_color"
            android:dependency="captioning_background_color"
            android:key="captioning_background_opacity"
            android:key="captioning_background_opacity"
            android:title="@string/captioning_background_opacity"/>
            android:title="@string/captioning_background_opacity"
            settings:controller="com.android.settings.accessibility.CaptionBackgroundOpacityController"/>


        <com.android.settings.accessibility.ColorPreference
        <com.android.settings.accessibility.ColorPreference
            android:key="captioning_window_color"
            android:key="captioning_window_color"
            android:title="@string/captioning_window_color"/>
            android:title="@string/captioning_window_color"
            settings:controller="com.android.settings.accessibility.CaptionWindowColorController"/>


        <com.android.settings.accessibility.ColorPreference
        <com.android.settings.accessibility.ColorPreference
            android:dependency="captioning_window_color"
            android:dependency="captioning_window_color"
            android:key="captioning_window_opacity"
            android:key="captioning_window_opacity"
            android:title="@string/captioning_window_opacity"/>
            android:title="@string/captioning_window_opacity"

            settings:controller="com.android.settings.accessibility.CaptionWindowOpacityController"/>
    </PreferenceCategory>
    </PreferenceCategory>


    <com.android.settings.accessibility.AccessibilityFooterPreference
    <com.android.settings.accessibility.AccessibilityFooterPreference
+1 −149
Original line number Original line Diff line number Diff line
@@ -20,7 +20,6 @@ import android.app.settings.SettingsEnums;
import android.content.ContentResolver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Bundle;
import android.provider.Settings;
import android.provider.Settings;
import android.view.accessibility.CaptioningManager;
import android.view.accessibility.CaptioningManager;
@@ -39,14 +38,6 @@ public class CaptionAppearanceFragment extends DashboardFragment
        implements OnValueChangedListener {
        implements OnValueChangedListener {


    private static final String TAG = "CaptionAppearanceFragment";
    private static final String TAG = "CaptionAppearanceFragment";
    private static final String PREF_BACKGROUND_COLOR = "captioning_background_color";
    private static final String PREF_BACKGROUND_OPACITY = "captioning_background_opacity";
    private static final String PREF_FOREGROUND_COLOR = "captioning_foreground_color";
    private static final String PREF_FOREGROUND_OPACITY = "captioning_foreground_opacity";
    private static final String PREF_WINDOW_COLOR = "captioning_window_color";
    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_PRESET = "captioning_preset";
    private static final String PREF_PRESET = "captioning_preset";
    private static final String PREF_CUSTOM = "custom";
    private static final String PREF_CUSTOM = "custom";


@@ -57,14 +48,6 @@ public class CaptionAppearanceFragment extends DashboardFragment
    private PresetPreference mPreset;
    private PresetPreference mPreset;


    // Custom options.
    // Custom options.
    private ColorPreference mForegroundColor;
    private ColorPreference mForegroundOpacity;
    private EdgeTypePreference mEdgeType;
    private ColorPreference mEdgeColor;
    private ColorPreference mBackgroundColor;
    private ColorPreference mBackgroundOpacity;
    private ColorPreference mWindowColor;
    private ColorPreference mWindowOpacity;
    private PreferenceCategory mCustom;
    private PreferenceCategory mCustom;


    private boolean mShowingCustom;
    private boolean mShowingCustom;
@@ -108,130 +91,15 @@ public class CaptionAppearanceFragment extends DashboardFragment


        mCustom = (PreferenceCategory) findPreference(PREF_CUSTOM);
        mCustom = (PreferenceCategory) findPreference(PREF_CUSTOM);
        mShowingCustom = true;
        mShowingCustom = true;

        final int[] colorValues = res.getIntArray(R.array.captioning_color_selector_values);
        final String[] colorTitles = res.getStringArray(R.array.captioning_color_selector_titles);
        mForegroundColor = (ColorPreference) mCustom.findPreference(PREF_FOREGROUND_COLOR);
        mForegroundColor.setTitles(colorTitles);
        mForegroundColor.setValues(colorValues);

        final int[] opacityValues = res.getIntArray(R.array.captioning_opacity_selector_values);
        final String[] opacityTitles = res.getStringArray(
                R.array.captioning_opacity_selector_titles);
        mForegroundOpacity = (ColorPreference) mCustom.findPreference(PREF_FOREGROUND_OPACITY);
        mForegroundOpacity.setTitles(opacityTitles);
        mForegroundOpacity.setValues(opacityValues);

        mEdgeColor = (ColorPreference) mCustom.findPreference(PREF_EDGE_COLOR);
        mEdgeColor.setTitles(colorTitles);
        mEdgeColor.setValues(colorValues);

        // Add "none" as an additional option for backgrounds.
        final int[] bgColorValues = new int[colorValues.length + 1];
        final String[] bgColorTitles = new String[colorTitles.length + 1];
        System.arraycopy(colorValues, 0, bgColorValues, 1, colorValues.length);
        System.arraycopy(colorTitles, 0, bgColorTitles, 1, colorTitles.length);
        bgColorValues[0] = Color.TRANSPARENT;
        bgColorTitles[0] = getString(R.string.color_none);
        mBackgroundColor = (ColorPreference) mCustom.findPreference(PREF_BACKGROUND_COLOR);
        mBackgroundColor.setTitles(bgColorTitles);
        mBackgroundColor.setValues(bgColorValues);

        mBackgroundOpacity = (ColorPreference) mCustom.findPreference(PREF_BACKGROUND_OPACITY);
        mBackgroundOpacity.setTitles(opacityTitles);
        mBackgroundOpacity.setValues(opacityValues);

        mWindowColor = (ColorPreference) mCustom.findPreference(PREF_WINDOW_COLOR);
        mWindowColor.setTitles(bgColorTitles);
        mWindowColor.setValues(bgColorValues);

        mWindowOpacity = (ColorPreference) mCustom.findPreference(PREF_WINDOW_OPACITY);
        mWindowOpacity.setTitles(opacityTitles);
        mWindowOpacity.setValues(opacityValues);

        mEdgeType = (EdgeTypePreference) mCustom.findPreference(PREF_EDGE_TYPE);
    }
    }


    private void installUpdateListeners() {
    private void installUpdateListeners() {
        mPreset.setOnValueChangedListener(this);
        mPreset.setOnValueChangedListener(this);
        mForegroundColor.setOnValueChangedListener(this);
        mForegroundOpacity.setOnValueChangedListener(this);
        mEdgeColor.setOnValueChangedListener(this);
        mBackgroundColor.setOnValueChangedListener(this);
        mBackgroundOpacity.setOnValueChangedListener(this);
        mWindowColor.setOnValueChangedListener(this);
        mWindowOpacity.setOnValueChangedListener(this);
        mEdgeType.setOnValueChangedListener(this);
    }
    }


    private void updateAllPreferences() {
    private void updateAllPreferences() {
        final int preset = mCaptioningManager.getRawUserStyle();
        final int preset = mCaptioningManager.getRawUserStyle();
        mPreset.setValue(preset);
        mPreset.setValue(preset);

        final ContentResolver cr = getContentResolver();
        final CaptioningManager.CaptionStyle attrs = CaptioningManager.CaptionStyle.getCustomStyle(
                cr);
        mEdgeType.setValue(attrs.edgeType);
        mEdgeColor.setValue(attrs.edgeColor);

        final int foregroundColor = attrs.hasForegroundColor() ? attrs.foregroundColor
                : CaptioningManager.CaptionStyle.COLOR_UNSPECIFIED;
        parseColorOpacity(mForegroundColor, mForegroundOpacity, foregroundColor);

        final int backgroundColor = attrs.hasBackgroundColor() ? attrs.backgroundColor
                : CaptioningManager.CaptionStyle.COLOR_UNSPECIFIED;
        parseColorOpacity(mBackgroundColor, mBackgroundOpacity, backgroundColor);

        final int windowColor = attrs.hasWindowColor() ? attrs.windowColor
                : CaptioningManager.CaptionStyle.COLOR_UNSPECIFIED;
        parseColorOpacity(mWindowColor, mWindowOpacity, windowColor);
    }

    /**
     * Unpacks the specified color value and update the preferences.
     *
     * @param color   color preference
     * @param opacity opacity preference
     * @param value   packed value
     */
    private void parseColorOpacity(ColorPreference color, ColorPreference opacity, int value) {
        final int colorValue;
        final int opacityValue;
        if (!CaptioningManager.CaptionStyle.hasColor(value)) {
            // "Default" color with variable alpha.
            colorValue = CaptioningManager.CaptionStyle.COLOR_UNSPECIFIED;
            opacityValue = (value & 0xFF) << 24;
        } else if ((value >>> 24) == 0) {
            // "None" color with variable alpha.
            colorValue = Color.TRANSPARENT;
            opacityValue = (value & 0xFF) << 24;
        } else {
            // Normal color.
            colorValue = value | 0xFF000000;
            opacityValue = value & 0xFF000000;
        }

        // Opacity value is always white.
        opacity.setValue(opacityValue | 0xFFFFFF);
        color.setValue(colorValue);
    }

    private int mergeColorOpacity(ColorPreference color, ColorPreference opacity) {
        final int colorValue = color.getValue();
        final int opacityValue = opacity.getValue();
        final int value;
        // "Default" is 0x00FFFFFF or, for legacy support, 0x00000100.
        if (!CaptioningManager.CaptionStyle.hasColor(colorValue)) {
            // Encode "default" as 0x00FFFFaa.
            value = 0x00FFFF00 | Color.alpha(opacityValue);
        } else if (colorValue == Color.TRANSPARENT) {
            // Encode "none" as 0x000000aa.
            value = Color.alpha(opacityValue);
        } else {
            // Encode custom color normally.
            value = colorValue & 0x00FFFFFF | opacityValue & 0xFF000000;
        }
        return value;
    }
    }


    private void refreshShowingCustom() {
    private void refreshShowingCustom() {
@@ -249,25 +117,9 @@ public class CaptionAppearanceFragment extends DashboardFragment
    @Override
    @Override
    public void onValueChanged(ListDialogPreference preference, int value) {
    public void onValueChanged(ListDialogPreference preference, int value) {
        final ContentResolver cr = getActivity().getContentResolver();
        final ContentResolver cr = getActivity().getContentResolver();
        if (mForegroundColor == preference || mForegroundOpacity == preference) {
        if (mPreset == preference) {
            final int merged = mergeColorOpacity(mForegroundColor, mForegroundOpacity);
            Settings.Secure.putInt(
                    cr, Settings.Secure.ACCESSIBILITY_CAPTIONING_FOREGROUND_COLOR, merged);
        } else if (mBackgroundColor == preference || mBackgroundOpacity == preference) {
            final int merged = mergeColorOpacity(mBackgroundColor, mBackgroundOpacity);
            Settings.Secure.putInt(
                    cr, Settings.Secure.ACCESSIBILITY_CAPTIONING_BACKGROUND_COLOR, merged);
        } else if (mWindowColor == preference || mWindowOpacity == preference) {
            final int merged = mergeColorOpacity(mWindowColor, mWindowOpacity);
            Settings.Secure.putInt(
                    cr, Settings.Secure.ACCESSIBILITY_CAPTIONING_WINDOW_COLOR, merged);
        } else if (mEdgeColor == preference) {
            Settings.Secure.putInt(cr, Settings.Secure.ACCESSIBILITY_CAPTIONING_EDGE_COLOR, value);
        } else if (mPreset == preference) {
            Settings.Secure.putInt(cr, Settings.Secure.ACCESSIBILITY_CAPTIONING_PRESET, value);
            Settings.Secure.putInt(cr, Settings.Secure.ACCESSIBILITY_CAPTIONING_PRESET, value);
            refreshShowingCustom();
            refreshShowingCustom();
        } else if (mEdgeType == preference) {
            Settings.Secure.putInt(cr, Settings.Secure.ACCESSIBILITY_CAPTIONING_EDGE_TYPE, value);
        }
        }
        mCaptionHelper.setEnabled(true);
        mCaptionHelper.setEnabled(true);
    }
    }
+76 −0
Original line number Original line 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.Context;
import android.content.res.Resources;
import android.graphics.Color;

import androidx.preference.PreferenceScreen;

import com.android.settings.R;
import com.android.settings.accessibility.ListDialogPreference.OnValueChangedListener;
import com.android.settings.core.BasePreferenceController;

/** Preference controller for caption background color. */
public class CaptionBackgroundColorController extends BasePreferenceController
        implements OnValueChangedListener {

    private final CaptionHelper mCaptionHelper;

    public CaptionBackgroundColorController(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);
        final ColorPreference preference = screen.findPreference(getPreferenceKey());
        final Resources res = mContext.getResources();
        final int[] colorValues = res.getIntArray(R.array.captioning_color_selector_values);
        final String[] colorTitles = res.getStringArray(
                R.array.captioning_color_selector_titles);
        // Add "none" as an additional option for backgrounds.
        final int[] backgroundColorValues = new int[colorValues.length + 1];
        final String[] backgroundColorTitles = new String[colorTitles.length + 1];
        System.arraycopy(colorValues, 0, backgroundColorValues, 1, colorValues.length);
        System.arraycopy(colorTitles, 0, backgroundColorTitles, 1, colorTitles.length);
        backgroundColorValues[0] = Color.TRANSPARENT;
        backgroundColorTitles[0] = mContext.getString(R.string.color_none);
        preference.setTitles(backgroundColorTitles);
        preference.setValues(backgroundColorValues);
        final int backBackgroundColor = mCaptionHelper.getBackgroundColor();
        final int color = CaptionUtils.parseColor(backBackgroundColor);
        preference.setValue(color);
        preference.setOnValueChangedListener(this);
    }

    @Override
    public void onValueChanged(ListDialogPreference preference, int value) {
        final int backBackgroundColor = mCaptionHelper.getBackgroundColor();
        final int opacity = CaptionUtils.parseOpacity(backBackgroundColor);
        final int merged = CaptionUtils.mergeColorOpacity(value, opacity);
        mCaptionHelper.setBackgroundColor(merged);
        mCaptionHelper.setEnabled(true);
    }
}
+68 −0
Original line number Original line 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.Context;
import android.content.res.Resources;

import androidx.preference.PreferenceScreen;

import com.android.settings.R;
import com.android.settings.accessibility.ListDialogPreference.OnValueChangedListener;
import com.android.settings.core.BasePreferenceController;

/** Preference controller for caption background opacity. */
public class CaptionBackgroundOpacityController extends BasePreferenceController
        implements OnValueChangedListener {

    private final CaptionHelper mCaptionHelper;

    public CaptionBackgroundOpacityController(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);
        final ColorPreference preference = screen.findPreference(getPreferenceKey());
        final Resources res = mContext.getResources();
        final int[] opacityValues = res.getIntArray(R.array.captioning_opacity_selector_values);
        final String[] opacityTitles = res.getStringArray(
                R.array.captioning_opacity_selector_titles);
        preference.setTitles(opacityTitles);
        preference.setValues(opacityValues);
        final int backBackgroundColor = mCaptionHelper.getBackgroundColor();
        final int opacity = CaptionUtils.parseOpacity(backBackgroundColor);
        preference.setValue(opacity);
        preference.setOnValueChangedListener(this);
    }

    @Override
    public void onValueChanged(ListDialogPreference preference, int value) {
        final int backBackgroundColor = mCaptionHelper.getBackgroundColor();
        final int color = CaptionUtils.parseColor(backBackgroundColor);
        final int merged = CaptionUtils.mergeColorOpacity(color, value);
        mCaptionHelper.setBackgroundColor(merged);
        mCaptionHelper.setEnabled(true);
    }
}
+63 −0
Original line number Original line 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.Context;
import android.content.res.Resources;

import androidx.preference.PreferenceScreen;

import com.android.settings.R;
import com.android.settings.accessibility.ListDialogPreference.OnValueChangedListener;
import com.android.settings.core.BasePreferenceController;

/** Preference controller for caption edge color. */
public class CaptionEdgeColorController extends BasePreferenceController
        implements OnValueChangedListener {

    private final CaptionHelper mCaptionHelper;

    public CaptionEdgeColorController(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);
        final ColorPreference preference = screen.findPreference(getPreferenceKey());
        final Resources res = mContext.getResources();
        final int[] colorValues = res.getIntArray(R.array.captioning_color_selector_values);
        final String[] colorTitles = res.getStringArray(
                R.array.captioning_color_selector_titles);
        preference.setTitles(colorTitles);
        preference.setValues(colorValues);
        preference.setValue(mCaptionHelper.getEdgeColor());
        preference.setOnValueChangedListener(this);
    }

    @Override
    public void onValueChanged(ListDialogPreference preference, int value) {
        mCaptionHelper.setEdgeColor(value);
        mCaptionHelper.setEnabled(true);
    }
}
Loading