diff options
author | dgn <dgn@chromium.org> | 2015-03-30 03:24:35 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-03-30 10:25:15 +0000 |
commit | 6a1d9fb79b048bb135f74cb4830d576053f75abd (patch) | |
tree | 5b7af9a4e545c08123439a0fa16a3d102cd53227 /base/android | |
parent | 8bf44ab1ba96236a263eafaa4850c977ad836858 (diff) | |
download | chromium_src-6a1d9fb79b048bb135f74cb4830d576053f75abd.zip chromium_src-6a1d9fb79b048bb135f74cb4830d576053f75abd.tar.gz chromium_src-6a1d9fb79b048bb135f74cb4830d576053f75abd.tar.bz2 |
Add utility function for checking package version
The change also refactors some pieces of code to use new utility
function
BUG=469115
Review URL: https://codereview.chromium.org/1040433002
Cr-Commit-Position: refs/heads/master@{#322760}
Diffstat (limited to 'base/android')
-rw-r--r-- | base/android/java/src/org/chromium/base/PackageUtils.java | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/base/android/java/src/org/chromium/base/PackageUtils.java b/base/android/java/src/org/chromium/base/PackageUtils.java new file mode 100644 index 0000000..ab554cd --- /dev/null +++ b/base/android/java/src/org/chromium/base/PackageUtils.java @@ -0,0 +1,37 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +package org.chromium.base; + +import android.content.Context; +import android.content.pm.PackageInfo; +import android.content.pm.PackageManager; + +/** + * This class provides package checking related methods. + */ +public class PackageUtils { + /** + * Retrieves the version of the given package installed on the device. + * + * @param context Any context. + * @param packageName Name of the package to find. + * @return The package's version code if found, -1 otherwise. + */ + public static int getPackageVersion(Context context, String packageName) { + int versionCode = -1; + PackageManager pm = context.getPackageManager(); + try { + PackageInfo packageInfo = pm.getPackageInfo(packageName, 0); + if (packageInfo != null) versionCode = packageInfo.versionCode; + } catch (PackageManager.NameNotFoundException e) { + // Do nothing, versionCode stays -1 + } + return versionCode; + } + + private PackageUtils() { + // Hide constructor + } +} |