Example usage for android.content Context getClassLoader

List of usage examples for android.content Context getClassLoader

Introduction

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

Prototype

public abstract ClassLoader getClassLoader();

Source Link

Document

Return a class loader you can use to retrieve classes in this package.

Usage

From source file:Main.java

static <T> T instantiateViewDelegate(Context context, String className) {
    try {//from  w  w  w  .ja v a 2s  .c o m
        Class<?> clazz = context.getClassLoader().loadClass(className);
        return newInstance(context, clazz, VIEW_DELEGATE_CONSTRUCTOR_SIGNATURE);
    } catch (Exception e) {
        Log.w(LOG_TAG, "Cannot instantiate class: " + className, e);
    }
    return null;
}

From source file:Main.java

static <T> T instantiateTransformer(Context context, String className) {
    try {//from  w  ww .j av  a2s  .c o m
        Class<?> clazz = context.getClassLoader().loadClass(className);
        return newInstance(context, clazz, TRANSFORMER_CONSTRUCTOR_SIGNATURE);
    } catch (Exception e) {
        Log.w(LOG_TAG, "Cannot instantiate class: " + className, e);
    }
    return null;
}

From source file:Main.java

public static String findJniLibrary(Context context, String libName) {
    String result = null;/*w w  w.  java  2  s.co m*/
    ClassLoader classLoader = (context.getClassLoader());
    if (classLoader != null) {
        try {
            Method findLibraryMethod = classLoader.getClass().getMethod("findLibrary",
                    new Class<?>[] { String.class });
            if (findLibraryMethod != null) {
                Object objPath = findLibraryMethod.invoke(classLoader, new Object[] { libName });
                if (objPath != null && objPath instanceof String) {
                    result = (String) objPath;
                }
            }
        } catch (Exception e) {
            Log.e(TAG, e.toString());
        }
    }

    return result;
}

From source file:Main.java

public static String getSystemProperties(Context context, String str) throws IllegalArgumentException {
    try {/*from ww  w. j  av a  2  s .co  m*/
        Class loadClass = context.getClassLoader().loadClass("android.os.SystemProperties");
        return (String) loadClass.getMethod("get", String.class).invoke(loadClass, str);
    } catch (IllegalArgumentException e) {
        throw e;
    } catch (Exception e2) {
        return "";
    }
}

From source file:Main.java

public static Object loadClass(Context context, String path) {
    try {//from   w  w  w .  j  ava  2 s  .  c o  m
        String dexPath = context.getApplicationInfo().sourceDir;
        PathClassLoader pathClassLoader = new PathClassLoader(dexPath, context.getClassLoader());
        Class<?> c = Class.forName(path, true, pathClassLoader);
        Object ret = c.newInstance();
        return ret;
    } catch (InstantiationException ex1) {
        ex1.printStackTrace();
    } catch (IllegalAccessException ex2) {
        ex2.printStackTrace();
    } catch (ClassNotFoundException ex3) {
        ex3.printStackTrace();
    }

    return null;
}

From source file:Main.java

private static void loadApplicationResources(Context context, Map<String, String> iconPackResources,
        String packageName) {// w  ww .  ja  v  a 2 s .  co 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:Main.java

public static DexClassLoader createDexClassLoader(Context context, String dexPath, String optimizedDir,
        String nativeLibDir, boolean isDependent) {
    ClassLoader parentClassLoader;
    if (isDependent) {
        parentClassLoader = ClassLoader.getSystemClassLoader().getParent();
    } else {/*  ww w . j  av  a 2 s  .  com*/
        parentClassLoader = context.getClassLoader();
    }
    return new DexClassLoader(dexPath, optimizedDir, nativeLibDir, parentClassLoader);
}

From source file:com.google.android.apps.authenticator.testability.HttpClientFactory.java

private static HttpClient createHttpClientForFroyoAndHigher(Context context) throws ClassNotFoundException,
        NoSuchMethodException, IllegalAccessException, InvocationTargetException {
    // IMPLEMENTATION NOTE: We create the instance via Reflection API to avoid raising the
    // target SDK version to 8 because that will cause Eclipse to complain for no good reason.
    // The code below invokes:
    //   AndroidHttpClient.newInstance(null, getContext())
    Class<?> androidHttpClientClass = context.getClassLoader().loadClass("android.net.http.AndroidHttpClient");
    Method newInstanceMethod = androidHttpClientClass.getMethod("newInstance", String.class, Context.class);
    return (HttpClient) newInstanceMethod.invoke(null, null, context);
}

From source file:im.ene.lab.design.widget.swipecards.TinderView.java

private static Style parseStyle(Context context, AttributeSet attrs, String name) {
    if (TextUtils.isEmpty(name)) {
        return null;
    } else {//  w w  w  .j a v a 2  s  .  c o m
        String fullName;
        if (name.startsWith(".")) {
            fullName = context.getPackageName() + name;
        } else if (name.indexOf(46) >= 0) {
            fullName = name;
        } else {
            fullName = WIDGET_PACKAGE_NAME + '.' + name;
        }

        try {
            Map e = (Map) sConstructors.get();
            if (e == null) {
                e = new HashMap();
                sConstructors.set(e);
            }

            Constructor c = (Constructor) e.get(fullName);
            if (c == null) {
                Class clazz = Class.forName(fullName, true, context.getClassLoader());
                c = clazz.getConstructor(CONSTRUCTOR_PARAMS);
                c.setAccessible(true);
                e.put(fullName, c);
            }

            return (Style) c.newInstance(context, attrs);
        } catch (Exception var7) {
            throw new RuntimeException("Could not inflate Style subclass " + fullName, var7);
        }
    }
}

From source file:org.zywx.wbpalmstar.engine.EUtil.java

public static DexClassLoader loadDex(Context ctx, String dexAssertPath) {
    int index = dexAssertPath.lastIndexOf('/');
    if (index < 0) {
        return null;
    }/*www .j  ava2  s.c  om*/
    String dexName = dexAssertPath.substring(index);
    String dexPath = ctx.getDir("dex", Context.MODE_PRIVATE).getAbsolutePath() + dexName;
    File f = new File(dexPath);
    if (!f.exists()) {
        boolean ok = copyDex(ctx, dexAssertPath, dexPath);
        if (!ok) {
            return null;
        }
    }
    String dexOutputDir = ctx.getDir("outdex", Context.MODE_PRIVATE).getAbsolutePath();
    DexClassLoader cl = new DexClassLoader(dexPath, dexOutputDir, null, ctx.getClassLoader());
    return cl;
}