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
5package org.chromium.base;
6
7import android.content.Context;
8import android.content.pm.ApplicationInfo;
9import android.content.pm.PackageInfo;
10import android.content.pm.PackageManager;
11import android.content.pm.PackageManager.NameNotFoundException;
12import android.os.Build;
13import 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 */
20public 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 getDeviceManufacturer() {
59        return Build.MANUFACTURER;
60    }
61
62    @CalledByNative
63    public static String getDeviceModel() {
64        return Build.MODEL;
65    }
66
67    @CalledByNative
68    public static String getPackageVersionCode(Context context) {
69        String msg = "versionCode not available.";
70        try {
71            PackageManager pm = context.getPackageManager();
72            PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
73            msg = "";
74            if (pi.versionCode > 0) {
75                msg = Integer.toString(pi.versionCode);
76            }
77        } catch (NameNotFoundException e) {
78            Log.d(TAG, msg);
79        }
80        return msg;
81
82    }
83
84    @CalledByNative
85    public static String getPackageVersionName(Context context) {
86        String msg = "versionName not available";
87        try {
88            PackageManager pm = context.getPackageManager();
89            PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
90            msg = pi.versionName;
91        } catch (NameNotFoundException e) {
92            Log.d(TAG, msg);
93        }
94        return msg;
95    }
96
97    @CalledByNative
98    public static String getPackageLabel(Context context) {
99        try {
100            PackageManager packageManager = context.getPackageManager();
101            ApplicationInfo appInfo = packageManager.getApplicationInfo(context.getPackageName(),
102                    PackageManager.GET_META_DATA);
103            CharSequence label = packageManager.getApplicationLabel(appInfo);
104            return  label != null ? label.toString() : "";
105        } catch (NameNotFoundException e) {
106            return "";
107        }
108    }
109
110    @CalledByNative
111    public static String getPackageName(Context context) {
112        String packageName = context != null ? context.getPackageName() : null;
113        return packageName != null ? packageName : "";
114    }
115
116    @CalledByNative
117    public static String getBuildType() {
118        return Build.TYPE;
119    }
120
121    @CalledByNative
122    public static int getSdkInt() {
123        return Build.VERSION.SDK_INT;
124    }
125}
126