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