summaryrefslogtreecommitdiffstats
path: root/services/core/java/com/android/server/am/CoreSettingsObserver.java
blob: 10ea67c34465f57a3b6b048947a6f3adff9bf7f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
 * Copyright (C) 2006-2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.server.am;

import android.content.Context;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.util.Log;

import java.util.HashMap;
import java.util.Map;

/**
 * Helper class for watching a set of core settings which the framework
 * propagates to application processes to avoid multiple lookups and potentially
 * disk I/O operations. Note: This class assumes that all core settings reside
 * in {@link Settings.Secure}.
 */
final class CoreSettingsObserver extends ContentObserver {
    private static final String LOG_TAG = CoreSettingsObserver.class.getSimpleName();

    // mapping form property name to its type
    private static final Map<String, Class<?>> sCoreSettingToTypeMap = new HashMap<
            String, Class<?>>();
    static {
        sCoreSettingToTypeMap.put(Settings.Secure.LONG_PRESS_TIMEOUT, int.class);
        // add other core settings here...
    }

    private final Bundle mCoreSettings = new Bundle();

    private final ActivityManagerService mActivityManagerService;

    public CoreSettingsObserver(ActivityManagerService activityManagerService) {
        super(activityManagerService.mHandler);
        mActivityManagerService = activityManagerService;
        beginObserveCoreSettings();
        sendCoreSettings();
    }

    public Bundle getCoreSettingsLocked() {
        return (Bundle) mCoreSettings.clone();
    }

    @Override
    public void onChange(boolean selfChange) {
        synchronized (mActivityManagerService) {
            sendCoreSettings();
        }
    }

    private void sendCoreSettings() {
        populateCoreSettings(mCoreSettings);
        mActivityManagerService.onCoreSettingsChange(mCoreSettings);
    }

    private void beginObserveCoreSettings() {
        for (String setting : sCoreSettingToTypeMap.keySet()) {
            Uri uri = Settings.Secure.getUriFor(setting);
            mActivityManagerService.mContext.getContentResolver().registerContentObserver(
                    uri, false, this);
        }
    }

    private void populateCoreSettings(Bundle snapshot) {
        Context context = mActivityManagerService.mContext;
        for (Map.Entry<String, Class<?>> entry : sCoreSettingToTypeMap.entrySet()) {
            String setting = entry.getKey();
            Class<?> type = entry.getValue();
            try {
                if (type == String.class) {
                    String value = Settings.Secure.getString(context.getContentResolver(),
                            setting);
                    snapshot.putString(setting, value);
                } else if (type == int.class) {
                    int value = Settings.Secure.getInt(context.getContentResolver(),
                            setting);
                    snapshot.putInt(setting, value);
                } else if (type == float.class) {
                    float value = Settings.Secure.getFloat(context.getContentResolver(),
                            setting);
                    snapshot.putFloat(setting, value);
                } else if (type == long.class) {
                    long value = Settings.Secure.getLong(context.getContentResolver(),
                            setting);
                    snapshot.putLong(setting, value);
                }
            } catch (SettingNotFoundException snfe) {
                Log.w(LOG_TAG, "Cannot find setting \"" + setting + "\"", snfe);
            }
        }
    }
}