• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 package org.chromium.base;
6 
7 import android.content.Context;
8 import android.content.pm.PackageInfo;
9 import android.content.pm.PackageManager;
10 import android.content.res.Resources;
11 import android.graphics.Bitmap;
12 import android.graphics.BitmapFactory;
13 
14 /**
15  * This class provides package checking related methods.
16  */
17 public class PackageUtils {
18     /**
19      * Retrieves the version of the given package installed on the device.
20      *
21      * @param context Any context.
22      * @param packageName Name of the package to find.
23      * @return The package's version code if found, -1 otherwise.
24      */
getPackageVersion(Context context, String packageName)25     public static int getPackageVersion(Context context, String packageName) {
26         int versionCode = -1;
27         PackageManager pm = context.getPackageManager();
28         try {
29             PackageInfo packageInfo = pm.getPackageInfo(packageName, 0);
30             if (packageInfo != null) versionCode = packageInfo.versionCode;
31         } catch (PackageManager.NameNotFoundException e) {
32             // Do nothing, versionCode stays -1
33         }
34         return versionCode;
35     }
36 
37     /**
38      * Decodes into a Bitmap an Image resource stored in another package.
39      * @param otherPackage The package containing the resource.
40      * @param resourceId The id of the resource.
41      * @return A Bitmap containing the resource or null if the package could not be found.
42      */
decodeImageResource(String otherPackage, int resourceId)43     public static Bitmap decodeImageResource(String otherPackage, int resourceId) {
44         PackageManager packageManager = ContextUtils.getApplicationContext().getPackageManager();
45         try {
46             Resources resources = packageManager.getResourcesForApplication(otherPackage);
47             return BitmapFactory.decodeResource(resources, resourceId);
48         } catch (PackageManager.NameNotFoundException e) {
49             return null;
50         }
51     }
52 
PackageUtils()53     private PackageUtils() {
54         // Hide constructor
55     }
56 }
57