List of usage examples for android.content Context getClassLoader
public abstract ClassLoader getClassLoader();
From source file:im.ene.ribbon.BottomNavigationView.java
static BadgeProvider parseBadgeProvider(final BottomNavigationView navigation, final Context context, final String name) { log(TAG, INFO, "parseBadgeProvider: %s", name); if (TextUtils.isEmpty(name)) { return new BadgeProvider(navigation); }//from w w w . j a v a 2 s . co m final String fullName; if (name.startsWith(".")) { fullName = context.getPackageName() + name; } else if (name.indexOf('.') >= 0) { fullName = name; } else { // Assume stock behavior in this package (if we have one) fullName = !TextUtils.isEmpty(WIDGET_PACKAGE_NAME) ? (WIDGET_PACKAGE_NAME + '.' + name) : name; } try { Map<String, Constructor<BadgeProvider>> constructors = S_CONSTRUCTORS.get(); if (constructors == null) { constructors = new HashMap<>(); S_CONSTRUCTORS.set(constructors); } Constructor<BadgeProvider> c = constructors.get(fullName); if (c == null) { final Class<BadgeProvider> clazz = (Class<BadgeProvider>) Class.forName(fullName, true, context.getClassLoader()); c = clazz.getConstructor(CONSTRUCTOR_PARAMS); c.setAccessible(true); constructors.put(fullName, c); } return c.newInstance(navigation); } catch (Exception e) { throw new RuntimeException("Could not inflate Behavior subclass " + fullName, e); } }
From source file:com.tencent.tws.assistant.support.v4.app.FragmentState.java
/** * Determine if the given fragment name is a support library fragment class. * * @param context Context used to determine the correct ClassLoader to use * @param fname Class name of the fragment to test * @return true if <code>fname</code> is <code>android.support.v4.app.Fragment</code> * or a subclass, false otherwise. *//*from w ww . ja va2 s.co m*/ static boolean isSupportFragmentClass(Context context, String fname) { try { Class<?> clazz = sClassMap.get(fname); if (clazz == null) { // Class not found in the cache, see if it's real, and try to add it clazz = context.getClassLoader().loadClass(fname); sClassMap.put(fname, clazz); } return Fragment.class.isAssignableFrom(clazz); } catch (ClassNotFoundException e) { return false; } }
From source file:com.aware.Aware.java
/** * Given a package and class name, check if the class exists or not. * @param package_name// w w w. j a v a2 s.c o m * @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 {// ww w. j a va2 s . c o 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; }
From source file:com.aware.Aware.java
/** * Given a plugin's package name, fetch the context card for reuse. * @param context: application context//from w ww . j a v a 2 s . com * @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:android.app.FragmentState.java
/** * Create a new instance of a Fragment with the given class name. This is * the same as calling its empty constructor. * * @param context The calling context being used to instantiate the fragment. * This is currently just used to get its ClassLoader. * @param fname The class name of the fragment to instantiate. * @param args Bundle of arguments to supply to the fragment, which it * can retrieve with {@link #getArguments()}. May be null. * @return Returns a new fragment instance. * @throws InstantiationException If there is a failure in instantiating * the given fragment class. This is a runtime exception; it is not * normally expected to happen./*w w w.ja va2 s. c o m*/ */ public static Fragment instantiate(Context context, String fname, Bundle args) { try { Class<?> clazz = sClassMap.get(fname); if (clazz == null) { // Class not found in the cache, see if it's real, and try to add it clazz = context.getClassLoader().loadClass(fname); sClassMap.put(fname, clazz); } Fragment f = (Fragment) clazz.newInstance(); if (args != null) { args.setClassLoader(f.getClass().getClassLoader()); f.mArguments = args; } return f; } catch (ClassNotFoundException e) { throw new InstantiationException("Unable to instantiate fragment " + fname + ": make sure class name exists, is public, and has an" + " empty constructor that is public", e); } catch (java.lang.InstantiationException e) { throw new InstantiationException("Unable to instantiate fragment " + fname + ": make sure class name exists, is public, and has an" + " empty constructor that is public", e); } catch (IllegalAccessException e) { throw new InstantiationException("Unable to instantiate fragment " + fname + ": make sure class name exists, is public, and has an" + " empty constructor that is public", e); } }
From source file:com.owen.tvrecyclerview.widget.TvRecyclerView.java
private void loadLayoutManagerFromName(Context context, AttributeSet attrs, String name) { try {//from www . j av a 2 s . com final int dotIndex = name.indexOf('.'); if (dotIndex == -1) { name = "com.owen.tvrecyclerview.widget." + name; } else if (dotIndex == 0) { final String packageName = context.getPackageName(); name = packageName + "." + name; } Class<? extends TwoWayLayoutManager> clazz = context.getClassLoader().loadClass(name) .asSubclass(TwoWayLayoutManager.class); Constructor<? extends TwoWayLayoutManager> constructor = clazz.getConstructor(sConstructorSignature); sConstructorArgs[0] = context; sConstructorArgs[1] = attrs; setLayoutManager(constructor.newInstance(sConstructorArgs)); } catch (Exception e) { throw new IllegalStateException("Could not load TwoWayLayoutManager from " + "class: " + name, e); } }
From source file:android.app.FragmentState.java
/** * Create a new instance of a Fragment with the given class name. This is * the same as calling its empty constructor. * * @param context The calling context being used to instantiate the fragment. * This is currently just used to get its ClassLoader. * @param fname The class name of the fragment to instantiate. * @param args Bundle of arguments to supply to the fragment, which it * can retrieve with {@link #getArguments()}. May be null. * @return Returns a new fragment instance. * @throws InstantiationException If there is a failure in instantiating * the given fragment class. This is a runtime exception; it is not * normally expected to happen.//from w w w .ja v a 2 s . co m */ public static Fragment instantiate(Context context, String fname, @Nullable Bundle args) { try { Class<?> clazz = sClassMap.get(fname); if (clazz == null) { // Class not found in the cache, see if it's real, and try to add it clazz = context.getClassLoader().loadClass(fname); if (!Fragment.class.isAssignableFrom(clazz)) { throw new InstantiationException( "Trying to instantiate a class " + fname + " that is not a Fragment", new ClassCastException()); } sClassMap.put(fname, clazz); } Fragment f = (Fragment) clazz.newInstance(); if (args != null) { args.setClassLoader(f.getClass().getClassLoader()); f.mArguments = args; } return f; } catch (ClassNotFoundException e) { throw new InstantiationException("Unable to instantiate fragment " + fname + ": make sure class name exists, is public, and has an" + " empty constructor that is public", e); } catch (java.lang.InstantiationException e) { throw new InstantiationException("Unable to instantiate fragment " + fname + ": make sure class name exists, is public, and has an" + " empty constructor that is public", e); } catch (IllegalAccessException e) { throw new InstantiationException("Unable to instantiate fragment " + fname + ": make sure class name exists, is public, and has an" + " empty constructor that is public", e); } }
From source file:net.wequick.small.ApkBundleLauncher.java
@Override public void setUp(Context context) { super.setUp(context); Field f;//from www . j a v a 2 s .co m // AOP for pending intent try { f = TaskStackBuilder.class.getDeclaredField("IMPL"); f.setAccessible(true); final Object impl = f.get(TaskStackBuilder.class); InvocationHandler aop = new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { Intent[] intents = (Intent[]) args[1]; for (Intent intent : intents) { sBundleInstrumentation.wrapIntent(intent); intent.setAction(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_LAUNCHER); } return method.invoke(impl, args); } }; Object newImpl = Proxy.newProxyInstance(context.getClassLoader(), impl.getClass().getInterfaces(), aop); f.set(TaskStackBuilder.class, newImpl); } catch (Exception ignored) { ignored.printStackTrace(); } }
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); }