OSDN Git Service

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