1 /* 2 * Copyright (C) 2015 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.common; 18 19 import com.android.contacts.common.util.PhoneNumberHelper; 20 import com.android.phone.common.PhoneConstants; 21 22 import android.content.Context; 23 import android.content.Intent; 24 import android.net.Uri; 25 import android.telecom.PhoneAccount; 26 import android.telecom.PhoneAccountHandle; 27 import android.telecom.TelecomManager; 28 import android.telecom.VideoProfile; 29 import android.text.TextUtils; 30 31 import java.util.List; 32 33 /** 34 * Utilities related to calls that can be used by non system apps. These 35 * use {@link Intent#ACTION_CALL} instead of ACTION_CALL_PRIVILEGED. 36 * 37 * The privileged version of this util exists inside Dialer. 38 */ 39 public class CallUtil { 40 41 /** 42 * Return an Intent for making a phone call. Scheme (e.g. tel, sip) will be determined 43 * automatically. 44 */ getCallWithSubjectIntent(String number, PhoneAccountHandle phoneAccountHandle, String callSubject)45 public static Intent getCallWithSubjectIntent(String number, 46 PhoneAccountHandle phoneAccountHandle, String callSubject) { 47 48 final Intent intent = getCallIntent(getCallUri(number)); 49 intent.putExtra(TelecomManager.EXTRA_CALL_SUBJECT, callSubject); 50 if (phoneAccountHandle != null) { 51 intent.putExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, phoneAccountHandle); 52 } 53 return intent; 54 } 55 56 /** 57 * Return an Intent for making a phone call. Scheme (e.g. tel, sip) will be determined 58 * automatically. 59 */ getCallIntent(String number)60 public static Intent getCallIntent(String number) { 61 return getCallIntent(getCallUri(number)); 62 } 63 64 /** 65 * Return an Intent for making a phone call. A given Uri will be used as is (without any 66 * sanity check). 67 */ getCallIntent(Uri uri)68 public static Intent getCallIntent(Uri uri) { 69 return new Intent(Intent.ACTION_CALL, uri); 70 } 71 72 /** 73 * A variant of {@link #getCallIntent} for starting a video call. 74 */ getVideoCallIntent(String number, String callOrigin)75 public static Intent getVideoCallIntent(String number, String callOrigin) { 76 final Intent intent = new Intent(Intent.ACTION_CALL, getCallUri(number)); 77 intent.putExtra(TelecomManager.EXTRA_START_CALL_WITH_VIDEO_STATE, 78 VideoProfile.STATE_BIDIRECTIONAL); 79 if (!TextUtils.isEmpty(callOrigin)) { 80 intent.putExtra(PhoneConstants.EXTRA_CALL_ORIGIN, callOrigin); 81 } 82 return intent; 83 } 84 85 /** 86 * Return Uri with an appropriate scheme, accepting both SIP and usual phone call 87 * numbers. 88 */ getCallUri(String number)89 public static Uri getCallUri(String number) { 90 if (PhoneNumberHelper.isUriNumber(number)) { 91 return Uri.fromParts(PhoneAccount.SCHEME_SIP, number, null); 92 } 93 return Uri.fromParts(PhoneAccount.SCHEME_TEL, number, null); 94 } 95 96 /** 97 * Determines if one of the call capable phone accounts defined supports video calling. 98 * 99 * @param context The context. 100 * @return {@code true} if one of the call capable phone accounts supports video calling, 101 * {@code false} otherwise. 102 */ isVideoEnabled(Context context)103 public static boolean isVideoEnabled(Context context) { 104 TelecomManager telecommMgr = (TelecomManager) 105 context.getSystemService(Context.TELECOM_SERVICE); 106 if (telecommMgr == null) { 107 return false; 108 } 109 110 List<PhoneAccountHandle> accountHandles = telecommMgr.getCallCapablePhoneAccounts(); 111 for (PhoneAccountHandle accountHandle : accountHandles) { 112 PhoneAccount account = telecommMgr.getPhoneAccount(accountHandle); 113 if (account != null && account.hasCapabilities(PhoneAccount.CAPABILITY_VIDEO_CALLING)) { 114 return true; 115 } 116 } 117 return false; 118 } 119 120 /** 121 * Determines if one of the call capable phone accounts defined supports calling with a subject 122 * specified. 123 * 124 * @param context The context. 125 * @return {@code true} if one of the call capable phone accounts supports calling with a 126 * subject specified, {@code false} otherwise. 127 */ isCallWithSubjectSupported(Context context)128 public static boolean isCallWithSubjectSupported(Context context) { 129 TelecomManager telecommMgr = (TelecomManager) 130 context.getSystemService(Context.TELECOM_SERVICE); 131 if (telecommMgr == null) { 132 return false; 133 } 134 135 List<PhoneAccountHandle> accountHandles = telecommMgr.getCallCapablePhoneAccounts(); 136 for (PhoneAccountHandle accountHandle : accountHandles) { 137 PhoneAccount account = telecommMgr.getPhoneAccount(accountHandle); 138 if (account != null && account.hasCapabilities(PhoneAccount.CAPABILITY_CALL_SUBJECT)) { 139 return true; 140 } 141 } 142 return false; 143 } 144 } 145