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; |
Sudheer Shanka | bce4722 | 2017-04-03 10:51:42 -0700 | [diff] [blame] | 24 | |
| 25 | import com.android.internal.annotations.VisibleForTesting; |
| 26 | |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 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 |
Sudheer Shanka | bce4722 | 2017-04-03 10:51:42 -0700 | [diff] [blame] | 40 | @VisibleForTesting |
| 41 | static final Map<String, Class<?>> sSecureSettingToTypeMap = new HashMap< |
Narayan Kamath | ccb2a086 | 2013-12-19 14:49:36 +0000 | [diff] [blame] | 42 | String, Class<?>>(); |
Sudheer Shanka | bce4722 | 2017-04-03 10:51:42 -0700 | [diff] [blame] | 43 | @VisibleForTesting |
| 44 | static final Map<String, Class<?>> sSystemSettingToTypeMap = new HashMap< |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 45 | String, Class<?>>(); |
Sudheer Shanka | bce4722 | 2017-04-03 10:51:42 -0700 | [diff] [blame] | 46 | @VisibleForTesting |
| 47 | static final Map<String, Class<?>> sGlobalSettingToTypeMap = new HashMap< |
Jon Miranda | 836c0a8 | 2014-08-11 12:32:26 -0700 | [diff] [blame] | 48 | String, Class<?>>(); |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 49 | static { |
Narayan Kamath | ccb2a086 | 2013-12-19 14:49:36 +0000 | [diff] [blame] | 50 | sSecureSettingToTypeMap.put(Settings.Secure.LONG_PRESS_TIMEOUT, int.class); |
Anthony Hugh | 96e9cc5 | 2016-07-12 15:17:24 -0700 | [diff] [blame] | 51 | sSecureSettingToTypeMap.put(Settings.Secure.MULTI_PRESS_TIMEOUT, int.class); |
Narayan Kamath | ccb2a086 | 2013-12-19 14:49:36 +0000 | [diff] [blame] | 52 | // add other secure settings here... |
| 53 | |
| 54 | sSystemSettingToTypeMap.put(Settings.System.TIME_12_24, String.class); |
| 55 | // add other system settings here... |
Jon Miranda | 836c0a8 | 2014-08-11 12:32:26 -0700 | [diff] [blame] | 56 | |
| 57 | sGlobalSettingToTypeMap.put(Settings.Global.DEBUG_VIEW_ATTRIBUTES, int.class); |
Tim Van Patten | 3c61284 | 2018-11-09 16:48:24 -0700 | [diff] [blame] | 58 | sGlobalSettingToTypeMap.put( |
| 59 | Settings.Global.GLOBAL_SETTINGS_ANGLE_GL_DRIVER_ALL_ANGLE, String.class); |
| 60 | sGlobalSettingToTypeMap.put( |
| 61 | Settings.Global.GLOBAL_SETTINGS_ANGLE_GL_DRIVER_SELECTION_PKGS, String.class); |
| 62 | sGlobalSettingToTypeMap.put( |
| 63 | Settings.Global.GLOBAL_SETTINGS_ANGLE_GL_DRIVER_SELECTION_VALUES, String.class); |
Cody Northrop | ebe6a56 | 2018-10-15 07:22:23 -0600 | [diff] [blame] | 64 | sGlobalSettingToTypeMap.put(Settings.Global.ENABLE_GPU_DEBUG_LAYERS, int.class); |
| 65 | sGlobalSettingToTypeMap.put(Settings.Global.GPU_DEBUG_APP, String.class); |
| 66 | sGlobalSettingToTypeMap.put(Settings.Global.GPU_DEBUG_LAYERS, String.class); |
Cody Northrop | 0fa1d22 | 2018-10-23 13:13:21 -0600 | [diff] [blame] | 67 | sGlobalSettingToTypeMap.put(Settings.Global.GPU_DEBUG_LAYERS_GLES, String.class); |
Cody Northrop | ebe6a56 | 2018-10-15 07:22:23 -0600 | [diff] [blame] | 68 | sGlobalSettingToTypeMap.put(Settings.Global.GPU_DEBUG_LAYER_APP, String.class); |
Svet Ganov | 5520368 | 2018-11-02 22:34:52 -0700 | [diff] [blame] | 69 | sGlobalSettingToTypeMap.put(Settings.Global.SMS_ACCESS_RESTRICTION_ENABLED, int.class); |
Yiwei Zhang | 4a626ac | 2019-01-10 10:23:09 +0800 | [diff] [blame^] | 70 | sGlobalSettingToTypeMap.put(Settings.Global.GUP_DEV_ALL_APPS, int.class); |
Peiyong Lin | b33ffca | 2018-12-26 13:22:52 -0800 | [diff] [blame] | 71 | sGlobalSettingToTypeMap.put(Settings.Global.GUP_DEV_OPT_IN_APPS, String.class); |
Yiwei Zhang | 51015a7 | 2018-12-29 03:47:56 +0800 | [diff] [blame] | 72 | sGlobalSettingToTypeMap.put(Settings.Global.GUP_DEV_OPT_OUT_APPS, String.class); |
Yiwei Zhang | 4a626ac | 2019-01-10 10:23:09 +0800 | [diff] [blame^] | 73 | sGlobalSettingToTypeMap.put(Settings.Global.GUP_BLACKLIST, String.class); |
Jon Miranda | 836c0a8 | 2014-08-11 12:32:26 -0700 | [diff] [blame] | 74 | // add other global settings here... |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | private final Bundle mCoreSettings = new Bundle(); |
| 78 | |
| 79 | private final ActivityManagerService mActivityManagerService; |
| 80 | |
| 81 | public CoreSettingsObserver(ActivityManagerService activityManagerService) { |
| 82 | super(activityManagerService.mHandler); |
| 83 | mActivityManagerService = activityManagerService; |
| 84 | beginObserveCoreSettings(); |
Svetoslav Ganov | 9aa597e | 2011-03-03 18:17:41 -0800 | [diff] [blame] | 85 | sendCoreSettings(); |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | public Bundle getCoreSettingsLocked() { |
| 89 | return (Bundle) mCoreSettings.clone(); |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public void onChange(boolean selfChange) { |
| 94 | synchronized (mActivityManagerService) { |
Svetoslav Ganov | 9aa597e | 2011-03-03 18:17:41 -0800 | [diff] [blame] | 95 | sendCoreSettings(); |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
Svetoslav Ganov | 9aa597e | 2011-03-03 18:17:41 -0800 | [diff] [blame] | 99 | private void sendCoreSettings() { |
Narayan Kamath | ccb2a086 | 2013-12-19 14:49:36 +0000 | [diff] [blame] | 100 | populateSettings(mCoreSettings, sSecureSettingToTypeMap); |
| 101 | populateSettings(mCoreSettings, sSystemSettingToTypeMap); |
Jon Miranda | 836c0a8 | 2014-08-11 12:32:26 -0700 | [diff] [blame] | 102 | populateSettings(mCoreSettings, sGlobalSettingToTypeMap); |
Svetoslav Ganov | 9aa597e | 2011-03-03 18:17:41 -0800 | [diff] [blame] | 103 | mActivityManagerService.onCoreSettingsChange(mCoreSettings); |
| 104 | } |
| 105 | |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 106 | private void beginObserveCoreSettings() { |
Narayan Kamath | ccb2a086 | 2013-12-19 14:49:36 +0000 | [diff] [blame] | 107 | for (String setting : sSecureSettingToTypeMap.keySet()) { |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 108 | Uri uri = Settings.Secure.getUriFor(setting); |
| 109 | mActivityManagerService.mContext.getContentResolver().registerContentObserver( |
| 110 | uri, false, this); |
| 111 | } |
Narayan Kamath | ccb2a086 | 2013-12-19 14:49:36 +0000 | [diff] [blame] | 112 | |
| 113 | for (String setting : sSystemSettingToTypeMap.keySet()) { |
| 114 | Uri uri = Settings.System.getUriFor(setting); |
| 115 | mActivityManagerService.mContext.getContentResolver().registerContentObserver( |
| 116 | uri, false, this); |
| 117 | } |
Jon Miranda | 836c0a8 | 2014-08-11 12:32:26 -0700 | [diff] [blame] | 118 | |
| 119 | for (String setting : sGlobalSettingToTypeMap.keySet()) { |
| 120 | Uri uri = Settings.Global.getUriFor(setting); |
| 121 | mActivityManagerService.mContext.getContentResolver().registerContentObserver( |
| 122 | uri, false, this); |
| 123 | } |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 124 | } |
| 125 | |
Sudheer Shanka | bce4722 | 2017-04-03 10:51:42 -0700 | [diff] [blame] | 126 | @VisibleForTesting |
| 127 | void populateSettings(Bundle snapshot, Map<String, Class<?>> map) { |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 128 | Context context = mActivityManagerService.mContext; |
Narayan Kamath | ccb2a086 | 2013-12-19 14:49:36 +0000 | [diff] [blame] | 129 | for (Map.Entry<String, Class<?>> entry : map.entrySet()) { |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 130 | String setting = entry.getKey(); |
Sudheer Shanka | bce4722 | 2017-04-03 10:51:42 -0700 | [diff] [blame] | 131 | final String value; |
| 132 | if (map == sSecureSettingToTypeMap) { |
| 133 | value = Settings.Secure.getString(context.getContentResolver(), setting); |
| 134 | } else if (map == sSystemSettingToTypeMap) { |
| 135 | value = Settings.System.getString(context.getContentResolver(), setting); |
| 136 | } else { |
| 137 | value = Settings.Global.getString(context.getContentResolver(), setting); |
| 138 | } |
| 139 | if (value == null) { |
Sudheer Shanka | c45c387 | 2018-10-10 18:24:19 -0700 | [diff] [blame] | 140 | snapshot.remove(setting); |
Sudheer Shanka | bce4722 | 2017-04-03 10:51:42 -0700 | [diff] [blame] | 141 | continue; |
| 142 | } |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 143 | Class<?> type = entry.getValue(); |
Dianne Hackborn | b3d4cb3 | 2015-01-09 09:54:06 -0800 | [diff] [blame] | 144 | if (type == String.class) { |
Dianne Hackborn | b3d4cb3 | 2015-01-09 09:54:06 -0800 | [diff] [blame] | 145 | snapshot.putString(setting, value); |
| 146 | } else if (type == int.class) { |
Sudheer Shanka | bce4722 | 2017-04-03 10:51:42 -0700 | [diff] [blame] | 147 | snapshot.putInt(setting, Integer.parseInt(value)); |
Dianne Hackborn | b3d4cb3 | 2015-01-09 09:54:06 -0800 | [diff] [blame] | 148 | } else if (type == float.class) { |
Sudheer Shanka | bce4722 | 2017-04-03 10:51:42 -0700 | [diff] [blame] | 149 | snapshot.putFloat(setting, Float.parseFloat(value)); |
Dianne Hackborn | b3d4cb3 | 2015-01-09 09:54:06 -0800 | [diff] [blame] | 150 | } else if (type == long.class) { |
Sudheer Shanka | bce4722 | 2017-04-03 10:51:42 -0700 | [diff] [blame] | 151 | snapshot.putLong(setting, Long.parseLong(value)); |
Svetoslav Ganov | 54d068e | 2011-03-02 12:58:40 -0800 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | } |
| 155 | } |