PhoneCapabilityTester.java revision e0b2f1e2d01d1ac52ba207dc7ce76971d853298e
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.contacts.util;
18
19import android.content.Context;
20import android.content.Intent;
21import android.content.pm.PackageManager;
22import android.content.pm.ResolveInfo;
23import android.net.Uri;
24import android.net.sip.SipManager;
25import android.telephony.TelephonyManager;
26
27import com.android.contacts.R;
28
29import java.util.List;
30
31/**
32 * Provides static functions to quickly test the capabilities of this device. The static
33 * members are not safe for threading
34 */
35public final class PhoneCapabilityTester {
36    private static boolean sIsInitialized;
37    private static boolean sIsPhone;
38    private static boolean sIsSipPhone;
39
40    /**
41     * Tests whether the Intent has a receiver registered. This can be used to show/hide
42     * functionality (like Phone, SMS)
43     */
44    public static boolean isIntentRegistered(Context context, Intent intent) {
45        final PackageManager packageManager = context.getPackageManager();
46        final List<ResolveInfo> receiverList = packageManager.queryIntentActivities(intent,
47                PackageManager.MATCH_DEFAULT_ONLY);
48        return receiverList.size() > 0;
49    }
50
51    /**
52     * Returns true if this device can be used to make phone calls
53     */
54    public static boolean isPhone(Context context) {
55        if (!sIsInitialized) initialize(context);
56        // Is the device physically capabable of making phone calls?
57        return sIsPhone;
58    }
59
60    private static void initialize(Context context) {
61        final TelephonyManager telephonyManager = new TelephonyManager(context);
62        sIsPhone = telephonyManager.isVoiceCapable();
63        sIsSipPhone = sIsPhone && SipManager.isVoipSupported(context);
64        sIsInitialized = true;
65    }
66
67    /**
68     * Returns true if this device can be used to make sip calls
69     */
70    public static boolean isSipPhone(Context context) {
71        if (!sIsInitialized) initialize(context);
72        return sIsSipPhone;
73    }
74
75    /**
76     * Returns true if the device has an SMS application installed.
77     */
78    public static boolean isSmsIntentRegistered(Context context) {
79        // Don't cache the result as the user might install third party apps to send SMS
80        final Intent intent = new Intent(Intent.ACTION_SENDTO,
81                Uri.fromParts(Constants.SCHEME_SMSTO, "", null));
82        return isIntentRegistered(context, intent);
83    }
84
85    /**
86     * True if we are using two-pane layouts ("tablet mode"), false if we are using single views
87     * ("phone mode")
88     */
89    public static boolean isUsingTwoPanes(Context context) {
90        return context.getResources().getBoolean(R.bool.config_use_two_panes);
91    }
92
93    /**
94     * True if the favorites tab should be shown in two-pane mode.  False, otherwise.
95     */
96    public static boolean isUsingTwoPanesInFavorites(Context context) {
97        return context.getResources().getBoolean(R.bool.config_use_two_panes_in_favorites);
98    }
99}
100