OSDN Git Service

Merge "Import translations. DO NOT MERGE" 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             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     public static final int SETTING_SHIMMY_REMAINING_PLAY_TIMES_INDEX = 21;
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     public static final String KEY_SHIMMY_REMAINING_PLAY_TIMES =
319             "pref_shimmy_remaining_play_times";
320
321     public static final int WHITE_BALANCE_DEFAULT_INDEX = 2;
322
323
324     /**
325      * Defines a simple class for holding a the spec of a setting.
326      * This spec is used by the generic api methods to query and
327      * update a setting.
328      */
329     public static class Setting {
330         private final String mSource;
331         private final String mType;
332         private final String mDefault;
333         private final String mKey;
334         private final String[] mValues;
335         private final boolean mFlushOnCameraChange;
336
337         /**
338          * A constructor used to store a setting's profile.
339          */
340         Setting(String source, String type, String defaultValue, String key,
341                 String[] values, boolean flushOnCameraChange) {
342             mSource = source;
343             mType = type;
344             mDefault = defaultValue;
345             mKey = key;
346             mValues = values;
347             mFlushOnCameraChange = flushOnCameraChange;
348         }
349
350         /**
351          * Returns the id of a SharedPreferences instance from which
352          * this Setting may be found.
353          * Possible values are {@link #SOURCE_DEFAULT}, {@link #SOURCE_GLOBAL},
354          * {@link #SOURCE_CAMERA}.
355          */
356         public String getSource() {
357             return mSource;
358         }
359
360         /**
361          * Returns the type of the setting stored in SharedPreferences.
362          * Possible values are {@link #TYPE_STRING}, {@link #TYPE_INTEGER},
363          * {@link #TYPE_BOOLEAN}.
364          */
365         public String getType() {
366             return mType;
367         }
368
369         /**
370          * Returns the default value of this setting.
371          */
372         public String getDefault() {
373             return mDefault;
374         }
375
376         /**
377          * Returns the SharedPreferences key for this setting.
378          */
379         public String getKey() {
380             return mKey;
381         }
382
383         /**
384          * Returns an array of possible String values for this setting.
385          * If this setting is not of type {@link #TYPE_STRING}, or
386          * it's not possible to generate the string values, this should
387          * return null;
388          */
389         public String[] getStringValues() {
390             return mValues;
391         }
392
393         /**
394          * Returns whether the setting should be flushed from the cache
395          * when the camera device has changed.
396          */
397         public boolean isFlushedOnCameraChanged() {
398             return mFlushOnCameraChange;
399         }
400     }
401
402     /**
403      * Get the SharedPreferences needed to query/update the setting.
404      */
405     public SharedPreferences getSettingSource(Setting setting) {
406         String source = setting.getSource();
407         if (source.equals(SOURCE_DEFAULT)) {
408             return mDefaultSettings;
409         }
410         if (source.equals(SOURCE_GLOBAL)) {
411             return mGlobalSettings;
412         }
413         if (source.equals(SOURCE_CAMERA)) {
414             return mCameraSettings;
415         }
416         return null;
417     }
418
419     /**
420      * Based on Setting id, finds the index of a Setting's
421      * String value in an array of possible String values.
422      *
423      * If the Setting is not of type String, this returns -1.
424      *
425      * <p>TODO: make this a supported api call for all types.</p>
426      */
427     public int getStringValueIndex(int id) {
428         Setting setting = mSettingsCache.get(id);
429         if (setting == null || !TYPE_STRING.equals(setting.getType())) {
430             return -1;
431         }
432
433         String value = get(id);
434         if (value != null) {
435             String[] possibleValues = setting.getStringValues();
436             if (possibleValues != null) {
437                 for (int i = 0; i < possibleValues.length; i++) {
438                     if (value.equals(possibleValues[i])) {
439                         return i;
440                     }
441                 }
442             }
443         }
444         return -1;
445     }
446
447     /**
448      * Based on Setting id, sets a Setting's String value using the
449      * index into an array of possible String values.
450      *
451      * Fails to set a value if the index is out of bounds or the Setting
452      * is not of type String.
453      *
454      * @return Whether the value was set.
455      */
456     public boolean setStringValueIndex(int id, int index) {
457         Setting setting = mSettingsCache.get(id);
458         if (setting == null || setting.getType() != TYPE_STRING) {
459             return false;
460         }
461
462         String[] possibleValues = setting.getStringValues();
463         if (possibleValues != null) {
464             if (index >= 0 && index < possibleValues.length) {
465                 set(id, possibleValues[index]);
466                 return true;
467             }
468         }
469         return false;
470     }
471
472     /**
473      * Get a Setting's String value based on Setting id.
474      */
475     // TODO: rename to something more descriptive.
476     public String get(int id) {
477         Setting setting = mSettingsCache.get(id);
478         SharedPreferences preferences = getSettingSource(setting);
479         if (preferences != null) {
480             return preferences.getString(setting.getKey(), setting.getDefault());
481         } else {
482             return null;
483         }
484     }
485
486     /**
487      * Get a Setting's boolean value based on Setting id.
488      */
489     public boolean getBoolean(int id) {
490         Setting setting = mSettingsCache.get(id);
491         SharedPreferences preferences = getSettingSource(setting);
492         boolean defaultValue = setting.getDefault().equals(VALUE_ON);
493         if (preferences != null) {
494             return preferences.getBoolean(setting.getKey(), defaultValue);
495         } else {
496             return defaultValue;
497         }
498     }
499
500     /**
501      * Get a Setting's int value based on Setting id.
502      */
503     public int getInt(int id) {
504         Setting setting = mSettingsCache.get(id);
505         SharedPreferences preferences = getSettingSource(setting);
506         int defaultValue = Integer.parseInt(setting.getDefault());
507         if (preferences != null) {
508             return preferences.getInt(setting.getKey(), defaultValue);
509         } else {
510             return defaultValue;
511         }
512     }
513
514     /**
515      * Set a Setting with a String value based on Setting id.
516      */
517     // TODO: rename to something more descriptive.
518     public void set(int id, String value) {
519         Setting setting = mSettingsCache.get(id);
520         SharedPreferences preferences = getSettingSource(setting);
521         if (preferences != null) {
522             preferences.edit().putString(setting.getKey(), value).apply();
523         }
524     }
525
526     /**
527      * Set a Setting with a boolean value based on Setting id.
528      */
529     public void setBoolean(int id, boolean value) {
530         Setting setting = mSettingsCache.get(id);
531         SharedPreferences preferences = getSettingSource(setting);
532         if (preferences != null) {
533             preferences.edit().putBoolean(setting.getKey(), value).apply();
534         }
535     }
536
537     /**
538      * Set a Setting with an int value based on Setting id.
539      */
540     public void setInt(int id, int value) {
541         Setting setting = mSettingsCache.get(id);
542         SharedPreferences preferences = getSettingSource(setting);
543         if (preferences != null) {
544             preferences.edit().putInt(setting.getKey(), value).apply();
545         }
546     }
547
548     /**
549      * Check if a Setting has ever been set based on Setting id.
550      */
551     public boolean isSet(int id) {
552         Setting setting = mSettingsCache.get(id);
553         SharedPreferences preferences = getSettingSource(setting);
554         if (preferences != null) {
555             return (preferences.getString(setting.getKey(), null) != null);
556         } else {
557             return false;
558         }
559     }
560
561     /**
562      * Set a Setting to its default value based on Setting id.
563      */
564     public void setDefault(int id) {
565         Setting setting = mSettingsCache.get(id);
566         SharedPreferences preferences = getSettingSource(setting);
567         if (preferences != null) {
568             preferences.edit().putString(setting.getKey(), setting.getDefault());
569         }
570     }
571
572     /**
573      * Check if a Setting is set to its default value.
574      */
575     public boolean isDefault(int id) {
576         Setting setting = mSettingsCache.get(id);
577         SharedPreferences preferences = getSettingSource(setting);
578         if (preferences != null) {
579             String type = setting.getType();
580             if (TYPE_STRING.equals(type)) {
581                 String value = get(id);
582                 return (value.equals(setting.getDefault()));
583             } else if (TYPE_BOOLEAN.equals(type)) {
584                 boolean value = getBoolean(id);
585                 boolean defaultValue = VALUE_ON.equals(setting.getDefault());
586                 return (value == defaultValue);
587             } else if (TYPE_INTEGER.equals(type)) {
588                 int value = getInt(id);
589                 int defaultValue = Integer.parseInt(setting.getDefault());
590                 return (value == defaultValue);
591             } else {
592                 throw new IllegalArgumentException("Type " + type + " is not known.");
593             }
594         } else {
595             return false;
596         }
597     }
598
599     public static Setting getLocationSetting(Context context) {
600         String defaultValue = context.getString(R.string.setting_none_value);
601         String[] values = context.getResources().getStringArray(
602             R.array.pref_camera_recordlocation_entryvalues);
603         return new Setting(SOURCE_GLOBAL, TYPE_STRING, defaultValue, KEY_RECORD_LOCATION,
604             values, FLUSH_OFF);
605     }
606
607     public static Setting getPictureSizeSetting(Context context) {
608         String defaultValue = null;
609         String[] values = context.getResources().getStringArray(
610             R.array.pref_camera_picturesize_entryvalues);
611         return new Setting(SOURCE_GLOBAL, TYPE_STRING, defaultValue, KEY_PICTURE_SIZE,
612             values, FLUSH_OFF);
613     }
614
615     public static Setting getDefaultCameraIdSetting(Context context,
616             SettingsCapabilities capabilities) {
617         String defaultValue = context.getString(R.string.pref_camera_id_default);
618         String[] values = null;
619         if (capabilities != null) {
620             values = capabilities.getSupportedCameraIds();
621         }
622         return new Setting(SOURCE_GLOBAL, TYPE_STRING, defaultValue, KEY_CAMERA_ID,
623             values, FLUSH_ON);
624     }
625
626     public static Setting getWhiteBalanceSetting(Context context) {
627         String defaultValue = context.getString(R.string.pref_camera_whitebalance_default);
628         String[] values = context.getResources().getStringArray(
629             R.array.pref_camera_whitebalance_entryvalues);
630         return new Setting(SOURCE_CAMERA, TYPE_STRING, defaultValue, KEY_WHITE_BALANCE,
631             values, FLUSH_OFF);
632     }
633
634     public static Setting getHdrSetting(Context context) {
635         String defaultValue = context.getString(R.string.pref_camera_hdr_default);
636         String[] values = context.getResources().getStringArray(
637             R.array.pref_camera_hdr_entryvalues);
638         return new Setting(SOURCE_GLOBAL, TYPE_STRING, defaultValue, KEY_CAMERA_HDR,
639             values, FLUSH_OFF);
640     }
641
642     public static Setting getHdrPlusSetting(Context context) {
643         String defaultValue = context.getString(R.string.pref_camera_hdr_plus_default);
644         String[] values = context.getResources().getStringArray(
645             R.array.pref_camera_hdr_plus_entryvalues);
646         return new Setting(SOURCE_GLOBAL, TYPE_STRING, defaultValue, KEY_CAMERA_HDR_PLUS,
647             values, FLUSH_OFF);
648     }
649
650     public static Setting getSceneModeSetting(Context context) {
651         String defaultValue = context.getString(R.string.pref_camera_scenemode_default);
652         String[] values = context.getResources().getStringArray(
653             R.array.pref_camera_scenemode_entryvalues);
654         return new Setting(SOURCE_CAMERA, TYPE_STRING, defaultValue, KEY_SCENE_MODE,
655             values, FLUSH_OFF);
656     }
657
658     public static Setting getFlashSetting(Context context) {
659         String defaultValue = context.getString(R.string.pref_camera_flashmode_default);
660         String[] values = context.getResources().getStringArray(
661             R.array.pref_camera_flashmode_entryvalues);
662         return new Setting(SOURCE_CAMERA, TYPE_STRING, defaultValue, KEY_FLASH_MODE,
663             values, FLUSH_OFF);
664     }
665
666     public static Setting getExposureSetting(Context context,
667             SettingsCapabilities capabilities) {
668         String defaultValue = context.getString(R.string.pref_exposure_default);
669         String[] values = null;
670         if (capabilities != null) {
671             values = capabilities.getSupportedExposureValues();
672         }
673         return new Setting(SOURCE_CAMERA, TYPE_STRING, defaultValue, KEY_EXPOSURE,
674             values, FLUSH_ON);
675     }
676
677     public static Setting getHintSetting(Context context) {
678         String defaultValue = context.getString(R.string.setting_on_value);
679         String[] values = null;
680         return new Setting(SOURCE_GLOBAL, TYPE_BOOLEAN, defaultValue,
681             KEY_CAMERA_FIRST_USE_HINT_SHOWN, values, FLUSH_OFF);
682     }
683
684     public static Setting getFocusModeSetting(Context context) {
685         String defaultValue = null;
686         String[] values = context.getResources().getStringArray(
687             R.array.pref_camera_focusmode_entryvalues);
688         return new Setting(SOURCE_CAMERA, TYPE_STRING, defaultValue, KEY_FOCUS_MODE,
689             values, FLUSH_OFF);
690     }
691
692     public static Setting getTimerSetting(Context context) {
693         String defaultValue = context.getString(R.string.pref_camera_timer_default);
694         String[] values = null; // TODO: get the values dynamically.
695         return new Setting(SOURCE_GLOBAL, TYPE_STRING, defaultValue, KEY_TIMER,
696             values, FLUSH_OFF);
697     }
698
699     public static Setting getTimerSoundSetting(Context context) {
700         String defaultValue = context.getString(R.string.pref_camera_timer_sound_default);
701         String[] values = context.getResources().getStringArray(
702             R.array.pref_camera_timer_sound_entryvalues);
703         return new Setting(SOURCE_GLOBAL, TYPE_STRING, defaultValue, KEY_TIMER_SOUND_EFFECTS,
704             values, FLUSH_OFF);
705     }
706
707     public static Setting getVideoQualitySetting(Context context) {
708         String defaultValue = context.getString(R.string.pref_video_quality_default);
709         String[] values = context.getResources().getStringArray(
710             R.array.pref_video_quality_entryvalues);
711         return new Setting(SOURCE_GLOBAL, TYPE_STRING, defaultValue, KEY_VIDEO_QUALITY,
712             values, FLUSH_OFF);
713     }
714
715     public static Setting getTimeLapseFrameIntervalSetting(Context context) {
716         String defaultValue = context.getString(
717             R.string.pref_video_time_lapse_frame_interval_default);
718         String[] values = context.getResources().getStringArray(
719             R.array.pref_video_time_lapse_frame_interval_entryvalues);
720         return new Setting(SOURCE_GLOBAL, TYPE_STRING, defaultValue,
721             KEY_VIDEO_TIME_LAPSE_FRAME_INTERVAL, values, FLUSH_OFF);
722     }
723
724     public static Setting getJpegQualitySetting(Context context) {
725         String defaultValue = context.getString(
726             R.string.pref_camera_jpeg_quality_normal);
727         String[] values = context.getResources().getStringArray(
728             R.array.pref_camera_jpeg_quality_entryvalues);
729         return new Setting(SOURCE_CAMERA, TYPE_STRING, defaultValue, KEY_JPEG_QUALITY,
730             values, FLUSH_OFF);
731     }
732
733     public static Setting getVideoFlashSetting(Context context) {
734         String defaultValue = context.getString(R.string.pref_camera_video_flashmode_default);
735         String[] values = context.getResources().getStringArray(
736             R.array.pref_camera_video_flashmode_entryvalues);
737         return new Setting(SOURCE_CAMERA, TYPE_STRING, defaultValue,
738             KEY_VIDEOCAMERA_FLASH_MODE, values, FLUSH_OFF);
739     }
740
741     public static Setting getVideoEffectSetting(Context context) {
742         String defaultValue = context.getString(R.string.pref_video_effect_default);
743         String[] values = context.getResources().getStringArray(
744             R.array.pref_video_effect_entryvalues);
745         return new Setting(SOURCE_GLOBAL, TYPE_STRING, defaultValue, KEY_VIDEO_EFFECT,
746             values, FLUSH_OFF);
747     }
748
749     public static Setting getHintVideoSetting(Context context) {
750         String defaultValue = context.getString(R.string.setting_on_value);
751         String[] values = null;
752         return new Setting(SOURCE_GLOBAL, TYPE_STRING, defaultValue,
753             KEY_VIDEO_FIRST_USE_HINT_SHOWN, values, FLUSH_OFF);
754     }
755
756     public static Setting getStartupModuleSetting(Context context) {
757         String defaultValue = context.getString(R.string.pref_camera_startup_index_default);
758         String[] values = null;
759         return new Setting(SOURCE_DEFAULT, TYPE_INTEGER, defaultValue,
760             KEY_STARTUP_MODULE_INDEX, values, FLUSH_OFF);
761     }
762
763     public static Setting getShimmyRemainingTimesSetting(Context context) {
764         String defaultValue = context.getString(R.string.pref_shimmy_play_times);
765         return new Setting(SOURCE_DEFAULT, TYPE_INTEGER, defaultValue,
766                 KEY_SHIMMY_REMAINING_PLAY_TIMES, null, FLUSH_OFF);
767     }
768
769     public static Setting getRefocusSetting(Context context) {
770         String defaultValue = context.getString(R.string.setting_off_value);
771         String[] values = context.getResources().getStringArray(
772             R.array.pref_camera_refocus_entryvalues);
773         return new Setting(SOURCE_GLOBAL, TYPE_STRING, defaultValue,
774             KEY_CAMERA_REFOCUS, values, FLUSH_OFF);
775     }
776
777     // Utilities.
778
779     /**
780      * Returns whether the camera has been set to back facing
781      * in settings.
782      */
783     public boolean isCameraBackFacing() {
784         int cameraFacingIndex = getStringValueIndex(SETTING_CAMERA_ID);
785         String backFacingIndex = mContext.getString(R.string.pref_camera_id_index_back);
786         return (cameraFacingIndex == Integer.parseInt(backFacingIndex));
787     }
788
789     /**
790      * Returns whether refocus mode is set on.
791      */
792     public boolean isRefocusOn() {
793         String refocusOn = get(SettingsManager.SETTING_CAMERA_REFOCUS);
794         return refocusOn.equals(SettingsManager.VALUE_ON);
795     }
796
797     /**
798      * Returns whether hdr plus mode is set on.
799      */
800     public boolean isHdrPlusOn() {
801         String hdrOn = get(SettingsManager.SETTING_CAMERA_HDR);
802         return hdrOn.equals(SettingsManager.VALUE_ON);
803     }
804
805     //TODO: refactor this into a separate utils module.
806
807     /**
808      * Get a String value from first the ListPreference, and if not found
809      * from the SettingsManager.
810      *
811      * This is a wrapper that adds backwards compatibility to views that
812      * rely on PreferenceGroups.
813      */
814     public String getValueFromPreference(ListPreference pref) {
815         String value = pref.getValue();
816         if (value == null) {
817             Integer id = mSettingsCache.getId(pref.getKey());
818             if (id == null) {
819                 return null;
820             }
821             value = get(id);
822         }
823         return value;
824     }
825
826     /**
827      * Set a String value first from the ListPreference, and if unable
828      * from the SettingsManager.
829      *
830      * This is a wrapper that adds backwards compatibility to views that
831      * rely on PreferenceGroups.
832      */
833     public void setValueFromPreference(ListPreference pref, String value) {
834         boolean set = pref.setValue(value);
835         if (!set) {
836             Integer id = mSettingsCache.getId(pref.getKey());
837             if (id != null) {
838                 set(id, value);
839             }
840         }
841     }
842
843     /**
844      * Set a String value first from the ListPreference based on a
845      * ListPreference index, and if unable use the ListPreference key
846      * to set the value using the SettingsManager.
847      *
848      * This is a wrapper that adds backwards compatibility to views that
849      * rely on PreferenceGroups.
850      */
851     public void setValueIndexFromPreference(ListPreference pref, int index) {
852         boolean set = pref.setValueIndex(index);
853         if (!set) {
854             Integer id = mSettingsCache.getId(pref.getKey());
855             if (id != null) {
856                 String value = pref.getValueAtIndex(index);
857                 set(id, value);
858             }
859         }
860     }
861 }