blob: acfad5249eaa9a9a432635c8d9bbdb2c2e9c54fd [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
17package com.android.contacts.common;
18
Nancy Chen5fe647d2015-12-08 16:57:48 -080019import com.android.contacts.common.compat.CompatUtils;
Ta-wei Yen59023c22016-01-08 15:46:27 -080020import com.android.contacts.common.compat.PhoneAccountSdkCompat;
Yorke Leed2c96922015-09-18 12:59:16 -070021import com.android.contacts.common.util.PermissionsUtil;
Brian Attwell066bd682015-02-03 17:10:19 -080022import com.android.contacts.common.util.PhoneNumberHelper;
Wenyi Wang7a27b852016-11-11 11:36:24 -080023import com.android.contactsbind.FeedbackHelper;
Brian Attwell066bd682015-02-03 17:10:19 -080024import com.android.phone.common.PhoneConstants;
25
Andrew Lee7e558dc2014-08-20 16:02:11 -070026import android.content.Context;
Chiao Chengcb5b2702012-09-05 16:10:50 -070027import android.content.Intent;
28import android.net.Uri;
Tyler Gunn5cf7f012014-09-10 15:20:21 -070029import android.telecom.PhoneAccount;
Vinit Deshpandeca6b31e2015-03-15 13:23:01 -070030import android.telecom.PhoneAccountHandle;
Tyler Gunn5cf7f012014-09-10 15:20:21 -070031import android.telecom.TelecomManager;
32import android.telecom.VideoProfile;
Brian Attwell066bd682015-02-03 17:10:19 -080033import android.text.TextUtils;
Chiao Chengcb5b2702012-09-05 16:10:50 -070034
Yorke Lee9028db02015-03-19 17:01:54 +000035import java.util.List;
36
Chiao Chengcb5b2702012-09-05 16:10:50 -070037/**
Brian Attwell066bd682015-02-03 17:10:19 -080038 * 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 Chengcb5b2702012-09-05 16:10:50 -070042 */
43public class CallUtil {
44
Wenyi Wang7a27b852016-11-11 11:36:24 -080045 public static final String TAG = "CallUtil";
46
Chiao Chengcb5b2702012-09-05 16:10:50 -070047 /**
Tyler Gunn001d9742015-12-18 13:57:02 -080048 * 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 Chengcb5b2702012-09-05 16:10:50 -070064 * Return an Intent for making a phone call. Scheme (e.g. tel, sip) will be determined
65 * automatically.
66 */
Tyler Gunn9e0c14e2015-08-04 13:33:49 -070067 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 Chengcb5b2702012-09-05 16:10:50 -070082 public static Intent getCallIntent(String number) {
Brian Attwell066bd682015-02-03 17:10:19 -080083 return getCallIntent(getCallUri(number));
Chiao Chengcb5b2702012-09-05 16:10:50 -070084 }
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 Attwell066bd682015-02-03 17:10:19 -080091 return new Intent(Intent.ACTION_CALL, uri);
Chiao Chengcb5b2702012-09-05 16:10:50 -070092 }
93
94 /**
Brian Attwell066bd682015-02-03 17:10:19 -080095 * A variant of {@link #getCallIntent} for starting a video call.
Andrew Leec18a7542014-07-08 16:02:30 -070096 */
Nancy Chenc095aee2014-07-09 10:29:41 -070097 public static Intent getVideoCallIntent(String number, String callOrigin) {
Brian Attwell066bd682015-02-03 17:10:19 -080098 final Intent intent = new Intent(Intent.ACTION_CALL, getCallUri(number));
99 intent.putExtra(TelecomManager.EXTRA_START_CALL_WITH_VIDEO_STATE,
Yorke Leecb93d462015-05-12 16:23:06 -0700100 VideoProfile.STATE_BIDIRECTIONAL);
Brian Attwell066bd682015-02-03 17:10:19 -0800101 if (!TextUtils.isEmpty(callOrigin)) {
Chiao Chengcb5b2702012-09-05 16:10:50 -0700102 intent.putExtra(PhoneConstants.EXTRA_CALL_ORIGIN, callOrigin);
103 }
104 return intent;
105 }
106
107 /**
Jay Shraunered1a3b22014-09-05 15:37:27 -0700108 * Return Uri with an appropriate scheme, accepting both SIP and usual phone call
Chiao Chengcb5b2702012-09-05 16:10:50 -0700109 * numbers.
110 */
111 public static Uri getCallUri(String number) {
Yorke Lee9e8e7cb2014-03-17 13:04:45 -0700112 if (PhoneNumberHelper.isUriNumber(number)) {
Jay Shraunered1a3b22014-09-05 15:37:27 -0700113 return Uri.fromParts(PhoneAccount.SCHEME_SIP, number, null);
Chiao Chengcb5b2702012-09-05 16:10:50 -0700114 }
Jay Shraunered1a3b22014-09-05 15:37:27 -0700115 return Uri.fromParts(PhoneAccount.SCHEME_TEL, number, null);
Yorke Lee9028db02015-03-19 17:01:54 +0000116 }
Paul Soulos56046082014-08-18 16:12:40 -0700117
Yorke Lee9028db02015-03-19 17:01:54 +0000118 /**
Yorke Leef0d79382015-09-14 16:29:05 -0700119 * @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 Gunn001d9742015-12-18 13:57:02 -0800126 * 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 Wang7a27b852016-11-11 11:36:24 -0800146 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 Gunn001d9742015-12-18 13:57:02 -0800156
Wenyi Wang7a27b852016-11-11 11:36:24 -0800157 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 Gunn001d9742015-12-18 13:57:02 -0800163 }
Tyler Gunn001d9742015-12-18 13:57:02 -0800164 }
165 }
Wenyi Wang7a27b852016-11-11 11:36:24 -0800166 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 Gunn001d9742015-12-18 13:57:02 -0800171 }
Tyler Gunn001d9742015-12-18 13:57:02 -0800172 }
173
174 /**
Yorke Lee9028db02015-03-19 17:01:54 +0000175 * 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 Lee7e558dc2014-08-20 16:02:11 -0700181 public static boolean isVideoEnabled(Context context) {
Tyler Gunn001d9742015-12-18 13:57:02 -0800182 return (getVideoCallingAvailability(context) & VIDEO_CALLING_ENABLED) != 0;
Paul Soulos56046082014-08-18 16:12:40 -0700183 }
Tyler Gunna05f1532015-08-06 15:01:44 -0700184
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 Chen5fe647d2015-12-08 16:57:48 -0800194 if (!PermissionsUtil.hasPermission(context, android.Manifest.permission.READ_PHONE_STATE)
195 || !CompatUtils.isCallSubjectCompatible()) {
Yorke Leed2c96922015-09-18 12:59:16 -0700196 return false;
197 }
Tyler Gunna05f1532015-08-06 15:01:44 -0700198 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 Chengcb5b2702012-09-05 16:10:50 -0700213}