1 /*
2  * Copyright (C) 2016 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.callintent;
18 
19 import android.content.Intent;
20 import android.net.Uri;
21 import android.os.Bundle;
22 import android.os.SystemClock;
23 import android.support.annotation.NonNull;
24 import android.support.annotation.Nullable;
25 import android.telecom.PhoneAccountHandle;
26 import android.telecom.TelecomManager;
27 import android.telecom.VideoProfile;
28 import android.text.TextUtils;
29 import com.android.dialer.common.Assert;
30 import com.android.dialer.util.CallUtil;
31 
32 /** Creates an intent to start a new outgoing call. */
33 public class CallIntentBuilder {
34   private final Uri uri;
35   private final CallSpecificAppData callSpecificAppData;
36   @Nullable private PhoneAccountHandle phoneAccountHandle;
37   private boolean isVideoCall;
38   private String callSubject;
39 
CallIntentBuilder(@onNull Uri uri, @NonNull CallSpecificAppData callSpecificAppData)40   public CallIntentBuilder(@NonNull Uri uri, @NonNull CallSpecificAppData callSpecificAppData) {
41     this.uri = Assert.isNotNull(uri);
42     this.callSpecificAppData = Assert.isNotNull(callSpecificAppData);
43     Assert.checkArgument(
44         callSpecificAppData.getCallInitiationType() != CallInitiationType.Type.UNKNOWN_INITIATION);
45   }
46 
CallIntentBuilder(@onNull Uri uri, CallInitiationType.Type callInitiationType)47   public CallIntentBuilder(@NonNull Uri uri, CallInitiationType.Type callInitiationType) {
48     this(uri, createCallSpecificAppData(callInitiationType));
49   }
50 
CallIntentBuilder( @onNull String number, @NonNull CallSpecificAppData callSpecificAppData)51   public CallIntentBuilder(
52       @NonNull String number, @NonNull CallSpecificAppData callSpecificAppData) {
53     this(CallUtil.getCallUri(Assert.isNotNull(number)), callSpecificAppData);
54   }
55 
CallIntentBuilder(@onNull String number, CallInitiationType.Type callInitiationType)56   public CallIntentBuilder(@NonNull String number, CallInitiationType.Type callInitiationType) {
57     this(CallUtil.getCallUri(Assert.isNotNull(number)), callInitiationType);
58   }
59 
getCallSpecificAppData()60   public CallSpecificAppData getCallSpecificAppData() {
61     return callSpecificAppData;
62   }
63 
setPhoneAccountHandle(@ullable PhoneAccountHandle accountHandle)64   public CallIntentBuilder setPhoneAccountHandle(@Nullable PhoneAccountHandle accountHandle) {
65     this.phoneAccountHandle = accountHandle;
66     return this;
67   }
68 
setIsVideoCall(boolean isVideoCall)69   public CallIntentBuilder setIsVideoCall(boolean isVideoCall) {
70     this.isVideoCall = isVideoCall;
71     return this;
72   }
73 
setCallSubject(String callSubject)74   public CallIntentBuilder setCallSubject(String callSubject) {
75     this.callSubject = callSubject;
76     return this;
77   }
78 
build()79   public Intent build() {
80     Intent intent = new Intent(Intent.ACTION_CALL, uri);
81     intent.putExtra(
82         TelecomManager.EXTRA_START_CALL_WITH_VIDEO_STATE,
83         isVideoCall ? VideoProfile.STATE_BIDIRECTIONAL : VideoProfile.STATE_AUDIO_ONLY);
84 
85     Bundle extras = new Bundle();
86     extras.putLong(Constants.EXTRA_CALL_CREATED_TIME_MILLIS, SystemClock.elapsedRealtime());
87     CallIntentParser.putCallSpecificAppData(extras, callSpecificAppData);
88     intent.putExtra(TelecomManager.EXTRA_OUTGOING_CALL_EXTRAS, extras);
89 
90     if (phoneAccountHandle != null) {
91       intent.putExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, phoneAccountHandle);
92     }
93 
94     if (!TextUtils.isEmpty(callSubject)) {
95       intent.putExtra(TelecomManager.EXTRA_CALL_SUBJECT, callSubject);
96     }
97 
98     return intent;
99   }
100 
createCallSpecificAppData( CallInitiationType.Type callInitiationType)101   private static @NonNull CallSpecificAppData createCallSpecificAppData(
102       CallInitiationType.Type callInitiationType) {
103     CallSpecificAppData callSpecificAppData =
104         CallSpecificAppData.newBuilder().setCallInitiationType(callInitiationType).build();
105     return callSpecificAppData;
106   }
107 }
108