Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 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 | import android.provider.Settings.SettingNotFoundException; |
| 25 | import android.util.Log; |
| 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 | */ |
Dianne Hackborn | be4e6aa | 2013-06-07 13:25:29 -0700 | [diff] [blame] | 36 | final class CoreSettingsObserver extends ContentObserver { |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 37 | private static final String LOG_TAG = CoreSettingsObserver.class.getSimpleName(); |
| 38 | |
| 39 | // mapping form property name to its type |
Narayan Kamath | ccb2a086 | 2013-12-19 14:49:36 +0000 | [diff] [blame] | 40 | private static final Map<String, Class<?>> sSecureSettingToTypeMap = new HashMap< |
| 41 | String, Class<?>>(); |
| 42 | private static final Map<String, Class<?>> sSystemSettingToTypeMap = new HashMap< |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 43 | String, Class<?>>(); |
Jon Miranda | 836c0a8 | 2014-08-11 12:32:26 -0700 | [diff] [blame] | 44 | private static final Map<String, Class<?>> sGlobalSettingToTypeMap = new HashMap< |
| 45 | String, Class<?>>(); |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 46 | static { |
Narayan Kamath | ccb2a086 | 2013-12-19 14:49:36 +0000 | [diff] [blame] | 47 | sSecureSettingToTypeMap.put(Settings.Secure.LONG_PRESS_TIMEOUT, int.class); |
| 48 | // add other secure settings here... |
| 49 | |
| 50 | sSystemSettingToTypeMap.put(Settings.System.TIME_12_24, String.class); |
| 51 | // add other system settings here... |
Jon Miranda | 836c0a8 | 2014-08-11 12:32:26 -0700 | [diff] [blame] | 52 | |
| 53 | sGlobalSettingToTypeMap.put(Settings.Global.DEBUG_VIEW_ATTRIBUTES, int.class); |
| 54 | // add other global settings here... |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | private final Bundle mCoreSettings = new Bundle(); |
| 58 | |
| 59 | private final ActivityManagerService mActivityManagerService; |
| 60 | |
| 61 | public CoreSettingsObserver(ActivityManagerService activityManagerService) { |
| 62 | super(activityManagerService.mHandler); |
| 63 | mActivityManagerService = activityManagerService; |
| 64 | beginObserveCoreSettings(); |
Svetoslav Ganov | 9aa597e | 2011-03-03 18:17:41 -0800 | [diff] [blame] | 65 | sendCoreSettings(); |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | public Bundle getCoreSettingsLocked() { |
| 69 | return (Bundle) mCoreSettings.clone(); |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public void onChange(boolean selfChange) { |
| 74 | synchronized (mActivityManagerService) { |
Svetoslav Ganov | 9aa597e | 2011-03-03 18:17:41 -0800 | [diff] [blame] | 75 | sendCoreSettings(); |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | |
Svetoslav Ganov | 9aa597e | 2011-03-03 18:17:41 -0800 | [diff] [blame] | 79 | private void sendCoreSettings() { |
Narayan Kamath | ccb2a086 | 2013-12-19 14:49:36 +0000 | [diff] [blame] | 80 | populateSettings(mCoreSettings, sSecureSettingToTypeMap); |
| 81 | populateSettings(mCoreSettings, sSystemSettingToTypeMap); |
Jon Miranda | 836c0a8 | 2014-08-11 12:32:26 -0700 | [diff] [blame] | 82 | populateSettings(mCoreSettings, sGlobalSettingToTypeMap); |
Svetoslav Ganov | 9aa597e | 2011-03-03 18:17:41 -0800 | [diff] [blame] | 83 | mActivityManagerService.onCoreSettingsChange(mCoreSettings); |
| 84 | } |
| 85 | |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 86 | private void beginObserveCoreSettings() { |
Narayan Kamath | ccb2a086 | 2013-12-19 14:49:36 +0000 | [diff] [blame] | 87 | for (String setting : sSecureSettingToTypeMap.keySet()) { |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 88 | Uri uri = Settings.Secure.getUriFor(setting); |
| 89 | mActivityManagerService.mContext.getContentResolver().registerContentObserver( |
| 90 | uri, false, this); |
| 91 | } |
Narayan Kamath | ccb2a086 | 2013-12-19 14:49:36 +0000 | [diff] [blame] | 92 | |
| 93 | for (String setting : sSystemSettingToTypeMap.keySet()) { |
| 94 | Uri uri = Settings.System.getUriFor(setting); |
| 95 | mActivityManagerService.mContext.getContentResolver().registerContentObserver( |
| 96 | uri, false, this); |
| 97 | } |
Jon Miranda | 836c0a8 | 2014-08-11 12:32:26 -0700 | [diff] [blame] | 98 | |
| 99 | for (String setting : sGlobalSettingToTypeMap.keySet()) { |
| 100 | Uri uri = Settings.Global.getUriFor(setting); |
| 101 | mActivityManagerService.mContext.getContentResolver().registerContentObserver( |
| 102 | uri, false, this); |
| 103 | } |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 104 | } |
| 105 | |
Narayan Kamath | ccb2a086 | 2013-12-19 14:49:36 +0000 | [diff] [blame] | 106 | private void populateSettings(Bundle snapshot, Map<String, Class<?>> map) { |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 107 | Context context = mActivityManagerService.mContext; |
Narayan Kamath | ccb2a086 | 2013-12-19 14:49:36 +0000 | [diff] [blame] | 108 | for (Map.Entry<String, Class<?>> entry : map.entrySet()) { |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 109 | String setting = entry.getKey(); |
| 110 | Class<?> type = entry.getValue(); |
| 111 | try { |
| 112 | if (type == String.class) { |
Narayan Kamath | ccb2a086 | 2013-12-19 14:49:36 +0000 | [diff] [blame] | 113 | final String value; |
| 114 | if (map == sSecureSettingToTypeMap) { |
| 115 | value = Settings.Secure.getString(context.getContentResolver(), setting); |
Jon Miranda | 836c0a8 | 2014-08-11 12:32:26 -0700 | [diff] [blame] | 116 | } else if (map == sSystemSettingToTypeMap) { |
Narayan Kamath | ccb2a086 | 2013-12-19 14:49:36 +0000 | [diff] [blame] | 117 | value = Settings.System.getString(context.getContentResolver(), setting); |
Jon Miranda | 836c0a8 | 2014-08-11 12:32:26 -0700 | [diff] [blame] | 118 | } else { |
| 119 | value = Settings.Global.getString(context.getContentResolver(), setting); |
Narayan Kamath | ccb2a086 | 2013-12-19 14:49:36 +0000 | [diff] [blame] | 120 | } |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 121 | snapshot.putString(setting, value); |
| 122 | } else if (type == int.class) { |
Narayan Kamath | ccb2a086 | 2013-12-19 14:49:36 +0000 | [diff] [blame] | 123 | final int value; |
| 124 | if (map == sSecureSettingToTypeMap) { |
| 125 | value = Settings.Secure.getInt(context.getContentResolver(), setting); |
Jon Miranda | 836c0a8 | 2014-08-11 12:32:26 -0700 | [diff] [blame] | 126 | } else if (map == sSystemSettingToTypeMap) { |
Narayan Kamath | ccb2a086 | 2013-12-19 14:49:36 +0000 | [diff] [blame] | 127 | value = Settings.System.getInt(context.getContentResolver(), setting); |
Jon Miranda | 836c0a8 | 2014-08-11 12:32:26 -0700 | [diff] [blame] | 128 | } else { |
| 129 | value = Settings.Global.getInt(context.getContentResolver(), setting); |
Narayan Kamath | ccb2a086 | 2013-12-19 14:49:36 +0000 | [diff] [blame] | 130 | } |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 131 | snapshot.putInt(setting, value); |
| 132 | } else if (type == float.class) { |
Narayan Kamath | ccb2a086 | 2013-12-19 14:49:36 +0000 | [diff] [blame] | 133 | final float value; |
| 134 | if (map == sSecureSettingToTypeMap) { |
| 135 | value = Settings.Secure.getFloat(context.getContentResolver(), setting); |
Jon Miranda | 836c0a8 | 2014-08-11 12:32:26 -0700 | [diff] [blame] | 136 | } else if (map == sSystemSettingToTypeMap) { |
Narayan Kamath | ccb2a086 | 2013-12-19 14:49:36 +0000 | [diff] [blame] | 137 | value = Settings.System.getFloat(context.getContentResolver(), setting); |
Jon Miranda | 836c0a8 | 2014-08-11 12:32:26 -0700 | [diff] [blame] | 138 | } else { |
| 139 | value = Settings.Global.getFloat(context.getContentResolver(), setting); |
Narayan Kamath | ccb2a086 | 2013-12-19 14:49:36 +0000 | [diff] [blame] | 140 | } |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 141 | snapshot.putFloat(setting, value); |
| 142 | } else if (type == long.class) { |
Narayan Kamath | ccb2a086 | 2013-12-19 14:49:36 +0000 | [diff] [blame] | 143 | final long value; |
| 144 | if (map == sSecureSettingToTypeMap) { |
| 145 | value = Settings.Secure.getLong(context.getContentResolver(), setting); |
Jon Miranda | 836c0a8 | 2014-08-11 12:32:26 -0700 | [diff] [blame] | 146 | } else if (map == sSystemSettingToTypeMap) { |
Narayan Kamath | ccb2a086 | 2013-12-19 14:49:36 +0000 | [diff] [blame] | 147 | value = Settings.System.getLong(context.getContentResolver(), setting); |
Jon Miranda | 836c0a8 | 2014-08-11 12:32:26 -0700 | [diff] [blame] | 148 | } else { |
| 149 | value = Settings.Global.getLong(context.getContentResolver(), setting); |
Narayan Kamath | ccb2a086 | 2013-12-19 14:49:36 +0000 | [diff] [blame] | 150 | } |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 151 | snapshot.putLong(setting, value); |
| 152 | } |
| 153 | } catch (SettingNotFoundException snfe) { |
| 154 | Log.w(LOG_TAG, "Cannot find setting \"" + setting + "\"", snfe); |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | } |