1 /*
2  * Copyright (C) 2012 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 android.content.Context;
20 import android.content.Intent;
21 import android.net.Uri;
22 import android.telecom.PhoneAccount;
23 import android.telecom.PhoneAccountHandle;
24 import android.telecom.TelecomManager;
25 import android.telecom.VideoProfile;
26 
27 import com.android.contacts.common.util.PhoneNumberHelper;
28 import com.android.phone.common.PhoneConstants;
29 
30 /**
31  * Utilities related to calls.
32  */
33 public class CallUtil {
34 
35     /**
36      * Return an Intent for making a phone call. Scheme (e.g. tel, sip) will be determined
37      * automatically.
38      */
getCallIntent(String number)39     public static Intent getCallIntent(String number) {
40         return getCallIntent(number, null, null);
41     }
42 
43     /**
44      * Return an Intent for making a phone call. A given Uri will be used as is (without any
45      * sanity check).
46      */
getCallIntent(Uri uri)47     public static Intent getCallIntent(Uri uri) {
48         return getCallIntent(uri, null, null);
49     }
50 
51     /**
52      * A variant of {@link #getCallIntent(String)} but also accept a call origin.
53      * For more information about call origin, see comments in Phone package (PhoneApp).
54      */
getCallIntent(String number, String callOrigin)55     public static Intent getCallIntent(String number, String callOrigin) {
56         return getCallIntent(getCallUri(number), callOrigin, null);
57     }
58 
59     /**
60      * A variant of {@link #getCallIntent(String)} but also include {@code Account}.
61      */
getCallIntent(String number, PhoneAccountHandle accountHandle)62     public static Intent getCallIntent(String number, PhoneAccountHandle accountHandle) {
63         return getCallIntent(number, null, accountHandle);
64     }
65 
66     /**
67      * A variant of {@link #getCallIntent(android.net.Uri)} but also include {@code Account}.
68      */
getCallIntent(Uri uri, PhoneAccountHandle accountHandle)69     public static Intent getCallIntent(Uri uri, PhoneAccountHandle accountHandle) {
70         return getCallIntent(uri, null, accountHandle);
71     }
72 
73     /**
74      * A variant of {@link #getCallIntent(String, String)} but also include {@code Account}.
75      */
getCallIntent( String number, String callOrigin, PhoneAccountHandle accountHandle)76     public static Intent getCallIntent(
77             String number, String callOrigin, PhoneAccountHandle accountHandle) {
78         return getCallIntent(getCallUri(number), callOrigin, accountHandle);
79     }
80 
81     /**
82      * A variant of {@link #getCallIntent(android.net.Uri)} but also accept a call
83      * origin and {@code Account}.
84      * For more information about call origin, see comments in Phone package (PhoneApp).
85      */
getCallIntent( Uri uri, String callOrigin, PhoneAccountHandle accountHandle)86     public static Intent getCallIntent(
87             Uri uri, String callOrigin, PhoneAccountHandle accountHandle) {
88         return getCallIntent(uri, callOrigin, accountHandle,
89                 VideoProfile.VideoState.AUDIO_ONLY);
90     }
91 
92     /**
93      * A variant of {@link #getCallIntent(String, String)} for starting a video call.
94      */
getVideoCallIntent(String number, String callOrigin)95     public static Intent getVideoCallIntent(String number, String callOrigin) {
96         return getCallIntent(getCallUri(number), callOrigin, null,
97                 VideoProfile.VideoState.BIDIRECTIONAL);
98     }
99 
100     /**
101      * A variant of {@link #getCallIntent(String, String, android.telecom.PhoneAccountHandle)} for
102      * starting a video call.
103      */
getVideoCallIntent( String number, String callOrigin, PhoneAccountHandle accountHandle)104     public static Intent getVideoCallIntent(
105             String number, String callOrigin, PhoneAccountHandle accountHandle) {
106         return getCallIntent(getCallUri(number), callOrigin, accountHandle,
107                 VideoProfile.VideoState.BIDIRECTIONAL);
108     }
109 
110     /**
111      * A variant of {@link #getCallIntent(String, String, android.telecom.PhoneAccountHandle)} for
112      * starting a video call.
113      */
getVideoCallIntent(String number, PhoneAccountHandle accountHandle)114     public static Intent getVideoCallIntent(String number, PhoneAccountHandle accountHandle) {
115         return getVideoCallIntent(number, null, accountHandle);
116     }
117 
118     /**
119      * A variant of {@link #getCallIntent(android.net.Uri)} for calling Voicemail.
120      */
getVoicemailIntent()121     public static Intent getVoicemailIntent() {
122         return getCallIntent(Uri.fromParts(PhoneAccount.SCHEME_VOICEMAIL, "", null));
123     }
124 
125     /**
126      * A variant of {@link #getCallIntent(android.net.Uri)} but also accept a call
127      * origin and {@code Account} and {@code VideoCallProfile} state.
128      * For more information about call origin, see comments in Phone package (PhoneApp).
129      */
getCallIntent( Uri uri, String callOrigin, PhoneAccountHandle accountHandle, int videoState)130     public static Intent getCallIntent(
131             Uri uri, String callOrigin, PhoneAccountHandle accountHandle, int videoState) {
132         final Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, uri);
133         intent.putExtra(TelecomManager.EXTRA_START_CALL_WITH_VIDEO_STATE, videoState);
134         if (callOrigin != null) {
135             intent.putExtra(PhoneConstants.EXTRA_CALL_ORIGIN, callOrigin);
136         }
137         if (accountHandle != null) {
138             intent.putExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, accountHandle);
139         }
140 
141         return intent;
142     }
143 
144     /**
145      * Return Uri with an appropriate scheme, accepting both SIP and usual phone call
146      * numbers.
147      */
getCallUri(String number)148     public static Uri getCallUri(String number) {
149         if (PhoneNumberHelper.isUriNumber(number)) {
150              return Uri.fromParts(PhoneAccount.SCHEME_SIP, number, null);
151         }
152         return Uri.fromParts(PhoneAccount.SCHEME_TEL, number, null);
153      }
154 
isVideoEnabled(Context context)155     public static boolean isVideoEnabled(Context context) {
156         TelecomManager telecommMgr = (TelecomManager)
157                 context.getSystemService(Context.TELECOM_SERVICE);
158         if (telecommMgr == null) {
159             return false;
160         }
161 
162         // TODO: Check telecommManager for value instead.
163         // return telecommMgr.isVideoEnabled();
164         return false;
165     }
166 }
167