1 /*
2  * Copyright (C) 2018 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.rtt.impl;
18 
19 import android.content.Context;
20 import android.telecom.CallAudioState;
21 import android.view.View;
22 import android.widget.PopupWindow;
23 import com.android.incallui.incall.protocol.InCallButtonUiDelegate;
24 import com.android.incallui.incall.protocol.InCallScreenDelegate;
25 import com.android.incallui.rtt.impl.RttCheckableButton.OnCheckedChangeListener;
26 import com.android.incallui.speakerbuttonlogic.SpeakerButtonInfo;
27 
28 /** Overflow menu for RTT call. */
29 public class RttOverflowMenu extends PopupWindow implements OnCheckedChangeListener {
30 
31   private final RttCheckableButton muteButton;
32   private final RttCheckableButton speakerButton;
33   private final RttCheckableButton dialpadButton;
34   private final RttCheckableButton addCallButton;
35   private final RttCheckableButton swapCallButton;
36   private final InCallButtonUiDelegate inCallButtonUiDelegate;
37   private final InCallScreenDelegate inCallScreenDelegate;
38   private boolean isSwitchToSecondaryButtonEnabled;
39   private boolean isSwapCallButtonEnabled;
40 
RttOverflowMenu( Context context, InCallButtonUiDelegate inCallButtonUiDelegate, InCallScreenDelegate inCallScreenDelegate)41   RttOverflowMenu(
42       Context context,
43       InCallButtonUiDelegate inCallButtonUiDelegate,
44       InCallScreenDelegate inCallScreenDelegate) {
45     super(context, null, 0, R.style.OverflowMenu);
46     this.inCallButtonUiDelegate = inCallButtonUiDelegate;
47     this.inCallScreenDelegate = inCallScreenDelegate;
48     View view = View.inflate(context, R.layout.overflow_menu, null);
49     setContentView(view);
50     setOnDismissListener(this::dismiss);
51     setFocusable(true);
52     setWidth(context.getResources().getDimensionPixelSize(R.dimen.rtt_overflow_menu_width));
53     muteButton = view.findViewById(R.id.menu_mute);
54     muteButton.setOnCheckedChangeListener(this);
55     speakerButton = view.findViewById(R.id.menu_speaker);
56     speakerButton.setOnCheckedChangeListener(this);
57     dialpadButton = view.findViewById(R.id.menu_keypad);
58     dialpadButton.setOnCheckedChangeListener(this);
59     addCallButton = view.findViewById(R.id.menu_add_call);
60     addCallButton.setOnClickListener(v -> this.inCallButtonUiDelegate.addCallClicked());
61     swapCallButton = view.findViewById(R.id.menu_swap_call);
62     swapCallButton.setOnClickListener(
63         v -> {
64           if (isSwapCallButtonEnabled) {
65             this.inCallButtonUiDelegate.swapClicked();
66           }
67           if (isSwitchToSecondaryButtonEnabled) {
68             this.inCallScreenDelegate.onSecondaryInfoClicked();
69           }
70         });
71   }
72 
73   @Override
onCheckedChanged(RttCheckableButton button, boolean isChecked)74   public void onCheckedChanged(RttCheckableButton button, boolean isChecked) {
75     if (button == muteButton) {
76       inCallButtonUiDelegate.muteClicked(isChecked, true);
77     } else if (button == speakerButton) {
78       inCallButtonUiDelegate.toggleSpeakerphone();
79     } else if (button == dialpadButton) {
80       inCallButtonUiDelegate.showDialpadClicked(isChecked);
81     }
82   }
83 
setMuteButtonChecked(boolean isChecked)84   void setMuteButtonChecked(boolean isChecked) {
85     muteButton.setChecked(isChecked);
86   }
87 
setAudioState(CallAudioState audioState)88   void setAudioState(CallAudioState audioState) {
89     SpeakerButtonInfo info = new SpeakerButtonInfo(audioState);
90     if (info.nonBluetoothMode) {
91       speakerButton.setChecked(info.isChecked);
92       speakerButton.setOnClickListener(null);
93       speakerButton.setOnCheckedChangeListener(this);
94     } else {
95       speakerButton.setText(info.label);
96       speakerButton.setCompoundDrawablesWithIntrinsicBounds(info.icon, 0, 0, 0);
97       speakerButton.setOnClickListener(
98           v -> {
99             inCallButtonUiDelegate.showAudioRouteSelector();
100             dismiss();
101           });
102       speakerButton.setOnCheckedChangeListener(null);
103     }
104   }
105 
setDialpadButtonChecked(boolean isChecked)106   void setDialpadButtonChecked(boolean isChecked) {
107     dialpadButton.setChecked(isChecked);
108   }
109 
enableSwapCallButton(boolean enabled)110   void enableSwapCallButton(boolean enabled) {
111     isSwapCallButtonEnabled = enabled;
112     swapCallButton.setVisibility(
113         isSwapCallButtonEnabled || isSwitchToSecondaryButtonEnabled ? View.VISIBLE : View.GONE);
114   }
115 
enableSwitchToSecondaryButton(boolean enabled)116   void enableSwitchToSecondaryButton(boolean enabled) {
117     isSwitchToSecondaryButtonEnabled = enabled;
118     swapCallButton.setVisibility(
119         isSwapCallButtonEnabled || isSwitchToSecondaryButtonEnabled ? View.VISIBLE : View.GONE);
120   }
121 }
122