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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
|
package com.android.systemui.qs;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.drawable.Animatable2;
import android.graphics.drawable.AnimatedVectorDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import com.android.settingslib.Utils;
import com.android.systemui.R;
import java.util.ArrayList;
/**
* Page indicator for using with pageable layouts
*
* Supports {@code android.R.attr.tint}. If missing, it will use the current accent color.
*/
public class PageIndicator extends ViewGroup {
private static final String TAG = "PageIndicator";
private static final boolean DEBUG = false;
private static final long ANIMATION_DURATION = 250;
private static final float MINOR_ALPHA = .42f;
private final ArrayList<Integer> mQueuedPositions = new ArrayList<>();
private final int mPageIndicatorWidth;
private final int mPageIndicatorHeight;
private final int mPageDotWidth;
private @NonNull ColorStateList mTint;
private int mPosition = -1;
private boolean mAnimating;
private final Animatable2.AnimationCallback mAnimationCallback =
new Animatable2.AnimationCallback() {
@Override
public void onAnimationEnd(Drawable drawable) {
super.onAnimationEnd(drawable);
if (DEBUG) Log.d(TAG, "onAnimationEnd - queued: " + mQueuedPositions.size());
if (drawable instanceof AnimatedVectorDrawable) {
((AnimatedVectorDrawable) drawable).unregisterAnimationCallback(
mAnimationCallback);
}
mAnimating = false;
if (mQueuedPositions.size() != 0) {
setPosition(mQueuedPositions.remove(0));
}
}
};
public PageIndicator(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray array = context.obtainStyledAttributes(attrs, new int[]{android.R.attr.tint});
if (array.hasValue(0)) {
mTint = array.getColorStateList(0);
} else {
mTint = Utils.getColorAccent(context);
}
array.recycle();
Resources res = context.getResources();
mPageIndicatorWidth = res.getDimensionPixelSize(R.dimen.qs_page_indicator_width);
mPageIndicatorHeight = res.getDimensionPixelSize(R.dimen.qs_page_indicator_height);
mPageDotWidth = res.getDimensionPixelSize(R.dimen.qs_page_indicator_dot_width);
}
public void setNumPages(int numPages) {
setVisibility(numPages > 1 ? View.VISIBLE : View.GONE);
if (numPages == getChildCount()) {
return;
}
if (mAnimating) {
Log.w(TAG, "setNumPages during animation");
}
while (numPages < getChildCount()) {
removeViewAt(getChildCount() - 1);
}
while (numPages > getChildCount()) {
ImageView v = new ImageView(mContext);
v.setImageResource(R.drawable.minor_a_b);
v.setImageTintList(mTint);
addView(v, new LayoutParams(mPageIndicatorWidth, mPageIndicatorHeight));
}
// Refresh state.
setIndex(mPosition >> 1);
requestLayout();
}
/**
* @return the current tint list for this view.
*/
@NonNull
public ColorStateList getTintList() {
return mTint;
}
/**
* Set the color for this view.
* <br>
* Calling this will change the color of the current view and any new dots that are added to it.
* @param color the new color
*/
public void setTintList(@NonNull ColorStateList color) {
if (color.equals(mTint)) {
return;
}
mTint = color;
final int N = getChildCount();
for (int i = 0; i < N; i++) {
View v = getChildAt(i);
if (v instanceof ImageView) {
((ImageView) v).setImageTintList(mTint);
}
}
}
public void setLocation(float location) {
int index = (int) location;
setContentDescription(getContext().getString(R.string.accessibility_quick_settings_page,
(index + 1), getChildCount()));
int position = index << 1 | ((location != index) ? 1 : 0);
if (DEBUG) Log.d(TAG, "setLocation " + location + " " + index + " " + position);
int lastPosition = mPosition;
if (mQueuedPositions.size() != 0) {
lastPosition = mQueuedPositions.get(mQueuedPositions.size() - 1);
}
if (position == lastPosition) return;
if (mAnimating) {
if (DEBUG) Log.d(TAG, "Queueing transition to " + Integer.toHexString(position));
mQueuedPositions.add(position);
return;
}
setPosition(position);
}
private void setPosition(int position) {
if (isVisibleToUser() && Math.abs(mPosition - position) == 1) {
animate(mPosition, position);
} else {
if (DEBUG) Log.d(TAG, "Skipping animation " + isVisibleToUser() + " " + mPosition
+ " " + position);
setIndex(position >> 1);
}
mPosition = position;
}
private void setIndex(int index) {
final int N = getChildCount();
for (int i = 0; i < N; i++) {
ImageView v = (ImageView) getChildAt(i);
// Clear out any animation positioning.
v.setTranslationX(0);
v.setImageResource(R.drawable.major_a_b);
v.setAlpha(getAlpha(i == index));
}
}
private void animate(int from, int to) {
if (DEBUG) Log.d(TAG, "Animating from " + Integer.toHexString(from) + " to "
+ Integer.toHexString(to));
int fromIndex = from >> 1;
int toIndex = to >> 1;
// Set the position of everything, then we will manually control the two views involved
// in the animation.
setIndex(fromIndex);
boolean fromTransition = (from & 1) != 0;
boolean isAState = fromTransition ? from > to : from < to;
int firstIndex = Math.min(fromIndex, toIndex);
int secondIndex = Math.max(fromIndex, toIndex);
if (secondIndex == firstIndex) {
secondIndex++;
}
ImageView first = (ImageView) getChildAt(firstIndex);
ImageView second = (ImageView) getChildAt(secondIndex);
if (first == null || second == null) {
// may happen during reInflation or other weird cases
return;
}
// Lay the two views on top of each other.
second.setTranslationX(first.getX() - second.getX());
playAnimation(first, getTransition(fromTransition, isAState, false));
first.setAlpha(getAlpha(false));
playAnimation(second, getTransition(fromTransition, isAState, true));
second.setAlpha(getAlpha(true));
mAnimating = true;
}
private float getAlpha(boolean isMajor) {
return isMajor ? 1 : MINOR_ALPHA;
}
private void playAnimation(ImageView imageView, int res) {
final AnimatedVectorDrawable avd = (AnimatedVectorDrawable) getContext().getDrawable(res);
imageView.setImageDrawable(avd);
avd.forceAnimationOnUI();
avd.registerAnimationCallback(mAnimationCallback);
avd.start();
}
private int getTransition(boolean fromB, boolean isMajorAState, boolean isMajor) {
if (isMajor) {
if (fromB) {
if (isMajorAState) {
return R.drawable.major_b_a_animation;
} else {
return R.drawable.major_b_c_animation;
}
} else {
if (isMajorAState) {
return R.drawable.major_a_b_animation;
} else {
return R.drawable.major_c_b_animation;
}
}
} else {
if (fromB) {
if (isMajorAState) {
return R.drawable.minor_b_c_animation;
} else {
return R.drawable.minor_b_a_animation;
}
} else {
if (isMajorAState) {
return R.drawable.minor_c_b_animation;
} else {
return R.drawable.minor_a_b_animation;
}
}
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
final int N = getChildCount();
if (N == 0) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
return;
}
final int widthChildSpec = MeasureSpec.makeMeasureSpec(mPageIndicatorWidth,
MeasureSpec.EXACTLY);
final int heightChildSpec = MeasureSpec.makeMeasureSpec(mPageIndicatorHeight,
MeasureSpec.EXACTLY);
for (int i = 0; i < N; i++) {
getChildAt(i).measure(widthChildSpec, heightChildSpec);
}
int width = (mPageIndicatorWidth - mPageDotWidth) * (N - 1) + mPageDotWidth;
setMeasuredDimension(width, mPageIndicatorHeight);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
final int N = getChildCount();
if (N == 0) {
return;
}
for (int i = 0; i < N; i++) {
int left = (mPageIndicatorWidth - mPageDotWidth) * i;
getChildAt(i).layout(left, 0, mPageIndicatorWidth + left, mPageIndicatorHeight);
}
}
}
|