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.dialer.util; 18 19 import android.content.Context; 20 import android.net.Uri; 21 import android.telecom.PhoneAccount; 22 import android.telecom.PhoneAccountHandle; 23 import android.telecom.TelecomManager; 24 import com.android.dialer.common.LogUtil; 25 import com.android.dialer.compat.CompatUtils; 26 import com.android.dialer.phonenumberutil.PhoneNumberHelper; 27 import java.util.List; 28 29 /** Utilities related to calls that can be used by non system apps. */ 30 public class CallUtil { 31 32 /** Indicates that the video calling is not available. */ 33 public static final int VIDEO_CALLING_DISABLED = 0; 34 35 /** Indicates that video calling is enabled, regardless of presence status. */ 36 public static final int VIDEO_CALLING_ENABLED = 1; 37 38 /** 39 * Indicates that video calling is enabled, but the availability of video call affordances is 40 * determined by the presence status associated with contacts. 41 */ 42 public static final int VIDEO_CALLING_PRESENCE = 2; 43 44 private static boolean hasInitializedIsVideoEnabledState; 45 private static boolean cachedIsVideoEnabledState; 46 47 /** Return Uri with an appropriate scheme, accepting both SIP and usual phone call numbers. */ 48 public static Uri getCallUri(String number) { 49 if (PhoneNumberHelper.isUriNumber(number)) { 50 return Uri.fromParts(PhoneAccount.SCHEME_SIP, number, null); 51 } 52 return Uri.fromParts(PhoneAccount.SCHEME_TEL, number, null); 53 } 54 55 /** @return Uri that directly dials a user's voicemail inbox. */ 56 public static Uri getVoicemailUri() { 57 return Uri.fromParts(PhoneAccount.SCHEME_VOICEMAIL, "", null); 58 } 59 60 /** 61 * Determines if video calling is available, and if so whether presence checking is available as 62 * well. 63 * 64 * <p>Returns a bitmask with {@link #VIDEO_CALLING_ENABLED} to indicate that video calling is 65 * available, and {@link #VIDEO_CALLING_PRESENCE} if presence indication is also available. 66 * 67 * @param context The context 68 * @return A bit-mask describing the current video capabilities. 69 */ 70 public static int getVideoCallingAvailability(Context context) { 71 if (!PermissionsUtil.hasPermission(context, android.Manifest.permission.READ_PHONE_STATE) 72 || !CompatUtils.isVideoCompatible()) { 73 return VIDEO_CALLING_DISABLED; 74 } 75 TelecomManager telecommMgr = (TelecomManager) context.getSystemService(Context.TELECOM_SERVICE); 76 if (telecommMgr == null) { 77 return VIDEO_CALLING_DISABLED; 78 } 79 80 List<PhoneAccountHandle> accountHandles = telecommMgr.getCallCapablePhoneAccounts(); 81 for (PhoneAccountHandle accountHandle : accountHandles) { 82 PhoneAccount account = telecommMgr.getPhoneAccount(accountHandle); 83 if (account != null) { 84 if (account.hasCapabilities(PhoneAccount.CAPABILITY_VIDEO_CALLING)) { 85 // Builds prior to N do not have presence support. 86 if (!CompatUtils.isVideoPresenceCompatible()) { 87 return VIDEO_CALLING_ENABLED; 88 } 89 90 int videoCapabilities = VIDEO_CALLING_ENABLED; 91 if (account.hasCapabilities(PhoneAccount.CAPABILITY_VIDEO_CALLING_RELIES_ON_PRESENCE)) { 92 videoCapabilities |= VIDEO_CALLING_PRESENCE; 93 } 94 return videoCapabilities; 95 } 96 } 97 } 98 return VIDEO_CALLING_DISABLED; 99 } 100 101 /** 102 * Determines if one of the call capable phone accounts defined supports video calling. 103 * 104 * @param context The context. 105 * @return {@code true} if one of the call capable phone accounts supports video calling, {@code 106 * false} otherwise. 107 */ 108 public static boolean isVideoEnabled(Context context) { 109 boolean isVideoEnabled = (getVideoCallingAvailability(context) & VIDEO_CALLING_ENABLED) != 0; 110 111 // Log everytime the video enabled state changes. 112 if (!hasInitializedIsVideoEnabledState) { 113 LogUtil.i("CallUtil.isVideoEnabled", "isVideoEnabled: " + isVideoEnabled); 114 hasInitializedIsVideoEnabledState = true; 115 cachedIsVideoEnabledState = isVideoEnabled; 116 } else if (cachedIsVideoEnabledState != isVideoEnabled) { 117 LogUtil.i( 118 "CallUtil.isVideoEnabled", 119 "isVideoEnabled changed from %b to %b", 120 cachedIsVideoEnabledState, 121 isVideoEnabled); 122 cachedIsVideoEnabledState = isVideoEnabled; 123 } 124 125 return isVideoEnabled; 126 } 127 128 /** 129 * Determines if one of the call capable phone accounts defined supports calling with a subject 130 * specified. 131 * 132 * @param context The context. 133 * @return {@code true} if one of the call capable phone accounts supports calling with a subject 134 * specified, {@code false} otherwise. 135 */ 136 public static boolean isCallWithSubjectSupported(Context context) { 137 if (!PermissionsUtil.hasPermission(context, android.Manifest.permission.READ_PHONE_STATE) 138 || !CompatUtils.isCallSubjectCompatible()) { 139 return false; 140 } 141 TelecomManager telecommMgr = (TelecomManager) context.getSystemService(Context.TELECOM_SERVICE); 142 if (telecommMgr == null) { 143 return false; 144 } 145 146 List<PhoneAccountHandle> accountHandles = telecommMgr.getCallCapablePhoneAccounts(); 147 for (PhoneAccountHandle accountHandle : accountHandles) { 148 PhoneAccount account = telecommMgr.getPhoneAccount(accountHandle); 149 if (account != null && account.hasCapabilities(PhoneAccount.CAPABILITY_CALL_SUBJECT)) { 150 return true; 151 } 152 } 153 return false; 154 } 155 } 156