blob: 7172766420e2ca9bbb432306f3075e18ca6e72ca [file] [log] [blame]
Chiao Chengcb5b2702012-09-05 16:10:50 -07001/*
Brian Attwell066bd682015-02-03 17:10:19 -08002 * Copyright (C) 2015 The Android Open Source Project
Chiao Chengcb5b2702012-09-05 16:10:50 -07003 *
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
Gary Mai0a49afa2016-12-05 15:53:58 -080017package com.android.contacts;
Brian Attwell066bd682015-02-03 17:10:19 -080018
Andrew Lee7e558dc2014-08-20 16:02:11 -070019import android.content.Context;
Chiao Chengcb5b2702012-09-05 16:10:50 -070020import android.content.Intent;
21import android.net.Uri;
Tyler Gunn5cf7f012014-09-10 15:20:21 -070022import android.telecom.PhoneAccount;
Vinit Deshpandeca6b31e2015-03-15 13:23:01 -070023import android.telecom.PhoneAccountHandle;
Tyler Gunn5cf7f012014-09-10 15:20:21 -070024import android.telecom.TelecomManager;
25import android.telecom.VideoProfile;
yaolu18417a42017-05-04 12:33:47 -070026import android.telephony.PhoneNumberUtils;
Brian Attwell066bd682015-02-03 17:10:19 -080027import android.text.TextUtils;
Chiao Chengcb5b2702012-09-05 16:10:50 -070028
Gary Mai0a49afa2016-12-05 15:53:58 -080029import com.android.contacts.compat.CompatUtils;
30import com.android.contacts.compat.PhoneAccountSdkCompat;
31import com.android.contacts.util.PermissionsUtil;
32import com.android.contacts.util.PhoneNumberHelper;
33import com.android.contactsbind.FeedbackHelper;
34import com.android.phone.common.PhoneConstants;
35
Yorke Lee9028db02015-03-19 17:01:54 +000036import java.util.List;
37
Chiao Chengcb5b2702012-09-05 16:10:50 -070038/**
Brian Attwell066bd682015-02-03 17:10:19 -080039 * Utilities related to calls that can be used by non system apps. These
40 * use {@link Intent#ACTION_CALL} instead of ACTION_CALL_PRIVILEGED.
41 *
42 * The privileged version of this util exists inside Dialer.
Chiao Chengcb5b2702012-09-05 16:10:50 -070043 */
44public class CallUtil {
45
Wenyi Wang7a27b852016-11-11 11:36:24 -080046 public static final String TAG = "CallUtil";
47
Chiao Chengcb5b2702012-09-05 16:10:50 -070048 /**
Tyler Gunn001d9742015-12-18 13:57:02 -080049 * Indicates that the video calling is not available.
50 */
51 public static final int VIDEO_CALLING_DISABLED = 0;
52
53 /**
54 * Indicates that video calling is enabled, regardless of presence status.
55 */
56 public static final int VIDEO_CALLING_ENABLED = 1;
57
58 /**
59 * Indicates that video calling is enabled, but the availability of video call affordances is
60 * determined by the presence status associated with contacts.
61 */
62 public static final int VIDEO_CALLING_PRESENCE = 2;
63
64 /**
Chiao Chengcb5b2702012-09-05 16:10:50 -070065 * Return an Intent for making a phone call. Scheme (e.g. tel, sip) will be determined
66 * automatically.
67 */
Tyler Gunn9e0c14e2015-08-04 13:33:49 -070068 public static Intent getCallWithSubjectIntent(String number,
69 PhoneAccountHandle phoneAccountHandle, String callSubject) {
70
71 final Intent intent = getCallIntent(getCallUri(number));
72 intent.putExtra(TelecomManager.EXTRA_CALL_SUBJECT, callSubject);
73 if (phoneAccountHandle != null) {
74 intent.putExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, phoneAccountHandle);
75 }
76 return intent;
77 }
78
79 /**
80 * Return an Intent for making a phone call. Scheme (e.g. tel, sip) will be determined
81 * automatically.
82 */
Chiao Chengcb5b2702012-09-05 16:10:50 -070083 public static Intent getCallIntent(String number) {
yaolu18417a42017-05-04 12:33:47 -070084 Uri uri = getCallUri(number);
85 return PhoneNumberUtils.isEmergencyNumber(number)
86 ? getCallIntentForEmergencyNumber(uri) : getCallIntent(uri);
87 }
88
89 /**
90 * Return an Intent to directly start Dialer when calling an emergency number. Scheme is always
91 * PhoneAccount.SCHEME_TEL.
92 */
93 private static Intent getCallIntentForEmergencyNumber(Uri uri) {
94 return new Intent(Intent.ACTION_DIAL, uri);
Chiao Chengcb5b2702012-09-05 16:10:50 -070095 }
96
97 /**
98 * Return an Intent for making a phone call. A given Uri will be used as is (without any
99 * sanity check).
100 */
101 public static Intent getCallIntent(Uri uri) {
Brian Attwell066bd682015-02-03 17:10:19 -0800102 return new Intent(Intent.ACTION_CALL, uri);
Chiao Chengcb5b2702012-09-05 16:10:50 -0700103 }
104
105 /**
Brian Attwell066bd682015-02-03 17:10:19 -0800106 * A variant of {@link #getCallIntent} for starting a video call.
Andrew Leec18a7542014-07-08 16:02:30 -0700107 */
Nancy Chenc095aee2014-07-09 10:29:41 -0700108 public static Intent getVideoCallIntent(String number, String callOrigin) {
Brian Attwell066bd682015-02-03 17:10:19 -0800109 final Intent intent = new Intent(Intent.ACTION_CALL, getCallUri(number));
110 intent.putExtra(TelecomManager.EXTRA_START_CALL_WITH_VIDEO_STATE,
Yorke Leecb93d462015-05-12 16:23:06 -0700111 VideoProfile.STATE_BIDIRECTIONAL);
Brian Attwell066bd682015-02-03 17:10:19 -0800112 if (!TextUtils.isEmpty(callOrigin)) {
Chiao Chengcb5b2702012-09-05 16:10:50 -0700113 intent.putExtra(PhoneConstants.EXTRA_CALL_ORIGIN, callOrigin);
114 }
115 return intent;
116 }
117
118 /**
Jay Shraunered1a3b22014-09-05 15:37:27 -0700119 * Return Uri with an appropriate scheme, accepting both SIP and usual phone call
Chiao Chengcb5b2702012-09-05 16:10:50 -0700120 * numbers.
121 */
122 public static Uri getCallUri(String number) {
Yorke Lee9e8e7cb2014-03-17 13:04:45 -0700123 if (PhoneNumberHelper.isUriNumber(number)) {
Jay Shraunered1a3b22014-09-05 15:37:27 -0700124 return Uri.fromParts(PhoneAccount.SCHEME_SIP, number, null);
Chiao Chengcb5b2702012-09-05 16:10:50 -0700125 }
Jay Shraunered1a3b22014-09-05 15:37:27 -0700126 return Uri.fromParts(PhoneAccount.SCHEME_TEL, number, null);
Yorke Lee9028db02015-03-19 17:01:54 +0000127 }
Paul Soulos56046082014-08-18 16:12:40 -0700128
Yorke Lee9028db02015-03-19 17:01:54 +0000129 /**
Tyler Gunn001d9742015-12-18 13:57:02 -0800130 * Determines if video calling is available, and if so whether presence checking is available
131 * as well.
132 *
133 * Returns a bitmask with {@link #VIDEO_CALLING_ENABLED} to indicate that video calling is
134 * available, and {@link #VIDEO_CALLING_PRESENCE} if presence indication is also available.
135 *
136 * @param context The context
137 * @return A bit-mask describing the current video capabilities.
138 */
139 public static int getVideoCallingAvailability(Context context) {
140 if (!PermissionsUtil.hasPermission(context, android.Manifest.permission.READ_PHONE_STATE)
141 || !CompatUtils.isVideoCompatible()) {
142 return VIDEO_CALLING_DISABLED;
143 }
144 TelecomManager telecommMgr = (TelecomManager)
145 context.getSystemService(Context.TELECOM_SERVICE);
146 if (telecommMgr == null) {
147 return VIDEO_CALLING_DISABLED;
148 }
149
Wenyi Wang7a27b852016-11-11 11:36:24 -0800150 try {
151 List<PhoneAccountHandle> accountHandles = telecommMgr.getCallCapablePhoneAccounts();
152 for (PhoneAccountHandle accountHandle : accountHandles) {
153 PhoneAccount account = telecommMgr.getPhoneAccount(accountHandle);
154 if (account != null) {
155 if (account.hasCapabilities(PhoneAccount.CAPABILITY_VIDEO_CALLING)) {
156 // Builds prior to N do not have presence support.
157 if (!CompatUtils.isVideoPresenceCompatible()) {
158 return VIDEO_CALLING_ENABLED;
159 }
Tyler Gunn001d9742015-12-18 13:57:02 -0800160
Wenyi Wang7a27b852016-11-11 11:36:24 -0800161 int videoCapabilities = VIDEO_CALLING_ENABLED;
162 if (account.hasCapabilities(PhoneAccountSdkCompat
163 .CAPABILITY_VIDEO_CALLING_RELIES_ON_PRESENCE)) {
164 videoCapabilities |= VIDEO_CALLING_PRESENCE;
165 }
166 return videoCapabilities;
Tyler Gunn001d9742015-12-18 13:57:02 -0800167 }
Tyler Gunn001d9742015-12-18 13:57:02 -0800168 }
169 }
Wenyi Wang7a27b852016-11-11 11:36:24 -0800170 return VIDEO_CALLING_DISABLED;
171 } catch (SecurityException e) {
172 FeedbackHelper.sendFeedback(context, TAG,
Wenyi Wang969c5952016-12-20 14:31:18 -0800173 "Security exception when getting call capable phone accounts", e);
Wenyi Wang7a27b852016-11-11 11:36:24 -0800174 return VIDEO_CALLING_DISABLED;
Tyler Gunn001d9742015-12-18 13:57:02 -0800175 }
Tyler Gunn001d9742015-12-18 13:57:02 -0800176 }
177
178 /**
Tyler Gunna05f1532015-08-06 15:01:44 -0700179 * Determines if one of the call capable phone accounts defined supports calling with a subject
180 * specified.
181 *
182 * @param context The context.
183 * @return {@code true} if one of the call capable phone accounts supports calling with a
184 * subject specified, {@code false} otherwise.
185 */
186 public static boolean isCallWithSubjectSupported(Context context) {
Nancy Chen5fe647d2015-12-08 16:57:48 -0800187 if (!PermissionsUtil.hasPermission(context, android.Manifest.permission.READ_PHONE_STATE)
188 || !CompatUtils.isCallSubjectCompatible()) {
Yorke Leed2c96922015-09-18 12:59:16 -0700189 return false;
190 }
Tyler Gunna05f1532015-08-06 15:01:44 -0700191 TelecomManager telecommMgr = (TelecomManager)
192 context.getSystemService(Context.TELECOM_SERVICE);
193 if (telecommMgr == null) {
194 return false;
195 }
196
Wenyi Wang969c5952016-12-20 14:31:18 -0800197 try {
198 List<PhoneAccountHandle> accountHandles = telecommMgr.getCallCapablePhoneAccounts();
199 for (PhoneAccountHandle accountHandle : accountHandles) {
200 PhoneAccount account = telecommMgr.getPhoneAccount(accountHandle);
201 if (account != null && account.hasCapabilities(PhoneAccount.CAPABILITY_CALL_SUBJECT)) {
202 return true;
203 }
Tyler Gunna05f1532015-08-06 15:01:44 -0700204 }
Wenyi Wang969c5952016-12-20 14:31:18 -0800205 return false;
206 } catch (SecurityException e) {
207 FeedbackHelper.sendFeedback(context, TAG,
208 "Security exception when getting call capable phone accounts", e);
209 return false;
Tyler Gunna05f1532015-08-06 15:01:44 -0700210 }
Wenyi Wang969c5952016-12-20 14:31:18 -0800211
Tyler Gunna05f1532015-08-06 15:01:44 -0700212 }
Chiao Chengcb5b2702012-09-05 16:10:50 -0700213}