blob: 0dedf0d90117bda9d1c2f149323d600374b59b7e [file] [log] [blame]
package org.robolectric.shadows;
import static android.os.Build.VERSION_CODES.JELLY_BEAN_MR1;
import static android.os.Build.VERSION_CODES.M;
import android.content.ContentResolver;
import android.content.Context;
import android.provider.Settings;
import java.util.HashMap;
import java.util.Map;
import java.util.WeakHashMap;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.annotation.Resetter;
@SuppressWarnings({"UnusedDeclaration"})
@Implements(Settings.class)
public class ShadowSettings {
@Implements(value = Settings.System.class)
public static class ShadowSystem {
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(minSdk = JELLY_BEAN_MR1)
public static String getStringForUser(ContentResolver cr, String name, int userHandle) {
return getString(cr, name);
}
@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);
}
}
private static Map<String, Object> get(ContentResolver cr) {
Map<String, Object> map = dataMap.get(cr);
if (map == null) {
map = new HashMap<>();
dataMap.put(cr, map);
}
return map;
}
}
@Implements(value = Settings.Secure.class, inheritImplementationMethods = true)
public static class ShadowSecure extends ShadowSystem {
}
@Implements(value = Settings.Global.class, inheritImplementationMethods = true, minSdk = JELLY_BEAN_MR1)
public static class ShadowGlobal extends ShadowSystem {
}
/**
* Sets the value of the {@link Settings.System#AIRPLANE_MODE_ON} setting.
*
* @param isAirplaneMode new status for airplane mode
*/
public static void setAirplaneMode(boolean isAirplaneMode) {
Settings.System.putInt(RuntimeEnvironment.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(RuntimeEnvironment.application.getContentResolver(), Settings.Secure.WIFI_ON, isOn ? 1 : 0);
}
/**
* Sets the value of the {@link Settings.System#TIME_12_24} setting.
*
* @param use24HourTimeFormat new status for the time setting
*/
public static void set24HourTimeFormat(boolean use24HourTimeFormat) {
Settings.System.putString(RuntimeEnvironment.application.getContentResolver(), Settings.System.TIME_12_24, use24HourTimeFormat ? "24" : "12");
}
private static boolean canDrawOverlays = false;
/** @return `false` by default, or the value specified via {@link #setCanDrawOverlays(boolean)} */
@Implementation(minSdk = M)
protected static boolean canDrawOverlays(Context context) {
return canDrawOverlays;
}
/** Sets the value returned by {@link #canDrawOverlays(Context)}. */
public static void setCanDrawOverlays(boolean canDrawOverlays) {
ShadowSettings.canDrawOverlays = canDrawOverlays;
}
@Resetter
public static void reset() {
canDrawOverlays = false;
}
}