blob: 241d8ed4840038a463c290c33935df2e31c8e1b6 [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
19import android.content.BroadcastReceiver;
20import android.content.Context;
21import android.content.Intent;
22import android.os.Build.VERSION_CODES;
erfanian270663c2018-05-09 13:38:18 -070023import android.support.annotation.NonNull;
Eric Erfanianccca3152017-02-22 16:32:36 -080024import android.support.annotation.RequiresApi;
yuegb26c1ae2017-09-18 16:59:16 -070025import android.telecom.CallAudioState;
Eric Erfanianccca3152017-02-22 16:32:36 -080026import android.telecom.VideoProfile;
27import com.android.dialer.common.LogUtil;
erfanian270663c2018-05-09 13:38:18 -070028import com.android.dialer.common.concurrent.DialerExecutorComponent;
Eric Erfanian8369df02017-05-03 10:27:13 -070029import com.android.dialer.logging.DialerImpression;
Eric Erfanianccca3152017-02-22 16:32:36 -080030import com.android.dialer.logging.Logger;
Eric Erfanianccca3152017-02-22 16:32:36 -080031import com.android.incallui.call.CallList;
32import com.android.incallui.call.DialerCall;
yuegb26c1ae2017-09-18 16:59:16 -070033import com.android.incallui.call.TelecomAdapter;
erfanian270663c2018-05-09 13:38:18 -070034import com.android.incallui.speakeasy.SpeakEasyCallManager;
35import com.google.common.util.concurrent.FutureCallback;
36import com.google.common.util.concurrent.Futures;
37import com.google.common.util.concurrent.ListenableFuture;
Eric Erfanianccca3152017-02-22 16:32:36 -080038
39/**
40 * Accepts broadcast Intents which will be prepared by {@link StatusBarNotifier} and thus sent from
41 * the notification manager. This should be visible from outside, but shouldn't be exported.
42 */
43public class NotificationBroadcastReceiver extends BroadcastReceiver {
44
45 /**
46 * Intent Action used for hanging up the current call from Notification bar. This will choose
47 * first ringing call, first active call, or first background call (typically in STATE_HOLDING
48 * state).
49 */
50 public static final String ACTION_DECLINE_INCOMING_CALL =
51 "com.android.incallui.ACTION_DECLINE_INCOMING_CALL";
52
53 public static final String ACTION_HANG_UP_ONGOING_CALL =
54 "com.android.incallui.ACTION_HANG_UP_ONGOING_CALL";
55 public static final String ACTION_ANSWER_VIDEO_INCOMING_CALL =
56 "com.android.incallui.ACTION_ANSWER_VIDEO_INCOMING_CALL";
57 public static final String ACTION_ANSWER_VOICE_INCOMING_CALL =
58 "com.android.incallui.ACTION_ANSWER_VOICE_INCOMING_CALL";
59 public static final String ACTION_ACCEPT_VIDEO_UPGRADE_REQUEST =
60 "com.android.incallui.ACTION_ACCEPT_VIDEO_UPGRADE_REQUEST";
61 public static final String ACTION_DECLINE_VIDEO_UPGRADE_REQUEST =
62 "com.android.incallui.ACTION_DECLINE_VIDEO_UPGRADE_REQUEST";
yuegb26c1ae2017-09-18 16:59:16 -070063 public static final String ACTION_TURN_ON_SPEAKER = "com.android.incallui.ACTION_TURN_ON_SPEAKER";
64 public static final String ACTION_TURN_OFF_SPEAKER =
65 "com.android.incallui.ACTION_TURN_OFF_SPEAKER";
erfanian1e4ef182018-06-11 11:05:51 -070066 public static final String ACTION_ANSWER_SPEAKEASY_CALL =
67 "com.android.incallui.ACTION_ANSWER_SPEAKEASY_CALL";
Eric Erfanianccca3152017-02-22 16:32:36 -080068
69 @RequiresApi(VERSION_CODES.N_MR1)
70 public static final String ACTION_PULL_EXTERNAL_CALL =
71 "com.android.incallui.ACTION_PULL_EXTERNAL_CALL";
72
73 public static final String EXTRA_NOTIFICATION_ID =
74 "com.android.incallui.extra.EXTRA_NOTIFICATION_ID";
75
76 @Override
77 public void onReceive(Context context, Intent intent) {
78 final String action = intent.getAction();
79 LogUtil.i("NotificationBroadcastReceiver.onReceive", "Broadcast from Notification: " + action);
80
81 // TODO: Commands of this nature should exist in the CallList.
82 if (action.equals(ACTION_ANSWER_VIDEO_INCOMING_CALL)) {
erfanian270663c2018-05-09 13:38:18 -070083 answerIncomingCall(VideoProfile.STATE_BIDIRECTIONAL, context);
Eric Erfanianccca3152017-02-22 16:32:36 -080084 } else if (action.equals(ACTION_ANSWER_VOICE_INCOMING_CALL)) {
erfanian270663c2018-05-09 13:38:18 -070085 answerIncomingCall(VideoProfile.STATE_AUDIO_ONLY, context);
erfanian1e4ef182018-06-11 11:05:51 -070086 } else if (action.equals(ACTION_ANSWER_SPEAKEASY_CALL)) {
87 markIncomingCallAsSpeakeasyCall();
88 answerIncomingCall(VideoProfile.STATE_AUDIO_ONLY, context);
Eric Erfanianccca3152017-02-22 16:32:36 -080089 } else if (action.equals(ACTION_DECLINE_INCOMING_CALL)) {
90 Logger.get(context)
91 .logImpression(DialerImpression.Type.REJECT_INCOMING_CALL_FROM_NOTIFICATION);
yueg01a964d2017-10-03 15:25:41 -070092 declineIncomingCall();
Eric Erfanianccca3152017-02-22 16:32:36 -080093 } else if (action.equals(ACTION_HANG_UP_ONGOING_CALL)) {
yueg01a964d2017-10-03 15:25:41 -070094 hangUpOngoingCall();
Eric Erfanianccca3152017-02-22 16:32:36 -080095 } else if (action.equals(ACTION_ACCEPT_VIDEO_UPGRADE_REQUEST)) {
96 acceptUpgradeRequest(context);
97 } else if (action.equals(ACTION_DECLINE_VIDEO_UPGRADE_REQUEST)) {
yueg01a964d2017-10-03 15:25:41 -070098 declineUpgradeRequest();
Eric Erfanianccca3152017-02-22 16:32:36 -080099 } else if (action.equals(ACTION_PULL_EXTERNAL_CALL)) {
100 context.sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
101 int notificationId = intent.getIntExtra(EXTRA_NOTIFICATION_ID, -1);
102 InCallPresenter.getInstance().getExternalCallNotifier().pullExternalCall(notificationId);
yuegb26c1ae2017-09-18 16:59:16 -0700103 } else if (action.equals(ACTION_TURN_ON_SPEAKER)) {
104 TelecomAdapter.getInstance().setAudioRoute(CallAudioState.ROUTE_SPEAKER);
105 } else if (action.equals(ACTION_TURN_OFF_SPEAKER)) {
106 TelecomAdapter.getInstance().setAudioRoute(CallAudioState.ROUTE_WIRED_OR_EARPIECE);
Eric Erfanianccca3152017-02-22 16:32:36 -0800107 }
108 }
109
110 private void acceptUpgradeRequest(Context context) {
111 CallList callList = InCallPresenter.getInstance().getCallList();
112 if (callList == null) {
yueg01a964d2017-10-03 15:25:41 -0700113 StatusBarNotifier.clearAllCallNotifications();
Eric Erfanianccca3152017-02-22 16:32:36 -0800114 LogUtil.e("NotificationBroadcastReceiver.acceptUpgradeRequest", "call list is empty");
115 } else {
116 DialerCall call = callList.getVideoUpgradeRequestCall();
117 if (call != null) {
twyen59209802017-09-13 10:37:01 -0700118 call.getVideoTech().acceptVideoRequest(context);
Eric Erfanianccca3152017-02-22 16:32:36 -0800119 }
120 }
121 }
122
yueg01a964d2017-10-03 15:25:41 -0700123 private void declineUpgradeRequest() {
Eric Erfanianccca3152017-02-22 16:32:36 -0800124 CallList callList = InCallPresenter.getInstance().getCallList();
125 if (callList == null) {
yueg01a964d2017-10-03 15:25:41 -0700126 StatusBarNotifier.clearAllCallNotifications();
Eric Erfanianccca3152017-02-22 16:32:36 -0800127 LogUtil.e("NotificationBroadcastReceiver.declineUpgradeRequest", "call list is empty");
128 } else {
129 DialerCall call = callList.getVideoUpgradeRequestCall();
130 if (call != null) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700131 call.getVideoTech().declineVideoRequest();
Eric Erfanianccca3152017-02-22 16:32:36 -0800132 }
133 }
134 }
135
yueg01a964d2017-10-03 15:25:41 -0700136 private void hangUpOngoingCall() {
Eric Erfanianccca3152017-02-22 16:32:36 -0800137 CallList callList = InCallPresenter.getInstance().getCallList();
138 if (callList == null) {
yueg01a964d2017-10-03 15:25:41 -0700139 StatusBarNotifier.clearAllCallNotifications();
Eric Erfanianccca3152017-02-22 16:32:36 -0800140 LogUtil.e("NotificationBroadcastReceiver.hangUpOngoingCall", "call list is empty");
141 } else {
142 DialerCall call = callList.getOutgoingCall();
143 if (call == null) {
144 call = callList.getActiveOrBackgroundCall();
145 }
146 LogUtil.i(
147 "NotificationBroadcastReceiver.hangUpOngoingCall", "disconnecting call, call: " + call);
148 if (call != null) {
149 call.disconnect();
150 }
151 }
152 }
153
erfanian1e4ef182018-06-11 11:05:51 -0700154 private void markIncomingCallAsSpeakeasyCall() {
155 CallList callList = InCallPresenter.getInstance().getCallList();
156 if (callList == null) {
157 LogUtil.e(
158 "NotificationBroadcastReceiver.markIncomingCallAsSpeakeasyCall", "call list is empty");
159 } else {
160 DialerCall call = callList.getIncomingCall();
161 if (call != null) {
162 call.setIsSpeakEasyCall(true);
163 }
164 }
165 }
166
erfanian270663c2018-05-09 13:38:18 -0700167 private void answerIncomingCall(int videoState, @NonNull Context context) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800168 CallList callList = InCallPresenter.getInstance().getCallList();
169 if (callList == null) {
yueg01a964d2017-10-03 15:25:41 -0700170 StatusBarNotifier.clearAllCallNotifications();
Eric Erfanianccca3152017-02-22 16:32:36 -0800171 LogUtil.e("NotificationBroadcastReceiver.answerIncomingCall", "call list is empty");
172 } else {
173 DialerCall call = callList.getIncomingCall();
174 if (call != null) {
erfanian270663c2018-05-09 13:38:18 -0700175
176 SpeakEasyCallManager speakEasyCallManager =
177 InCallPresenter.getInstance().getSpeakEasyCallManager();
178 ListenableFuture<Void> answerPrecondition;
179
180 if (speakEasyCallManager != null) {
181 answerPrecondition = speakEasyCallManager.onNewIncomingCall(call);
182 } else {
183 answerPrecondition = Futures.immediateFuture(null);
184 }
185
186 Futures.addCallback(
187 answerPrecondition,
188 new FutureCallback<Void>() {
189 @Override
190 public void onSuccess(Void result) {
191 answerIncomingCallCallback(call, videoState);
192 }
193
194 @Override
195 public void onFailure(Throwable t) {
196 answerIncomingCallCallback(call, videoState);
197 // TODO(erfanian): Enumerate all error states and specify recovery strategies.
198 throw new RuntimeException("Failed to successfully complete pre call tasks.", t);
199 }
200 },
201 DialerExecutorComponent.get(context).uiExecutor());
Eric Erfanianccca3152017-02-22 16:32:36 -0800202 }
203 }
204 }
205
erfanian270663c2018-05-09 13:38:18 -0700206 private void answerIncomingCallCallback(@NonNull DialerCall call, int videoState) {
207 call.answer(videoState);
208 InCallPresenter.getInstance().showInCall(false /* showDialpad */, false /* newOutgoingCall */);
209 }
210
yueg01a964d2017-10-03 15:25:41 -0700211 private void declineIncomingCall() {
Eric Erfanianccca3152017-02-22 16:32:36 -0800212 CallList callList = InCallPresenter.getInstance().getCallList();
213 if (callList == null) {
yueg01a964d2017-10-03 15:25:41 -0700214 StatusBarNotifier.clearAllCallNotifications();
Eric Erfanianccca3152017-02-22 16:32:36 -0800215 LogUtil.e("NotificationBroadcastReceiver.declineIncomingCall", "call list is empty");
216 } else {
217 DialerCall call = callList.getIncomingCall();
218 if (call != null) {
219 call.reject(false /* rejectWithMessage */, null);
220 }
221 }
222 }
223}