Search in sources :

Example 86 with ContextWrapper

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);
}
Also used : Intent(android.content.Intent) BroadcastReceiver(android.content.BroadcastReceiver) Application(android.app.Application) ContextWrapper(android.content.ContextWrapper) Test(org.junit.Test)

Example 87 with ContextWrapper

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;
}
Also used : ContextWrapper(android.content.ContextWrapper) File(java.io.File)

Example 88 with ContextWrapper

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;
    }
}
Also used : JsonArray(com.google.gson.JsonArray) JsonSyntaxException(com.google.gson.JsonSyntaxException) Reader(java.io.Reader) FileReader(java.io.FileReader) FileReader(java.io.FileReader) IOException(java.io.IOException) ContextWrapper(android.content.ContextWrapper) File(java.io.File) JsonParser(com.google.gson.JsonParser)

Example 89 with ContextWrapper

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;
}
Also used : Bitmap(android.graphics.Bitmap) GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) ChunkedInputStream(org.apache.commons.httpclient.ChunkedInputStream) InputStream(java.io.InputStream) BitmapFactory(android.graphics.BitmapFactory) IOException(java.io.IOException) ContextWrapper(android.content.ContextWrapper) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 90 with ContextWrapper

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;
    }
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) JsonObject(com.google.gson.JsonObject) Reader(java.io.Reader) FileReader(java.io.FileReader) FileReader(java.io.FileReader) IOException(java.io.IOException) ContextWrapper(android.content.ContextWrapper) File(java.io.File) JsonParser(com.google.gson.JsonParser)

Aggregations

ContextWrapper (android.content.ContextWrapper)109 Context (android.content.Context)37 Activity (android.app.Activity)25 File (java.io.File)16 Test (org.junit.Test)13 Intent (android.content.Intent)11 MockContentResolver (android.test.mock.MockContentResolver)11 PackageManager (android.content.pm.PackageManager)10 View (android.view.View)10 Resources (android.content.res.Resources)9 IOException (java.io.IOException)8 Before (org.junit.Before)8 ContentResolver (android.content.ContentResolver)6 UserManager (android.os.UserManager)6 LayoutInflater (android.view.LayoutInflater)6 ThemePreferenceController (com.android.settings.display.ThemePreferenceController)6 OverlayManager (com.android.settings.display.ThemePreferenceController.OverlayManager)6 ArrayList (java.util.ArrayList)6 AppWidgetHostView (android.appwidget.AppWidgetHostView)5 JsonParser (com.google.gson.JsonParser)5