• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
17 package com.android.settings.accessibility;
18 
19 import android.annotation.Nullable;
20 import android.content.ContentResolver;
21 import android.content.Context;
22 import android.os.Handler;
23 import android.os.Looper;
24 import android.provider.Settings;
25 import android.view.accessibility.CaptioningManager;
26 
27 import androidx.annotation.VisibleForTesting;
28 import androidx.preference.Preference;
29 import androidx.preference.PreferenceScreen;
30 
31 import com.android.settingslib.core.lifecycle.LifecycleObserver;
32 import com.android.settingslib.core.lifecycle.events.OnStart;
33 import com.android.settingslib.core.lifecycle.events.OnStop;
34 
35 import java.util.Arrays;
36 import java.util.List;
37 
38 /** Preference controller for captioning custom visibility. */
39 public class CaptioningCustomController extends BaseCaptioningCustomController
40         implements LifecycleObserver, OnStart, OnStop {
41 
42     @Nullable
43     private Preference mCustom;
44     private final ContentResolver mContentResolver;
45     @VisibleForTesting
46     AccessibilitySettingsContentObserver mSettingsContentObserver;
47     @VisibleForTesting
48     static final List<String> CAPTIONING_FEATURE_KEYS = Arrays.asList(
49             Settings.Secure.ACCESSIBILITY_CAPTIONING_PRESET
50     );
51 
CaptioningCustomController(Context context, String preferenceKey)52     public CaptioningCustomController(Context context, String preferenceKey) {
53         this(context, preferenceKey,
54                 new AccessibilitySettingsContentObserver(new Handler(Looper.getMainLooper())));
55     }
56 
57     @VisibleForTesting
CaptioningCustomController( Context context, String preferenceKey, AccessibilitySettingsContentObserver contentObserver)58     CaptioningCustomController(
59             Context context, String preferenceKey,
60             AccessibilitySettingsContentObserver contentObserver) {
61         super(context, preferenceKey);
62         mContentResolver = context.getContentResolver();
63         mSettingsContentObserver = contentObserver;
64         mSettingsContentObserver.registerKeysToObserverCallback(CAPTIONING_FEATURE_KEYS, key -> {
65             if (mCustom != null) {
66                 mCustom.setVisible(shouldShowPreference());
67             }
68         });
69     }
70 
71     @Override
displayPreference(PreferenceScreen screen)72     public void displayPreference(PreferenceScreen screen) {
73         super.displayPreference(screen);
74         mCustom = screen.findPreference(getPreferenceKey());
75         if (mCustom != null) {
76             mCustom.setVisible(shouldShowPreference());
77         }
78     }
79 
80     @Override
onStart()81     public void onStart() {
82         mSettingsContentObserver.register(mContentResolver);
83     }
84 
85     @Override
onStop()86     public void onStop() {
87         mSettingsContentObserver.unregister(mContentResolver);
88     }
89 
shouldShowPreference()90     private boolean shouldShowPreference() {
91         return mCaptionHelper.getRawUserStyle() == CaptioningManager.CaptionStyle.PRESET_CUSTOM;
92     }
93 }
94