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

Commit 591e44bb authored by menghanli's avatar menghanli
Browse files

Refactor CaptionAppearanceFragment to improve maintainability (1/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 preview 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: Ie8acdcb8659606ce3faf6d5532cc73ee19024725
parent 3d2e5a24
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -25,7 +25,8 @@
        android:title="@string/summary_placeholder"
        android:layout="@layout/captioning_preview"
        android:selectable="false"
        settings:searchable="false"/>
        settings:searchable="false"
        settings:controller="com.android.settings.accessibility.CaptionPreviewPreferenceController"/>

    <ListPreference
        android:entries="@array/captioning_font_size_selector_titles"
+0 −107
Original line number Diff line number Diff line
@@ -24,10 +24,7 @@ import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.provider.Settings;
import android.view.View;
import android.view.accessibility.CaptioningManager;

import androidx.preference.ListPreference;
@@ -35,18 +32,14 @@ import androidx.preference.Preference;
import androidx.preference.Preference.OnPreferenceChangeListener;
import androidx.preference.PreferenceCategory;

import com.android.internal.widget.SubtitleView;
import com.android.settings.R;
import com.android.settings.accessibility.ListDialogPreference.OnValueChangedListener;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settingslib.accessibility.AccessibilityUtils;
import com.android.settingslib.search.SearchIndexable;
import com.android.settingslib.widget.LayoutPreference;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/** Settings fragment containing font style of captioning properties. */
@SearchIndexable(forTarget = SearchIndexable.ALL & ~SearchIndexable.ARC)
@@ -54,7 +47,6 @@ public class CaptionAppearanceFragment extends DashboardFragment
        implements OnPreferenceChangeListener, OnValueChangedListener {

    private static final String TAG = "CaptionAppearanceFragment";
    private static final String PREF_CAPTION_PREVIEW = "caption_preview";
    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";
@@ -68,13 +60,7 @@ public class CaptionAppearanceFragment extends DashboardFragment
    private static final String PREF_PRESET = "captioning_preset";
    private static final String PREF_CUSTOM = "custom";

    /* WebVtt specifies line height as 5.3% of the viewport height. */
    private static final float LINE_HEIGHT_RATIO = 0.0533f;

    private CaptioningManager mCaptioningManager;
    private SubtitleView mPreviewText;
    private View mPreviewWindow;
    private View mPreviewViewport;

    // Standard options.
    private ListPreference mFontSize;
@@ -96,18 +82,6 @@ public class CaptionAppearanceFragment extends DashboardFragment

    private final List<Preference> mPreferenceList = new ArrayList<>();

    private final Handler mHandler = new Handler(Looper.getMainLooper());
    private final View.OnLayoutChangeListener mLayoutChangeListener =
            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) {
                    // Remove the listener once the callback is triggered.
                    mPreviewViewport.removeOnLayoutChangeListener(this);
                    mHandler.post(() ->refreshPreviewText());
                }
            };

    @Override
    public int getMetricsCategory() {
        return SettingsEnums.ACCESSIBILITY_CAPTION_APPEARANCE;
@@ -123,7 +97,6 @@ public class CaptionAppearanceFragment extends DashboardFragment
        updateAllPreferences();
        refreshShowingCustom();
        installUpdateListeners();
        refreshPreviewText();
    }

    @Override
