1/*
2 * Copyright (C) 2014 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
19import java.util.HashMap;
20
21/**
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
26 * requested:
27 *
28 * <ul>getString: default is null</ul>
29 * <ul>getInteger: default is 0</ul>
30 * <ul>getBoolean: default is false</ul>
31 *
32 * If possible values aren't specified for a
33 * SharedPreferences key, then calling getIndexOfCurrentValue
34 * and setValueByIndex will throw an IllegalArgumentException.
35 */
36class DefaultsStore {
37
38    /**
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.
43     */
44    private static class Defaults {
45        private String mDefaultValue;
46        private String[] mPossibleValues;
47
48        public Defaults(String defaultValue, String[] possibleValues) {
49            mDefaultValue = defaultValue;
50            mPossibleValues = possibleValues;
51        }
52
53        public String getDefaultValue() {
54            return mDefaultValue;
55        }
56
57        public String[] getPossibleValues() {
58            return mPossibleValues;
59        }
60    }
61
62    /** Map of Defaults for SharedPreferences keys. */
63    private static HashMap<String, Defaults> mDefaultsInternalStore =
64        new HashMap<String, Defaults>();
65
66    /**
67     * Store a default value and a set of possible values
68     * for a SharedPreferences key.
69     */
70    public void storeDefaults(String key, String defaultValue, String[] possibleValues) {
71        Defaults defaults = new Defaults(defaultValue, possibleValues);
72        mDefaultsInternalStore.put(key, defaults);
73    }
74
75    /**
76     * Get the default value for a SharedPreferences key,
77     * if one has been stored.
78     */
79    public String getDefaultValue(String key) {
80        Defaults defaults = mDefaultsInternalStore.get(key);
81        if (defaults == null) {
82            return null;
83        }
84        return defaults.getDefaultValue();
85    }
86
87    /**
88     * Get the set of possible values for a SharedPreferences key,
89     * if a set has been stored.
90     */
91    public String[] getPossibleValues(String key) {
92        Defaults defaults = mDefaultsInternalStore.get(key);
93        if (defaults == null) {
94            return null;
95        }
96        return defaults.getPossibleValues();
97    }
98}