blob: 73a17c613c4d04096b406c8a6f58d94b50de98d6 [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;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -080024import java.util.HashMap;
25import java.util.Map;
26
27/**
28 * Helper class for watching a set of core settings which the framework
29 * propagates to application processes to avoid multiple lookups and potentially
30 * disk I/O operations. Note: This class assumes that all core settings reside
31 * in {@link Settings.Secure}.
32 */
Dianne Hackbornbe4e6aa2013-06-07 13:25:29 -070033final class CoreSettingsObserver extends ContentObserver {
Svetoslav Ganov54d068e2011-03-02 12:58:40 -080034 private static final String LOG_TAG = CoreSettingsObserver.class.getSimpleName();
35
36 // mapping form property name to its type
Narayan Kamathccb2a0862013-12-19 14:49:36 +000037 private static final Map<String, Class<?>> sSecureSettingToTypeMap = new HashMap<
38 String, Class<?>>();
39 private static final Map<String, Class<?>> sSystemSettingToTypeMap = new HashMap<
Svetoslav Ganov54d068e2011-03-02 12:58:40 -080040 String, Class<?>>();
Jon Miranda836c0a82014-08-11 12:32:26 -070041 private static final Map<String, Class<?>> sGlobalSettingToTypeMap = new HashMap<
42 String, Class<?>>();
Svetoslav Ganov54d068e2011-03-02 12:58:40 -080043 static {
Narayan Kamathccb2a0862013-12-19 14:49:36 +000044 sSecureSettingToTypeMap.put(Settings.Secure.LONG_PRESS_TIMEOUT, int.class);
Anthony Hugh96e9cc52016-07-12 15:17:24 -070045 sSecureSettingToTypeMap.put(Settings.Secure.MULTI_PRESS_TIMEOUT, int.class);
Narayan Kamathccb2a0862013-12-19 14:49:36 +000046 // add other secure settings here...
47
48 sSystemSettingToTypeMap.put(Settings.System.TIME_12_24, String.class);
49 // add other system settings here...
Jon Miranda836c0a82014-08-11 12:32:26 -070050
51 sGlobalSettingToTypeMap.put(Settings.Global.DEBUG_VIEW_ATTRIBUTES, int.class);
52 // add other global settings here...
Svetoslav Ganov54d068e2011-03-02 12:58:40 -080053 }
54
55 private final Bundle mCoreSettings = new Bundle();
56
57 private final ActivityManagerService mActivityManagerService;
58
59 public CoreSettingsObserver(ActivityManagerService activityManagerService) {
60 super(activityManagerService.mHandler);
61 mActivityManagerService = activityManagerService;
62 beginObserveCoreSettings();
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -080063 sendCoreSettings();
Svetoslav Ganov54d068e2011-03-02 12:58:40 -080064 }
65
66 public Bundle getCoreSettingsLocked() {
67 return (Bundle) mCoreSettings.clone();
68 }
69
70 @Override
71 public void onChange(boolean selfChange) {
72 synchronized (mActivityManagerService) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -080073 sendCoreSettings();
Svetoslav Ganov54d068e2011-03-02 12:58:40 -080074 }
75 }
76
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -080077 private void sendCoreSettings() {
Narayan Kamathccb2a0862013-12-19 14:49:36 +000078 populateSettings(mCoreSettings, sSecureSettingToTypeMap);
79 populateSettings(mCoreSettings, sSystemSettingToTypeMap);
Jon Miranda836c0a82014-08-11 12:32:26 -070080 populateSettings(mCoreSettings, sGlobalSettingToTypeMap);
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -080081 mActivityManagerService.onCoreSettingsChange(mCoreSettings);
82 }
83
Svetoslav Ganov54d068e2011-03-02 12:58:40 -080084 private void beginObserveCoreSettings() {
Narayan Kamathccb2a0862013-12-19 14:49:36 +000085 for (String setting : sSecureSettingToTypeMap.keySet()) {
Svetoslav Ganov54d068e2011-03-02 12:58:40 -080086 Uri uri = Settings.Secure.getUriFor(setting);
87 mActivityManagerService.mContext.getContentResolver().registerContentObserver(
88 uri, false, this);
89 }
Narayan Kamathccb2a0862013-12-19 14:49:36 +000090
91 for (String setting : sSystemSettingToTypeMap.keySet()) {
92 Uri uri = Settings.System.getUriFor(setting);
93 mActivityManagerService.mContext.getContentResolver().registerContentObserver(
94 uri, false, this);
95 }
Jon Miranda836c0a82014-08-11 12:32:26 -070096
97 for (String setting : sGlobalSettingToTypeMap.keySet()) {
98 Uri uri = Settings.Global.getUriFor(setting);
99 mActivityManagerService.mContext.getContentResolver().registerContentObserver(
100 uri, false, this);
101 }
Svetoslav Ganov54d068e2011-03-02 12:58:40 -0800102 }
103
Narayan Kamathccb2a0862013-12-19 14:49:36 +0000104 private void populateSettings(Bundle snapshot, Map<String, Class<?>> map) {
Svetoslav Ganov54d068e2011-03-02 12:58:40 -0800105 Context context = mActivityManagerService.mContext;
Narayan Kamathccb2a0862013-12-19 14:49:36 +0000106 for (Map.Entry<String, Class<?>> entry : map.entrySet()) {
Svetoslav Ganov54d068e2011-03-02 12:58:40 -0800107 String setting = entry.getKey();
108 Class<?> type = entry.getValue();
Dianne Hackbornb3d4cb32015-01-09 09:54:06 -0800109 if (type == String.class) {
110 final String value;
111 if (map == sSecureSettingToTypeMap) {
112 value = Settings.Secure.getString(context.getContentResolver(), setting);
113 } else if (map == sSystemSettingToTypeMap) {
114 value = Settings.System.getString(context.getContentResolver(), setting);
115 } else {
116 value = Settings.Global.getString(context.getContentResolver(), setting);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -0800117 }
Dianne Hackbornb3d4cb32015-01-09 09:54:06 -0800118 snapshot.putString(setting, value);
119 } else if (type == int.class) {
120 final int value;
121 if (map == sSecureSettingToTypeMap) {
122 value = Settings.Secure.getInt(context.getContentResolver(), setting, 0);
123 } else if (map == sSystemSettingToTypeMap) {
124 value = Settings.System.getInt(context.getContentResolver(), setting, 0);
125 } else {
126 value = Settings.Global.getInt(context.getContentResolver(), setting, 0);
127 }
128 snapshot.putInt(setting, value);
129 } else if (type == float.class) {
130 final float value;
131 if (map == sSecureSettingToTypeMap) {
132 value = Settings.Secure.getFloat(context.getContentResolver(), setting, 0);
133 } else if (map == sSystemSettingToTypeMap) {
134 value = Settings.System.getFloat(context.getContentResolver(), setting, 0);
135 } else {
136 value = Settings.Global.getFloat(context.getContentResolver(), setting, 0);
137 }
138 snapshot.putFloat(setting, value);
139 } else if (type == long.class) {
140 final long value;
141 if (map == sSecureSettingToTypeMap) {
142 value = Settings.Secure.getLong(context.getContentResolver(), setting, 0);
143 } else if (map == sSystemSettingToTypeMap) {
144 value = Settings.System.getLong(context.getContentResolver(), setting, 0);
145 } else {
146 value = Settings.Global.getLong(context.getContentResolver(), setting, 0);
147 }
148 snapshot.putLong(setting, value);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -0800149 }
150 }
151 }
152}