blob: 10ea67c34465f57a3b6b048947a6f3adff9bf7f3 [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;
24import android.provider.Settings.SettingNotFoundException;
25import android.util.Log;
26
27import 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
40 private static final Map<String, Class<?>> sCoreSettingToTypeMap = new HashMap<
41 String, Class<?>>();
42 static {
43 sCoreSettingToTypeMap.put(Settings.Secure.LONG_PRESS_TIMEOUT, int.class);
44 // add other core settings here...
45 }
46
47 private final Bundle mCoreSettings = new Bundle();
48
49 private final ActivityManagerService mActivityManagerService;
50
51 public CoreSettingsObserver(ActivityManagerService activityManagerService) {
52 super(activityManagerService.mHandler);
53 mActivityManagerService = activityManagerService;
54 beginObserveCoreSettings();
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -080055 sendCoreSettings();
Svetoslav Ganov54d068e2011-03-02 12:58:40 -080056 }
57
58 public Bundle getCoreSettingsLocked() {
59 return (Bundle) mCoreSettings.clone();
60 }
61
62 @Override
63 public void onChange(boolean selfChange) {
64 synchronized (mActivityManagerService) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -080065 sendCoreSettings();
Svetoslav Ganov54d068e2011-03-02 12:58:40 -080066 }
67 }
68
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -080069 private void sendCoreSettings() {
70 populateCoreSettings(mCoreSettings);
71 mActivityManagerService.onCoreSettingsChange(mCoreSettings);
72 }
73
Svetoslav Ganov54d068e2011-03-02 12:58:40 -080074 private void beginObserveCoreSettings() {
75 for (String setting : sCoreSettingToTypeMap.keySet()) {
76 Uri uri = Settings.Secure.getUriFor(setting);
77 mActivityManagerService.mContext.getContentResolver().registerContentObserver(
78 uri, false, this);
79 }
80 }
81
82 private void populateCoreSettings(Bundle snapshot) {
83 Context context = mActivityManagerService.mContext;
84 for (Map.Entry<String, Class<?>> entry : sCoreSettingToTypeMap.entrySet()) {
85 String setting = entry.getKey();
86 Class<?> type = entry.getValue();
87 try {
88 if (type == String.class) {
89 String value = Settings.Secure.getString(context.getContentResolver(),
90 setting);
91 snapshot.putString(setting, value);
92 } else if (type == int.class) {
93 int value = Settings.Secure.getInt(context.getContentResolver(),
94 setting);
95 snapshot.putInt(setting, value);
96 } else if (type == float.class) {
97 float value = Settings.Secure.getFloat(context.getContentResolver(),
98 setting);
99 snapshot.putFloat(setting, value);
100 } else if (type == long.class) {
101 long value = Settings.Secure.getLong(context.getContentResolver(),
102 setting);
103 snapshot.putLong(setting, value);
104 }
105 } catch (SettingNotFoundException snfe) {
106 Log.w(LOG_TAG, "Cannot find setting \"" + setting + "\"", snfe);
107 }
108 }
109 }
110}