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 11 /** 12 * This class provides package checking related methods. 13 */ 14 public class PackageUtils { 15 /** 16 * Retrieves the version of the given package installed on the device. 17 * 18 * @param context Any context. 19 * @param packageName Name of the package to find. 20 * @return The package's version code if found, -1 otherwise. 21 */ 22 public static int getPackageVersion(Context context, String packageName) { 23 int versionCode = -1; 24 PackageManager pm = context.getPackageManager(); 25 try { 26 PackageInfo packageInfo = pm.getPackageInfo(packageName, 0); 27 if (packageInfo != null) versionCode = packageInfo.versionCode; 28 } catch (PackageManager.NameNotFoundException e) { 29 // Do nothing, versionCode stays -1 30 } 31 return versionCode; 32 } 33 34 private PackageUtils() { 35 // Hide constructor 36 } 37 } 38