1package com.xtremelabs.robolectric.shadows; 2 3import android.content.ContentResolver; 4import android.provider.Settings; 5import com.xtremelabs.robolectric.Robolectric; 6import com.xtremelabs.robolectric.internal.Implementation; 7import com.xtremelabs.robolectric.internal.Implements; 8 9import java.util.HashMap; 10import java.util.Map; 11import java.util.WeakHashMap; 12 13/** 14 * Shadow of {@code Settings} that allows the status of various System and Secure settings to be simulated, changed and 15 * queried. 16 */ 17@SuppressWarnings({"UnusedDeclaration"}) 18@Implements(Settings.class) 19public class ShadowSettings { 20 @Implements(Settings.class) 21 private static class SettingsImpl { 22 private static final WeakHashMap<ContentResolver, Map<String, Object>> dataMap = new WeakHashMap<ContentResolver, Map<String, Object>>(); 23 24 @Implementation 25 public static boolean putInt(ContentResolver cr, String name, int value) { 26 get(cr).put(name, value); 27 return true; 28 } 29 30 @Implementation 31 public static int getInt(ContentResolver cr, String name, int def) { 32 if (get(cr).get(name) instanceof Integer) { 33 return (Integer) get(cr).get(name); 34 } else { 35 return def; 36 } 37 } 38 39 @Implementation 40 public static int getInt(ContentResolver cr, String name) throws Settings.SettingNotFoundException { 41 if (get(cr).get(name) instanceof Integer) { 42 return (Integer) get(cr).get(name); 43 } else { 44 throw new Settings.SettingNotFoundException(name); 45 } 46 } 47 48 @Implementation 49 public static boolean putString(ContentResolver cr, String name, String value) { 50 get(cr).put(name, value); 51 return true; 52 } 53 54 @Implementation 55 public static String getString(ContentResolver cr, String name) { 56 if (get(cr).get(name) instanceof String) { 57 return (String) get(cr).get(name); 58 } else { 59 return null; 60 } 61 } 62 63 @Implementation 64 public static boolean putLong(ContentResolver cr, String name, long value) { 65 get(cr).put(name, value); 66 return true; 67 } 68 69 @Implementation 70 public static long getLong(ContentResolver cr, String name, long def) { 71 if (get(cr).get(name) instanceof Long) { 72 return (Long) get(cr).get(name); 73 } else { 74 return def; 75 } 76 } 77 78 @Implementation 79 public static long getLong(ContentResolver cr, String name) throws Settings.SettingNotFoundException { 80 if (get(cr).get(name) instanceof Long) { 81 return (Long) get(cr).get(name); 82 } else { 83 throw new Settings.SettingNotFoundException(name); 84 } 85 } 86 87 @Implementation 88 public static boolean putFloat(ContentResolver cr, String name, float value) { 89 get(cr).put(name, value); 90 return true; 91 } 92 93 @Implementation 94 public static float getFloat(ContentResolver cr, String name, float def) { 95 if (get(cr).get(name) instanceof Float) { 96 return (Float) get(cr).get(name); 97 } else { 98 return def; 99 } 100 } 101 102 @Implementation 103 public static float getFloat(ContentResolver cr, String name) throws Settings.SettingNotFoundException { 104 if (get(cr).get(name) instanceof Float) { 105 return (Float) get(cr).get(name); 106 } else { 107 throw new Settings.SettingNotFoundException(name); 108 } 109 } 110 111 @Implementation 112 private static Map<String, Object> get(ContentResolver cr) { 113 Map<String, Object> map = dataMap.get(cr); 114 if (map == null) { 115 map = new HashMap<String, Object>(); 116 dataMap.put(cr, map); 117 } 118 return map; 119 } 120 } 121 122 @Implements(Settings.System.class) 123 public static class ShadowSystem extends SettingsImpl { 124 } 125 126 @Implements(Settings.Secure.class) 127 public static class ShadowSecure extends SettingsImpl { 128 } 129 130 @Implements(Settings.Global.class) 131 public static class ShadowGlobal extends SettingsImpl { 132 } 133 134 /** 135 * Non-Android accessor that allows the value of the AIRPLANE_MODE_ON setting to be set. 136 * 137 * @param isAirplaneMode new status for airplane mode 138 */ 139 public static void setAirplaneMode(boolean isAirplaneMode) { 140 Settings.System.putInt(Robolectric.application.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, isAirplaneMode ? 1 : 0); 141 } 142 143 /** 144 * Non-Android accessor that allows the value of the WIFI_ON setting to be set. 145 * 146 * @param isOn new status for wifi mode 147 */ 148 public static void setWifiOn(boolean isOn) { 149 Settings.Secure.putInt(Robolectric.application.getContentResolver(), Settings.Secure.WIFI_ON, isOn ? 1 : 0); 150 } 151 152 /** 153 * Non-Android accessor thatallows the value of the TIME_12_24 setting to be set. 154 * 155 * @param use24HourTimeFormat new status for the time setting 156 */ 157 public static void set24HourTimeFormat(boolean use24HourTimeFormat) { 158 Settings.System.putInt(Robolectric.application.getContentResolver(), Settings.System.TIME_12_24, use24HourTimeFormat ? 24 : 12); 159 } 160} 161