blob: 9b459d197c8954f84ca0a224e175b9060d5669eb [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);
Sudheer Shanka27e69312017-01-23 13:22:12 -080052 sGlobalSettingToTypeMap.put(Settings.Global.WAIT_FOR_NETWORK_TIMEOUT_MS, long.class);
Jon Miranda836c0a82014-08-11 12:32:26 -070053 // add other global settings here...
Svetoslav Ganov54d068e2011-03-02 12:58:40 -080054 }
55
56 private final Bundle mCoreSettings = new Bundle();
57
58 private final ActivityManagerService mActivityManagerService;
59
Sudheer Shanka27e69312017-01-23 13:22:12 -080060 private static final long WAIT_FOR_NETWORK_TIMEOUT_DEFAULT_MS = 2000; // 2 sec
61
Svetoslav Ganov54d068e2011-03-02 12:58:40 -080062 public CoreSettingsObserver(ActivityManagerService activityManagerService) {
63 super(activityManagerService.mHandler);
64 mActivityManagerService = activityManagerService;
65 beginObserveCoreSettings();
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -080066 sendCoreSettings();
Svetoslav Ganov54d068e2011-03-02 12:58:40 -080067 }
68
69 public Bundle getCoreSettingsLocked() {
70 return (Bundle) mCoreSettings.clone();
71 }
72
73 @Override
74 public void onChange(boolean selfChange) {
75 synchronized (mActivityManagerService) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -080076 sendCoreSettings();
Svetoslav Ganov54d068e2011-03-02 12:58:40 -080077 }
78 }
79
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -080080 private void sendCoreSettings() {
Narayan Kamathccb2a0862013-12-19 14:49:36 +000081 populateSettings(mCoreSettings, sSecureSettingToTypeMap);
82 populateSettings(mCoreSettings, sSystemSettingToTypeMap);
Jon Miranda836c0a82014-08-11 12:32:26 -070083 populateSettings(mCoreSettings, sGlobalSettingToTypeMap);
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -080084 mActivityManagerService.onCoreSettingsChange(mCoreSettings);
85 }
86
Svetoslav Ganov54d068e2011-03-02 12:58:40 -080087 private void beginObserveCoreSettings() {
Narayan Kamathccb2a0862013-12-19 14:49:36 +000088 for (String setting : sSecureSettingToTypeMap.keySet()) {
Svetoslav Ganov54d068e2011-03-02 12:58:40 -080089 Uri uri = Settings.Secure.getUriFor(setting);
90 mActivityManagerService.mContext.getContentResolver().registerContentObserver(
91 uri, false, this);
92 }
Narayan Kamathccb2a0862013-12-19 14:49:36 +000093
94 for (String setting : sSystemSettingToTypeMap.keySet()) {
95 Uri uri = Settings.System.getUriFor(setting);
96 mActivityManagerService.mContext.getContentResolver().registerContentObserver(
97 uri, false, this);
98 }
Jon Miranda836c0a82014-08-11 12:32:26 -070099
100 for (String setting : sGlobalSettingToTypeMap.keySet()) {
101 Uri uri = Settings.Global.getUriFor(setting);
102 mActivityManagerService.mContext.getContentResolver().registerContentObserver(
103 uri, false, this);
104 }
Svetoslav Ganov54d068e2011-03-02 12:58:40 -0800105 }
106
Narayan Kamathccb2a0862013-12-19 14:49:36 +0000107 private void populateSettings(Bundle snapshot, Map<String, Class<?>> map) {
Svetoslav Ganov54d068e2011-03-02 12:58:40 -0800108 Context context = mActivityManagerService.mContext;
Narayan Kamathccb2a0862013-12-19 14:49:36 +0000109 for (Map.Entry<String, Class<?>> entry : map.entrySet()) {
Svetoslav Ganov54d068e2011-03-02 12:58:40 -0800110 String setting = entry.getKey();
111 Class<?> type = entry.getValue();
Dianne Hackbornb3d4cb32015-01-09 09:54:06 -0800112 if (type == String.class) {
113 final String value;
114 if (map == sSecureSettingToTypeMap) {
115 value = Settings.Secure.getString(context.getContentResolver(), setting);
116 } else if (map == sSystemSettingToTypeMap) {
117 value = Settings.System.getString(context.getContentResolver(), setting);
118 } else {
119 value = Settings.Global.getString(context.getContentResolver(), setting);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -0800120 }
Dianne Hackbornb3d4cb32015-01-09 09:54:06 -0800121 snapshot.putString(setting, value);
122 } else if (type == int.class) {
123 final int value;
124 if (map == sSecureSettingToTypeMap) {
125 value = Settings.Secure.getInt(context.getContentResolver(), setting, 0);
126 } else if (map == sSystemSettingToTypeMap) {
127 value = Settings.System.getInt(context.getContentResolver(), setting, 0);
128 } else {
129 value = Settings.Global.getInt(context.getContentResolver(), setting, 0);
130 }
131 snapshot.putInt(setting, value);
132 } else if (type == float.class) {
133 final float value;
134 if (map == sSecureSettingToTypeMap) {
135 value = Settings.Secure.getFloat(context.getContentResolver(), setting, 0);
136 } else if (map == sSystemSettingToTypeMap) {
137 value = Settings.System.getFloat(context.getContentResolver(), setting, 0);
138 } else {
139 value = Settings.Global.getFloat(context.getContentResolver(), setting, 0);
140 }
141 snapshot.putFloat(setting, value);
142 } else if (type == long.class) {
143 final long value;
144 if (map == sSecureSettingToTypeMap) {
145 value = Settings.Secure.getLong(context.getContentResolver(), setting, 0);
146 } else if (map == sSystemSettingToTypeMap) {
147 value = Settings.System.getLong(context.getContentResolver(), setting, 0);
148 } else {
Sudheer Shanka27e69312017-01-23 13:22:12 -0800149 // TODO: remove this conditional and set the default in settings provider.
150 if (Settings.Global.WAIT_FOR_NETWORK_TIMEOUT_MS.equals(setting)) {
151 value = Settings.Global.getLong(context.getContentResolver(), setting,
152 WAIT_FOR_NETWORK_TIMEOUT_DEFAULT_MS);
153 } else {
154 value = Settings.Global.getLong(context.getContentResolver(), setting, 0);
155 }
Dianne Hackbornb3d4cb32015-01-09 09:54:06 -0800156 }
157 snapshot.putLong(setting, value);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -0800158 }
159 }
160 }
161}