Merge changes Ia52cd272,Ie8acdcb8

* changes:
  Refactor CaptionAppearanceFragment to improve maintainability (2/n)
  Refactor CaptionAppearanceFragment to improve maintainability (1/n)
This commit is contained in:
Menghan Li
2022-07-15 11:47:37 +00:00
committed by Android (Google) Code Review
12 changed files with 851 additions and 187 deletions

View File

@@ -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"

View File

@@ -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

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

@@ -0,0 +1,107 @@
/*
* 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);
}
}
}

View File

@@ -0,0 +1,135 @@
/*
* 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);
}
}
}
}

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

View File

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

View File

@@ -0,0 +1,114 @@
/*
* 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 static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.provider.Settings;
import android.view.accessibility.CaptioningManager;
import androidx.preference.ListPreference;
import androidx.preference.PreferenceScreen;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.shadow.api.Shadow;
import org.robolectric.shadows.ShadowCaptioningManager;
/** Tests for {@link CaptionFontSizeController}. */
@RunWith(RobolectricTestRunner.class)
public class CaptionFontSizeControllerTest {
@Rule
public final MockitoRule mMockitoRule = MockitoJUnit.rule();
@Mock
private PreferenceScreen mScreen;
private final Context mContext = ApplicationProvider.getApplicationContext();
private CaptionFontSizeController mController;
private ListPreference mPreference;
private ShadowCaptioningManager mShadowCaptioningManager;
@Before
public void setUp() {
mController = new CaptionFontSizeController(mContext, "captioning_font_size");
mPreference = new ListPreference(mContext);
mPreference.setEntries(R.array.captioning_font_size_selector_titles);
mPreference.setEntryValues(R.array.captioning_font_size_selector_values);
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
CaptioningManager captioningManager = mContext.getSystemService(CaptioningManager.class);
mShadowCaptioningManager = Shadow.extract(captioningManager);
}
@Test
public void getAvailabilityStatus_shouldReturnAvailable() {
assertThat(mController.getAvailabilityStatus())
.isEqualTo(BasePreferenceController.AVAILABLE);
}
@Test
public void displayPreference_byDefault_shouldReturnDefault() {
mController.displayPreference(mScreen);
assertThat(mPreference.getEntry().toString()).isEqualTo("Medium");
}
@Test
public void displayPreference_bySmallValue_shouldReturnSmall() {
mShadowCaptioningManager.setFontScale(0.5f);
mController.displayPreference(mScreen);
assertThat(mPreference.getEntry().toString()).isEqualTo("Small");
}
@Test
public void onPreferenceChange_shouldReturnSmall() {
mController.displayPreference(mScreen);
mController.onPreferenceChange(mPreference, "0.5");
assertThat(mPreference.getEntry().toString()).isEqualTo("Small");
}
@Test
public void onPreferenceChange_shouldSetCaptionEnabled() {
mShadowCaptioningManager.setEnabled(false);
mController.displayPreference(mScreen);
mController.onPreferenceChange(mPreference, "0.5");
final boolean isCaptionEnabled = Settings.Secure.getInt(mContext.getContentResolver(),
Settings.Secure.ACCESSIBILITY_CAPTIONING_ENABLED, OFF) == ON;
assertThat(isCaptionEnabled).isTrue();
}
}

View File

@@ -0,0 +1,132 @@
/*
* 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 static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.provider.Settings;
import android.view.View;
import android.view.accessibility.CaptioningManager;
import androidx.test.core.app.ApplicationProvider;
import com.android.internal.widget.SubtitleView;
import com.android.settings.R;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.robolectric.RobolectricTestRunner;
import java.util.Locale;
/** Tests for {@link CaptionHelper}. */
@RunWith(RobolectricTestRunner.class)
public class CaptionHelperTest {
@Rule
public final MockitoRule mMockitoRule = MockitoJUnit.rule();
@Mock
private CaptioningManager mCaptioningManager;
@Mock
private SubtitleView mSubtitleView;
@Mock
private View mPreviewWindow;
@Spy
private final Context mContext = ApplicationProvider.getApplicationContext();
private CaptionHelper mCaptionHelper;
@Before
public void setUp() {
when(mContext.getSystemService(CaptioningManager.class)).thenReturn(mCaptioningManager);
mCaptionHelper = new CaptionHelper(mContext);
}
@Test
public void applyCaptionProperties_verifyAction() {
final float fontScale = 1.0f;
when(mCaptioningManager.getFontScale()).thenReturn(fontScale);
final int windowSize = 100;
when(mPreviewWindow.getWidth()).thenReturn(windowSize);
when(mPreviewWindow.getHeight()).thenReturn(windowSize);
final float textSize = CaptionHelper.LINE_HEIGHT_RATIO * windowSize * fontScale;
mCaptionHelper.applyCaptionProperties(mSubtitleView, mPreviewWindow, /* styleId= */ 0);
verify(mSubtitleView).setTextSize(textSize);
verify(mSubtitleView).setText(R.string.captioning_preview_characters);
}
@Test
public void applyCaptionProperties_withoutPreviewWindow_verifyAction() {
final float fontScale = 1.0f;
when(mCaptioningManager.getFontScale()).thenReturn(fontScale);
final float textSize = mContext.getResources().getDimension(
R.dimen.caption_preview_text_size) * fontScale;
mCaptionHelper.applyCaptionProperties(mSubtitleView, /* PreviewWindow= */ null,
/* styleId= */ 0);
verify(mSubtitleView).setTextSize(textSize);
verify(mSubtitleView).setText(R.string.captioning_preview_characters);
}
@Test
public void applyCaptionProperties_localeUS_verifyAction() {
when(mCaptioningManager.getLocale()).thenReturn(Locale.US);
final String text = mContext.getString(R.string.captioning_preview_characters);
mCaptionHelper.applyCaptionProperties(mSubtitleView, /* PreviewWindow= */ null,
/* styleId= */ 0);
verify(mSubtitleView).setText(text);
}
@Test
public void enableCaptioningManager_shouldSetCaptionEnabled() {
when(mCaptioningManager.isEnabled()).thenReturn(false);
mCaptionHelper.setEnabled(true);
final boolean isCaptionEnabled = Settings.Secure.getInt(mContext.getContentResolver(),
Settings.Secure.ACCESSIBILITY_CAPTIONING_ENABLED, OFF) == ON;
assertThat(isCaptionEnabled).isTrue();
}
@Test
public void disableCaptioningManager_shouldSetCaptionDisabled() {
when(mCaptioningManager.isEnabled()).thenReturn(true);
mCaptionHelper.setEnabled(false);
final boolean isCaptionEnabled = Settings.Secure.getInt(mContext.getContentResolver(),
Settings.Secure.ACCESSIBILITY_CAPTIONING_ENABLED, OFF) == ON;
assertThat(isCaptionEnabled).isFalse();
}
}

