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

Commit 95c771c0 authored by Menghan Li's avatar Menghan Li Committed by Android (Google) Code Review
Browse files

Merge changes Ia52cd272,Ie8acdcb8

* changes:
  Refactor CaptionAppearanceFragment to improve maintainability (2/n)
  Refactor CaptionAppearanceFragment to improve maintainability (1/n)
parents 8f36fb7f 3a591f9a
Loading
Loading
Loading
Loading
+6 −3
Original line number Diff line number Diff line
@@ -25,14 +25,16 @@
        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"
        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"
@@ -47,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 −164
Original line number Diff line number Diff line
@@ -16,45 +16,29 @@

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;
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;
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)
public class CaptionAppearanceFragment extends DashboardFragment
        implements OnPreferenceChangeListener, OnValueChangedListener {
        implements 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";
@@ -63,25 +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";

    /* 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;
    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;
@@ -94,20 +69,6 @@ public class CaptionAppearanceFragment extends DashboardFragment

    private boolean mShowingCustom;

    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;
@@ -118,12 +79,12 @@ public class CaptionAppearanceFragment extends DashboardFragment
        super.onCreatePreferences(savedInstanceState, rootKey);

        mCaptioningManager = (CaptioningManager) getSystemService(Context.CAPTIONING_SERVICE);
        mCaptionHelper = new CaptionHelper(getContext());

        initializeAllPreferences();
        updateAllPreferences();
        refreshShowingCustom();
        installUpdateListeners();
        refreshPreviewText();
    }

    @Override
@@ -136,83 +97,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);
@@ -221,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;

@@ -271,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() {
@@ -284,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);
@@ -313,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);
    }

    /**
@@ -400,36 +269,7 @@ public class CaptionAppearanceFragment extends DashboardFragment
        } else if (mEdgeType == preference) {
            Settings.Secure.putInt(cr, Settings.Secure.ACCESSIBILITY_CAPTIONING_EDGE_TYPE, value);
        }

        refreshPreviewText();
        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);
            refreshPreviewText();
            enableCaptioningManager();
        } else if (mFontSize == preference) {
            Settings.Secure.putFloat(
                    cr, Settings.Secure.ACCESSIBILITY_CAPTIONING_FONT_SCALE,
                    Float.parseFloat((String) value));
            refreshPreviewText();
            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;
    }
}
+107 −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 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;

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);
    }

    /**
     * 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.
     *
     * @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);
            }
        }
    }
}
Loading