summaryrefslogtreecommitdiffstats
path: root/base/android
diff options
context:
space:
mode:
Diffstat (limited to 'base/android')
-rw-r--r--base/android/build_info.cc68
-rw-r--r--base/android/build_info.h88
-rw-r--r--base/android/java/org/chromium/base/BuildInfo.java88
3 files changed, 244 insertions, 0 deletions
diff --git a/base/android/build_info.cc b/base/android/build_info.cc
new file mode 100644
index 0000000..54be7a2
--- /dev/null
+++ b/base/android/build_info.cc
@@ -0,0 +1,68 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/android/build_info.h"
+
+#include <string>
+
+#include "base/android/jni_android.h"
+#include "base/android/jni_string.h"
+#include "base/android/scoped_java_ref.h"
+#include "base/logging.h"
+#include "base/memory/singleton.h"
+#include "jni/build_info_jni.h"
+
+namespace base {
+namespace android {
+
+BuildInfo::BuildInfo() {
+
+ JNIEnv* env = AttachCurrentThread();
+
+ // The const char* pointers initialized below will be owned by the
+ // resultant BuildInfo.
+ std::string device_str =
+ ConvertJavaStringToUTF8(Java_BuildInfo_getDevice(env));
+ device_ = strdup(device_str.c_str());
+
+ std::string model_str =
+ ConvertJavaStringToUTF8(Java_BuildInfo_getDeviceModel(env));
+ model_ = strdup(model_str.c_str());
+
+ std::string brand_str =
+ ConvertJavaStringToUTF8(Java_BuildInfo_getBrand(env));
+ brand_ = strdup(brand_str.c_str());
+
+ std::string android_build_id_str =
+ ConvertJavaStringToUTF8(Java_BuildInfo_getAndroidBuildId(env));
+ android_build_id_ = strdup(android_build_id_str.c_str());
+
+ std::string android_build_fp_str =
+ ConvertJavaStringToUTF8(Java_BuildInfo_getAndroidBuildFingerprint(env));
+ android_build_fp_ = strdup(android_build_fp_str.c_str());
+
+ jobject app_context = GetApplicationContext();
+ std::string package_version_code_str =
+ ConvertJavaStringToUTF8(Java_BuildInfo_getPackageVersionCode(
+ env, app_context));
+ package_version_code_ = strdup(package_version_code_str.c_str());
+
+ std::string package_version_name_str =
+ ConvertJavaStringToUTF8(
+ Java_BuildInfo_getPackageVersionName(env, app_context));
+ package_version_name_ = strdup(package_version_name_str.c_str());
+
+}
+
+// static
+BuildInfo* BuildInfo::GetInstance() {
+ return Singleton<BuildInfo, LeakySingletonTraits<BuildInfo> >::get();
+}
+
+bool RegisterBuildInfo(JNIEnv* env) {
+ return RegisterNativesImpl(env);
+}
+
+} // namespace android
+} // namespace base
diff --git a/base/android/build_info.h b/base/android/build_info.h
new file mode 100644
index 0000000..82635cd
--- /dev/null
+++ b/base/android/build_info.h
@@ -0,0 +1,88 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef BASE_ANDROID_BUILD_INFO_H_
+#define BASE_ANDROID_BUILD_INFO_H_
+
+#include <jni.h>
+
+#include <string>
+
+#include "base/memory/singleton.h"
+
+namespace base {
+namespace android {
+
+// BuildInfo is a singleton class that stores android build and device
+// information.
+class BuildInfo {
+ public:
+
+ ~BuildInfo() {}
+
+ // Static factory method for getting the singleton BuildInfo instance.
+ // Note that ownership is not conferred on the caller and the BuildInfo in
+ // question isn't actually freed until shutdown. This is ok because there
+ // should only be one instance of BuildInfo ever created.
+ static BuildInfo* GetInstance();
+
+ // Const char* is used instead of std::strings because these values must be
+ // available even if the process is in a crash state. Sadly
+ // std::string.c_str() doesn't guarantee that memory won't be allocated when
+ // it is called.
+ const char* device() const {
+ return device_;
+ }
+
+ const char* model() const {
+ return model_;
+ }
+
+ const char* brand() const {
+ return brand_;
+ }
+
+ const char* android_build_id() const {
+ return android_build_id_;
+ }
+
+ const char* android_build_fp() const {
+ return android_build_fp_;
+ }
+
+ const char* package_version_code() const {
+ return package_version_code_;
+ }
+
+ const char* package_version_name() const {
+ return package_version_name_;
+ }
+
+ private:
+ BuildInfo();
+
+ friend struct DefaultSingletonTraits<BuildInfo>;
+
+ // Const char* is used instead of std::strings because these values must be
+ // available even if the process is in a crash state. Sadly
+ // std::string.c_str() doesn't guarantee that memory won't be allocated when
+ // it is called.
+ char* device_;
+ char* model_;
+ char* brand_;
+ char* android_build_id_;
+ char* android_build_fp_;
+ char* package_version_code_;
+ char* package_version_name_;
+
+ DISALLOW_COPY_AND_ASSIGN(BuildInfo);
+
+};
+
+bool RegisterBuildInfo(JNIEnv* env);
+
+} // namespace android
+} // namespace base
+
+#endif // BASE_ANDROID_BUILD_INFO_H_
diff --git a/base/android/java/org/chromium/base/BuildInfo.java b/base/android/java/org/chromium/base/BuildInfo.java
new file mode 100644
index 0000000..75748ac
--- /dev/null
+++ b/base/android/java/org/chromium/base/BuildInfo.java
@@ -0,0 +1,88 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package org.chromium.base;
+
+import android.content.Context;
+import android.content.pm.PackageInfo;
+import android.content.pm.PackageManager;
+import android.content.pm.PackageManager.NameNotFoundException;
+import android.os.Build;
+import android.util.Log;
+
+/**
+ * BuildInfo is a utility class providing easy access to {@link PackageInfo}
+ * information. This is primarly of use for accessesing package information
+ * from native code.
+ */
+public class BuildInfo {
+ private static final String TAG = "BuildInfo";
+ private static final int MAX_FINGERPRINT_LENGTH = 128;
+
+ /**
+ * BuildInfo is a static utility class and therefore should'nt be
+ * instantiated.
+ */
+ private BuildInfo() {
+ }
+
+ @CalledByNative
+ public static String getDevice() {
+ return Build.DEVICE;
+ }
+
+ @CalledByNative
+ public static String getBrand() {
+ return Build.BRAND;
+ }
+
+ @CalledByNative
+ public static String getAndroidBuildId() {
+ return Build.ID;
+ }
+
+ /**
+ * @return The build fingerprint for the current Android install. The value is truncated to a
+ * 128 characters as this is used for crash and UMA reporting, which should avoid huge
+ * strings.
+ */
+ @CalledByNative
+ public static String getAndroidBuildFingerprint() {
+ return Build.FINGERPRINT.substring(
+ 0, Math.min(Build.FINGERPRINT.length(), MAX_FINGERPRINT_LENGTH));
+ }
+
+ @CalledByNative
+ public static String getDeviceModel() {
+ return Build.MODEL;
+ }
+
+ @CalledByNative
+ public static String getPackageVersionCode(Context context) {
+ String msg = "versionCode not available.";
+ try {
+ PackageManager pm = context.getPackageManager();
+ PackageInfo pi = pm.getPackageInfo("com.android.chrome", 0);
+ msg = "" + pi.versionCode;
+ } catch (NameNotFoundException e) {
+ Log.d(TAG, msg);
+ }
+ return msg;
+
+ }
+
+ @CalledByNative
+ public static String getPackageVersionName(Context context) {
+ String msg = "versionName not available";
+ try {
+ PackageManager pm = context.getPackageManager();
+ PackageInfo pi = pm.getPackageInfo("com.android.chrome", 0);
+ msg = pi.versionName;
+ } catch (NameNotFoundException e) {
+ Log.d(TAG, msg);
+ }
+ return msg;
+ }
+
+}