1ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian/*
2ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian * Copyright (C) 2016 The Android Open Source Project
3ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian *
4ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian * Licensed under the Apache License, Version 2.0 (the "License");
5ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian * you may not use this file except in compliance with the License.
6ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian * You may obtain a copy of the License at
7ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian *
8ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian *      http://www.apache.org/licenses/LICENSE-2.0
9ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian *
10ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian * Unless required by applicable law or agreed to in writing, software
11ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian * distributed under the License is distributed on an "AS IS" BASIS,
12ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian * See the License for the specific language governing permissions and
14ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian * limitations under the License.
15ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian */
16ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian
17ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanianpackage com.android.incallui.video.impl;
18ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian
19ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanianimport android.support.annotation.DrawableRes;
20ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanianimport android.support.annotation.NonNull;
21ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanianimport android.support.annotation.StringRes;
22ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanianimport android.telecom.CallAudioState;
23ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanianimport android.view.View;
24ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanianimport android.view.View.OnClickListener;
25ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanianimport com.android.dialer.common.Assert;
26ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanianimport com.android.dialer.common.LogUtil;
27ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanianimport com.android.incallui.incall.protocol.InCallButtonUiDelegate;
28ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanianimport com.android.incallui.video.impl.CheckableImageButton.OnCheckedChangeListener;
29ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanianimport com.android.incallui.video.protocol.VideoCallScreenDelegate;
30ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian
31ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian/** Manages a single button. */
32ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanianpublic class SpeakerButtonController implements OnCheckedChangeListener, OnClickListener {
33ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian
34ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian  @NonNull private final InCallButtonUiDelegate inCallButtonUiDelegate;
35ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian  @NonNull private final VideoCallScreenDelegate videoCallScreenDelegate;
36ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian
37ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian  @NonNull private CheckableImageButton button;
38ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian
39ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian  @DrawableRes private int icon = R.drawable.quantum_ic_volume_up_white_36;
40ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian
41ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian  private boolean isChecked;
42ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian  private boolean checkable;
43ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian  private boolean isEnabled;
44ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian  private CharSequence contentDescription;
45ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian
46ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian  public SpeakerButtonController(
47ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian      @NonNull CheckableImageButton button,
48ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian      @NonNull InCallButtonUiDelegate inCallButtonUiDelegate,
49ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian      @NonNull VideoCallScreenDelegate videoCallScreenDelegate) {
50ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian    this.inCallButtonUiDelegate = Assert.isNotNull(inCallButtonUiDelegate);
51ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian    this.videoCallScreenDelegate = Assert.isNotNull(videoCallScreenDelegate);
52ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian    this.button = Assert.isNotNull(button);
53ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian  }
54ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian
55ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian  public void setEnabled(boolean isEnabled) {
56ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian    this.isEnabled = isEnabled;
57ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian  }
58ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian
59ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian  public void updateButtonState() {
60ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian    button.setVisibility(View.VISIBLE);
61ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian    button.setEnabled(isEnabled);
62ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian    button.setChecked(isChecked);
63ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian    button.setOnClickListener(checkable ? null : this);
64ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian    button.setOnCheckedChangeListener(checkable ? this : null);
65ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian    button.setImageResource(icon);
66ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian    button.setContentDescription(contentDescription);
67ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian  }
68ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian
69ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian  public void setAudioState(CallAudioState audioState) {
70ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian    LogUtil.i("SpeakerButtonController.setSupportedAudio", "audioState: " + audioState);
71ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian
72ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian    @StringRes int contentDescriptionResId;
73ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian    if ((audioState.getSupportedRouteMask() & CallAudioState.ROUTE_BLUETOOTH)
74ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian        == CallAudioState.ROUTE_BLUETOOTH) {
75ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian      checkable = false;
76ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian      isChecked = false;
77ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian
78ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian      if ((audioState.getRoute() & CallAudioState.ROUTE_BLUETOOTH)
79ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian          == CallAudioState.ROUTE_BLUETOOTH) {
80ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian        icon = R.drawable.quantum_ic_bluetooth_audio_white_36;
81ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian        contentDescriptionResId = R.string.incall_content_description_bluetooth;
82ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian      } else if ((audioState.getRoute() & CallAudioState.ROUTE_SPEAKER)
83ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian          == CallAudioState.ROUTE_SPEAKER) {
84ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian        icon = R.drawable.quantum_ic_volume_up_white_36;
85ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian        contentDescriptionResId = R.string.incall_content_description_speaker;
86ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian      } else if ((audioState.getRoute() & CallAudioState.ROUTE_WIRED_HEADSET)
87ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian          == CallAudioState.ROUTE_WIRED_HEADSET) {
88ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian        icon = R.drawable.quantum_ic_headset_white_36;
89ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian        contentDescriptionResId = R.string.incall_content_description_headset;
90ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian      } else {
912f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian        icon = R.drawable.quantum_ic_phone_in_talk_white_36;
92ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian        contentDescriptionResId = R.string.incall_content_description_earpiece;
93ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian      }
94ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian    } else {
95ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian      checkable = true;
96ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian      isChecked = audioState.getRoute() == CallAudioState.ROUTE_SPEAKER;
97ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian      icon = R.drawable.quantum_ic_volume_up_white_36;
98ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian      contentDescriptionResId = R.string.incall_content_description_speaker;
99ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian    }
100ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian
101ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian    contentDescription = button.getContext().getText(contentDescriptionResId);
102ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian    updateButtonState();
103ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian  }
104ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian
105ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian  @Override
106ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian  public void onCheckedChanged(CheckableImageButton button, boolean isChecked) {
107ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian    LogUtil.i("SpeakerButtonController.onCheckedChanged", null);
108ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian    inCallButtonUiDelegate.toggleSpeakerphone();
109ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian    videoCallScreenDelegate.resetAutoFullscreenTimer();
110ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian  }
111ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian
112ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian  @Override
113ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian  public void onClick(View view) {
114ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian    LogUtil.i("SpeakerButtonController.onClick", null);
115ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian    inCallButtonUiDelegate.showAudioRouteSelector();
116ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian    videoCallScreenDelegate.resetAutoFullscreenTimer();
117ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian  }
118ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian}
119