Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / base / android / java / src / org / chromium / base / BuildInfo.java
1 // Copyright 2012 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.ApplicationInfo;
9 import android.content.pm.PackageInfo;
10 import android.content.pm.PackageManager;
11 import android.content.pm.PackageManager.NameNotFoundException;
12 import android.os.Build;
13 import android.util.Log;
14
15 /**
16  * BuildInfo is a utility class providing easy access to {@link PackageInfo}
17  * information. This is primarly of use for accessesing package information
18  * from native code.
19  */
20 public class BuildInfo {
21     private static final String TAG = "BuildInfo";
22     private static final int MAX_FINGERPRINT_LENGTH = 128;
23
24     /**
25      * BuildInfo is a static utility class and therefore shouldn't be
26      * instantiated.
27      */
28     private BuildInfo() {
29     }
30
31     @CalledByNative
32     public static String getDevice() {
33         return Build.DEVICE;
34     }
35
36     @CalledByNative
37     public static String getBrand() {
38         return Build.BRAND;
39     }
40
41     @CalledByNative
42     public static String getAndroidBuildId() {
43         return Build.ID;
44     }
45
46     /**
47      * @return The build fingerprint for the current Android install.  The value is truncated to a
48      *         128 characters as this is used for crash and UMA reporting, which should avoid huge
49      *         strings.
50      */
51     @CalledByNative
52     public static String getAndroidBuildFingerprint() {
53         return Build.FINGERPRINT.substring(
54                 0, Math.min(Build.FINGERPRINT.length(), MAX_FINGERPRINT_LENGTH));
55     }
56
57     @CalledByNative
58     public static String getDeviceModel() {
59         return Build.MODEL;
60     }
61
62     @CalledByNative
63     public static String getPackageVersionCode(Context context) {
64         String msg = "versionCode not available.";
65         try {
66             PackageManager pm = context.getPackageManager();
67             PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
68             msg = "";
69             if (pi.versionCode > 0) {
70                 msg = Integer.toString(pi.versionCode);
71             }
72         } catch (NameNotFoundException e) {
73             Log.d(TAG, msg);
74         }
75         return msg;
76
77     }
78
79     @CalledByNative
80     public static String getPackageVersionName(Context context) {
81         String msg = "versionName not available";
82         try {
83             PackageManager pm = context.getPackageManager();
84             PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
85             msg = pi.versionName;
86         } catch (NameNotFoundException e) {
87             Log.d(TAG, msg);
88         }
89         return msg;
90     }
91
92     @CalledByNative
93     public static String getPackageLabel(Context context) {
94         try {
95             PackageManager packageManager = context.getPackageManager();
96             ApplicationInfo appInfo = packageManager.getApplicationInfo(context.getPackageName(),
97                     PackageManager.GET_META_DATA);
98             CharSequence label = packageManager.getApplicationLabel(appInfo);
99             return  label != null ? label.toString() : "";
100         } catch (NameNotFoundException e) {
101             return "";
102         }
103     }
104
105     @CalledByNative
106     public static String getPackageName(Context context) {
107         String packageName = context != null ? context.getPackageName() : null;
108         return packageName != null ? packageName : "";
109     }
110
111     @CalledByNative
112     public static String getBuildType() {
113         return Build.TYPE;
114     }
115
116     @CalledByNative
117     public static int getSdkInt() {
118         return Build.VERSION.SDK_INT;
119     }
120 }