• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.xtremelabs.robolectric.shadows;
2 
3 import android.content.ContentResolver;
4 import android.provider.Settings;
5 import com.xtremelabs.robolectric.Robolectric;
6 import com.xtremelabs.robolectric.internal.Implementation;
7 import com.xtremelabs.robolectric.internal.Implements;
8 
9 import java.util.HashMap;
10 import java.util.Map;
11 import 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)
19 public 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
putInt(ContentResolver cr, String name, int value)25         public static boolean putInt(ContentResolver cr, String name, int value) {
26             get(cr).put(name, value);
27             return true;
28         }
29 
30         @Implementation
getInt(ContentResolver cr, String name, int def)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
getInt(ContentResolver cr, String name)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
putString(ContentResolver cr, String name, String value)49         public static boolean putString(ContentResolver cr, String name, String value) {
50             get(cr).put(name, value);
51             return true;
52         }
53 
54         @Implementation
getString(ContentResolver cr, String name)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
putLong(ContentResolver cr, String name, long value)64         public static boolean putLong(ContentResolver cr, String name, long value) {
65             get(cr).put(name, value);
66             return true;
67         }
68 
69         @Implementation
getLong(ContentResolver cr, String name, long def)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
getLong(ContentResolver cr, String name)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
putFloat(ContentResolver cr, String name, float value)88         public static boolean putFloat(ContentResolver cr, String name, float value) {
89             get(cr).put(name, value);
90             return true;
91         }
92 
93         @Implementation
getFloat(ContentResolver cr, String name, float def)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
getFloat(ContentResolver cr, String name)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
get(ContentResolver cr)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      */
setAirplaneMode(boolean isAirplaneMode)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      */
setWifiOn(boolean isOn)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      */
set24HourTimeFormat(boolean use24HourTimeFormat)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