Daniel Lehmann | f77b8e9 | 2010-07-16 18:58:36 -0700 | [diff] [blame] | 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 | |
David Braun | 76de0fa | 2013-09-24 17:08:14 -0700 | [diff] [blame] | 19 | import android.content.ComponentName; |
Daniel Lehmann | f77b8e9 | 2010-07-16 18:58:36 -0700 | [diff] [blame] | 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; |
Daniel Lehmann | 7781ea1 | 2010-10-21 21:56:43 -0700 | [diff] [blame] | 25 | import android.net.sip.SipManager; |
Yorke Lee | c21751d | 2012-12-05 15:56:40 -0800 | [diff] [blame] | 26 | import android.provider.MediaStore; |
David Braun | 76de0fa | 2013-09-24 17:08:14 -0700 | [diff] [blame] | 27 | import android.provider.Telephony; |
Nancy Chen | b8e0f40 | 2015-12-22 14:04:57 -0800 | [diff] [blame] | 28 | import android.telephony.TelephonyManager; |
Daniel Lehmann | f77b8e9 | 2010-07-16 18:58:36 -0700 | [diff] [blame] | 29 | |
Gary Mai | 0a49afa | 2016-12-05 15:53:58 -0800 | [diff] [blame^] | 30 | import com.android.contacts.ContactsUtils; |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 31 | import com.android.contacts.compat.TelephonyManagerCompat; |
Chiao Cheng | e0b2f1e | 2012-06-12 13:07:56 -0700 | [diff] [blame] | 32 | |
Daniel Lehmann | f77b8e9 | 2010-07-16 18:58:36 -0700 | [diff] [blame] | 33 | import java.util.List; |
| 34 | |
Daniel Lehmann | e9b196e | 2010-10-26 12:17:39 -0700 | [diff] [blame] | 35 | /** |
| 36 | * Provides static functions to quickly test the capabilities of this device. The static |
| 37 | * members are not safe for threading |
| 38 | */ |
Daniel Lehmann | f77b8e9 | 2010-07-16 18:58:36 -0700 | [diff] [blame] | 39 | public final class PhoneCapabilityTester { |
Daniel Lehmann | e9b196e | 2010-10-26 12:17:39 -0700 | [diff] [blame] | 40 | private static boolean sIsInitialized; |
| 41 | private static boolean sIsPhone; |
| 42 | private static boolean sIsSipPhone; |
| 43 | |
Daniel Lehmann | f77b8e9 | 2010-07-16 18:58:36 -0700 | [diff] [blame] | 44 | /** |
| 45 | * Tests whether the Intent has a receiver registered. This can be used to show/hide |
| 46 | * functionality (like Phone, SMS) |
| 47 | */ |
| 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 | /** |
Daniel Lehmann | 7779f49 | 2010-08-24 15:26:36 -0700 | [diff] [blame] | 56 | * Returns true if this device can be used to make phone calls |
Daniel Lehmann | f77b8e9 | 2010-07-16 18:58:36 -0700 | [diff] [blame] | 57 | */ |
Daniel Lehmann | 7779f49 | 2010-08-24 15:26:36 -0700 | [diff] [blame] | 58 | public static boolean isPhone(Context context) { |
Daniel Lehmann | e9b196e | 2010-10-26 12:17:39 -0700 | [diff] [blame] | 59 | if (!sIsInitialized) initialize(context); |
Daniel Lehmann | 7779f49 | 2010-08-24 15:26:36 -0700 | [diff] [blame] | 60 | // Is the device physically capabable of making phone calls? |
Daniel Lehmann | e9b196e | 2010-10-26 12:17:39 -0700 | [diff] [blame] | 61 | return sIsPhone; |
| 62 | } |
Daniel Lehmann | 7779f49 | 2010-08-24 15:26:36 -0700 | [diff] [blame] | 63 | |
Daniel Lehmann | e9b196e | 2010-10-26 12:17:39 -0700 | [diff] [blame] | 64 | private static void initialize(Context context) { |
Nancy Chen | b8e0f40 | 2015-12-22 14:04:57 -0800 | [diff] [blame] | 65 | sIsPhone = TelephonyManagerCompat.isVoiceCapable( |
| 66 | (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE)); |
Dmitri Plotnikov | 27bfa40 | 2011-02-22 17:56:15 -0800 | [diff] [blame] | 67 | sIsSipPhone = sIsPhone && SipManager.isVoipSupported(context); |
Daniel Lehmann | e9b196e | 2010-10-26 12:17:39 -0700 | [diff] [blame] | 68 | sIsInitialized = true; |
Daniel Lehmann | f77b8e9 | 2010-07-16 18:58:36 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /** |
Daniel Lehmann | 7781ea1 | 2010-10-21 21:56:43 -0700 | [diff] [blame] | 72 | * Returns true if this device can be used to make sip calls |
| 73 | */ |
| 74 | public static boolean isSipPhone(Context context) { |
Daniel Lehmann | e9b196e | 2010-10-26 12:17:39 -0700 | [diff] [blame] | 75 | if (!sIsInitialized) initialize(context); |
| 76 | return sIsSipPhone; |
Daniel Lehmann | 7781ea1 | 2010-10-21 21:56:43 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | /** |
David Braun | 76de0fa | 2013-09-24 17:08:14 -0700 | [diff] [blame] | 80 | * Returns the component name to use for sending to sms or null. |
Daniel Lehmann | f77b8e9 | 2010-07-16 18:58:36 -0700 | [diff] [blame] | 81 | */ |
David Braun | 76de0fa | 2013-09-24 17:08:14 -0700 | [diff] [blame] | 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, |
Jay Shrauner | 1cd88e3 | 2014-09-05 15:37:55 -0700 | [diff] [blame] | 87 | Uri.fromParts(ContactsUtils.SCHEME_SMSTO, "", null)); |
David Braun | 76de0fa | 2013-09-24 17:08:14 -0700 | [diff] [blame] | 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; |
Daniel Lehmann | f77b8e9 | 2010-07-16 18:58:36 -0700 | [diff] [blame] | 96 | } |
Daniel Lehmann | c90c705 | 2011-03-16 18:42:34 -0700 | [diff] [blame] | 97 | |
| 98 | /** |
Yorke Lee | c21751d | 2012-12-05 15:56:40 -0800 | [diff] [blame] | 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 | } |
Daniel Lehmann | f77b8e9 | 2010-07-16 18:58:36 -0700 | [diff] [blame] | 105 | } |