use of android.content.SharedPreferences in project jpHolo by teusink.
the class AndroidPreferences method execute.
@Override
public boolean execute(final String action, final JSONArray args, final CallbackContext callbackContext) {
cordova.getThreadPool().execute(new Runnable() {
@Override
public void run() {
try {
final JSONObject params = args.getJSONObject(0);
final String preferenceLib = params.getString("preferenceLib");
final String preferenceName = params.getString("preferenceName");
final String preferenceValue = params.getString("preferenceValue");
if (preferenceLib != null && preferenceName != null && preferenceValue != null && !preferenceLib.equals("") && !preferenceName.equals("")) {
final SharedPreferences settings = cordova.getActivity().getSharedPreferences(preferenceLib, Context.MODE_PRIVATE);
final SharedPreferences.Editor editor = settings.edit();
if (action.equals("set") && settings != null && editor != null) {
editor.putString(preferenceName, preferenceValue);
editor.commit();
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK));
} else if (action.equals("get") && settings != null && editor != null) {
final String returnValue = settings.getString(preferenceName, "");
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, returnValue));
} else {
Log.e(LOG_PROV, LOG_NAME + "Error: " + PluginResult.Status.INVALID_ACTION);
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.INVALID_ACTION));
}
} else {
Log.e(LOG_PROV, LOG_NAME + "Error: " + PluginResult.Status.ERROR);
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR));
}
} catch (final JSONException e) {
e.printStackTrace();
Log.e(LOG_PROV, LOG_NAME + "Error: " + PluginResult.Status.JSON_EXCEPTION);
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
}
}
});
return true;
}
use of android.content.SharedPreferences in project Shuttle by timusus.
the class ThemeUtils method getThemeType.
/**
* @param context the context
* @return the current {@link com.simplecity.amp_library.utils.ThemeUtils.ThemeType}
*/
public static int getThemeType(Context context) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
String theme = sharedPreferences.getString("pref_theme_base", "0");
if (Integer.valueOf(theme) == 0) {
return ThemeType.TYPE_SOLID_LIGHT;
} else if (Integer.valueOf(theme) == 1) {
return ThemeType.TYPE_SOLID_DARK;
} else if (Integer.valueOf(theme) == 2) {
return ThemeType.TYPE_SOLID_BLACK;
}
return ThemeType.TYPE_SOLID_LIGHT;
}
use of android.content.SharedPreferences in project Shuttle by timusus.
the class ShuttleUtilsPowerMockTest method testIsOnlineWifiConnected.
@Test
public void testIsOnlineWifiConnected() {
ShuttleApplication mockApplication = mock(ShuttleApplication.class);
SharedPreferences mockSharedPreferences = mock(SharedPreferences.class);
ConnectivityManager mockConnectivityManager = mock(ConnectivityManager.class);
NetworkInfo mockNetworkInfo = mock(NetworkInfo.class);
mockStatic(PreferenceManager.class);
mockStatic(ShuttleApplication.class);
when(PreferenceManager.getDefaultSharedPreferences(any(Context.class))).thenReturn(mockSharedPreferences);
when(ShuttleApplication.getInstance()).thenReturn(mockApplication);
// Mock the connection to Wi-Fi
when(mockApplication.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(mockConnectivityManager);
when(mockConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)).thenReturn(mockNetworkInfo);
when(mockNetworkInfo.isConnectedOrConnecting()).thenReturn(true);
// Test when we care about Wi-Fi (and it's connected), regardless of user preference
assertThat(ShuttleUtils.isOnline(true)).isTrue();
// Test when we don't care about Wi-Fi (but it's connected anyway), regardless of user preference
assertThat(ShuttleUtils.isOnline(false)).isTrue();
}
use of android.content.SharedPreferences in project Shuttle by timusus.
the class ShuttleUtilsPowerMockTest method testIsOnlineCellularConnected.
@Test
public void testIsOnlineCellularConnected() {
ShuttleApplication mockApplication = mock(ShuttleApplication.class);
SharedPreferences mockSharedPreferences = mock(SharedPreferences.class);
ConnectivityManager mockConnectivityManager = mock(ConnectivityManager.class);
NetworkInfo mockNetworkInfo = mock(NetworkInfo.class);
mockStatic(PreferenceManager.class);
mockStatic(ShuttleApplication.class);
when(PreferenceManager.getDefaultSharedPreferences(any(Context.class))).thenReturn(mockSharedPreferences);
when(ShuttleApplication.getInstance()).thenReturn(mockApplication);
// Mock the connection to cellular data, but not Wi-Fi
when(mockApplication.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(mockConnectivityManager);
when(mockConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)).thenReturn(null);
when(mockConnectivityManager.getActiveNetworkInfo()).thenReturn(mockNetworkInfo);
when(mockNetworkInfo.isConnectedOrConnecting()).thenReturn(true);
// Test when we care about Wi-Fi and so does the user (but only cellular is connected)
when(mockSharedPreferences.getBoolean(eq("pref_download_wifi_only"), anyBoolean())).thenReturn(true);
assertThat(ShuttleUtils.isOnline(true)).isFalse();
// Test when we care about Wi-Fi, but the user doesn't (and only cellular is connected)
when(mockSharedPreferences.getBoolean(eq("pref_download_wifi_only"), anyBoolean())).thenReturn(false);
assertThat(ShuttleUtils.isOnline(true)).isTrue();
// Test when we don't care about Wi-Fi, even if the user does (and only cellular is connected)
when(mockSharedPreferences.getBoolean(eq("pref_download_wifi_only"), anyBoolean())).thenReturn(true);
assertThat(ShuttleUtils.isOnline(false)).isTrue();
// Test when we don't care about Wi-Fi and neither does the user (and only cellular is connected)
when(mockSharedPreferences.getBoolean(eq("pref_download_wifi_only"), anyBoolean())).thenReturn(false);
assertThat(ShuttleUtils.isOnline(false)).isTrue();
}
use of android.content.SharedPreferences in project Shuttle by timusus.
the class DrawableUtils method getColoredAccentDrawable.
/**
* Takes a drawable and applies the current theme accent color to it
*
* @param baseDrawable the drawable to theme
* @return a themed {@link android.graphics.drawable.Drawable}
*/
public static Drawable getColoredAccentDrawable(Context context, Drawable baseDrawable, boolean canFallBackToWhite, boolean usePrimary) {
if (baseDrawable == null) {
return null;
}
baseDrawable = baseDrawable.getConstantState().newDrawable();
int accentColor = ColorUtils.getAccentColor();
if (accentColor == ColorUtils.getPrimaryColor() && canFallBackToWhite) {
accentColor = Color.WHITE;
}
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
if (!canFallBackToWhite && sharedPreferences.getBoolean("pref_theme_white_accent", false)) {
accentColor = ColorUtils.getAccentColor(false, true);
}
if (!canFallBackToWhite && usePrimary && sharedPreferences.getBoolean("pref_theme_white_accent", false)) {
accentColor = ColorUtils.getPrimaryColor();
}
ColorFilter highlightColorFilter = new LightingColorFilter(accentColor, 0);
baseDrawable.mutate().setColorFilter(highlightColorFilter);
return baseDrawable;
}
Aggregations