use of android.content.ContextWrapper in project robolectric by robolectric.
the class ShadowContextWrapperTest method broadcastReceivers_shouldBeSharedAcrossContextsPerApplicationContext.
@Test
public void broadcastReceivers_shouldBeSharedAcrossContextsPerApplicationContext() throws Exception {
BroadcastReceiver receiver = broadcastReceiver("Larry");
Application application = ApplicationProvider.getApplicationContext();
new ContextWrapper(application).registerReceiver(receiver, intentFilter("foo", "baz"));
new ContextWrapper(application).sendBroadcast(new Intent("foo"));
application.sendBroadcast(new Intent("baz"));
asyncAssertThat(transcript).containsExactly("Larry notified of foo", "Larry notified of baz");
new ContextWrapper(application).unregisterReceiver(receiver);
}
use of android.content.ContextWrapper in project kcanotify by antest1.
the class KcaUtils method clearFairyImageFileFromStorage.
public static boolean clearFairyImageFileFromStorage(Context context) {
ContextWrapper cw = new ContextWrapper(context);
File directory = cw.getDir("fairy", Context.MODE_PRIVATE);
for (File file : directory.listFiles()) {
if (!file.isDirectory())
file.delete();
}
return true;
}
use of android.content.ContextWrapper in project kcanotify by antest1.
the class KcaUtils method getJsonArrayFromStorage.
public static JsonArray getJsonArrayFromStorage(Context context, String name, KcaDBHelper helper) {
if (getBooleanPreferences(context, PREF_RES_USELOCAL)) {
return getJsonArrayFromAsset(context, name, helper);
} else {
ContextWrapper cw = new ContextWrapper(context);
File directory = cw.getDir("data", Context.MODE_PRIVATE);
File jsonFile = new File(directory, name);
JsonArray data = new JsonArray();
try {
Reader reader = new FileReader(jsonFile);
data = new JsonParser().parse(reader).getAsJsonArray();
reader.close();
} catch (IOException | IllegalStateException | JsonSyntaxException e) {
e.printStackTrace();
setPreferences(context, PREF_DATALOAD_ERROR_FLAG, true);
if (helper != null)
helper.recordErrorLog(ERROR_TYPE_DATALOAD, name, "getJsonArrayFromStorage", "0", getStringFromException(e));
data = getJsonArrayFromAsset(context, name, helper);
}
return data;
}
}
use of android.content.ContextWrapper in project kcanotify by antest1.
the class KcaUtils method checkFairyImageFileFromStorage.
public static boolean checkFairyImageFileFromStorage(Context context, String name) {
ContextWrapper cw = new ContextWrapper(context);
File directory = cw.getDir("fairy", Context.MODE_PRIVATE);
File myImageFile = new File(directory, KcaUtils.format("%s", name));
Bitmap bitmap = null;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
try {
InputStream is = new FileInputStream(myImageFile);
bitmap = BitmapFactory.decodeStream(is, null, options);
is.close();
} catch (IOException e) {
// Log.e("KCA", getStringFromException(e));
return false;
}
if (bitmap == null) {
return false;
}
return true;
}
use of android.content.ContextWrapper in project kcanotify by antest1.
the class KcaUtils method getJsonObjectFromStorage.
public static JsonObject getJsonObjectFromStorage(Context context, String name, KcaDBHelper helper) {
if (getBooleanPreferences(context, PREF_RES_USELOCAL)) {
return getJsonObjectFromAsset(context, name, helper);
} else {
ContextWrapper cw = new ContextWrapper(context);
File directory = cw.getDir("data", Context.MODE_PRIVATE);
File jsonFile = new File(directory, name);
JsonObject data = null;
try {
Reader reader = new FileReader(jsonFile);
data = new JsonParser().parse(reader).getAsJsonObject();
reader.close();
} catch (IOException | IllegalStateException | JsonSyntaxException e) {
e.printStackTrace();
setPreferences(context, PREF_DATALOAD_ERROR_FLAG, true);
if (helper != null)
helper.recordErrorLog(ERROR_TYPE_DATALOAD, name, "getJsonObjectFromStorage", "0", getStringFromException(e));
data = getJsonObjectFromAsset(context, name, helper);
}
return data;
}
}
Aggregations