OSDN Git Service

Merge "Make flash state sticky on switching to photo mode." 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.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             mSharedPreferenceListeners.remove(listener);
193
194             if (mGlobalSettings != null) {
195                 mGlobalSettings.unregisterOnSharedPreferenceChangeListener(listener);
196             }
197
198             if (mCameraSettings != null) {
199                 mCameraSettings.unregisterOnSharedPreferenceChangeListener(listener);
200             }
201
202             if (mDefaultSettings != null) {
203                 mDefaultSettings.unregisterOnSharedPreferenceChangeListener(listener);
204             }
205         }
206     }
207
208     /**
209      * SettingsCapabilities defines constraints around settings that need to be
210      * queried from external sources, like the camera parameters.
211      *
212      * This interface is camera api agnostic.
213      */
214     public interface SettingsCapabilities {
215         /**
216          * Returns a list of the picture sizes currently
217          * supported by the camera device.
218          */
219         public List<Size> getSupportedPictureSizes();
220
221         /**
222          * Returns a dynamically calculated list of
223          * exposure values, based on the min and max
224          * exposure compensation supported by the camera device.
225          */
226         public String[] getSupportedExposureValues();
227
228         /**
229          * Returns a list of camera ids based on the number
230          * of cameras available on the device.
231          */
232         public String[] getSupportedCameraIds();
233     }
234
235     /**
236      * Exposes SettingsCapabilities functionality.
237      */
238     public List<Size> getSupportedPictureSizes() {
239         if (mCapabilities != null) {
240             return mCapabilities.getSupportedPictureSizes();
241         } else {
242             return null;
243         }
244     }
245
246     /**
247      * Get the camera id for which the SettingsManager has loaded camera
248      * specific preferences.
249      */
250     public int getRegisteredCameraId() {
251         return mCameraId;
252     }
253
254     // Manage individual settings.
255     public static final String VALUE_NONE = "none";
256     public static final String VALUE_ON = "on";
257     public static final String VALUE_OFF = "off";
258
259     public static final String TYPE_STRING = "string";
260     public static final String TYPE_BOOLEAN = "boolean";
261     public static final String TYPE_INTEGER = "integer";
262
263     public static final String SOURCE_DEFAULT = "default";
264     public static final String SOURCE_GLOBAL = "global";
265     public static final String SOURCE_CAMERA = "camera";
266
267     public static final boolean FLUSH_ON = true;
268     public static final boolean FLUSH_OFF = false;
269
270     // For quick lookup from id to Setting.
271     public static final int SETTING_RECORD_LOCATION = 0;
272     public static final int SETTING_VIDEO_QUALITY = 1;
273     public static final int SETTING_VIDEO_TIME_LAPSE_FRAME_INTERVAL = 2;
274     public static final int SETTING_PICTURE_SIZE = 3;
275     public static final int SETTING_JPEG_QUALITY = 4;
276     public static final int SETTING_FOCUS_MODE = 5;
277     public static final int SETTING_FLASH_MODE = 6;
278     public static final int SETTING_VIDEOCAMERA_FLASH_MODE = 7;
279     public static final int SETTING_WHITE_BALANCE = 8;
280     public static final int SETTING_SCENE_MODE = 9;
281     public static final int SETTING_EXPOSURE = 10;
282     public static final int SETTING_TIMER = 11;
283     public static final int SETTING_TIMER_SOUND_EFFECTS = 12;
284     public static final int SETTING_VIDEO_EFFECT = 13;
285     public static final int SETTING_CAMERA_ID = 14;
286     public static final int SETTING_CAMERA_HDR = 15;
287     public static final int SETTING_CAMERA_HDR_PLUS = 16;
288     public static final int SETTING_CAMERA_FIRST_USE_HINT_SHOWN = 17;
289     public static final int SETTING_VIDEO_FIRST_USE_HINT_SHOWN = 18;
290     public static final int SETTING_STARTUP_MODULE_INDEX = 19;
291     public static final int SETTING_CAMERA_REFOCUS = 20;
292
293     // Shared preference keys.
294     public static final String KEY_RECORD_LOCATION = "pref_camera_recordlocation_key";
295     public static final String KEY_VIDEO_QUALITY = "pref_video_quality_key";
296     public static final String KEY_VIDEO_TIME_LAPSE_FRAME_INTERVAL =
297         "pref_video_time_lapse_frame_interval_key";
298     public static final String KEY_PICTURE_SIZE = "pref_camera_picturesize_key";
299     public static final String KEY_JPEG_QUALITY = "pref_camera_jpegquality_key";
300     public static final String KEY_FOCUS_MODE = "pref_camera_focusmode_key";
301     public static final String KEY_FLASH_MODE = "pref_camera_flashmode_key";
302     public static final String KEY_VIDEOCAMERA_FLASH_MODE = "pref_camera_video_flashmode_key";
303     public static final String KEY_WHITE_BALANCE = "pref_camera_whitebalance_key";
304     public static final String KEY_SCENE_MODE = "pref_camera_scenemode_key";
305     public static final String KEY_EXPOSURE = "pref_camera_exposure_key";
306     public static final String KEY_TIMER = "pref_camera_timer_key";
307     public static final String KEY_TIMER_SOUND_EFFECTS = "pref_camera_timer_sound_key";
308     public static final String KEY_VIDEO_EFFECT = "pref_video_effect_key";
309     public static final String KEY_CAMERA_ID = "pref_camera_id_key";
310     public static final String KEY_CAMERA_HDR = "pref_camera_hdr_key";
311     public static final String KEY_CAMERA_HDR_PLUS = "pref_camera_hdr_plus_key";
312     public static final String KEY_CAMERA_FIRST_USE_HINT_SHOWN =
313         "pref_camera_first_use_hint_shown_key";
314     public static final String KEY_VIDEO_FIRST_USE_HINT_SHOWN =
315         "pref_video_first_use_hint_shown_key";
316     public static final String KEY_STARTUP_MODULE_INDEX = "camera.startup_module";
317     public static final String KEY_CAMERA_REFOCUS = "pref_camera_refocus";
318
319     public static final int WHITE_BALANCE_DEFAULT_INDEX = 2;
320
321
322     /**
323      * Defines a simple class for holding a the spec of a setting.
324      * This spec is used by the generic api methods to query and
325      * update a setting.
326      */
327     public static class Setting {
328         private final String mSource;
329         private final String mType;
330         private final String mDefault;
331         private final String mKey;
332         private final String[] mValues;
333         private final boolean mFlushOnCameraChange;
334
335         /**
336          * A constructor used to store a setting's profile.
337          */
338         Setting(String source, String type, String defaultValue, String key,
339                 String[] values, boolean flushOnCameraChange) {
340             mSource = source;
341             mType = type;
342             mDefault = defaultValue;
343             mKey = key;
344             mValues = values;
345             mFlushOnCameraChange = flushOnCameraChange;
346         }
347
348         /**
349          * Returns the id of a SharedPreferences instance from which
350          * this Setting may be found.
351          * Possible values are {@link #SOURCE_DEFAULT}, {@link #SOURCE_GLOBAL},
352          * {@link #SOURCE_CAMERA}.
353          */
354         public String getSource() {
355             return mSource;
356         }
357
358         /**
359          * Returns the type of the setting stored in SharedPreferences.
360          * Possible values are {@link #TYPE_STRING}, {@link #TYPE_INTEGER},
361          * {@link #TYPE_BOOLEAN}.
362          */
363         public String getType() {
364             return mType;
365         }
366
367         /**
368          * Returns the default value of this setting.
369          */
370         public String getDefault() {
371             return mDefault;
372         }
373
374         /**
375          * Returns the SharedPreferences key for this setting.
376          */
377         public String getKey() {
378             return mKey;
379         }
380
381         /**
382          * Returns an array of possible String values for this setting.
383          * If this setting is not of type {@link #TYPE_STRING}, or
384          * it's not possible to generate the string values, this should
385          * return null;
386          */
387         public String[] getStringValues() {
388             return mValues;
389         }
390
391         /**
392          * Returns whether the setting should be flushed from the cache
393          * when the camera device has changed.
394          */
395         public boolean isFlushedOnCameraChanged() {
396             return mFlushOnCameraChange;
397         }
398     }
399
400     /**
401      * Get the SharedPreferences needed to query/update the setting.
402      */
403     public SharedPreferences getSettingSource(Setting setting) {
404         String source = setting.getSource();
405         if (source.equals(SOURCE_DEFAULT)) {
406             return mDefaultSettings;
407         }
408         if (source.equals(SOURCE_GLOBAL)) {
409             return mGlobalSettings;
410         }
411         if (source.equals(SOURCE_CAMERA)) {
412             return mCameraSettings;
413         }
414         return null;
415     }
416
417     /**
418      * Based on Setting id, finds the index of a Setting's
419      * String value in an array of possible String values.
420      *
421      * If the Setting is not of type String, this returns -1.
422      *
423      * <p>TODO: make this a supported api call for all types.</p>
424      */
425     public int getStringValueIndex(int id) {
426         Setting setting = mSettingsCache.get(id);
427         if (setting == null || !TYPE_STRING.equals(setting.getType())) {
428             return -1;
429         }
430
431         String value = get(id);
432         if (value != null) {
433             String[] possibleValues = setting.getStringValues();
434             if (possibleValues != null) {
435                 for (int i = 0; i < possibleValues.length; i++) {
436                     if (value.equals(possibleValues[i])) {
437                         return i;
438                     }
439                 }
440             }
441         }
442         return -1;
443     }
444
445     /**
446      * Based on Setting id, sets a Setting's String value using the
447      * index into an array of possible String values.
448      *
449      * Fails to set a value if the index is out of bounds or the Setting
450      * is not of type String.
451      *
452      * @return Whether the value was set.
453      */
454     public boolean setStringValueIndex(int id, int index) {
455         Setting setting = mSettingsCache.get(id);
456         if (setting == null || setting.getType() != TYPE_STRING) {
457             return false;
458         }
459
460         String[] possibleValues = setting.getStringValues();
461         if (possibleValues != null) {
462             if (index >= 0 && index < possibleValues.length) {
463                 set(id, possibleValues[index]);
464                 return true;
465             }
466         }
467         return false;
468     }
469
470     /**
471      * Get a Setting's String value based on Setting id.
472      */
473     // TODO: rename to something more descriptive.
474     public String get(int id) {
475         Setting setting = mSettingsCache.get(id);
476         SharedPreferences preferences = getSettingSource(setting);
477         if (preferences != null) {
478             return preferences.getString(setting.getKey(), setting.getDefault());
479         } else {
480             return null;
481         }
482     }
483
484     /**
485      * Get a Setting's boolean value based on Setting id.
486      */
487     public boolean getBoolean(int id) {
488         Setting setting = mSettingsCache.get(id);
489         SharedPreferences preferences = getSettingSource(setting);
490         boolean defaultValue = setting.getDefault().equals(VALUE_ON);
491         if (preferences != null) {
492             return preferences.getBoolean(setting.getKey(), defaultValue);
493         } else {
494             return defaultValue;
495         }
496     }
497
498     /**
499      * Get a Setting's int value based on Setting id.
500      */
501     public int getInt(int id) {
502         Setting setting = mSettingsCache.get(id);
503         SharedPreferences preferences = getSettingSource(setting);
504         int defaultValue = Integer.parseInt(setting.getDefault());
505         if (preferences != null) {
506             return preferences.getInt(setting.getKey(), defaultValue);
507         } else {
508             return defaultValue;
509         }
510     }
511
512     /**
513      * Set a Setting with a String value based on Setting id.
514      */
515     // TODO: rename to something more descriptive.
516     public void set(int id, String value) {
517         Setting setting = mSettingsCache.get(id);
518         SharedPreferences preferences = getSettingSource(setting);
519         if (preferences != null) {
520             preferences.edit().putString(setting.getKey(), value).apply();
521         }
522     }
523
524     /**
525      * Set a Setting with a boolean value based on Setting id.
526      */
527     public void setBoolean(int id, boolean value) {
528         Setting setting = mSettingsCache.get(id);
529         SharedPreferences preferences = getSettingSource(setting);
530         if (preferences != null) {
531             preferences.edit().putBoolean(setting.getKey(), value).apply();
532         }
533     }
534
535     /**
536      * Set a Setting with an int value based on Setting id.
537      */
538     public void setInt(int id, int value) {
539         Setting setting = mSettingsCache.get(id);
540         SharedPreferences preferences = getSettingSource(setting);
541         if (preferences != null) {
542             preferences.edit().putInt(setting.getKey(), value).apply();
543         }
544     }
545
546     /**
547      * Check if a Setting has ever been set based on Setting id.
548      */
549     public boolean isSet(int id) {
550         Setting setting = mSettingsCache.get(id);
551         SharedPreferences preferences = getSettingSource(setting);
552         if (preferences != null) {
553             return (preferences.getString(setting.getKey(), null) != null);
554         } else {
555             return false;
556         }
557     }
558
559     /**
560      * Set a Setting to its default value based on Setting id.
561      */
562     public void setDefault(int id) {
563         Setting setting = mSettingsCache.get(id);
564         SharedPreferences preferences = getSettingSource(setting);
565         if (preferences != null) {
566             preferences.edit().putString(setting.getKey(), setting.getDefault());
567         }
568     }
569
570     /**
571      * Check if a Setting is set to its default value.
572      */
573     public boolean isDefault(int id) {
574         Setting setting = mSettingsCache.get(id);
575         SharedPreferences preferences = getSettingSource(setting);
576         if (preferences != null) {
577             String type = setting.getType();
578             if (TYPE_STRING.equals(type)) {
579                 String value = get(id);
580                 return (value.equals(setting.getDefault()));
581             } else if (TYPE_BOOLEAN.equals(type)) {
582                 boolean value = getBoolean(id);
583                 boolean defaultValue = VALUE_ON.equals(setting.getDefault());
584                 return (value == defaultValue);
585             } else if (TYPE_INTEGER.equals(type)) {
586                 int value = getInt(id);
587                 int defaultValue = Integer.parseInt(setting.getDefault());
588                 return (value == defaultValue);
589             } else {
590                 throw new IllegalArgumentException("Type " + type + " is not known.");
591             }
592         } else {
593             return false;
594         }
595     }
596
597     public static Setting getLocationSetting(Context context) {
598         String defaultValue = context.getString(R.string.setting_none_value);
599         String[] values = context.getResources().getStringArray(
600             R.array.pref_camera_recordlocation_entryvalues);
601         return new Setting(SOURCE_GLOBAL, TYPE_STRING, defaultValue, KEY_RECORD_LOCATION,
602             values, FLUSH_OFF);
603     }
604
605     public static Setting getPictureSizeSetting(Context context) {
606         String defaultValue = null;
607         String[] values = context.getResources().getStringArray(
608             R.array.pref_camera_picturesize_entryvalues);
609         return new Setting(SOURCE_GLOBAL, TYPE_STRING, defaultValue, KEY_PICTURE_SIZE,
610             values, FLUSH_OFF);
611     }
612
613     public static Setting getDefaultCameraIdSetting(Context context,
614             SettingsCapabilities capabilities) {
615         String defaultValue = context.getString(R.string.pref_camera_id_default);
616         String[] values = null;
617         if (capabilities != null) {
618             values = capabilities.getSupportedCameraIds();
619         }
620         return new Setting(SOURCE_GLOBAL, TYPE_STRING, defaultValue, KEY_CAMERA_ID,
621             values, FLUSH_ON);
622     }
623
624     public static Setting getWhiteBalanceSetting(Context context) {
625         String defaultValue = context.getString(R.string.pref_camera_whitebalance_default);
626         String[] values = context.getResources().getStringArray(
627             R.array.pref_camera_whitebalance_entryvalues);
628         return new Setting(SOURCE_CAMERA, TYPE_STRING, defaultValue, KEY_WHITE_BALANCE,
629             values, FLUSH_OFF);
630     }
631
632     public static Setting getHdrSetting(Context context) {
633         String defaultValue = context.getString(R.string.pref_camera_hdr_default);
634         String[] values = context.getResources().getStringArray(
635             R.array.pref_camera_hdr_entryvalues);
636         return new Setting(SOURCE_GLOBAL, TYPE_STRING, defaultValue, KEY_CAMERA_HDR,
637             values, FLUSH_OFF);
638     }
639
640     public static Setting getHdrPlusSetting(Context context) {
641         String defaultValue = context.getString(R.string.pref_camera_hdr_plus_default);
642         String[] values = context.getResources().getStringArray(
643             R.array.pref_camera_hdr_plus_entryvalues);
644         return new Setting(SOURCE_GLOBAL, TYPE_STRING, defaultValue, KEY_CAMERA_HDR_PLUS,
645             values, FLUSH_OFF);
646     }
647
648     public static Setting getSceneModeSetting(Context context) {
649         String defaultValue = context.getString(R.string.pref_camera_scenemode_default);
650         String[] values = context.getResources().getStringArray(
651             R.array.pref_camera_scenemode_entryvalues);
652         return new Setting(SOURCE_CAMERA, TYPE_STRING, defaultValue, KEY_SCENE_MODE,
653             values, FLUSH_OFF);
654     }
655
656     public static Setting getFlashSetting(Context context) {
657         String defaultValue = context.getString(R.string.pref_camera_flashmode_default);
658         String[] values = context.getResources().getStringArray(
659             R.array.pref_camera_flashmode_entryvalues);
660         return new Setting(SOURCE_CAMERA, TYPE_STRING, defaultValue, KEY_FLASH_MODE,
661             values, FLUSH_OFF);
662     }
663
664     public static Setting getExposureSetting(Context context,
665             SettingsCapabilities capabilities) {
666         String defaultValue = context.getString(R.string.pref_exposure_default);
667         String[] values = null;
668         if (capabilities != null) {
669             values = capabilities.getSupportedExposureValues();
670         }
671         return new Setting(SOURCE_CAMERA, TYPE_STRING, defaultValue, KEY_EXPOSURE,
672             values, FLUSH_ON);
673     }
674
675     public static Setting getHintSetting(Context context) {
676         String defaultValue = context.getString(R.string.setting_on_value);
677         String[] values = null;
678         return new Setting(SOURCE_GLOBAL, TYPE_BOOLEAN, defaultValue,
679             KEY_CAMERA_FIRST_USE_HINT_SHOWN, values, FLUSH_OFF);
680     }
681
682     public static Setting getFocusModeSetting(Context context) {
683         String defaultValue = null;
684         String[] values = context.getResources().getStringArray(
685             R.array.pref_camera_focusmode_entryvalues);
686         return new Setting(SOURCE_CAMERA, TYPE_STRING, defaultValue, KEY_FOCUS_MODE,
687             values, FLUSH_OFF);
688     }
689
690     public static Setting getTimerSetting(Context context) {
691         String defaultValue = context.getString(R.string.pref_camera_timer_default);
692         String[] values = null; // TODO: get the values dynamically.
693         return new Setting(SOURCE_GLOBAL, TYPE_STRING, defaultValue, KEY_TIMER,
694             values, FLUSH_OFF);
695     }
696
697     public static Setting getTimerSoundSetting(Context context) {
698         String defaultValue = context.getString(R.string.pref_camera_timer_sound_default);
699         String[] values = context.getResources().getStringArray(
700             R.array.pref_camera_timer_sound_entryvalues);
701         return new Setting(SOURCE_GLOBAL, TYPE_STRING, defaultValue, KEY_TIMER_SOUND_EFFECTS,
702             values, FLUSH_OFF);
703     }
704
705     public static Setting getVideoQualitySetting(Context context) {
706         String defaultValue = context.getString(R.string.pref_video_quality_default);
707         String[] values = context.getResources().getStringArray(
708             R.array.pref_video_quality_entryvalues);
709         return new Setting(SOURCE_GLOBAL, TYPE_STRING, defaultValue, KEY_VIDEO_QUALITY,
710             values, FLUSH_OFF);
711     }
712
713     public static Setting getTimeLapseFrameIntervalSetting(Context context) {
714         String defaultValue = context.getString(
715             R.string.pref_video_time_lapse_frame_interval_default);
716         String[] values = context.getResources().getStringArray(
717             R.array.pref_video_time_lapse_frame_interval_entryvalues);
718         return new Setting(SOURCE_GLOBAL, TYPE_STRING, defaultValue,
719             KEY_VIDEO_TIME_LAPSE_FRAME_INTERVAL, values, FLUSH_OFF);
720     }
721
722     public static Setting getJpegQualitySetting(Context context) {
723         String defaultValue = context.getString(
724             R.string.pref_camera_jpeg_quality_normal);
725         String[] values = context.getResources().getStringArray(
726             R.array.pref_camera_jpeg_quality_entryvalues);
727         return new Setting(SOURCE_CAMERA, TYPE_STRING, defaultValue, KEY_JPEG_QUALITY,
728             values, FLUSH_OFF);
729     }
730
731     public static Setting getVideoFlashSetting(Context context) {
732         String defaultValue = context.getString(R.string.pref_camera_video_flashmode_default);
733         String[] values = context.getResources().getStringArray(
734             R.array.pref_camera_video_flashmode_entryvalues);
735         return new Setting(SOURCE_CAMERA, TYPE_STRING, defaultValue,
736             KEY_VIDEOCAMERA_FLASH_MODE, values, FLUSH_OFF);
737     }
738
739     public static Setting getVideoEffectSetting(Context context) {
740         String defaultValue = context.getString(R.string.pref_video_effect_default);
741         String[] values = context.getResources().getStringArray(
742             R.array.pref_video_effect_entryvalues);
743         return new Setting(SOURCE_GLOBAL, TYPE_STRING, defaultValue, KEY_VIDEO_EFFECT,
744             values, FLUSH_OFF);
745     }
746
747     public static Setting getHintVideoSetting(Context context) {
748         String defaultValue = context.getString(R.string.setting_on_value);
749         String[] values = null;
750         return new Setting(SOURCE_GLOBAL, TYPE_STRING, defaultValue,
751             KEY_VIDEO_FIRST_USE_HINT_SHOWN, values, FLUSH_OFF);
752     }
753
754     public static Setting getStartupModuleSetting(Context context) {
755         String defaultValue = context.getString(R.string.pref_camera_startup_index_default);
756         String[] values = null;
757         return new Setting(SOURCE_DEFAULT, TYPE_INTEGER, defaultValue,
758             KEY_STARTUP_MODULE_INDEX, values, FLUSH_OFF);
759     }
760
761     public static Setting getRefocusSetting(Context context) {
762         String defaultValue = context.getString(R.string.setting_off_value);
763         String[] values = context.getResources().getStringArray(
764             R.array.pref_camera_refocus_entryvalues);
765         return new Setting(SOURCE_GLOBAL, TYPE_STRING, defaultValue,
766             KEY_CAMERA_REFOCUS, values, FLUSH_OFF);
767     }
768
769     // Utilities.
770
771     /**
772      * Returns whether the camera has been set to back facing
773      * in settings.
774      */
775     public boolean isCameraBackFacing() {
776         int cameraFacingIndex = getStringValueIndex(SETTING_CAMERA_ID);
777         String backFacingIndex = mContext.getString(R.string.pref_camera_id_index_back);
778         return (cameraFacingIndex == Integer.parseInt(backFacingIndex));
779     }
780
781     //TODO: refactor this into a separate utils module.
782
783     /**
784      * Get a String value from first the ListPreference, and if not found
785      * from the SettingsManager.
786      *
787      * This is a wrapper that adds backwards compatibility to views that
788      * rely on PreferenceGroups.
789      */
790     public String getValueFromPreference(ListPreference pref) {
791         String value = pref.getValue();
792         if (value == null) {
793             Integer id = mSettingsCache.getId(pref.getKey());
794             if (id == null) {
795                 return null;
796             }
797             value = get(id);
798         }
799         return value;
800     }
801
802     /**
803      * Set a String value first from the ListPreference, and if unable
804      * from the SettingsManager.
805      *
806      * This is a wrapper that adds backwards compatibility to views that
807      * rely on PreferenceGroups.
808      */
809     public void setValueFromPreference(ListPreference pref, String value) {
810         boolean set = pref.setValue(value);
811         if (!set) {
812             Integer id = mSettingsCache.getId(pref.getKey());
813             if (id != null) {
814                 set(id, value);
815             }
816         }
817     }
818
819     /**
820      * Set a String value first from the ListPreference based on a
821      * ListPreference index, and if unable use the ListPreference key
822      * to set the value using the SettingsManager.
823      *
824      * This is a wrapper that adds backwards compatibility to views that
825      * rely on PreferenceGroups.
826      */
827     public void setValueIndexFromPreference(ListPreference pref, int index) {
828         boolean set = pref.setValueIndex(index);
829         if (!set) {
830             Integer id = mSettingsCache.getId(pref.getKey());
831             if (id != null) {
832                 String value = pref.getValueAtIndex(index);
833                 set(id, value);
834             }
835         }
836     }
837 }