Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1382)

Unified Diff: base/android/java/src/org/chromium/base/BuildInfo.java

Issue 2825233002: Android: Refactor BuildInfo to use less jni and remove StrictMode exception (Closed)
Patch Set: rebase Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« base/android/build_info.h ('K') | « base/android/build_info.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/android/java/src/org/chromium/base/BuildInfo.java
diff --git a/base/android/java/src/org/chromium/base/BuildInfo.java b/base/android/java/src/org/chromium/base/BuildInfo.java
index 79a128b7cd2fcc9b55df5f295cbaeaf1f5abfa57..63c37be4f7deeebb1fa27164d5e355502f6ab8fd 100644
--- a/base/android/java/src/org/chromium/base/BuildInfo.java
+++ b/base/android/java/src/org/chromium/base/BuildInfo.java
@@ -5,12 +5,10 @@
package org.chromium.base;
import android.content.Context;
-import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Build;
-import android.os.StrictMode;
import org.chromium.base.annotations.CalledByNative;
@@ -28,18 +26,32 @@ public class BuildInfo {
private BuildInfo() {}
@CalledByNative
- public static String getDevice() {
- return Build.DEVICE;
- }
+ private static String[] getAll() {
+ try {
+ String packageName = ContextUtils.getApplicationContext().getPackageName();
+ PackageManager pm = ContextUtils.getApplicationContext().getPackageManager();
+ PackageInfo pi = pm.getPackageInfo(packageName, 0);
+ String versionCode = pi.versionCode <= 0 ? "" : Integer.toString(pi.versionCode);
+ String versionName = pi.versionName == null ? "" : pi.versionName;
- @CalledByNative
- public static String getBrand() {
- return Build.BRAND;
- }
+ CharSequence label = pm.getApplicationLabel(pi.applicationInfo);
+ String packageLabel = label == null ? "" : label.toString();
- @CalledByNative
- public static String getAndroidBuildId() {
- return Build.ID;
+ // Use lastUpdateTime when developing locally, since versionCode does not normally
+ // change in this case.
+ long version = pi.versionCode > 10 ? pi.versionCode : pi.lastUpdateTime;
+ String extractedFileSuffix = String.format("@%s", Long.toHexString(version));
+
+ // Do not alter this list without updating callers of it.
+ return new String[] {
+ Build.BRAND, Build.DEVICE, Build.ID, Build.MANUFACTURER, Build.MODEL,
+ String.valueOf(Build.VERSION.SDK_INT), Build.TYPE, packageLabel, packageName,
+ versionCode, versionName, getAndroidBuildFingerprint(), getGMSVersionCode(pm),
+ extractedFileSuffix,
+ };
+ } catch (NameNotFoundException e) {
+ throw new RuntimeException(e);
+ }
}
/**
@@ -47,28 +59,14 @@ public class BuildInfo {
* 128 characters as this is used for crash and UMA reporting, which should avoid huge
* strings.
*/
- @CalledByNative
- public static String getAndroidBuildFingerprint() {
+ private static String getAndroidBuildFingerprint() {
return Build.FINGERPRINT.substring(
0, Math.min(Build.FINGERPRINT.length(), MAX_FINGERPRINT_LENGTH));
}
- @CalledByNative
- public static String getDeviceManufacturer() {
- return Build.MANUFACTURER;
- }
-
- @CalledByNative
- public static String getDeviceModel() {
- return Build.MODEL;
- }
-
- @CalledByNative
- public static String getGMSVersionCode() {
+ private static String getGMSVersionCode(PackageManager packageManager) {
String msg = "gms versionCode not available.";
try {
- PackageManager packageManager =
- ContextUtils.getApplicationContext().getPackageManager();
PackageInfo packageInfo = packageManager.getPackageInfo("com.google.android.gms", 0);
msg = Integer.toString(packageInfo.versionCode);
} catch (NameNotFoundException e) {
@@ -77,85 +75,23 @@ public class BuildInfo {
return msg;
}
- @CalledByNative
- public static String getPackageVersionCode() {
- String msg = "versionCode not available.";
- try {
- PackageManager pm = ContextUtils.getApplicationContext().getPackageManager();
- PackageInfo pi = pm.getPackageInfo(getPackageName(), 0);
- msg = "";
- if (pi.versionCode > 0) {
- msg = Integer.toString(pi.versionCode);
- }
- } catch (NameNotFoundException e) {
- Log.d(TAG, msg);
- }
- return msg;
- }
-
- @CalledByNative
public static String getPackageVersionName() {
- String msg = "versionName not available";
- try {
- PackageManager pm = ContextUtils.getApplicationContext().getPackageManager();
- PackageInfo pi = pm.getPackageInfo(getPackageName(), 0);
- msg = "";
- if (pi.versionName != null) {
- msg = pi.versionName;
- }
- } catch (NameNotFoundException e) {
- Log.d(TAG, msg);
- }
- return msg;
+ return getAll()[10];
Torne 2017/04/21 19:41:25 Can we just retrieve all the values once and save
agrieve 2017/04/21 20:00:33 We could cache them, but we'd still need to call g
}
/** Returns a string that is different each time the apk changes. */
- @CalledByNative
public static String getExtractedFileSuffix() {
- PackageManager pm = ContextUtils.getApplicationContext().getPackageManager();
- try {
- PackageInfo pi =
- pm.getPackageInfo(ContextUtils.getApplicationContext().getPackageName(), 0);
- // Use lastUpdateTime when developing locally, since versionCode does not normally
- // change in this case.
- long version = pi.versionCode > 10 ? pi.versionCode : pi.lastUpdateTime;
- return "@" + Long.toHexString(version);
- } catch (PackageManager.NameNotFoundException e) {
- throw new RuntimeException(e);
- }
+ return getAll()[13];
}
- @CalledByNative
public static String getPackageLabel() {
- // Third-party code does disk read on the getApplicationInfo call. http://crbug.com/614343
- StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads();
- try {
- PackageManager packageManager =
- ContextUtils.getApplicationContext().getPackageManager();
- ApplicationInfo appInfo = packageManager.getApplicationInfo(
- getPackageName(), PackageManager.GET_META_DATA);
- CharSequence label = packageManager.getApplicationLabel(appInfo);
- return label != null ? label.toString() : "";
- } catch (NameNotFoundException e) {
- return "";
- } finally {
- StrictMode.setThreadPolicy(oldPolicy);
- }
+ return getAll()[7];
}
- @CalledByNative
public static String getPackageName() {
- if (ContextUtils.getApplicationContext() == null) {
- return "";
- }
return ContextUtils.getApplicationContext().getPackageName();
}
- @CalledByNative
- public static String getBuildType() {
- return Build.TYPE;
- }
-
/**
* Check if this is a debuggable build of Android. Use this to enable developer-only features.
*/
@@ -163,11 +99,6 @@ public class BuildInfo {
return "eng".equals(Build.TYPE) || "userdebug".equals(Build.TYPE);
}
- @CalledByNative
- public static int getSdkInt() {
- return Build.VERSION.SDK_INT;
- }
-
/**
* @return Whether the current device is running Android O release or newer.
*/
« base/android/build_info.h ('K') | « base/android/build_info.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698