1/*
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;
24
25import com.android.internal.annotations.VisibleForTesting;
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 */
36final class CoreSettingsObserver extends ContentObserver {
37    private static final String LOG_TAG = CoreSettingsObserver.class.getSimpleName();
38
39    // mapping form property name to its type
40    @VisibleForTesting
41    static final Map<String, Class<?>> sSecureSettingToTypeMap = new HashMap<
42            String, Class<?>>();
43    @VisibleForTesting
44    static final Map<String, Class<?>> sSystemSettingToTypeMap = new HashMap<
45            String, Class<?>>();
46    @VisibleForTesting
47    static final Map<String, Class<?>> sGlobalSettingToTypeMap = new HashMap<
48            String, Class<?>>();
49    static {
50        sSecureSettingToTypeMap.put(Settings.Secure.LONG_PRESS_TIMEOUT, int.class);
51        sSecureSettingToTypeMap.put(Settings.Secure.MULTI_PRESS_TIMEOUT, int.class);
52        // add other secure settings here...
53
54        sSystemSettingToTypeMap.put(Settings.System.TIME_12_24, String.class);
55        // add other system settings here...
56
57        sGlobalSettingToTypeMap.put(Settings.Global.DEBUG_VIEW_ATTRIBUTES, int.class);
58        // add other global settings here...
59    }
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();
69        sendCoreSettings();
70    }
71
72    public Bundle getCoreSettingsLocked() {
73        return (Bundle) mCoreSettings.clone();
74    }
75
76    @Override
77    public void onChange(boolean selfChange) {
78        synchronized (mActivityManagerService) {
79            sendCoreSettings();
80        }
81    }
82
83    private void sendCoreSettings() {
84        populateSettings(mCoreSettings, sSecureSettingToTypeMap);
85        populateSettings(mCoreSettings, sSystemSettingToTypeMap);
86        populateSettings(mCoreSettings, sGlobalSettingToTypeMap);
87        mActivityManagerService.onCoreSettingsChange(mCoreSettings);
88    }
89
90    private void beginObserveCoreSettings() {
91        for (String setting : sSecureSettingToTypeMap.keySet()) {
92            Uri uri = Settings.Secure.getUriFor(setting);
93            mActivityManagerService.mContext.getContentResolver().registerContentObserver(
94                    uri, false, this);
95        }
96
97        for (String setting : sSystemSettingToTypeMap.keySet()) {
98            Uri uri = Settings.System.getUriFor(setting);
99            mActivityManagerService.mContext.getContentResolver().registerContentObserver(
100                    uri, false, this);
101        }
102
103        for (String setting : sGlobalSettingToTypeMap.keySet()) {
104            Uri uri = Settings.Global.getUriFor(setting);
105            mActivityManagerService.mContext.getContentResolver().registerContentObserver(
106                    uri, false, this);
107        }
108    }
109
110    @VisibleForTesting
111    void populateSettings(Bundle snapshot, Map<String, Class<?>> map) {
112        Context context = mActivityManagerService.mContext;
113        for (Map.Entry<String, Class<?>> entry : map.entrySet()) {
114            String setting = entry.getKey();
115            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            }
126            Class<?> type = entry.getValue();
127            if (type == String.class) {
128                snapshot.putString(setting, value);
129            } else if (type == int.class) {
130                snapshot.putInt(setting, Integer.parseInt(value));
131            } else if (type == float.class) {
132                snapshot.putFloat(setting, Float.parseFloat(value));
133            } else if (type == long.class) {
134                snapshot.putLong(setting, Long.parseLong(value));
135            }
136        }
137    }
138}
139