1 /*
2  * Copyright (C) 2016 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.video.impl;
18 
19 import android.support.annotation.DrawableRes;
20 import android.support.annotation.NonNull;
21 import android.support.annotation.StringRes;
22 import android.telecom.CallAudioState;
23 import android.view.View;
24 import android.view.View.OnClickListener;
25 import com.android.dialer.common.Assert;
26 import com.android.dialer.common.LogUtil;
27 import com.android.incallui.incall.protocol.InCallButtonUiDelegate;
28 import com.android.incallui.video.impl.CheckableImageButton.OnCheckedChangeListener;
29 import com.android.incallui.video.protocol.VideoCallScreenDelegate;
30 
31 /** Manages a single button. */
32 public class SpeakerButtonController implements OnCheckedChangeListener, OnClickListener {
33 
34   @NonNull private final InCallButtonUiDelegate inCallButtonUiDelegate;
35   @NonNull private final VideoCallScreenDelegate videoCallScreenDelegate;
36 
37   @NonNull private CheckableImageButton button;
38 
39   @DrawableRes private int icon = R.drawable.quantum_ic_volume_up_vd_theme_24;
40 
41   private boolean isChecked;
42   private boolean checkable;
43   private boolean isEnabled;
44   private CharSequence contentDescription;
45 
SpeakerButtonController( @onNull CheckableImageButton button, @NonNull InCallButtonUiDelegate inCallButtonUiDelegate, @NonNull VideoCallScreenDelegate videoCallScreenDelegate)46   SpeakerButtonController(
47       @NonNull CheckableImageButton button,
48       @NonNull InCallButtonUiDelegate inCallButtonUiDelegate,
49       @NonNull VideoCallScreenDelegate videoCallScreenDelegate) {
50     this.inCallButtonUiDelegate = Assert.isNotNull(inCallButtonUiDelegate);
51     this.videoCallScreenDelegate = Assert.isNotNull(videoCallScreenDelegate);
52     this.button = Assert.isNotNull(button);
53   }
54 
setEnabled(boolean isEnabled)55   public void setEnabled(boolean isEnabled) {
56     this.isEnabled = isEnabled;
57     updateButtonState();
58   }
59 
updateButtonState()60   void updateButtonState() {
61     button.setVisibility(View.VISIBLE);
62     button.setEnabled(isEnabled);
63     button.setChecked(isChecked);
64     button.setOnClickListener(checkable ? null : this);
65     button.setOnCheckedChangeListener(checkable ? this : null);
66     button.setImageResource(icon);
67     button.setContentDescription(contentDescription);
68   }
69 
setAudioState(CallAudioState audioState)70   public void setAudioState(CallAudioState audioState) {
71     LogUtil.i("SpeakerButtonController.setSupportedAudio", "audioState: " + audioState);
72 
73     @StringRes int contentDescriptionResId;
74     if ((audioState.getSupportedRouteMask() & CallAudioState.ROUTE_BLUETOOTH)
75         == CallAudioState.ROUTE_BLUETOOTH) {
76       checkable = false;
77       isChecked = false;
78 
79       if ((audioState.getRoute() & CallAudioState.ROUTE_BLUETOOTH)
80           == CallAudioState.ROUTE_BLUETOOTH) {
81         icon = R.drawable.quantum_ic_bluetooth_audio_vd_theme_24;
82         contentDescriptionResId = R.string.incall_content_description_bluetooth;
83       } else if ((audioState.getRoute() & CallAudioState.ROUTE_SPEAKER)
84           == CallAudioState.ROUTE_SPEAKER) {
85         icon = R.drawable.quantum_ic_volume_up_vd_theme_24;
86         contentDescriptionResId = R.string.incall_content_description_speaker;
87       } else if ((audioState.getRoute() & CallAudioState.ROUTE_WIRED_HEADSET)
88           == CallAudioState.ROUTE_WIRED_HEADSET) {
89         icon = R.drawable.quantum_ic_headset_vd_theme_24;
90         contentDescriptionResId = R.string.incall_content_description_headset;
91       } else {
92         icon = R.drawable.quantum_ic_phone_in_talk_vd_theme_24;
93         contentDescriptionResId = R.string.incall_content_description_earpiece;
94       }
95     } else {
96       checkable = true;
97       isChecked = audioState.getRoute() == CallAudioState.ROUTE_SPEAKER;
98       icon = R.drawable.quantum_ic_volume_up_vd_theme_24;
99       contentDescriptionResId = R.string.incall_content_description_speaker;
100     }
101 
102     contentDescription = button.getContext().getText(contentDescriptionResId);
103     updateButtonState();
104   }
105 
106   @Override
onCheckedChanged(CheckableImageButton button, boolean isChecked)107   public void onCheckedChanged(CheckableImageButton button, boolean isChecked) {
108     LogUtil.i("SpeakerButtonController.onCheckedChanged", null);
109     inCallButtonUiDelegate.toggleSpeakerphone();
110     videoCallScreenDelegate.resetAutoFullscreenTimer();
111   }
112 
113   @Override
onClick(View view)114   public void onClick(View view) {
115     LogUtil.i("SpeakerButtonController.onClick", null);
116     inCallButtonUiDelegate.showAudioRouteSelector();
117     videoCallScreenDelegate.resetAutoFullscreenTimer();
118   }
119 }
120