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

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

Issue 2847523002: Android: Remove GetApplicationContext part 4 (Closed)
Patch Set: Rebase and fix build Created 3 years, 7 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 unified diff | Download patch
OLDNEW
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 @Deprecated
33 public static boolean isTablet(Context context) {
34 return isTablet();
35 }
36
30 /** 37 /**
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 38 * @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. 39 * affected by Android N multi-window.
34 */ 40 */
35 @CalledByNative 41 @CalledByNative
36 public static boolean isTablet(Context context) { 42 public static boolean isTablet() {
37 if (sIsTablet == null) { 43 if (sIsTablet == null) {
38 sIsTablet = getSmallestDeviceWidthDp(context) >= MINIMUM_TABLET_WIDT H_DP; 44 sIsTablet = getSmallestDeviceWidthDp() >= MINIMUM_TABLET_WIDTH_DP;
39 } 45 }
40 return sIsTablet; 46 return sIsTablet;
41 } 47 }
42 48
43 /** 49 /**
44 * @param context {@link Context} used to get the Application Context. 50 * @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 51 * @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. 52 * method is not affected by Android N multi-window.
47 */ 53 */
48 public static boolean isLargeTablet(Context context) { 54 public static boolean isLargeTablet(Context context) {
49 if (sIsLargeTablet == null) { 55 if (sIsLargeTablet == null) {
50 sIsLargeTablet = getSmallestDeviceWidthDp(context) >= MINIMUM_LARGE_ TABLET_WIDTH_DP; 56 sIsLargeTablet = getSmallestDeviceWidthDp() >= MINIMUM_LARGE_TABLET_ WIDTH_DP;
51 } 57 }
52 return sIsLargeTablet; 58 return sIsLargeTablet;
53 } 59 }
54 60
55 /** 61 /**
56 * Calculates the minimum device width in dp. This method is not affected by Android N 62 * Calculates the minimum device width in dp. This method is not affected by Android N
57 * multi-window. 63 * multi-window.
58 * 64 *
59 * @param context {@link Context} used to get the Application Context.
60 * @return The smaller of device width and height in dp. 65 * @return The smaller of device width and height in dp.
61 */ 66 */
62 public static int getSmallestDeviceWidthDp(Context context) { 67 public static int getSmallestDeviceWidthDp() {
63 assert context.getApplicationContext() != null;
64
65 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { 68 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
66 DisplayMetrics metrics = new DisplayMetrics(); 69 DisplayMetrics metrics = new DisplayMetrics();
67 // The Application Context must be used instead of the regular Conte xt, because 70 // 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 71 // in Android N multi-window calling Display.getRealMetrics() using the regular Context
69 // returns the size of the current screen rather than the device. 72 // returns the size of the current screen rather than the device.
70 ((WindowManager) context.getApplicationContext().getSystemService( 73 ((WindowManager) ContextUtils.getApplicationContext().getSystemServi ce(
71 Context.WINDOW_SERVICE)).getDefaultDisplay().getRealMetrics( metrics); 74 Context.WINDOW_SERVICE))
75 .getDefaultDisplay()
76 .getRealMetrics(metrics);
72 return Math.round(Math.min(metrics.heightPixels / metrics.density, 77 return Math.round(Math.min(metrics.heightPixels / metrics.density,
73 metrics.widthPixels / metrics.density)); 78 metrics.widthPixels / metrics.density));
74 } else { 79 } else {
75 // Display.getRealMetrics() is only available in API level 17+, so 80 // Display.getRealMetrics() is only available in API level 17+, so
76 // Configuration.smallestScreenWidthDp is used instead. Proir to the introduction of 81 // Configuration.smallestScreenWidthDp is used instead. Proir to the introduction of
77 // multi-window in Android N, smallestScreenWidthDp was the same as the minimum size 82 // multi-window in Android N, smallestScreenWidthDp was the same as the minimum size
78 // in getRealMetrics(). 83 // in getRealMetrics().
79 return context.getResources().getConfiguration().smallestScreenWidth Dp; 84 return ContextUtils.getApplicationContext()
85 .getResources()
86 .getConfiguration()
87 .smallestScreenWidthDp;
80 } 88 }
81 } 89 }
82 90
83 /** 91 /**
84 * @param context {@link Context} used to get the display density. 92 * @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 93 * @return The minimum width in px at which the device should be treated lik e a tablet for
86 * layout. 94 * layout.
87 */ 95 */
88 public static int getMinimumTabletWidthPx(Context context) { 96 public static int getMinimumTabletWidthPx(Context context) {
89 if (sMinimumTabletWidthPx == null) { 97 if (sMinimumTabletWidthPx == null) {
(...skipping 20 matching lines...) Expand all
110 * Sets whether the device is a tablet. 118 * Sets whether the device is a tablet.
111 * @param isTablet Whether the app should treat the device as a tablet for l ayout. 119 * @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. 120 * @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. 121 * If this is true, isTablet should also be true.
114 */ 122 */
115 public static void setIsTablet(boolean isTablet, boolean isLargeTablet) { 123 public static void setIsTablet(boolean isTablet, boolean isLargeTablet) {
116 sIsTablet = isTablet; 124 sIsTablet = isTablet;
117 sIsLargeTablet = isLargeTablet; 125 sIsLargeTablet = isLargeTablet;
118 } 126 }
119 } 127 }
OLDNEW
« no previous file with comments | « testing/android/native_test/native_test_launcher.cc ('k') | ui/android/java/src/org/chromium/ui/base/TouchDevice.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698