View File

@@ -0,0 +1,92 @@
/*
* 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.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.ContentResolver;
import android.content.Context;
import android.provider.Settings;
import android.view.View;
import androidx.preference.PreferenceScreen;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.core.BasePreferenceController;
import com.android.settingslib.widget.LayoutPreference;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.robolectric.RobolectricTestRunner;
/** Tests for {@link CaptionPreviewPreferenceController}. */
@RunWith(RobolectricTestRunner.class)
public class CaptionPreviewPreferenceControllerTest {
@Rule
public final MockitoRule mMockitoRule = MockitoJUnit.rule();
@Mock
private PreferenceScreen mScreen;
@Mock
private ContentResolver mContentResolver;
@Spy
private final Context mContext = ApplicationProvider.getApplicationContext();
private CaptionPreviewPreferenceController mController;
private LayoutPreference mLayoutPreference;
@Before
public void setUp() {
when(mContext.getContentResolver()).thenReturn(mContentResolver);
mController = new CaptionPreviewPreferenceController(mContext,
"captioning_preference_switch");
final View view = new View(mContext);
mLayoutPreference = new LayoutPreference(mContext, view);
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mLayoutPreference);
}
@Test
public void getAvailabilityStatus_shouldReturnAvailable() {
assertThat(mController.getAvailabilityStatus())
.isEqualTo(BasePreferenceController.AVAILABLE);
}
@Test
public void onStart_registerSpecificContentObserverForSpecificKeys() {
mController.onStart();
for (String key : mController.CAPTIONING_FEATURE_KEYS) {
verify(mContentResolver).registerContentObserver(Settings.Secure.getUriFor(key),
/* notifyForDescendants= */ false, mController.mSettingsContentObserver);
}
}
@Test
public void onPause_unregisterContentObserver() {
mController.onStop();
verify(mContentResolver).unregisterContentObserver(mController.mSettingsContentObserver);
}
}

View File

@@ -0,0 +1,115 @@
/*
* 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 static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.provider.Settings;
import android.view.accessibility.CaptioningManager;
import androidx.preference.ListPreference;
import androidx.preference.PreferenceScreen;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.shadow.api.Shadow;
import org.robolectric.shadows.ShadowCaptioningManager;
/** Tests for {@link CaptionTypefaceController}. */
@RunWith(RobolectricTestRunner.class)
public class CaptionTypefaceControllerTest {
@Rule
public final MockitoRule mMockitoRule = MockitoJUnit.rule();
@Mock
private PreferenceScreen mScreen;
private final Context mContext = ApplicationProvider.getApplicationContext();
private CaptionTypefaceController mController;
private ListPreference mPreference;
private ShadowCaptioningManager mShadowCaptioningManager;
@Before
public void setUp() {
mController = new CaptionTypefaceController(mContext, "captioning_typeface");
mPreference = new ListPreference(mContext);
mPreference.setEntries(R.array.captioning_typeface_selector_titles);
mPreference.setEntryValues(R.array.captioning_typeface_selector_values);
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
CaptioningManager captioningManager = mContext.getSystemService(CaptioningManager.class);
mShadowCaptioningManager = Shadow.extract(captioningManager);
}
@Test
public void getAvailabilityStatus_shouldReturnAvailable() {
assertThat(mController.getAvailabilityStatus())
.isEqualTo(BasePreferenceController.AVAILABLE);
}
@Test
public void displayPreference_byDefault_shouldReturnDefault() {
mController.displayPreference(mScreen);
assertThat(mPreference.getEntry().toString()).isEqualTo("Default");
}
@Test
public void displayPreference_bySerif_shouldReturnSerif() {
Settings.Secure.putString(mContext.getContentResolver(),
Settings.Secure.ACCESSIBILITY_CAPTIONING_TYPEFACE, "serif");
mController.displayPreference(mScreen);
assertThat(mPreference.getEntry().toString()).isEqualTo("Serif");
}
@Test
public void onPreferenceChange_bySerif_shouldReturnSerif() {
mController.displayPreference(mScreen);
mController.onPreferenceChange(mPreference, "serif");
assertThat(mPreference.getEntry().toString()).isEqualTo("Serif");
}
@Test
public void onPreferenceChange_shouldSetCaptionEnabled() {
mShadowCaptioningManager.setEnabled(false);
mController.displayPreference(mScreen);
mController.onPreferenceChange(mPreference, "serif");
final boolean isCaptionEnabled = Settings.Secure.getInt(mContext.getContentResolver(),
Settings.Secure.ACCESSIBILITY_CAPTIONING_ENABLED, OFF) == ON;
assertThat(isCaptionEnabled).isTrue();
}
}