ComboPreferences.java revision b13acb76f5b86f46dfe07830648ec14524e83d39
1/* 2 * Copyright (C) 2010 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.camera; 18 19import android.content.Context; 20import android.content.SharedPreferences; 21import android.content.SharedPreferences.Editor; 22import android.content.SharedPreferences.OnSharedPreferenceChangeListener; 23import android.preference.PreferenceManager; 24 25import java.util.Map; 26import java.util.Set; 27import java.util.WeakHashMap; 28import java.util.concurrent.CopyOnWriteArrayList; 29 30public class ComboPreferences implements SharedPreferences, OnSharedPreferenceChangeListener { 31 private SharedPreferences mPrefGlobal; // global preferences 32 private SharedPreferences mPrefLocal; // per-camera preferences 33 private CopyOnWriteArrayList<OnSharedPreferenceChangeListener> mListeners; 34 private static WeakHashMap<Context, ComboPreferences> sMap = 35 new WeakHashMap<Context, ComboPreferences>(); 36 37 public ComboPreferences(Context context) { 38 mPrefGlobal = PreferenceManager.getDefaultSharedPreferences(context); 39 mPrefGlobal.registerOnSharedPreferenceChangeListener(this); 40 synchronized (sMap) { 41 sMap.put(context, this); 42 } 43 mListeners = new CopyOnWriteArrayList<OnSharedPreferenceChangeListener>(); 44 } 45 46 public static ComboPreferences get(Context context) { 47 synchronized (sMap) { 48 return sMap.get(context); 49 } 50 } 51 52 public void setLocalId(Context context, int id) { 53 String prefName = context.getPackageName() + "_preferences_" + id; 54 if (mPrefLocal != null) { 55 mPrefLocal.unregisterOnSharedPreferenceChangeListener(this); 56 } 57 mPrefLocal = context.getSharedPreferences( 58 prefName, Context.MODE_PRIVATE); 59 mPrefLocal.registerOnSharedPreferenceChangeListener(this); 60 } 61 62 public SharedPreferences getGlobal() { 63 return mPrefGlobal; 64 } 65 66 public SharedPreferences getLocal() { 67 return mPrefLocal; 68 } 69 70 public Map<String, ?> getAll() { 71 throw new UnsupportedOperationException(); // Can be implemented if needed. 72 } 73 74 private static boolean isGlobal(String key) { 75 return key.equals(CameraSettings.KEY_CAMERA_ID) 76 || key.equals(CameraSettings.KEY_RECORD_LOCATION); 77 } 78 79 public String getString(String key, String defValue) { 80 if (isGlobal(key) || !mPrefLocal.contains(key)) { 81 return mPrefGlobal.getString(key, defValue); 82 } else { 83 return mPrefLocal.getString(key, defValue); 84 } 85 } 86 87 public int getInt(String key, int defValue) { 88 if (isGlobal(key) || !mPrefLocal.contains(key)) { 89 return mPrefGlobal.getInt(key, defValue); 90 } else { 91 return mPrefLocal.getInt(key, defValue); 92 } 93 } 94 95 public long getLong(String key, long defValue) { 96 if (isGlobal(key) || !mPrefLocal.contains(key)) { 97 return mPrefGlobal.getLong(key, defValue); 98 } else { 99 return mPrefLocal.getLong(key, defValue); 100 } 101 } 102 103 public float getFloat(String key, float defValue) { 104 if (isGlobal(key) || !mPrefLocal.contains(key)) { 105 return mPrefGlobal.getFloat(key, defValue); 106 } else { 107 return mPrefLocal.getFloat(key, defValue); 108 } 109 } 110 111 public boolean getBoolean(String key, boolean defValue) { 112 if (isGlobal(key) || !mPrefLocal.contains(key)) { 113 return mPrefGlobal.getBoolean(key, defValue); 114 } else { 115 return mPrefLocal.getBoolean(key, defValue); 116 } 117 } 118 119 // This method is not used. 120 public Set<String> getStringSet(String key, Set<String> defValues) { 121 throw new UnsupportedOperationException(); 122 } 123 124 public boolean contains(String key) { 125 if (mPrefLocal.contains(key)) return true; 126 if (mPrefGlobal.contains(key)) return true; 127 return false; 128 } 129 130 private class MyEditor implements Editor { 131 private Editor mEditorGlobal; 132 private Editor mEditorLocal; 133 134 MyEditor() { 135 mEditorGlobal = mPrefGlobal.edit(); 136 mEditorLocal = mPrefLocal.edit(); 137 } 138 139 public boolean commit() { 140 boolean result1 = mEditorGlobal.commit(); 141 boolean result2 = mEditorLocal.commit(); 142 return result1 && result2; 143 } 144 145 // Note: clear() and remove() affects both local and global preferences. 146 public Editor clear() { 147 mEditorGlobal.clear(); 148 mEditorLocal.clear(); 149 return this; 150 } 151 152 public Editor remove(String key) { 153 mEditorGlobal.remove(key); 154 mEditorLocal.remove(key); 155 return this; 156 } 157 158 public Editor putString(String key, String value) { 159 if (isGlobal(key)) { 160 mEditorGlobal.putString(key, value); 161 } else { 162 mEditorLocal.putString(key, value); 163 } 164 return this; 165 } 166 167 public Editor putInt(String key, int value) { 168 if (isGlobal(key)) { 169 mEditorGlobal.putInt(key, value); 170 } else { 171 mEditorLocal.putInt(key, value); 172 } 173 return this; 174 } 175 176 public Editor putLong(String key, long value) { 177 if (isGlobal(key)) { 178 mEditorGlobal.putLong(key, value); 179 } else { 180 mEditorLocal.putLong(key, value); 181 } 182 return this; 183 } 184 185 public Editor putFloat(String key, float value) { 186 if (isGlobal(key)) { 187 mEditorGlobal.putFloat(key, value); 188 } else { 189 mEditorLocal.putFloat(key, value); 190 } 191 return this; 192 } 193 194 public Editor putBoolean(String key, boolean value) { 195 if (isGlobal(key)) { 196 mEditorGlobal.putBoolean(key, value); 197 } else { 198 mEditorLocal.putBoolean(key, value); 199 } 200 return this; 201 } 202 203 // This method is not used. 204 public Editor putStringSet(String key, Set<String> values) { 205 throw new UnsupportedOperationException(); 206 } 207 } 208 209 // Note the remove() and clear() of the returned Editor may not work as 210 // expected because it doesn't touch the global preferences at all. 211 public Editor edit() { 212 return new MyEditor(); 213 } 214 215 public void registerOnSharedPreferenceChangeListener( 216 OnSharedPreferenceChangeListener listener) { 217 mListeners.add(listener); 218 } 219 220 public void unregisterOnSharedPreferenceChangeListener( 221 OnSharedPreferenceChangeListener listener) { 222 mListeners.remove(listener); 223 } 224 225 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, 226 String key) { 227 for (OnSharedPreferenceChangeListener listener : mListeners) { 228 listener.onSharedPreferenceChanged(this, key); 229 } 230 } 231} 232