blob: 602eb5c5aadc32ff627e507aa26e65edbbab2528 [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";
Eric Erfanianccca3152017-02-22 16:32:36 -080066
67 @RequiresApi(VERSION_CODES.N_MR1)
68 public static final String ACTION_PULL_EXTERNAL_CALL =
69 "com.android.incallui.ACTION_PULL_EXTERNAL_CALL";
70
71 public static final String EXTRA_NOTIFICATION_ID =
72 "com.android.incallui.extra.EXTRA_NOTIFICATION_ID";
73
74 @Override
75 public void onReceive(Context context, Intent intent) {
76 final String action = intent.getAction();
77 LogUtil.i("NotificationBroadcastReceiver.onReceive", "Broadcast from Notification: " + action);
78
79 // TODO: Commands of this nature should exist in the CallList.
80 if (action.equals(ACTION_ANSWER_VIDEO_INCOMING_CALL)) {
erfanian270663c2018-05-09 13:38:18 -070081 answerIncomingCall(VideoProfile.STATE_BIDIRECTIONAL, context);
Eric Erfanianccca3152017-02-22 16:32:36 -080082 } else if (action.equals(ACTION_ANSWER_VOICE_INCOMING_CALL)) {
erfanian270663c2018-05-09 13:38:18 -070083 answerIncomingCall(VideoProfile.STATE_AUDIO_ONLY, context);
Eric Erfanianccca3152017-02-22 16:32:36 -080084 } else if (action.equals(ACTION_DECLINE_INCOMING_CALL)) {
85 Logger.get(context)
86 .logImpression(DialerImpression.Type.REJECT_INCOMING_CALL_FROM_NOTIFICATION);
yueg01a964d2017-10-03 15:25:41 -070087 declineIncomingCall();
Eric Erfanianccca3152017-02-22 16:32:36 -080088 } else if (action.equals(ACTION_HANG_UP_ONGOING_CALL)) {
yueg01a964d2017-10-03 15:25:41 -070089 hangUpOngoingCall();
Eric Erfanianccca3152017-02-22 16:32:36 -080090 } else if (action.equals(ACTION_ACCEPT_VIDEO_UPGRADE_REQUEST)) {
91 acceptUpgradeRequest(context);
92 } else if (action.equals(ACTION_DECLINE_VIDEO_UPGRADE_REQUEST)) {
yueg01a964d2017-10-03 15:25:41 -070093 declineUpgradeRequest();
Eric Erfanianccca3152017-02-22 16:32:36 -080094 } else if (action.equals(ACTION_PULL_EXTERNAL_CALL)) {
95 context.sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
96 int notificationId = intent.getIntExtra(EXTRA_NOTIFICATION_ID, -1);
97 InCallPresenter.getInstance().getExternalCallNotifier().pullExternalCall(notificationId);
yuegb26c1ae2017-09-18 16:59:16 -070098 } else if (action.equals(ACTION_TURN_ON_SPEAKER)) {
99 TelecomAdapter.getInstance().setAudioRoute(CallAudioState.ROUTE_SPEAKER);
100 } else if (action.equals(ACTION_TURN_OFF_SPEAKER)) {
101 TelecomAdapter.getInstance().setAudioRoute(CallAudioState.ROUTE_WIRED_OR_EARPIECE);
Eric Erfanianccca3152017-02-22 16:32:36 -0800102 }
103 }
104
105 private void acceptUpgradeRequest(Context context) {
106 CallList callList = InCallPresenter.getInstance().getCallList();
107 if (callList == null) {
yueg01a964d2017-10-03 15:25:41 -0700108 StatusBarNotifier.clearAllCallNotifications();
Eric Erfanianccca3152017-02-22 16:32:36 -0800109 LogUtil.e("NotificationBroadcastReceiver.acceptUpgradeRequest", "call list is empty");
110 } else {
111 DialerCall call = callList.getVideoUpgradeRequestCall();
112 if (call != null) {
twyen59209802017-09-13 10:37:01 -0700113 call.getVideoTech().acceptVideoRequest(context);
Eric Erfanianccca3152017-02-22 16:32:36 -0800114 }
115 }
116 }
117
yueg01a964d2017-10-03 15:25:41 -0700118 private void declineUpgradeRequest() {
Eric Erfanianccca3152017-02-22 16:32:36 -0800119 CallList callList = InCallPresenter.getInstance().getCallList();
120 if (callList == null) {
yueg01a964d2017-10-03 15:25:41 -0700121 StatusBarNotifier.clearAllCallNotifications();
Eric Erfanianccca3152017-02-22 16:32:36 -0800122 LogUtil.e("NotificationBroadcastReceiver.declineUpgradeRequest", "call list is empty");
123 } else {
124 DialerCall call = callList.getVideoUpgradeRequestCall();
125 if (call != null) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700126 call.getVideoTech().declineVideoRequest();
Eric Erfanianccca3152017-02-22 16:32:36 -0800127 }
128 }
129 }
130
yueg01a964d2017-10-03 15:25:41 -0700131 private void hangUpOngoingCall() {
Eric Erfanianccca3152017-02-22 16:32:36 -0800132 CallList callList = InCallPresenter.getInstance().getCallList();
133 if (callList == null) {
yueg01a964d2017-10-03 15:25:41 -0700134 StatusBarNotifier.clearAllCallNotifications();
Eric Erfanianccca3152017-02-22 16:32:36 -0800135 LogUtil.e("NotificationBroadcastReceiver.hangUpOngoingCall", "call list is empty");
136 } else {
137 DialerCall call = callList.getOutgoingCall();
138 if (call == null) {
139 call = callList.getActiveOrBackgroundCall();
140 }
141 LogUtil.i(
142 "NotificationBroadcastReceiver.hangUpOngoingCall", "disconnecting call, call: " + call);
143 if (call != null) {
144 call.disconnect();
145 }
146 }
147 }
148
erfanian270663c2018-05-09 13:38:18 -0700149 private void answerIncomingCall(int videoState, @NonNull Context context) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800150 CallList callList = InCallPresenter.getInstance().getCallList();
151 if (callList == null) {
yueg01a964d2017-10-03 15:25:41 -0700152 StatusBarNotifier.clearAllCallNotifications();
Eric Erfanianccca3152017-02-22 16:32:36 -0800153 LogUtil.e("NotificationBroadcastReceiver.answerIncomingCall", "call list is empty");
154 } else {
155 DialerCall call = callList.getIncomingCall();
156 if (call != null) {
erfanian270663c2018-05-09 13:38:18 -0700157
158 SpeakEasyCallManager speakEasyCallManager =
159 InCallPresenter.getInstance().getSpeakEasyCallManager();
160 ListenableFuture<Void> answerPrecondition;
161
162 if (speakEasyCallManager != null) {
163 answerPrecondition = speakEasyCallManager.onNewIncomingCall(call);
164 } else {
165 answerPrecondition = Futures.immediateFuture(null);
166 }
167
168 Futures.addCallback(
169 answerPrecondition,
170 new FutureCallback<Void>() {
171 @Override
172 public void onSuccess(Void result) {
173 answerIncomingCallCallback(call, videoState);
174 }
175
176 @Override
177 public void onFailure(Throwable t) {
178 answerIncomingCallCallback(call, videoState);
179 // TODO(erfanian): Enumerate all error states and specify recovery strategies.
180 throw new RuntimeException("Failed to successfully complete pre call tasks.", t);
181 }
182 },
183 DialerExecutorComponent.get(context).uiExecutor());
Eric Erfanianccca3152017-02-22 16:32:36 -0800184 }
185 }
186 }
187
erfanian270663c2018-05-09 13:38:18 -0700188 private void answerIncomingCallCallback(@NonNull DialerCall call, int videoState) {
189 call.answer(videoState);
190 InCallPresenter.getInstance().showInCall(false /* showDialpad */, false /* newOutgoingCall */);
191 }
192
yueg01a964d2017-10-03 15:25:41 -0700193 private void declineIncomingCall() {
Eric Erfanianccca3152017-02-22 16:32:36 -0800194 CallList callList = InCallPresenter.getInstance().getCallList();
195 if (callList == null) {
yueg01a964d2017-10-03 15:25:41 -0700196 StatusBarNotifier.clearAllCallNotifications();
Eric Erfanianccca3152017-02-22 16:32:36 -0800197 LogUtil.e("NotificationBroadcastReceiver.declineIncomingCall", "call list is empty");
198 } else {
199 DialerCall call = callList.getIncomingCall();
200 if (call != null) {
201 call.reject(false /* rejectWithMessage */, null);
202 }
203 }
204 }
205}