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