aboutsummaryrefslogtreecommitdiff
path: root/v1/src/main/java/com/xtremelabs/robolectric/shadows/ShadowSettings.java
blob: f8787eacd1116fccac32a3bd4473c7c87d42a2d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package com.xtremelabs.robolectric.shadows;

import android.content.ContentResolver;
import android.provider.Settings;
import com.xtremelabs.robolectric.Robolectric;
import com.xtremelabs.robolectric.internal.Implementation;
import com.xtremelabs.robolectric.internal.Implements;

import java.util.HashMap;
import java.util.Map;
import java.util.WeakHashMap;

/**
 * Shadow of {@code Settings} that allows the status of various System and Secure settings to be simulated, changed and
 * queried.
 */
@SuppressWarnings({"UnusedDeclaration"})
@Implements(Settings.class)
public class ShadowSettings {
    @Implements(Settings.class)
    private static class SettingsImpl {
        private static final WeakHashMap<ContentResolver, Map<String, Object>> dataMap = new WeakHashMap<ContentResolver, Map<String, Object>>();

        @Implementation
        public static boolean putInt(ContentResolver cr, String name, int value) {
            get(cr).put(name, value);
            return true;
        }

        @Implementation
        public static int getInt(ContentResolver cr, String name, int def) {
            if (get(cr).get(name) instanceof Integer) {
                return (Integer) get(cr).get(name);
            } else {
                return def;
            }
        }

        @Implementation
        public static int getInt(ContentResolver cr, String name) throws Settings.SettingNotFoundException {
            if (get(cr).get(name) instanceof Integer) {
                return (Integer) get(cr).get(name);
            } else {
                throw new Settings.SettingNotFoundException(name);
            }
        }

        @Implementation
        public static boolean putString(ContentResolver cr, String name, String value) {
            get(cr).put(name, value);
            return true;
        }

        @Implementation
        public static String getString(ContentResolver cr, String name) {
            if (get(cr).get(name) instanceof String) {
                return (String) get(cr).get(name);
            } else {
                return null;
            }
        }

        @Implementation
        public static boolean putLong(ContentResolver cr, String name, long value) {
            get(cr).put(name, value);
            return true;
        }

        @Implementation
        public static long getLong(ContentResolver cr, String name, long def) {
            if (get(cr).get(name) instanceof Long) {
                return (Long) get(cr).get(name);
            } else {
                return def;
            }
        }

        @Implementation
        public static long getLong(ContentResolver cr, String name) throws Settings.SettingNotFoundException {
            if (get(cr).get(name) instanceof Long) {
                return (Long) get(cr).get(name);
            } else {
                throw new Settings.SettingNotFoundException(name);
            }
        }

        @Implementation
        public static boolean putFloat(ContentResolver cr, String name, float value) {
            get(cr).put(name, value);
            return true;
        }

        @Implementation
        public static float getFloat(ContentResolver cr, String name, float def) {
            if (get(cr).get(name) instanceof Float) {
                return (Float) get(cr).get(name);
            } else {
                return def;
            }
        }

        @Implementation
        public static float getFloat(ContentResolver cr, String name) throws Settings.SettingNotFoundException {
            if (get(cr).get(name) instanceof Float) {
                return (Float) get(cr).get(name);
            } else {
                throw new Settings.SettingNotFoundException(name);
            }
        }

        @Implementation
        private static Map<String, Object> get(ContentResolver cr) {
            Map<String, Object> map = dataMap.get(cr);
            if (map == null) {
                map = new HashMap<String, Object>();
                dataMap.put(cr, map);
            }
            return map;
        }
    }

    @Implements(Settings.System.class)
    public static class ShadowSystem extends SettingsImpl {
    }

    @Implements(Settings.Secure.class)
    public static class ShadowSecure extends SettingsImpl {
    }

    @Implements(Settings.Global.class)
    public static class ShadowGlobal extends SettingsImpl {
    }

    /**
     * Non-Android accessor that allows the value of the AIRPLANE_MODE_ON setting to be set.
     *
     * @param isAirplaneMode new status for airplane mode
     */
    public static void setAirplaneMode(boolean isAirplaneMode) {
        Settings.System.putInt(Robolectric.application.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, isAirplaneMode ? 1 : 0);
    }

    /**
     * Non-Android accessor that allows the value of the WIFI_ON setting to be set.
     *
     * @param isOn new status for wifi mode
     */
    public static void setWifiOn(boolean isOn) {
        Settings.Secure.putInt(Robolectric.application.getContentResolver(), Settings.Secure.WIFI_ON, isOn ? 1 : 0);
    }

    /**
     * Non-Android accessor thatallows the value of the TIME_12_24 setting to be set.
     *
     * @param use24HourTimeFormat new status for the time setting
     */
    public static void set24HourTimeFormat(boolean use24HourTimeFormat) {
        Settings.System.putInt(Robolectric.application.getContentResolver(), Settings.System.TIME_12_24, use24HourTimeFormat ? 24 : 12);
    }
}