1 /*
2  * Copyright (C) 2017 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.telecom.CallAudioState;
23 import com.android.dialer.common.Assert;
24 import com.android.dialer.common.LogUtil;
25 import com.android.dialer.logging.DialerImpression;
26 import com.android.dialer.logging.Logger;
27 import com.android.incallui.audiomode.AudioModeProvider;
28 import com.android.incallui.call.CallList;
29 import com.android.incallui.call.DialerCall;
30 import com.android.incallui.call.TelecomAdapter;
31 
32 /** Handles clicks on the return-to-call bubble */
33 public class ReturnToCallActionReceiver extends BroadcastReceiver {
34 
35   public static final String ACTION_RETURN_TO_CALL = "returnToCallV2";
36   public static final String ACTION_TOGGLE_SPEAKER = "toggleSpeakerV2";
37   public static final String ACTION_SHOW_AUDIO_ROUTE_SELECTOR = "showAudioRouteSelectorV2";
38   public static final String ACTION_TOGGLE_MUTE = "toggleMuteV2";
39   public static final String ACTION_END_CALL = "endCallV2";
40 
41   @Override
onReceive(Context context, Intent intent)42   public void onReceive(Context context, Intent intent) {
43     switch (intent.getAction()) {
44       case ACTION_RETURN_TO_CALL:
45         returnToCall(context);
46         break;
47       case ACTION_TOGGLE_SPEAKER:
48         toggleSpeaker(context);
49         break;
50       case ACTION_SHOW_AUDIO_ROUTE_SELECTOR:
51         showAudioRouteSelector(context);
52         break;
53       case ACTION_TOGGLE_MUTE:
54         toggleMute(context);
55         break;
56       case ACTION_END_CALL:
57         endCall(context);
58         break;
59       default:
60         throw Assert.createIllegalStateFailException(
61             "Invalid intent action: " + intent.getAction());
62     }
63   }
64 
returnToCall(Context context)65   private void returnToCall(Context context) {
66     DialerCall call = getCall();
67     Logger.get(context)
68         .logCallImpression(
69             DialerImpression.Type.BUBBLE_V2_RETURN_TO_CALL,
70             call != null ? call.getUniqueCallId() : "",
71             call != null ? call.getTimeAddedMs() : 0);
72 
73     Intent activityIntent = InCallActivity.getIntent(context, false, false, false);
74     activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
75     context.startActivity(activityIntent);
76   }
77 
toggleSpeaker(Context context)78   private void toggleSpeaker(Context context) {
79     CallAudioState audioState = AudioModeProvider.getInstance().getAudioState();
80 
81     if ((audioState.getSupportedRouteMask() & CallAudioState.ROUTE_BLUETOOTH)
82         == CallAudioState.ROUTE_BLUETOOTH) {
83       LogUtil.w(
84           "ReturnToCallActionReceiver.toggleSpeaker",
85           "toggleSpeaker() called when bluetooth available."
86               + " Probably should have shown audio route selector");
87     }
88 
89     DialerCall call = getCall();
90 
91     int newRoute;
92     if (audioState.getRoute() == CallAudioState.ROUTE_SPEAKER) {
93       newRoute = CallAudioState.ROUTE_WIRED_OR_EARPIECE;
94       Logger.get(context)
95           .logCallImpression(
96               DialerImpression.Type.BUBBLE_V2_WIRED_OR_EARPIECE,
97               call != null ? call.getUniqueCallId() : "",
98               call != null ? call.getTimeAddedMs() : 0);
99     } else {
100       newRoute = CallAudioState.ROUTE_SPEAKER;
101       Logger.get(context)
102           .logCallImpression(
103               DialerImpression.Type.BUBBLE_V2_SPEAKERPHONE,
104               call != null ? call.getUniqueCallId() : "",
105               call != null ? call.getTimeAddedMs() : 0);
106     }
107     TelecomAdapter.getInstance().setAudioRoute(newRoute);
108   }
109 
showAudioRouteSelector(Context context)110   public void showAudioRouteSelector(Context context) {
111     Intent intent = new Intent(context, AudioRouteSelectorActivity.class);
112     intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
113     context.startActivity(intent);
114   }
115 
toggleMute(Context context)116   private void toggleMute(Context context) {
117     DialerCall call = getCall();
118     boolean shouldMute = !AudioModeProvider.getInstance().getAudioState().isMuted();
119     Logger.get(context)
120         .logCallImpression(
121             shouldMute
122                 ? DialerImpression.Type.BUBBLE_V2_MUTE_CALL
123                 : DialerImpression.Type.BUBBLE_V2_UNMUTE_CALL,
124             call != null ? call.getUniqueCallId() : "",
125             call != null ? call.getTimeAddedMs() : 0);
126     TelecomAdapter.getInstance().mute(shouldMute);
127   }
128 
endCall(Context context)129   private void endCall(Context context) {
130     DialerCall call = getCall();
131 
132     Logger.get(context)
133         .logCallImpression(
134             DialerImpression.Type.BUBBLE_V2_END_CALL,
135             call != null ? call.getUniqueCallId() : "",
136             call != null ? call.getTimeAddedMs() : 0);
137     if (call != null) {
138       call.disconnect();
139     }
140   }
141 
getCall()142   private DialerCall getCall() {
143     CallList callList = InCallPresenter.getInstance().getCallList();
144     if (callList != null) {
145       DialerCall call = callList.getOutgoingCall();
146       if (call == null) {
147         call = callList.getActiveOrBackgroundCall();
148       }
149       if (call != null) {
150         return call;
151       }
152     }
153     return null;
154   }
155 }
156