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