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