Home | History | Annotate | Download | only in incallui
      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;
     24 import android.telecom.CallAudioState;
     25 import android.telecom.VideoProfile;
     26 import com.android.dialer.common.LogUtil;
     27 import com.android.dialer.logging.DialerImpression;
     28 import com.android.dialer.logging.Logger;
     29 import com.android.incallui.call.CallList;
     30 import com.android.incallui.call.DialerCall;
     31 import com.android.incallui.call.TelecomAdapter;
     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";
     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";
     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)) {
     75       answerIncomingCall(VideoProfile.STATE_BIDIRECTIONAL);
     76     } else if (action.equals(ACTION_ANSWER_VOICE_INCOMING_CALL)) {
     77       answerIncomingCall(VideoProfile.STATE_AUDIO_ONLY);
     78     } else if (action.equals(ACTION_DECLINE_INCOMING_CALL)) {
     79       Logger.get(context)
     80           .logImpression(DialerImpression.Type.REJECT_INCOMING_CALL_FROM_NOTIFICATION);
     81       declineIncomingCall();
     82     } else if (action.equals(ACTION_HANG_UP_ONGOING_CALL)) {
     83       hangUpOngoingCall();
     84     } else if (action.equals(ACTION_ACCEPT_VIDEO_UPGRADE_REQUEST)) {
     85       acceptUpgradeRequest(context);
     86     } else if (action.equals(ACTION_DECLINE_VIDEO_UPGRADE_REQUEST)) {
     87       declineUpgradeRequest();
     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);
     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);
     96     }
     97   }
     98 
     99   private void acceptUpgradeRequest(Context context) {
    100     CallList callList = InCallPresenter.getInstance().getCallList();
    101     if (callList == null) {
    102       StatusBarNotifier.clearAllCallNotifications();
    103       LogUtil.e("NotificationBroadcastReceiver.acceptUpgradeRequest", "call list is empty");
    104     } else {
    105       DialerCall call = callList.getVideoUpgradeRequestCall();
    106       if (call != null) {
    107         call.getVideoTech().acceptVideoRequest(context);
    108       }
    109     }
    110   }
    111 
    112   private void declineUpgradeRequest() {
    113     CallList callList = InCallPresenter.getInstance().getCallList();
    114     if (callList == null) {
    115       StatusBarNotifier.clearAllCallNotifications();
    116       LogUtil.e("NotificationBroadcastReceiver.declineUpgradeRequest", "call list is empty");
    117     } else {
    118       DialerCall call = callList.getVideoUpgradeRequestCall();
    119       if (call != null) {
    120         call.getVideoTech().declineVideoRequest();
    121       }
    122     }
    123   }
    124 
    125   private void hangUpOngoingCall() {
    126     CallList callList = InCallPresenter.getInstance().getCallList();
    127     if (callList == null) {
    128       StatusBarNotifier.clearAllCallNotifications();
    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 
    143   private void answerIncomingCall(int videoState) {
    144     CallList callList = InCallPresenter.getInstance().getCallList();
    145     if (callList == null) {
    146       StatusBarNotifier.clearAllCallNotifications();
    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()
    153             .showInCall(false /* showDialpad */, false /* newOutgoingCall */);
    154       }
    155     }
    156   }
    157 
    158   private void declineIncomingCall() {
    159     CallList callList = InCallPresenter.getInstance().getCallList();
    160     if (callList == null) {
    161       StatusBarNotifier.clearAllCallNotifications();
    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 }
    171