blob: 5c5d255cc0a0b3eed41f069030107c8ad9322c83 [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;
24import android.telecom.VideoProfile;
25import com.android.dialer.common.LogUtil;
26import com.android.dialer.logging.Logger;
27import com.android.dialer.logging.nano.DialerImpression;
28import com.android.incallui.call.CallList;
29import com.android.incallui.call.DialerCall;
30import com.android.incallui.call.VideoUtils;
31
32/**
33 * Accepts broadcast Intents which will be prepared by {@link StatusBarNotifier} and thus sent from
34 * the notification manager. This should be visible from outside, but shouldn't be exported.
35 */
36public class NotificationBroadcastReceiver extends BroadcastReceiver {
37
38 /**
39 * Intent Action used for hanging up the current call from Notification bar. This will choose
40 * first ringing call, first active call, or first background call (typically in STATE_HOLDING
41 * state).
42 */
43 public static final String ACTION_DECLINE_INCOMING_CALL =
44 "com.android.incallui.ACTION_DECLINE_INCOMING_CALL";
45
46 public static final String ACTION_HANG_UP_ONGOING_CALL =
47 "com.android.incallui.ACTION_HANG_UP_ONGOING_CALL";
48 public static final String ACTION_ANSWER_VIDEO_INCOMING_CALL =
49 "com.android.incallui.ACTION_ANSWER_VIDEO_INCOMING_CALL";
50 public static final String ACTION_ANSWER_VOICE_INCOMING_CALL =
51 "com.android.incallui.ACTION_ANSWER_VOICE_INCOMING_CALL";
52 public static final String ACTION_ACCEPT_VIDEO_UPGRADE_REQUEST =
53 "com.android.incallui.ACTION_ACCEPT_VIDEO_UPGRADE_REQUEST";
54 public static final String ACTION_DECLINE_VIDEO_UPGRADE_REQUEST =
55 "com.android.incallui.ACTION_DECLINE_VIDEO_UPGRADE_REQUEST";
56
57 @RequiresApi(VERSION_CODES.N_MR1)
58 public static final String ACTION_PULL_EXTERNAL_CALL =
59 "com.android.incallui.ACTION_PULL_EXTERNAL_CALL";
60
61 public static final String EXTRA_NOTIFICATION_ID =
62 "com.android.incallui.extra.EXTRA_NOTIFICATION_ID";
63
64 @Override
65 public void onReceive(Context context, Intent intent) {
66 final String action = intent.getAction();
67 LogUtil.i("NotificationBroadcastReceiver.onReceive", "Broadcast from Notification: " + action);
68
69 // TODO: Commands of this nature should exist in the CallList.
70 if (action.equals(ACTION_ANSWER_VIDEO_INCOMING_CALL)) {
71 answerIncomingCall(context, VideoProfile.STATE_BIDIRECTIONAL);
72 } else if (action.equals(ACTION_ANSWER_VOICE_INCOMING_CALL)) {
73 answerIncomingCall(context, VideoProfile.STATE_AUDIO_ONLY);
74 } else if (action.equals(ACTION_DECLINE_INCOMING_CALL)) {
75 Logger.get(context)
76 .logImpression(DialerImpression.Type.REJECT_INCOMING_CALL_FROM_NOTIFICATION);
77 declineIncomingCall(context);
78 } else if (action.equals(ACTION_HANG_UP_ONGOING_CALL)) {
79 hangUpOngoingCall(context);
80 } else if (action.equals(ACTION_ACCEPT_VIDEO_UPGRADE_REQUEST)) {
81 acceptUpgradeRequest(context);
82 } else if (action.equals(ACTION_DECLINE_VIDEO_UPGRADE_REQUEST)) {
83 declineUpgradeRequest(context);
84 } else if (action.equals(ACTION_PULL_EXTERNAL_CALL)) {
85 context.sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
86 int notificationId = intent.getIntExtra(EXTRA_NOTIFICATION_ID, -1);
87 InCallPresenter.getInstance().getExternalCallNotifier().pullExternalCall(notificationId);
88 }
89 }
90
91 private void acceptUpgradeRequest(Context context) {
92 CallList callList = InCallPresenter.getInstance().getCallList();
93 if (callList == null) {
94 StatusBarNotifier.clearAllCallNotifications(context);
95 LogUtil.e("NotificationBroadcastReceiver.acceptUpgradeRequest", "call list is empty");
96 } else {
97 DialerCall call = callList.getVideoUpgradeRequestCall();
98 if (call != null) {
99 call.acceptUpgradeRequest(call.getRequestedVideoState());
100 }
101 }
102 }
103
104 private void declineUpgradeRequest(Context context) {
105 CallList callList = InCallPresenter.getInstance().getCallList();
106 if (callList == null) {
107 StatusBarNotifier.clearAllCallNotifications(context);
108 LogUtil.e("NotificationBroadcastReceiver.declineUpgradeRequest", "call list is empty");
109 } else {
110 DialerCall call = callList.getVideoUpgradeRequestCall();
111 if (call != null) {
112 call.declineUpgradeRequest();
113 }
114 }
115 }
116
117 private void hangUpOngoingCall(Context context) {
118 CallList callList = InCallPresenter.getInstance().getCallList();
119 if (callList == null) {
120 StatusBarNotifier.clearAllCallNotifications(context);
121 LogUtil.e("NotificationBroadcastReceiver.hangUpOngoingCall", "call list is empty");
122 } else {
123 DialerCall call = callList.getOutgoingCall();
124 if (call == null) {
125 call = callList.getActiveOrBackgroundCall();
126 }
127 LogUtil.i(
128 "NotificationBroadcastReceiver.hangUpOngoingCall", "disconnecting call, call: " + call);
129 if (call != null) {
130 call.disconnect();
131 }
132 }
133 }
134
135 private void answerIncomingCall(Context context, int videoState) {
136 CallList callList = InCallPresenter.getInstance().getCallList();
137 if (callList == null) {
138 StatusBarNotifier.clearAllCallNotifications(context);
139 LogUtil.e("NotificationBroadcastReceiver.answerIncomingCall", "call list is empty");
140 } else {
141 DialerCall call = callList.getIncomingCall();
142 if (call != null) {
143 call.answer(videoState);
144 InCallPresenter.getInstance()
145 .showInCall(
146 false /* showDialpad */,
147 false /* newOutgoingCall */,
148 VideoUtils.isVideoCall(videoState));
149 }
150 }
151 }
152
153 private void declineIncomingCall(Context context) {
154 CallList callList = InCallPresenter.getInstance().getCallList();
155 if (callList == null) {
156 StatusBarNotifier.clearAllCallNotifications(context);
157 LogUtil.e("NotificationBroadcastReceiver.declineIncomingCall", "call list is empty");
158 } else {
159 DialerCall call = callList.getIncomingCall();
160 if (call != null) {
161 call.reject(false /* rejectWithMessage */, null);
162 }
163 }
164 }
165}