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