blob: d7cb2bd36bde9198f9c4c71665290d7af540941e [file] [log] [blame]
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001/*
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
17package com.android.server.am;
18
19import android.content.Context;
20import android.database.ContentObserver;
21import android.net.Uri;
22import android.os.Bundle;
23import android.provider.Settings;
Sudheer Shankabce47222017-04-03 10:51:42 -070024
25import com.android.internal.annotations.VisibleForTesting;
26
Svetoslav Ganov54d068e2011-03-02 12:58:40 -080027import java.util.HashMap;
28import 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 Hackbornbe4e6aa2013-06-07 13:25:29 -070036final class CoreSettingsObserver extends ContentObserver {
Svetoslav Ganov54d068e2011-03-02 12:58:40 -080037 private static final String LOG_TAG = CoreSettingsObserver.class.getSimpleName();
38
39 // mapping form property name to its type
Sudheer Shankabce47222017-04-03 10:51:42 -070040 @VisibleForTesting
41 static final Map<String, Class<?>> sSecureSettingToTypeMap = new HashMap<
Narayan Kamathccb2a0862013-12-19 14:49:36 +000042 String, Class<?>>();
Sudheer Shankabce47222017-04-03 10:51:42 -070043 @VisibleForTesting
44 static final Map<String, Class<?>> sSystemSettingToTypeMap = new HashMap<
Svetoslav Ganov54d068e2011-03-02 12:58:40 -080045 String, Class<?>>();
Sudheer Shankabce47222017-04-03 10:51:42 -070046 @VisibleForTesting
47 static final Map<String, Class<?>> sGlobalSettingToTypeMap = new HashMap<
Jon Miranda836c0a82014-08-11 12:32:26 -070048 String, Class<?>>();
Svetoslav Ganov54d068e2011-03-02 12:58:40 -080049 static {
Narayan Kamathccb2a0862013-12-19 14:49:36 +000050 sSecureSettingToTypeMap.put(Settings.Secure.LONG_PRESS_TIMEOUT, int.class);
Anthony Hugh96e9cc52016-07-12 15:17:24 -070051 sSecureSettingToTypeMap.put(Settings.Secure.MULTI_PRESS_TIMEOUT, int.class);
Narayan Kamathccb2a0862013-12-19 14:49:36 +000052 // add other secure settings here...
53
54 sSystemSettingToTypeMap.put(Settings.System.TIME_12_24, String.class);
55 // add other system settings here...
Jon Miranda836c0a82014-08-11 12:32:26 -070056
57 sGlobalSettingToTypeMap.put(Settings.Global.DEBUG_VIEW_ATTRIBUTES, int.class);
Tim Van Patten3c612842018-11-09 16:48:24 -070058 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 Northropebe6a562018-10-15 07:22:23 -060064 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 Northrop0fa1d222018-10-23 13:13:21 -060067 sGlobalSettingToTypeMap.put(Settings.Global.GPU_DEBUG_LAYERS_GLES, String.class);
Cody Northropebe6a562018-10-15 07:22:23 -060068 sGlobalSettingToTypeMap.put(Settings.Global.GPU_DEBUG_LAYER_APP, String.class);
Svet Ganov55203682018-11-02 22:34:52 -070069 sGlobalSettingToTypeMap.put(Settings.Global.SMS_ACCESS_RESTRICTION_ENABLED, int.class);
Peiyong Linb33ffca2018-12-26 13:22:52 -080070 sGlobalSettingToTypeMap.put(Settings.Global.GUP_DEV_OPT_IN_APPS, String.class);
Yiwei Zhang51015a72018-12-29 03:47:56 +080071 sGlobalSettingToTypeMap.put(Settings.Global.GUP_DEV_OPT_OUT_APPS, String.class);
Peiyong Linb33ffca2018-12-26 13:22:52 -080072 sGlobalSettingToTypeMap.put(Settings.Global.GUP_BLACK_LIST, String.class);
Jon Miranda836c0a82014-08-11 12:32:26 -070073 // add other global settings here...
Svetoslav Ganov54d068e2011-03-02 12:58:40 -080074 }
75
76 private final Bundle mCoreSettings = new Bundle();
77
78 private final ActivityManagerService mActivityManagerService;
79
80 public CoreSettingsObserver(ActivityManagerService activityManagerService) {
81 super(activityManagerService.mHandler);
82 mActivityManagerService = activityManagerService;
83 beginObserveCoreSettings();
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -080084 sendCoreSettings();
Svetoslav Ganov54d068e2011-03-02 12:58:40 -080085 }
86
87 public Bundle getCoreSettingsLocked() {
88 return (Bundle) mCoreSettings.clone();
89 }
90
91 @Override
92 public void onChange(boolean selfChange) {
93 synchronized (mActivityManagerService) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -080094 sendCoreSettings();
Svetoslav Ganov54d068e2011-03-02 12:58:40 -080095 }
96 }
97
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -080098 private void sendCoreSettings() {
Narayan Kamathccb2a0862013-12-19 14:49:36 +000099 populateSettings(mCoreSettings, sSecureSettingToTypeMap);
100 populateSettings(mCoreSettings, sSystemSettingToTypeMap);
Jon Miranda836c0a82014-08-11 12:32:26 -0700101 populateSettings(mCoreSettings, sGlobalSettingToTypeMap);
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800102 mActivityManagerService.onCoreSettingsChange(mCoreSettings);
103 }
104
Svetoslav Ganov54d068e2011-03-02 12:58:40 -0800105 private void beginObserveCoreSettings() {
Narayan Kamathccb2a0862013-12-19 14:49:36 +0000106 for (String setting : sSecureSettingToTypeMap.keySet()) {
Svetoslav Ganov54d068e2011-03-02 12:58:40 -0800107 Uri uri = Settings.Secure.getUriFor(setting);
108 mActivityManagerService.mContext.getContentResolver().registerContentObserver(
109 uri, false, this);
110 }
Narayan Kamathccb2a0862013-12-19 14:49:36 +0000111
112 for (String setting : sSystemSettingToTypeMap.keySet()) {
113 Uri uri = Settings.System.getUriFor(setting);
114 mActivityManagerService.mContext.getContentResolver().registerContentObserver(
115 uri, false, this);
116 }
Jon Miranda836c0a82014-08-11 12:32:26 -0700117
118 for (String setting : sGlobalSettingToTypeMap.keySet()) {
119 Uri uri = Settings.Global.getUriFor(setting);
120 mActivityManagerService.mContext.getContentResolver().registerContentObserver(
121 uri, false, this);
122 }
Svetoslav Ganov54d068e2011-03-02 12:58:40 -0800123 }
124
Sudheer Shankabce47222017-04-03 10:51:42 -0700125 @VisibleForTesting
126 void populateSettings(Bundle snapshot, Map<String, Class<?>> map) {
Svetoslav Ganov54d068e2011-03-02 12:58:40 -0800127 Context context = mActivityManagerService.mContext;
Narayan Kamathccb2a0862013-12-19 14:49:36 +0000128 for (Map.Entry<String, Class<?>> entry : map.entrySet()) {
Svetoslav Ganov54d068e2011-03-02 12:58:40 -0800129 String setting = entry.getKey();
Sudheer Shankabce47222017-04-03 10:51:42 -0700130 final String value;
131 if (map == sSecureSettingToTypeMap) {
132 value = Settings.Secure.getString(context.getContentResolver(), setting);
133 } else if (map == sSystemSettingToTypeMap) {
134 value = Settings.System.getString(context.getContentResolver(), setting);
135 } else {
136 value = Settings.Global.getString(context.getContentResolver(), setting);
137 }
138 if (value == null) {
Sudheer Shankac45c3872018-10-10 18:24:19 -0700139 snapshot.remove(setting);
Sudheer Shankabce47222017-04-03 10:51:42 -0700140 continue;
141 }
Svetoslav Ganov54d068e2011-03-02 12:58:40 -0800142 Class<?> type = entry.getValue();
Dianne Hackbornb3d4cb32015-01-09 09:54:06 -0800143 if (type == String.class) {
Dianne Hackbornb3d4cb32015-01-09 09:54:06 -0800144 snapshot.putString(setting, value);
145 } else if (type == int.class) {
Sudheer Shankabce47222017-04-03 10:51:42 -0700146 snapshot.putInt(setting, Integer.parseInt(value));
Dianne Hackbornb3d4cb32015-01-09 09:54:06 -0800147 } else if (type == float.class) {
Sudheer Shankabce47222017-04-03 10:51:42 -0700148 snapshot.putFloat(setting, Float.parseFloat(value));
Dianne Hackbornb3d4cb32015-01-09 09:54:06 -0800149 } else if (type == long.class) {
Sudheer Shankabce47222017-04-03 10:51:42 -0700150 snapshot.putLong(setting, Long.parseLong(value));
Svetoslav Ganov54d068e2011-03-02 12:58:40 -0800151 }
152 }
153 }
154}