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