ComboPreferences.java revision 450254a7c9b30e4f36429b46895c944458f59962
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    // The following two methods are not used.
120    public Editor putStringSet(String key, Set<String> values) {
121        throw new UnsupportedOperationException();
122    }
123
124    public Set<String> getStringSet(String key, Set<String> defValues) {
125        throw new UnsupportedOperationException();
126    }
127
128    public boolean contains(String key) {
129        if (mPrefLocal.contains(key)) return true;
130        if (mPrefGlobal.contains(key)) return true;
131        return false;
132    }
133
134    private class MyEditor implements Editor {
135        private Editor mEditorGlobal;
136        private Editor mEditorLocal;
137
138        MyEditor() {
139            mEditorGlobal = mPrefGlobal.edit();
140            mEditorLocal = mPrefLocal.edit();
141        }
142
143        public boolean commit() {
144            boolean result1 = mEditorGlobal.commit();
145            boolean result2 = mEditorLocal.commit();
146            return result1 && result2;
147        }
148
149        // Note: clear() and remove() affects both local and global preferences.
150        public Editor clear() {
151            mEditorGlobal.clear();
152            mEditorLocal.clear();
153            return this;
154        }
155
156        public Editor remove(String key) {
157            mEditorGlobal.remove(key);
158            mEditorLocal.remove(key);
159            return this;
160        }
161
162        public Editor putString(String key, String value) {
163            if (isGlobal(key)) {
164                mEditorGlobal.putString(key, value);
165            } else {
166                mEditorLocal.putString(key, value);
167            }
168            return this;
169        }
170
171        public Editor putInt(String key, int value) {
172            if (isGlobal(key)) {
173                mEditorGlobal.putInt(key, value);
174            } else {
175                mEditorLocal.putInt(key, value);
176            }
177            return this;
178        }
179
180        public Editor putLong(String key, long value) {
181            if (isGlobal(key)) {
182                mEditorGlobal.putLong(key, value);
183            } else {
184                mEditorLocal.putLong(key, value);
185            }
186            return this;
187        }
188
189        public Editor putFloat(String key, float value) {
190            if (isGlobal(key)) {
191                mEditorGlobal.putFloat(key, value);
192            } else {
193                mEditorLocal.putFloat(key, value);
194            }
195            return this;
196        }
197
198        public Editor putBoolean(String key, boolean value) {
199            if (isGlobal(key)) {
200                mEditorGlobal.putBoolean(key, value);
201            } else {
202                mEditorLocal.putBoolean(key, value);
203            }
204            return this;
205        }
206    }
207
208    // Note the remove() and clear() of the returned Editor may not work as
209    // expected because it doesn't touch the global preferences at all.
210    public Editor edit() {
211        return new MyEditor();
212    }
213
214    public void registerOnSharedPreferenceChangeListener(
215            OnSharedPreferenceChangeListener listener) {
216        mListeners.add(listener);
217    }
218
219    public void unregisterOnSharedPreferenceChangeListener(
220            OnSharedPreferenceChangeListener listener) {
221        mListeners.remove(listener);
222    }
223
224    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
225            String key) {
226        for (OnSharedPreferenceChangeListener listener : mListeners) {
227            listener.onSharedPreferenceChanged(this, key);
228        }
229    }
230}
231