OSDN Git Service

Merge "fixing intent ui controls visibility" into gb-ub-photos-denali
[android-x86/packages-apps-Camera2.git] / src / com / android / camera / settings / SettingsManager.java
1 /*
2  * Copyright (C) 2013 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
17 package com.android.camera.settings;
18
19 import android.content.Context;
20 import android.content.SharedPreferences;
21 import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
22 import android.preference.PreferenceManager;
23 import android.util.Log;
24 import android.util.SparseArray;
25
26 import com.android.camera.ListPreference;
27 import com.android.camera.app.AppController;
28 import com.android.camera.util.SettingsHelper;
29 import com.android.camera2.R;
30
31 import java.util.ArrayList;
32 import java.util.List;
33
34 /**
35  * SettingsManager class provides an api for getting and setting both
36  * global and local SharedPreferences.
37  */
38 public class SettingsManager {
39     private static final String TAG = "SettingsManager";
40
41     private final Context mContext;
42     private final SharedPreferences mDefaultSettings;
43     private final SettingsCache mSettingsCache;
44     private SharedPreferences mGlobalSettings;
45     private SharedPreferences mCameraSettings;
46     private final SparseArray<SharedPreferences> mModuleSettings
47         = new SparseArray<SharedPreferences>();
48     private SettingsCapabilities mCapabilities;
49
50     private int mCameraId = -1;
51     private AppController mAppController;
52
53     /**
54      * A List of OnSettingChangedListener's, maintained
55      * to compare to new listeners and prevent duplicate
56      * registering.
57      */
58     private final List<OnSettingChangedListener>
59         mListeners = new ArrayList<OnSettingChangedListener>();
60
61     /**
62      * A List of OnSharedPreferenceChangeListener's, maintained
63      * to hold pointers to actually registered listeners,
64      * so they can be unregistered.
65      */
66     private final List<OnSharedPreferenceChangeListener>
67         mSharedPreferenceListeners = new ArrayList<OnSharedPreferenceChangeListener>();
68
69     public SettingsManager(Context context, AppController app, int nCameras) {
70         mContext = context;
71         mAppController = app;
72
73         SettingsCache.ExtraSettings extraSettings = new SettingsHelper();
74         mSettingsCache = new SettingsCache(mContext, extraSettings);
75
76         mDefaultSettings = PreferenceManager.getDefaultSharedPreferences(context);
77         initGlobal();
78     }
79
80     /**
81      * Initialize global SharedPreferences.
82      */
83     private void initGlobal() {
84         String globalKey = mContext.getPackageName() + "_preferences_camera";
85         mGlobalSettings = mContext.getSharedPreferences(globalKey, Context.MODE_PRIVATE);
86     }
87
88     /**
89      * Load and cache a module specific SharedPreferences.
90      */
91     public SharedPreferences getModulePreferences(int modeIndex) {
92         SharedPreferences sharedPreferences = mModuleSettings.get(modeIndex);
93         if (sharedPreferences == null) {
94             String moduleKey = mContext.getPackageName() + "_preferences_module_" + modeIndex;
95             sharedPreferences = mContext.getSharedPreferences(moduleKey, Context.MODE_PRIVATE);
96             mModuleSettings.put(modeIndex, sharedPreferences);
97         }
98         return sharedPreferences;
99     }
100
101     /**
102      * Initialize SharedPreferences for other cameras.
103      */
104     public void changeCamera(int cameraId, SettingsCapabilities capabilities) {
105         mCapabilities = capabilities;
106         mSettingsCache.setCapabilities(mCapabilities);
107
108         if (cameraId == mCameraId) {
109             return;
110         }
111
112         // We've changed camera ids, that means we need to flush the
113         // settings cache of settings dependent on SettingsCapabilities.
114         mSettingsCache.flush();
115
116         // Cache the camera id so we don't need to reload preferences
117         // if we're using the same camera.
118         mCameraId = cameraId;
119
120         String cameraKey = mContext.getPackageName() + "_preferences_" + cameraId;
121         mCameraSettings = mContext.getSharedPreferences(
122             cameraKey, Context.MODE_PRIVATE);
123
124         for (OnSharedPreferenceChangeListener listener : mSharedPreferenceListeners) {
125             mCameraSettings.registerOnSharedPreferenceChangeListener(listener);
126         }
127     }
128
129     /**
130      * Interface with Camera Parameters and Modules.
131      */
132     public interface OnSettingChangedListener {
133         /**
134          * Called every time a SharedPreference has been changed.
135          */
136         public void onSettingChanged(SettingsManager settingsManager, int setting);
137     }
138
139     private OnSharedPreferenceChangeListener getSharedPreferenceListener(
140             final OnSettingChangedListener listener) {
141         return new OnSharedPreferenceChangeListener() {
142                 @Override
143                 public void onSharedPreferenceChanged(
144                         SharedPreferences sharedPreferences, String key) {
145                     Integer settingId = mSettingsCache.getId(key);
146                     if (settingId != null) {
147                         listener.onSettingChanged(SettingsManager.this, settingId);
148                     } else {
149                         Log.w(TAG, "Setting id from key=" + key + " is null");
150                     }
151                 }
152             };
153     }
154
155     /**
156      * Add an OnSettingChangedListener to the SettingsManager, which will execute
157      * onSettingsChanged when any SharedPreference has been updated.
158      */
159     public void addListener(final OnSettingChangedListener listener) {
160         if (listener == null) {
161             throw new IllegalArgumentException("OnSettingChangedListener cannot be null.");
162         }
163
164         if (mListeners.contains(listener)) {
165             return;
166         }
167
168         mListeners.add(listener);
169         OnSharedPreferenceChangeListener sharedPreferenceListener =
170             getSharedPreferenceListener(listener);
171         mSharedPreferenceListeners.add(sharedPreferenceListener);
172
173         if (mGlobalSettings != null) {
174             mGlobalSettings.registerOnSharedPreferenceChangeListener(sharedPreferenceListener);
175         }
176
177         if (mCameraSettings != null) {
178             mCameraSettings.registerOnSharedPreferenceChangeListener(sharedPreferenceListener);
179         }
180
181         if (mDefaultSettings != null) {
182             mDefaultSettings.registerOnSharedPreferenceChangeListener(sharedPreferenceListener);
183         }
184     }
185
186     /**
187      * Remove a specific SettingsListener.
188      * This should be done in onPause if a listener has been set.
189      */
190     public void removeListener(OnSettingChangedListener listener) {
191         if (listener == null) {
192             throw new IllegalArgumentException();
193         }
194
195         if (!mListeners.contains(listener)) {
196             return;
197         }
198
199         int index = mListeners.indexOf(listener);
200         mListeners.remove(listener);
201
202         // Get the reference to the actual OnSharedPreferenceChangeListener
203         // that was registered.
204         OnSharedPreferenceChangeListener sharedPreferenceListener =
205             mSharedPreferenceListeners.get(index);
206         mSharedPreferenceListeners.remove(index);
207
208         if (mGlobalSettings != null) {
209             mGlobalSettings.unregisterOnSharedPreferenceChangeListener(
210                 sharedPreferenceListener);
211         }
212
213         if (mCameraSettings != null) {
214             mCameraSettings.unregisterOnSharedPreferenceChangeListener(
215                 sharedPreferenceListener);
216         }
217
218         if (mDefaultSettings != null) {
219             mDefaultSettings.unregisterOnSharedPreferenceChangeListener(
220                 sharedPreferenceListener);
221         }
222     }
223
224     /**
225      * Remove all OnSharedPreferenceChangedListener's.
226      * This should be done in onDestroy.
227      */
228     public void removeAllListeners() {
229         for (OnSharedPreferenceChangeListener listener : mSharedPreferenceListeners) {
230             if (mGlobalSettings != null) {
231                 mGlobalSettings.unregisterOnSharedPreferenceChangeListener(listener);
232             }
233
234             if (mCameraSettings != null) {
235                 mCameraSettings.unregisterOnSharedPreferenceChangeListener(listener);
236             }
237
238             if (mDefaultSettings != null) {
239                 mDefaultSettings.unregisterOnSharedPreferenceChangeListener(listener);
240             }
241         }
242         mSharedPreferenceListeners.clear();
243         mListeners.clear();
244     }
245
246     /**
247      * SettingsCapabilities defines constraints around settings that need to be
248      * queried from external sources, like the camera parameters.
249      *
250      * This interface is camera api agnostic.
251      */
252     public interface SettingsCapabilities {
253         /**
254          * Returns a dynamically calculated list of
255          * exposure values, based on the min and max
256          * exposure compensation supported by the camera device.
257          */
258         public String[] getSupportedExposureValues();
259
260         /**
261          * Returns a list of camera ids based on the number
262          * of cameras available on the device.
263          */
264         public String[] getSupportedCameraIds();
265     }
266
267     /**
268      * Get the camera id for which the SettingsManager has loaded camera
269      * specific preferences.
270      */
271     public int getRegisteredCameraId() {
272         return mCameraId;
273     }
274
275     // Manage individual settings.
276     public static final String VALUE_NONE = "none";
277     public static final String VALUE_ON = "on";
278     public static final String VALUE_OFF = "off";
279
280     public static final String TYPE_STRING = "string";
281     public static final String TYPE_BOOLEAN = "boolean";
282     public static final String TYPE_INTEGER = "integer";
283
284     public static final String SOURCE_DEFAULT = "default";
285     public static final String SOURCE_GLOBAL = "global";
286     public static final String SOURCE_CAMERA = "camera";
287     public static final String SOURCE_MODULE = "module";
288
289     public static final boolean FLUSH_ON = true;
290     public static final boolean FLUSH_OFF = false;
291
292     // For quick lookup from id to Setting.
293     public static final int SETTING_RECORD_LOCATION = 0;
294     public static final int SETTING_VIDEO_QUALITY = 1;
295     public static final int SETTING_VIDEO_TIME_LAPSE_FRAME_INTERVAL = 2;
296     public static final int SETTING_PICTURE_SIZE = 3;
297     public static final int SETTING_JPEG_QUALITY = 4;
298     public static final int SETTING_FOCUS_MODE = 5;
299     public static final int SETTING_FLASH_MODE = 6;
300     public static final int SETTING_VIDEOCAMERA_FLASH_MODE = 7;
301     public static final int SETTING_WHITE_BALANCE = 8;
302     public static final int SETTING_SCENE_MODE = 9;
303     public static final int SETTING_EXPOSURE = 10;
304     public static final int SETTING_TIMER = 11;
305     public static final int SETTING_TIMER_SOUND_EFFECTS = 12;
306     public static final int SETTING_VIDEO_EFFECT = 13;
307     public static final int SETTING_CAMERA_ID = 14;
308     public static final int SETTING_CAMERA_HDR = 15;
309     public static final int SETTING_CAMERA_HDR_PLUS = 16;
310     public static final int SETTING_CAMERA_FIRST_USE_HINT_SHOWN = 17;
311     public static final int SETTING_VIDEO_FIRST_USE_HINT_SHOWN = 18;
312     public static final int SETTING_STARTUP_MODULE_INDEX = 19;
313     public static final int SETTING_SHIMMY_REMAINING_PLAY_TIMES_INDEX = 20;
314     public static final int SETTING_KEY_CAMERA_MODULE_LAST_USED_INDEX = 21;
315     public static final int SETTING_CAMERA_PANO_ORIENTATION = 22;
316     public static final int SETTING_CAMERA_GRID_LINES = 23;
317
318     // Shared preference keys.
319     public static final String KEY_RECORD_LOCATION = "pref_camera_recordlocation_key";
320     public static final String KEY_VIDEO_QUALITY = "pref_video_quality_key";
321     public static final String KEY_VIDEO_TIME_LAPSE_FRAME_INTERVAL =
322         "pref_video_time_lapse_frame_interval_key";
323     public static final String KEY_PICTURE_SIZE = "pref_camera_picturesize_key";
324     public static final String KEY_JPEG_QUALITY = "pref_camera_jpegquality_key";
325     public static final String KEY_FOCUS_MODE = "pref_camera_focusmode_key";
326     public static final String KEY_FLASH_MODE = "pref_camera_flashmode_key";
327     public static final String KEY_VIDEOCAMERA_FLASH_MODE = "pref_camera_video_flashmode_key";
328     public static final String KEY_WHITE_BALANCE = "pref_camera_whitebalance_key";
329     public static final String KEY_SCENE_MODE = "pref_camera_scenemode_key";
330     public static final String KEY_EXPOSURE = "pref_camera_exposure_key";
331     public static final String KEY_TIMER = "pref_camera_timer_key";
332     public static final String KEY_TIMER_SOUND_EFFECTS = "pref_camera_timer_sound_key";
333     public static final String KEY_VIDEO_EFFECT = "pref_video_effect_key";
334     public static final String KEY_CAMERA_ID = "pref_camera_id_key";
335     public static final String KEY_CAMERA_HDR = "pref_camera_hdr_key";
336     public static final String KEY_CAMERA_HDR_PLUS = "pref_camera_hdr_plus_key";
337     public static final String KEY_CAMERA_FIRST_USE_HINT_SHOWN =
338         "pref_camera_first_use_hint_shown_key";
339     public static final String KEY_VIDEO_FIRST_USE_HINT_SHOWN =
340         "pref_video_first_use_hint_shown_key";
341     public static final String KEY_STARTUP_MODULE_INDEX = "camera.startup_module";
342     public static final String KEY_SHIMMY_REMAINING_PLAY_TIMES =
343             "pref_shimmy_remaining_play_times";
344     public static final String KEY_CAMERA_MODULE_LAST_USED =
345             "pref_camera_module_last_used_index";
346     public static final String KEY_CAMERA_PANO_ORIENTATION = "pref_camera_pano_orientation";
347     public static final String KEY_CAMERA_GRID_LINES = "pref_camera_grid_lines";
348
349     public static final int WHITE_BALANCE_DEFAULT_INDEX = 2;
350
351
352     /**
353      * Defines a simple class for holding a the spec of a setting.
354      * This spec is used by the generic api methods to query and
355      * update a setting.
356      */
357     public static class Setting {
358         private final String mSource;
359         private final String mType;
360         private final String mDefault;
361         private final String mKey;
362         private final String[] mValues;
363         private final boolean mFlushOnCameraChange;
364
365         /**
366          * A constructor used to store a setting's profile.
367          */
368         Setting(String source, String type, String defaultValue, String key,
369                 String[] values, boolean flushOnCameraChange) {
370             mSource = source;
371             mType = type;
372             mDefault = defaultValue;
373             mKey = key;
374             mValues = values;
375             mFlushOnCameraChange = flushOnCameraChange;
376         }
377
378         /**
379          * Returns the id of a SharedPreferences instance from which
380          * this Setting may be found.
381          * Possible values are {@link #SOURCE_DEFAULT}, {@link #SOURCE_GLOBAL},
382          * {@link #SOURCE_CAMERA}.
383          */
384         public String getSource() {
385             return mSource;
386         }
387
388         /**
389          * Returns the type of the setting stored in SharedPreferences.
390          * Possible values are {@link #TYPE_STRING}, {@link #TYPE_INTEGER},
391          * {@link #TYPE_BOOLEAN}.
392          */
393         public String getType() {
394             return mType;
395         }
396
397         /**
398          * Returns the default value of this setting.
399          */
400         public String getDefault() {
401             return mDefault;
402         }
403
404         /**
405          * Returns the SharedPreferences key for this setting.
406          */
407         public String getKey() {
408             return mKey;
409         }
410
411         /**
412          * Returns an array of possible String values for this setting.
413          * If this setting is not of type {@link #TYPE_STRING}, or
414          * it's not possible to generate the string values, this should
415          * return null;
416          */
417         public String[] getStringValues() {
418             return mValues;
419         }
420
421         /**
422          * Returns whether the setting should be flushed from the cache
423          * when the camera device has changed.
424          */
425         public boolean isFlushedOnCameraChanged() {
426             return mFlushOnCameraChange;
427         }
428     }
429
430     /**
431      * Get the SharedPreferences needed to query/update the setting.
432      */
433     public SharedPreferences getSettingSource(Setting setting) {
434         String source = setting.getSource();
435         if (source.equals(SOURCE_DEFAULT)) {
436             return mDefaultSettings;
437         }
438         if (source.equals(SOURCE_GLOBAL)) {
439             return mGlobalSettings;
440         }
441         if (source.equals(SOURCE_CAMERA)) {
442             return mCameraSettings;
443         }
444         if (source.equals(SOURCE_MODULE)) {
445             int modeIndex = mAppController.getCurrentModuleIndex();
446             return getModulePreferences(modeIndex);
447         }
448         return null;
449     }
450
451     /**
452      * Based on Setting id, finds the index of a Setting's
453      * String value in an array of possible String values.
454      *
455      * If the Setting is not of type String, this returns -1.
456      *
457      * <p>TODO: make this a supported api call for all types.</p>
458      */
459     public int getStringValueIndex(int id) {
460         Setting setting = mSettingsCache.get(id);
461         if (setting == null || !TYPE_STRING.equals(setting.getType())) {
462             return -1;
463         }
464
465         String value = get(id);
466         if (value != null) {
467             String[] possibleValues = setting.getStringValues();
468             if (possibleValues != null) {
469                 for (int i = 0; i < possibleValues.length; i++) {
470                     if (value.equals(possibleValues[i])) {
471                         return i;
472                     }
473                 }
474             }
475         }
476         return -1;
477     }
478
479     /**
480      * Based on Setting id, sets a Setting's String value using the
481      * index into an array of possible String values.
482      *
483      * Fails to set a value if the index is out of bounds or the Setting
484      * is not of type String.
485      *
486      * @return Whether the value was set.
487      */
488     public boolean setStringValueIndex(int id, int index) {
489         Setting setting = mSettingsCache.get(id);
490         if (setting == null || setting.getType() != TYPE_STRING) {
491             return false;
492         }
493
494         String[] possibleValues = setting.getStringValues();
495         if (possibleValues != null) {
496             if (index >= 0 && index < possibleValues.length) {
497                 set(id, possibleValues[index]);
498                 return true;
499             }
500         }
501         return false;
502     }
503
504     /**
505      * Get a Setting's String value based on Setting id.
506      */
507     // TODO: rename to something more descriptive.
508     public String get(int id) {
509         Setting setting = mSettingsCache.get(id);
510         SharedPreferences preferences = getSettingSource(setting);
511         if (preferences != null) {
512             return preferences.getString(setting.getKey(), setting.getDefault());
513         } else {
514             return null;
515         }
516     }
517
518     /**
519      * Get a Setting's boolean value based on Setting id.
520      */
521     public boolean getBoolean(int id) {
522         Setting setting = mSettingsCache.get(id);
523         SharedPreferences preferences = getSettingSource(setting);
524         boolean defaultValue = setting.getDefault().equals(VALUE_ON);
525         if (preferences != null) {
526             return preferences.getBoolean(setting.getKey(), defaultValue);
527         } else {
528             return defaultValue;
529         }
530     }
531
532     /**
533      * Get a Setting's int value based on Setting id.
534      */
535     public int getInt(int id) {
536         Setting setting = mSettingsCache.get(id);
537         SharedPreferences preferences = getSettingSource(setting);
538         int defaultValue = Integer.parseInt(setting.getDefault());
539         if (preferences != null) {
540             return preferences.getInt(setting.getKey(), defaultValue);
541         } else {
542             return defaultValue;
543         }
544     }
545
546     /**
547      * Set a Setting with a String value based on Setting id.
548      */
549     // TODO: rename to something more descriptive.
550     public void set(int id, String value) {
551         Setting setting = mSettingsCache.get(id);
552         SharedPreferences preferences = getSettingSource(setting);
553         if (preferences != null) {
554             preferences.edit().putString(setting.getKey(), value).apply();
555         }
556     }
557
558     /**
559      * Set a Setting with a boolean value based on Setting id.
560      */
561     public void setBoolean(int id, boolean value) {
562         Setting setting = mSettingsCache.get(id);
563         SharedPreferences preferences = getSettingSource(setting);
564         if (preferences != null) {
565             preferences.edit().putBoolean(setting.getKey(), value).apply();
566         }
567     }
568
569     /**
570      * Set a Setting with an int value based on Setting id.
571      */
572     public void setInt(int id, int value) {
573         Setting setting = mSettingsCache.get(id);
574         SharedPreferences preferences = getSettingSource(setting);
575         if (preferences != null) {
576             preferences.edit().putInt(setting.getKey(), value).apply();
577         }
578     }
579
580     /**
581      * Check if a Setting has ever been set based on Setting id.
582      */
583     public boolean isSet(int id) {
584         Setting setting = mSettingsCache.get(id);
585         SharedPreferences preferences = getSettingSource(setting);
586         if (preferences != null) {
587             return (preferences.getString(setting.getKey(), null) != null);
588         } else {
589             return false;
590         }
591     }
592
593     /**
594      * Set a Setting to its default value based on Setting id.
595      */
596     public void setDefault(int id) {
597         Setting setting = mSettingsCache.get(id);
598         SharedPreferences preferences = getSettingSource(setting);
599         if (preferences != null) {
600             preferences.edit().putString(setting.getKey(), setting.getDefault());
601         }
602     }
603
604     /**
605      * Check if a Setting is set to its default value.
606      */
607     public boolean isDefault(int id) {
608         Setting setting = mSettingsCache.get(id);
609         SharedPreferences preferences = getSettingSource(setting);
610         if (preferences != null) {
611             String type = setting.getType();
612             if (TYPE_STRING.equals(type)) {
613                 String value = get(id);
614                 return (value.equals(setting.getDefault()));
615             } else if (TYPE_BOOLEAN.equals(type)) {
616                 boolean value = getBoolean(id);
617                 boolean defaultValue = VALUE_ON.equals(setting.getDefault());
618                 return (value == defaultValue);
619             } else if (TYPE_INTEGER.equals(type)) {
620                 int value = getInt(id);
621                 int defaultValue = Integer.parseInt(setting.getDefault());
622                 return (value == defaultValue);
623             } else {
624                 throw new IllegalArgumentException("Type " + type + " is not known.");
625             }
626         } else {
627             return false;
628         }
629     }
630
631     public static Setting getLocationSetting(Context context) {
632         String defaultValue = context.getString(R.string.setting_none_value);
633         String[] values = context.getResources().getStringArray(
634             R.array.pref_camera_recordlocation_entryvalues);
635         return new Setting(SOURCE_GLOBAL, TYPE_STRING, defaultValue, KEY_RECORD_LOCATION,
636             values, FLUSH_OFF);
637     }
638
639     public static Setting getPictureSizeSetting(Context context) {
640         String defaultValue = null;
641         String[] values = context.getResources().getStringArray(
642             R.array.pref_camera_picturesize_entryvalues);
643         return new Setting(SOURCE_GLOBAL, TYPE_STRING, defaultValue, KEY_PICTURE_SIZE,
644             values, FLUSH_OFF);
645     }
646
647     public static Setting getDefaultCameraIdSetting(Context context,
648             SettingsCapabilities capabilities) {
649         String defaultValue = context.getString(R.string.pref_camera_id_default);
650         String[] values = null;
651         if (capabilities != null) {
652             values = capabilities.getSupportedCameraIds();
653         }
654         return new Setting(SOURCE_MODULE, TYPE_STRING, defaultValue, KEY_CAMERA_ID,
655             values, FLUSH_ON);
656     }
657
658     public static Setting getWhiteBalanceSetting(Context context) {
659         String defaultValue = context.getString(R.string.pref_camera_whitebalance_default);
660         String[] values = context.getResources().getStringArray(
661             R.array.pref_camera_whitebalance_entryvalues);
662         return new Setting(SOURCE_CAMERA, TYPE_STRING, defaultValue, KEY_WHITE_BALANCE,
663             values, FLUSH_OFF);
664     }
665
666     public static Setting getHdrSetting(Context context) {
667         String defaultValue = context.getString(R.string.pref_camera_hdr_default);
668         String[] values = context.getResources().getStringArray(
669             R.array.pref_camera_hdr_entryvalues);
670         return new Setting(SOURCE_GLOBAL, TYPE_STRING, defaultValue, KEY_CAMERA_HDR,
671             values, FLUSH_OFF);
672     }
673
674     public static Setting getHdrPlusSetting(Context context) {
675         String defaultValue = context.getString(R.string.pref_camera_hdr_plus_default);
676         String[] values = context.getResources().getStringArray(
677             R.array.pref_camera_hdr_plus_entryvalues);
678         return new Setting(SOURCE_GLOBAL, TYPE_STRING, defaultValue, KEY_CAMERA_HDR_PLUS,
679             values, FLUSH_OFF);
680     }
681
682     public static Setting getSceneModeSetting(Context context) {
683         String defaultValue = context.getString(R.string.pref_camera_scenemode_default);
684         String[] values = context.getResources().getStringArray(
685             R.array.pref_camera_scenemode_entryvalues);
686         return new Setting(SOURCE_CAMERA, TYPE_STRING, defaultValue, KEY_SCENE_MODE,
687             values, FLUSH_OFF);
688     }
689
690     public static Setting getFlashSetting(Context context) {
691         String defaultValue = context.getString(R.string.pref_camera_flashmode_default);
692         String[] values = context.getResources().getStringArray(
693             R.array.pref_camera_flashmode_entryvalues);
694         return new Setting(SOURCE_CAMERA, TYPE_STRING, defaultValue, KEY_FLASH_MODE,
695             values, FLUSH_OFF);
696     }
697
698     public static Setting getExposureSetting(Context context,
699             SettingsCapabilities capabilities) {
700         String defaultValue = context.getString(R.string.pref_exposure_default);
701         String[] values = null;
702         if (capabilities != null) {
703             values = capabilities.getSupportedExposureValues();
704         }
705         return new Setting(SOURCE_CAMERA, TYPE_STRING, defaultValue, KEY_EXPOSURE,
706             values, FLUSH_ON);
707     }
708
709     public static Setting getHintSetting(Context context) {
710         String defaultValue = context.getString(R.string.setting_on_value);
711         String[] values = null;
712         return new Setting(SOURCE_GLOBAL, TYPE_BOOLEAN, defaultValue,
713             KEY_CAMERA_FIRST_USE_HINT_SHOWN, values, FLUSH_OFF);
714     }
715
716     public static Setting getFocusModeSetting(Context context) {
717         String defaultValue = null;
718         String[] values = context.getResources().getStringArray(
719             R.array.pref_camera_focusmode_entryvalues);
720         return new Setting(SOURCE_CAMERA, TYPE_STRING, defaultValue, KEY_FOCUS_MODE,
721             values, FLUSH_OFF);
722     }
723
724     public static Setting getTimerSetting(Context context) {
725         String defaultValue = context.getString(R.string.pref_camera_timer_default);
726         String[] values = null; // TODO: get the values dynamically.
727         return new Setting(SOURCE_GLOBAL, TYPE_STRING, defaultValue, KEY_TIMER,
728             values, FLUSH_OFF);
729     }
730
731     public static Setting getTimerSoundSetting(Context context) {
732         String defaultValue = context.getString(R.string.pref_camera_timer_sound_default);
733         String[] values = context.getResources().getStringArray(
734             R.array.pref_camera_timer_sound_entryvalues);
735         return new Setting(SOURCE_GLOBAL, TYPE_STRING, defaultValue, KEY_TIMER_SOUND_EFFECTS,
736             values, FLUSH_OFF);
737     }
738
739     public static Setting getVideoQualitySetting(Context context) {
740         String defaultValue = context.getString(R.string.pref_video_quality_default);
741         String[] values = context.getResources().getStringArray(
742             R.array.pref_video_quality_entryvalues);
743         return new Setting(SOURCE_GLOBAL, TYPE_STRING, defaultValue, KEY_VIDEO_QUALITY,
744             values, FLUSH_OFF);
745     }
746
747     public static Setting getTimeLapseFrameIntervalSetting(Context context) {
748         String defaultValue = context.getString(
749             R.string.pref_video_time_lapse_frame_interval_default);
750         String[] values = context.getResources().getStringArray(
751             R.array.pref_video_time_lapse_frame_interval_entryvalues);
752         return new Setting(SOURCE_GLOBAL, TYPE_STRING, defaultValue,
753             KEY_VIDEO_TIME_LAPSE_FRAME_INTERVAL, values, FLUSH_OFF);
754     }
755
756     public static Setting getJpegQualitySetting(Context context) {
757         String defaultValue = context.getString(
758             R.string.pref_camera_jpeg_quality_normal);
759         String[] values = context.getResources().getStringArray(
760             R.array.pref_camera_jpeg_quality_entryvalues);
761         return new Setting(SOURCE_CAMERA, TYPE_STRING, defaultValue, KEY_JPEG_QUALITY,
762             values, FLUSH_OFF);
763     }
764
765     public static Setting getVideoFlashSetting(Context context) {
766         String defaultValue = context.getString(R.string.pref_camera_video_flashmode_default);
767         String[] values = context.getResources().getStringArray(
768             R.array.pref_camera_video_flashmode_entryvalues);
769         return new Setting(SOURCE_CAMERA, TYPE_STRING, defaultValue,
770             KEY_VIDEOCAMERA_FLASH_MODE, values, FLUSH_OFF);
771     }
772
773     public static Setting getVideoEffectSetting(Context context) {
774         String defaultValue = context.getString(R.string.pref_video_effect_default);
775         String[] values = context.getResources().getStringArray(
776             R.array.pref_video_effect_entryvalues);
777         return new Setting(SOURCE_GLOBAL, TYPE_STRING, defaultValue, KEY_VIDEO_EFFECT,
778             values, FLUSH_OFF);
779     }
780
781     public static Setting getHintVideoSetting(Context context) {
782         String defaultValue = context.getString(R.string.setting_on_value);
783         String[] values = null;
784         return new Setting(SOURCE_GLOBAL, TYPE_STRING, defaultValue,
785             KEY_VIDEO_FIRST_USE_HINT_SHOWN, values, FLUSH_OFF);
786     }
787
788     public static Setting getStartupModuleSetting(Context context) {
789         String defaultValue = context.getString(R.string.pref_camera_startup_index_default);
790         String[] values = null;
791         return new Setting(SOURCE_DEFAULT, TYPE_INTEGER, defaultValue,
792             KEY_STARTUP_MODULE_INDEX, values, FLUSH_OFF);
793     }
794
795     public static Setting getShimmyRemainingTimesSetting(Context context) {
796         String defaultValue = context.getString(R.string.pref_shimmy_play_times);
797         return new Setting(SOURCE_DEFAULT, TYPE_INTEGER, defaultValue,
798                 KEY_SHIMMY_REMAINING_PLAY_TIMES, null, FLUSH_OFF);
799     }
800
801     public static Setting getLastUsedCameraModule(Context context) {
802         String defaultValue = Integer.toString(context.getResources()
803                 .getInteger(R.integer.camera_mode_photo));
804         return new Setting(SOURCE_DEFAULT, TYPE_INTEGER, defaultValue,
805                 KEY_CAMERA_MODULE_LAST_USED, null, FLUSH_OFF);
806     }
807
808     public static Setting getPanoOrientationSetting(Context context) {
809         String defaultValue = context.getString(R.string.pano_orientation_horizontal);
810         String[] values = context.getResources().getStringArray(
811             R.array.pref_camera_pano_orientation_entryvalues);
812         return new Setting(SOURCE_GLOBAL, TYPE_STRING, defaultValue,
813             KEY_CAMERA_PANO_ORIENTATION, values, FLUSH_OFF);
814     }
815
816     public static Setting getGridLinesSetting(Context context) {
817         String defaultValue = context.getString(R.string.setting_off_value);
818         String[] values = context.getResources().getStringArray(
819             R.array.pref_camera_gridlines_entryvalues);
820         return new Setting(SOURCE_GLOBAL, TYPE_STRING, defaultValue,
821             KEY_CAMERA_GRID_LINES, values, FLUSH_OFF);
822     }
823
824     // Utilities.
825
826     /**
827      * Returns whether the camera has been set to back facing
828      * in settings.
829      */
830     public boolean isCameraBackFacing() {
831         String cameraFacing = get(SETTING_CAMERA_ID);
832         String backFacing = mContext.getString(R.string.pref_camera_id_default);
833         return (Integer.parseInt(cameraFacing) == Integer.parseInt(backFacing));
834     }
835
836     /**
837      * Returns whether hdr plus mode is set on.
838      */
839     public boolean isHdrPlusOn() {
840         String hdrOn = get(SettingsManager.SETTING_CAMERA_HDR);
841         return hdrOn.equals(SettingsManager.VALUE_ON);
842     }
843
844     /**
845      * Returns whether grid lines are set on.
846      */
847     public boolean areGridLinesOn() {
848         String gridLinesOn = get(SettingsManager.SETTING_CAMERA_GRID_LINES);
849         return gridLinesOn.equals(SettingsManager.VALUE_ON);
850     }
851
852     //TODO: refactor this into a separate utils module.
853
854     /**
855      * Get a String value from first the ListPreference, and if not found
856      * from the SettingsManager.
857      *
858      * This is a wrapper that adds backwards compatibility to views that
859      * rely on PreferenceGroups.
860      */
861     public String getValueFromPreference(ListPreference pref) {
862         String value = pref.getValue();
863         if (value == null) {
864             Integer id = mSettingsCache.getId(pref.getKey());
865             if (id == null) {
866                 return null;
867             }
868             value = get(id);
869         }
870         return value;
871     }
872
873     /**
874      * Set a String value first from the ListPreference, and if unable
875      * from the SettingsManager.
876      *
877      * This is a wrapper that adds backwards compatibility to views that
878      * rely on PreferenceGroups.
879      */
880     public void setValueFromPreference(ListPreference pref, String value) {
881         boolean set = pref.setValue(value);
882         if (!set) {
883             Integer id = mSettingsCache.getId(pref.getKey());
884             if (id != null) {
885                 set(id, value);
886             }
887         }
888     }
889
890     /**
891      * Set a String value first from the ListPreference based on a
892      * ListPreference index, and if unable use the ListPreference key
893      * to set the value using the SettingsManager.
894      *
895      * This is a wrapper that adds backwards compatibility to views that
896      * rely on PreferenceGroups.
897      */
898     public void setValueIndexFromPreference(ListPreference pref, int index) {
899         boolean set = pref.setValueIndex(index);
900         if (!set) {
901             Integer id = mSettingsCache.getId(pref.getKey());
902             if (id != null) {
903                 String value = pref.getValueAtIndex(index);
904                 set(id, value);
905             }
906         }
907     }
908 }