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.content.Context; 20 import android.database.ContentObserver; 21 import android.net.Uri; 22 import android.os.Bundle; 23 import android.provider.Settings; 24 25 import com.android.internal.annotations.VisibleForTesting; 26 27 import java.util.HashMap; 28 import java.util.Map; 29 30 /** 31 * Helper class for watching a set of core settings which the framework 32 * propagates to application processes to avoid multiple lookups and potentially 33 * disk I/O operations. Note: This class assumes that all core settings reside 34 * in {@link Settings.Secure}. 35 */ 36 final class CoreSettingsObserver extends ContentObserver { 37 private static final String LOG_TAG = CoreSettingsObserver.class.getSimpleName(); 38 39 // mapping form property name to its type 40 @VisibleForTesting 41 static final Map<String, Class<?>> sSecureSettingToTypeMap = new HashMap< 42 String, Class<?>>(); 43 @VisibleForTesting 44 static final Map<String, Class<?>> sSystemSettingToTypeMap = new HashMap< 45 String, Class<?>>(); 46 @VisibleForTesting 47 static final Map<String, Class<?>> sGlobalSettingToTypeMap = new HashMap< 48 String, Class<?>>(); 49 static { sSecureSettingToTypeMap.put(Settings.Secure.LONG_PRESS_TIMEOUT, int.class)50 sSecureSettingToTypeMap.put(Settings.Secure.LONG_PRESS_TIMEOUT, int.class); sSecureSettingToTypeMap.put(Settings.Secure.MULTI_PRESS_TIMEOUT, int.class)51 sSecureSettingToTypeMap.put(Settings.Secure.MULTI_PRESS_TIMEOUT, int.class); 52 // add other secure settings here... 53 sSystemSettingToTypeMap.put(Settings.System.TIME_12_24, String.class)54 sSystemSettingToTypeMap.put(Settings.System.TIME_12_24, String.class); 55 // add other system settings here... 56 sGlobalSettingToTypeMap.put(Settings.Global.DEBUG_VIEW_ATTRIBUTES, int.class)57 sGlobalSettingToTypeMap.put(Settings.Global.DEBUG_VIEW_ATTRIBUTES, int.class); sGlobalSettingToTypeMap.put( Settings.Global.DEBUG_VIEW_ATTRIBUTES_APPLICATION_PACKAGE, String.class)58 sGlobalSettingToTypeMap.put( 59 Settings.Global.DEBUG_VIEW_ATTRIBUTES_APPLICATION_PACKAGE, String.class); sGlobalSettingToTypeMap.put( Settings.Global.GLOBAL_SETTINGS_ANGLE_DEBUG_PACKAGE, String.class)60 sGlobalSettingToTypeMap.put( 61 Settings.Global.GLOBAL_SETTINGS_ANGLE_DEBUG_PACKAGE, String.class); sGlobalSettingToTypeMap.put( Settings.Global.GLOBAL_SETTINGS_ANGLE_GL_DRIVER_ALL_ANGLE, String.class)62 sGlobalSettingToTypeMap.put( 63 Settings.Global.GLOBAL_SETTINGS_ANGLE_GL_DRIVER_ALL_ANGLE, String.class); sGlobalSettingToTypeMap.put( Settings.Global.GLOBAL_SETTINGS_ANGLE_GL_DRIVER_SELECTION_PKGS, String.class)64 sGlobalSettingToTypeMap.put( 65 Settings.Global.GLOBAL_SETTINGS_ANGLE_GL_DRIVER_SELECTION_PKGS, String.class); sGlobalSettingToTypeMap.put( Settings.Global.GLOBAL_SETTINGS_ANGLE_GL_DRIVER_SELECTION_VALUES, String.class)66 sGlobalSettingToTypeMap.put( 67 Settings.Global.GLOBAL_SETTINGS_ANGLE_GL_DRIVER_SELECTION_VALUES, String.class); sGlobalSettingToTypeMap.put( Settings.Global.GLOBAL_SETTINGS_ANGLE_WHITELIST, String.class)68 sGlobalSettingToTypeMap.put( 69 Settings.Global.GLOBAL_SETTINGS_ANGLE_WHITELIST, String.class); sGlobalSettingToTypeMap.put( Settings.Global.GLOBAL_SETTINGS_SHOW_ANGLE_IN_USE_DIALOG_BOX, String.class)70 sGlobalSettingToTypeMap.put( 71 Settings.Global.GLOBAL_SETTINGS_SHOW_ANGLE_IN_USE_DIALOG_BOX, String.class); sGlobalSettingToTypeMap.put(Settings.Global.ENABLE_GPU_DEBUG_LAYERS, int.class)72 sGlobalSettingToTypeMap.put(Settings.Global.ENABLE_GPU_DEBUG_LAYERS, int.class); sGlobalSettingToTypeMap.put(Settings.Global.GPU_DEBUG_APP, String.class)73 sGlobalSettingToTypeMap.put(Settings.Global.GPU_DEBUG_APP, String.class); sGlobalSettingToTypeMap.put(Settings.Global.GPU_DEBUG_LAYERS, String.class)74 sGlobalSettingToTypeMap.put(Settings.Global.GPU_DEBUG_LAYERS, String.class); sGlobalSettingToTypeMap.put(Settings.Global.GPU_DEBUG_LAYERS_GLES, String.class)75 sGlobalSettingToTypeMap.put(Settings.Global.GPU_DEBUG_LAYERS_GLES, String.class); sGlobalSettingToTypeMap.put(Settings.Global.GPU_DEBUG_LAYER_APP, String.class)76 sGlobalSettingToTypeMap.put(Settings.Global.GPU_DEBUG_LAYER_APP, String.class); sGlobalSettingToTypeMap.put(Settings.Global.GAME_DRIVER_ALL_APPS, int.class)77 sGlobalSettingToTypeMap.put(Settings.Global.GAME_DRIVER_ALL_APPS, int.class); sGlobalSettingToTypeMap.put(Settings.Global.GAME_DRIVER_OPT_IN_APPS, String.class)78 sGlobalSettingToTypeMap.put(Settings.Global.GAME_DRIVER_OPT_IN_APPS, String.class); sGlobalSettingToTypeMap.put( Settings.Global.GAME_DRIVER_PRERELEASE_OPT_IN_APPS, String.class)79 sGlobalSettingToTypeMap.put( 80 Settings.Global.GAME_DRIVER_PRERELEASE_OPT_IN_APPS, String.class); sGlobalSettingToTypeMap.put(Settings.Global.GAME_DRIVER_OPT_OUT_APPS, String.class)81 sGlobalSettingToTypeMap.put(Settings.Global.GAME_DRIVER_OPT_OUT_APPS, String.class); sGlobalSettingToTypeMap.put(Settings.Global.GAME_DRIVER_BLACKLIST, String.class)82 sGlobalSettingToTypeMap.put(Settings.Global.GAME_DRIVER_BLACKLIST, String.class); sGlobalSettingToTypeMap.put(Settings.Global.GAME_DRIVER_WHITELIST, String.class)83 sGlobalSettingToTypeMap.put(Settings.Global.GAME_DRIVER_WHITELIST, String.class); sGlobalSettingToTypeMap.put(Settings.Global.GAME_DRIVER_BLACKLISTS, String.class)84 sGlobalSettingToTypeMap.put(Settings.Global.GAME_DRIVER_BLACKLISTS, String.class); sGlobalSettingToTypeMap.put(Settings.Global.GAME_DRIVER_SPHAL_LIBRARIES, String.class)85 sGlobalSettingToTypeMap.put(Settings.Global.GAME_DRIVER_SPHAL_LIBRARIES, String.class); 86 // add other global settings here... 87 } 88 89 private final Bundle mCoreSettings = new Bundle(); 90 91 private final ActivityManagerService mActivityManagerService; 92 CoreSettingsObserver(ActivityManagerService activityManagerService)93 public CoreSettingsObserver(ActivityManagerService activityManagerService) { 94 super(activityManagerService.mHandler); 95 mActivityManagerService = activityManagerService; 96 beginObserveCoreSettings(); 97 sendCoreSettings(); 98 } 99 getCoreSettingsLocked()100 public Bundle getCoreSettingsLocked() { 101 return (Bundle) mCoreSettings.clone(); 102 } 103 104 @Override onChange(boolean selfChange)105 public void onChange(boolean selfChange) { 106 synchronized (mActivityManagerService) { 107 sendCoreSettings(); 108 } 109 } 110 sendCoreSettings()111 private void sendCoreSettings() { 112 populateSettings(mCoreSettings, sSecureSettingToTypeMap); 113 populateSettings(mCoreSettings, sSystemSettingToTypeMap); 114 populateSettings(mCoreSettings, sGlobalSettingToTypeMap); 115 mActivityManagerService.onCoreSettingsChange(mCoreSettings); 116 } 117 beginObserveCoreSettings()118 private void beginObserveCoreSettings() { 119 for (String setting : sSecureSettingToTypeMap.keySet()) { 120 Uri uri = Settings.Secure.getUriFor(setting); 121 mActivityManagerService.mContext.getContentResolver().registerContentObserver( 122 uri, false, this); 123 } 124 125 for (String setting : sSystemSettingToTypeMap.keySet()) { 126 Uri uri = Settings.System.getUriFor(setting); 127 mActivityManagerService.mContext.getContentResolver().registerContentObserver( 128 uri, false, this); 129 } 130 131 for (String setting : sGlobalSettingToTypeMap.keySet()) { 132 Uri uri = Settings.Global.getUriFor(setting); 133 mActivityManagerService.mContext.getContentResolver().registerContentObserver( 134 uri, false, this); 135 } 136 } 137 138 @VisibleForTesting populateSettings(Bundle snapshot, Map<String, Class<?>> map)139 void populateSettings(Bundle snapshot, Map<String, Class<?>> map) { 140 Context context = mActivityManagerService.mContext; 141 for (Map.Entry<String, Class<?>> entry : map.entrySet()) { 142 String setting = entry.getKey(); 143 final String value; 144 if (map == sSecureSettingToTypeMap) { 145 value = Settings.Secure.getString(context.getContentResolver(), setting); 146 } else if (map == sSystemSettingToTypeMap) { 147 value = Settings.System.getString(context.getContentResolver(), setting); 148 } else { 149 value = Settings.Global.getString(context.getContentResolver(), setting); 150 } 151 if (value == null) { 152 snapshot.remove(setting); 153 continue; 154 } 155 Class<?> type = entry.getValue(); 156 if (type == String.class) { 157 snapshot.putString(setting, value); 158 } else if (type == int.class) { 159 snapshot.putInt(setting, Integer.parseInt(value)); 160 } else if (type == float.class) { 161 snapshot.putFloat(setting, Float.parseFloat(value)); 162 } else if (type == long.class) { 163 snapshot.putLong(setting, Long.parseLong(value)); 164 } 165 } 166 } 167 } 168