1 /*
2  * Copyright (C) 2018 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.app.settings.SettingsEnums;
20 import android.content.Context;
21 import android.content.res.Resources;
22 
23 import androidx.preference.Preference;
24 
25 import com.android.settings.R;
26 import com.android.settings.dashboard.DashboardFragment;
27 import com.android.settings.search.BaseSearchIndexProvider;
28 import com.android.settingslib.core.AbstractPreferenceController;
29 import com.android.settingslib.core.lifecycle.Lifecycle;
30 import com.android.settingslib.search.SearchIndexable;
31 
32 import java.util.ArrayList;
33 import java.util.List;
34 
35 /** Settings fragment containing accessibility control timeout preference. */
36 @SearchIndexable(forTarget = SearchIndexable.ALL & ~SearchIndexable.ARC)
37 public final class AccessibilityControlTimeoutPreferenceFragment extends DashboardFragment
38         implements AccessibilityTimeoutController.OnChangeListener {
39 
40     static final String TAG = "AccessibilityControlTimeoutPreferenceFragment";
41     private static final List<AbstractPreferenceController> sControllers = new ArrayList<>();
42 
43     @Override
onCheckedChanged(Preference preference)44     public void onCheckedChanged(Preference preference) {
45         for (AbstractPreferenceController controller : sControllers) {
46             controller.updateState(preference);
47         }
48     }
49 
50     @Override
onResume()51     public void onResume() {
52         super.onResume();
53 
54         for (AbstractPreferenceController controller :
55                 buildPreferenceControllers(getPrefContext(), getSettingsLifecycle())) {
56             ((AccessibilityTimeoutController)controller).setOnChangeListener(this);
57         }
58     }
59 
60     @Override
onPause()61     public void onPause() {
62         super.onPause();
63 
64         for (AbstractPreferenceController controller :
65                 buildPreferenceControllers(getPrefContext(), getSettingsLifecycle())) {
66             ((AccessibilityTimeoutController)controller).setOnChangeListener(null);
67         }
68     }
69 
70     @Override
getMetricsCategory()71     public int getMetricsCategory() {
72         return SettingsEnums.ACCESSIBILITY;
73     }
74 
75     @Override
getLogTag()76     protected String getLogTag() {
77         return TAG;
78     }
79 
80     @Override
getPreferenceScreenResId()81     protected int getPreferenceScreenResId() {
82         return R.xml.accessibility_control_timeout_settings;
83     }
84 
85     @Override
createPreferenceControllers(Context context)86     protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
87         return buildPreferenceControllers(context, getSettingsLifecycle());
88     }
89 
90     @Override
getHelpResource()91     public int getHelpResource() {
92         return R.string.help_url_timeout;
93     }
94 
buildPreferenceControllers(Context context, Lifecycle lifecycle)95     private static List<AbstractPreferenceController> buildPreferenceControllers(Context context,
96             Lifecycle lifecycle) {
97         if (sControllers.size() == 0) {
98             Resources resources = context.getResources();
99 
100             String[] timeoutKeys = resources.getStringArray(
101                     R.array.accessibility_timeout_control_selector_keys);
102 
103             for (int i=0; i < timeoutKeys.length; i++) {
104                 sControllers.add(new AccessibilityTimeoutController(
105                         context, lifecycle, timeoutKeys[i]));
106             }
107         }
108         return sControllers;
109     }
110 
111     public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
112             new BaseSearchIndexProvider(R.xml.accessibility_control_timeout_settings) {
113 
114                 @Override
115                 public List<AbstractPreferenceController> createPreferenceControllers(
116                         Context context) {
117                     return buildPreferenceControllers(context, null);
118                 }
119             };
120 }
121