2 * Copyright (C) 2014 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.camera.settings;
19 import java.util.HashMap;
22 * A class for storing default values and possible values of
23 * SharedPreferences settings. It is optional to store defaults
24 * and possible values for a setting. If a default is not specified,
25 * the SettingsManager API chooses a default based on the type
28 * <ul>getString: default is null</ul>
29 * <ul>getInteger: default is 0</ul>
30 * <ul>getBoolean: default is false</ul>
32 * If possible values aren't specified for a
33 * SharedPreferences key, then calling getIndexOfCurrentValue
34 * and setValueByIndex will throw an IllegalArgumentException.
39 * A class for storing a default value and set of possible
40 * values. Since all settings values are saved as Strings in
41 * SharedPreferences, the default and possible values are
42 * Strings. This simplifies default values management.
44 private static class Defaults {
45 private String mDefaultValue;
46 private String[] mPossibleValues;
48 public Defaults(String defaultValue, String[] possibleValues) {
49 mDefaultValue = defaultValue;
50 mPossibleValues = possibleValues;
53 public String getDefaultValue() {
57 public String[] getPossibleValues() {
58 return mPossibleValues;
62 /** Map of Defaults for SharedPreferences keys. */
63 private static HashMap<String, Defaults> mDefaultsInternalStore =
64 new HashMap<String, Defaults>();
67 * Store a default value and a set of possible values
68 * for a SharedPreferences key.
70 public void storeDefaults(String key, String defaultValue, String[] possibleValues) {
71 Defaults defaults = new Defaults(defaultValue, possibleValues);
72 mDefaultsInternalStore.put(key, defaults);
76 * Get the default value for a SharedPreferences key,
77 * if one has been stored.
79 public String getDefaultValue(String key) {
80 Defaults defaults = mDefaultsInternalStore.get(key);
81 if (defaults == null) {
84 return defaults.getDefaultValue();
88 * Get the set of possible values for a SharedPreferences key,
89 * if a set has been stored.
91 public String[] getPossibleValues(String key) {
92 Defaults defaults = mDefaultsInternalStore.get(key);
93 if (defaults == null) {
96 return defaults.getPossibleValues();