Chiao Cheng | cb5b270 | 2012-09-05 16:10:50 -0700 | [diff] [blame] | 1 | /* |
Brian Attwell | 066bd68 | 2015-02-03 17:10:19 -0800 | [diff] [blame] | 2 | * Copyright (C) 2015 The Android Open Source Project |
Chiao Cheng | cb5b270 | 2012-09-05 16:10:50 -0700 | [diff] [blame] | 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.common; |
| 18 | |
Nancy Chen | 5fe647d | 2015-12-08 16:57:48 -0800 | [diff] [blame] | 19 | import com.android.contacts.common.compat.CompatUtils; |
Ta-wei Yen | 59023c2 | 2016-01-08 15:46:27 -0800 | [diff] [blame] | 20 | import com.android.contacts.common.compat.PhoneAccountSdkCompat; |
Yorke Lee | d2c9692 | 2015-09-18 12:59:16 -0700 | [diff] [blame] | 21 | import com.android.contacts.common.util.PermissionsUtil; |
Brian Attwell | 066bd68 | 2015-02-03 17:10:19 -0800 | [diff] [blame] | 22 | import com.android.contacts.common.util.PhoneNumberHelper; |
Wenyi Wang | 7a27b85 | 2016-11-11 11:36:24 -0800 | [diff] [blame] | 23 | import com.android.contactsbind.FeedbackHelper; |
Brian Attwell | 066bd68 | 2015-02-03 17:10:19 -0800 | [diff] [blame] | 24 | import com.android.phone.common.PhoneConstants; |
| 25 | |
Andrew Lee | 7e558dc | 2014-08-20 16:02:11 -0700 | [diff] [blame] | 26 | import android.content.Context; |
Chiao Cheng | cb5b270 | 2012-09-05 16:10:50 -0700 | [diff] [blame] | 27 | import android.content.Intent; |
| 28 | import android.net.Uri; |
Tyler Gunn | 5cf7f01 | 2014-09-10 15:20:21 -0700 | [diff] [blame] | 29 | import android.telecom.PhoneAccount; |
Vinit Deshpande | ca6b31e | 2015-03-15 13:23:01 -0700 | [diff] [blame] | 30 | import android.telecom.PhoneAccountHandle; |
Tyler Gunn | 5cf7f01 | 2014-09-10 15:20:21 -0700 | [diff] [blame] | 31 | import android.telecom.TelecomManager; |
| 32 | import android.telecom.VideoProfile; |
Brian Attwell | 066bd68 | 2015-02-03 17:10:19 -0800 | [diff] [blame] | 33 | import android.text.TextUtils; |
Chiao Cheng | cb5b270 | 2012-09-05 16:10:50 -0700 | [diff] [blame] | 34 | |
Yorke Lee | 9028db0 | 2015-03-19 17:01:54 +0000 | [diff] [blame] | 35 | import java.util.List; |
| 36 | |
Chiao Cheng | cb5b270 | 2012-09-05 16:10:50 -0700 | [diff] [blame] | 37 | /** |
Brian Attwell | 066bd68 | 2015-02-03 17:10:19 -0800 | [diff] [blame] | 38 | * Utilities related to calls that can be used by non system apps. These |
| 39 | * use {@link Intent#ACTION_CALL} instead of ACTION_CALL_PRIVILEGED. |
| 40 | * |
| 41 | * The privileged version of this util exists inside Dialer. |
Chiao Cheng | cb5b270 | 2012-09-05 16:10:50 -0700 | [diff] [blame] | 42 | */ |
| 43 | public class CallUtil { |
| 44 | |
Wenyi Wang | 7a27b85 | 2016-11-11 11:36:24 -0800 | [diff] [blame] | 45 | public static final String TAG = "CallUtil"; |
| 46 | |
Chiao Cheng | cb5b270 | 2012-09-05 16:10:50 -0700 | [diff] [blame] | 47 | /** |
Tyler Gunn | 001d974 | 2015-12-18 13:57:02 -0800 | [diff] [blame] | 48 | * Indicates that the video calling is not available. |
| 49 | */ |
| 50 | public static final int VIDEO_CALLING_DISABLED = 0; |
| 51 | |
| 52 | /** |
| 53 | * Indicates that video calling is enabled, regardless of presence status. |
| 54 | */ |
| 55 | public static final int VIDEO_CALLING_ENABLED = 1; |
| 56 | |
| 57 | /** |
| 58 | * Indicates that video calling is enabled, but the availability of video call affordances is |
| 59 | * determined by the presence status associated with contacts. |
| 60 | */ |
| 61 | public static final int VIDEO_CALLING_PRESENCE = 2; |
| 62 | |
| 63 | /** |
Chiao Cheng | cb5b270 | 2012-09-05 16:10:50 -0700 | [diff] [blame] | 64 | * Return an Intent for making a phone call. Scheme (e.g. tel, sip) will be determined |
| 65 | * automatically. |
| 66 | */ |
Tyler Gunn | 9e0c14e | 2015-08-04 13:33:49 -0700 | [diff] [blame] | 67 | public static Intent getCallWithSubjectIntent(String number, |
| 68 | PhoneAccountHandle phoneAccountHandle, String callSubject) { |
| 69 | |
| 70 | final Intent intent = getCallIntent(getCallUri(number)); |
| 71 | intent.putExtra(TelecomManager.EXTRA_CALL_SUBJECT, callSubject); |
| 72 | if (phoneAccountHandle != null) { |
| 73 | intent.putExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, phoneAccountHandle); |
| 74 | } |
| 75 | return intent; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Return an Intent for making a phone call. Scheme (e.g. tel, sip) will be determined |
| 80 | * automatically. |
| 81 | */ |
Chiao Cheng | cb5b270 | 2012-09-05 16:10:50 -0700 | [diff] [blame] | 82 | public static Intent getCallIntent(String number) { |
Brian Attwell | 066bd68 | 2015-02-03 17:10:19 -0800 | [diff] [blame] | 83 | return getCallIntent(getCallUri(number)); |
Chiao Cheng | cb5b270 | 2012-09-05 16:10:50 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Return an Intent for making a phone call. A given Uri will be used as is (without any |
| 88 | * sanity check). |
| 89 | */ |
| 90 | public static Intent getCallIntent(Uri uri) { |
Brian Attwell | 066bd68 | 2015-02-03 17:10:19 -0800 | [diff] [blame] | 91 | return new Intent(Intent.ACTION_CALL, uri); |
Chiao Cheng | cb5b270 | 2012-09-05 16:10:50 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /** |
Brian Attwell | 066bd68 | 2015-02-03 17:10:19 -0800 | [diff] [blame] | 95 | * A variant of {@link #getCallIntent} for starting a video call. |
Andrew Lee | c18a754 | 2014-07-08 16:02:30 -0700 | [diff] [blame] | 96 | */ |
Nancy Chen | c095aee | 2014-07-09 10:29:41 -0700 | [diff] [blame] | 97 | public static Intent getVideoCallIntent(String number, String callOrigin) { |
Brian Attwell | 066bd68 | 2015-02-03 17:10:19 -0800 | [diff] [blame] | 98 | final Intent intent = new Intent(Intent.ACTION_CALL, getCallUri(number)); |
| 99 | intent.putExtra(TelecomManager.EXTRA_START_CALL_WITH_VIDEO_STATE, |
Yorke Lee | cb93d46 | 2015-05-12 16:23:06 -0700 | [diff] [blame] | 100 | VideoProfile.STATE_BIDIRECTIONAL); |
Brian Attwell | 066bd68 | 2015-02-03 17:10:19 -0800 | [diff] [blame] | 101 | if (!TextUtils.isEmpty(callOrigin)) { |
Chiao Cheng | cb5b270 | 2012-09-05 16:10:50 -0700 | [diff] [blame] | 102 | intent.putExtra(PhoneConstants.EXTRA_CALL_ORIGIN, callOrigin); |
| 103 | } |
| 104 | return intent; |
| 105 | } |
| 106 | |
| 107 | /** |
Jay Shrauner | ed1a3b2 | 2014-09-05 15:37:27 -0700 | [diff] [blame] | 108 | * Return Uri with an appropriate scheme, accepting both SIP and usual phone call |
Chiao Cheng | cb5b270 | 2012-09-05 16:10:50 -0700 | [diff] [blame] | 109 | * numbers. |
| 110 | */ |
| 111 | public static Uri getCallUri(String number) { |
Yorke Lee | 9e8e7cb | 2014-03-17 13:04:45 -0700 | [diff] [blame] | 112 | if (PhoneNumberHelper.isUriNumber(number)) { |
Jay Shrauner | ed1a3b2 | 2014-09-05 15:37:27 -0700 | [diff] [blame] | 113 | return Uri.fromParts(PhoneAccount.SCHEME_SIP, number, null); |
Chiao Cheng | cb5b270 | 2012-09-05 16:10:50 -0700 | [diff] [blame] | 114 | } |
Jay Shrauner | ed1a3b2 | 2014-09-05 15:37:27 -0700 | [diff] [blame] | 115 | return Uri.fromParts(PhoneAccount.SCHEME_TEL, number, null); |
Yorke Lee | 9028db0 | 2015-03-19 17:01:54 +0000 | [diff] [blame] | 116 | } |
Paul Soulos | 5604608 | 2014-08-18 16:12:40 -0700 | [diff] [blame] | 117 | |
Yorke Lee | 9028db0 | 2015-03-19 17:01:54 +0000 | [diff] [blame] | 118 | /** |
Yorke Lee | f0d7938 | 2015-09-14 16:29:05 -0700 | [diff] [blame] | 119 | * @return Uri that directly dials a user's voicemail inbox. |
| 120 | */ |
| 121 | public static Uri getVoicemailUri() { |
| 122 | return Uri.fromParts(PhoneAccount.SCHEME_VOICEMAIL, "", null); |
| 123 | } |
| 124 | |
| 125 | /** |
Tyler Gunn | 001d974 | 2015-12-18 13:57:02 -0800 | [diff] [blame] | 126 | * Determines if video calling is available, and if so whether presence checking is available |
| 127 | * as well. |
| 128 | * |
| 129 | * Returns a bitmask with {@link #VIDEO_CALLING_ENABLED} to indicate that video calling is |
| 130 | * available, and {@link #VIDEO_CALLING_PRESENCE} if presence indication is also available. |
| 131 | * |
| 132 | * @param context The context |
| 133 | * @return A bit-mask describing the current video capabilities. |
| 134 | */ |
| 135 | public static int getVideoCallingAvailability(Context context) { |
| 136 | if (!PermissionsUtil.hasPermission(context, android.Manifest.permission.READ_PHONE_STATE) |
| 137 | || !CompatUtils.isVideoCompatible()) { |
| 138 | return VIDEO_CALLING_DISABLED; |
| 139 | } |
| 140 | TelecomManager telecommMgr = (TelecomManager) |
| 141 | context.getSystemService(Context.TELECOM_SERVICE); |
| 142 | if (telecommMgr == null) { |
| 143 | return VIDEO_CALLING_DISABLED; |
| 144 | } |
| 145 | |
Wenyi Wang | 7a27b85 | 2016-11-11 11:36:24 -0800 | [diff] [blame] | 146 | try { |
| 147 | List<PhoneAccountHandle> accountHandles = telecommMgr.getCallCapablePhoneAccounts(); |
| 148 | for (PhoneAccountHandle accountHandle : accountHandles) { |
| 149 | PhoneAccount account = telecommMgr.getPhoneAccount(accountHandle); |
| 150 | if (account != null) { |
| 151 | if (account.hasCapabilities(PhoneAccount.CAPABILITY_VIDEO_CALLING)) { |
| 152 | // Builds prior to N do not have presence support. |
| 153 | if (!CompatUtils.isVideoPresenceCompatible()) { |
| 154 | return VIDEO_CALLING_ENABLED; |
| 155 | } |
Tyler Gunn | 001d974 | 2015-12-18 13:57:02 -0800 | [diff] [blame] | 156 | |
Wenyi Wang | 7a27b85 | 2016-11-11 11:36:24 -0800 | [diff] [blame] | 157 | int videoCapabilities = VIDEO_CALLING_ENABLED; |
| 158 | if (account.hasCapabilities(PhoneAccountSdkCompat |
| 159 | .CAPABILITY_VIDEO_CALLING_RELIES_ON_PRESENCE)) { |
| 160 | videoCapabilities |= VIDEO_CALLING_PRESENCE; |
| 161 | } |
| 162 | return videoCapabilities; |
Tyler Gunn | 001d974 | 2015-12-18 13:57:02 -0800 | [diff] [blame] | 163 | } |
Tyler Gunn | 001d974 | 2015-12-18 13:57:02 -0800 | [diff] [blame] | 164 | } |
| 165 | } |
Wenyi Wang | 7a27b85 | 2016-11-11 11:36:24 -0800 | [diff] [blame] | 166 | return VIDEO_CALLING_DISABLED; |
| 167 | } catch (SecurityException e) { |
| 168 | FeedbackHelper.sendFeedback(context, TAG, |
| 169 | "Security exception when querying intent activities", e); |
| 170 | return VIDEO_CALLING_DISABLED; |
Tyler Gunn | 001d974 | 2015-12-18 13:57:02 -0800 | [diff] [blame] | 171 | } |
Tyler Gunn | 001d974 | 2015-12-18 13:57:02 -0800 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | /** |
Yorke Lee | 9028db0 | 2015-03-19 17:01:54 +0000 | [diff] [blame] | 175 | * Determines if one of the call capable phone accounts defined supports video calling. |
| 176 | * |
| 177 | * @param context The context. |
| 178 | * @return {@code true} if one of the call capable phone accounts supports video calling, |
| 179 | * {@code false} otherwise. |
| 180 | */ |
Andrew Lee | 7e558dc | 2014-08-20 16:02:11 -0700 | [diff] [blame] | 181 | public static boolean isVideoEnabled(Context context) { |
Tyler Gunn | 001d974 | 2015-12-18 13:57:02 -0800 | [diff] [blame] | 182 | return (getVideoCallingAvailability(context) & VIDEO_CALLING_ENABLED) != 0; |
Paul Soulos | 5604608 | 2014-08-18 16:12:40 -0700 | [diff] [blame] | 183 | } |
Tyler Gunn | a05f153 | 2015-08-06 15:01:44 -0700 | [diff] [blame] | 184 | |
| 185 | /** |
| 186 | * Determines if one of the call capable phone accounts defined supports calling with a subject |
| 187 | * specified. |
| 188 | * |
| 189 | * @param context The context. |
| 190 | * @return {@code true} if one of the call capable phone accounts supports calling with a |
| 191 | * subject specified, {@code false} otherwise. |
| 192 | */ |
| 193 | public static boolean isCallWithSubjectSupported(Context context) { |
Nancy Chen | 5fe647d | 2015-12-08 16:57:48 -0800 | [diff] [blame] | 194 | if (!PermissionsUtil.hasPermission(context, android.Manifest.permission.READ_PHONE_STATE) |
| 195 | || !CompatUtils.isCallSubjectCompatible()) { |
Yorke Lee | d2c9692 | 2015-09-18 12:59:16 -0700 | [diff] [blame] | 196 | return false; |
| 197 | } |
Tyler Gunn | a05f153 | 2015-08-06 15:01:44 -0700 | [diff] [blame] | 198 | TelecomManager telecommMgr = (TelecomManager) |
| 199 | context.getSystemService(Context.TELECOM_SERVICE); |
| 200 | if (telecommMgr == null) { |
| 201 | return false; |
| 202 | } |
| 203 | |
| 204 | List<PhoneAccountHandle> accountHandles = telecommMgr.getCallCapablePhoneAccounts(); |
| 205 | for (PhoneAccountHandle accountHandle : accountHandles) { |
| 206 | PhoneAccount account = telecommMgr.getPhoneAccount(accountHandle); |
| 207 | if (account != null && account.hasCapabilities(PhoneAccount.CAPABILITY_CALL_SUBJECT)) { |
| 208 | return true; |
| 209 | } |
| 210 | } |
| 211 | return false; |
| 212 | } |
Chiao Cheng | cb5b270 | 2012-09-05 16:10:50 -0700 | [diff] [blame] | 213 | } |