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 17 package com.android.server.am; 18 19 import android.content.Context; 20 import android.database.ContentObserver; 21 import android.net.Uri; 22 import android.os.Bundle; 23 import android.provider.Settings; 24 import android.provider.Settings.SettingNotFoundException; 25 import android.util.Log; 26 27 import java.util.HashMap; 28 import 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 */ 36 final class CoreSettingsObserver extends ContentObserver { 37 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(); 55 sendCoreSettings(); 56 } 57 58 public Bundle getCoreSettingsLocked() { 59 return (Bundle) mCoreSettings.clone(); 60 } 61 62 @Override 63 public void onChange(boolean selfChange) { 64 synchronized (mActivityManagerService) { 65 sendCoreSettings(); 66 } 67 } 68 69 private void sendCoreSettings() { 70 populateCoreSettings(mCoreSettings); 71 mActivityManagerService.onCoreSettingsChange(mCoreSettings); 72 } 73 74 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 } 111