Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 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 | |
| 17 | package com.android.incallui; |
| 18 | |
| 19 | import android.content.BroadcastReceiver; |
| 20 | import android.content.Context; |
| 21 | import android.content.Intent; |
| 22 | import android.os.Build.VERSION_CODES; |
| 23 | import android.support.annotation.RequiresApi; |
yueg | b26c1ae | 2017-09-18 16:59:16 -0700 | [diff] [blame] | 24 | import android.telecom.CallAudioState; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 25 | import android.telecom.VideoProfile; |
| 26 | import com.android.dialer.common.LogUtil; |
Eric Erfanian | 8369df0 | 2017-05-03 10:27:13 -0700 | [diff] [blame] | 27 | import com.android.dialer.logging.DialerImpression; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 28 | import com.android.dialer.logging.Logger; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 29 | import com.android.incallui.call.CallList; |
| 30 | import com.android.incallui.call.DialerCall; |
yueg | b26c1ae | 2017-09-18 16:59:16 -0700 | [diff] [blame] | 31 | import com.android.incallui.call.TelecomAdapter; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 32 | |
| 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 | */ |
| 37 | public 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"; |
yueg | b26c1ae | 2017-09-18 16:59:16 -0700 | [diff] [blame] | 57 | 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 Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 60 | |
| 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)) { |
yueg | 01a964d | 2017-10-03 15:25:41 -0700 | [diff] [blame^] | 75 | answerIncomingCall(VideoProfile.STATE_BIDIRECTIONAL); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 76 | } else if (action.equals(ACTION_ANSWER_VOICE_INCOMING_CALL)) { |
yueg | 01a964d | 2017-10-03 15:25:41 -0700 | [diff] [blame^] | 77 | answerIncomingCall(VideoProfile.STATE_AUDIO_ONLY); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 78 | } else if (action.equals(ACTION_DECLINE_INCOMING_CALL)) { |
| 79 | Logger.get(context) |
| 80 | .logImpression(DialerImpression.Type.REJECT_INCOMING_CALL_FROM_NOTIFICATION); |
yueg | 01a964d | 2017-10-03 15:25:41 -0700 | [diff] [blame^] | 81 | declineIncomingCall(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 82 | } else if (action.equals(ACTION_HANG_UP_ONGOING_CALL)) { |
yueg | 01a964d | 2017-10-03 15:25:41 -0700 | [diff] [blame^] | 83 | hangUpOngoingCall(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 84 | } else if (action.equals(ACTION_ACCEPT_VIDEO_UPGRADE_REQUEST)) { |
| 85 | acceptUpgradeRequest(context); |
| 86 | } else if (action.equals(ACTION_DECLINE_VIDEO_UPGRADE_REQUEST)) { |
yueg | 01a964d | 2017-10-03 15:25:41 -0700 | [diff] [blame^] | 87 | declineUpgradeRequest(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 88 | } 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); |
yueg | b26c1ae | 2017-09-18 16:59:16 -0700 | [diff] [blame] | 92 | } 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 Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
| 99 | private void acceptUpgradeRequest(Context context) { |
| 100 | CallList callList = InCallPresenter.getInstance().getCallList(); |
| 101 | if (callList == null) { |
yueg | 01a964d | 2017-10-03 15:25:41 -0700 | [diff] [blame^] | 102 | StatusBarNotifier.clearAllCallNotifications(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 103 | LogUtil.e("NotificationBroadcastReceiver.acceptUpgradeRequest", "call list is empty"); |
| 104 | } else { |
| 105 | DialerCall call = callList.getVideoUpgradeRequestCall(); |
| 106 | if (call != null) { |
twyen | 5920980 | 2017-09-13 10:37:01 -0700 | [diff] [blame] | 107 | call.getVideoTech().acceptVideoRequest(context); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
yueg | 01a964d | 2017-10-03 15:25:41 -0700 | [diff] [blame^] | 112 | private void declineUpgradeRequest() { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 113 | CallList callList = InCallPresenter.getInstance().getCallList(); |
| 114 | if (callList == null) { |
yueg | 01a964d | 2017-10-03 15:25:41 -0700 | [diff] [blame^] | 115 | StatusBarNotifier.clearAllCallNotifications(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 116 | LogUtil.e("NotificationBroadcastReceiver.declineUpgradeRequest", "call list is empty"); |
| 117 | } else { |
| 118 | DialerCall call = callList.getVideoUpgradeRequestCall(); |
| 119 | if (call != null) { |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 120 | call.getVideoTech().declineVideoRequest(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
yueg | 01a964d | 2017-10-03 15:25:41 -0700 | [diff] [blame^] | 125 | private void hangUpOngoingCall() { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 126 | CallList callList = InCallPresenter.getInstance().getCallList(); |
| 127 | if (callList == null) { |
yueg | 01a964d | 2017-10-03 15:25:41 -0700 | [diff] [blame^] | 128 | StatusBarNotifier.clearAllCallNotifications(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 129 | 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 | |
yueg | 01a964d | 2017-10-03 15:25:41 -0700 | [diff] [blame^] | 143 | private void answerIncomingCall(int videoState) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 144 | CallList callList = InCallPresenter.getInstance().getCallList(); |
| 145 | if (callList == null) { |
yueg | 01a964d | 2017-10-03 15:25:41 -0700 | [diff] [blame^] | 146 | StatusBarNotifier.clearAllCallNotifications(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 147 | 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 Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 153 | .showInCall(false /* showDialpad */, false /* newOutgoingCall */); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
yueg | 01a964d | 2017-10-03 15:25:41 -0700 | [diff] [blame^] | 158 | private void declineIncomingCall() { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 159 | CallList callList = InCallPresenter.getInstance().getCallList(); |
| 160 | if (callList == null) { |
yueg | 01a964d | 2017-10-03 15:25:41 -0700 | [diff] [blame^] | 161 | StatusBarNotifier.clearAllCallNotifications(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 162 | 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 | } |