PhoneCapabilityTester.java revision 27bfa40a248101925abd69cc3106897baf81cbac
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 java.util.List;
28
29/**
30 * Provides static functions to quickly test the capabilities of this device. The static
31 * members are not safe for threading
32 */
33public final class PhoneCapabilityTester {
34    private static boolean sIsInitialized;
35    private static boolean sIsPhone;
36    private static boolean sIsSipPhone;
37
38    /**
39     * Tests whether the Intent has a receiver registered. This can be used to show/hide
40     * functionality (like Phone, SMS)
41     */
42    public static boolean isIntentRegistered(Context context, Intent intent) {
43        final PackageManager packageManager = context.getPackageManager();
44        final List<ResolveInfo> receiverList = packageManager.queryIntentActivities(intent,
45                PackageManager.MATCH_DEFAULT_ONLY);
46        return receiverList.size() > 0;
47    }
48
49    /**
50     * Returns true if this device can be used to make phone calls
51     */
52    public static boolean isPhone(Context context) {
53        if (!sIsInitialized) initialize(context);
54        // Is the device physically capabable of making phone calls?
55        return sIsPhone;
56    }
57
58    private static void initialize(Context context) {
59        final TelephonyManager telephonyManager = new TelephonyManager(context);
60        sIsPhone = telephonyManager.isVoiceCapable();
61        sIsSipPhone = sIsPhone && SipManager.isVoipSupported(context);
62        sIsInitialized = true;
63    }
64
65    /**
66     * Returns true if this device can be used to make sip calls
67     */
68    public static boolean isSipPhone(Context context) {
69        if (!sIsInitialized) initialize(context);
70        return sIsSipPhone;
71    }
72
73    /**
74     * Returns true if the device has an SMS application installed.
75     */
76    public static boolean isSmsIntentRegistered(Context context) {
77        // Don't cache the result as the user might install third party apps to send SMS
78        final Intent intent = new Intent(Intent.ACTION_SENDTO,
79                Uri.fromParts(Constants.SCHEME_SMSTO, "", null));
80        return isIntentRegistered(context, intent);
81    }
82}
83