1 /*
2  * Copyright (C) 2020 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.settings.brightness;
18 
19 import static com.android.systemui.Flags.brightnessSliderFocusState;
20 
21 import android.content.Context;
22 import android.util.AttributeSet;
23 import android.view.MotionEvent;
24 import android.view.accessibility.AccessibilityNodeInfo;
25 import android.widget.SeekBar;
26 
27 import com.android.systemui.res.R;
28 
29 public class ToggleSeekBar extends SeekBar {
30     private String mAccessibilityLabel;
31 
32     private AdminBlocker mAdminBlocker;
33 
ToggleSeekBar(Context context)34     public ToggleSeekBar(Context context) {
35         super(context);
36     }
37 
ToggleSeekBar(Context context, AttributeSet attrs)38     public ToggleSeekBar(Context context, AttributeSet attrs) {
39         super(context, attrs);
40     }
41 
ToggleSeekBar(Context context, AttributeSet attrs, int defStyleAttr)42     public ToggleSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
43         super(context, attrs, defStyleAttr);
44     }
45 
46     @Override
onTouchEvent(MotionEvent event)47     public boolean onTouchEvent(MotionEvent event) {
48         if (mAdminBlocker != null && mAdminBlocker.block()) {
49             return true;
50         }
51         if (!isEnabled()) {
52             setEnabled(true);
53         }
54 
55         return super.onTouchEvent(event);
56     }
57 
58     @Override
onHoverEvent(MotionEvent event)59     public boolean onHoverEvent(MotionEvent event) {
60         if (event.getAction() == MotionEvent.ACTION_HOVER_ENTER) {
61             setHovered(true);
62         } else if (event.getAction() == MotionEvent.ACTION_HOVER_EXIT) {
63             setHovered(false);
64         }
65         return true;
66     }
67 
68     @Override
onFinishInflate()69     protected void onFinishInflate() {
70         super.onFinishInflate();
71         if (brightnessSliderFocusState()) {
72             setBackground(mContext.getDrawable(R.drawable.brightness_slider_focus_bg));
73         }
74     }
75 
setAccessibilityLabel(String label)76     public void setAccessibilityLabel(String label) {
77         mAccessibilityLabel = label;
78     }
79 
80     @Override
onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info)81     public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
82         super.onInitializeAccessibilityNodeInfo(info);
83         if (mAccessibilityLabel != null) {
84             info.setText(mAccessibilityLabel);
85         }
86     }
87 
setAdminBlocker(AdminBlocker blocker)88     void setAdminBlocker(AdminBlocker blocker) {
89         mAdminBlocker = blocker;
90         setEnabled(blocker == null);
91     }
92 
93     interface AdminBlocker {
block()94         boolean block();
95     }
96 }
97