1 /*
2  * Copyright (C) 2012 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.calendar;
18 
19 import android.content.SharedPreferences;
20 import android.content.SharedPreferences.Editor;
21 import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
22 
23 import java.util.HashMap;
24 import java.util.Map;
25 import java.util.Set;
26 
27 class FakeSharedPreferences implements SharedPreferences, SharedPreferences.Editor {
28 
29     private HashMap<String, Object> mValues = new HashMap<String, Object>();
30     private HashMap<String, Object> mTempValues = new HashMap<String, Object>();
31 
32     @Override
edit()33     public Editor edit() {
34         return this;
35     }
36 
37     @Override
contains(String key)38     public boolean contains(String key) {
39         return mValues.containsKey(key);
40     }
41 
42     @Override
getAll()43     public Map<String, ?> getAll() {
44         return new HashMap<String, Object>(mValues);
45     }
46 
47     @Override
getBoolean(String key, boolean defValue)48     public boolean getBoolean(String key, boolean defValue) {
49         if (mValues.containsKey(key)) {
50             return ((Boolean)mValues.get(key)).booleanValue();
51         }
52         return defValue;
53     }
54 
55     @Override
getFloat(String key, float defValue)56     public float getFloat(String key, float defValue) {
57         if (mValues.containsKey(key)) {
58             return ((Float)mValues.get(key)).floatValue();
59         }
60         return defValue;
61     }
62 
63     @Override
getInt(String key, int defValue)64     public int getInt(String key, int defValue) {
65         if (mValues.containsKey(key)) {
66             return ((Integer)mValues.get(key)).intValue();
67         }
68         return defValue;
69     }
70 
71     @Override
getLong(String key, long defValue)72     public long getLong(String key, long defValue) {
73         if (mValues.containsKey(key)) {
74             return ((Long)mValues.get(key)).longValue();
75         }
76         return defValue;
77     }
78 
79     @Override
getString(String key, String defValue)80     public String getString(String key, String defValue) {
81         if (mValues.containsKey(key))
82             return (String)mValues.get(key);
83         return defValue;
84     }
85 
86     @SuppressWarnings("unchecked")
87     @Override
getStringSet(String key, Set<String> defValues)88     public Set<String> getStringSet(String key, Set<String> defValues) {
89         if (mValues.containsKey(key)) {
90             return (Set<String>) mValues.get(key);
91         }
92         return defValues;
93     }
94 
95     @Override
registerOnSharedPreferenceChangeListener( OnSharedPreferenceChangeListener listener)96     public void registerOnSharedPreferenceChangeListener(
97             OnSharedPreferenceChangeListener listener) {
98         throw new UnsupportedOperationException();
99     }
100 
101     @Override
unregisterOnSharedPreferenceChangeListener( OnSharedPreferenceChangeListener listener)102     public void unregisterOnSharedPreferenceChangeListener(
103             OnSharedPreferenceChangeListener listener) {
104         throw new UnsupportedOperationException();
105     }
106 
107     @Override
putBoolean(String key, boolean value)108     public Editor putBoolean(String key, boolean value) {
109         mTempValues.put(key, Boolean.valueOf(value));
110         return this;
111     }
112 
113     @Override
putFloat(String key, float value)114     public Editor putFloat(String key, float value) {
115         mTempValues.put(key, value);
116         return this;
117     }
118 
119     @Override
putInt(String key, int value)120     public Editor putInt(String key, int value) {
121         mTempValues.put(key, value);
122         return this;
123     }
124 
125     @Override
putLong(String key, long value)126     public Editor putLong(String key, long value) {
127         mTempValues.put(key, value);
128         return this;
129     }
130 
131     @Override
putString(String key, String value)132     public Editor putString(String key, String value) {
133         mTempValues.put(key, value);
134         return this;
135     }
136 
137     @Override
putStringSet(String key, Set<String> values)138     public Editor putStringSet(String key, Set<String> values) {
139         mTempValues.put(key, values);
140         return this;
141     }
142 
143     @Override
remove(String key)144     public Editor remove(String key) {
145         mTempValues.remove(key);
146         return this;
147     }
148 
149     @Override
clear()150     public Editor clear() {
151         mTempValues.clear();
152         return this;
153     }
154 
155     @SuppressWarnings("unchecked")
156     @Override
commit()157     public boolean commit() {
158         mValues = (HashMap<String, Object>)mTempValues.clone();
159         return true;
160     }
161 
162     @Override
apply()163     public void apply() {
164         commit();
165     }
166 }