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 
17 package com.android.contacts.util;
18 
19 import android.content.ComponentName;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.pm.PackageManager;
23 import android.content.pm.ResolveInfo;
24 import android.net.Uri;
25 import android.net.sip.SipManager;
26 import android.provider.MediaStore;
27 import android.provider.Telephony;
28 import android.telephony.TelephonyManager;
29 
30 import com.android.contacts.ContactsUtils;
31 import com.android.contacts.compat.TelephonyManagerCompat;
32 
33 import java.util.List;
34 
35 /**
36  * Provides static functions to quickly test the capabilities of this device. The static
37  * members are not safe for threading
38  */
39 public final class PhoneCapabilityTester {
40     private static boolean sIsInitialized;
41     private static boolean sIsPhone;
42     private static boolean sIsSipPhone;
43 
44     /**
45      * Tests whether the Intent has a receiver registered. This can be used to show/hide
46      * functionality (like Phone, SMS)
47      */
isIntentRegistered(Context context, Intent intent)48     public static boolean isIntentRegistered(Context context, Intent intent) {
49         final PackageManager packageManager = context.getPackageManager();
50         final List<ResolveInfo> receiverList = packageManager.queryIntentActivities(intent,
51                 PackageManager.MATCH_DEFAULT_ONLY);
52         return receiverList.size() > 0;
53     }
54 
55     /**
56      * Returns true if this device can be used to make phone calls
57      */
isPhone(Context context)58     public static boolean isPhone(Context context) {
59         if (!sIsInitialized) initialize(context);
60         // Is the device physically capabable of making phone calls?
61         return sIsPhone;
62     }
63 
initialize(Context context)64     private static void initialize(Context context) {
65         sIsPhone = TelephonyManagerCompat.isVoiceCapable(
66                 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE));
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      */
isSipPhone(Context context)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      */
getSmsComponent(Context context)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      */
isCameraIntentRegistered(Context context)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