blob: 52d01f5c4a6d0bf1c26096d29b7f7c6cc5b2800a [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;
23import android.support.annotation.RequiresApi;
yuegb26c1ae2017-09-18 16:59:16 -070024import android.telecom.CallAudioState;
Eric Erfanianccca3152017-02-22 16:32:36 -080025import android.telecom.VideoProfile;
26import com.android.dialer.common.LogUtil;
Eric Erfanian8369df02017-05-03 10:27:13 -070027import com.android.dialer.logging.DialerImpression;
Eric Erfanianccca3152017-02-22 16:32:36 -080028import com.android.dialer.logging.Logger;
Eric Erfanianccca3152017-02-22 16:32:36 -080029import com.android.incallui.call.CallList;
30import com.android.incallui.call.DialerCall;
yuegb26c1ae2017-09-18 16:59:16 -070031import com.android.incallui.call.TelecomAdapter;
Eric Erfanianccca3152017-02-22 16:32:36 -080032
33/**
34 * Accepts broadcast Intents which will be prepared by {@link StatusBarNotifier} and thus sent from
35 * the notification manager. This should be visible from outside, but shouldn't be exported.
36 */
37public class NotificationBroadcastReceiver extends BroadcastReceiver {
38
39 /**
40 * Intent Action used for hanging up the current call from Notification bar. This will choose
41 * first ringing call, first active call, or first background call (typically in STATE_HOLDING
42 * state).
43 */
44 public static final String ACTION_DECLINE_INCOMING_CALL =
45 "com.android.incallui.ACTION_DECLINE_INCOMING_CALL";
46
47 public static final String ACTION_HANG_UP_ONGOING_CALL =
48 "com.android.incallui.ACTION_HANG_UP_ONGOING_CALL";
49 public static final String ACTION_ANSWER_VIDEO_INCOMING_CALL =
50 "com.android.incallui.ACTION_ANSWER_VIDEO_INCOMING_CALL";
51 public static final String ACTION_ANSWER_VOICE_INCOMING_CALL =
52 "com.android.incallui.ACTION_ANSWER_VOICE_INCOMING_CALL";
53 public static final String ACTION_ACCEPT_VIDEO_UPGRADE_REQUEST =
54 "com.android.incallui.ACTION_ACCEPT_VIDEO_UPGRADE_REQUEST";
55 public static final String ACTION_DECLINE_VIDEO_UPGRADE_REQUEST =
56 "com.android.incallui.ACTION_DECLINE_VIDEO_UPGRADE_REQUEST";
yuegb26c1ae2017-09-18 16:59:16 -070057 public static final String ACTION_TURN_ON_SPEAKER = "com.android.incallui.ACTION_TURN_ON_SPEAKER";
58 public static final String ACTION_TURN_OFF_SPEAKER =
59 "com.android.incallui.ACTION_TURN_OFF_SPEAKER";
Eric Erfanianccca3152017-02-22 16:32:36 -080060
61 @RequiresApi(VERSION_CODES.N_MR1)
62 public static final String ACTION_PULL_EXTERNAL_CALL =
63 "com.android.incallui.ACTION_PULL_EXTERNAL_CALL";
64
65 public static final String EXTRA_NOTIFICATION_ID =
66 "com.android.incallui.extra.EXTRA_NOTIFICATION_ID";
67
68 @Override
69 public void onReceive(Context context, Intent intent) {
70 final String action = intent.getAction();
71 LogUtil.i("NotificationBroadcastReceiver.onReceive", "Broadcast from Notification: " + action);
72
73 // TODO: Commands of this nature should exist in the CallList.
74 if (action.equals(ACTION_ANSWER_VIDEO_INCOMING_CALL)) {
yueg01a964d2017-10-03 15:25:41 -070075 answerIncomingCall(VideoProfile.STATE_BIDIRECTIONAL);
Eric Erfanianccca3152017-02-22 16:32:36 -080076 } else if (action.equals(ACTION_ANSWER_VOICE_INCOMING_CALL)) {
yueg01a964d2017-10-03 15:25:41 -070077 answerIncomingCall(VideoProfile.STATE_AUDIO_ONLY);
Eric Erfanianccca3152017-02-22 16:32:36 -080078 } else if (action.equals(ACTION_DECLINE_INCOMING_CALL)) {
79 Logger.get(context)
80 .logImpression(DialerImpression.Type.REJECT_INCOMING_CALL_FROM_NOTIFICATION);
yueg01a964d2017-10-03 15:25:41 -070081 declineIncomingCall();
Eric Erfanianccca3152017-02-22 16:32:36 -080082 } else if (action.equals(ACTION_HANG_UP_ONGOING_CALL)) {
yueg01a964d2017-10-03 15:25:41 -070083 hangUpOngoingCall();
Eric Erfanianccca3152017-02-22 16:32:36 -080084 } else if (action.equals(ACTION_ACCEPT_VIDEO_UPGRADE_REQUEST)) {
85 acceptUpgradeRequest(context);
86 } else if (action.equals(ACTION_DECLINE_VIDEO_UPGRADE_REQUEST)) {
yueg01a964d2017-10-03 15:25:41 -070087 declineUpgradeRequest();
Eric Erfanianccca3152017-02-22 16:32:36 -080088 } else if (action.equals(ACTION_PULL_EXTERNAL_CALL)) {
89 context.sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
90 int notificationId = intent.getIntExtra(EXTRA_NOTIFICATION_ID, -1);
91 InCallPresenter.getInstance().getExternalCallNotifier().pullExternalCall(notificationId);
yuegb26c1ae2017-09-18 16:59:16 -070092 } else if (action.equals(ACTION_TURN_ON_SPEAKER)) {
93 TelecomAdapter.getInstance().setAudioRoute(CallAudioState.ROUTE_SPEAKER);
94 } else if (action.equals(ACTION_TURN_OFF_SPEAKER)) {
95 TelecomAdapter.getInstance().setAudioRoute(CallAudioState.ROUTE_WIRED_OR_EARPIECE);
Eric Erfanianccca3152017-02-22 16:32:36 -080096 }
97 }
98
99 private void acceptUpgradeRequest(Context context) {
100 CallList callList = InCallPresenter.getInstance().getCallList();
101 if (callList == null) {
yueg01a964d2017-10-03 15:25:41 -0700102 StatusBarNotifier.clearAllCallNotifications();
Eric Erfanianccca3152017-02-22 16:32:36 -0800103 LogUtil.e("NotificationBroadcastReceiver.acceptUpgradeRequest", "call list is empty");
104 } else {
105 DialerCall call = callList.getVideoUpgradeRequestCall();
106 if (call != null) {
twyen59209802017-09-13 10:37:01 -0700107 call.getVideoTech().acceptVideoRequest(context);
Eric Erfanianccca3152017-02-22 16:32:36 -0800108 }
109 }
110 }
111
yueg01a964d2017-10-03 15:25:41 -0700112 private void declineUpgradeRequest() {
Eric Erfanianccca3152017-02-22 16:32:36 -0800113 CallList callList = InCallPresenter.getInstance().getCallList();
114 if (callList == null) {
yueg01a964d2017-10-03 15:25:41 -0700115 StatusBarNotifier.clearAllCallNotifications();
Eric Erfanianccca3152017-02-22 16:32:36 -0800116 LogUtil.e("NotificationBroadcastReceiver.declineUpgradeRequest", "call list is empty");
117 } else {
118 DialerCall call = callList.getVideoUpgradeRequestCall();
119 if (call != null) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700120 call.getVideoTech().declineVideoRequest();
Eric Erfanianccca3152017-02-22 16:32:36 -0800121 }
122 }
123 }
124
yueg01a964d2017-10-03 15:25:41 -0700125 private void hangUpOngoingCall() {
Eric Erfanianccca3152017-02-22 16:32:36 -0800126 CallList callList = InCallPresenter.getInstance().getCallList();
127 if (callList == null) {
yueg01a964d2017-10-03 15:25:41 -0700128 StatusBarNotifier.clearAllCallNotifications();
Eric Erfanianccca3152017-02-22 16:32:36 -0800129 LogUtil.e("NotificationBroadcastReceiver.hangUpOngoingCall", "call list is empty");
130 } else {
131 DialerCall call = callList.getOutgoingCall();
132 if (call == null) {
133 call = callList.getActiveOrBackgroundCall();
134 }
135 LogUtil.i(
136 "NotificationBroadcastReceiver.hangUpOngoingCall", "disconnecting call, call: " + call);
137 if (call != null) {
138 call.disconnect();
139 }
140 }
141 }
142
yueg01a964d2017-10-03 15:25:41 -0700143 private void answerIncomingCall(int videoState) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800144 CallList callList = InCallPresenter.getInstance().getCallList();
145 if (callList == null) {
yueg01a964d2017-10-03 15:25:41 -0700146 StatusBarNotifier.clearAllCallNotifications();
Eric Erfanianccca3152017-02-22 16:32:36 -0800147 LogUtil.e("NotificationBroadcastReceiver.answerIncomingCall", "call list is empty");
148 } else {
149 DialerCall call = callList.getIncomingCall();
150 if (call != null) {
151 call.answer(videoState);
152 InCallPresenter.getInstance()
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700153 .showInCall(false /* showDialpad */, false /* newOutgoingCall */);
Eric Erfanianccca3152017-02-22 16:32:36 -0800154 }
155 }
156 }
157
yueg01a964d2017-10-03 15:25:41 -0700158 private void declineIncomingCall() {
Eric Erfanianccca3152017-02-22 16:32:36 -0800159 CallList callList = InCallPresenter.getInstance().getCallList();
160 if (callList == null) {
yueg01a964d2017-10-03 15:25:41 -0700161 StatusBarNotifier.clearAllCallNotifications();
Eric Erfanianccca3152017-02-22 16:32:36 -0800162 LogUtil.e("NotificationBroadcastReceiver.declineIncomingCall", "call list is empty");
163 } else {
164 DialerCall call = callList.getIncomingCall();
165 if (call != null) {
166 call.reject(false /* rejectWithMessage */, null);
167 }
168 }
169 }
170}