blob: 2fd9d541e016e358f83d0bb8c90e5b070b5dd10a [file] [log] [blame]
Erin Dahlgren357b7672013-11-20 17:38:14 -08001/*
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
17package com.android.camera.settings;
18
Erin Dahlgren357b7672013-11-20 17:38:14 -080019import android.content.Context;
20import android.content.SharedPreferences;
Erin Dahlgren357b7672013-11-20 17:38:14 -080021import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
Erin Dahlgren357b7672013-11-20 17:38:14 -080022import android.preference.PreferenceManager;
Erin Dahlgrend186f222014-01-10 11:24:31 -080023import android.util.SparseArray;
Erin Dahlgren357b7672013-11-20 17:38:14 -080024
Erin Dahlgren357b7672013-11-20 17:38:14 -080025import com.android.camera.ListPreference;
Erin Dahlgrend186f222014-01-10 11:24:31 -080026import com.android.camera.app.AppController;
Sascha Haeberlingde303232014-02-07 02:30:53 +010027import com.android.camera.app.LocationManager;
Angus Kong2bca2102014-03-11 16:27:30 -070028import com.android.camera.debug.Log;
Erin Dahlgren1ba90e32014-03-03 12:05:57 -080029import com.android.camera.util.CameraUtil;
Erin Dahlgrene419b192013-12-03 13:10:27 -080030import com.android.camera.util.SettingsHelper;
Erin Dahlgren357b7672013-11-20 17:38:14 -080031import com.android.camera2.R;
32
Erin Dahlgren1648c362014-01-06 15:06:04 -080033import java.util.ArrayList;
Sascha Haeberling3b0ab892014-01-29 20:54:39 +010034import java.util.List;
Erin Dahlgren357b7672013-11-20 17:38:14 -080035
Erin Dahlgren635a4b82013-11-25 15:21:18 -080036/**
Sascha Haeberlingde303232014-02-07 02:30:53 +010037 * SettingsManager class provides an api for getting and setting both global and
38 * local SharedPreferences.
Erin Dahlgren635a4b82013-11-25 15:21:18 -080039 */
Erin Dahlgren357b7672013-11-20 17:38:14 -080040public class SettingsManager {
Angus Kong2bca2102014-03-11 16:27:30 -070041 private static final Log.Tag TAG = new Log.Tag("SettingsManager");
Erin Dahlgren357b7672013-11-20 17:38:14 -080042
Sascha Haeberlinga63dbb62013-11-22 11:55:32 -080043 private final Context mContext;
44 private final SharedPreferences mDefaultSettings;
45 private final SettingsCache mSettingsCache;
Erin Dahlgren357b7672013-11-20 17:38:14 -080046 private SharedPreferences mGlobalSettings;
47 private SharedPreferences mCameraSettings;
Sascha Haeberlingde303232014-02-07 02:30:53 +010048 private final SparseArray<SharedPreferences> mModuleSettings = new SparseArray<SharedPreferences>();
Erin Dahlgren357b7672013-11-20 17:38:14 -080049 private SettingsCapabilities mCapabilities;
50
51 private int mCameraId = -1;
Sascha Haeberlingde303232014-02-07 02:30:53 +010052 private final AppController mAppController;
Erin Dahlgren357b7672013-11-20 17:38:14 -080053
Erin Dahlgren1841b112014-01-29 15:44:57 -080054 /**
Erin Dahlgren4569b702014-02-24 14:21:11 -080055 * General settings upgrade model:
56 *
57 * On app upgrade, there are three main ways Settings can be stale/incorrect:
58 * (1) if the type of a setting has changed.
59 * (2) if a set value is no longer a member of the set of possible values.
60 * (3) if the SharedPreferences backing file has changed for a setting.
61 *
62 * Recovery strategies:
63 * (1) catch the ClassCastException or NumberFormatException and try to do a
64 * type conversion, or reset the setting to whatever default is valid.
65 * (2) sanitize sets, and reset to default if set value is no longer valid.
66 * (3) use the default value by virtue of the setting not yet being in the
67 * new file.
68 *
69 * Special cases:
70 *
71 * There are some settings which shouldn't be reset to default on upgrade if
72 * possible. We provide a callback which is executed only on strict upgrade.
73 * This callback does special case upgrades to a subset of the settings. This
74 * contrasts with the general upgrade strategies, which happen lazily, once a
75 * setting is used.
76 *
77 * Removing obsolete key/value pairs:
78 *
79 * This can be done in the strict upgrade callback. The strict upgrade callback
80 * should be idempotent, so it is important to leave removal code in the upgrade
81 * callback so the key/value pairs are removed even if a user skips a version.
82 */
83 public interface StrictUpgradeCallback {
84 /**
85 * Will be executed in the SettingsManager constructor if the strict
86 * upgrade version counter has changed.
87 */
88 public void upgrade(SettingsManager settingsManager, int version);
89 }
90
91 /**
92 * Increment this value whenever a new StrictUpgradeCallback needs to
93 * be executed. This defines upgrade behavior that should be executed
94 * strictly on app upgrades, when the upgrade behavior differs from the general,
95 * lazy upgrade strategies.
96 */
Erin Dahlgrenbd245b22014-03-18 09:48:51 -070097 private static final int STRICT_UPGRADE_VERSION = 2;
Erin Dahlgren4569b702014-02-24 14:21:11 -080098
99 /**
Sascha Haeberlingde303232014-02-07 02:30:53 +0100100 * A List of OnSettingChangedListener's, maintained to compare to new
101 * listeners and prevent duplicate registering.
Erin Dahlgren1841b112014-01-29 15:44:57 -0800102 */
Sascha Haeberlingde303232014-02-07 02:30:53 +0100103 private final List<OnSettingChangedListener> mListeners = new ArrayList<OnSettingChangedListener>();
Erin Dahlgren1841b112014-01-29 15:44:57 -0800104
105 /**
Sascha Haeberlingde303232014-02-07 02:30:53 +0100106 * A List of OnSharedPreferenceChangeListener's, maintained to hold pointers
107 * to actually registered listeners, so they can be unregistered.
Erin Dahlgren1841b112014-01-29 15:44:57 -0800108 */
Sascha Haeberlingde303232014-02-07 02:30:53 +0100109 private final List<OnSharedPreferenceChangeListener> mSharedPreferenceListeners = new ArrayList<OnSharedPreferenceChangeListener>();
Erin Dahlgren1648c362014-01-06 15:06:04 -0800110
Erin Dahlgren4569b702014-02-24 14:21:11 -0800111 public SettingsManager(Context context, AppController app, int nCameras,
112 StrictUpgradeCallback upgradeCallback) {
Erin Dahlgren357b7672013-11-20 17:38:14 -0800113 mContext = context;
Erin Dahlgrend186f222014-01-10 11:24:31 -0800114 mAppController = app;
Erin Dahlgrene419b192013-12-03 13:10:27 -0800115
116 SettingsCache.ExtraSettings extraSettings = new SettingsHelper();
117 mSettingsCache = new SettingsCache(mContext, extraSettings);
118
Erin Dahlgren357b7672013-11-20 17:38:14 -0800119 mDefaultSettings = PreferenceManager.getDefaultSharedPreferences(context);
Erin Dahlgren7f0151d2014-01-02 16:08:12 -0800120 initGlobal();
Erin Dahlgren4569b702014-02-24 14:21:11 -0800121
122 if (upgradeCallback != null) {
123 // Check for a strict version upgrade.
124 int version = getInt(SETTING_STRICT_UPGRADE_VERSION);
125 if (STRICT_UPGRADE_VERSION != version) {
126 upgradeCallback.upgrade(this, STRICT_UPGRADE_VERSION);
127 }
128 setInt(SETTING_STRICT_UPGRADE_VERSION, STRICT_UPGRADE_VERSION);
129 }
Erin Dahlgren357b7672013-11-20 17:38:14 -0800130 }
131
132 /**
133 * Initialize global SharedPreferences.
134 */
Erin Dahlgren7f0151d2014-01-02 16:08:12 -0800135 private void initGlobal() {
Erin Dahlgren357b7672013-11-20 17:38:14 -0800136 String globalKey = mContext.getPackageName() + "_preferences_camera";
Erin Dahlgren1648c362014-01-06 15:06:04 -0800137 mGlobalSettings = mContext.getSharedPreferences(globalKey, Context.MODE_PRIVATE);
Erin Dahlgren357b7672013-11-20 17:38:14 -0800138 }
139
140 /**
Erin Dahlgrend186f222014-01-10 11:24:31 -0800141 * Load and cache a module specific SharedPreferences.
142 */
143 public SharedPreferences getModulePreferences(int modeIndex) {
144 SharedPreferences sharedPreferences = mModuleSettings.get(modeIndex);
145 if (sharedPreferences == null) {
146 String moduleKey = mContext.getPackageName() + "_preferences_module_" + modeIndex;
147 sharedPreferences = mContext.getSharedPreferences(moduleKey, Context.MODE_PRIVATE);
148 mModuleSettings.put(modeIndex, sharedPreferences);
149 }
150 return sharedPreferences;
151 }
152
153 /**
Erin Dahlgren357b7672013-11-20 17:38:14 -0800154 * Initialize SharedPreferences for other cameras.
155 */
156 public void changeCamera(int cameraId, SettingsCapabilities capabilities) {
157 mCapabilities = capabilities;
Erin Dahlgrene419b192013-12-03 13:10:27 -0800158 mSettingsCache.setCapabilities(mCapabilities);
Erin Dahlgren357b7672013-11-20 17:38:14 -0800159
160 if (cameraId == mCameraId) {
Erin Dahlgren357b7672013-11-20 17:38:14 -0800161 return;
162 }
Erin Dahlgrene419b192013-12-03 13:10:27 -0800163
164 // We've changed camera ids, that means we need to flush the
165 // settings cache of settings dependent on SettingsCapabilities.
166 mSettingsCache.flush();
167
Erin Dahlgren357b7672013-11-20 17:38:14 -0800168 // Cache the camera id so we don't need to reload preferences
169 // if we're using the same camera.
170 mCameraId = cameraId;
171
172 String cameraKey = mContext.getPackageName() + "_preferences_" + cameraId;
173 mCameraSettings = mContext.getSharedPreferences(
Sascha Haeberlingde303232014-02-07 02:30:53 +0100174 cameraKey, Context.MODE_PRIVATE);
Erin Dahlgren1841b112014-01-29 15:44:57 -0800175
Erin Dahlgren1648c362014-01-06 15:06:04 -0800176 for (OnSharedPreferenceChangeListener listener : mSharedPreferenceListeners) {
177 mCameraSettings.registerOnSharedPreferenceChangeListener(listener);
178 }
Erin Dahlgren357b7672013-11-20 17:38:14 -0800179 }
180
181 /**
182 * Interface with Camera Parameters and Modules.
183 */
Erin Dahlgren7f0151d2014-01-02 16:08:12 -0800184 public interface OnSettingChangedListener {
185 /**
186 * Called every time a SharedPreference has been changed.
187 */
Erin Dahlgren1648c362014-01-06 15:06:04 -0800188 public void onSettingChanged(SettingsManager settingsManager, int setting);
Erin Dahlgren357b7672013-11-20 17:38:14 -0800189 }
190
Erin Dahlgren1648c362014-01-06 15:06:04 -0800191 private OnSharedPreferenceChangeListener getSharedPreferenceListener(
192 final OnSettingChangedListener listener) {
193 return new OnSharedPreferenceChangeListener() {
Sascha Haeberlingde303232014-02-07 02:30:53 +0100194 @Override
195 public void onSharedPreferenceChanged(
196 SharedPreferences sharedPreferences, String key) {
197 Integer settingId = mSettingsCache.getId(key);
198 if (settingId != null) {
199 listener.onSettingChanged(SettingsManager.this, settingId);
200 } else {
201 Log.w(TAG, "Setting id from key=" + key + " is null");
Erin Dahlgren357b7672013-11-20 17:38:14 -0800202 }
Sascha Haeberlingde303232014-02-07 02:30:53 +0100203 }
204 };
Erin Dahlgren1648c362014-01-06 15:06:04 -0800205 }
Erin Dahlgren7f0151d2014-01-02 16:08:12 -0800206
Erin Dahlgren1648c362014-01-06 15:06:04 -0800207 /**
Sascha Haeberlingde303232014-02-07 02:30:53 +0100208 * Add an OnSettingChangedListener to the SettingsManager, which will
209 * execute onSettingsChanged when any SharedPreference has been updated.
Erin Dahlgren1648c362014-01-06 15:06:04 -0800210 */
211 public void addListener(final OnSettingChangedListener listener) {
212 if (listener == null) {
213 throw new IllegalArgumentException("OnSettingChangedListener cannot be null.");
Erin Dahlgren7f0151d2014-01-02 16:08:12 -0800214 }
Erin Dahlgren1648c362014-01-06 15:06:04 -0800215
Erin Dahlgren1841b112014-01-29 15:44:57 -0800216 if (mListeners.contains(listener)) {
217 return;
218 }
219
220 mListeners.add(listener);
Erin Dahlgren1648c362014-01-06 15:06:04 -0800221 OnSharedPreferenceChangeListener sharedPreferenceListener =
Sascha Haeberlingde303232014-02-07 02:30:53 +0100222 getSharedPreferenceListener(listener);
Erin Dahlgren1841b112014-01-29 15:44:57 -0800223 mSharedPreferenceListeners.add(sharedPreferenceListener);
Erin Dahlgren1648c362014-01-06 15:06:04 -0800224
Erin Dahlgren1841b112014-01-29 15:44:57 -0800225 if (mGlobalSettings != null) {
226 mGlobalSettings.registerOnSharedPreferenceChangeListener(sharedPreferenceListener);
227 }
Erin Dahlgren1648c362014-01-06 15:06:04 -0800228
Erin Dahlgren1841b112014-01-29 15:44:57 -0800229 if (mCameraSettings != null) {
230 mCameraSettings.registerOnSharedPreferenceChangeListener(sharedPreferenceListener);
231 }
Erin Dahlgren1648c362014-01-06 15:06:04 -0800232
Erin Dahlgren1841b112014-01-29 15:44:57 -0800233 if (mDefaultSettings != null) {
234 mDefaultSettings.registerOnSharedPreferenceChangeListener(sharedPreferenceListener);
Erin Dahlgren7f0151d2014-01-02 16:08:12 -0800235 }
Erin Dahlgren357b7672013-11-20 17:38:14 -0800236 }
237
Erin Dahlgren635a4b82013-11-25 15:21:18 -0800238 /**
Sascha Haeberlingde303232014-02-07 02:30:53 +0100239 * Remove a specific SettingsListener. This should be done in onPause if a
240 * listener has been set.
Erin Dahlgren635a4b82013-11-25 15:21:18 -0800241 */
Erin Dahlgren1648c362014-01-06 15:06:04 -0800242 public void removeListener(OnSettingChangedListener listener) {
243 if (listener == null) {
244 throw new IllegalArgumentException();
245 }
246
Erin Dahlgren1841b112014-01-29 15:44:57 -0800247 if (!mListeners.contains(listener)) {
248 return;
249 }
250
251 int index = mListeners.indexOf(listener);
252 mListeners.remove(listener);
253
254 // Get the reference to the actual OnSharedPreferenceChangeListener
255 // that was registered.
Erin Dahlgren1648c362014-01-06 15:06:04 -0800256 OnSharedPreferenceChangeListener sharedPreferenceListener =
Sascha Haeberlingde303232014-02-07 02:30:53 +0100257 mSharedPreferenceListeners.get(index);
Erin Dahlgren1841b112014-01-29 15:44:57 -0800258 mSharedPreferenceListeners.remove(index);
Erin Dahlgren1648c362014-01-06 15:06:04 -0800259
Erin Dahlgren1841b112014-01-29 15:44:57 -0800260 if (mGlobalSettings != null) {
261 mGlobalSettings.unregisterOnSharedPreferenceChangeListener(
Sascha Haeberlingde303232014-02-07 02:30:53 +0100262 sharedPreferenceListener);
Erin Dahlgren1841b112014-01-29 15:44:57 -0800263 }
Erin Dahlgren1648c362014-01-06 15:06:04 -0800264
Erin Dahlgren1841b112014-01-29 15:44:57 -0800265 if (mCameraSettings != null) {
266 mCameraSettings.unregisterOnSharedPreferenceChangeListener(
Sascha Haeberlingde303232014-02-07 02:30:53 +0100267 sharedPreferenceListener);
Erin Dahlgren1841b112014-01-29 15:44:57 -0800268 }
Erin Dahlgren1648c362014-01-06 15:06:04 -0800269
Erin Dahlgren1841b112014-01-29 15:44:57 -0800270 if (mDefaultSettings != null) {
271 mDefaultSettings.unregisterOnSharedPreferenceChangeListener(
Sascha Haeberlingde303232014-02-07 02:30:53 +0100272 sharedPreferenceListener);
Erin Dahlgren1648c362014-01-06 15:06:04 -0800273 }
274 }
275
276 /**
Sascha Haeberlingde303232014-02-07 02:30:53 +0100277 * Remove all OnSharedPreferenceChangedListener's. This should be done in
278 * onDestroy.
Erin Dahlgren1648c362014-01-06 15:06:04 -0800279 */
280 public void removeAllListeners() {
281 for (OnSharedPreferenceChangeListener listener : mSharedPreferenceListeners) {
Erin Dahlgren1648c362014-01-06 15:06:04 -0800282 if (mGlobalSettings != null) {
283 mGlobalSettings.unregisterOnSharedPreferenceChangeListener(listener);
284 }
285
286 if (mCameraSettings != null) {
287 mCameraSettings.unregisterOnSharedPreferenceChangeListener(listener);
288 }
289
290 if (mDefaultSettings != null) {
291 mDefaultSettings.unregisterOnSharedPreferenceChangeListener(listener);
292 }
Erin Dahlgren357b7672013-11-20 17:38:14 -0800293 }
Erin Dahlgren8da34522014-01-07 10:12:15 -0800294 mSharedPreferenceListeners.clear();
Erin Dahlgren1841b112014-01-29 15:44:57 -0800295 mListeners.clear();
Erin Dahlgren357b7672013-11-20 17:38:14 -0800296 }
297
Erin Dahlgren635a4b82013-11-25 15:21:18 -0800298 /**
299 * SettingsCapabilities defines constraints around settings that need to be
Sascha Haeberlingde303232014-02-07 02:30:53 +0100300 * queried from external sources, like the camera parameters. This interface
301 * is camera api agnostic.
Erin Dahlgren635a4b82013-11-25 15:21:18 -0800302 */
Erin Dahlgren357b7672013-11-20 17:38:14 -0800303 public interface SettingsCapabilities {
Erin Dahlgrene419b192013-12-03 13:10:27 -0800304 /**
Sascha Haeberlingde303232014-02-07 02:30:53 +0100305 * Returns a dynamically calculated list of exposure values, based on
306 * the min and max exposure compensation supported by the camera device.
Erin Dahlgrene419b192013-12-03 13:10:27 -0800307 */
308 public String[] getSupportedExposureValues();
309
310 /**
Sascha Haeberlingde303232014-02-07 02:30:53 +0100311 * Returns a list of camera ids based on the number of cameras available
312 * on the device.
Erin Dahlgrene419b192013-12-03 13:10:27 -0800313 */
314 public String[] getSupportedCameraIds();
Erin Dahlgren357b7672013-11-20 17:38:14 -0800315 }
316
Erin Dahlgren635a4b82013-11-25 15:21:18 -0800317 /**
Erin Dahlgren635a4b82013-11-25 15:21:18 -0800318 * Get the camera id for which the SettingsManager has loaded camera
319 * specific preferences.
320 */
Erin Dahlgren357b7672013-11-20 17:38:14 -0800321 public int getRegisteredCameraId() {
322 return mCameraId;
323 }
324
Erin Dahlgren635a4b82013-11-25 15:21:18 -0800325 // Manage individual settings.
Erin Dahlgren357b7672013-11-20 17:38:14 -0800326 public static final String VALUE_NONE = "none";
327 public static final String VALUE_ON = "on";
328 public static final String VALUE_OFF = "off";
329
Erin Dahlgrene419b192013-12-03 13:10:27 -0800330 public static final String TYPE_STRING = "string";
331 public static final String TYPE_BOOLEAN = "boolean";
332 public static final String TYPE_INTEGER = "integer";
Erin Dahlgren357b7672013-11-20 17:38:14 -0800333
Erin Dahlgrene419b192013-12-03 13:10:27 -0800334 public static final String SOURCE_DEFAULT = "default";
335 public static final String SOURCE_GLOBAL = "global";
336 public static final String SOURCE_CAMERA = "camera";
Erin Dahlgrend186f222014-01-10 11:24:31 -0800337 public static final String SOURCE_MODULE = "module";
Erin Dahlgrene419b192013-12-03 13:10:27 -0800338
339 public static final boolean FLUSH_ON = true;
340 public static final boolean FLUSH_OFF = false;
Erin Dahlgren357b7672013-11-20 17:38:14 -0800341
Erin Dahlgren635a4b82013-11-25 15:21:18 -0800342 // For quick lookup from id to Setting.
Erin Dahlgrene419b192013-12-03 13:10:27 -0800343 public static final int SETTING_RECORD_LOCATION = 0;
Sascha Haeberling6ccec202014-03-11 09:44:34 -0700344 public static final int SETTING_VIDEO_QUALITY_BACK = 1;
345 public static final int SETTING_VIDEO_QUALITY_FRONT = 2;
346 public static final int SETTING_VIDEO_TIME_LAPSE_FRAME_INTERVAL = 3;
347 public static final int SETTING_PICTURE_SIZE_BACK = 4;
348 public static final int SETTING_PICTURE_SIZE_FRONT = 5;
349 public static final int SETTING_JPEG_QUALITY = 6;
350 public static final int SETTING_FOCUS_MODE = 7;
351 public static final int SETTING_FLASH_MODE = 8;
352 public static final int SETTING_VIDEOCAMERA_FLASH_MODE = 9;
353 public static final int SETTING_WHITE_BALANCE = 10;
354 public static final int SETTING_SCENE_MODE = 11;
355 public static final int SETTING_EXPOSURE = 12;
356 public static final int SETTING_TIMER = 13;
357 public static final int SETTING_TIMER_SOUND_EFFECTS = 14;
358 public static final int SETTING_VIDEO_EFFECT = 15;
359 public static final int SETTING_CAMERA_ID = 16;
360 public static final int SETTING_CAMERA_HDR = 17;
361 public static final int SETTING_CAMERA_HDR_PLUS = 18;
362 public static final int SETTING_CAMERA_FIRST_USE_HINT_SHOWN = 19;
363 public static final int SETTING_VIDEO_FIRST_USE_HINT_SHOWN = 20;
364 public static final int SETTING_STARTUP_MODULE_INDEX = 21;
Sascha Haeberling6ccec202014-03-11 09:44:34 -0700365 public static final int SETTING_KEY_CAMERA_MODULE_LAST_USED_INDEX = 23;
366 public static final int SETTING_CAMERA_PANO_ORIENTATION = 24;
367 public static final int SETTING_CAMERA_GRID_LINES = 25;
368 public static final int SETTING_RELEASE_DIALOG_LAST_SHOWN_VERSION = 26;
369 public static final int SETTING_FLASH_SUPPORTED_BACK_CAMERA = 27;
370 public static final int SETTING_STRICT_UPGRADE_VERSION = 28;
Erin Dahlgren1ba90e32014-03-03 12:05:57 -0800371 // A boolean for requesting to return to HDR plus
372 // as soon as possible, if a user requests a setting/mode option
373 // that forces them to leave HDR plus.
Sascha Haeberling6ccec202014-03-11 09:44:34 -0700374 public static final int SETTING_REQUEST_RETURN_HDR_PLUS = 30;
Erin Dahlgren635a4b82013-11-25 15:21:18 -0800375
376 // Shared preference keys.
Erin Dahlgren357b7672013-11-20 17:38:14 -0800377 public static final String KEY_RECORD_LOCATION = "pref_camera_recordlocation_key";
Sascha Haeberling6ccec202014-03-11 09:44:34 -0700378 public static final String KEY_VIDEO_QUALITY_BACK = "pref_video_quality_back_key";
379 public static final String KEY_VIDEO_QUALITY_FRONT = "pref_video_quality_front_key";
Erin Dahlgren635a4b82013-11-25 15:21:18 -0800380 public static final String KEY_VIDEO_TIME_LAPSE_FRAME_INTERVAL =
Sascha Haeberlingde303232014-02-07 02:30:53 +0100381 "pref_video_time_lapse_frame_interval_key";
Sascha Haeberling6ccec202014-03-11 09:44:34 -0700382 public static final String KEY_PICTURE_SIZE_BACK = "pref_camera_picturesize_back_key";
383 public static final String KEY_PICTURE_SIZE_FRONT = "pref_camera_picturesize_front_key";
Erin Dahlgren357b7672013-11-20 17:38:14 -0800384 public static final String KEY_JPEG_QUALITY = "pref_camera_jpegquality_key";
385 public static final String KEY_FOCUS_MODE = "pref_camera_focusmode_key";
386 public static final String KEY_FLASH_MODE = "pref_camera_flashmode_key";
387 public static final String KEY_VIDEOCAMERA_FLASH_MODE = "pref_camera_video_flashmode_key";
388 public static final String KEY_WHITE_BALANCE = "pref_camera_whitebalance_key";
389 public static final String KEY_SCENE_MODE = "pref_camera_scenemode_key";
390 public static final String KEY_EXPOSURE = "pref_camera_exposure_key";
391 public static final String KEY_TIMER = "pref_camera_timer_key";
392 public static final String KEY_TIMER_SOUND_EFFECTS = "pref_camera_timer_sound_key";
393 public static final String KEY_VIDEO_EFFECT = "pref_video_effect_key";
394 public static final String KEY_CAMERA_ID = "pref_camera_id_key";
395 public static final String KEY_CAMERA_HDR = "pref_camera_hdr_key";
396 public static final String KEY_CAMERA_HDR_PLUS = "pref_camera_hdr_plus_key";
Erin Dahlgren635a4b82013-11-25 15:21:18 -0800397 public static final String KEY_CAMERA_FIRST_USE_HINT_SHOWN =
Sascha Haeberlingde303232014-02-07 02:30:53 +0100398 "pref_camera_first_use_hint_shown_key";
Erin Dahlgren635a4b82013-11-25 15:21:18 -0800399 public static final String KEY_VIDEO_FIRST_USE_HINT_SHOWN =
Sascha Haeberlingde303232014-02-07 02:30:53 +0100400 "pref_video_first_use_hint_shown_key";
Erin Dahlgren357b7672013-11-20 17:38:14 -0800401 public static final String KEY_STARTUP_MODULE_INDEX = "camera.startup_module";
Doris Liucf8b6532014-01-15 19:17:38 -0800402 public static final String KEY_CAMERA_MODULE_LAST_USED =
403 "pref_camera_module_last_used_index";
Erin Dahlgrena1fab412014-01-21 09:31:11 -0800404 public static final String KEY_CAMERA_PANO_ORIENTATION = "pref_camera_pano_orientation";
Erin Dahlgrend5e51462014-02-07 12:38:57 -0800405 public static final String KEY_CAMERA_GRID_LINES = "pref_camera_grid_lines";
Sascha Haeberlingc26a3282014-02-19 08:39:28 -0800406 public static final String KEY_RELEASE_DIALOG_LAST_SHOWN_VERSION =
407 "pref_release_dialog_last_shown_version";
Erin Dahlgrene346fb22014-02-19 17:23:01 -0800408 public static final String KEY_FLASH_SUPPORTED_BACK_CAMERA =
409 "pref_flash_supported_back_camera";
Erin Dahlgren4569b702014-02-24 14:21:11 -0800410 public static final String KEY_STRICT_UPGRADE_VERSION = "pref_strict_upgrade_version";
Erin Dahlgren1ba90e32014-03-03 12:05:57 -0800411 public static final String KEY_REQUEST_RETURN_HDR_PLUS = "pref_request_return_hdr_plus";
Erin Dahlgren357b7672013-11-20 17:38:14 -0800412
413 public static final int WHITE_BALANCE_DEFAULT_INDEX = 2;
414
Erin Dahlgren635a4b82013-11-25 15:21:18 -0800415 /**
Sascha Haeberlingde303232014-02-07 02:30:53 +0100416 * Defines a simple class for holding a the spec of a setting. This spec is
417 * used by the generic api methods to query and update a setting.
Erin Dahlgren635a4b82013-11-25 15:21:18 -0800418 */
Erin Dahlgrene419b192013-12-03 13:10:27 -0800419 public static class Setting {
420 private final String mSource;
421 private final String mType;
422 private final String mDefault;
423 private final String mKey;
424 private final String[] mValues;
425 private final boolean mFlushOnCameraChange;
426
427 /**
428 * A constructor used to store a setting's profile.
429 */
Sascha Haeberlinged107412014-03-19 19:48:06 -0700430 public Setting(String source, String type, String defaultValue, String key,
Erin Dahlgrene419b192013-12-03 13:10:27 -0800431 String[] values, boolean flushOnCameraChange) {
432 mSource = source;
433 mType = type;
434 mDefault = defaultValue;
435 mKey = key;
436 mValues = values;
437 mFlushOnCameraChange = flushOnCameraChange;
438 }
439
440 /**
Sascha Haeberlingde303232014-02-07 02:30:53 +0100441 * Returns the id of a SharedPreferences instance from which this
442 * Setting may be found. Possible values are {@link #SOURCE_DEFAULT},
443 * {@link #SOURCE_GLOBAL}, {@link #SOURCE_CAMERA}.
Erin Dahlgrene419b192013-12-03 13:10:27 -0800444 */
445 public String getSource() {
446 return mSource;
447 }
448
449 /**
Sascha Haeberlingde303232014-02-07 02:30:53 +0100450 * Returns the type of the setting stored in SharedPreferences. Possible
451 * values are {@link #TYPE_STRING}, {@link #TYPE_INTEGER},
Erin Dahlgrene419b192013-12-03 13:10:27 -0800452 * {@link #TYPE_BOOLEAN}.
453 */
454 public String getType() {
455 return mType;
456 }
457
458 /**
459 * Returns the default value of this setting.
460 */
461 public String getDefault() {
462 return mDefault;
463 }
464
465 /**
466 * Returns the SharedPreferences key for this setting.
467 */
468 public String getKey() {
469 return mKey;
470 }
471
472 /**
Sascha Haeberlingde303232014-02-07 02:30:53 +0100473 * Returns an array of possible String values for this setting. If this
474 * setting is not of type {@link #TYPE_STRING}, or it's not possible to
475 * generate the string values, this should return null;
Erin Dahlgrene419b192013-12-03 13:10:27 -0800476 */
477 public String[] getStringValues() {
478 return mValues;
479 }
480
481 /**
Sascha Haeberlingde303232014-02-07 02:30:53 +0100482 * Returns whether the setting should be flushed from the cache when the
483 * camera device has changed.
Erin Dahlgrene419b192013-12-03 13:10:27 -0800484 */
485 public boolean isFlushedOnCameraChanged() {
486 return mFlushOnCameraChange;
487 }
Erin Dahlgren357b7672013-11-20 17:38:14 -0800488 }
489
Erin Dahlgren635a4b82013-11-25 15:21:18 -0800490 /**
491 * Get the SharedPreferences needed to query/update the setting.
492 */
Erin Dahlgren357b7672013-11-20 17:38:14 -0800493 public SharedPreferences getSettingSource(Setting setting) {
Erin Dahlgren9182ac82014-03-17 12:26:48 -0700494 return getSettingSource(setting.getSource());
495 }
496
497 private SharedPreferences getSettingSource(String source) {
Erin Dahlgrene419b192013-12-03 13:10:27 -0800498 if (source.equals(SOURCE_DEFAULT)) {
Erin Dahlgren357b7672013-11-20 17:38:14 -0800499 return mDefaultSettings;
500 }
Erin Dahlgrene419b192013-12-03 13:10:27 -0800501 if (source.equals(SOURCE_GLOBAL)) {
Erin Dahlgren357b7672013-11-20 17:38:14 -0800502 return mGlobalSettings;
503 }
Erin Dahlgrene419b192013-12-03 13:10:27 -0800504 if (source.equals(SOURCE_CAMERA)) {
Erin Dahlgren357b7672013-11-20 17:38:14 -0800505 return mCameraSettings;
506 }
Erin Dahlgrend186f222014-01-10 11:24:31 -0800507 if (source.equals(SOURCE_MODULE)) {
Erin Dahlgren1ba90e32014-03-03 12:05:57 -0800508 int modeIndex = CameraUtil.getCameraModeParentModeId(
509 mAppController.getCurrentModuleIndex(), mAppController.getAndroidContext());
Erin Dahlgrend186f222014-01-10 11:24:31 -0800510 return getModulePreferences(modeIndex);
511 }
Erin Dahlgren357b7672013-11-20 17:38:14 -0800512 return null;
513 }
514
Erin Dahlgren635a4b82013-11-25 15:21:18 -0800515 /**
Sascha Haeberlingde303232014-02-07 02:30:53 +0100516 * Based on Setting id, finds the index of a Setting's String value in an
517 * array of possible String values. If the Setting is not of type String,
518 * this returns -1.
519 * <p>
520 * TODO: make this a supported api call for all types.
521 * </p>
Erin Dahlgrene419b192013-12-03 13:10:27 -0800522 */
523 public int getStringValueIndex(int id) {
524 Setting setting = mSettingsCache.get(id);
Erin Dahlgren1648c362014-01-06 15:06:04 -0800525 if (setting == null || !TYPE_STRING.equals(setting.getType())) {
Erin Dahlgrene419b192013-12-03 13:10:27 -0800526 return -1;
527 }
Erin Dahlgren4569b702014-02-24 14:21:11 -0800528 return getStringValueIndex(setting.getStringValues(), get(id));
529 }
Erin Dahlgrene419b192013-12-03 13:10:27 -0800530
Erin Dahlgren4569b702014-02-24 14:21:11 -0800531 private int getStringValueIndex(String[] possibleValues, String value) {
Erin Dahlgrene419b192013-12-03 13:10:27 -0800532 if (value != null) {
Erin Dahlgrene419b192013-12-03 13:10:27 -0800533 if (possibleValues != null) {
534 for (int i = 0; i < possibleValues.length; i++) {
535 if (value.equals(possibleValues[i])) {
536 return i;
537 }
538 }
539 }
540 }
541 return -1;
542 }
543
544 /**
Sascha Haeberlingde303232014-02-07 02:30:53 +0100545 * Based on Setting id, sets a Setting's String value using the index into
546 * an array of possible String values. Fails to set a value if the index is
547 * out of bounds or the Setting is not of type String.
Erin Dahlgrene419b192013-12-03 13:10:27 -0800548 *
549 * @return Whether the value was set.
550 */
551 public boolean setStringValueIndex(int id, int index) {
552 Setting setting = mSettingsCache.get(id);
553 if (setting == null || setting.getType() != TYPE_STRING) {
554 return false;
555 }
556
557 String[] possibleValues = setting.getStringValues();
558 if (possibleValues != null) {
559 if (index >= 0 && index < possibleValues.length) {
560 set(id, possibleValues[index]);
561 return true;
562 }
563 }
564 return false;
565 }
566
567 /**
Erin Dahlgren4569b702014-02-24 14:21:11 -0800568 * Returns whether this Setting was last set as a String.
569 */
Erin Dahlgren9182ac82014-03-17 12:26:48 -0700570 private boolean isString(int id, String source) {
Erin Dahlgren4569b702014-02-24 14:21:11 -0800571 Setting setting = mSettingsCache.get(id);
Erin Dahlgren9182ac82014-03-17 12:26:48 -0700572 SharedPreferences preferences = getSettingSource(source);
Erin Dahlgren4569b702014-02-24 14:21:11 -0800573 try {
574 preferences.getString(setting.getKey(), null);
575 return true;
576 } catch (ClassCastException e) {
577 return false;
578 }
579 }
580
581 /**
582 * Returns whether this Setting was last set as a boolean.
583 */
Erin Dahlgren9182ac82014-03-17 12:26:48 -0700584 private boolean isBoolean(int id, String source) {
Erin Dahlgren4569b702014-02-24 14:21:11 -0800585 Setting setting = mSettingsCache.get(id);
Erin Dahlgren9182ac82014-03-17 12:26:48 -0700586 SharedPreferences preferences = getSettingSource(source);
Erin Dahlgren4569b702014-02-24 14:21:11 -0800587 try {
588 preferences.getBoolean(setting.getKey(), false);
589 return true;
590 } catch (ClassCastException e) {
591 return false;
592 }
593 }
594
595 /**
596 * Returns whether this Setting was last set as an Integer.
597 */
Erin Dahlgren9182ac82014-03-17 12:26:48 -0700598 private boolean isInteger(int id, String source) {
Erin Dahlgren4569b702014-02-24 14:21:11 -0800599 Setting setting = mSettingsCache.get(id);
Erin Dahlgren9182ac82014-03-17 12:26:48 -0700600 SharedPreferences preferences = getSettingSource(source);
Erin Dahlgren4569b702014-02-24 14:21:11 -0800601 try {
602 preferences.getInt(setting.getKey(), 0);
603 return true;
604 } catch (NumberFormatException e) {
605 return false;
606 }
607 }
608
609 /**
610 * Recover a Setting by converting it to a String if the type
611 * is known and the type conversion is successful, otherwise
612 * reset to the default.
613 */
Erin Dahlgren9182ac82014-03-17 12:26:48 -0700614 private String recoverToString(int id, String source) {
Erin Dahlgren4569b702014-02-24 14:21:11 -0800615 String value;
616 try {
Erin Dahlgren9182ac82014-03-17 12:26:48 -0700617 if (isBoolean(id, source)) {
618 value = (getBoolean(id, source) ? VALUE_ON : VALUE_OFF);
619 } else if (isInteger(id, source)) {
620 value = Integer.toString(getInt(id, source));
Erin Dahlgren4569b702014-02-24 14:21:11 -0800621 } else {
622 throw new Exception();
623 }
624 } catch (Exception e) {
625 value = mSettingsCache.get(id).getDefault();
626 }
Erin Dahlgren9182ac82014-03-17 12:26:48 -0700627 set(id, source, value);
Erin Dahlgren4569b702014-02-24 14:21:11 -0800628 return value;
629 }
630
631 /**
632 * Recover a Setting by converting it to a boolean if the type
633 * is known and the type conversion is successful, otherwise
634 * reset to the default.
635 */
Erin Dahlgren9182ac82014-03-17 12:26:48 -0700636 private boolean recoverToBoolean(int id, String source) {
Erin Dahlgren4569b702014-02-24 14:21:11 -0800637 boolean value;
638 try {
Erin Dahlgren9182ac82014-03-17 12:26:48 -0700639 if (isString(id, source)) {
640 value = VALUE_ON.equals(get(id, source));
641 } else if (isInteger(id, source)) {
642 value = getInt(id, source) != 0;
Erin Dahlgren4569b702014-02-24 14:21:11 -0800643 } else {
644 throw new Exception();
645 }
646 } catch (Exception e) {
647 value = VALUE_ON.equals(mSettingsCache.get(id).getDefault());
648 }
Erin Dahlgren9182ac82014-03-17 12:26:48 -0700649 setBoolean(id, source, value);
Erin Dahlgren4569b702014-02-24 14:21:11 -0800650 return value;
651 }
652
653 /**
654 * Recover a Setting by converting it to an Integer if the type
655 * is known and the type conversion is successful, otherwise
656 * reset to the default.
657 */
Erin Dahlgren9182ac82014-03-17 12:26:48 -0700658 private int recoverToInteger(int id, String source) {
Erin Dahlgren4569b702014-02-24 14:21:11 -0800659 int value;
660 try {
Erin Dahlgren9182ac82014-03-17 12:26:48 -0700661 if (isString(id, source)) {
662 value = Integer.parseInt(get(id, source));
663 } else if (isBoolean(id, source)) {
664 value = getBoolean(id, source) ? 1 : 0;
Erin Dahlgren4569b702014-02-24 14:21:11 -0800665 } else {
666 throw new Exception();
667 }
668 } catch (Exception e) {
669 value = Integer.parseInt(mSettingsCache.get(id).getDefault());
670 }
671 setInt(id, value);
672 return value;
673 }
674
675 /**
676 * Check if a String value is in the set of possible values for a Setting.
677 * We only keep track of possible values for String types for now.
678 */
679 private String sanitize(Setting setting, String value) {
680 if (setting.getStringValues() != null &&
681 getStringValueIndex(setting.getStringValues(), value) < 0) {
682 // If the set of possible values is not empty, and the value
683 // is not in the set of possible values, use the default, because
684 // the set of possible values probably changed.
685 return setting.getDefault();
686 }
687 return value;
688 }
689
690 /**
Erin Dahlgren635a4b82013-11-25 15:21:18 -0800691 * Get a Setting's String value based on Setting id.
692 */
Erin Dahlgren9182ac82014-03-17 12:26:48 -0700693 // TODO: rename to something more descriptive like getString.
Erin Dahlgren635a4b82013-11-25 15:21:18 -0800694 public String get(int id) {
695 Setting setting = mSettingsCache.get(id);
Erin Dahlgren9182ac82014-03-17 12:26:48 -0700696 return get(id, setting.getSource());
697 }
Erin Dahlgren4569b702014-02-24 14:21:11 -0800698
Erin Dahlgren9182ac82014-03-17 12:26:48 -0700699 /**
700 * Get a Setting's String value based on Setting id and a source file id.
701 */
702 public String get(int id, String source) {
703 Setting setting = mSettingsCache.get(id);
704 SharedPreferences preferences = getSettingSource(source);
Erin Dahlgren357b7672013-11-20 17:38:14 -0800705 if (preferences != null) {
Erin Dahlgren4569b702014-02-24 14:21:11 -0800706 try {
707 String value = preferences.getString(setting.getKey(), setting.getDefault());
708 return sanitize(setting, value);
709 } catch (ClassCastException e) {
710 // If the api defines this Setting as a String, but the
711 // last set saved it as a different type, try to recover
712 // the value, but if impossible reset to default.
Erin Dahlgren9182ac82014-03-17 12:26:48 -0700713 return recoverToString(id, source);
Erin Dahlgren4569b702014-02-24 14:21:11 -0800714 }
Erin Dahlgren357b7672013-11-20 17:38:14 -0800715 } else {
Erin Dahlgren4569b702014-02-24 14:21:11 -0800716 throw new IllegalStateException(
Erin Dahlgren9182ac82014-03-17 12:26:48 -0700717 "Setting source=" + source + " is unitialized.");
Erin Dahlgren357b7672013-11-20 17:38:14 -0800718 }
719 }
720
Erin Dahlgren635a4b82013-11-25 15:21:18 -0800721 /**
722 * Get a Setting's boolean value based on Setting id.
723 */
724 public boolean getBoolean(int id) {
725 Setting setting = mSettingsCache.get(id);
Erin Dahlgren9182ac82014-03-17 12:26:48 -0700726 return getBoolean(id, setting.getSource());
727 }
Erin Dahlgren4569b702014-02-24 14:21:11 -0800728
Erin Dahlgren9182ac82014-03-17 12:26:48 -0700729 /**
730 * Get a Setting's boolean value based on a Setting id and a source file id.
731 */
732 public boolean getBoolean(int id, String source) {
733 Setting setting = mSettingsCache.get(id);
734 SharedPreferences preferences = getSettingSource(source);
Erin Dahlgren4569b702014-02-24 14:21:11 -0800735 boolean defaultValue = VALUE_ON.equals(setting.getDefault());
Erin Dahlgren357b7672013-11-20 17:38:14 -0800736 if (preferences != null) {
Erin Dahlgren4569b702014-02-24 14:21:11 -0800737 try {
738 return preferences.getBoolean(setting.getKey(), defaultValue);
739 } catch (ClassCastException e) {
740 // If the api defines this Setting as a boolean, but the
741 // last set saved it as a different type, try to recover
742 // the value, but if impossible reset to default.
Erin Dahlgren9182ac82014-03-17 12:26:48 -0700743 return recoverToBoolean(id, source);
Erin Dahlgren4569b702014-02-24 14:21:11 -0800744 }
Erin Dahlgren357b7672013-11-20 17:38:14 -0800745 } else {
Erin Dahlgren4569b702014-02-24 14:21:11 -0800746 throw new IllegalStateException(
Erin Dahlgren9182ac82014-03-17 12:26:48 -0700747 "Setting source=" + source + " is unitialized.");
Erin Dahlgren357b7672013-11-20 17:38:14 -0800748 }
749 }
750
Erin Dahlgren635a4b82013-11-25 15:21:18 -0800751 /**
752 * Get a Setting's int value based on Setting id.
753 */
754 public int getInt(int id) {
755 Setting setting = mSettingsCache.get(id);
Erin Dahlgren9182ac82014-03-17 12:26:48 -0700756 return getInt(id, setting.getSource());
757 }
Erin Dahlgren4569b702014-02-24 14:21:11 -0800758
Erin Dahlgren9182ac82014-03-17 12:26:48 -0700759 /**
760 * Get a Setting's int value based on Setting id and a source file id.
761 */
762 public int getInt(int id, String source) {
763 Setting setting = mSettingsCache.get(id);
764 SharedPreferences preferences = getSettingSource(source);
Erin Dahlgrene419b192013-12-03 13:10:27 -0800765 int defaultValue = Integer.parseInt(setting.getDefault());
Erin Dahlgren357b7672013-11-20 17:38:14 -0800766 if (preferences != null) {
Erin Dahlgren4569b702014-02-24 14:21:11 -0800767 try {
768 return preferences.getInt(setting.getKey(), defaultValue);
769 } catch (NumberFormatException e) {
770 // If the api defines this Setting as an Integer, but the
771 // last set saved it as a different type, try to recover
772 // the value, but if impossible reset to default.
Erin Dahlgren9182ac82014-03-17 12:26:48 -0700773 return recoverToInteger(id, source);
Erin Dahlgren4569b702014-02-24 14:21:11 -0800774 }
Erin Dahlgren357b7672013-11-20 17:38:14 -0800775 } else {
Erin Dahlgren4569b702014-02-24 14:21:11 -0800776 throw new IllegalStateException(
Erin Dahlgren9182ac82014-03-17 12:26:48 -0700777 "Setting source=" + source + " is unitialized.");
Erin Dahlgren357b7672013-11-20 17:38:14 -0800778 }
779 }
780
Erin Dahlgren635a4b82013-11-25 15:21:18 -0800781 /**
782 * Set a Setting with a String value based on Setting id.
783 */
Erin Dahlgrene419b192013-12-03 13:10:27 -0800784 // TODO: rename to something more descriptive.
Erin Dahlgren635a4b82013-11-25 15:21:18 -0800785 public void set(int id, String value) {
786 Setting setting = mSettingsCache.get(id);
Erin Dahlgren9182ac82014-03-17 12:26:48 -0700787 set(id, setting.getSource(), value);
788 }
789
790 /**
791 * Set a Setting with a String value based on Setting id and a source file id.
792 */
793 public void set(int id, String source, String value) {
794 Setting setting = mSettingsCache.get(id);
Erin Dahlgren4569b702014-02-24 14:21:11 -0800795 value = sanitize(setting, value);
Erin Dahlgren9182ac82014-03-17 12:26:48 -0700796 SharedPreferences preferences = getSettingSource(source);
Erin Dahlgren357b7672013-11-20 17:38:14 -0800797 if (preferences != null) {
798 preferences.edit().putString(setting.getKey(), value).apply();
Erin Dahlgren4569b702014-02-24 14:21:11 -0800799 } else {
800 throw new IllegalStateException(
Erin Dahlgren9182ac82014-03-17 12:26:48 -0700801 "Setting source=" + source + " is unitialized.");
Erin Dahlgren357b7672013-11-20 17:38:14 -0800802 }
803 }
804
Erin Dahlgren635a4b82013-11-25 15:21:18 -0800805 /**
806 * Set a Setting with a boolean value based on Setting id.
807 */
808 public void setBoolean(int id, boolean value) {
809 Setting setting = mSettingsCache.get(id);
Erin Dahlgren9182ac82014-03-17 12:26:48 -0700810 setBoolean(id, setting.getSource(), value);
811 }
812 /**
813 * Set a Setting with a boolean value based on Setting id and a source file id.
814 */
815 public void setBoolean(int id, String source, boolean value) {
816 Setting setting = mSettingsCache.get(id);
817 SharedPreferences preferences = getSettingSource(source);
Erin Dahlgren357b7672013-11-20 17:38:14 -0800818 if (preferences != null) {
819 preferences.edit().putBoolean(setting.getKey(), value).apply();
Erin Dahlgren4569b702014-02-24 14:21:11 -0800820 } else {
821 throw new IllegalStateException(
Erin Dahlgren9182ac82014-03-17 12:26:48 -0700822 "Setting source=" + source + " is unitialized.");
Erin Dahlgren357b7672013-11-20 17:38:14 -0800823 }
824 }
825
Erin Dahlgren635a4b82013-11-25 15:21:18 -0800826 /**
827 * Set a Setting with an int value based on Setting id.
828 */
829 public void setInt(int id, int value) {
830 Setting setting = mSettingsCache.get(id);
Erin Dahlgren9182ac82014-03-17 12:26:48 -0700831 setInt(id, setting.getSource(), value);
832 }
833
834 /**
835 * Set a Setting with an int value based on Setting id and a source file id.
836 */
837 public void setInt(int id, String source, int value) {
838 Setting setting = mSettingsCache.get(id);
839 SharedPreferences preferences = getSettingSource(source);
Erin Dahlgren357b7672013-11-20 17:38:14 -0800840 if (preferences != null) {
841 preferences.edit().putInt(setting.getKey(), value).apply();
Erin Dahlgren4569b702014-02-24 14:21:11 -0800842 } else {
843 throw new IllegalStateException(
Erin Dahlgren9182ac82014-03-17 12:26:48 -0700844 "Setting source=" + source + " is unitialized.");
Erin Dahlgren357b7672013-11-20 17:38:14 -0800845 }
846 }
847
Erin Dahlgren635a4b82013-11-25 15:21:18 -0800848 /**
849 * Check if a Setting has ever been set based on Setting id.
850 */
851 public boolean isSet(int id) {
852 Setting setting = mSettingsCache.get(id);
Erin Dahlgren9182ac82014-03-17 12:26:48 -0700853 return isSet(id, setting.getSource());
854 }
855
856 /**
857 * Check if a Setting has ever been set based on Setting id and a source file id.
858 */
859 public boolean isSet(int id, String source) {
860 Setting setting = mSettingsCache.get(id);
861 SharedPreferences preferences = getSettingSource(source);
Erin Dahlgren4569b702014-02-24 14:21:11 -0800862 if (preferences != null) {
863 return preferences.contains(setting.getKey());
864 } else {
865 throw new IllegalStateException(
866 "Setting source=" + setting.getSource() + " is unitialized.");
Erin Dahlgren357b7672013-11-20 17:38:14 -0800867 }
868 }
869
Erin Dahlgren635a4b82013-11-25 15:21:18 -0800870 /**
871 * Set a Setting to its default value based on Setting id.
872 */
873 public void setDefault(int id) {
874 Setting setting = mSettingsCache.get(id);
Erin Dahlgren357b7672013-11-20 17:38:14 -0800875 SharedPreferences preferences = getSettingSource(setting);
876 if (preferences != null) {
Erin Dahlgrene419b192013-12-03 13:10:27 -0800877 preferences.edit().putString(setting.getKey(), setting.getDefault());
Erin Dahlgren4569b702014-02-24 14:21:11 -0800878 } else {
879 throw new IllegalStateException(
880 "Setting source=" + setting.getSource() + " is unitialized.");
Erin Dahlgren357b7672013-11-20 17:38:14 -0800881 }
882 }
883
Erin Dahlgren1648c362014-01-06 15:06:04 -0800884 /**
885 * Check if a Setting is set to its default value.
886 */
887 public boolean isDefault(int id) {
888 Setting setting = mSettingsCache.get(id);
889 SharedPreferences preferences = getSettingSource(setting);
890 if (preferences != null) {
891 String type = setting.getType();
892 if (TYPE_STRING.equals(type)) {
893 String value = get(id);
894 return (value.equals(setting.getDefault()));
895 } else if (TYPE_BOOLEAN.equals(type)) {
896 boolean value = getBoolean(id);
897 boolean defaultValue = VALUE_ON.equals(setting.getDefault());
898 return (value == defaultValue);
899 } else if (TYPE_INTEGER.equals(type)) {
900 int value = getInt(id);
901 int defaultValue = Integer.parseInt(setting.getDefault());
902 return (value == defaultValue);
903 } else {
904 throw new IllegalArgumentException("Type " + type + " is not known.");
905 }
906 } else {
Erin Dahlgren4569b702014-02-24 14:21:11 -0800907 throw new IllegalStateException(
908 "Setting source=" + setting.getSource() + " is unitialized.");
909 }
910 }
911
912 /**
913 * Remove a Setting from SharedPreferences.
914 */
915 public void remove(int id) {
916 Setting setting = mSettingsCache.get(id);
917 SharedPreferences preferences = getSettingSource(setting);
918 if (preferences != null) {
919 preferences.edit().remove(setting.getKey()).apply();
920 } else {
921 throw new IllegalStateException(
922 "Setting source=" + setting.getSource() + " is unitialized.");
Erin Dahlgren1648c362014-01-06 15:06:04 -0800923 }
924 }
925
Erin Dahlgrene419b192013-12-03 13:10:27 -0800926 public static Setting getLocationSetting(Context context) {
927 String defaultValue = context.getString(R.string.setting_none_value);
Sascha Haeberlingde303232014-02-07 02:30:53 +0100928 String[] values = null;
929 return new Setting(SOURCE_DEFAULT, TYPE_BOOLEAN, defaultValue, KEY_RECORD_LOCATION,
930 values, FLUSH_OFF);
Erin Dahlgren357b7672013-11-20 17:38:14 -0800931 }
932
Sascha Haeberling6ccec202014-03-11 09:44:34 -0700933 public static Setting getPictureSizeBackSetting(Context context) {
Erin Dahlgrene419b192013-12-03 13:10:27 -0800934 String defaultValue = null;
935 String[] values = context.getResources().getStringArray(
Sascha Haeberlingde303232014-02-07 02:30:53 +0100936 R.array.pref_camera_picturesize_entryvalues);
Sascha Haeberling6ccec202014-03-11 09:44:34 -0700937 return new Setting(SOURCE_DEFAULT, TYPE_STRING, defaultValue, KEY_PICTURE_SIZE_BACK,
938 values, FLUSH_OFF);
939 }
940
941 public static Setting getPictureSizeFrontSetting(Context context) {
942 String defaultValue = null;
943 String[] values = context.getResources().getStringArray(
944 R.array.pref_camera_picturesize_entryvalues);
945 return new Setting(SOURCE_DEFAULT, TYPE_STRING, defaultValue, KEY_PICTURE_SIZE_FRONT,
Sascha Haeberlingde303232014-02-07 02:30:53 +0100946 values, FLUSH_OFF);
Erin Dahlgren357b7672013-11-20 17:38:14 -0800947 }
948
Erin Dahlgrene419b192013-12-03 13:10:27 -0800949 public static Setting getDefaultCameraIdSetting(Context context,
950 SettingsCapabilities capabilities) {
951 String defaultValue = context.getString(R.string.pref_camera_id_default);
952 String[] values = null;
953 if (capabilities != null) {
954 values = capabilities.getSupportedCameraIds();
Erin Dahlgren357b7672013-11-20 17:38:14 -0800955 }
Erin Dahlgrend186f222014-01-10 11:24:31 -0800956 return new Setting(SOURCE_MODULE, TYPE_STRING, defaultValue, KEY_CAMERA_ID,
Sascha Haeberlingde303232014-02-07 02:30:53 +0100957 values, FLUSH_ON);
Erin Dahlgren357b7672013-11-20 17:38:14 -0800958 }
959
Erin Dahlgrene419b192013-12-03 13:10:27 -0800960 public static Setting getWhiteBalanceSetting(Context context) {
961 String defaultValue = context.getString(R.string.pref_camera_whitebalance_default);
962 String[] values = context.getResources().getStringArray(
Sascha Haeberlingde303232014-02-07 02:30:53 +0100963 R.array.pref_camera_whitebalance_entryvalues);
Erin Dahlgrene419b192013-12-03 13:10:27 -0800964 return new Setting(SOURCE_CAMERA, TYPE_STRING, defaultValue, KEY_WHITE_BALANCE,
Sascha Haeberlingde303232014-02-07 02:30:53 +0100965 values, FLUSH_OFF);
Erin Dahlgren357b7672013-11-20 17:38:14 -0800966 }
967
Erin Dahlgrene419b192013-12-03 13:10:27 -0800968 public static Setting getHdrSetting(Context context) {
969 String defaultValue = context.getString(R.string.pref_camera_hdr_default);
970 String[] values = context.getResources().getStringArray(
Sascha Haeberlingde303232014-02-07 02:30:53 +0100971 R.array.pref_camera_hdr_entryvalues);
Erin Dahlgrenba6994d2013-12-12 16:04:38 -0800972 return new Setting(SOURCE_GLOBAL, TYPE_STRING, defaultValue, KEY_CAMERA_HDR,
Sascha Haeberlingde303232014-02-07 02:30:53 +0100973 values, FLUSH_OFF);
Erin Dahlgren357b7672013-11-20 17:38:14 -0800974 }
975
Erin Dahlgrene419b192013-12-03 13:10:27 -0800976 public static Setting getHdrPlusSetting(Context context) {
977 String defaultValue = context.getString(R.string.pref_camera_hdr_plus_default);
978 String[] values = context.getResources().getStringArray(
Sascha Haeberlingde303232014-02-07 02:30:53 +0100979 R.array.pref_camera_hdr_plus_entryvalues);
Erin Dahlgrenba6994d2013-12-12 16:04:38 -0800980 return new Setting(SOURCE_GLOBAL, TYPE_STRING, defaultValue, KEY_CAMERA_HDR_PLUS,
Sascha Haeberlingde303232014-02-07 02:30:53 +0100981 values, FLUSH_OFF);
Erin Dahlgren357b7672013-11-20 17:38:14 -0800982 }
983
Erin Dahlgrene419b192013-12-03 13:10:27 -0800984 public static Setting getSceneModeSetting(Context context) {
985 String defaultValue = context.getString(R.string.pref_camera_scenemode_default);
986 String[] values = context.getResources().getStringArray(
Sascha Haeberlingde303232014-02-07 02:30:53 +0100987 R.array.pref_camera_scenemode_entryvalues);
Erin Dahlgrene419b192013-12-03 13:10:27 -0800988 return new Setting(SOURCE_CAMERA, TYPE_STRING, defaultValue, KEY_SCENE_MODE,
Sascha Haeberlingde303232014-02-07 02:30:53 +0100989 values, FLUSH_OFF);
Erin Dahlgren357b7672013-11-20 17:38:14 -0800990 }
991
Erin Dahlgrene419b192013-12-03 13:10:27 -0800992 public static Setting getFlashSetting(Context context) {
993 String defaultValue = context.getString(R.string.pref_camera_flashmode_default);
994 String[] values = context.getResources().getStringArray(
Sascha Haeberlingde303232014-02-07 02:30:53 +0100995 R.array.pref_camera_flashmode_entryvalues);
Erin Dahlgrene419b192013-12-03 13:10:27 -0800996 return new Setting(SOURCE_CAMERA, TYPE_STRING, defaultValue, KEY_FLASH_MODE,
Sascha Haeberlingde303232014-02-07 02:30:53 +0100997 values, FLUSH_OFF);
Erin Dahlgren357b7672013-11-20 17:38:14 -0800998 }
999
Erin Dahlgrene419b192013-12-03 13:10:27 -08001000 public static Setting getExposureSetting(Context context,
1001 SettingsCapabilities capabilities) {
1002 String defaultValue = context.getString(R.string.pref_exposure_default);
1003 String[] values = null;
1004 if (capabilities != null) {
1005 values = capabilities.getSupportedExposureValues();
Erin Dahlgren357b7672013-11-20 17:38:14 -08001006 }
Erin Dahlgrene419b192013-12-03 13:10:27 -08001007 return new Setting(SOURCE_CAMERA, TYPE_STRING, defaultValue, KEY_EXPOSURE,
Sascha Haeberlingde303232014-02-07 02:30:53 +01001008 values, FLUSH_ON);
Erin Dahlgren357b7672013-11-20 17:38:14 -08001009 }
1010
Erin Dahlgrene419b192013-12-03 13:10:27 -08001011 public static Setting getHintSetting(Context context) {
1012 String defaultValue = context.getString(R.string.setting_on_value);
1013 String[] values = null;
1014 return new Setting(SOURCE_GLOBAL, TYPE_BOOLEAN, defaultValue,
Sascha Haeberlingde303232014-02-07 02:30:53 +01001015 KEY_CAMERA_FIRST_USE_HINT_SHOWN, values, FLUSH_OFF);
Erin Dahlgren357b7672013-11-20 17:38:14 -08001016 }
1017
Erin Dahlgrene419b192013-12-03 13:10:27 -08001018 public static Setting getFocusModeSetting(Context context) {
1019 String defaultValue = null;
1020 String[] values = context.getResources().getStringArray(
Sascha Haeberlingde303232014-02-07 02:30:53 +01001021 R.array.pref_camera_focusmode_entryvalues);
Erin Dahlgrene419b192013-12-03 13:10:27 -08001022 return new Setting(SOURCE_CAMERA, TYPE_STRING, defaultValue, KEY_FOCUS_MODE,
Sascha Haeberlingde303232014-02-07 02:30:53 +01001023 values, FLUSH_OFF);
Erin Dahlgren357b7672013-11-20 17:38:14 -08001024 }
1025
Erin Dahlgrene419b192013-12-03 13:10:27 -08001026 public static Setting getTimerSetting(Context context) {
1027 String defaultValue = context.getString(R.string.pref_camera_timer_default);
1028 String[] values = null; // TODO: get the values dynamically.
1029 return new Setting(SOURCE_GLOBAL, TYPE_STRING, defaultValue, KEY_TIMER,
Sascha Haeberlingde303232014-02-07 02:30:53 +01001030 values, FLUSH_OFF);
Erin Dahlgren357b7672013-11-20 17:38:14 -08001031 }
1032
Erin Dahlgrene419b192013-12-03 13:10:27 -08001033 public static Setting getTimerSoundSetting(Context context) {
1034 String defaultValue = context.getString(R.string.pref_camera_timer_sound_default);
1035 String[] values = context.getResources().getStringArray(
Sascha Haeberlingde303232014-02-07 02:30:53 +01001036 R.array.pref_camera_timer_sound_entryvalues);
Erin Dahlgrene419b192013-12-03 13:10:27 -08001037 return new Setting(SOURCE_GLOBAL, TYPE_STRING, defaultValue, KEY_TIMER_SOUND_EFFECTS,
Sascha Haeberlingde303232014-02-07 02:30:53 +01001038 values, FLUSH_OFF);
Erin Dahlgren357b7672013-11-20 17:38:14 -08001039 }
1040
Sascha Haeberling6ccec202014-03-11 09:44:34 -07001041 public static Setting getVideoQualityBackSetting(Context context) {
Erin Dahlgrene419b192013-12-03 13:10:27 -08001042 String defaultValue = context.getString(R.string.pref_video_quality_default);
1043 String[] values = context.getResources().getStringArray(
Sascha Haeberlingde303232014-02-07 02:30:53 +01001044 R.array.pref_video_quality_entryvalues);
Sascha Haeberling6ccec202014-03-11 09:44:34 -07001045 return new Setting(SOURCE_DEFAULT, TYPE_STRING, defaultValue, KEY_VIDEO_QUALITY_BACK,
1046 values, FLUSH_OFF);
1047 }
1048
1049 public static Setting getVideoQualityFrontSetting(Context context) {
1050 String defaultValue = context.getString(R.string.pref_video_quality_default);
1051 String[] values = context.getResources().getStringArray(
1052 R.array.pref_video_quality_entryvalues);
1053 return new Setting(SOURCE_DEFAULT, TYPE_STRING, defaultValue, KEY_VIDEO_QUALITY_FRONT,
Sascha Haeberlingde303232014-02-07 02:30:53 +01001054 values, FLUSH_OFF);
Erin Dahlgren357b7672013-11-20 17:38:14 -08001055 }
1056
Erin Dahlgrene419b192013-12-03 13:10:27 -08001057 public static Setting getTimeLapseFrameIntervalSetting(Context context) {
1058 String defaultValue = context.getString(
Sascha Haeberlingde303232014-02-07 02:30:53 +01001059 R.string.pref_video_time_lapse_frame_interval_default);
Erin Dahlgrene419b192013-12-03 13:10:27 -08001060 String[] values = context.getResources().getStringArray(
Sascha Haeberlingde303232014-02-07 02:30:53 +01001061 R.array.pref_video_time_lapse_frame_interval_entryvalues);
Erin Dahlgrene419b192013-12-03 13:10:27 -08001062 return new Setting(SOURCE_GLOBAL, TYPE_STRING, defaultValue,
Sascha Haeberlingde303232014-02-07 02:30:53 +01001063 KEY_VIDEO_TIME_LAPSE_FRAME_INTERVAL, values, FLUSH_OFF);
Erin Dahlgren357b7672013-11-20 17:38:14 -08001064 }
1065
Erin Dahlgrene419b192013-12-03 13:10:27 -08001066 public static Setting getJpegQualitySetting(Context context) {
1067 String defaultValue = context.getString(
Sascha Haeberlingde303232014-02-07 02:30:53 +01001068 R.string.pref_camera_jpeg_quality_normal);
Erin Dahlgrene419b192013-12-03 13:10:27 -08001069 String[] values = context.getResources().getStringArray(
Sascha Haeberlingde303232014-02-07 02:30:53 +01001070 R.array.pref_camera_jpeg_quality_entryvalues);
Erin Dahlgrene419b192013-12-03 13:10:27 -08001071 return new Setting(SOURCE_CAMERA, TYPE_STRING, defaultValue, KEY_JPEG_QUALITY,
Sascha Haeberlingde303232014-02-07 02:30:53 +01001072 values, FLUSH_OFF);
Erin Dahlgren357b7672013-11-20 17:38:14 -08001073 }
1074
Erin Dahlgrene419b192013-12-03 13:10:27 -08001075 public static Setting getVideoFlashSetting(Context context) {
1076 String defaultValue = context.getString(R.string.pref_camera_video_flashmode_default);
1077 String[] values = context.getResources().getStringArray(
Sascha Haeberlingde303232014-02-07 02:30:53 +01001078 R.array.pref_camera_video_flashmode_entryvalues);
Erin Dahlgrene419b192013-12-03 13:10:27 -08001079 return new Setting(SOURCE_CAMERA, TYPE_STRING, defaultValue,
Sascha Haeberlingde303232014-02-07 02:30:53 +01001080 KEY_VIDEOCAMERA_FLASH_MODE, values, FLUSH_OFF);
Erin Dahlgren357b7672013-11-20 17:38:14 -08001081 }
1082
Erin Dahlgrene419b192013-12-03 13:10:27 -08001083 public static Setting getVideoEffectSetting(Context context) {
1084 String defaultValue = context.getString(R.string.pref_video_effect_default);
1085 String[] values = context.getResources().getStringArray(
Sascha Haeberlingde303232014-02-07 02:30:53 +01001086 R.array.pref_video_effect_entryvalues);
Erin Dahlgrene419b192013-12-03 13:10:27 -08001087 return new Setting(SOURCE_GLOBAL, TYPE_STRING, defaultValue, KEY_VIDEO_EFFECT,
Sascha Haeberlingde303232014-02-07 02:30:53 +01001088 values, FLUSH_OFF);
Erin Dahlgren357b7672013-11-20 17:38:14 -08001089 }
1090
Erin Dahlgrene419b192013-12-03 13:10:27 -08001091 public static Setting getHintVideoSetting(Context context) {
1092 String defaultValue = context.getString(R.string.setting_on_value);
1093 String[] values = null;
1094 return new Setting(SOURCE_GLOBAL, TYPE_STRING, defaultValue,
Sascha Haeberlingde303232014-02-07 02:30:53 +01001095 KEY_VIDEO_FIRST_USE_HINT_SHOWN, values, FLUSH_OFF);
Erin Dahlgren357b7672013-11-20 17:38:14 -08001096 }
1097
Erin Dahlgrene419b192013-12-03 13:10:27 -08001098 public static Setting getStartupModuleSetting(Context context) {
1099 String defaultValue = context.getString(R.string.pref_camera_startup_index_default);
1100 String[] values = null;
1101 return new Setting(SOURCE_DEFAULT, TYPE_INTEGER, defaultValue,
Sascha Haeberlingde303232014-02-07 02:30:53 +01001102 KEY_STARTUP_MODULE_INDEX, values, FLUSH_OFF);
Erin Dahlgren357b7672013-11-20 17:38:14 -08001103 }
1104
Doris Liucf8b6532014-01-15 19:17:38 -08001105 public static Setting getLastUsedCameraModule(Context context) {
1106 String defaultValue = Integer.toString(context.getResources()
1107 .getInteger(R.integer.camera_mode_photo));
1108 return new Setting(SOURCE_DEFAULT, TYPE_INTEGER, defaultValue,
1109 KEY_CAMERA_MODULE_LAST_USED, null, FLUSH_OFF);
1110 }
1111
Erin Dahlgrena1fab412014-01-21 09:31:11 -08001112 public static Setting getPanoOrientationSetting(Context context) {
1113 String defaultValue = context.getString(R.string.pano_orientation_horizontal);
1114 String[] values = context.getResources().getStringArray(
Sascha Haeberlingde303232014-02-07 02:30:53 +01001115 R.array.pref_camera_pano_orientation_entryvalues);
Erin Dahlgrena1fab412014-01-21 09:31:11 -08001116 return new Setting(SOURCE_GLOBAL, TYPE_STRING, defaultValue,
Sascha Haeberlingde303232014-02-07 02:30:53 +01001117 KEY_CAMERA_PANO_ORIENTATION, values, FLUSH_OFF);
Erin Dahlgrena1fab412014-01-21 09:31:11 -08001118 }
1119
Erin Dahlgrend5e51462014-02-07 12:38:57 -08001120 public static Setting getGridLinesSetting(Context context) {
1121 String defaultValue = context.getString(R.string.setting_off_value);
1122 String[] values = context.getResources().getStringArray(
Sascha Haeberlingde303232014-02-07 02:30:53 +01001123 R.array.pref_camera_gridlines_entryvalues);
Erin Dahlgrend5e51462014-02-07 12:38:57 -08001124 return new Setting(SOURCE_GLOBAL, TYPE_STRING, defaultValue,
Sascha Haeberlingde303232014-02-07 02:30:53 +01001125 KEY_CAMERA_GRID_LINES, values, FLUSH_OFF);
Erin Dahlgrend5e51462014-02-07 12:38:57 -08001126 }
1127
Sascha Haeberlingc26a3282014-02-19 08:39:28 -08001128 public static Setting getReleaseDialogLastShownVersionSetting(Context context) {
1129 return new Setting(SOURCE_DEFAULT, TYPE_STRING, null,
1130 KEY_RELEASE_DIALOG_LAST_SHOWN_VERSION, null, FLUSH_OFF);
1131 }
1132
Erin Dahlgrene346fb22014-02-19 17:23:01 -08001133 public static Setting getFlashSupportedBackCameraSetting(Context context) {
1134 String defaultValue = context.getString(R.string.setting_none_value);
1135 return new Setting(SOURCE_GLOBAL, TYPE_BOOLEAN, defaultValue,
1136 KEY_FLASH_SUPPORTED_BACK_CAMERA, null, FLUSH_OFF);
1137 }
1138
Erin Dahlgren4569b702014-02-24 14:21:11 -08001139 public static Setting getStrictUpgradeVersionSetting(Context context) {
1140 String defaultValue = "0";
1141 return new Setting(SOURCE_DEFAULT, TYPE_INTEGER, defaultValue,
1142 KEY_STRICT_UPGRADE_VERSION, null, FLUSH_OFF);
1143 }
1144
Erin Dahlgren1ba90e32014-03-03 12:05:57 -08001145 public static Setting getRequestReturnHdrPlusSetting(Context context) {
1146 String defaultValue = context.getString(R.string.setting_none_value);
1147 return new Setting(SOURCE_MODULE, TYPE_BOOLEAN, VALUE_OFF,
1148 KEY_REQUEST_RETURN_HDR_PLUS, null, FLUSH_OFF);
1149 }
1150
Erin Dahlgren635a4b82013-11-25 15:21:18 -08001151 // Utilities.
Erin Dahlgren8a2933b2013-12-06 12:07:33 -08001152
1153 /**
Sascha Haeberlingde303232014-02-07 02:30:53 +01001154 * Returns whether the camera has been set to back facing in settings.
Erin Dahlgren8a2933b2013-12-06 12:07:33 -08001155 */
1156 public boolean isCameraBackFacing() {
Erin Dahlgrend186f222014-01-10 11:24:31 -08001157 String cameraFacing = get(SETTING_CAMERA_ID);
1158 String backFacing = mContext.getString(R.string.pref_camera_id_default);
1159 return (Integer.parseInt(cameraFacing) == Integer.parseInt(backFacing));
Erin Dahlgren8a2933b2013-12-06 12:07:33 -08001160 }
1161
Erin Dahlgrena340f072014-01-06 17:31:23 -08001162 /**
Erin Dahlgrena340f072014-01-06 17:31:23 -08001163 * Returns whether hdr plus mode is set on.
1164 */
1165 public boolean isHdrPlusOn() {
1166 String hdrOn = get(SettingsManager.SETTING_CAMERA_HDR);
1167 return hdrOn.equals(SettingsManager.VALUE_ON);
1168 }
1169
Erin Dahlgrend5e51462014-02-07 12:38:57 -08001170 /**
Erin Dahlgren1ba90e32014-03-03 12:05:57 -08001171 * Returns whether the app should return to hdr plus mode if possible.
1172 */
1173 public boolean requestsReturnToHdrPlus() {
1174 return getBoolean(SettingsManager.SETTING_REQUEST_RETURN_HDR_PLUS);
1175 }
1176
1177 /**
Erin Dahlgrend5e51462014-02-07 12:38:57 -08001178 * Returns whether grid lines are set on.
1179 */
1180 public boolean areGridLinesOn() {
1181 String gridLinesOn = get(SettingsManager.SETTING_CAMERA_GRID_LINES);
1182 return gridLinesOn.equals(SettingsManager.VALUE_ON);
1183 }
1184
Erin Dahlgrenf80ac9e2014-02-18 11:20:34 -08001185 /**
1186 * Returns whether pano orientation is horizontal.
1187 */
1188 public boolean isPanoOrientationHorizontal() {
1189 String orientation = get(SettingsManager.SETTING_CAMERA_PANO_ORIENTATION);
1190 String horizontal = mContext.getString(R.string.pano_orientation_horizontal);
1191 return orientation.equals(horizontal);
1192 }
1193
Sascha Haeberlingde303232014-02-07 02:30:53 +01001194 // TODO: refactor this into a separate utils module.
Erin Dahlgren357b7672013-11-20 17:38:14 -08001195
Erin Dahlgren635a4b82013-11-25 15:21:18 -08001196 /**
Sascha Haeberlingde303232014-02-07 02:30:53 +01001197 * Get a String value from first the ListPreference, and if not found from
1198 * the SettingsManager. This is a wrapper that adds backwards compatibility
1199 * to views that rely on PreferenceGroups.
Erin Dahlgren635a4b82013-11-25 15:21:18 -08001200 */
Erin Dahlgren357b7672013-11-20 17:38:14 -08001201 public String getValueFromPreference(ListPreference pref) {
1202 String value = pref.getValue();
1203 if (value == null) {
Erin Dahlgren635a4b82013-11-25 15:21:18 -08001204 Integer id = mSettingsCache.getId(pref.getKey());
1205 if (id == null) {
1206 return null;
1207 }
1208 value = get(id);
Erin Dahlgren357b7672013-11-20 17:38:14 -08001209 }
1210 return value;
1211 }
1212
Erin Dahlgren635a4b82013-11-25 15:21:18 -08001213 /**
Sascha Haeberlingde303232014-02-07 02:30:53 +01001214 * Set a String value first from the ListPreference, and if unable from the
1215 * SettingsManager. This is a wrapper that adds backwards compatibility to
1216 * views that rely on PreferenceGroups.
Erin Dahlgren635a4b82013-11-25 15:21:18 -08001217 */
Erin Dahlgren357b7672013-11-20 17:38:14 -08001218 public void setValueFromPreference(ListPreference pref, String value) {
1219 boolean set = pref.setValue(value);
1220 if (!set) {
Erin Dahlgren635a4b82013-11-25 15:21:18 -08001221 Integer id = mSettingsCache.getId(pref.getKey());
1222 if (id != null) {
1223 set(id, value);
1224 }
Erin Dahlgren357b7672013-11-20 17:38:14 -08001225 }
1226 }
1227
Erin Dahlgren635a4b82013-11-25 15:21:18 -08001228 /**
1229 * Set a String value first from the ListPreference based on a
Sascha Haeberlingde303232014-02-07 02:30:53 +01001230 * ListPreference index, and if unable use the ListPreference key to set the
1231 * value using the SettingsManager. This is a wrapper that adds backwards
1232 * compatibility to views that rely on PreferenceGroups.
Erin Dahlgren635a4b82013-11-25 15:21:18 -08001233 */
Erin Dahlgren357b7672013-11-20 17:38:14 -08001234 public void setValueIndexFromPreference(ListPreference pref, int index) {
1235 boolean set = pref.setValueIndex(index);
1236 if (!set) {
Erin Dahlgren635a4b82013-11-25 15:21:18 -08001237 Integer id = mSettingsCache.getId(pref.getKey());
1238 if (id != null) {
1239 String value = pref.getValueAtIndex(index);
1240 set(id, value);
1241 }
Erin Dahlgren357b7672013-11-20 17:38:14 -08001242 }
1243 }
Sascha Haeberlingde303232014-02-07 02:30:53 +01001244
1245 /**
1246 * Sets the settings for whether location recording should be enabled or
1247 * not. Also makes sure to pass on the change to the location manager.
1248 */
1249 public void setLocation(boolean on, LocationManager locationManager) {
1250 setBoolean(SettingsManager.SETTING_RECORD_LOCATION, on);
1251 locationManager.recordLocation(on);
1252 }
1253
1254 /**
1255 * Reads the current location recording settings and passes it on to the
1256 * given location manager.
1257 */
1258 public void syncLocationManager(LocationManager locationManager) {
Erin Dahlgren4569b702014-02-24 14:21:11 -08001259 boolean value = getBoolean(SettingsManager.SETTING_RECORD_LOCATION);
Sascha Haeberlingde303232014-02-07 02:30:53 +01001260 locationManager.recordLocation(value);
1261 }
Erin Dahlgren357b7672013-11-20 17:38:14 -08001262}