Example usage for android.content.pm PackageManager getResourcesForApplication

List of usage examples for android.content.pm PackageManager getResourcesForApplication

Introduction

In this page you can find the example usage for android.content.pm PackageManager getResourcesForApplication.

Prototype

public abstract Resources getResourcesForApplication(String appPackageName) throws NameNotFoundException;

Source Link

Document

Retrieve the resources associated with an application.

Usage

From source file:Main.java

static int getResourceId(Context context, String name, String type, String packageName) {

    Resources themeResources = null;
    PackageManager pm = context.getPackageManager();
    try {/*from  w ww  . ja  va2 s .c  o m*/
        themeResources = pm.getResourcesForApplication(packageName);
        return themeResources.getIdentifier(name, type, packageName);
    } catch (NameNotFoundException e) {
        e.printStackTrace();
    }
    return 0;
}

From source file:Main.java

/**
 * Returns the string corresponding to a resource name in a specific package.
 *
 * @param context The parent context.//from   ww w  .  j av a  2s . c om
 * @param packageName The application's package name, e.g. "com.android.settings".
 * @param resName The short name of the resource, e.g. "ok".
 * @return A package-specific string, or {@code null}.
 */
public static String getPackageString(Context context, String packageName, String resName) {
    final PackageManager pm = context.getPackageManager();

    final Resources packageRes;
    try {
        packageRes = pm.getResourcesForApplication(packageName);
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
        return null;
    }

    final int resId = packageRes.getIdentifier(resName, "string", packageName);
    if (resId <= 0) {
        return null;
    }

    final String result;
    try {
        result = packageRes.getString(resId);
    } catch (Resources.NotFoundException e) {
        e.printStackTrace();
        return null;
    }

    return result;
}

From source file:Main.java

