1 /*
2  * Copyright (C) 2022 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.car.qc.view;
18 
19 import android.content.Context;
20 import android.util.AttributeSet;
21 import android.view.MotionEvent;
22 import android.widget.SeekBar;
23 
24 import androidx.annotation.Nullable;
25 
26 import com.android.car.ui.uxr.DrawableStateSeekBar;
27 
28 import java.util.function.Consumer;
29 
30 /**
31  * A {@link SeekBar} specifically for Quick Controls that allows for a disabled click action
32  * to execute on {@link MotionEvent.ACTION_UP}.
33  */
34 public class QCSeekBarView extends DrawableStateSeekBar {
35     private boolean mClickableWhileDisabled;
36     private Consumer<SeekBar> mDisabledClickListener;
37 
QCSeekBarView(Context context)38     public QCSeekBarView(Context context) {
39         super(context);
40     }
41 
QCSeekBarView(Context context, AttributeSet attrs)42     public QCSeekBarView(Context context, AttributeSet attrs) {
43         super(context, attrs);
44     }
45 
QCSeekBarView(Context context, AttributeSet attrs, int defStyleAttr)46     public QCSeekBarView(Context context, AttributeSet attrs, int defStyleAttr) {
47         super(context, attrs, defStyleAttr);
48     }
49 
QCSeekBarView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)50     public QCSeekBarView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
51         super(context, attrs, defStyleAttr, defStyleRes);
52     }
53 
54     @Override
onTouchEvent(MotionEvent event)55     public boolean onTouchEvent(MotionEvent event) {
56         // AbsSeekBar will ignore all touch events if not enabled. If this SeekBar should be
57         // clickable while disabled, the touch event will be handled here.
58         if (!isEnabled() && mClickableWhileDisabled) {
59             if (event.getAction() == MotionEvent.ACTION_UP && mDisabledClickListener != null) {
60                 mDisabledClickListener.accept(this);
61             }
62             return true;
63         }
64         return super.onTouchEvent(event);
65     }
66 
setClickableWhileDisabled(boolean clickable)67     public void setClickableWhileDisabled(boolean clickable) {
68         mClickableWhileDisabled = clickable;
69     }
70 
setDisabledClickListener(@ullable Consumer<SeekBar> disabledClickListener)71     public void setDisabledClickListener(@Nullable Consumer<SeekBar> disabledClickListener) {
72         mDisabledClickListener = disabledClickListener;
73     }
74 
isClickableWhileDisabled()75     public boolean isClickableWhileDisabled() {
76         return mClickableWhileDisabled;
77     }
78 }
79