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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
|
/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License
*/
package com.android.systemui.statusbar.stack;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import com.android.systemui.R;
import com.android.systemui.statusbar.ExpandableNotificationRow;
import com.android.systemui.statusbar.ExpandableView;
import java.util.ArrayList;
import java.util.List;
/**
* A container containing child notifications
*/
public class NotificationChildrenContainer extends ViewGroup {
private final int mChildPadding;
private final int mDividerHeight;
private final int mMaxNotificationHeight;
private final List<View> mDividers = new ArrayList<>();
private final List<ExpandableNotificationRow> mChildren = new ArrayList<>();
private final View mCollapseButton;
private final View mCollapseDivider;
private final int mCollapseButtonHeight;
private final int mNotificationAppearDistance;
public NotificationChildrenContainer(Context context) {
this(context, null);
}
public NotificationChildrenContainer(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public NotificationChildrenContainer(Context context, AttributeSet attrs, int defStyleAttr) {
this(context, attrs, defStyleAttr, 0);
}
public NotificationChildrenContainer(Context context, AttributeSet attrs, int defStyleAttr,
int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
mChildPadding = getResources().getDimensionPixelSize(
R.dimen.notification_children_padding);
mDividerHeight = getResources().getDimensionPixelSize(
R.dimen.notification_children_divider_height);
mMaxNotificationHeight = getResources().getDimensionPixelSize(
R.dimen.notification_max_height);
mNotificationAppearDistance = getResources().getDimensionPixelSize(
R.dimen.notification_appear_distance);
LayoutInflater inflater = mContext.getSystemService(LayoutInflater.class);
mCollapseButton = inflater.inflate(R.layout.notification_collapse_button, this,
false);
mCollapseButtonHeight = getResources().getDimensionPixelSize(
R.dimen.notification_bottom_decor_height);
addView(mCollapseButton);
mCollapseDivider = inflateDivider();
addView(mCollapseDivider);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int childCount = mChildren.size();
boolean firstChild = true;
for (int i = 0; i < childCount; i++) {
View child = mChildren.get(i);
boolean viewGone = child.getVisibility() == View.GONE;
if (i != 0) {
View divider = mDividers.get(i - 1);
int dividerVisibility = divider.getVisibility();
int newVisibility = viewGone ? INVISIBLE : VISIBLE;
if (dividerVisibility != newVisibility) {
divider.setVisibility(newVisibility);
}
}
if (viewGone) {
continue;
}
child.layout(0, 0, getWidth(), child.getMeasuredHeight());
if (!firstChild) {
mDividers.get(i - 1).layout(0, 0, getWidth(), mDividerHeight);
} else {
firstChild = false;
}
}
mCollapseButton.layout(0, 0, getWidth(), mCollapseButtonHeight);
mCollapseDivider.layout(0, mCollapseButtonHeight - mDividerHeight, getWidth(),
mCollapseButtonHeight);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int ownMaxHeight = mMaxNotificationHeight;
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
boolean hasFixedHeight = heightMode == MeasureSpec.EXACTLY;
boolean isHeightLimited = heightMode == MeasureSpec.AT_MOST;
if (hasFixedHeight || isHeightLimited) {
int size = MeasureSpec.getSize(heightMeasureSpec);
ownMaxHeight = Math.min(ownMaxHeight, size);
}
int newHeightSpec = MeasureSpec.makeMeasureSpec(ownMaxHeight, MeasureSpec.AT_MOST);
int dividerHeightSpec = MeasureSpec.makeMeasureSpec(mDividerHeight, MeasureSpec.EXACTLY);
int collapseButtonHeightSpec = MeasureSpec.makeMeasureSpec(mCollapseButtonHeight,
MeasureSpec.EXACTLY);
mCollapseButton.measure(widthMeasureSpec, collapseButtonHeightSpec);
mCollapseDivider.measure(widthMeasureSpec, dividerHeightSpec);
int height = mCollapseButtonHeight;
int childCount = mChildren.size();
boolean firstChild = true;
for (int i = 0; i < childCount; i++) {
View child = mChildren.get(i);
if (child.getVisibility() == View.GONE) {
continue;
}
child.measure(widthMeasureSpec, newHeightSpec);
height += child.getMeasuredHeight();
if (!firstChild) {
// layout the divider
View divider = mDividers.get(i - 1);
divider.measure(widthMeasureSpec, dividerHeightSpec);
height += mChildPadding;
} else {
firstChild = false;
}
}
int width = MeasureSpec.getSize(widthMeasureSpec);
height = hasFixedHeight ? ownMaxHeight
: isHeightLimited ? Math.min(ownMaxHeight, height)
: height;
setMeasuredDimension(width, height);
}
/**
* Add a child notification to this view.
*
* @param row the row to add
* @param childIndex the index to add it at, if -1 it will be added at the end
*/
public void addNotification(ExpandableNotificationRow row, int childIndex) {
int newIndex = childIndex < 0 ? mChildren.size() : childIndex;
mChildren.add(newIndex, row);
addView(row);
if (mChildren.size() != 1) {
View divider = inflateDivider();
addView(divider);
mDividers.add(Math.max(newIndex - 1, 0), divider);
}
// TODO: adapt background corners
// TODO: fix overdraw
}
public void removeNotification(ExpandableNotificationRow row) {
int childIndex = mChildren.indexOf(row);
mChildren.remove(row);
removeView(row);
if (!mDividers.isEmpty()) {
View divider = mDividers.remove(Math.max(childIndex - 1, 0));
removeView(divider);
}
row.setSystemChildExpanded(false);
// TODO: adapt background corners
}
private View inflateDivider() {
return LayoutInflater.from(mContext).inflate(
R.layout.notification_children_divider, this, false);
}
public List<ExpandableNotificationRow> getNotificationChildren() {
return mChildren;
}
/**
* Apply the order given in the list to the children.
*
* @param childOrder the new list order
* @return whether the list order has changed
*/
public boolean applyChildOrder(List<ExpandableNotificationRow> childOrder) {
if (childOrder == null) {
return false;
}
boolean result = false;
for (int i = 0; i < mChildren.size() && i < childOrder.size(); i++) {
ExpandableNotificationRow child = mChildren.get(i);
ExpandableNotificationRow desiredChild = childOrder.get(i);
if (child != desiredChild) {
mChildren.remove(desiredChild);
mChildren.add(i, desiredChild);
result = true;
}
}
// Let's make the first child expanded!
boolean first = true;
for (int i = 0; i < childOrder.size(); i++) {
ExpandableNotificationRow child = childOrder.get(i);
child.setSystemChildExpanded(first);
first = false;
}
return result;
}
public int getIntrinsicHeight() {
int childCount = mChildren.size();
int intrinsicHeight = 0;
int visibleChildren = 0;
for (int i = 0; i < childCount; i++) {
ExpandableNotificationRow child = mChildren.get(i);
if (child.getVisibility() == View.GONE) {
continue;
}
intrinsicHeight += child.getIntrinsicHeight();
visibleChildren++;
}
if (visibleChildren > 0) {
intrinsicHeight += (visibleChildren - 1) * mDividerHeight;
}
return intrinsicHeight;
}
/**
* Update the state of all its children based on a linear layout algorithm.
*
* @param resultState the state to update
* @param parentState the state of the parent
*/
public void getState(StackScrollState resultState, StackViewState parentState) {
int childCount = mChildren.size();
int yPosition = mCollapseButtonHeight;
boolean firstChild = true;
for (int i = 0; i < childCount; i++) {
ExpandableNotificationRow child = mChildren.get(i);
if (child.getVisibility() == View.GONE) {
continue;
}
if (!firstChild) {
// There's a divider
yPosition += mChildPadding;
} else {
firstChild = false;
}
StackViewState childState = resultState.getViewStateForView(child);
int intrinsicHeight = child.getIntrinsicHeight();
childState.yTranslation = yPosition;
childState.zTranslation = 0;
childState.height = intrinsicHeight;
childState.dimmed = parentState.dimmed;
childState.dark = parentState.dark;
childState.hideSensitive = parentState.hideSensitive;
childState.belowSpeedBump = parentState.belowSpeedBump;
childState.scale = parentState.scale;
childState.clipTopAmount = 0;
childState.topOverLap = 0;
childState.location = parentState.location;
yPosition += intrinsicHeight;
}
}
public void applyState(StackScrollState state) {
int childCount = mChildren.size();
boolean firstChild = true;
ViewState dividerState = new ViewState();
for (int i = 0; i < childCount; i++) {
ExpandableNotificationRow child = mChildren.get(i);
StackViewState viewState = state.getViewStateForView(child);
if (child.getVisibility() == View.GONE) {
continue;
}
if (!firstChild) {
// layout the divider
View divider = mDividers.get(i - 1);
dividerState.initFrom(divider);
dividerState.yTranslation = (int) (viewState.yTranslation
- (mChildPadding + mDividerHeight) / 2.0f);
dividerState.alpha = 1;
state.applyViewState(divider, dividerState);
} else {
firstChild = false;
}
state.applyState(child, viewState);
}
}
public void setCollapseClickListener(OnClickListener collapseClickListener) {
mCollapseButton.setOnClickListener(collapseClickListener);
}
/**
* This is called when the children expansion has changed and positions the children properly
* for an appear animation.
*
* @param state the new state we animate to
*/
public void prepareExpansionChanged(StackScrollState state) {
int childCount = mChildren.size();
boolean firstChild = true;
StackViewState sourceState = new StackViewState();
ViewState dividerState = new ViewState();
for (int i = 0; i < childCount; i++) {
ExpandableNotificationRow child = mChildren.get(i);
StackViewState viewState = state.getViewStateForView(child);
if (child.getVisibility() == View.GONE) {
continue;
}
if (!firstChild) {
// layout the divider
View divider = mDividers.get(i - 1);
dividerState.initFrom(divider);
dividerState.yTranslation = viewState.yTranslation
- (mChildPadding + mDividerHeight) / 2.0f + mNotificationAppearDistance;
dividerState.alpha = 0;
state.applyViewState(divider, dividerState);
} else {
firstChild = false;
}
sourceState.copyFrom(viewState);
sourceState.alpha = 0;
sourceState.yTranslation += mNotificationAppearDistance;
state.applyState(child, sourceState);
}
mCollapseButton.setAlpha(0);
mCollapseDivider.setAlpha(0);
mCollapseDivider.setTranslationY(mNotificationAppearDistance / 4);
}
public void startAnimationToState(StackScrollState state, StackStateAnimator stateAnimator,
boolean withDelays, long baseDelay, long duration) {
int childCount = mChildren.size();
boolean firstChild = true;
ViewState dividerState = new ViewState();
int notGoneIndex = 0;
for (int i = 0; i < childCount; i++) {
ExpandableNotificationRow child = mChildren.get(i);
StackViewState viewState = state.getViewStateForView(child);
if (child.getVisibility() == View.GONE) {
continue;
}
int difference = Math.min(StackStateAnimator.DELAY_EFFECT_MAX_INDEX_DIFFERENCE_CHILDREN,
notGoneIndex + 1);
long delay = withDelays
? difference * StackStateAnimator.ANIMATION_DELAY_PER_ELEMENT_EXPAND_CHILDREN
: 0;
delay += baseDelay;
if (!firstChild) {
// layout the divider
View divider = mDividers.get(i - 1);
dividerState.initFrom(divider);
dividerState.yTranslation = viewState.yTranslation
- (mChildPadding + mDividerHeight) / 2.0f;
dividerState.alpha = 1;
stateAnimator.startViewAnimations(divider, dividerState, delay, duration);
} else {
firstChild = false;
}
stateAnimator.startStackAnimations(child, viewState, state, -1, delay);
notGoneIndex++;
}
dividerState.initFrom(mCollapseButton);
dividerState.alpha = 1.0f;
stateAnimator.startViewAnimations(mCollapseButton, dividerState, baseDelay, duration);
dividerState.initFrom(mCollapseDivider);
dividerState.alpha = 1.0f;
dividerState.yTranslation = 0.0f;
stateAnimator.startViewAnimations(mCollapseDivider, dividerState, baseDelay, duration);
}
public ExpandableNotificationRow getViewAtPosition(float y) {
// find the view under the pointer, accounting for GONE views
final int count = mChildren.size();
for (int childIdx = 0; childIdx < count; childIdx++) {
ExpandableNotificationRow slidingChild = mChildren.get(childIdx);
float childTop = slidingChild.getTranslationY();
float top = childTop + slidingChild.getClipTopAmount();
float bottom = childTop + slidingChild.getActualHeight();
if (y >= top && y <= bottom) {
return slidingChild;
}
}
return null;
}
public void setTintColor(int color) {
ExpandableNotificationRow.applyTint(mCollapseDivider, color);
}
}
|