blob: 91421ea1bc96c2ea3e846d66b32058583aaae037 [file] [log] [blame]
Eric Erfanianccca3152017-02-22 16:32:36 -08001/*
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
17package com.android.incallui;
18
Sudheer Shankaf1e05872023-03-15 16:00:45 -070019import android.app.BroadcastOptions;
Eric Erfanianccca3152017-02-22 16:32:36 -080020import android.content.BroadcastReceiver;
21import android.content.Context;
22import android.content.Intent;
23import android.os.Build.VERSION_CODES;
Sudheer Shankaf1e05872023-03-15 16:00:45 -070024import android.os.Bundle;
erfanian270663c2018-05-09 13:38:18 -070025import android.support.annotation.NonNull;
Eric Erfanianccca3152017-02-22 16:32:36 -080026import android.support.annotation.RequiresApi;
yuegb26c1ae2017-09-18 16:59:16 -070027import android.telecom.CallAudioState;
Eric Erfanianccca3152017-02-22 16:32:36 -080028import android.telecom.VideoProfile;
Sudheer Shankaf1e05872023-03-15 16:00:45 -070029
Eric Erfanianccca3152017-02-22 16:32:36 -080030import com.android.dialer.common.LogUtil;
erfanian270663c2018-05-09 13:38:18 -070031import com.android.dialer.common.concurrent.DialerExecutorComponent;
Eric Erfanian8369df02017-05-03 10:27:13 -070032import com.android.dialer.logging.DialerImpression;
Eric Erfanianccca3152017-02-22 16:32:36 -080033import com.android.dialer.logging.Logger;
Eric Erfanianccca3152017-02-22 16:32:36 -080034import com.android.incallui.call.CallList;
35import com.android.incallui.call.DialerCall;
yuegb26c1ae2017-09-18 16:59:16 -070036import com.android.incallui.call.TelecomAdapter;
erfanian270663c2018-05-09 13:38:18 -070037import com.android.incallui.speakeasy.SpeakEasyCallManager;
Sudheer Shankaf1e05872023-03-15 16:00:45 -070038
erfanian270663c2018-05-09 13:38:18 -070039import com.google.common.util.concurrent.FutureCallback;
40import com.google.common.util.concurrent.Futures;
41import com.google.common.util.concurrent.ListenableFuture;
Eric Erfanianccca3152017-02-22 16:32:36 -080042
43/**
44 * Accepts broadcast Intents which will be prepared by {@link StatusBarNotifier} and thus sent from
45 * the notification manager. This should be visible from outside, but shouldn't be exported.
46 */
47public class NotificationBroadcastReceiver extends BroadcastReceiver {
48
49 /**
50 * Intent Action used for hanging up the current call from Notification bar. This will choose
51 * first ringing call, first active call, or first background call (typically in STATE_HOLDING
52 * state).
53 */
54 public static final String ACTION_DECLINE_INCOMING_CALL =
55 "com.android.incallui.ACTION_DECLINE_INCOMING_CALL";
56
57 public static final String ACTION_HANG_UP_ONGOING_CALL =
58 "com.android.incallui.ACTION_HANG_UP_ONGOING_CALL";
59 public static final String ACTION_ANSWER_VIDEO_INCOMING_CALL =
60 "com.android.incallui.ACTION_ANSWER_VIDEO_INCOMING_CALL";
61 public static final String ACTION_ANSWER_VOICE_INCOMING_CALL =
62 "com.android.incallui.ACTION_ANSWER_VOICE_INCOMING_CALL";
63 public static final String ACTION_ACCEPT_VIDEO_UPGRADE_REQUEST =
64 "com.android.incallui.ACTION_ACCEPT_VIDEO_UPGRADE_REQUEST";
65 public static final String ACTION_DECLINE_VIDEO_UPGRADE_REQUEST =
66 "com.android.incallui.ACTION_DECLINE_VIDEO_UPGRADE_REQUEST";
yuegb26c1ae2017-09-18 16:59:16 -070067 public static final String ACTION_TURN_ON_SPEAKER = "com.android.incallui.ACTION_TURN_ON_SPEAKER";
68 public static final String ACTION_TURN_OFF_SPEAKER =
69 "com.android.incallui.ACTION_TURN_OFF_SPEAKER";
erfanian1e4ef182018-06-11 11:05:51 -070070 public static final String ACTION_ANSWER_SPEAKEASY_CALL =
71 "com.android.incallui.ACTION_ANSWER_SPEAKEASY_CALL";
Eric Erfanianccca3152017-02-22 16:32:36 -080072
73 @RequiresApi(VERSION_CODES.N_MR1)
74 public static final String ACTION_PULL_EXTERNAL_CALL =
75 "com.android.incallui.ACTION_PULL_EXTERNAL_CALL";
76
77 public static final String EXTRA_NOTIFICATION_ID =
78 "com.android.incallui.extra.EXTRA_NOTIFICATION_ID";
79
80 @Override
81 public void onReceive(Context context, Intent intent) {
82 final String action = intent.getAction();
83 LogUtil.i("NotificationBroadcastReceiver.onReceive", "Broadcast from Notification: " + action);
84
85 // TODO: Commands of this nature should exist in the CallList.
86 if (action.equals(ACTION_ANSWER_VIDEO_INCOMING_CALL)) {
erfanian270663c2018-05-09 13:38:18 -070087 answerIncomingCall(VideoProfile.STATE_BIDIRECTIONAL, context);
Eric Erfanianccca3152017-02-22 16:32:36 -080088 } else if (action.equals(ACTION_ANSWER_VOICE_INCOMING_CALL)) {
erfanian270663c2018-05-09 13:38:18 -070089 answerIncomingCall(VideoProfile.STATE_AUDIO_ONLY, context);
erfanian1e4ef182018-06-11 11:05:51 -070090 } else if (action.equals(ACTION_ANSWER_SPEAKEASY_CALL)) {
91 markIncomingCallAsSpeakeasyCall();
92 answerIncomingCall(VideoProfile.STATE_AUDIO_ONLY, context);
Eric Erfanianccca3152017-02-22 16:32:36 -080093 } else if (action.equals(ACTION_DECLINE_INCOMING_CALL)) {
94 Logger.get(context)
95 .logImpression(DialerImpression.Type.REJECT_INCOMING_CALL_FROM_NOTIFICATION);
yueg01a964d2017-10-03 15:25:41 -070096 declineIncomingCall();
Eric Erfanianccca3152017-02-22 16:32:36 -080097 } else if (action.equals(ACTION_HANG_UP_ONGOING_CALL)) {
yueg01a964d2017-10-03 15:25:41 -070098 hangUpOngoingCall();
Eric Erfanianccca3152017-02-22 16:32:36 -080099 } else if (action.equals(ACTION_ACCEPT_VIDEO_UPGRADE_REQUEST)) {
100 acceptUpgradeRequest(context);
101 } else if (action.equals(ACTION_DECLINE_VIDEO_UPGRADE_REQUEST)) {
yueg01a964d2017-10-03 15:25:41 -0700102 declineUpgradeRequest();
Eric Erfanianccca3152017-02-22 16:32:36 -0800103 } else if (action.equals(ACTION_PULL_EXTERNAL_CALL)) {
Sudheer Shankaf1e05872023-03-15 16:00:45 -0700104 closeSystemDialogs(context);
Eric Erfanianccca3152017-02-22 16:32:36 -0800105 int notificationId = intent.getIntExtra(EXTRA_NOTIFICATION_ID, -1);
106 InCallPresenter.getInstance().getExternalCallNotifier().pullExternalCall(notificationId);
yuegb26c1ae2017-09-18 16:59:16 -0700107 } else if (action.equals(ACTION_TURN_ON_SPEAKER)) {
108 TelecomAdapter.getInstance().setAudioRoute(CallAudioState.ROUTE_SPEAKER);
109 } else if (action.equals(ACTION_TURN_OFF_SPEAKER)) {
110 TelecomAdapter.getInstance().setAudioRoute(CallAudioState.ROUTE_WIRED_OR_EARPIECE);
Eric Erfanianccca3152017-02-22 16:32:36 -0800111 }
112 }
113
114 private void acceptUpgradeRequest(Context context) {
115 CallList callList = InCallPresenter.getInstance().getCallList();
116 if (callList == null) {
yueg01a964d2017-10-03 15:25:41 -0700117 StatusBarNotifier.clearAllCallNotifications();
Eric Erfanianccca3152017-02-22 16:32:36 -0800118 LogUtil.e("NotificationBroadcastReceiver.acceptUpgradeRequest", "call list is empty");
119 } else {
120 DialerCall call = callList.getVideoUpgradeRequestCall();
121 if (call != null) {
twyen59209802017-09-13 10:37:01 -0700122 call.getVideoTech().acceptVideoRequest(context);
Eric Erfanianccca3152017-02-22 16:32:36 -0800123 }
124 }
125 }
126
yueg01a964d2017-10-03 15:25:41 -0700127 private void declineUpgradeRequest() {
Eric Erfanianccca3152017-02-22 16:32:36 -0800128 CallList callList = InCallPresenter.getInstance().getCallList();
129 if (callList == null) {
yueg01a964d2017-10-03 15:25:41 -0700130 StatusBarNotifier.clearAllCallNotifications();
Eric Erfanianccca3152017-02-22 16:32:36 -0800131 LogUtil.e("NotificationBroadcastReceiver.declineUpgradeRequest", "call list is empty");
132 } else {
133 DialerCall call = callList.getVideoUpgradeRequestCall();
134 if (call != null) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700135 call.getVideoTech().declineVideoRequest();
Eric Erfanianccca3152017-02-22 16:32:36 -0800136 }
137 }
138 }
139
yueg01a964d2017-10-03 15:25:41 -0700140 private void hangUpOngoingCall() {
Eric Erfanianccca3152017-02-22 16:32:36 -0800141 CallList callList = InCallPresenter.getInstance().getCallList();
142 if (callList == null) {
yueg01a964d2017-10-03 15:25:41 -0700143 StatusBarNotifier.clearAllCallNotifications();
Eric Erfanianccca3152017-02-22 16:32:36 -0800144 LogUtil.e("NotificationBroadcastReceiver.hangUpOngoingCall", "call list is empty");
145 } else {
146 DialerCall call = callList.getOutgoingCall();
147 if (call == null) {
148 call = callList.getActiveOrBackgroundCall();
149 }
150 LogUtil.i(
151 "NotificationBroadcastReceiver.hangUpOngoingCall", "disconnecting call, call: " + call);
152 if (call != null) {
153 call.disconnect();
154 }
155 }
156 }
157
erfanian1e4ef182018-06-11 11:05:51 -0700158 private void markIncomingCallAsSpeakeasyCall() {
159 CallList callList = InCallPresenter.getInstance().getCallList();
160 if (callList == null) {
161 LogUtil.e(
162 "NotificationBroadcastReceiver.markIncomingCallAsSpeakeasyCall", "call list is empty");
163 } else {
164 DialerCall call = callList.getIncomingCall();
165 if (call != null) {
166 call.setIsSpeakEasyCall(true);
167 }
168 }
169 }
170
erfanian270663c2018-05-09 13:38:18 -0700171 private void answerIncomingCall(int videoState, @NonNull Context context) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800172 CallList callList = InCallPresenter.getInstance().getCallList();
173 if (callList == null) {
yueg01a964d2017-10-03 15:25:41 -0700174 StatusBarNotifier.clearAllCallNotifications();
Eric Erfanianccca3152017-02-22 16:32:36 -0800175 LogUtil.e("NotificationBroadcastReceiver.answerIncomingCall", "call list is empty");
176 } else {
177 DialerCall call = callList.getIncomingCall();
178 if (call != null) {
erfanian270663c2018-05-09 13:38:18 -0700179
180 SpeakEasyCallManager speakEasyCallManager =
181 InCallPresenter.getInstance().getSpeakEasyCallManager();
182 ListenableFuture<Void> answerPrecondition;
183
184 if (speakEasyCallManager != null) {
185 answerPrecondition = speakEasyCallManager.onNewIncomingCall(call);
186 } else {
187 answerPrecondition = Futures.immediateFuture(null);
188 }
189
190 Futures.addCallback(
191 answerPrecondition,
192 new FutureCallback<Void>() {
193 @Override
194 public void onSuccess(Void result) {
195 answerIncomingCallCallback(call, videoState);
196 }
197
198 @Override
199 public void onFailure(Throwable t) {
200 answerIncomingCallCallback(call, videoState);
201 // TODO(erfanian): Enumerate all error states and specify recovery strategies.
202 throw new RuntimeException("Failed to successfully complete pre call tasks.", t);
203 }
204 },
205 DialerExecutorComponent.get(context).uiExecutor());
Eric Erfanianccca3152017-02-22 16:32:36 -0800206 }
207 }
208 }
209
erfanian270663c2018-05-09 13:38:18 -0700210 private void answerIncomingCallCallback(@NonNull DialerCall call, int videoState) {
211 call.answer(videoState);
212 InCallPresenter.getInstance().showInCall(false /* showDialpad */, false /* newOutgoingCall */);
213 }
214
yueg01a964d2017-10-03 15:25:41 -0700215 private void declineIncomingCall() {
Eric Erfanianccca3152017-02-22 16:32:36 -0800216 CallList callList = InCallPresenter.getInstance().getCallList();
217 if (callList == null) {
yueg01a964d2017-10-03 15:25:41 -0700218 StatusBarNotifier.clearAllCallNotifications();
Eric Erfanianccca3152017-02-22 16:32:36 -0800219 LogUtil.e("NotificationBroadcastReceiver.declineIncomingCall", "call list is empty");
220 } else {
221 DialerCall call = callList.getIncomingCall();
222 if (call != null) {
223 call.reject(false /* rejectWithMessage */, null);
224 }
225 }
226 }
Sudheer Shankaf1e05872023-03-15 16:00:45 -0700227
228 /** Closes open system dialogs and the notification shade. */
229 private void closeSystemDialogs(Context context) {
230 final Intent intent = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)
231 .addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
232 final Bundle options = BroadcastOptions.makeBasic()
233 .setDeliveryGroupPolicy(BroadcastOptions.DELIVERY_GROUP_POLICY_MOST_RECENT)
234 .setDeferralPolicy(BroadcastOptions.DEFERRAL_POLICY_UNTIL_ACTIVE)
235 .toBundle();
236 context.sendBroadcast(intent, null /* receiverPermission */, options);
237 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800238}