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.ComponentName;
20import android.content.Context;
21import android.content.Intent;
22import android.content.pm.PackageManager;
23import android.content.pm.ResolveInfo;
24import android.net.Uri;
25import android.net.sip.SipManager;
26import android.provider.MediaStore;
27import android.provider.Telephony;
28import android.telephony.TelephonyManager;
29
30import com.android.contacts.common.ContactsUtils;
31
32import java.util.List;
33
34/**
35 * Provides static functions to quickly test the capabilities of this device. The static
36 * members are not safe for threading
37 */
38public final class PhoneCapabilityTester {
39    private static boolean sIsInitialized;
40    private static boolean sIsPhone;
41    private static boolean sIsSipPhone;
42
43    /**
44     * Tests whether the Intent has a receiver registered. This can be used to show/hide
45     * functionality (like Phone, SMS)
46     */
47    public static boolean isIntentRegistered(Context context, Intent intent) {
48        final PackageManager packageManager = context.getPackageManager();
49        final List<ResolveInfo> receiverList = packageManager.queryIntentActivities(intent,
50                PackageManager.MATCH_DEFAULT_ONLY);
51        return receiverList.size() > 0;
52    }
53
54    /**
55     * Returns true if this device can be used to make phone calls
56     */
57    public static boolean isPhone(Context context) {
58        if (!sIsInitialized) initialize(context);
59        // Is the device physically capabable of making phone calls?
60        return sIsPhone;
61    }
62
63    private static void initialize(Context context) {
64        final TelephonyManager telephonyManager
65                = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
66        sIsPhone = telephonyManager.isVoiceCapable();
67        sIsSipPhone = sIsPhone && SipManager.isVoipSupported(context);
68        sIsInitialized = true;
69    }
70
71    /**
72     * Returns true if this device can be used to make sip calls
73     */
74    public static boolean isSipPhone(Context context) {
75        if (!sIsInitialized) initialize(context);
76        return sIsSipPhone;
77    }
78
79    /**
80     * Returns the component name to use for sending to sms or null.
81     */
82    public static ComponentName getSmsComponent(Context context) {
83        String smsPackage = Telephony.Sms.getDefaultSmsPackage(context);
84        if (smsPackage != null) {
85            final PackageManager packageManager = context.getPackageManager();
86            final Intent intent = new Intent(Intent.ACTION_SENDTO,
87                    Uri.fromParts(ContactsUtils.SCHEME_SMSTO, "", null));
88            final List<ResolveInfo> resolveInfos = packageManager.queryIntentActivities(intent, 0);
89            for (ResolveInfo resolveInfo : resolveInfos) {
90                if (smsPackage.equals(resolveInfo.activityInfo.packageName)) {
91                    return new ComponentName(smsPackage, resolveInfo.activityInfo.name);
92                }
93            }
94        }
95        return null;
96    }
97
98    /**
99     * Returns true if there is a camera on the device
100     */
101    public static boolean isCameraIntentRegistered(Context context) {
102        final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
103        return isIntentRegistered(context, intent);
104    }
105}
106