@@ -136,83 +109,7 @@ public class CaptionAppearanceFragment extends DashboardFragment
        return TAG;
    }

    private void refreshPreviewText() {
        final Context context = getActivity();
        if (context == null) {
            // We've been destroyed, abort!
            return;
        }

        final SubtitleView preview = mPreviewText;
        if (preview != null) {
            final int styleId = mCaptioningManager.getRawUserStyle();
            applyCaptionProperties(mCaptioningManager, preview, mPreviewViewport, styleId);

            final Locale locale = mCaptioningManager.getLocale();
            if (locale != null) {
                final CharSequence localizedText = AccessibilityUtils.getTextForLocale(
                        context, locale, R.string.captioning_preview_text);
                preview.setText(localizedText);
            } else {
                preview.setText(R.string.captioning_preview_text);
            }

            final CaptioningManager.CaptionStyle style = mCaptioningManager.getUserStyle();
            if (style.hasWindowColor()) {
                mPreviewWindow.setBackgroundColor(style.windowColor);
            } else {
                final CaptioningManager.CaptionStyle defStyle =
                        CaptioningManager.CaptionStyle.DEFAULT;
                mPreviewWindow.setBackgroundColor(defStyle.windowColor);
            }
        }
    }

    /**
     * Updates font style of captioning properties for preview screen.
     *
     * @param manager caption manager
     * @param previewText preview text
     * @param previewWindow preview window
     * @param styleId font style id
     */
    public static void applyCaptionProperties(CaptioningManager manager, SubtitleView previewText,
            View previewWindow, int styleId) {
        previewText.setStyle(styleId);

        final Context context = previewText.getContext();
        final ContentResolver cr = context.getContentResolver();
        final float fontScale = manager.getFontScale();
        if (previewWindow != null) {
            // Assume the viewport is clipped with a 16:9 aspect ratio.
            final float virtualHeight = Math.max(9 * previewWindow.getWidth(),
                    16 * previewWindow.getHeight()) / 16.0f;
            previewText.setTextSize(virtualHeight * LINE_HEIGHT_RATIO * fontScale);
        } else {
            final float textSize = context.getResources().getDimension(
                    R.dimen.caption_preview_text_size);
            previewText.setTextSize(textSize * fontScale);
        }

        final Locale locale = manager.getLocale();
        if (locale != null) {
            final CharSequence localizedText = AccessibilityUtils.getTextForLocale(
                    context, locale, R.string.captioning_preview_characters);
            previewText.setText(localizedText);
        } else {
            previewText.setText(R.string.captioning_preview_characters);
        }
    }

    private void initializeAllPreferences() {
        final LayoutPreference captionPreview = findPreference(PREF_CAPTION_PREVIEW);

        mPreviewText = captionPreview.findViewById(R.id.preview_text);

        mPreviewWindow = captionPreview.findViewById(R.id.preview_window);

        mPreviewViewport = captionPreview.findViewById(R.id.preview_viewport);
        mPreviewViewport.addOnLayoutChangeListener(mLayoutChangeListener);

        final Resources res = getResources();
        final int[] presetValues = res.getIntArray(R.array.captioning_preset_selector_values);
@@ -400,8 +297,6 @@ public class CaptionAppearanceFragment extends DashboardFragment
        } else if (mEdgeType == preference) {
            Settings.Secure.putInt(cr, Settings.Secure.ACCESSIBILITY_CAPTIONING_EDGE_TYPE, value);
        }

        refreshPreviewText();
        enableCaptioningManager();
    }

@@ -411,13 +306,11 @@ public class CaptionAppearanceFragment extends DashboardFragment
        if (mTypeface == preference) {
            Settings.Secure.putString(
                    cr, Settings.Secure.ACCESSIBILITY_CAPTIONING_TYPEFACE, (String) value);
            refreshPreviewText();
            enableCaptioningManager();
        } else if (mFontSize == preference) {
            Settings.Secure.putFloat(
                    cr, Settings.Secure.ACCESSIBILITY_CAPTIONING_FONT_SCALE,
                    Float.parseFloat((String) value));
            refreshPreviewText();
            enableCaptioningManager();
        }

+80 −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.Context;
import android.view.View;
import android.view.accessibility.CaptioningManager;

import com.android.internal.widget.SubtitleView;
import com.android.settings.R;
import com.android.settingslib.accessibility.AccessibilityUtils;

import com.google.common.annotations.VisibleForTesting;

import java.util.Locale;

/**
 * Helper class for caption.
 */
public class CaptionHelper {

    /* WebVtt specifies line height as 5.3% of the viewport height. */
    @VisibleForTesting
    static final float LINE_HEIGHT_RATIO = 0.0533f;

    private final Context mContext;
    private final CaptioningManager mCaptioningManager;

    public CaptionHelper(Context context) {
        mContext = context;
        mCaptioningManager = context.getSystemService(CaptioningManager.class);
    }

    /**
     * Updates font style of captioning properties for preview screen.
     *
     * @param previewText preview text
     * @param previewWindow preview window
     * @param styleId font style id
     */
    public void applyCaptionProperties(SubtitleView previewText, View previewWindow,
            int styleId) {
        previewText.setStyle(styleId);

        final float fontScale = mCaptioningManager.getFontScale();
        if (previewWindow != null) {
            // Assume the viewport is clipped with a 16:9 aspect ratio.
            final float virtualHeight = Math.max(9 * previewWindow.getWidth(),
                    16 * previewWindow.getHeight()) / 16.0f;
            previewText.setTextSize(virtualHeight * LINE_HEIGHT_RATIO * fontScale);
        } else {
            final float textSize = mContext.getResources().getDimension(
                    R.dimen.caption_preview_text_size);
            previewText.setTextSize(textSize * fontScale);
        }

        final Locale locale = mCaptioningManager.getLocale();
        if (locale != null) {
            final CharSequence localizedText = AccessibilityUtils.getTextForLocale(
                    mContext, locale, R.string.captioning_preview_characters);
            previewText.setText(localizedText);
        } else {
            previewText.setText(R.string.captioning_preview_characters);
        }
    }
}
+135 −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.Context;
import android.os.Handler;
import android.os.Looper;
import android.provider.Settings;
import android.view.View;
import android.view.accessibility.CaptioningManager;

import androidx.annotation.VisibleForTesting;
import androidx.preference.PreferenceScreen;

