| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.ui.base; | 5 package org.chromium.ui.base; |
| 6 | 6 |
| 7 import android.content.Context; | 7 import android.content.Context; |
| 8 import android.os.Build; | 8 import android.os.Build; |
| 9 import android.util.DisplayMetrics; | 9 import android.util.DisplayMetrics; |
| 10 import android.view.WindowManager; | 10 import android.view.WindowManager; |
| 11 | 11 |
| 12 import org.chromium.base.ContextUtils; |
| 12 import org.chromium.base.annotations.CalledByNative; | 13 import org.chromium.base.annotations.CalledByNative; |
| 13 | 14 |
| 14 /** | 15 /** |
| 15 * UI utilities for accessing form factor information. | 16 * UI utilities for accessing form factor information. |
| 16 */ | 17 */ |
| 17 public class DeviceFormFactor { | 18 public class DeviceFormFactor { |
| 18 | 19 |
| 19 /** | 20 /** |
| 20 * The minimum width that would classify the device as a tablet or a large t
ablet. | 21 * The minimum width that would classify the device as a tablet or a large t
ablet. |
| 21 */ | 22 */ |
| 22 public static final int MINIMUM_TABLET_WIDTH_DP = 600; | 23 public static final int MINIMUM_TABLET_WIDTH_DP = 600; |
| 23 private static final int MINIMUM_LARGE_TABLET_WIDTH_DP = 720; | 24 private static final int MINIMUM_LARGE_TABLET_WIDTH_DP = 720; |
| 24 | 25 |
| 25 private static Boolean sIsTablet; | 26 private static Boolean sIsTablet; |
| 26 private static Boolean sIsLargeTablet; | 27 private static Boolean sIsLargeTablet; |
| 27 private static Integer sMinimumTabletWidthPx; | 28 private static Integer sMinimumTabletWidthPx; |
| 28 private static Float sDensity; | 29 private static Float sDensity; |
| 29 | 30 |
| 31 /** TODO(wnwen): Remove when downstream is migrated. */ |
| 32 public static boolean isTablet(Context context) { |
| 33 return isTablet(); |
| 34 } |
| 35 |
| 30 /** | 36 /** |
| 31 * @param context {@link Context} used to get the Application Context. | |
| 32 * @return Whether the app should treat the device as a tablet for layout. T
his method is not | 37 * @return Whether the app should treat the device as a tablet for layout. T
his method is not |
| 33 * affected by Android N multi-window. | 38 * affected by Android N multi-window. |
| 34 */ | 39 */ |
| 35 @CalledByNative | 40 @CalledByNative |
| 36 public static boolean isTablet(Context context) { | 41 public static boolean isTablet() { |
| 37 if (sIsTablet == null) { | 42 if (sIsTablet == null) { |
| 38 sIsTablet = getSmallestDeviceWidthDp(context) >= MINIMUM_TABLET_WIDT
H_DP; | 43 sIsTablet = getSmallestDeviceWidthDp() >= MINIMUM_TABLET_WIDTH_DP; |
| 39 } | 44 } |
| 40 return sIsTablet; | 45 return sIsTablet; |
| 41 } | 46 } |
| 42 | 47 |
| 43 /** | 48 /** |
| 44 * @param context {@link Context} used to get the Application Context. | 49 * @param context {@link Context} used to get the Application Context. |
| 45 * @return True if the app should treat the device as a large (> 720dp) tabl
et for layout. This | 50 * @return True if the app should treat the device as a large (> 720dp) tabl
et for layout. This |
| 46 * method is not affected by Android N multi-window. | 51 * method is not affected by Android N multi-window. |
| 47 */ | 52 */ |
| 48 public static boolean isLargeTablet(Context context) { | 53 public static boolean isLargeTablet(Context context) { |
| 49 if (sIsLargeTablet == null) { | 54 if (sIsLargeTablet == null) { |
| 50 sIsLargeTablet = getSmallestDeviceWidthDp(context) >= MINIMUM_LARGE_
TABLET_WIDTH_DP; | 55 sIsLargeTablet = getSmallestDeviceWidthDp() >= MINIMUM_LARGE_TABLET_
WIDTH_DP; |
| 51 } | 56 } |
| 52 return sIsLargeTablet; | 57 return sIsLargeTablet; |
| 53 } | 58 } |
| 54 | 59 |
| 55 /** | 60 /** |
| 56 * Calculates the minimum device width in dp. This method is not affected by
Android N | 61 * Calculates the minimum device width in dp. This method is not affected by
Android N |
| 57 * multi-window. | 62 * multi-window. |
| 58 * | 63 * |
| 59 * @param context {@link Context} used to get the Application Context. | |
| 60 * @return The smaller of device width and height in dp. | 64 * @return The smaller of device width and height in dp. |
| 61 */ | 65 */ |
| 62 public static int getSmallestDeviceWidthDp(Context context) { | 66 public static int getSmallestDeviceWidthDp() { |
| 63 assert context.getApplicationContext() != null; | |
| 64 | |
| 65 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { | 67 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { |
| 66 DisplayMetrics metrics = new DisplayMetrics(); | 68 DisplayMetrics metrics = new DisplayMetrics(); |
| 67 // The Application Context must be used instead of the regular Conte
xt, because | 69 // The Application Context must be used instead of the regular Conte
xt, because |
| 68 // in Android N multi-window calling Display.getRealMetrics() using
the regular Context | 70 // in Android N multi-window calling Display.getRealMetrics() using
the regular Context |
| 69 // returns the size of the current screen rather than the device. | 71 // returns the size of the current screen rather than the device. |
| 70 ((WindowManager) context.getApplicationContext().getSystemService( | 72 ((WindowManager) ContextUtils.getApplicationContext().getSystemServi
ce( |
| 71 Context.WINDOW_SERVICE)).getDefaultDisplay().getRealMetrics(
metrics); | 73 Context.WINDOW_SERVICE)) |
| 74 .getDefaultDisplay() |
| 75 .getRealMetrics(metrics); |
| 72 return Math.round(Math.min(metrics.heightPixels / metrics.density, | 76 return Math.round(Math.min(metrics.heightPixels / metrics.density, |
| 73 metrics.widthPixels / metrics.density)); | 77 metrics.widthPixels / metrics.density)); |
| 74 } else { | 78 } else { |
| 75 // Display.getRealMetrics() is only available in API level 17+, so | 79 // Display.getRealMetrics() is only available in API level 17+, so |
| 76 // Configuration.smallestScreenWidthDp is used instead. Proir to the
introduction of | 80 // Configuration.smallestScreenWidthDp is used instead. Proir to the
introduction of |
| 77 // multi-window in Android N, smallestScreenWidthDp was the same as
the minimum size | 81 // multi-window in Android N, smallestScreenWidthDp was the same as
the minimum size |
| 78 // in getRealMetrics(). | 82 // in getRealMetrics(). |
| 79 return context.getResources().getConfiguration().smallestScreenWidth
Dp; | 83 return ContextUtils.getApplicationContext() |
| 84 .getResources() |
| 85 .getConfiguration() |
| 86 .smallestScreenWidthDp; |
| 80 } | 87 } |
| 81 } | 88 } |
| 82 | 89 |
| 83 /** | 90 /** |
| 84 * @param context {@link Context} used to get the display density. | 91 * @param context {@link Context} used to get the display density. |
| 85 * @return The minimum width in px at which the device should be treated lik
e a tablet for | 92 * @return The minimum width in px at which the device should be treated lik
e a tablet for |
| 86 * layout. | 93 * layout. |
| 87 */ | 94 */ |
| 88 public static int getMinimumTabletWidthPx(Context context) { | 95 public static int getMinimumTabletWidthPx(Context context) { |
| 89 if (sMinimumTabletWidthPx == null) { | 96 if (sMinimumTabletWidthPx == null) { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 110 * Sets whether the device is a tablet. | 117 * Sets whether the device is a tablet. |
| 111 * @param isTablet Whether the app should treat the device as a tablet for l
ayout. | 118 * @param isTablet Whether the app should treat the device as a tablet for l
ayout. |
| 112 * @param isLargeTablet Whether the app should treat the device as a large t
ablet for layout. | 119 * @param isLargeTablet Whether the app should treat the device as a large t
ablet for layout. |
| 113 * If this is true, isTablet should also be true. | 120 * If this is true, isTablet should also be true. |
| 114 */ | 121 */ |
| 115 public static void setIsTablet(boolean isTablet, boolean isLargeTablet) { | 122 public static void setIsTablet(boolean isTablet, boolean isLargeTablet) { |
| 116 sIsTablet = isTablet; | 123 sIsTablet = isTablet; |
| 117 sIsLargeTablet = isLargeTablet; | 124 sIsLargeTablet = isLargeTablet; |
| 118 } | 125 } |
| 119 } | 126 } |
| OLD | NEW |