Home | History | Annotate | Download | only in util
      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   /**
     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   public static int getVideoCallingAvailability(Context context) {
     66     if (!PermissionsUtil.hasPermission(context, android.Manifest.permission.READ_PHONE_STATE)
     67         || !CompatUtils.isVideoCompatible()) {
     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           // Builds prior to N do not have presence support.
     81           if (!CompatUtils.isVideoPresenceCompatible()) {
     82             return VIDEO_CALLING_ENABLED;
     83           }
     84 
     85           int videoCapabilities = VIDEO_CALLING_ENABLED;
     86           if (account.hasCapabilities(PhoneAccount.CAPABILITY_VIDEO_CALLING_RELIES_ON_PRESENCE)) {
     87             videoCapabilities |= VIDEO_CALLING_PRESENCE;
     88           }
     89           return videoCapabilities;
     90         }
     91       }
     92     }
     93     return VIDEO_CALLING_DISABLED;
     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, {@code
    101    *     false} otherwise.
    102    */
    103   public static boolean isVideoEnabled(Context context) {
    104     boolean isVideoEnabled = (getVideoCallingAvailability(context) & VIDEO_CALLING_ENABLED) != 0;
    105 
    106     // Log everytime the video enabled state changes.
    107     if (!hasInitializedIsVideoEnabledState) {
    108       LogUtil.i("CallUtil.isVideoEnabled", "isVideoEnabled: " + isVideoEnabled);
    109       hasInitializedIsVideoEnabledState = true;
    110       cachedIsVideoEnabledState = isVideoEnabled;
    111     } else if (cachedIsVideoEnabledState != isVideoEnabled) {
    112       LogUtil.i(
    113           "CallUtil.isVideoEnabled",
    114           "isVideoEnabled changed from %b to %b",
    115           cachedIsVideoEnabledState,
    116           isVideoEnabled);
    117       cachedIsVideoEnabledState = isVideoEnabled;
    118     }
    119 
    120     return isVideoEnabled;
    121   }
    122 
    123   /**
    124    * Determines if one of the call capable phone accounts defined supports calling with a subject
    125    * specified.
    126    *
    127    * @param context The context.
    128    * @return {@code true} if one of the call capable phone accounts supports calling with a subject
    129    *     specified, {@code false} otherwise.
    130    */
    131   public static boolean isCallWithSubjectSupported(Context context) {
    132     if (!PermissionsUtil.hasPermission(context, android.Manifest.permission.READ_PHONE_STATE)
    133         || !CompatUtils.isCallSubjectCompatible()) {
    134       return false;
    135     }
    136     TelecomManager telecommMgr = (TelecomManager) context.getSystemService(Context.TELECOM_SERVICE);
    137     if (telecommMgr == null) {
    138       return false;
    139     }
    140 
    141     List<PhoneAccountHandle> accountHandles = telecommMgr.getCallCapablePhoneAccounts();
    142     for (PhoneAccountHandle accountHandle : accountHandles) {
    143       PhoneAccount account = telecommMgr.getPhoneAccount(accountHandle);
    144       if (account != null && account.hasCapabilities(PhoneAccount.CAPABILITY_CALL_SUBJECT)) {
    145         return true;
    146       }
    147     }
    148     return false;
    149   }
    150 }
    151