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