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

Unified Diff: ui/android/java/src/org/chromium/ui/base/DeviceFormFactor.java

Issue 1770883004: Use Display.getRealMetrics() to calculate device size (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove target api annotation Created 4 years, 9 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
« no previous file with comments | « chrome/android/java/src/org/chromium/chrome/browser/init/AsyncInitializationActivity.java ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/android/java/src/org/chromium/ui/base/DeviceFormFactor.java
diff --git a/ui/android/java/src/org/chromium/ui/base/DeviceFormFactor.java b/ui/android/java/src/org/chromium/ui/base/DeviceFormFactor.java
index 4b472c02718213b3faedc03bb316f30a399d6040..e8a52fa39245eae107e21bc2568e604bbd7f1488 100644
--- a/ui/android/java/src/org/chromium/ui/base/DeviceFormFactor.java
+++ b/ui/android/java/src/org/chromium/ui/base/DeviceFormFactor.java
@@ -5,7 +5,9 @@
package org.chromium.ui.base;
import android.content.Context;
+import android.os.Build;
import android.util.DisplayMetrics;
+import android.view.WindowManager;
import org.chromium.base.annotations.CalledByNative;
@@ -24,42 +26,52 @@ public class DeviceFormFactor {
private static Boolean sIsLargeTablet = null;
/**
- * @param context Android's context.
- * @return Whether the app should treat the device as a tablet for layout. This method
- * does not depend on the current window size and is not affected by
- * multi-window. It is dependent only on the device's size.
+ * @param context {@link Context} used to get the Application Context.
+ * @return Whether the app should treat the device as a tablet for layout. This method is not
+ * affected by Android N multi-window.
*/
@CalledByNative
public static boolean isTablet(Context context) {
if (sIsTablet == null) {
- sIsTablet = getSmallestDeviceWidthDp(context.getResources().getDisplayMetrics())
- >= MINIMUM_TABLET_WIDTH_DP;
+ sIsTablet = getSmallestDeviceWidthDp(context) >= MINIMUM_TABLET_WIDTH_DP;
}
return sIsTablet;
}
/**
- * @param context Android's context.
- * @return True if the current device's minimum dimension is larger than 720dp. This method
- * does not depend on the current window size and is not affected by
- * multi-window.
+ * @param context {@link Context} used to get the Application Context.
+ * @return True if the app should treat the device as a large (> 720dp) tablet for layout. This
+ * method is not affected by Android N multi-window.
*/
public static boolean isLargeTablet(Context context) {
if (sIsLargeTablet == null) {
- sIsLargeTablet = getSmallestDeviceWidthDp(context.getResources().getDisplayMetrics())
- >= MINIMUM_LARGE_TABLET_WIDTH_DP;
+ sIsLargeTablet = getSmallestDeviceWidthDp(context) >= MINIMUM_LARGE_TABLET_WIDTH_DP;
}
return sIsLargeTablet;
}
/**
- * Calculates the minimum device width in dp.
- * @param metrics The {@link DisplayMetrics} to use for calculating device size.
+ * Calculates the minimum device width in dp. This method is not affected by Android N
+ * multi-window.
+ * @param context Android's context.
* @return The smaller of device width and height in dp.
*/
- public static int getSmallestDeviceWidthDp(DisplayMetrics metrics) {
- int smallestDeviceWidthDp = Math.round(Math.min(metrics.heightPixels / metrics.density,
- metrics.widthPixels / metrics.density));
- return smallestDeviceWidthDp;
+ public static int getSmallestDeviceWidthDp(Context context) {
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
+ DisplayMetrics metrics = new DisplayMetrics();
+ // The Application Context must be used instead of the regular Context, because
+ // in Android N multi-window calling Display.getRealMetrics() using the regular Context
+ // returns the size of the current screen rather than the device.
+ ((WindowManager) context.getApplicationContext().getSystemService(
+ Context.WINDOW_SERVICE)).getDefaultDisplay().getRealMetrics(metrics);
+ return Math.round(Math.min(metrics.heightPixels / metrics.density,
+ metrics.widthPixels / metrics.density));
+ } else {
+ // Display.getRealMetrics() is only available in API level 17+, so
+ // Configuration.smallestScreenWidthDp is used instead. Proir to the introduction of
+ // multi-window in Android N, smallestScreenWidthDp was the same as the minimum size
+ // in getRealMetrics().
+ return context.getResources().getConfiguration().smallestScreenWidthDp;
+ }
}
}
« no previous file with comments | « chrome/android/java/src/org/chromium/chrome/browser/init/AsyncInitializationActivity.java ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698