blob: 160c753d9540510d64b900cffffdbe8657cd017d [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);
58 // add other global settings here...
Svetoslav Ganov54d068e2011-03-02 12:58:40 -080059 }
60
61 private final Bundle mCoreSettings = new Bundle();
62
63 private final ActivityManagerService mActivityManagerService;
64
65 public CoreSettingsObserver(ActivityManagerService activityManagerService) {
66 super(activityManagerService.mHandler);
67 mActivityManagerService = activityManagerService;
68 beginObserveCoreSettings();
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -080069 sendCoreSettings();
Svetoslav Ganov54d068e2011-03-02 12:58:40 -080070 }
71
72 public Bundle getCoreSettingsLocked() {
73 return (Bundle) mCoreSettings.clone();
74 }
75
76 @Override
77 public void onChange(boolean selfChange) {
78 synchronized (mActivityManagerService) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -080079 sendCoreSettings();
Svetoslav Ganov54d068e2011-03-02 12:58:40 -080080 }
81 }
82
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -080083 private void sendCoreSettings() {
Narayan Kamathccb2a0862013-12-19 14:49:36 +000084 populateSettings(mCoreSettings, sSecureSettingToTypeMap);
85 populateSettings(mCoreSettings, sSystemSettingToTypeMap);
Jon Miranda836c0a82014-08-11 12:32:26 -070086 populateSettings(mCoreSettings, sGlobalSettingToTypeMap);
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -080087 mActivityManagerService.onCoreSettingsChange(mCoreSettings);
88 }
89
Svetoslav Ganov54d068e2011-03-02 12:58:40 -080090 private void beginObserveCoreSettings() {
Narayan Kamathccb2a0862013-12-19 14:49:36 +000091 for (String setting : sSecureSettingToTypeMap.keySet()) {
Svetoslav Ganov54d068e2011-03-02 12:58:40 -080092 Uri uri = Settings.Secure.getUriFor(setting);
93 mActivityManagerService.mContext.getContentResolver().registerContentObserver(
94 uri, false, this);
95 }
Narayan Kamathccb2a0862013-12-19 14:49:36 +000096
97 for (String setting : sSystemSettingToTypeMap.keySet()) {
98 Uri uri = Settings.System.getUriFor(setting);
99 mActivityManagerService.mContext.getContentResolver().registerContentObserver(
100 uri, false, this);
101 }
Jon Miranda836c0a82014-08-11 12:32:26 -0700102
103 for (String setting : sGlobalSettingToTypeMap.keySet()) {
104 Uri uri = Settings.Global.getUriFor(setting);
105 mActivityManagerService.mContext.getContentResolver().registerContentObserver(
106 uri, false, this);
107 }
Svetoslav Ganov54d068e2011-03-02 12:58:40 -0800108 }
109
Sudheer Shankabce47222017-04-03 10:51:42 -0700110 @VisibleForTesting
111 void populateSettings(Bundle snapshot, Map<String, Class<?>> map) {
Svetoslav Ganov54d068e2011-03-02 12:58:40 -0800112 Context context = mActivityManagerService.mContext;
Narayan Kamathccb2a0862013-12-19 14:49:36 +0000113 for (Map.Entry<String, Class<?>> entry : map.entrySet()) {
Svetoslav Ganov54d068e2011-03-02 12:58:40 -0800114 String setting = entry.getKey();
Sudheer Shankabce47222017-04-03 10:51:42 -0700115 final String value;
116 if (map == sSecureSettingToTypeMap) {
117 value = Settings.Secure.getString(context.getContentResolver(), setting);
118 } else if (map == sSystemSettingToTypeMap) {
119 value = Settings.System.getString(context.getContentResolver(), setting);
120 } else {
121 value = Settings.Global.getString(context.getContentResolver(), setting);
122 }
123 if (value == null) {
124 continue;
125 }
Svetoslav Ganov54d068e2011-03-02 12:58:40 -0800126 Class<?> type = entry.getValue();
Dianne Hackbornb3d4cb32015-01-09 09:54:06 -0800127 if (type == String.class) {
Dianne Hackbornb3d4cb32015-01-09 09:54:06 -0800128 snapshot.putString(setting, value);
129 } else if (type == int.class) {
Sudheer Shankabce47222017-04-03 10:51:42 -0700130 snapshot.putInt(setting, Integer.parseInt(value));
Dianne Hackbornb3d4cb32015-01-09 09:54:06 -0800131 } else if (type == float.class) {
Sudheer Shankabce47222017-04-03 10:51:42 -0700132 snapshot.putFloat(setting, Float.parseFloat(value));
Dianne Hackbornb3d4cb32015-01-09 09:54:06 -0800133 } else if (type == long.class) {
Sudheer Shankabce47222017-04-03 10:51:42 -0700134 snapshot.putLong(setting, Long.parseLong(value));
Svetoslav Ganov54d068e2011-03-02 12:58:40 -0800135 }
136 }
137 }
138}