Example usage for android.content Context createPackageContext

List of usage examples for android.content Context createPackageContext

Introduction

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

Prototype

public abstract Context createPackageContext(String packageName, @CreatePackageOptions int flags)
        throws PackageManager.NameNotFoundException;

Source Link

Document

Return a new Context object for the given application name.

Usage

From source file:Main.java

public static boolean isGXModSystemUI(Context context) {
    Context mContext = null;//www. j  a  v a 2 s  .  c  o  m
    int id = 0;
    try {
        mContext = context.createPackageContext("com.android.systemui", Context.CONTEXT_RESTRICTED);
        id = mContext.getResources().getIdentifier("custom_SystemUI0_3", "bool", mContext.getPackageName());
    } catch (NameNotFoundException e) {
        return false;
    }
    return id > 0;
}

From source file:com.achep.acdisplay.notifications.NotificationUtils.java

public static Context createContext(Context context, StatusBarNotification n) {
    try {// ww w .  j  a v  a 2s .c o m
        return context.createPackageContext(n.getPackageName(), Context.CONTEXT_RESTRICTED);
    } catch (PackageManager.NameNotFoundException e) {
        Log.w(TAG, "Failed to create notification\'s context");
        return null;
    }
}

From source file:Main.java

static String getString(Context context, int resourceID) {
    String stringResult = "Instagram";
    try {//from w w w  .j a  v a2s .co m
        Context packageContext = context.createPackageContext("com.ihelp101.instagram",
                Context.CONTEXT_IGNORE_SECURITY);
        stringResult = packageContext.getString(resourceID);
    } catch (Exception e) {

    }
    return stringResult;
}

From source file:Main.java

/**
 * Get a color value from a theme attribute.
 * @param context used for getting the color.
 * @param attribute theme attribute./*from w  ww  . j a v a  2 s .  c  o m*/
 * @param defaultColor default to use.
 * @return color value
 */
public static int getThemeColor(Context context, int attribute, int defaultColor) {
    int themeColor = 0;
    String packageName = context.getPackageName();
    try {
        Context packageContext = context.createPackageContext(packageName, 0);
        ApplicationInfo applicationInfo = context.getPackageManager().getApplicationInfo(packageName, 0);
        packageContext.setTheme(applicationInfo.theme);
        Resources.Theme theme = packageContext.getTheme();
        TypedArray ta = theme.obtainStyledAttributes(new int[] { attribute });
        themeColor = ta.getColor(0, defaultColor);
        ta.recycle();
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return themeColor;
}

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. 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 Drawable getIconFromPackageName(String packageName, Context context) {
    PackageManager pm = context.getPackageManager();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
        try {//from  www . ja v a2s .  c o m
            PackageInfo pi = pm.getPackageInfo(packageName, 0);
            Context otherAppCtx = context.createPackageContext(packageName, Context.CONTEXT_IGNORE_SECURITY);

            int displayMetrics[] = { DisplayMetrics.DENSITY_XXXHIGH, DisplayMetrics.DENSITY_XXHIGH,
                    DisplayMetrics.DENSITY_XHIGH, DisplayMetrics.DENSITY_HIGH, DisplayMetrics.DENSITY_MEDIUM,
                    DisplayMetrics.DENSITY_TV };

            for (int displayMetric : displayMetrics) {
                try {
                    Drawable d = otherAppCtx.getResources().getDrawableForDensity(pi.applicationInfo.icon,
                            displayMetric);
                    if (d != null) {

                        return d;
                    }
                } catch (Resources.NotFoundException e) {
                }
            }

        } catch (Exception e) {
            // Handle Error here
        }
    }

    ApplicationInfo appInfo = null;
    try {
        appInfo = pm.getApplicationInfo(packageName, PackageManager.GET_META_DATA);
    } catch (PackageManager.NameNotFoundException e) {
        return null;
    }

    return appInfo.loadIcon(pm);
}

From source file:com.google.android.gms.common.GooglePlayServicesUtilLight.java

public static Context getRemoteContext(Context context) {
    try {//  w  ww. ja  v  a  2s  .  c  om
        return context.createPackageContext("com.google.android.gms", 3);
    } catch (NameNotFoundException e) {
        return null;
    }
}

From source file:Main.java

static void askEveryone(final Context context, boolean optedOut, boolean shouldUpdate) {
    Boolean status = false;/*from   w w w . j av a  2  s.  c o m*/
    PackageManager pm = context.getPackageManager();
    if (pm != null) {
        for (PackageInfo info : pm.getInstalledPackages(0)) {
            if (!info.packageName.equals(context.getPackageName())) {
                try {
                    Context foreignContext = context.createPackageContext(info.packageName, 0);
                    if (shouldUpdate) {
                        if (isQuantified(foreignContext)) {
                            createOptOut(foreignContext, optedOut);
                        }
                    } else {
                        status = isOptedOut(foreignContext, false);
                        if (status) {
                            break;
                        }
                    }
                } catch (Exception ignored) {
                }
            }
        }
    }
    if (!shouldUpdate) {
        createOptOut(context, status);
    }
}

From source file:com.xlythe.engine.theme.Theme.java

public static Context getThemeContext(Context context) {
    try {//from  w  w w  .  j ava2  s  .  c o m
        return context.createPackageContext(getPackageName(),
                context.CONTEXT_INCLUDE_CODE + Context.CONTEXT_IGNORE_SECURITY);
    } catch (NameNotFoundException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:com.google.android.gms.common.GooglePlayServicesUtil.java

public static Context getRemoteContext(Context context) {
    try {/*from  w w w .  ja  v  a 2  s  . c o  m*/
        return context.createPackageContext(GOOGLE_PLAY_SERVICES_PACKAGE, 3);
    } catch (NameNotFoundException e) {
        return null;
    }
}