• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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.tv.settings;
18 
19 import android.content.Context;
20 import android.os.Bundle;
21 import android.util.ArraySet;
22 import android.util.Log;
23 
24 import androidx.annotation.XmlRes;
25 import androidx.preference.Preference;
26 import androidx.preference.PreferenceManager;
27 import androidx.preference.PreferenceScreen;
28 
29 import com.android.settingslib.core.AbstractPreferenceController;
30 
31 import java.util.ArrayList;
32 import java.util.Collection;
33 import java.util.List;
34 import java.util.Set;
35 import java.util.stream.Collectors;
36 
37 /**
38  * Fragment for managing a screen of preferences controlled entirely through
39  * {@link AbstractPreferenceController} objects. Based on DashboardFragment from mobile settings,
40  * but without the Tile support.
41  */
42 public abstract class PreferenceControllerFragment extends SettingsPreferenceFragment {
43     private static final String TAG = "PrefControllerFrag";
44 
45     private final Set<AbstractPreferenceController> mPreferenceControllers = new ArraySet<>();
46 
47     @Override
onAttach(Context context)48     public void onAttach(Context context) {
49         super.onAttach(context);
50         List<AbstractPreferenceController> controllers = onCreatePreferenceControllers(context);
51         if (controllers == null) {
52             controllers = new ArrayList<>();
53         }
54         mPreferenceControllers.addAll(controllers);
55     }
56 
57     @Override
onCreate(Bundle savedInstanceState)58     public void onCreate(Bundle savedInstanceState) {
59         super.onCreate(savedInstanceState);
60         getPreferenceManager().setPreferenceComparisonCallback(
61                 new PreferenceManager.SimplePreferenceComparisonCallback());
62     }
63 
64     @Override
onCreatePreferences(Bundle savedInstanceState, String rootKey)65     public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
66         setPreferencesFromResource(getPreferenceScreenResId(), null);
67         refreshAllPreferences();
68     }
69 
70     @Override
onResume()71     public void onResume() {
72         super.onResume();
73         updatePreferenceStates();
74     }
75 
76     @Override
onPreferenceTreeClick(Preference preference)77     public boolean onPreferenceTreeClick(Preference preference) {
78         // Copy before iterating, in case a preference controller tries to update the list
79         Collection<AbstractPreferenceController> controllers =
80                 new ArrayList<>(mPreferenceControllers);
81         // If preference contains intent, log it before handling.
82         // Give all controllers a chance to handle click.
83         for (AbstractPreferenceController controller : controllers) {
84             if (controller.handlePreferenceTreeClick(preference)) {
85                 return true;
86             }
87         }
88         return super.onPreferenceTreeClick(preference);
89     }
90 
91     @SuppressWarnings("unchecked")
getOnePreferenceController( Class<T> clazz)92     protected <T extends AbstractPreferenceController> T getOnePreferenceController(
93             Class<T> clazz) {
94         final List<AbstractPreferenceController> foundControllers =
95                 mPreferenceControllers.stream()
96                         .filter(clazz::isInstance).collect(Collectors.toList());
97         if (foundControllers.size() > 0) {
98             return (T) foundControllers.get(0);
99         } else {
100             return null;
101         }
102     }
103 
104     @SuppressWarnings("unchecked")
getPreferenceControllers( Class<T> clazz)105     protected  <T extends AbstractPreferenceController> Collection<T> getPreferenceControllers(
106             Class<T> clazz) {
107         return (Collection<T>) mPreferenceControllers.stream()
108                 .filter(clazz::isInstance).collect(Collectors.toList());
109     }
110 
addPreferenceController(AbstractPreferenceController controller)111     protected void addPreferenceController(AbstractPreferenceController controller) {
112         mPreferenceControllers.add(controller);
113     }
114 
115     /**
116      * Update state of each preference managed by PreferenceController.
117      */
updatePreferenceStates()118     protected void updatePreferenceStates() {
119         Collection<AbstractPreferenceController> controllers =
120                 new ArrayList<>(mPreferenceControllers);
121         final PreferenceScreen screen = getPreferenceScreen();
122         for (AbstractPreferenceController controller : controllers) {
123             if (!controller.isAvailable()) {
124                 continue;
125             }
126             final String key = controller.getPreferenceKey();
127 
128             final Preference preference = screen.findPreference(key);
129             if (preference == null) {
130                 Log.d(TAG, "Cannot find preference with key " + key
131                         + " in Controller " + controller.getClass().getSimpleName());
132                 continue;
133             }
134             controller.updateState(preference);
135         }
136     }
137 
refreshAllPreferences()138     private void refreshAllPreferences() {
139         final PreferenceScreen screen = getPreferenceScreen();
140         Collection<AbstractPreferenceController> controllers =
141                 new ArrayList<>(mPreferenceControllers);
142         for (AbstractPreferenceController controller : controllers) {
143             controller.displayPreference(screen);
144         }
145     }
146 
147     /**
148      * Get the res id for static preference xml for this fragment.
149      */
150     @XmlRes
getPreferenceScreenResId()151     protected abstract int getPreferenceScreenResId();
152 
153     /**
154      * Get a list of {@link AbstractPreferenceController} for this fragment.
155      */
onCreatePreferenceControllers( Context context)156     protected abstract List<AbstractPreferenceController> onCreatePreferenceControllers(
157             Context context);
158 }
159