blob: 9753b8851408740e4a8f356e1799f0fcd9b3e9a3 [file] [log] [blame]
Daniel Lehmannf77b8e92010-07-16 18:58:36 -07001/*
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
David Braun76de0fa2013-09-24 17:08:14 -070019import android.content.ComponentName;
Daniel Lehmannf77b8e92010-07-16 18:58:36 -070020import android.content.Context;
21import android.content.Intent;
22import android.content.pm.PackageManager;
23import android.content.pm.ResolveInfo;
24import android.net.Uri;
Daniel Lehmann7781ea12010-10-21 21:56:43 -070025import android.net.sip.SipManager;
Yorke Leec21751d2012-12-05 15:56:40 -080026import android.provider.MediaStore;
David Braun76de0fa2013-09-24 17:08:14 -070027import android.provider.Telephony;
Nancy Chenb8e0f402015-12-22 14:04:57 -080028import android.telephony.TelephonyManager;
Daniel Lehmannf77b8e92010-07-16 18:58:36 -070029
Gary Mai0a49afa2016-12-05 15:53:58 -080030import com.android.contacts.ContactsUtils;
Gary Mai69c182a2016-12-05 13:07:03 -080031import com.android.contacts.compat.TelephonyManagerCompat;
Chiao Chenge0b2f1e2012-06-12 13:07:56 -070032
Daniel Lehmannf77b8e92010-07-16 18:58:36 -070033import java.util.List;
34
Daniel Lehmanne9b196e2010-10-26 12:17:39 -070035/**
36 * Provides static functions to quickly test the capabilities of this device. The static
37 * members are not safe for threading
38 */
Daniel Lehmannf77b8e92010-07-16 18:58:36 -070039public final class PhoneCapabilityTester {
Daniel Lehmanne9b196e2010-10-26 12:17:39 -070040 private static boolean sIsInitialized;
41 private static boolean sIsPhone;
42 private static boolean sIsSipPhone;
43
Daniel Lehmannf77b8e92010-07-16 18:58:36 -070044 /**
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 Lehmann7779f492010-08-24 15:26:36 -070056 * Returns true if this device can be used to make phone calls
Daniel Lehmannf77b8e92010-07-16 18:58:36 -070057 */
Daniel Lehmann7779f492010-08-24 15:26:36 -070058 public static boolean isPhone(Context context) {
Daniel Lehmanne9b196e2010-10-26 12:17:39 -070059 if (!sIsInitialized) initialize(context);
Daniel Lehmann7779f492010-08-24 15:26:36 -070060 // Is the device physically capabable of making phone calls?
Daniel Lehmanne9b196e2010-10-26 12:17:39 -070061 return sIsPhone;
62 }
Daniel Lehmann7779f492010-08-24 15:26:36 -070063
Daniel Lehmanne9b196e2010-10-26 12:17:39 -070064 private static void initialize(Context context) {
Nancy Chenb8e0f402015-12-22 14:04:57 -080065 sIsPhone = TelephonyManagerCompat.isVoiceCapable(
66 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE));
Dmitri Plotnikov27bfa402011-02-22 17:56:15 -080067 sIsSipPhone = sIsPhone && SipManager.isVoipSupported(context);
Daniel Lehmanne9b196e2010-10-26 12:17:39 -070068 sIsInitialized = true;
Daniel Lehmannf77b8e92010-07-16 18:58:36 -070069 }
70
71 /**
Daniel Lehmann7781ea12010-10-21 21:56:43 -070072 * Returns true if this device can be used to make sip calls
73 */
74 public static boolean isSipPhone(Context context) {
Daniel Lehmanne9b196e2010-10-26 12:17:39 -070075 if (!sIsInitialized) initialize(context);
76 return sIsSipPhone;
Daniel Lehmann7781ea12010-10-21 21:56:43 -070077 }
78
79 /**
David Braun76de0fa2013-09-24 17:08:14 -070080 * Returns the component name to use for sending to sms or null.
Daniel Lehmannf77b8e92010-07-16 18:58:36 -070081 */
David Braun76de0fa2013-09-24 17:08:14 -070082 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 Shrauner1cd88e32014-09-05 15:37:55 -070087 Uri.fromParts(ContactsUtils.SCHEME_SMSTO, "", null));
David Braun76de0fa2013-09-24 17:08:14 -070088 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 Lehmannf77b8e92010-07-16 18:58:36 -070096 }
Daniel Lehmannc90c7052011-03-16 18:42:34 -070097
98 /**
Yorke Leec21751d2012-12-05 15:56:40 -080099 * 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 Lehmannf77b8e92010-07-16 18:58:36 -0700105}