OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.base; | 5 package org.chromium.base; |
6 | 6 |
7 import android.content.Context; | 7 import android.content.Context; |
8 import android.content.pm.PackageInfo; | 8 import android.content.pm.PackageInfo; |
9 import android.content.pm.PackageManager; | 9 import android.content.pm.PackageManager; |
10 import android.content.pm.PackageManager.NameNotFoundException; | |
11 | 10 |
12 /** | 11 /** |
13 * This class provides package checking related methods. | 12 * This class provides package checking related methods. |
14 */ | 13 */ |
15 public class PackageUtils { | 14 public class PackageUtils { |
16 /** | 15 /** |
17 * Retrieves the PackageInfo object for this application. | |
18 * | |
19 * @param context Any context. | |
20 * @return The PackageInfo object for this application. | |
21 */ | |
22 public static PackageInfo getOwnPackageInfo(Context context) { | |
23 PackageManager manager = context.getPackageManager(); | |
24 try { | |
25 String packageName = context.getApplicationContext().getPackageName(
); | |
26 return manager.getPackageInfo(packageName, 0); | |
27 } catch (NameNotFoundException e) { | |
28 // Should never happen. | |
29 throw new AssertionError("Failed to retrieve own package info"); | |
30 } | |
31 } | |
32 | |
33 /** | |
34 * Retrieves the version of the given package installed on the device. | 16 * Retrieves the version of the given package installed on the device. |
35 * | 17 * |
36 * @param context Any context. | 18 * @param context Any context. |
37 * @param packageName Name of the package to find. | 19 * @param packageName Name of the package to find. |
38 * @return The package's version code if found, -1 otherwise. | 20 * @return The package's version code if found, -1 otherwise. |
39 */ | 21 */ |
40 public static int getPackageVersion(Context context, String packageName) { | 22 public static int getPackageVersion(Context context, String packageName) { |
41 int versionCode = -1; | 23 int versionCode = -1; |
42 PackageManager pm = context.getPackageManager(); | 24 PackageManager pm = context.getPackageManager(); |
43 try { | 25 try { |
44 PackageInfo packageInfo = pm.getPackageInfo(packageName, 0); | 26 PackageInfo packageInfo = pm.getPackageInfo(packageName, 0); |
45 if (packageInfo != null) versionCode = packageInfo.versionCode; | 27 if (packageInfo != null) versionCode = packageInfo.versionCode; |
46 } catch (PackageManager.NameNotFoundException e) { | 28 } catch (PackageManager.NameNotFoundException e) { |
47 // Do nothing, versionCode stays -1 | 29 // Do nothing, versionCode stays -1 |
48 } | 30 } |
49 return versionCode; | 31 return versionCode; |
50 } | 32 } |
51 | 33 |
52 private PackageUtils() { | 34 private PackageUtils() { |
53 // Hide constructor | 35 // Hide constructor |
54 } | 36 } |
55 } | 37 } |
OLD | NEW |