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