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.content.ContentResolver; 20 import android.content.Context; 21 import android.os.Handler; 22 import android.os.Looper; 23 import android.provider.Settings; 24 import android.view.accessibility.CaptioningManager; 25 26 import androidx.annotation.VisibleForTesting; 27 import androidx.preference.Preference; 28 import androidx.preference.PreferenceScreen; 29 30 import com.android.settings.core.BasePreferenceController; 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 BasePreferenceController 40 implements LifecycleObserver, OnStart, OnStop { 41 42 private Preference mCustom; 43 private final CaptionHelper mCaptionHelper; 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 super(context, preferenceKey); 54 mCaptionHelper = new CaptionHelper(context); 55 mContentResolver = context.getContentResolver(); 56 mSettingsContentObserver = new AccessibilitySettingsContentObserver( 57 new Handler(Looper.getMainLooper())); 58 mSettingsContentObserver.registerKeysToObserverCallback(CAPTIONING_FEATURE_KEYS, 59 key -> refreshShowingCustom()); 60 } 61 62 @VisibleForTesting CaptioningCustomController(Context context, String preferenceKey, AccessibilitySettingsContentObserver contentObserver)63 CaptioningCustomController(Context context, String preferenceKey, 64 AccessibilitySettingsContentObserver contentObserver) { 65 this(context, preferenceKey); 66 mSettingsContentObserver = contentObserver; 67 } 68 69 @Override getAvailabilityStatus()70 public int getAvailabilityStatus() { 71 return AVAILABLE; 72 } 73 74 @Override displayPreference(PreferenceScreen screen)75 public void displayPreference(PreferenceScreen screen) { 76 super.displayPreference(screen); 77 mCustom = screen.findPreference(getPreferenceKey()); 78 refreshShowingCustom(); 79 } 80 81 @Override onStart()82 public void onStart() { 83 mSettingsContentObserver.register(mContentResolver); 84 } 85 86 @Override onStop()87 public void onStop() { 88 mSettingsContentObserver.unregister(mContentResolver); 89 } 90 refreshShowingCustom()91 private void refreshShowingCustom() { 92 final boolean isCustomPreset = 93 mCaptionHelper.getRawUserStyle() == CaptioningManager.CaptionStyle.PRESET_CUSTOM; 94 mCustom.setVisible(isCustomPreset); 95 } 96 } 97