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<?>>(); |
| 44 | static { |
Narayan Kamath | ccb2a086 | 2013-12-19 14:49:36 +0000 | [diff] [blame] | 45 | sSecureSettingToTypeMap.put(Settings.Secure.LONG_PRESS_TIMEOUT, int.class); |
| 46 | // add other secure settings here... |
| 47 | |
| 48 | sSystemSettingToTypeMap.put(Settings.System.TIME_12_24, String.class); |
| 49 | // add other system settings here... |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | private final Bundle mCoreSettings = new Bundle(); |
| 53 | |
| 54 | private final ActivityManagerService mActivityManagerService; |
| 55 | |
| 56 | public CoreSettingsObserver(ActivityManagerService activityManagerService) { |
| 57 | super(activityManagerService.mHandler); |
| 58 | mActivityManagerService = activityManagerService; |
| 59 | beginObserveCoreSettings(); |
Svetoslav Ganov | 9aa597e | 2011-03-03 18:17:41 -0800 | [diff] [blame] | 60 | sendCoreSettings(); |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | public Bundle getCoreSettingsLocked() { |
| 64 | return (Bundle) mCoreSettings.clone(); |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public void onChange(boolean selfChange) { |
| 69 | synchronized (mActivityManagerService) { |
Svetoslav Ganov | 9aa597e | 2011-03-03 18:17:41 -0800 | [diff] [blame] | 70 | sendCoreSettings(); |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
Svetoslav Ganov | 9aa597e | 2011-03-03 18:17:41 -0800 | [diff] [blame] | 74 | private void sendCoreSettings() { |
Narayan Kamath | ccb2a086 | 2013-12-19 14:49:36 +0000 | [diff] [blame] | 75 | populateSettings(mCoreSettings, sSecureSettingToTypeMap); |
| 76 | populateSettings(mCoreSettings, sSystemSettingToTypeMap); |
Svetoslav Ganov | 9aa597e | 2011-03-03 18:17:41 -0800 | [diff] [blame] | 77 | mActivityManagerService.onCoreSettingsChange(mCoreSettings); |
| 78 | } |
| 79 | |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 80 | private void beginObserveCoreSettings() { |
Narayan Kamath | ccb2a086 | 2013-12-19 14:49:36 +0000 | [diff] [blame] | 81 | for (String setting : sSecureSettingToTypeMap.keySet()) { |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 82 | Uri uri = Settings.Secure.getUriFor(setting); |
| 83 | mActivityManagerService.mContext.getContentResolver().registerContentObserver( |
| 84 | uri, false, this); |
| 85 | } |
Narayan Kamath | ccb2a086 | 2013-12-19 14:49:36 +0000 | [diff] [blame] | 86 | |
| 87 | for (String setting : sSystemSettingToTypeMap.keySet()) { |
| 88 | Uri uri = Settings.System.getUriFor(setting); |
| 89 | mActivityManagerService.mContext.getContentResolver().registerContentObserver( |
| 90 | uri, false, this); |
| 91 | } |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 92 | } |
| 93 | |
Narayan Kamath | ccb2a086 | 2013-12-19 14:49:36 +0000 | [diff] [blame] | 94 | private void populateSettings(Bundle snapshot, Map<String, Class<?>> map) { |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 95 | Context context = mActivityManagerService.mContext; |
Narayan Kamath | ccb2a086 | 2013-12-19 14:49:36 +0000 | [diff] [blame] | 96 | for (Map.Entry<String, Class<?>> entry : map.entrySet()) { |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 97 | String setting = entry.getKey(); |
| 98 | Class<?> type = entry.getValue(); |
| 99 | try { |
| 100 | if (type == String.class) { |
Narayan Kamath | ccb2a086 | 2013-12-19 14:49:36 +0000 | [diff] [blame] | 101 | final String value; |
| 102 | if (map == sSecureSettingToTypeMap) { |
| 103 | value = Settings.Secure.getString(context.getContentResolver(), setting); |
| 104 | } else { |
| 105 | value = Settings.System.getString(context.getContentResolver(), setting); |
| 106 | } |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 107 | snapshot.putString(setting, value); |
| 108 | } else if (type == int.class) { |
Narayan Kamath | ccb2a086 | 2013-12-19 14:49:36 +0000 | [diff] [blame] | 109 | final int value; |
| 110 | if (map == sSecureSettingToTypeMap) { |
| 111 | value = Settings.Secure.getInt(context.getContentResolver(), setting); |
| 112 | } else { |
| 113 | value = Settings.System.getInt(context.getContentResolver(), setting); |
| 114 | } |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 115 | snapshot.putInt(setting, value); |
| 116 | } else if (type == float.class) { |
Narayan Kamath | ccb2a086 | 2013-12-19 14:49:36 +0000 | [diff] [blame] | 117 | final float value; |
| 118 | if (map == sSecureSettingToTypeMap) { |
| 119 | value = Settings.Secure.getFloat(context.getContentResolver(), setting); |
| 120 | } else { |
| 121 | value = Settings.System.getFloat(context.getContentResolver(), setting); |
| 122 | } |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 123 | snapshot.putFloat(setting, value); |
| 124 | } else if (type == long.class) { |
Narayan Kamath | ccb2a086 | 2013-12-19 14:49:36 +0000 | [diff] [blame] | 125 | final long value; |
| 126 | if (map == sSecureSettingToTypeMap) { |
| 127 | value = Settings.Secure.getLong(context.getContentResolver(), setting); |
| 128 | } else { |
| 129 | value = Settings.System.getLong(context.getContentResolver(), setting); |
| 130 | } |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 131 | snapshot.putLong(setting, value); |
| 132 | } |
| 133 | } catch (SettingNotFoundException snfe) { |
| 134 | Log.w(LOG_TAG, "Cannot find setting \"" + setting + "\"", snfe); |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | } |