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
|
/*
* Copyright (C) 2016 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.internal.widget;
import android.annotation.AttrRes;
import android.annotation.Nullable;
import android.annotation.StyleRes;
import android.compat.annotation.UnsupportedAppUsage;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import com.android.internal.R;
/**
* Special implementation of linear layout that's capable of laying out alert
* dialog components.
* <p>
* A dialog consists of up to three panels. All panels are optional, and a
* dialog may contain only a single panel. The panels are laid out according
* to the following guidelines:
* <ul>
* <li>topPanel: exactly wrap_content</li>
* <li>contentPanel OR customPanel: at most fill_parent, first priority for
* extra space</li>
* <li>buttonPanel: at least minHeight, at most wrap_content, second
* priority for extra space</li>
* </ul>
*/
public class AlertDialogLayout extends LinearLayout {
public AlertDialogLayout(@Nullable Context context) {
super(context);
}
@UnsupportedAppUsage
public AlertDialogLayout(@Nullable Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public AlertDialogLayout(@Nullable Context context, @Nullable AttributeSet attrs,
@AttrRes int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public AlertDialogLayout(@Nullable Context context, @Nullable AttributeSet attrs,
@AttrRes int defStyleAttr, @StyleRes int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (!tryOnMeasure(widthMeasureSpec, heightMeasureSpec)) {
// Failed to perform custom measurement, let superclass handle it.
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
private boolean tryOnMeasure(int widthMeasureSpec, int heightMeasureSpec) {
View topPanel = null;
View buttonPanel = null;
View middlePanel = null;
final int count = getChildCount();
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (child.getVisibility() == View.GONE) {
continue;
}
final int id = child.getId();
switch (id) {
case R.id.topPanel:
topPanel = child;
break;
case R.id.buttonPanel:
buttonPanel = child;
break;
case R.id.contentPanel:
case R.id.customPanel:
if (middlePanel != null) {
// Both the content and custom are visible. Abort!
return false;
}
middlePanel = child;
break;
default:
// Unknown top-level child. Abort!
return false;
}
}
final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
final int heightSize = MeasureSpec.getSize(heightMeasureSpec);
final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int childState = 0;
int usedHeight = getPaddingTop() + getPaddingBottom();
if (topPanel != null) {
topPanel.measure(widthMeasureSpec, MeasureSpec.UNSPECIFIED);
usedHeight += topPanel.getMeasuredHeight();
childState = combineMeasuredStates(childState, topPanel.getMeasuredState());
}
int buttonHeight = 0;
int buttonWantsHeight = 0;
if (buttonPanel != null) {
buttonPanel.measure(widthMeasureSpec, MeasureSpec.UNSPECIFIED);
buttonHeight = resolveMinimumHeight(buttonPanel);
buttonWantsHeight = buttonPanel.getMeasuredHeight() - buttonHeight;
usedHeight += buttonHeight;
childState = combineMeasuredStates(childState, buttonPanel.getMeasuredState());
}
int middleHeight = 0;
if (middlePanel != null) {
final int childHeightSpec;
if (heightMode == MeasureSpec.UNSPECIFIED) {
childHeightSpec = MeasureSpec.UNSPECIFIED;
} else {
childHeightSpec = MeasureSpec.makeMeasureSpec(
Math.max(0, heightSize - usedHeight), heightMode);
}
middlePanel.measure(widthMeasureSpec, childHeightSpec);
middleHeight = middlePanel.getMeasuredHeight();
usedHeight += middleHeight;
childState = combineMeasuredStates(childState, middlePanel.getMeasuredState());
}
int remainingHeight = heightSize - usedHeight;
// Time for the "real" button measure pass. If we have remaining space,
// make the button pane bigger up to its target height. Otherwise,
// just remeasure the button at whatever height it needs.
if (buttonPanel != null) {
usedHeight -= buttonHeight;
final int heightToGive = Math.min(remainingHeight, buttonWantsHeight);
if (heightToGive > 0) {
remainingHeight -= heightToGive;
buttonHeight += heightToGive;
}
final int childHeightSpec = MeasureSpec.makeMeasureSpec(
buttonHeight, MeasureSpec.EXACTLY);
buttonPanel.measure(widthMeasureSpec, childHeightSpec);
usedHeight += buttonPanel.getMeasuredHeight();
childState = combineMeasuredStates(childState, buttonPanel.getMeasuredState());
}
// If we still have remaining space, make the middle pane bigger up
// to the maximum height.
if (middlePanel != null && remainingHeight > 0) {
usedHeight -= middleHeight;
final int heightToGive = remainingHeight;
remainingHeight -= heightToGive;
middleHeight += heightToGive;
// Pass the same height mode as we're using for the dialog itself.
// If it's EXACTLY, then the middle pane MUST use the entire
// height.
final int childHeightSpec = MeasureSpec.makeMeasureSpec(
middleHeight, heightMode);
middlePanel.measure(widthMeasureSpec, childHeightSpec);
usedHeight += middlePanel.getMeasuredHeight();
childState = combineMeasuredStates(childState, middlePanel.getMeasuredState());
}
// Compute desired width as maximum child width.
int maxWidth = 0;
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (child.getVisibility() != View.GONE) {
maxWidth = Math.max(maxWidth, child.getMeasuredWidth());
}
}
maxWidth += getPaddingLeft() + getPaddingRight();
final int widthSizeAndState = resolveSizeAndState(maxWidth, widthMeasureSpec, childState);
final int heightSizeAndState = resolveSizeAndState(usedHeight, heightMeasureSpec, 0);
setMeasuredDimension(widthSizeAndState, heightSizeAndState);
// If the children weren't already measured EXACTLY, we need to run
// another measure pass to for MATCH_PARENT widths.
if (widthMode != MeasureSpec.EXACTLY) {
forceUniformWidth(count, heightMeasureSpec);
}
return true;
}
/**
* Remeasures child views to exactly match the layout's measured width.
*
* @param count the number of child views
* @param heightMeasureSpec the original height measure spec
*/
private void forceUniformWidth(int count, int heightMeasureSpec) {
// Pretend that the linear layout has an exact size.
final int uniformMeasureSpec = MeasureSpec.makeMeasureSpec(
getMeasuredWidth(), MeasureSpec.EXACTLY);
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (child.getVisibility() != GONE) {
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
if (lp.width == LayoutParams.MATCH_PARENT) {
// Temporarily force children to reuse their old measured
// height.
final int oldHeight = lp.height;
lp.height = child.getMeasuredHeight();
// Remeasure with new dimensions.
measureChildWithMargins(child, uniformMeasureSpec, 0, heightMeasureSpec, 0);
lp.height = oldHeight;
}
}
}
}
/**
* Attempts to resolve the minimum height of a view.
* <p>
* If the view doesn't have a minimum height set and only contains a single
* child, attempts to resolve the minimum height of the child view.
*
* @param v the view whose minimum height to resolve
* @return the minimum height
*/
private int resolveMinimumHeight(View v) {
final int minHeight = v.getMinimumHeight();
if (minHeight > 0) {
return minHeight;
}
if (v instanceof ViewGroup) {
final ViewGroup vg = (ViewGroup) v;
if (vg.getChildCount() == 1) {
return resolveMinimumHeight(vg.getChildAt(0));
}
}
return 0;
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
final int paddingLeft = mPaddingLeft;
// Where right end of child should go
final int width = right - left;
final int childRight = width - mPaddingRight;
// Space available for child
final int childSpace = width - paddingLeft - mPaddingRight;
final int totalLength = getMeasuredHeight();
final int count = getChildCount();
final int gravity = getGravity();
final int majorGravity = gravity & Gravity.VERTICAL_GRAVITY_MASK;
final int minorGravity = gravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK;
int childTop;
switch (majorGravity) {
case Gravity.BOTTOM:
// totalLength contains the padding already
childTop = mPaddingTop + bottom - top - totalLength;
break;
// totalLength contains the padding already
case Gravity.CENTER_VERTICAL:
childTop = mPaddingTop + (bottom - top - totalLength) / 2;
break;
case Gravity.TOP:
default:
childTop = mPaddingTop;
break;
}
final Drawable dividerDrawable = getDividerDrawable();
final int dividerHeight = dividerDrawable == null ?
0 : dividerDrawable.getIntrinsicHeight();
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (child != null && child.getVisibility() != GONE) {
final int childWidth = child.getMeasuredWidth();
final int childHeight = child.getMeasuredHeight();
final LinearLayout.LayoutParams lp =
(LinearLayout.LayoutParams) child.getLayoutParams();
int layoutGravity = lp.gravity;
if (layoutGravity < 0) {
layoutGravity = minorGravity;
}
final int layoutDirection = getLayoutDirection();
final int absoluteGravity = Gravity.getAbsoluteGravity(
layoutGravity, layoutDirection);
final int childLeft;
switch (absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
case Gravity.CENTER_HORIZONTAL:
childLeft = paddingLeft + ((childSpace - childWidth) / 2)
+ lp.leftMargin - lp.rightMargin;
break;
case Gravity.RIGHT:
childLeft = childRight - childWidth - lp.rightMargin;
break;
case Gravity.LEFT:
default:
childLeft = paddingLeft + lp.leftMargin;
break;
}
if (hasDividerBeforeChildAt(i)) {
childTop += dividerHeight;
}
childTop += lp.topMargin;
setChildFrame(child, childLeft, childTop, childWidth, childHeight);
childTop += childHeight + lp.bottomMargin;
}
}
}
private void setChildFrame(View child, int left, int top, int width, int height) {
child.layout(left, top, left + width, top + height);
}
}
|