2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.camera;
19 import android.content.Context;
20 import android.content.SharedPreferences;
21 import android.content.SharedPreferences.Editor;
22 import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
23 import android.preference.PreferenceManager;
27 import java.util.WeakHashMap;
28 import java.util.concurrent.CopyOnWriteArrayList;
30 public 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>();
37 public ComboPreferences(Context context) {
38 mPrefGlobal = PreferenceManager.getDefaultSharedPreferences(context);
39 mPrefGlobal.registerOnSharedPreferenceChangeListener(this);
41 sMap.put(context, this);
43 mListeners = new CopyOnWriteArrayList<OnSharedPreferenceChangeListener>();
46 public static ComboPreferences get(Context context) {
48 return sMap.get(context);
52 public void setLocalId(Context context, int cameraId) {
53 String prefName = context.getPackageName() + "_preferences_" + cameraId;
54 if (mPrefLocal != null) {
55 mPrefLocal.unregisterOnSharedPreferenceChangeListener(this);
57 mPrefLocal = context.getSharedPreferences(
58 prefName, Context.MODE_PRIVATE);
59 mPrefLocal.registerOnSharedPreferenceChangeListener(this);
62 public SharedPreferences getGlobal() {
66 public SharedPreferences getLocal() {
70 public Map<String, ?> getAll() {
71 throw new UnsupportedOperationException(); // Can be implemented if needed.
74 private static boolean isGlobal(String key) {
75 return key.equals(CameraSettings.KEY_CAMERA_ID)
76 || key.equals(CameraSettings.KEY_RECORD_LOCATION);
79 public String getString(String key, String defValue) {
80 if (isGlobal(key) || !mPrefLocal.contains(key)) {
81 return mPrefGlobal.getString(key, defValue);
83 return mPrefLocal.getString(key, defValue);
87 public int getInt(String key, int defValue) {
88 if (isGlobal(key) || !mPrefLocal.contains(key)) {
89 return mPrefGlobal.getInt(key, defValue);
91 return mPrefLocal.getInt(key, defValue);
95 public long getLong(String key, long defValue) {
96 if (isGlobal(key) || !mPrefLocal.contains(key)) {
97 return mPrefGlobal.getLong(key, defValue);
99 return mPrefLocal.getLong(key, defValue);
103 public float getFloat(String key, float defValue) {
104 if (isGlobal(key) || !mPrefLocal.contains(key)) {
105 return mPrefGlobal.getFloat(key, defValue);
107 return mPrefLocal.getFloat(key, defValue);
111 public boolean getBoolean(String key, boolean defValue) {
112 if (isGlobal(key) || !mPrefLocal.contains(key)) {
113 return mPrefGlobal.getBoolean(key, defValue);
115 return mPrefLocal.getBoolean(key, defValue);
119 // This method is not used.
120 public Set<String> getStringSet(String key, Set<String> defValues) {
121 throw new UnsupportedOperationException();
124 public boolean contains(String key) {
125 if (mPrefLocal.contains(key)) return true;
126 if (mPrefGlobal.contains(key)) return true;
130 private class MyEditor implements Editor {
131 private Editor mEditorGlobal;
132 private Editor mEditorLocal;
135 mEditorGlobal = mPrefGlobal.edit();
136 mEditorLocal = mPrefLocal.edit();
139 public boolean commit() {
140 boolean result1 = mEditorGlobal.commit();
141 boolean result2 = mEditorLocal.commit();
142 return result1 && result2;
145 public void apply() {
146 mEditorGlobal.apply();
147 mEditorLocal.apply();
150 // Note: clear() and remove() affects both local and global preferences.
151 public Editor clear() {
152 mEditorGlobal.clear();
153 mEditorLocal.clear();
157 public Editor remove(String key) {
158 mEditorGlobal.remove(key);
159 mEditorLocal.remove(key);
163 public Editor putString(String key, String value) {
165 mEditorGlobal.putString(key, value);
167 mEditorLocal.putString(key, value);
172 public Editor putInt(String key, int value) {
174 mEditorGlobal.putInt(key, value);
176 mEditorLocal.putInt(key, value);
181 public Editor putLong(String key, long value) {
183 mEditorGlobal.putLong(key, value);
185 mEditorLocal.putLong(key, value);
190 public Editor putFloat(String key, float value) {
192 mEditorGlobal.putFloat(key, value);
194 mEditorLocal.putFloat(key, value);
199 public Editor putBoolean(String key, boolean value) {
201 mEditorGlobal.putBoolean(key, value);
203 mEditorLocal.putBoolean(key, value);
208 // This method is not used.
209 public Editor putStringSet(String key, Set<String> values) {
210 throw new UnsupportedOperationException();
214 // Note the remove() and clear() of the returned Editor may not work as
215 // expected because it doesn't touch the global preferences at all.
216 public Editor edit() {
217 return new MyEditor();
220 public void registerOnSharedPreferenceChangeListener(
221 OnSharedPreferenceChangeListener listener) {
222 mListeners.add(listener);
225 public void unregisterOnSharedPreferenceChangeListener(
226 OnSharedPreferenceChangeListener listener) {
227 mListeners.remove(listener);
230 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
232 for (OnSharedPreferenceChangeListener listener : mListeners) {
233 listener.onSharedPreferenceChanged(this, key);