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