• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2006-2011 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.server.am;
18 
19 import android.annotation.NonNull;
20 import android.app.ActivityThread;
21 import android.content.ContentResolver;
22 import android.content.Context;
23 import android.database.ContentObserver;
24 import android.net.Uri;
25 import android.os.Bundle;
26 import android.provider.DeviceConfig;
27 import android.provider.Settings;
28 import android.widget.WidgetFlags;
29 
30 import com.android.internal.R;
31 import com.android.internal.annotations.VisibleForTesting;
32 
33 import java.util.ArrayList;
34 import java.util.HashMap;
35 import java.util.HashSet;
36 import java.util.List;
37 import java.util.Map;
38 import java.util.Objects;
39 
40 /**
41  * Helper class for watching a set of core settings which the framework
42  * propagates to application processes to avoid multiple lookups and potentially
43  * disk I/O operations. Note: This class assumes that all core settings reside
44  * in {@link Settings.Secure}.
45  */
46 final class CoreSettingsObserver extends ContentObserver {
47     private static final String LOG_TAG = CoreSettingsObserver.class.getSimpleName();
48 
49     private static class DeviceConfigEntry<T> {
50         String namespace;
51         String flag;
52         String coreSettingKey;
53         Class<T> type;
54         T defaultValue;
55 
DeviceConfigEntry(String namespace, String flag, String coreSettingKey, Class<T> type, @NonNull T defaultValue)56         DeviceConfigEntry(String namespace, String flag, String coreSettingKey, Class<T> type,
57                 @NonNull T defaultValue) {
58             this.namespace = namespace;
59             this.flag = flag;
60             this.coreSettingKey = coreSettingKey;
61             this.type = type;
62             this.defaultValue = Objects.requireNonNull(defaultValue);
63         }
64     }
65 
66     // mapping form property name to its type
67     @VisibleForTesting
68     static final Map<String, Class<?>> sSecureSettingToTypeMap = new HashMap<
69             String, Class<?>>();
70     @VisibleForTesting
71     static final Map<String, Class<?>> sSystemSettingToTypeMap = new HashMap<
72             String, Class<?>>();
73     @VisibleForTesting
74     static final Map<String, Class<?>> sGlobalSettingToTypeMap = new HashMap<
75             String, Class<?>>();
76     static final List<DeviceConfigEntry> sDeviceConfigEntries = new ArrayList<DeviceConfigEntry>();
77     static {
sSecureSettingToTypeMap.put(Settings.Secure.LONG_PRESS_TIMEOUT, int.class)78         sSecureSettingToTypeMap.put(Settings.Secure.LONG_PRESS_TIMEOUT, int.class);
sSecureSettingToTypeMap.put(Settings.Secure.MULTI_PRESS_TIMEOUT, int.class)79         sSecureSettingToTypeMap.put(Settings.Secure.MULTI_PRESS_TIMEOUT, int.class);
80         // add other secure settings here...
81 
sSystemSettingToTypeMap.put(Settings.System.TIME_12_24, String.class)82         sSystemSettingToTypeMap.put(Settings.System.TIME_12_24, String.class);
83         // add other system settings here...
84 
sGlobalSettingToTypeMap.put(Settings.Global.DEBUG_VIEW_ATTRIBUTES, int.class)85         sGlobalSettingToTypeMap.put(Settings.Global.DEBUG_VIEW_ATTRIBUTES, int.class);
sGlobalSettingToTypeMap.put( Settings.Global.DEBUG_VIEW_ATTRIBUTES_APPLICATION_PACKAGE, String.class)86         sGlobalSettingToTypeMap.put(
87                 Settings.Global.DEBUG_VIEW_ATTRIBUTES_APPLICATION_PACKAGE, String.class);
sGlobalSettingToTypeMap.put( Settings.Global.ANGLE_DEBUG_PACKAGE, String.class)88         sGlobalSettingToTypeMap.put(
89                 Settings.Global.ANGLE_DEBUG_PACKAGE, String.class);
sGlobalSettingToTypeMap.put( Settings.Global.ANGLE_GL_DRIVER_ALL_ANGLE, int.class)90         sGlobalSettingToTypeMap.put(
91                 Settings.Global.ANGLE_GL_DRIVER_ALL_ANGLE, int.class);
sGlobalSettingToTypeMap.put( Settings.Global.ANGLE_GL_DRIVER_SELECTION_PKGS, String.class)92         sGlobalSettingToTypeMap.put(
93                 Settings.Global.ANGLE_GL_DRIVER_SELECTION_PKGS, String.class);
sGlobalSettingToTypeMap.put( Settings.Global.ANGLE_GL_DRIVER_SELECTION_VALUES, String.class)94         sGlobalSettingToTypeMap.put(
95                 Settings.Global.ANGLE_GL_DRIVER_SELECTION_VALUES, String.class);
sGlobalSettingToTypeMap.put( Settings.Global.ANGLE_ALLOWLIST, String.class)96         sGlobalSettingToTypeMap.put(
97                 Settings.Global.ANGLE_ALLOWLIST, String.class);
sGlobalSettingToTypeMap.put( Settings.Global.ANGLE_EGL_FEATURES, String.class)98         sGlobalSettingToTypeMap.put(
99                 Settings.Global.ANGLE_EGL_FEATURES, String.class);
sGlobalSettingToTypeMap.put( Settings.Global.SHOW_ANGLE_IN_USE_DIALOG_BOX, String.class)100         sGlobalSettingToTypeMap.put(
101                 Settings.Global.SHOW_ANGLE_IN_USE_DIALOG_BOX, String.class);
sGlobalSettingToTypeMap.put(Settings.Global.ENABLE_GPU_DEBUG_LAYERS, int.class)102         sGlobalSettingToTypeMap.put(Settings.Global.ENABLE_GPU_DEBUG_LAYERS, int.class);
sGlobalSettingToTypeMap.put(Settings.Global.GPU_DEBUG_APP, String.class)103         sGlobalSettingToTypeMap.put(Settings.Global.GPU_DEBUG_APP, String.class);
sGlobalSettingToTypeMap.put(Settings.Global.GPU_DEBUG_LAYERS, String.class)104         sGlobalSettingToTypeMap.put(Settings.Global.GPU_DEBUG_LAYERS, String.class);
sGlobalSettingToTypeMap.put(Settings.Global.GPU_DEBUG_LAYERS_GLES, String.class)105         sGlobalSettingToTypeMap.put(Settings.Global.GPU_DEBUG_LAYERS_GLES, String.class);
sGlobalSettingToTypeMap.put(Settings.Global.GPU_DEBUG_LAYER_APP, String.class)106         sGlobalSettingToTypeMap.put(Settings.Global.GPU_DEBUG_LAYER_APP, String.class);
sGlobalSettingToTypeMap.put(Settings.Global.UPDATABLE_DRIVER_ALL_APPS, int.class)107         sGlobalSettingToTypeMap.put(Settings.Global.UPDATABLE_DRIVER_ALL_APPS, int.class);
sGlobalSettingToTypeMap.put( Settings.Global.UPDATABLE_DRIVER_PRODUCTION_OPT_IN_APPS, String.class)108         sGlobalSettingToTypeMap.put(
109                 Settings.Global.UPDATABLE_DRIVER_PRODUCTION_OPT_IN_APPS, String.class);
sGlobalSettingToTypeMap.put( Settings.Global.UPDATABLE_DRIVER_PRERELEASE_OPT_IN_APPS, String.class)110         sGlobalSettingToTypeMap.put(
111                 Settings.Global.UPDATABLE_DRIVER_PRERELEASE_OPT_IN_APPS, String.class);
sGlobalSettingToTypeMap.put( Settings.Global.UPDATABLE_DRIVER_PRODUCTION_OPT_OUT_APPS, String.class)112         sGlobalSettingToTypeMap.put(
113                 Settings.Global.UPDATABLE_DRIVER_PRODUCTION_OPT_OUT_APPS, String.class);
sGlobalSettingToTypeMap.put( Settings.Global.UPDATABLE_DRIVER_PRODUCTION_DENYLIST, String.class)114         sGlobalSettingToTypeMap.put(
115                 Settings.Global.UPDATABLE_DRIVER_PRODUCTION_DENYLIST, String.class);
sGlobalSettingToTypeMap.put( Settings.Global.UPDATABLE_DRIVER_PRODUCTION_ALLOWLIST, String.class)116         sGlobalSettingToTypeMap.put(
117                 Settings.Global.UPDATABLE_DRIVER_PRODUCTION_ALLOWLIST, String.class);
sGlobalSettingToTypeMap.put( Settings.Global.UPDATABLE_DRIVER_PRODUCTION_DENYLISTS, String.class)118         sGlobalSettingToTypeMap.put(
119                 Settings.Global.UPDATABLE_DRIVER_PRODUCTION_DENYLISTS, String.class);
sGlobalSettingToTypeMap.put(Settings.Global.UPDATABLE_DRIVER_SPHAL_LIBRARIES, String.class)120         sGlobalSettingToTypeMap.put(Settings.Global.UPDATABLE_DRIVER_SPHAL_LIBRARIES, String.class);
121         // add other global settings here...
122 
sDeviceConfigEntries.add(new DeviceConfigEntry<Boolean>( DeviceConfig.NAMESPACE_WIDGET, WidgetFlags.ENABLE_CURSOR_DRAG_FROM_ANYWHERE, WidgetFlags.KEY_ENABLE_CURSOR_DRAG_FROM_ANYWHERE, boolean.class, WidgetFlags.ENABLE_CURSOR_DRAG_FROM_ANYWHERE_DEFAULT))123         sDeviceConfigEntries.add(new DeviceConfigEntry<Boolean>(
124                 DeviceConfig.NAMESPACE_WIDGET, WidgetFlags.ENABLE_CURSOR_DRAG_FROM_ANYWHERE,
125                 WidgetFlags.KEY_ENABLE_CURSOR_DRAG_FROM_ANYWHERE, boolean.class,
126                 WidgetFlags.ENABLE_CURSOR_DRAG_FROM_ANYWHERE_DEFAULT));
sDeviceConfigEntries.add(new DeviceConfigEntry<Integer>( DeviceConfig.NAMESPACE_WIDGET, WidgetFlags.CURSOR_DRAG_MIN_ANGLE_FROM_VERTICAL, WidgetFlags.KEY_CURSOR_DRAG_MIN_ANGLE_FROM_VERTICAL, int.class, WidgetFlags.CURSOR_DRAG_MIN_ANGLE_FROM_VERTICAL_DEFAULT))127         sDeviceConfigEntries.add(new DeviceConfigEntry<Integer>(
128                 DeviceConfig.NAMESPACE_WIDGET, WidgetFlags.CURSOR_DRAG_MIN_ANGLE_FROM_VERTICAL,
129                 WidgetFlags.KEY_CURSOR_DRAG_MIN_ANGLE_FROM_VERTICAL, int.class,
130                 WidgetFlags.CURSOR_DRAG_MIN_ANGLE_FROM_VERTICAL_DEFAULT));
sDeviceConfigEntries.add(new DeviceConfigEntry<Integer>( DeviceConfig.NAMESPACE_WIDGET, WidgetFlags.FINGER_TO_CURSOR_DISTANCE, WidgetFlags.KEY_FINGER_TO_CURSOR_DISTANCE, int.class, WidgetFlags.FINGER_TO_CURSOR_DISTANCE_DEFAULT))131         sDeviceConfigEntries.add(new DeviceConfigEntry<Integer>(
132                 DeviceConfig.NAMESPACE_WIDGET, WidgetFlags.FINGER_TO_CURSOR_DISTANCE,
133                 WidgetFlags.KEY_FINGER_TO_CURSOR_DISTANCE, int.class,
134                 WidgetFlags.FINGER_TO_CURSOR_DISTANCE_DEFAULT));
sDeviceConfigEntries.add(new DeviceConfigEntry<Boolean>( DeviceConfig.NAMESPACE_WIDGET, WidgetFlags.ENABLE_INSERTION_HANDLE_GESTURES, WidgetFlags.KEY_ENABLE_INSERTION_HANDLE_GESTURES, boolean.class, WidgetFlags.ENABLE_INSERTION_HANDLE_GESTURES_DEFAULT))135         sDeviceConfigEntries.add(new DeviceConfigEntry<Boolean>(
136                 DeviceConfig.NAMESPACE_WIDGET, WidgetFlags.ENABLE_INSERTION_HANDLE_GESTURES,
137                 WidgetFlags.KEY_ENABLE_INSERTION_HANDLE_GESTURES, boolean.class,
138                 WidgetFlags.ENABLE_INSERTION_HANDLE_GESTURES_DEFAULT));
sDeviceConfigEntries.add(new DeviceConfigEntry<Integer>( DeviceConfig.NAMESPACE_WIDGET, WidgetFlags.INSERTION_HANDLE_DELTA_HEIGHT, WidgetFlags.KEY_INSERTION_HANDLE_DELTA_HEIGHT, int.class, WidgetFlags.INSERTION_HANDLE_DELTA_HEIGHT_DEFAULT))139         sDeviceConfigEntries.add(new DeviceConfigEntry<Integer>(
140                 DeviceConfig.NAMESPACE_WIDGET, WidgetFlags.INSERTION_HANDLE_DELTA_HEIGHT,
141                 WidgetFlags.KEY_INSERTION_HANDLE_DELTA_HEIGHT, int.class,
142                 WidgetFlags.INSERTION_HANDLE_DELTA_HEIGHT_DEFAULT));
sDeviceConfigEntries.add(new DeviceConfigEntry<Integer>( DeviceConfig.NAMESPACE_WIDGET, WidgetFlags.INSERTION_HANDLE_OPACITY, WidgetFlags.KEY_INSERTION_HANDLE_OPACITY, int.class, WidgetFlags.INSERTION_HANDLE_OPACITY_DEFAULT))143         sDeviceConfigEntries.add(new DeviceConfigEntry<Integer>(
144                 DeviceConfig.NAMESPACE_WIDGET, WidgetFlags.INSERTION_HANDLE_OPACITY,
145                 WidgetFlags.KEY_INSERTION_HANDLE_OPACITY, int.class,
146                 WidgetFlags.INSERTION_HANDLE_OPACITY_DEFAULT));
sDeviceConfigEntries.add(new DeviceConfigEntry<Float>( DeviceConfig.NAMESPACE_WIDGET, WidgetFlags.LINE_SLOP_RATIO, WidgetFlags.KEY_LINE_SLOP_RATIO, float.class, WidgetFlags.LINE_SLOP_RATIO_DEFAULT))147         sDeviceConfigEntries.add(new DeviceConfigEntry<Float>(
148                 DeviceConfig.NAMESPACE_WIDGET, WidgetFlags.LINE_SLOP_RATIO,
149                 WidgetFlags.KEY_LINE_SLOP_RATIO, float.class,
150                 WidgetFlags.LINE_SLOP_RATIO_DEFAULT));
sDeviceConfigEntries.add(new DeviceConfigEntry<Boolean>( DeviceConfig.NAMESPACE_WIDGET, WidgetFlags.ENABLE_NEW_MAGNIFIER, WidgetFlags.KEY_ENABLE_NEW_MAGNIFIER, boolean.class, WidgetFlags.ENABLE_NEW_MAGNIFIER_DEFAULT))151         sDeviceConfigEntries.add(new DeviceConfigEntry<Boolean>(
152                 DeviceConfig.NAMESPACE_WIDGET, WidgetFlags.ENABLE_NEW_MAGNIFIER,
153                 WidgetFlags.KEY_ENABLE_NEW_MAGNIFIER, boolean.class,
154                 WidgetFlags.ENABLE_NEW_MAGNIFIER_DEFAULT));
sDeviceConfigEntries.add(new DeviceConfigEntry<Float>( DeviceConfig.NAMESPACE_WIDGET, WidgetFlags.MAGNIFIER_ZOOM_FACTOR, WidgetFlags.KEY_MAGNIFIER_ZOOM_FACTOR, float.class, WidgetFlags.MAGNIFIER_ZOOM_FACTOR_DEFAULT))155         sDeviceConfigEntries.add(new DeviceConfigEntry<Float>(
156                 DeviceConfig.NAMESPACE_WIDGET, WidgetFlags.MAGNIFIER_ZOOM_FACTOR,
157                 WidgetFlags.KEY_MAGNIFIER_ZOOM_FACTOR, float.class,
158                 WidgetFlags.MAGNIFIER_ZOOM_FACTOR_DEFAULT));
sDeviceConfigEntries.add(new DeviceConfigEntry<Float>( DeviceConfig.NAMESPACE_WIDGET, WidgetFlags.MAGNIFIER_ASPECT_RATIO, WidgetFlags.KEY_MAGNIFIER_ASPECT_RATIO, float.class, WidgetFlags.MAGNIFIER_ASPECT_RATIO_DEFAULT))159         sDeviceConfigEntries.add(new DeviceConfigEntry<Float>(
160                 DeviceConfig.NAMESPACE_WIDGET, WidgetFlags.MAGNIFIER_ASPECT_RATIO,
161                 WidgetFlags.KEY_MAGNIFIER_ASPECT_RATIO, float.class,
162                 WidgetFlags.MAGNIFIER_ASPECT_RATIO_DEFAULT));
163         // add other device configs here...
164     }
165     private static volatile boolean sDeviceConfigContextEntriesLoaded = false;
166 
167     private final Bundle mCoreSettings = new Bundle();
168 
169     private final ActivityManagerService mActivityManagerService;
170 
CoreSettingsObserver(ActivityManagerService activityManagerService)171     public CoreSettingsObserver(ActivityManagerService activityManagerService) {
172         super(activityManagerService.mHandler);
173 
174         if (!sDeviceConfigContextEntriesLoaded) {
175             synchronized (sDeviceConfigEntries) {
176                 if (!sDeviceConfigContextEntriesLoaded) {
177                     loadDeviceConfigContextEntries(activityManagerService.mContext);
178                     sDeviceConfigContextEntriesLoaded = true;
179                 }
180             }
181         }
182 
183         mActivityManagerService = activityManagerService;
184         beginObserveCoreSettings();
185         sendCoreSettings();
186     }
187 
loadDeviceConfigContextEntries(Context context)188     private static void loadDeviceConfigContextEntries(Context context) {
189         sDeviceConfigEntries.add(new DeviceConfigEntry<>(
190                 DeviceConfig.NAMESPACE_WIDGET, WidgetFlags.ANALOG_CLOCK_SECONDS_HAND_FPS,
191                 WidgetFlags.KEY_ANALOG_CLOCK_SECONDS_HAND_FPS, int.class,
192                 context.getResources()
193                         .getInteger(R.integer.config_defaultAnalogClockSecondsHandFps)));
194     }
195 
getCoreSettingsLocked()196     public Bundle getCoreSettingsLocked() {
197         return (Bundle) mCoreSettings.clone();
198     }
199 
200     @Override
onChange(boolean selfChange)201     public void onChange(boolean selfChange) {
202         synchronized (mActivityManagerService) {
203             sendCoreSettings();
204         }
205     }
206 
sendCoreSettings()207     private void sendCoreSettings() {
208         populateSettings(mCoreSettings, sSecureSettingToTypeMap);
209         populateSettings(mCoreSettings, sSystemSettingToTypeMap);
210         populateSettings(mCoreSettings, sGlobalSettingToTypeMap);
211         populateSettingsFromDeviceConfig();
212         mActivityManagerService.onCoreSettingsChange(mCoreSettings);
213     }
214 
beginObserveCoreSettings()215     private void beginObserveCoreSettings() {
216         for (String setting : sSecureSettingToTypeMap.keySet()) {
217             Uri uri = Settings.Secure.getUriFor(setting);
218             mActivityManagerService.mContext.getContentResolver().registerContentObserver(
219                     uri, false, this);
220         }
221 
222         for (String setting : sSystemSettingToTypeMap.keySet()) {
223             Uri uri = Settings.System.getUriFor(setting);
224             mActivityManagerService.mContext.getContentResolver().registerContentObserver(
225                     uri, false, this);
226         }
227 
228         for (String setting : sGlobalSettingToTypeMap.keySet()) {
229             Uri uri = Settings.Global.getUriFor(setting);
230             mActivityManagerService.mContext.getContentResolver().registerContentObserver(
231                     uri, false, this);
232         }
233 
234         HashSet<String> deviceConfigNamespaces = new HashSet<>();
235         for (DeviceConfigEntry entry : sDeviceConfigEntries) {
236             if (!deviceConfigNamespaces.contains(entry.namespace)) {
237                 DeviceConfig.addOnPropertiesChangedListener(
238                         entry.namespace, ActivityThread.currentApplication().getMainExecutor(),
239                         (DeviceConfig.Properties prop) -> onChange(false));
240                 deviceConfigNamespaces.add(entry.namespace);
241             }
242         }
243     }
244 
245     @VisibleForTesting
populateSettings(Bundle snapshot, Map<String, Class<?>> map)246     void populateSettings(Bundle snapshot, Map<String, Class<?>> map) {
247         final Context context = mActivityManagerService.mContext;
248         final ContentResolver cr = context.getContentResolver();
249         for (Map.Entry<String, Class<?>> entry : map.entrySet()) {
250             String setting = entry.getKey();
251             final String value;
252             if (map == sSecureSettingToTypeMap) {
253                 value = Settings.Secure.getStringForUser(cr, setting, cr.getUserId());
254             } else if (map == sSystemSettingToTypeMap) {
255                 value = Settings.System.getStringForUser(cr, setting, cr.getUserId());
256             } else {
257                 value = Settings.Global.getString(cr, setting);
258             }
259             if (value == null) {
260                 snapshot.remove(setting);
261                 continue;
262             }
263             Class<?> type = entry.getValue();
264             if (type == String.class) {
265                 snapshot.putString(setting, value);
266             } else if (type == int.class) {
267                 snapshot.putInt(setting, Integer.parseInt(value));
268             } else if (type == float.class) {
269                 snapshot.putFloat(setting, Float.parseFloat(value));
270             } else if (type == long.class) {
271                 snapshot.putLong(setting, Long.parseLong(value));
272             }
273         }
274     }
275 
276     @SuppressWarnings("unchecked")
populateSettingsFromDeviceConfig()277     private void populateSettingsFromDeviceConfig() {
278         for (DeviceConfigEntry<?> entry : sDeviceConfigEntries) {
279             if (entry.type == String.class) {
280                 String defaultValue = ((DeviceConfigEntry<String>) entry).defaultValue;
281                 mCoreSettings.putString(entry.coreSettingKey,
282                         DeviceConfig.getString(entry.namespace, entry.flag, defaultValue));
283             } else if (entry.type == int.class) {
284                 int defaultValue = ((DeviceConfigEntry<Integer>) entry).defaultValue;
285                 mCoreSettings.putInt(entry.coreSettingKey,
286                         DeviceConfig.getInt(entry.namespace, entry.flag, defaultValue));
287             } else if (entry.type == float.class) {
288                 float defaultValue = ((DeviceConfigEntry<Float>) entry).defaultValue;
289                 mCoreSettings.putFloat(entry.coreSettingKey,
290                         DeviceConfig.getFloat(entry.namespace, entry.flag, defaultValue));
291             } else if (entry.type == long.class) {
292                 long defaultValue = ((DeviceConfigEntry<Long>) entry).defaultValue;
293                 mCoreSettings.putLong(entry.coreSettingKey,
294                         DeviceConfig.getLong(entry.namespace, entry.flag, defaultValue));
295             } else if (entry.type == boolean.class) {
296                 boolean defaultValue = ((DeviceConfigEntry<Boolean>) entry).defaultValue;
297                 mCoreSettings.putInt(entry.coreSettingKey,
298                         DeviceConfig.getBoolean(entry.namespace, entry.flag, defaultValue) ? 1 : 0);
299             }
300         }
301     }
302 }
303