public static Bitmap packageNameToBitmap(PackageManager packageManager, String packageName) {
    try {/* w w  w  .j  a  v a  2s.  c om*/
        ApplicationInfo applicationInfo = packageManager.getApplicationInfo(packageName,
                PackageManager.GET_META_DATA);
        Resources resources = packageManager.getResourcesForApplication(applicationInfo);
        int appIconResId = applicationInfo.icon;
        return resIdToBitmap(resources, appIconResId);
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static Bitmap packageNameToBitmap(Context context, PackageManager packageManager, String packageName,
        int resId) {
    try {/*  ww  w  .  j  av a2s  . co  m*/
        ApplicationInfo applicationInfo = packageManager.getApplicationInfo(packageName,
                PackageManager.GET_META_DATA);
        Resources resources = packageManager.getResourcesForApplication(applicationInfo);
        Bitmap bitmap = resIdToBitmap(resources, resId);
        if (bitmap == null) {
            Drawable drawable = packageManager.getApplicationIcon(packageName);
            if (drawable != null) {
                bitmap = drawableToBitmap(drawable);
            }
        }
        return bitmap;
    } catch (PackageManager.NameNotFoundException | Resources.NotFoundException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:com.squareup.picasso3.Utils.java

static Resources getResources(Context context, Request data) throws FileNotFoundException {
    if (data.resourceId != 0 || data.uri == null) {
        return context.getResources();
    }//w  w w  .ja va  2s.c  om

    String pkg = data.uri.getAuthority();
    if (pkg == null)
        throw new FileNotFoundException("No package provided: " + data.uri);
    try {
        PackageManager pm = context.getPackageManager();
        return pm.getResourcesForApplication(pkg);
    } catch (PackageManager.NameNotFoundException e) {
        throw new FileNotFoundException("Unable to obtain resources for package: " + data.uri);
    }
}

From source file:com.dm.material.dashboard.candybar.helpers.DrawableHelper.java

@Nullable
public static Bitmap getHighQualityIcon(@NonNull Context context, String packageName) {
    try {//from   w  ww  .  ja  va 2s  .  com
        PackageManager packageManager = context.getPackageManager();
        ApplicationInfo info = packageManager.getApplicationInfo(packageName, PackageManager.GET_META_DATA);

        Resources resources = packageManager.getResourcesForApplication(packageName);
        int density = DisplayMetrics.DENSITY_XXHIGH;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            density = DisplayMetrics.DENSITY_XXXHIGH;
        }

        Drawable drawable = ResourcesCompat.getDrawableForDensity(resources, info.icon, density, null);
        if (drawable != null)
            return ((BitmapDrawable) drawable).getBitmap();
    } catch (Exception | OutOfMemoryError e) {
        LogUtil.e(Log.getStackTraceString(e));
    }
    return null;
}

From source file:Main.java

static Pair<String, Resources> findSystemApk(String action, PackageManager pm) {
    final Intent intent = new Intent(action);
    for (ResolveInfo info : pm.queryBroadcastReceivers(intent, 0)) {
        if (info.activityInfo != null
                && (info.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
            final String packageName = info.activityInfo.packageName;
            try {
                final Resources res = pm.getResourcesForApplication(packageName);
                return Pair.create(packageName, res);
            } catch (NameNotFoundException e) {
                Log.w(TAG, "Failed to find resources for " + packageName);
            }/*w  w w .ja  v a2 s .  c  om*/
        }
    }
    return null;
}

From source file:Main.java

static List<Pair<String, Resources>> findSystemApks(String action, PackageManager pm) {
    final Intent intent = new Intent(action);
    List<Pair<String, Resources>> systemApks = new ArrayList<Pair<String, Resources>>();
    for (ResolveInfo info : pm.queryBroadcastReceivers(intent, 0)) {
        if (info.activityInfo != null
                && (info.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
            final String packageName = info.activityInfo.packageName;
            try {
                final Resources res = pm.getResourcesForApplication(packageName);
                systemApks.add(Pair.create(packageName, res));
            } catch (NameNotFoundException e) {
                Log.w(TAG, "Failed to find resources for " + packageName);
            }//ww  w.j  a  v a  2s .c o m
        }
    }
    return systemApks;
}

From source file:eu.faircode.netguard.ServiceJob.java

private static void submit(Rule rule, PersistableBundle bundle, Context context) {
    PackageManager pm = context.getPackageManager();
    JobScheduler scheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);

    // Get english application label
    String label = null;//from ww w.j  a  va 2 s . c  o  m
    try {
        Configuration config = new Configuration();
        config.setLocale(new Locale("en"));
        Resources res = pm.getResourcesForApplication(rule.info.packageName);
        res.updateConfiguration(config, res.getDisplayMetrics());
        label = res.getString(rule.info.applicationInfo.labelRes);
    } catch (Throwable ex) {
        Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
        CharSequence cs = rule.info.applicationInfo.loadLabel(pm);
        if (cs != null)
            label = cs.toString();
    }

    // Add application data
    bundle.putInt("uid", rule.info.applicationInfo.uid);
    bundle.putString("package", rule.info.packageName);
    bundle.putInt("version_code", rule.info.versionCode);
    bundle.putString("version_name", rule.info.versionName);
    bundle.putString("label", label);
    bundle.putInt("system", rule.system ? 1 : 0);
    try {
        bundle.putString("installer", pm.getInstallerPackageName(rule.info.packageName));
    } catch (Throwable ex) {
        Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
        bundle.putString("installer", null);
    }

    // Cancel overlapping jobs
    for (JobInfo pending : scheduler.getAllPendingJobs()) {
        String type = pending.getExtras().getString("type");
        if (type != null && type.equals(bundle.getString("type"))) {
            if (type.equals("rule")) {
                int uid = pending.getExtras().getInt("uid");
                if (uid == bundle.getInt("uid")) {
                    Log.i(TAG, "Canceling id=" + pending.getId());
                    scheduler.cancel(pending.getId());
                }
            } else if (type.equals("host")) {
                int uid = pending.getExtras().getInt("uid");
                int version = pending.getExtras().getInt("version");
                int protocol = pending.getExtras().getInt("protocol");
                String daddr = pending.getExtras().getString("daddr");
                int dport = pending.getExtras().getInt("dport");
                if (uid == bundle.getInt("uid") && version == bundle.getInt("version")
                        && protocol == bundle.getInt("protocol") && daddr != null
                        && daddr.equals(bundle.getString("daddr")) && dport == bundle.getInt("dport")) {
                    Log.i(TAG, "Canceling id=" + pending.getId());
                    scheduler.cancel(pending.getId());
                }
            }
        }
    }

    // Schedule job
    ComponentName serviceName = new ComponentName(context, ServiceJob.class);
    JobInfo job = new JobInfo.Builder(++id, serviceName).setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED)
            .setMinimumLatency(Util.isDebuggable(context) ? 10 * 1000 : 60 * 1000).setExtras(bundle)
            .setPersisted(true).build();
    if (scheduler.schedule(job) == JobScheduler.RESULT_SUCCESS)
        Log.i(TAG, "Scheduled job=" + job.getId() + " success");
    else
        Log.e(TAG, "Scheduled job=" + job.getId() + " failed");
}

From source file:uk.ac.bournemouth.darwin.auth.AuthTokenPermissionActivity.java

@Override
protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mBinding = DataBindingUtil.setContentView(this, R.layout.get_permission);
    mBinding.setAccount(getIntent().<Account>getParcelableExtra(DarwinAuthenticator.KEY_ACCOUNT));
    mCallerUid = getIntent().getIntExtra(AccountManager.KEY_CALLER_UID, -1);
    final PackageManager pm = getPackageManager();
    String callerPackage = pm.getPackagesForUid(mCallerUid)[0];
    String packageName;/*from w  ww. j a  v a  2  s  . c o m*/
    try {
        final PackageInfo packageInfo = pm.getPackageInfo(callerPackage, 0);
        int labelRes = packageInfo.applicationInfo.labelRes;
        packageName = pm.getResourcesForApplication(packageInfo.applicationInfo).getString(labelRes);
    } catch (NameNotFoundException e) {
        Log.w(TAG, "onCreate: ", e);
        packageName = callerPackage;
    }

    mBinding.setCallerName(packageName);
    mBinding.cancelbutton.setOnClickListener(this);
    mBinding.okbutton.setOnClickListener(this);
}