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.annotation.SuppressLint;
20 import android.content.Context;
21 import android.net.Uri;
22 import android.telecom.PhoneAccount;
23 import android.telecom.PhoneAccountHandle;
24 import android.telecom.TelecomManager;
25 import com.android.dialer.common.LogUtil;
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. */
getCallUri(String number)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   /**
56    * Determines if video calling is available, and if so whether presence checking is available as
57    * well.
58    *
59    * <p>Returns a bitmask with {@link #VIDEO_CALLING_ENABLED} to indicate that video calling is
60    * available, and {@link #VIDEO_CALLING_PRESENCE} if presence indication is also available.
61    *
62    * @param context The context
63    * @return A bit-mask describing the current video capabilities.
64    */
65   @SuppressLint("MissingPermission")
getVideoCallingAvailability(Context context)66   public static int getVideoCallingAvailability(Context context) {
67     if (!PermissionsUtil.hasPermission(context, android.Manifest.permission.READ_PHONE_STATE)) {
68       return VIDEO_CALLING_DISABLED;
69     }
70     TelecomManager telecommMgr = (TelecomManager) context.getSystemService(Context.TELECOM_SERVICE);
71     if (telecommMgr == null) {
72       return VIDEO_CALLING_DISABLED;
73     }
74 
75     List<PhoneAccountHandle> accountHandles = telecommMgr.getCallCapablePhoneAccounts();
76     for (PhoneAccountHandle accountHandle : accountHandles) {
77       PhoneAccount account = telecommMgr.getPhoneAccount(accountHandle);
78       if (account != null) {
79         if (account.hasCapabilities(PhoneAccount.CAPABILITY_VIDEO_CALLING)) {
80           int videoCapabilities = VIDEO_CALLING_ENABLED;
81           if (account.hasCapabilities(PhoneAccount.CAPABILITY_VIDEO_CALLING_RELIES_ON_PRESENCE)) {
82             videoCapabilities |= VIDEO_CALLING_PRESENCE;
83           }
84           return videoCapabilities;
85         }
86       }
87     }
88     return VIDEO_CALLING_DISABLED;
89   }
90 
91   /**
92    * Determines if one of the call capable phone accounts defined supports video calling.
93    *
94    * @param context The context.
95    * @return {@code true} if one of the call capable phone accounts supports video calling, {@code
96    *     false} otherwise.
97    */
isVideoEnabled(Context context)98   public static boolean isVideoEnabled(Context context) {
99     boolean isVideoEnabled = (getVideoCallingAvailability(context) & VIDEO_CALLING_ENABLED) != 0;
100 
101     // Log everytime the video enabled state changes.
102     if (!hasInitializedIsVideoEnabledState) {
103       LogUtil.i("CallUtil.isVideoEnabled", "isVideoEnabled: " + isVideoEnabled);
104       hasInitializedIsVideoEnabledState = true;
105       cachedIsVideoEnabledState = isVideoEnabled;
106     } else if (cachedIsVideoEnabledState != isVideoEnabled) {
107       LogUtil.i(
108           "CallUtil.isVideoEnabled",
109           "isVideoEnabled changed from %b to %b",
110           cachedIsVideoEnabledState,
111           isVideoEnabled);
112       cachedIsVideoEnabledState = isVideoEnabled;
113     }
114 
115     return isVideoEnabled;
116   }
117 
118   /**
119    * Determines if one of the call capable phone accounts defined supports calling with a subject
120    * specified.
121    *
122    * @param context The context.
123    * @return {@code true} if one of the call capable phone accounts supports calling with a subject
124    *     specified, {@code false} otherwise.
125    */
126   @SuppressLint("MissingPermission")
isCallWithSubjectSupported(Context context)127   public static boolean isCallWithSubjectSupported(Context context) {
128     if (!PermissionsUtil.hasPermission(context, android.Manifest.permission.READ_PHONE_STATE)) {
129       return false;
130     }
131     TelecomManager telecommMgr = (TelecomManager) context.getSystemService(Context.TELECOM_SERVICE);
132     if (telecommMgr == null) {
133       return false;
134     }
135 
136     List<PhoneAccountHandle> accountHandles = telecommMgr.getCallCapablePhoneAccounts();
137     for (PhoneAccountHandle accountHandle : accountHandles) {
138       PhoneAccount account = telecommMgr.getPhoneAccount(accountHandle);
139       if (account != null && account.hasCapabilities(PhoneAccount.CAPABILITY_CALL_SUBJECT)) {
140         return true;
141       }
142     }
143     return false;
144   }
145 }
146