OSDN Git Service

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