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