Example usage for android.content Context CONTEXT_INCLUDE_CODE

List of usage examples for android.content Context CONTEXT_INCLUDE_CODE

Introduction

In this page you can find the example usage for android.content Context CONTEXT_INCLUDE_CODE.

Prototype

int CONTEXT_INCLUDE_CODE

To view the source code for android.content Context CONTEXT_INCLUDE_CODE.

Click Source Link

Document

Flag for use with #createPackageContext : include the application code with the context.

Usage

From source file:Main.java

private static void loadApplicationResources(Context context, Map<String, String> iconPackResources,
        String packageName) {//from w  w w. j  a  va 2s  . c  o  m
    Field[] drawableItems = null;
    try {
        Context appContext = context.createPackageContext(packageName,
                Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY);
        drawableItems = Class.forName(packageName + ".R$drawable", true, appContext.getClassLoader())
                .getFields();
    } catch (Exception e) {
        return;
    }
    for (Field f : drawableItems) {
        String name = f.getName();

        String icon = name.toLowerCase();
        name = name.replaceAll("_", ".");

        iconPackResources.put(name, icon);

        int activityIndex = name.lastIndexOf(".");
        if (activityIndex <= 0 || activityIndex == name.length() - 1) {
            continue;
        }

        String iconPackage = name.substring(0, activityIndex);
        if (TextUtils.isEmpty(iconPackage)) {
            continue;
        }
        iconPackResources.put(iconPackage, icon);

        String iconActivity = name.substring(activityIndex + 1);
        if (TextUtils.isEmpty(iconActivity)) {
            continue;
        }
        iconPackResources.put(iconPackage + "." + iconActivity, icon);
    }
}

From source file:com.aware.ui.Plugins_Manager.java

/**
 * Given a package and class name, check if the class exists or not.
 * @param String package_name/*from w  w  w. ja v  a2 s .  c o m*/
 * @param String class_name
 * @return true if exists, false otherwise
 */
private boolean isClassAvailable(String package_name, String class_name) {
    try {
        Context package_context = createPackageContext(package_name,
                Context.CONTEXT_IGNORE_SECURITY | Context.CONTEXT_INCLUDE_CODE);
        package_context.getClassLoader().loadClass(package_name + "." + class_name);
    } catch (ClassNotFoundException e) {
        return false;
    } catch (NameNotFoundException e) {
        return false;
    }
    return true;
}

From source file:org.mozilla.gecko.GeckoApp.java

Class<?> getPluginClass(String packageName, String className)
        throws NameNotFoundException, ClassNotFoundException {
    Context pluginContext = mAppContext.createPackageContext(packageName,
            Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY);
    ClassLoader pluginCL = pluginContext.getClassLoader();
    return pluginCL.loadClass(className);
}

From source file:com.aware.Aware.java

/**
 * Given a plugin's package name, fetch the context card for reuse.
 * @param context: application context//  ww  w.ja  v  a 2 s  . c o  m
 * @param package_name: plugin's package name
 * @return View for reuse (instance of LinearLayout)
 */
public static View getContextCard(final Context context, final String package_name) {

    if (!isClassAvailable(context, package_name, "ContextCard")) {
        return null;
    }

    String ui_class = package_name + ".ContextCard";
    LinearLayout card = new LinearLayout(context);
    LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    card.setLayoutParams(params);
    card.setOrientation(LinearLayout.VERTICAL);

    try {
        Context packageContext = context.createPackageContext(package_name,
                Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY);

        Class<?> fragment_loader = packageContext.getClassLoader().loadClass(ui_class);
        Object fragment = fragment_loader.newInstance();
        Method[] allMethods = fragment_loader.getDeclaredMethods();
        Method m = null;
        for (Method mItem : allMethods) {
            String mName = mItem.getName();
            if (mName.contains("getContextCard")) {
                mItem.setAccessible(true);
                m = mItem;
                break;
            }
        }

        View ui = (View) m.invoke(fragment, packageContext);
        if (ui != null) {
            //Check if plugin has settings. If it does, tapping the card shows the settings
            if (isClassAvailable(context, package_name, "Settings")) {
                ui.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent open_settings = new Intent();
                        open_settings.setClassName(package_name, package_name + ".Settings");
                        open_settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        context.startActivity(open_settings);
                    }
                });
            }

            //Set card look-n-feel
            ui.setBackgroundColor(Color.WHITE);
            ui.setPadding(20, 20, 20, 20);
            card.addView(ui);

            LinearLayout shadow = new LinearLayout(context);
            LayoutParams params_shadow = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
            params_shadow.setMargins(0, 0, 0, 10);
            shadow.setBackgroundColor(context.getResources().getColor(R.color.card_shadow));
            shadow.setMinimumHeight(5);
            shadow.setLayoutParams(params_shadow);
            card.addView(shadow);

            return card;
        } else {
            return null;
        }
    } catch (NameNotFoundException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    } catch (NullPointerException e) {
        e.printStackTrace();
    } catch (InstantiationException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.aware.Aware.java

/**
* Given a package and class name, check if the class exists or not.
* @param package_name/*from  www.j a  v  a  2  s  .c om*/
* @param class_name
* @return true if exists, false otherwise
*/
private static boolean isClassAvailable(Context context, String package_name, String class_name) {
    try {
        Context package_context = context.createPackageContext(package_name,
                Context.CONTEXT_IGNORE_SECURITY | Context.CONTEXT_INCLUDE_CODE);
        package_context.getClassLoader().loadClass(package_name + "." + class_name);
    } catch (ClassNotFoundException e) {
        return false;
    } catch (NameNotFoundException e) {
        return false;
    }
    return true;
}

From source file:org.mozilla.gecko.GeckoAppShell.java

public static Class<?> loadPluginClass(String className, String libName) {
    Log.i(LOGTAG, "in loadPluginClass... attempting to access className, then libName.....");
    Log.i(LOGTAG, "className: " + className);
    Log.i(LOGTAG, "libName: " + libName);

    try {/*w  w  w . jav a  2  s.  co m*/
        String packageName = GeckoApp.mAppContext.getPluginPackage(libName);
        Log.i(LOGTAG, "load \"" + className + "\" from \"" + packageName + "\" for \"" + libName + "\"");
        Context pluginContext = GeckoApp.mAppContext.createPackageContext(packageName,
                Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY);
        ClassLoader pluginCL = pluginContext.getClassLoader();
        return pluginCL.loadClass(className);
    } catch (ClassNotFoundException cnfe) {
        Log.i(LOGTAG, "class not found", cnfe);
    } catch (PackageManager.NameNotFoundException nnfe) {
        Log.i(LOGTAG, "package not found", nnfe);
    }
    Log.e(LOGTAG, "couldn't find class");
    return null;
}