1 /*
2  * Copyright (C) 2019 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.systemui.volume;
18 
19 import android.content.Context;
20 import android.os.Handler;
21 import android.util.AttributeSet;
22 import android.view.GestureDetector;
23 import android.view.MotionEvent;
24 
25 import androidx.core.view.ViewCompat;
26 import androidx.core.view.accessibility.AccessibilityNodeInfoCompat.AccessibilityActionCompat;
27 
28 import com.android.keyguard.AlphaOptimizedImageButton;
29 import com.android.systemui.res.R;
30 
31 /** Toggle button in Volume Dialog for controlling system captions state */
32 public class CaptionsToggleImageButton extends AlphaOptimizedImageButton {
33 
34     private ConfirmedTapListener mConfirmedTapListener;
35     private boolean mCaptionsEnabled = false;
36 
37     private GestureDetector mGestureDetector;
38     private GestureDetector.SimpleOnGestureListener mGestureListener =
39             new GestureDetector.SimpleOnGestureListener() {
40         @Override
41         public boolean onSingleTapConfirmed(MotionEvent e) {
42             return tryToSendTapConfirmedEvent();
43         }
44     };
45 
CaptionsToggleImageButton(Context context, AttributeSet attrs)46     public CaptionsToggleImageButton(Context context, AttributeSet attrs) {
47         super(context, attrs);
48         this.setContentDescription(
49                 getContext().getString(R.string.volume_odi_captions_content_description));
50     }
51 
52     @Override
onTouchEvent(MotionEvent event)53     public boolean onTouchEvent(MotionEvent event) {
54         if (mGestureDetector != null) mGestureDetector.onTouchEvent(event);
55         return super.onTouchEvent(event);
56     }
57 
58     @Override
onCreateDrawableState(int extraSpace)59     public int[] onCreateDrawableState(int extraSpace) {
60         return super.onCreateDrawableState(extraSpace + 1);
61     }
62 
setCaptionsEnabled(boolean areCaptionsEnabled)63     Runnable setCaptionsEnabled(boolean areCaptionsEnabled) {
64         this.mCaptionsEnabled = areCaptionsEnabled;
65 
66         ViewCompat.replaceAccessibilityAction(
67                 this,
68                 AccessibilityActionCompat.ACTION_CLICK,
69                 mCaptionsEnabled
70                         ? getContext().getString(R.string.volume_odi_captions_hint_disable)
71                         : getContext().getString(R.string.volume_odi_captions_hint_enable),
72                 (view, commandArguments) -> tryToSendTapConfirmedEvent());
73 
74         return this.setImageResourceAsync(mCaptionsEnabled
75                 ? R.drawable.ic_volume_odi_captions
76                 : R.drawable.ic_volume_odi_captions_disabled);
77     }
78 
tryToSendTapConfirmedEvent()79     private boolean tryToSendTapConfirmedEvent() {
80         if (mConfirmedTapListener != null) {
81             mConfirmedTapListener.onConfirmedTap();
82             return true;
83         }
84         return false;
85     }
86 
getCaptionsEnabled()87     boolean getCaptionsEnabled() {
88         return this.mCaptionsEnabled;
89     }
90 
setOnConfirmedTapListener(ConfirmedTapListener listener, Handler handler)91     void setOnConfirmedTapListener(ConfirmedTapListener listener, Handler handler) {
92         mConfirmedTapListener = listener;
93 
94         if (mGestureDetector == null) {
95             this.mGestureDetector = new GestureDetector(getContext(), mGestureListener, handler);
96         }
97     }
98 
99     /** Listener for confirmed taps rather than normal on click listener. */
100     interface ConfirmedTapListener {
onConfirmedTap()101         void onConfirmedTap();
102     }
103 }
104