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_EGL_FEATURES, String.class)96 sGlobalSettingToTypeMap.put( 97 Settings.Global.ANGLE_EGL_FEATURES, String.class); sGlobalSettingToTypeMap.put( Settings.Global.SHOW_ANGLE_IN_USE_DIALOG_BOX, String.class)98 sGlobalSettingToTypeMap.put( 99 Settings.Global.SHOW_ANGLE_IN_USE_DIALOG_BOX, String.class); sGlobalSettingToTypeMap.put(Settings.Global.ENABLE_GPU_DEBUG_LAYERS, int.class)100 sGlobalSettingToTypeMap.put(Settings.Global.ENABLE_GPU_DEBUG_LAYERS, int.class); sGlobalSettingToTypeMap.put(Settings.Global.GPU_DEBUG_APP, String.class)101 sGlobalSettingToTypeMap.put(Settings.Global.GPU_DEBUG_APP, String.class); sGlobalSettingToTypeMap.put(Settings.Global.GPU_DEBUG_LAYERS, String.class)102 sGlobalSettingToTypeMap.put(Settings.Global.GPU_DEBUG_LAYERS, String.class); sGlobalSettingToTypeMap.put(Settings.Global.GPU_DEBUG_LAYERS_GLES, String.class)103 sGlobalSettingToTypeMap.put(Settings.Global.GPU_DEBUG_LAYERS_GLES, String.class); sGlobalSettingToTypeMap.put(Settings.Global.GPU_DEBUG_LAYER_APP, String.class)104 sGlobalSettingToTypeMap.put(Settings.Global.GPU_DEBUG_LAYER_APP, String.class); sGlobalSettingToTypeMap.put(Settings.Global.UPDATABLE_DRIVER_ALL_APPS, int.class)105 sGlobalSettingToTypeMap.put(Settings.Global.UPDATABLE_DRIVER_ALL_APPS, int.class); sGlobalSettingToTypeMap.put( Settings.Global.UPDATABLE_DRIVER_PRODUCTION_OPT_IN_APPS, String.class)106 sGlobalSettingToTypeMap.put( 107 Settings.Global.UPDATABLE_DRIVER_PRODUCTION_OPT_IN_APPS, String.class); sGlobalSettingToTypeMap.put( Settings.Global.UPDATABLE_DRIVER_PRERELEASE_OPT_IN_APPS, String.class)108 sGlobalSettingToTypeMap.put( 109 Settings.Global.UPDATABLE_DRIVER_PRERELEASE_OPT_IN_APPS, String.class); sGlobalSettingToTypeMap.put( Settings.Global.UPDATABLE_DRIVER_PRODUCTION_OPT_OUT_APPS, String.class)110 sGlobalSettingToTypeMap.put( 111 Settings.Global.UPDATABLE_DRIVER_PRODUCTION_OPT_OUT_APPS, String.class); sGlobalSettingToTypeMap.put( Settings.Global.UPDATABLE_DRIVER_PRODUCTION_DENYLIST, String.class)112 sGlobalSettingToTypeMap.put( 113 Settings.Global.UPDATABLE_DRIVER_PRODUCTION_DENYLIST, String.class); sGlobalSettingToTypeMap.put( Settings.Global.UPDATABLE_DRIVER_PRODUCTION_ALLOWLIST, String.class)114 sGlobalSettingToTypeMap.put( 115 Settings.Global.UPDATABLE_DRIVER_PRODUCTION_ALLOWLIST, String.class); sGlobalSettingToTypeMap.put( Settings.Global.UPDATABLE_DRIVER_PRODUCTION_DENYLISTS, String.class)116 sGlobalSettingToTypeMap.put( 117 Settings.Global.UPDATABLE_DRIVER_PRODUCTION_DENYLISTS, String.class); sGlobalSettingToTypeMap.put(Settings.Global.UPDATABLE_DRIVER_SPHAL_LIBRARIES, String.class)118 sGlobalSettingToTypeMap.put(Settings.Global.UPDATABLE_DRIVER_SPHAL_LIBRARIES, String.class); 119 // add other global settings here... 120 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))121 sDeviceConfigEntries.add(new DeviceConfigEntry<Boolean>( 122 DeviceConfig.NAMESPACE_WIDGET, WidgetFlags.ENABLE_CURSOR_DRAG_FROM_ANYWHERE, 123 WidgetFlags.KEY_ENABLE_CURSOR_DRAG_FROM_ANYWHERE, boolean.class, 124 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))125 sDeviceConfigEntries.add(new DeviceConfigEntry<Integer>( 126 DeviceConfig.NAMESPACE_WIDGET, WidgetFlags.CURSOR_DRAG_MIN_ANGLE_FROM_VERTICAL, 127 WidgetFlags.KEY_CURSOR_DRAG_MIN_ANGLE_FROM_VERTICAL, int.class, 128 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))129 sDeviceConfigEntries.add(new DeviceConfigEntry<Integer>( 130 DeviceConfig.NAMESPACE_WIDGET, WidgetFlags.FINGER_TO_CURSOR_DISTANCE, 131 WidgetFlags.KEY_FINGER_TO_CURSOR_DISTANCE, int.class, 132 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))133 sDeviceConfigEntries.add(new DeviceConfigEntry<Boolean>( 134 DeviceConfig.NAMESPACE_WIDGET, WidgetFlags.ENABLE_INSERTION_HANDLE_GESTURES, 135 WidgetFlags.KEY_ENABLE_INSERTION_HANDLE_GESTURES, boolean.class, 136 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))137 sDeviceConfigEntries.add(new DeviceConfigEntry<Integer>( 138 DeviceConfig.NAMESPACE_WIDGET, WidgetFlags.INSERTION_HANDLE_DELTA_HEIGHT, 139 WidgetFlags.KEY_INSERTION_HANDLE_DELTA_HEIGHT, int.class, 140 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))141 sDeviceConfigEntries.add(new DeviceConfigEntry<Integer>( 142 DeviceConfig.NAMESPACE_WIDGET, WidgetFlags.INSERTION_HANDLE_OPACITY, 143 WidgetFlags.KEY_INSERTION_HANDLE_OPACITY, int.class, 144 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))145 sDeviceConfigEntries.add(new DeviceConfigEntry<Float>( 146 DeviceConfig.NAMESPACE_WIDGET, WidgetFlags.LINE_SLOP_RATIO, 147 WidgetFlags.KEY_LINE_SLOP_RATIO, float.class, 148 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))149 sDeviceConfigEntries.add(new DeviceConfigEntry<Boolean>( 150 DeviceConfig.NAMESPACE_WIDGET, WidgetFlags.ENABLE_NEW_MAGNIFIER, 151 WidgetFlags.KEY_ENABLE_NEW_MAGNIFIER, boolean.class, 152 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))153 sDeviceConfigEntries.add(new DeviceConfigEntry<Float>( 154 DeviceConfig.NAMESPACE_WIDGET, WidgetFlags.MAGNIFIER_ZOOM_FACTOR, 155 WidgetFlags.KEY_MAGNIFIER_ZOOM_FACTOR, float.class, 156 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))157 sDeviceConfigEntries.add(new DeviceConfigEntry<Float>( 158 DeviceConfig.NAMESPACE_WIDGET, WidgetFlags.MAGNIFIER_ASPECT_RATIO, 159 WidgetFlags.KEY_MAGNIFIER_ASPECT_RATIO, float.class, 160 WidgetFlags.MAGNIFIER_ASPECT_RATIO_DEFAULT)); 161 // add other device configs here... 162 } 163 private static volatile boolean sDeviceConfigContextEntriesLoaded = false; 164 165 private final Bundle mCoreSettings = new Bundle(); 166 167 private final ActivityManagerService mActivityManagerService; 168 CoreSettingsObserver(ActivityManagerService activityManagerService)169 public CoreSettingsObserver(ActivityManagerService activityManagerService) { 170 super(activityManagerService.mHandler); 171 172 if (!sDeviceConfigContextEntriesLoaded) { 173 synchronized (sDeviceConfigEntries) { 174 if (!sDeviceConfigContextEntriesLoaded) { 175 loadDeviceConfigContextEntries(activityManagerService.mContext); 176 sDeviceConfigContextEntriesLoaded = true; 177 } 178 } 179 } 180 181 mActivityManagerService = activityManagerService; 182 beginObserveCoreSettings(); 183 sendCoreSettings(); 184 } 185 loadDeviceConfigContextEntries(Context context)186 private static void loadDeviceConfigContextEntries(Context context) { 187 sDeviceConfigEntries.add(new DeviceConfigEntry<>( 188 DeviceConfig.NAMESPACE_WIDGET, WidgetFlags.ANALOG_CLOCK_SECONDS_HAND_FPS, 189 WidgetFlags.KEY_ANALOG_CLOCK_SECONDS_HAND_FPS, int.class, 190 context.getResources() 191 .getInteger(R.integer.config_defaultAnalogClockSecondsHandFps))); 192 } 193 getCoreSettingsLocked()194 public Bundle getCoreSettingsLocked() { 195 return (Bundle) mCoreSettings.clone(); 196 } 197 198 @Override onChange(boolean selfChange)199 public void onChange(boolean selfChange) { 200 synchronized (mActivityManagerService) { 201 sendCoreSettings(); 202 } 203 } 204 sendCoreSettings()205 private void sendCoreSettings() { 206 populateSettings(mCoreSettings, sSecureSettingToTypeMap); 207 populateSettings(mCoreSettings, sSystemSettingToTypeMap); 208 populateSettings(mCoreSettings, sGlobalSettingToTypeMap); 209 populateSettingsFromDeviceConfig(); 210 mActivityManagerService.onCoreSettingsChange(mCoreSettings); 211 } 212 beginObserveCoreSettings()213 private void beginObserveCoreSettings() { 214 for (String setting : sSecureSettingToTypeMap.keySet()) { 215 Uri uri = Settings.Secure.getUriFor(setting); 216 mActivityManagerService.mContext.getContentResolver().registerContentObserver( 217 uri, false, this); 218 } 219 220 for (String setting : sSystemSettingToTypeMap.keySet()) { 221 Uri uri = Settings.System.getUriFor(setting); 222 mActivityManagerService.mContext.getContentResolver().registerContentObserver( 223 uri, false, this); 224 } 225 226 for (String setting : sGlobalSettingToTypeMap.keySet()) { 227 Uri uri = Settings.Global.getUriFor(setting); 228 mActivityManagerService.mContext.getContentResolver().registerContentObserver( 229 uri, false, this); 230 } 231 232 HashSet<String> deviceConfigNamespaces = new HashSet<>(); 233 for (DeviceConfigEntry entry : sDeviceConfigEntries) { 234 if (!deviceConfigNamespaces.contains(entry.namespace)) { 235 DeviceConfig.addOnPropertiesChangedListener( 236 entry.namespace, ActivityThread.currentApplication().getMainExecutor(), 237 (DeviceConfig.Properties prop) -> onChange(false)); 238 deviceConfigNamespaces.add(entry.namespace); 239 } 240 } 241 } 242 243 @VisibleForTesting populateSettings(Bundle snapshot, Map<String, Class<?>> map)244 void populateSettings(Bundle snapshot, Map<String, Class<?>> map) { 245 final Context context = mActivityManagerService.mContext; 246 final ContentResolver cr = context.getContentResolver(); 247 for (Map.Entry<String, Class<?>> entry : map.entrySet()) { 248 String setting = entry.getKey(); 249 final String value; 250 if (map == sSecureSettingToTypeMap) { 251 value = Settings.Secure.getStringForUser(cr, setting, cr.getUserId()); 252 } else if (map == sSystemSettingToTypeMap) { 253 value = Settings.System.getStringForUser(cr, setting, cr.getUserId()); 254 } else { 255 value = Settings.Global.getString(cr, setting); 256 } 257 if (value == null) { 258 snapshot.remove(setting); 259 continue; 260 } 261 Class<?> type = entry.getValue(); 262 if (type == String.class) { 263 snapshot.putString(setting, value); 264 } else if (type == int.class) { 265 snapshot.putInt(setting, Integer.parseInt(value)); 266 } else if (type == float.class) { 267 snapshot.putFloat(setting, Float.parseFloat(value)); 268 } else if (type == long.class) { 269 snapshot.putLong(setting, Long.parseLong(value)); 270 } 271 } 272 } 273 274 @SuppressWarnings("unchecked") populateSettingsFromDeviceConfig()275 private void populateSettingsFromDeviceConfig() { 276 for (DeviceConfigEntry<?> entry : sDeviceConfigEntries) { 277 if (entry.type == String.class) { 278 String defaultValue = ((DeviceConfigEntry<String>) entry).defaultValue; 279 mCoreSettings.putString(entry.coreSettingKey, 280 DeviceConfig.getString(entry.namespace, entry.flag, defaultValue)); 281 } else if (entry.type == int.class) { 282 int defaultValue = ((DeviceConfigEntry<Integer>) entry).defaultValue; 283 mCoreSettings.putInt(entry.coreSettingKey, 284 DeviceConfig.getInt(entry.namespace, entry.flag, defaultValue)); 285 } else if (entry.type == float.class) { 286 float defaultValue = ((DeviceConfigEntry<Float>) entry).defaultValue; 287 mCoreSettings.putFloat(entry.coreSettingKey, 288 DeviceConfig.getFloat(entry.namespace, entry.flag, defaultValue)); 289 } else if (entry.type == long.class) { 290 long defaultValue = ((DeviceConfigEntry<Long>) entry).defaultValue; 291 mCoreSettings.putLong(entry.coreSettingKey, 292 DeviceConfig.getLong(entry.namespace, entry.flag, defaultValue)); 293 } else if (entry.type == boolean.class) { 294 boolean defaultValue = ((DeviceConfigEntry<Boolean>) entry).defaultValue; 295 mCoreSettings.putInt(entry.coreSettingKey, 296 DeviceConfig.getBoolean(entry.namespace, entry.flag, defaultValue) ? 1 : 0); 297 } 298 } 299 } 300 } 301