Home | History | Annotate | Download | only in common
      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      */
     45     public static Intent getCallIntent(String number) {
     46         return getCallIntent(getCallUri(number));
     47     }
     48 
     49     /**
     50      * Return an Intent for making a phone call. A given Uri will be used as is (without any
     51      * sanity check).
     52      */
     53     public static Intent getCallIntent(Uri uri) {
     54         return new Intent(Intent.ACTION_CALL, uri);
     55     }
     56 
     57     /**
     58      * A variant of {@link #getCallIntent} for starting a video call.
     59      */
     60     public static Intent getVideoCallIntent(String number, String callOrigin) {
     61         final Intent intent = new Intent(Intent.ACTION_CALL, getCallUri(number));
     62         intent.putExtra(TelecomManager.EXTRA_START_CALL_WITH_VIDEO_STATE,
     63                 VideoProfile.STATE_BIDIRECTIONAL);
     64         if (!TextUtils.isEmpty(callOrigin)) {
     65             intent.putExtra(PhoneConstants.EXTRA_CALL_ORIGIN, callOrigin);
     66         }
     67         return intent;
     68     }
     69 
     70     /**
     71      * Return Uri with an appropriate scheme, accepting both SIP and usual phone call
     72      * numbers.
     73      */
     74     public static Uri getCallUri(String number) {
     75         if (PhoneNumberHelper.isUriNumber(number)) {
     76              return Uri.fromParts(PhoneAccount.SCHEME_SIP, number, null);
     77         }
     78         return Uri.fromParts(PhoneAccount.SCHEME_TEL, number, null);
     79     }
     80 
     81     /**
     82      * Determines if one of the call capable phone accounts defined supports video calling.
     83      *
     84      * @param context The context.
     85      * @return {@code true} if one of the call capable phone accounts supports video calling,
     86      *      {@code false} otherwise.
     87      */
     88     public static boolean isVideoEnabled(Context context) {
     89         TelecomManager telecommMgr = (TelecomManager)
     90                 context.getSystemService(Context.TELECOM_SERVICE);
     91         if (telecommMgr == null) {
     92             return false;
     93         }
     94 
     95         List<PhoneAccountHandle> accountHandles = telecommMgr.getCallCapablePhoneAccounts();
     96         for (PhoneAccountHandle accountHandle : accountHandles) {
     97             PhoneAccount account = telecommMgr.getPhoneAccount(accountHandle);
     98             if (account != null && account.hasCapabilities(PhoneAccount.CAPABILITY_VIDEO_CALLING)) {
     99                 return true;
    100             }
    101         }
    102         return false;
    103     }
    104 }
    105