summaryrefslogtreecommitdiffstats
path: root/content
diff options
context:
space:
mode:
authorleandrogracia@chromium.org <leandrogracia@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-06-06 10:58:48 +0000
committerleandrogracia@chromium.org <leandrogracia@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-06-06 10:58:48 +0000
commited5c5706a923d79c593c39e50372c4fb7d0f6a69 (patch)
tree9998075b862310472862678e3d5f210cae747d73 /content
parent7225440953f38c425dbc21a6f3c94b6649fcdd3a (diff)
downloadchromium_src-ed5c5706a923d79c593c39e50372c4fb7d0f6a69.zip
chromium_src-ed5c5706a923d79c593c39e50372c4fb7d0f6a69.tar.gz
chromium_src-ed5c5706a923d79c593c39e50372c4fb7d0f6a69.tar.bz2
[Android] Facilitate access to device information.
Introduce a class to expose public Android device information normally available only using the Android SDK. This information includes Display properties, as well as the telephony network country ISO code which is used for phone number detection. BUG=125390 TEST=none Review URL: https://chromiumcodereview.appspot.com/10459073 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@140739 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'content')
-rw-r--r--content/app/android/content_jni_registrar.cc2
-rw-r--r--content/browser/android/device_info.cc80
-rw-r--r--content/browser/android/device_info.h42
-rw-r--r--content/content_browser.gypi2
-rw-r--r--content/content_jni.gypi2
-rw-r--r--content/public/android/java/org/chromium/content/browser/DeviceInfo.java115
6 files changed, 243 insertions, 0 deletions
diff --git a/content/app/android/content_jni_registrar.cc b/content/app/android/content_jni_registrar.cc
index d894f71..70aee5b 100644
--- a/content/app/android/content_jni_registrar.cc
+++ b/content/app/android/content_jni_registrar.cc
@@ -9,6 +9,7 @@
#include "content/app/android/content_main.h"
#include "content/browser/android/android_browser_process.h"
#include "content/browser/android/command_line.h"
+#include "content/browser/android/device_info.h"
#include "content/browser/android/download_controller.h"
#include "content/browser/android/trace_event_binding.h"
@@ -19,6 +20,7 @@ base::android::RegistrationMethod kContentRegisteredMethods[] = {
{ "AndroidBrowserProcess", content::RegisterAndroidBrowserProcess },
{ "CommandLine", RegisterCommandLine },
{ "ContentMain", content::RegisterContentMain },
+ { "DeviceInfo", RegisterDeviceInfo },
{ "DownloadController", DownloadController::RegisterDownloadController },
{ "TraceEvent", RegisterTraceEvent },
};
diff --git a/content/browser/android/device_info.cc b/content/browser/android/device_info.cc
new file mode 100644
index 0000000..6169c04
--- /dev/null
+++ b/content/browser/android/device_info.cc
@@ -0,0 +1,80 @@
+// 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 "content/browser/android/device_info.h"
+
+#include "base/android/jni_android.h"
+#include "base/android/jni_string.h"
+#include "base/logging.h"
+#include "jni/device_info_jni.h"
+
+using base::android::AttachCurrentThread;
+using base::android::ConvertJavaStringToUTF8;
+using base::android::ScopedJavaLocalRef;
+
+namespace content {
+
+DeviceInfo::DeviceInfo() {
+ JNIEnv* env = AttachCurrentThread();
+ j_device_info_.Reset(Java_DeviceInfo_create(env,
+ base::android::GetApplicationContext()));
+}
+
+DeviceInfo::~DeviceInfo() {
+}
+
+int DeviceInfo::GetHeight() {
+ JNIEnv* env = AttachCurrentThread();
+ jint result =
+ Java_DeviceInfo_getHeight(env, j_device_info_.obj());
+ return static_cast<int>(result);
+}
+
+int DeviceInfo::GetWidth() {
+ JNIEnv* env = AttachCurrentThread();
+ jint result =
+ Java_DeviceInfo_getWidth(env, j_device_info_.obj());
+ return static_cast<int>(result);
+}
+
+int DeviceInfo::GetBitsPerPixel() {
+ JNIEnv* env = AttachCurrentThread();
+ jint result =
+ Java_DeviceInfo_getBitsPerPixel(env, j_device_info_.obj());
+ return static_cast<int>(result);
+}
+
+int DeviceInfo::GetBitsPerComponent() {
+ JNIEnv* env = AttachCurrentThread();
+ jint result =
+ Java_DeviceInfo_getBitsPerComponent(env, j_device_info_.obj());
+ return static_cast<int>(result);
+}
+
+double DeviceInfo::GetDPIScale() {
+ JNIEnv* env = AttachCurrentThread();
+ jdouble result =
+ Java_DeviceInfo_getDPIScale(env, j_device_info_.obj());
+ return static_cast<double>(result);
+}
+
+double DeviceInfo::GetRefreshRate() {
+ JNIEnv* env = AttachCurrentThread();
+ jdouble result =
+ Java_DeviceInfo_getRefreshRate(env, j_device_info_.obj());
+ return static_cast<double>(result);
+}
+
+std::string DeviceInfo::GetNetworkCountryIso() {
+ JNIEnv* env = AttachCurrentThread();
+ ScopedJavaLocalRef<jstring> result =
+ Java_DeviceInfo_getNetworkCountryIso(env, j_device_info_.obj());
+ return ConvertJavaStringToUTF8(result);
+}
+
+bool RegisterDeviceInfo(JNIEnv* env) {
+ return RegisterNativesImpl(env);
+}
+
+} // namespace content
diff --git a/content/browser/android/device_info.h b/content/browser/android/device_info.h
new file mode 100644
index 0000000..b084fa2
--- /dev/null
+++ b/content/browser/android/device_info.h
@@ -0,0 +1,42 @@
+// 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 CONTENT_BROWSER_ANDROID_DEVICE_INFO_H_
+#define CONTENT_BROWSER_ANDROID_DEVICE_INFO_H_
+#pragma once
+
+#include <jni.h>
+#include <string>
+
+#include "base/android/scoped_java_ref.h"
+#include "base/basictypes.h"
+
+namespace content {
+
+// Facilitates access to device information typically only
+// available using the Android SDK, including Display properties.
+class DeviceInfo {
+ public:
+ DeviceInfo();
+ ~DeviceInfo();
+
+ int GetHeight();
+ int GetWidth();
+ int GetBitsPerPixel();
+ int GetBitsPerComponent();
+ double GetDPIScale();
+ double GetRefreshRate();
+ std::string GetNetworkCountryIso();
+
+ private:
+ base::android::ScopedJavaGlobalRef<jobject> j_device_info_;
+
+ DISALLOW_COPY_AND_ASSIGN(DeviceInfo);
+};
+
+bool RegisterDeviceInfo(JNIEnv* env);
+
+} // namespace content
+
+#endif // CONTENT_BROWSER_ANDROID_DEVICE_INFO_H_
diff --git a/content/content_browser.gypi b/content/content_browser.gypi
index c2710a0..5946f2d 100644
--- a/content/content_browser.gypi
+++ b/content/content_browser.gypi
@@ -188,6 +188,8 @@
'browser/android/android_browser_process.h',
'browser/android/command_line.cc',
'browser/android/command_line.h',
+ 'browser/android/device_info.cc',
+ 'browser/android/device_info.h',
'browser/android/download_controller.cc',
'browser/android/download_controller.h',
'browser/android/jni_helper.cc',
diff --git a/content/content_jni.gypi b/content/content_jni.gypi
index 42d2acc..d00c838 100644
--- a/content/content_jni.gypi
+++ b/content/content_jni.gypi
@@ -12,6 +12,7 @@
'public/android/java/org/chromium/content/app/ContentMain.java',
'public/android/java/org/chromium/content/browser/AndroidBrowserProcess.java',
'public/android/java/org/chromium/content/browser/CommandLine.java',
+ 'public/android/java/org/chromium/content/browser/DeviceInfo.java',
'public/android/java/org/chromium/content/browser/DeviceOrientation.java',
'public/android/java/org/chromium/content/browser/DownloadController.java',
'public/android/java/org/chromium/content/browser/JNIHelper.java',
@@ -23,6 +24,7 @@
'<(SHARED_INTERMEDIATE_DIR)/content/jni/content_main_jni.h',
'<(SHARED_INTERMEDIATE_DIR)/content/jni/android_browser_process_jni.h',
'<(SHARED_INTERMEDIATE_DIR)/content/jni/command_line_jni.h',
+ '<(SHARED_INTERMEDIATE_DIR)/content/jni/device_info_jni.h',
'<(SHARED_INTERMEDIATE_DIR)/content/jni/device_orientation_jni.h',
'<(SHARED_INTERMEDIATE_DIR)/content/jni/download_controller_jni.h',
'<(SHARED_INTERMEDIATE_DIR)/content/jni/jni_helper_jni.h',
diff --git a/content/public/android/java/org/chromium/content/browser/DeviceInfo.java b/content/public/android/java/org/chromium/content/browser/DeviceInfo.java
new file mode 100644
index 0000000..5837da9
--- /dev/null
+++ b/content/public/android/java/org/chromium/content/browser/DeviceInfo.java
@@ -0,0 +1,115 @@
+// 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.content.browser;
+
+import android.content.Context;
+import android.graphics.PixelFormat;
+import android.telephony.TelephonyManager;
+import android.util.DisplayMetrics;
+import android.view.Display;
+import android.view.WindowManager;
+
+import org.chromium.base.CalledByNative;
+
+/**
+ * This class facilitates access to android information typically only
+ * available using the Java SDK, including {@link Display} properties.
+ *
+ * Currently the information consists of very raw display information (height, width, DPI scale)
+ * regarding the main display, and also the current telephony region.
+ */
+public class DeviceInfo {
+
+ private WindowManager mWinManager;
+ private TelephonyManager mTelManager;
+
+ private DeviceInfo(Context context) {
+ Context appContext = context.getApplicationContext();
+ mWinManager = (WindowManager) appContext.getSystemService(Context.WINDOW_SERVICE);
+ mTelManager = (TelephonyManager) appContext.getSystemService(Context.TELEPHONY_SERVICE);
+ }
+
+ @CalledByNative
+ public int getHeight() {
+ return getMetrics().heightPixels;
+ }
+
+ @CalledByNative
+ public int getWidth() {
+ return getMetrics().widthPixels;
+ }
+
+ @CalledByNative
+ public int getBitsPerPixel() {
+ PixelFormat info = new PixelFormat();
+ PixelFormat.getPixelFormatInfo(getDisplay().getPixelFormat(), info);
+ return info.bitsPerPixel;
+ }
+
+ @CalledByNative
+ public int getBitsPerComponent() {
+ int format = getDisplay().getPixelFormat();
+ switch (format) {
+ case PixelFormat.RGBA_4444:
+ return 4;
+
+ case PixelFormat.RGBA_5551:
+ return 5;
+
+ case PixelFormat.RGBA_8888:
+ case PixelFormat.RGBX_8888:
+ case PixelFormat.RGB_888:
+ return 8;
+
+ case PixelFormat.RGB_332:
+ return 2;
+
+ case PixelFormat.RGB_565:
+ return 5;
+
+ // Non-RGB formats.
+ case PixelFormat.A_8:
+ case PixelFormat.LA_88:
+ case PixelFormat.L_8:
+ return 0;
+
+ // Unknown format. Use 8 as a sensible default.
+ default:
+ return 8;
+ }
+ }
+
+ @CalledByNative
+ public double getDPIScale() {
+ return getMetrics().density;
+ }
+
+ @CalledByNative
+ public double getRefreshRate() {
+ double result = getDisplay().getRefreshRate();
+ // Sanity check.
+ return (result >= 61 || result < 30) ? 0 : result;
+ }
+
+ @CalledByNative
+ public String getNetworkCountryIso() {
+ return mTelManager.getNetworkCountryIso();
+ }
+
+ private Display getDisplay() {
+ return mWinManager.getDefaultDisplay();
+ }
+
+ private DisplayMetrics getMetrics() {
+ DisplayMetrics metrics = new DisplayMetrics();
+ getDisplay().getMetrics(metrics);
+ return metrics;
+ }
+
+ @CalledByNative
+ private static DeviceInfo create(Context context) {
+ return new DeviceInfo(context);
+ }
+}