-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathFocusAwareSeekBar.java
More file actions
147 lines (116 loc) · 4.43 KB
/
FocusAwareSeekBar.java
File metadata and controls
147 lines (116 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
* Copyright (C) Eltex ltd 2019 <eltex@eltex-co.ru>
* FocusAwareDrawerLayout.java is part of NewPipe.
*
* NewPipe is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* NewPipe is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with NewPipe. If not, see <http://www.gnu.org/licenses/>.
*/
package org.schabi.newpipe.views;
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.ViewTreeObserver;
import android.widget.SeekBar;
import androidx.appcompat.widget.AppCompatSeekBar;
import org.schabi.newpipe.util.DeviceUtils;
/**
* SeekBar, adapted for directional navigation. It emulates touch-related callbacks
* (onStartTrackingTouch/onStopTrackingTouch), so existing code does not need to be changed to
* work with it.
*/
public class FocusAwareSeekBar extends AppCompatSeekBar {
private NestedListener listener;
private ViewTreeObserver treeObserver;
public FocusAwareSeekBar(final Context context) {
super(context);
}
public FocusAwareSeekBar(final Context context, final AttributeSet attrs) {
super(context, attrs);
}
public FocusAwareSeekBar(final Context context, final AttributeSet attrs,
final int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void setOnSeekBarChangeListener(final OnSeekBarChangeListener l) {
this.listener = l == null ? null : new NestedListener(l);
super.setOnSeekBarChangeListener(listener);
}
@Override
public boolean onKeyDown(final int keyCode, final KeyEvent event) {
if (!isInTouchMode() && DeviceUtils.isConfirmKey(keyCode)) {
releaseTrack();
}
return super.onKeyDown(keyCode, event);
}
@Override
protected void onFocusChanged(final boolean gainFocus, final int direction,
final Rect previouslyFocusedRect) {
super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
if (!isInTouchMode() && !gainFocus) {
releaseTrack();
}
}
private final ViewTreeObserver.OnTouchModeChangeListener touchModeListener = isInTouchMode -> {
if (isInTouchMode) {
releaseTrack();
}
};
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
treeObserver = getViewTreeObserver();
treeObserver.addOnTouchModeChangeListener(touchModeListener);
}
@Override
protected void onDetachedFromWindow() {
if (treeObserver == null || !treeObserver.isAlive()) {
treeObserver = getViewTreeObserver();
}
treeObserver.removeOnTouchModeChangeListener(touchModeListener);
treeObserver = null;
super.onDetachedFromWindow();
}
private void releaseTrack() {
if (listener != null && listener.isSeeking) {
listener.onStopTrackingTouch(this);
}
}
private static final class NestedListener implements OnSeekBarChangeListener {
private final OnSeekBarChangeListener delegate;
boolean isSeeking;
private NestedListener(final OnSeekBarChangeListener delegate) {
this.delegate = delegate;
}
@Override
public void onProgressChanged(final SeekBar seekBar, final int progress,
final boolean fromUser) {
if (!seekBar.isInTouchMode() && !isSeeking && fromUser) {
isSeeking = true;
onStartTrackingTouch(seekBar);
}
delegate.onProgressChanged(seekBar, progress, fromUser);
}
@Override
public void onStartTrackingTouch(final SeekBar seekBar) {
isSeeking = true;
delegate.onStartTrackingTouch(seekBar);
}
@Override
public void onStopTrackingTouch(final SeekBar seekBar) {
isSeeking = false;
delegate.onStopTrackingTouch(seekBar);
}
}
}