blob: 73ee8469116888f5bb86a138e009cb6a1542f294 [file] [log] [blame]
menghanli591e44b2022-07-05 17:37:09 +08001/*
2 * Copyright (C) 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.settings.accessibility;
18
19import android.content.Context;
20import android.os.Handler;
21import android.os.Looper;
22import android.provider.Settings;
23import android.view.View;
menghanlib9194c42022-07-29 08:26:44 +080024import android.view.accessibility.CaptioningManager.CaptionStyle;
menghanli591e44b2022-07-05 17:37:09 +080025
26import androidx.annotation.VisibleForTesting;
27import androidx.preference.PreferenceScreen;
28
29import com.android.internal.widget.SubtitleView;
30import com.android.settings.R;
31import com.android.settings.core.BasePreferenceController;
32import com.android.settingslib.accessibility.AccessibilityUtils;
33import com.android.settingslib.core.lifecycle.LifecycleObserver;
34import com.android.settingslib.core.lifecycle.events.OnStart;
35import com.android.settingslib.core.lifecycle.events.OnStop;
36import com.android.settingslib.widget.LayoutPreference;
37
38import java.util.Arrays;
39import java.util.List;
40import java.util.Locale;
41
menghanlie5712562022-08-01 16:47:12 +080042/** Controller that shows the captioning locale summary. */
43public class CaptioningPreviewPreferenceController extends BasePreferenceController
menghanli591e44b2022-07-05 17:37:09 +080044 implements LifecycleObserver, OnStart, OnStop {
45
46 @VisibleForTesting
47 static final List<String> CAPTIONING_FEATURE_KEYS = Arrays.asList(
48 Settings.Secure.ACCESSIBILITY_CAPTIONING_FOREGROUND_COLOR,
49 Settings.Secure.ACCESSIBILITY_CAPTIONING_BACKGROUND_COLOR,
50 Settings.Secure.ACCESSIBILITY_CAPTIONING_WINDOW_COLOR,
51 Settings.Secure.ACCESSIBILITY_CAPTIONING_EDGE_COLOR,
52 Settings.Secure.ACCESSIBILITY_CAPTIONING_PRESET,
53 Settings.Secure.ACCESSIBILITY_CAPTIONING_EDGE_TYPE,
54 Settings.Secure.ACCESSIBILITY_CAPTIONING_TYPEFACE,
55 Settings.Secure.ACCESSIBILITY_CAPTIONING_FONT_SCALE
56 );
57 private final Handler mHandler = new Handler(Looper.getMainLooper());
58 @VisibleForTesting
59 AccessibilitySettingsContentObserver mSettingsContentObserver;
menghanli591e44b2022-07-05 17:37:09 +080060 private CaptionHelper mCaptionHelper;
61 private LayoutPreference mPreference;
menghanli591e44b2022-07-05 17:37:09 +080062
menghanlie5712562022-08-01 16:47:12 +080063 public CaptioningPreviewPreferenceController(Context context, String preferenceKey) {
menghanli591e44b2022-07-05 17:37:09 +080064 super(context, preferenceKey);
menghanli591e44b2022-07-05 17:37:09 +080065 mCaptionHelper = new CaptionHelper(context);
66 mSettingsContentObserver = new AccessibilitySettingsContentObserver(mHandler);
67 mSettingsContentObserver.registerKeysToObserverCallback(CAPTIONING_FEATURE_KEYS,
68 key -> refreshPreviewText());
69 }
70
71 @Override
72 public void onStart() {
73 mSettingsContentObserver.register(mContext.getContentResolver());
74 }
75
76 @Override
77 public void onStop() {
78 mContext.getContentResolver().unregisterContentObserver(mSettingsContentObserver);
79 }
80
81 @Override
82 public int getAvailabilityStatus() {
83 return AVAILABLE;
84 }
85
86 @Override
87 public void displayPreference(PreferenceScreen screen) {
88 super.displayPreference(screen);
89 mPreference = screen.findPreference(getPreferenceKey());
menghanlib9194c42022-07-29 08:26:44 +080090 final View previewViewport = mPreference.findViewById(R.id.preview_viewport);
91 previewViewport.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
menghanli591e44b2022-07-05 17:37:09 +080092 @Override
93 public void onLayoutChange(View v, int left, int top, int right,
94 int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
95 if ((oldRight - oldLeft) != (right - left)) {
96 // Remove the listener once the callback is triggered.
menghanlib9194c42022-07-29 08:26:44 +080097 previewViewport.removeOnLayoutChangeListener(this);
menghanli591e44b2022-07-05 17:37:09 +080098 mHandler.post(() -> refreshPreviewText());
99 }
100 }
101 });
102 }
103
104 private void refreshPreviewText() {
menghanlib9194c42022-07-29 08:26:44 +0800105 final SubtitleView previewText = mPreference.findViewById(R.id.preview_text);
106 if (previewText != null) {
107 final View previewViewport = mPreference.findViewById(R.id.preview_viewport);
108 final int styleId = mCaptionHelper.getRawUserStyle();
109 mCaptionHelper.applyCaptionProperties(previewText, previewViewport, styleId);
menghanli591e44b2022-07-05 17:37:09 +0800110
menghanlib9194c42022-07-29 08:26:44 +0800111 final Locale locale = mCaptionHelper.getLocale();
menghanli591e44b2022-07-05 17:37:09 +0800112 if (locale != null) {
113 final CharSequence localizedText = AccessibilityUtils.getTextForLocale(
114 mContext, locale, R.string.captioning_preview_text);
menghanlib9194c42022-07-29 08:26:44 +0800115 previewText.setText(localizedText);
menghanli591e44b2022-07-05 17:37:09 +0800116 } else {
menghanlib9194c42022-07-29 08:26:44 +0800117 previewText.setText(R.string.captioning_preview_text);
menghanli591e44b2022-07-05 17:37:09 +0800118 }
119
menghanlib9194c42022-07-29 08:26:44 +0800120 final View previewWindow = mPreference.findViewById(R.id.preview_window);
121 final CaptionStyle style = mCaptionHelper.getUserStyle();
menghanli591e44b2022-07-05 17:37:09 +0800122 if (style.hasWindowColor()) {
menghanlib9194c42022-07-29 08:26:44 +0800123 previewWindow.setBackgroundColor(style.windowColor);
menghanli591e44b2022-07-05 17:37:09 +0800124 } else {
menghanlib9194c42022-07-29 08:26:44 +0800125 final CaptionStyle defStyle = CaptionStyle.DEFAULT;
126 previewWindow.setBackgroundColor(defStyle.windowColor);
menghanli591e44b2022-07-05 17:37:09 +0800127 }
128 }
129 }
130}