| 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | package org.chromium.content.browser; |
| 6 | |
| 7 | import android.content.Context; |
| 8 | import android.content.pm.PackageManager; |
| 9 | |
| 10 | import org.chromium.content.common.CommandLine; |
| 11 | |
| 12 | /** |
| 13 | * A utility class that has helper methods for device configuration. |
| 14 | */ |
| 15 | public class DeviceUtils { |
| 16 | |
| 17 | /** |
| 18 | * The minimum width that would classify the device as a tablet. |
| 19 | */ |
| 20 | private static final int MINIMUM_TABLET_WIDTH_DP = 600; |
| 21 | |
| 22 | private static Boolean sIsTv = null; |
| 23 | private static Boolean sIsTablet = null; |
| 24 | |
| 25 | /** |
| 26 | * @param context Android's context |
| 27 | * @return Whether the app is should treat the device as a tablet for layout. |
| 28 | */ |
| 29 | public static boolean isTablet(Context context) { |
| 30 | if (sIsTablet == null) { |
| 31 | if (isTv(context)) { |
| 32 | sIsTablet = true; |
| 33 | return sIsTablet; |
| 34 | } |
| 35 | int minimumScreenWidthDp = context.getResources().getConfiguration(). |
| 36 | smallestScreenWidthDp; |
| 37 | sIsTablet = minimumScreenWidthDp >= MINIMUM_TABLET_WIDTH_DP; |
| 38 | } |
| 39 | return sIsTablet; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Checks if the device should be treated as TV. Note that this should be |
| 44 | * invoked before {@link #isTablet(Context)} to get the correct result |
| 45 | * since they are not orthogonal. |
| 46 | * |
| 47 | * @param context Android context |
| 48 | * @return {@code true} if the device should be treated as TV. |
| 49 | */ |
| 50 | public static boolean isTv(Context context) { |
| 51 | if (sIsTv == null) { |
| 52 | PackageManager manager = context.getPackageManager(); |
| 53 | if (manager != null) { |
| 54 | sIsTv = manager.hasSystemFeature(PackageManager.FEATURE_TELEVISION); |
| 55 | return sIsTv; |
| 56 | } |
| 57 | sIsTv = false; |
| 58 | } |
| 59 | return sIsTv; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Appends the switch specifying which user agent should be used for this device. |
| 64 | * @param context The context for the caller activity. |
| 65 | */ |
| 66 | public static void addDeviceSpecificUserAgentSwitch(Context context) { |
| 67 | if (isTablet(context)) { |
| 68 | CommandLine.getInstance().appendSwitch(CommandLine.TABLET_UI); |
| 69 | } else { |
| 70 | CommandLine.getInstance().appendSwitch(CommandLine.USE_MOBILE_UA); |
| 71 | } |
| 72 | } |
| 73 | } |