Search in sources :

Example 31 with ContextWrapper

use of android.content.ContextWrapper in project kcanotify by antest1.

the class KcaUtils method getJsonArrayFromAsset.

public static JsonArray getJsonArrayFromAsset(Context context, String name, KcaDBHelper helper) {
    ContextWrapper cw = new ContextWrapper(context);
    JsonArray data = new JsonArray();
    AssetManager am = cw.getAssets();
    try {
        AssetManager.AssetInputStream ais = (AssetManager.AssetInputStream) am.open(name);
        byte[] bytes = ByteStreams.toByteArray(ais);
        data = new JsonParser().parse(new String(bytes)).getAsJsonArray();
        ais.close();
    } catch (IOException e1) {
        e1.printStackTrace();
        if (helper != null)
            helper.recordErrorLog(ERROR_TYPE_DATALOAD, name, "getJsonArrayFromStorage", "1", getStringFromException(e1));
    }
    return data;
}
Also used : JsonArray(com.google.gson.JsonArray) AssetManager(android.content.res.AssetManager) IOException(java.io.IOException) ContextWrapper(android.content.ContextWrapper) JsonParser(com.google.gson.JsonParser)

Example 32 with ContextWrapper

use of android.content.ContextWrapper in project kcanotify by antest1.

the class KcaUtils method checkFairyImageInStorage.

public static boolean checkFairyImageInStorage(Context context, String name) {
    ContextWrapper cw = new ContextWrapper(context);
    File directory = cw.getDir("fairy", Context.MODE_PRIVATE);
    File image = new File(directory, name);
    return image.exists();
}
Also used : ContextWrapper(android.content.ContextWrapper) File(java.io.File)

Example 33 with ContextWrapper

use of android.content.ContextWrapper in project kcanotify by antest1.

the class KcaUtils method validateResourceFiles.

public static boolean validateResourceFiles(Context context, KcaDBHelper helper) {
    int count = 0;
    ContextWrapper cw = new ContextWrapper(context);
    File directory = cw.getDir("data", Context.MODE_PRIVATE);
    for (final File entry : directory.listFiles()) {
        try {
            Reader reader = new FileReader(entry);
            new JsonParser().parse(reader);
            count += 1;
        } catch (FileNotFoundException | IllegalStateException | JsonSyntaxException e) {
            e.printStackTrace();
            if (helper != null)
                helper.recordErrorLog(ERROR_TYPE_DATALOAD, entry.getName(), "validateResourceFiles", "2", getStringFromException(e));
            setPreferences(context, PREF_DATALOAD_ERROR_FLAG, true);
            return false;
        }
    }
    return count > 0;
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) FileNotFoundException(java.io.FileNotFoundException) Reader(java.io.Reader) FileReader(java.io.FileReader) FileReader(java.io.FileReader) ContextWrapper(android.content.ContextWrapper) File(java.io.File) JsonParser(com.google.gson.JsonParser)

Example 34 with ContextWrapper

use of android.content.ContextWrapper in project kcanotify by antest1.

the class KcaUtils method getJsonObjectFromAsset.

public static JsonObject getJsonObjectFromAsset(Context context, String name, KcaDBHelper helper) {
    ContextWrapper cw = new ContextWrapper(context);
    JsonObject data = null;
    AssetManager am = cw.getAssets();
    try {
        AssetManager.AssetInputStream ais = (AssetManager.AssetInputStream) am.open(name);
        byte[] bytes = ByteStreams.toByteArray(ais);
        data = new JsonParser().parse(new String(bytes)).getAsJsonObject();
        ais.close();
    } catch (IOException e1) {
        e1.printStackTrace();
        if (helper != null)
            helper.recordErrorLog(ERROR_TYPE_DATALOAD, name, "getJsonObjectFromStorage", "1", getStringFromException(e1));
    }
    return data;
}
Also used : AssetManager(android.content.res.AssetManager) JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException) ContextWrapper(android.content.ContextWrapper) JsonParser(com.google.gson.JsonParser)

Example 35 with ContextWrapper

use of android.content.ContextWrapper in project Android-skin-support by ximsfei.

the class SkinCompatViewInflater method checkOnClickListener.

/**
 * android:onClick doesn't handle views with a ContextWrapper context. This method
 * backports new framework functionality to traverse the Context wrappers to find a
 * suitable target.
 */
private void checkOnClickListener(View view, AttributeSet attrs) {
    final Context context = view.getContext();
    if (!(context instanceof ContextWrapper) || (Build.VERSION.SDK_INT >= 15 && !ViewCompat.hasOnClickListeners(view))) {
        // always use our compat code on older devices)
        return;
    }
    final TypedArray a = context.obtainStyledAttributes(attrs, sOnClickAttrs);
    final String handlerName = a.getString(0);
    if (handlerName != null) {
        view.setOnClickListener(new DeclaredOnClickListener(view, handlerName));
    }
    a.recycle();
}
Also used : Context(android.content.Context) TypedArray(android.content.res.TypedArray) ContextWrapper(android.content.ContextWrapper)

Aggregations

ContextWrapper (android.content.ContextWrapper)108 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 Resources (android.content.res.Resources)9 View (android.view.View)9 IOException (java.io.IOException)8 Before (org.junit.Before)8 ContentResolver (android.content.ContentResolver)6 UserManager (android.os.UserManager)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 LayoutInflater (android.view.LayoutInflater)5 JsonParser (com.google.gson.JsonParser)5