import com.android.internal.widget.SubtitleView;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
import com.android.settingslib.accessibility.AccessibilityUtils;
import com.android.settingslib.core.lifecycle.LifecycleObserver;
import com.android.settingslib.core.lifecycle.events.OnStart;
import com.android.settingslib.core.lifecycle.events.OnStop;
import com.android.settingslib.widget.LayoutPreference;

import java.util.Arrays;
import java.util.List;
import java.util.Locale;

/** Controller that shows the caption locale summary. */
public class CaptionPreviewPreferenceController extends BasePreferenceController
        implements LifecycleObserver, OnStart, OnStop {

    @VisibleForTesting
    static final List<String> CAPTIONING_FEATURE_KEYS = Arrays.asList(
            Settings.Secure.ACCESSIBILITY_CAPTIONING_FOREGROUND_COLOR,
            Settings.Secure.ACCESSIBILITY_CAPTIONING_BACKGROUND_COLOR,
            Settings.Secure.ACCESSIBILITY_CAPTIONING_WINDOW_COLOR,
            Settings.Secure.ACCESSIBILITY_CAPTIONING_EDGE_COLOR,
            Settings.Secure.ACCESSIBILITY_CAPTIONING_PRESET,
            Settings.Secure.ACCESSIBILITY_CAPTIONING_EDGE_TYPE,
            Settings.Secure.ACCESSIBILITY_CAPTIONING_TYPEFACE,
            Settings.Secure.ACCESSIBILITY_CAPTIONING_FONT_SCALE
    );
    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,
                key -> refreshPreviewText());
    }

    @Override
    public void onStart() {
        mSettingsContentObserver.register(mContext.getContentResolver());
    }

    @Override
    public void onStop() {
        mContext.getContentResolver().unregisterContentObserver(mSettingsContentObserver);
    }

    @Override
    public int getAvailabilityStatus() {
        return AVAILABLE;
    }

    @Override
    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() {
            @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);
                    mHandler.post(() -> refreshPreviewText());
                }
            }
        });
    }

    private void refreshPreviewText() {
        if (mPreviewText != null) {
            final int styleId = mCaptioningManager.getRawUserStyle();
            mCaptionHelper.applyCaptionProperties(mPreviewText, mPreviewViewport, styleId);

            final Locale locale = mCaptioningManager.getLocale();
            if (locale != null) {
                final CharSequence localizedText = AccessibilityUtils.getTextForLocale(
                        mContext, locale, R.string.captioning_preview_text);
                mPreviewText.setText(localizedText);
            } else {
                mPreviewText.setText(R.string.captioning_preview_text);
            }

            final CaptioningManager.CaptionStyle style = mCaptioningManager.getUserStyle();
            if (style.hasWindowColor()) {
                mPreviewWindow.setBackgroundColor(style.windowColor);
            } else {
                final CaptioningManager.CaptionStyle defStyle =
                        CaptioningManager.CaptionStyle.DEFAULT;
                mPreviewWindow.setBackgroundColor(defStyle.windowColor);
            }
        }
    }
}
+7 −10
Original line number Diff line number Diff line
@@ -19,26 +19,24 @@ package com.android.settings.accessibility;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.accessibility.CaptioningManager;
import android.view.accessibility.CaptioningManager.CaptionStyle;
import android.widget.TextView;

import com.android.internal.widget.SubtitleView;
import com.android.settings.R;

/** Grid preference that allows the user to pick a captioning preset type. */
public class PresetPreference extends ListDialogPreference {
    private static final float DEFAULT_FONT_SIZE = 32f;

    private final CaptioningManager mCaptioningManager;
    private static final float DEFAULT_FONT_SIZE = 32f;
    private final CaptionHelper mCaptionHelper;

    public PresetPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        mCaptionHelper = new CaptionHelper(context);

        setDialogLayoutResource(R.layout.grid_picker_dialog);
        setListItemLayoutResource(R.layout.preset_picker_item);

        mCaptioningManager = (CaptioningManager) context.getSystemService(
                Context.CAPTIONING_SERVICE);
    }

    @Override
@@ -50,17 +48,16 @@ public class PresetPreference extends ListDialogPreference {
    @Override
    protected void onBindListItem(View view, int index) {
        final View previewViewport = view.findViewById(R.id.preview_viewport);
        final SubtitleView previewText = (SubtitleView) view.findViewById(R.id.preview);
        final SubtitleView previewText = view.findViewById(R.id.preview);
        final int value = getValueAt(index);
        CaptionAppearanceFragment.applyCaptionProperties(
                mCaptioningManager, previewText, previewViewport, value);
        mCaptionHelper.applyCaptionProperties(previewText, previewViewport, value);

        final float density = getContext().getResources().getDisplayMetrics().density;
        previewText.setTextSize(DEFAULT_FONT_SIZE * density);

        final CharSequence title = getTitleAt(index);
        if (title != null) {
            final TextView summary = (TextView) view.findViewById(R.id.summary);
            final TextView summary = view.findViewById(R.id.summary);
            summary.setText(title);
        }
    }
Loading