| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.internal.widget; |
| 18 |
|
| 19 |
import android.annotation.AttrRes; |
| 20 |
import android.annotation.Nullable; |
| 21 |
import android.annotation.StyleRes; |
| 22 |
import android.compat.annotation.UnsupportedAppUsage; |
| 23 |
import android.content.Context; |
| 24 |
import android.graphics.drawable.Drawable; |
| 25 |
import android.util.AttributeSet; |
| 26 |
import android.view.Gravity; |
| 27 |
import android.view.View; |
| 28 |
import android.view.ViewGroup; |
| 29 |
import android.widget.LinearLayout; |
| 30 |
|
| 31 |
import com.android.internal.R; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public class AlertDialogLayout extends LinearLayout { |
| 49 |
|
| 50 |
public AlertDialogLayout(@Nullable Context context) { |
| 51 |
super(context); |
| 52 |
} |
| 53 |
|
| 54 |
@UnsupportedAppUsage |
| 55 |
public AlertDialogLayout(@Nullable Context context, @Nullable AttributeSet attrs) { |
| 56 |
super(context, attrs); |
| 57 |
} |
| 58 |
|
| 59 |
public AlertDialogLayout(@Nullable Context context, @Nullable AttributeSet attrs, |
| 60 |
@AttrRes int defStyleAttr) { |
| 61 |
super(context, attrs, defStyleAttr); |
| 62 |
} |
| 63 |
|
| 64 |
public AlertDialogLayout(@Nullable Context context, @Nullable AttributeSet attrs, |
| 65 |
@AttrRes int defStyleAttr, @StyleRes int defStyleRes) { |
| 66 |
super(context, attrs, defStyleAttr, defStyleRes); |
| 67 |
} |
| 68 |
|
| 69 |
@Override |
| 70 |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 71 |
if (!tryOnMeasure(widthMeasureSpec, heightMeasureSpec)) { |
| 72 |
|
| 73 |
super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
private boolean tryOnMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 78 |
View topPanel = null; |
| 79 |
View buttonPanel = null; |
| 80 |
View middlePanel = null; |
| 81 |
|
| 82 |
final int count = getChildCount(); |
| 83 |
for (int i = 0; i < count; i++) { |
| 84 |
final View child = getChildAt(i); |
| 85 |
if (child.getVisibility() == View.GONE) { |
| 86 |
continue; |
| 87 |
} |
| 88 |
|
| 89 |
final int id = child.getId(); |
| 90 |
switch (id) { |
| 91 |
case R.id.topPanel: |
| 92 |
topPanel = child; |
| 93 |
break; |
| 94 |
case R.id.buttonPanel: |
| 95 |
buttonPanel = child; |
| 96 |
break; |
| 97 |
case R.id.contentPanel: |
| 98 |
case R.id.customPanel: |
| 99 |
if (middlePanel != null) { |
| 100 |
|
| 101 |
return false; |
| 102 |
} |
| 103 |
middlePanel = child; |
| 104 |
break; |
| 105 |
default: |
| 106 |
|
| 107 |
return false; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
final int heightMode = MeasureSpec.getMode(heightMeasureSpec); |
| 112 |
final int heightSize = MeasureSpec.getSize(heightMeasureSpec); |
| 113 |
final int widthMode = MeasureSpec.getMode(widthMeasureSpec); |
| 114 |
|
| 115 |
int childState = 0; |
| 116 |
int usedHeight = getPaddingTop() + getPaddingBottom(); |
| 117 |
|
| 118 |
if (topPanel != null) { |
| 119 |
topPanel.measure(widthMeasureSpec, MeasureSpec.UNSPECIFIED); |
| 120 |
|
| 121 |
usedHeight += topPanel.getMeasuredHeight(); |
| 122 |
childState = combineMeasuredStates(childState, topPanel.getMeasuredState()); |
| 123 |
} |
| 124 |
|
| 125 |
int buttonHeight = 0; |
| 126 |
int buttonWantsHeight = 0; |
| 127 |
if (buttonPanel != null) { |
| 128 |
buttonPanel.measure(widthMeasureSpec, MeasureSpec.UNSPECIFIED); |
| 129 |
buttonHeight = resolveMinimumHeight(buttonPanel); |
| 130 |
buttonWantsHeight = buttonPanel.getMeasuredHeight() - buttonHeight; |
| 131 |
|
| 132 |
usedHeight += buttonHeight; |
| 133 |
childState = combineMeasuredStates(childState, buttonPanel.getMeasuredState()); |
| 134 |
} |
| 135 |
|
| 136 |
int middleHeight = 0; |
| 137 |
if (middlePanel != null) { |
| 138 |
final int childHeightSpec; |
| 139 |
if (heightMode == MeasureSpec.UNSPECIFIED) { |
| 140 |
childHeightSpec = MeasureSpec.UNSPECIFIED; |
| 141 |
} else { |
| 142 |
childHeightSpec = MeasureSpec.makeMeasureSpec( |
| 143 |
Math.max(0, heightSize - usedHeight), heightMode); |
| 144 |
} |
| 145 |
|
| 146 |
middlePanel.measure(widthMeasureSpec, childHeightSpec); |
| 147 |
middleHeight = middlePanel.getMeasuredHeight(); |
| 148 |
|
| 149 |
usedHeight += middleHeight; |
| 150 |
childState = combineMeasuredStates(childState, middlePanel.getMeasuredState()); |
| 151 |
} |
| 152 |
|
| 153 |
int remainingHeight = heightSize - usedHeight; |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
if (buttonPanel != null) { |
| 159 |
usedHeight -= buttonHeight; |
| 160 |
|
| 161 |
final int heightToGive = Math.min(remainingHeight, buttonWantsHeight); |
| 162 |
if (heightToGive > 0) { |
| 163 |
remainingHeight -= heightToGive; |
| 164 |
buttonHeight += heightToGive; |
| 165 |
} |
| 166 |
|
| 167 |
final int childHeightSpec = MeasureSpec.makeMeasureSpec( |
| 168 |
buttonHeight, MeasureSpec.EXACTLY); |
| 169 |
buttonPanel.measure(widthMeasureSpec, childHeightSpec); |
| 170 |
|
| 171 |
usedHeight += buttonPanel.getMeasuredHeight(); |
| 172 |
childState = combineMeasuredStates(childState, buttonPanel.getMeasuredState()); |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
if (middlePanel != null && remainingHeight > 0) { |
| 178 |
usedHeight -= middleHeight; |
| 179 |
|
| 180 |
final int heightToGive = remainingHeight; |
| 181 |
remainingHeight -= heightToGive; |
| 182 |
middleHeight += heightToGive; |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
final int childHeightSpec = MeasureSpec.makeMeasureSpec( |
| 188 |
middleHeight, heightMode); |
| 189 |
middlePanel.measure(widthMeasureSpec, childHeightSpec); |
| 190 |
|
| 191 |
usedHeight += middlePanel.getMeasuredHeight(); |
| 192 |
childState = combineMeasuredStates(childState, middlePanel.getMeasuredState()); |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
int maxWidth = 0; |
| 197 |
for (int i = 0; i < count; i++) { |
| 198 |
final View child = getChildAt(i); |
| 199 |
if (child.getVisibility() != View.GONE) { |
| 200 |
maxWidth = Math.max(maxWidth, child.getMeasuredWidth()); |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
maxWidth += getPaddingLeft() + getPaddingRight(); |
| 205 |
|
| 206 |
final int widthSizeAndState = resolveSizeAndState(maxWidth, widthMeasureSpec, childState); |
| 207 |
final int heightSizeAndState = resolveSizeAndState(usedHeight, heightMeasureSpec, 0); |
| 208 |
setMeasuredDimension(widthSizeAndState, heightSizeAndState); |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
if (widthMode != MeasureSpec.EXACTLY) { |
| 213 |
forceUniformWidth(count, heightMeasureSpec); |
| 214 |
} |
| 215 |
|
| 216 |
return true; |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
private void forceUniformWidth(int count, int heightMeasureSpec) { |
| 226 |
|
| 227 |
final int uniformMeasureSpec = MeasureSpec.makeMeasureSpec( |
| 228 |
getMeasuredWidth(), MeasureSpec.EXACTLY); |
| 229 |
|
| 230 |
for (int i = 0; i < count; i++) { |
| 231 |
final View child = getChildAt(i); |
| 232 |
if (child.getVisibility() != GONE) { |
| 233 |
final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 234 |
if (lp.width == LayoutParams.MATCH_PARENT) { |
| 235 |
|
| 236 |
|
| 237 |
final int oldHeight = lp.height; |
| 238 |
lp.height = child.getMeasuredHeight(); |
| 239 |
|
| 240 |
|
| 241 |
measureChildWithMargins(child, uniformMeasureSpec, 0, heightMeasureSpec, 0); |
| 242 |
lp.height = oldHeight; |
| 243 |
} |
| 244 |
} |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
private int resolveMinimumHeight(View v) { |
| 258 |
final int minHeight = v.getMinimumHeight(); |
| 259 |
if (minHeight > 0) { |
| 260 |
return minHeight; |
| 261 |
} |
| 262 |
|
| 263 |
if (v instanceof ViewGroup) { |
| 264 |
final ViewGroup vg = (ViewGroup) v; |
| 265 |
if (vg.getChildCount() == 1) { |
| 266 |
return resolveMinimumHeight(vg.getChildAt(0)); |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
return 0; |
| 271 |
} |
| 272 |
|
| 273 |
@Override |
| 274 |
protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
| 275 |
final int paddingLeft = mPaddingLeft; |
| 276 |
|
| 277 |
|
| 278 |
final int width = right - left; |
| 279 |
final int childRight = width - mPaddingRight; |
| 280 |
|
| 281 |
|
| 282 |
final int childSpace = width - paddingLeft - mPaddingRight; |
| 283 |
|
| 284 |
final int totalLength = getMeasuredHeight(); |
| 285 |
final int count = getChildCount(); |
| 286 |
final int gravity = getGravity(); |
| 287 |
final int majorGravity = gravity & Gravity.VERTICAL_GRAVITY_MASK; |
| 288 |
final int minorGravity = gravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK; |
| 289 |
|
| 290 |
int childTop; |
| 291 |
switch (majorGravity) { |
| 292 |
case Gravity.BOTTOM: |
| 293 |
|
| 294 |
childTop = mPaddingTop + bottom - top - totalLength; |
| 295 |
break; |
| 296 |
|
| 297 |
|
| 298 |
case Gravity.CENTER_VERTICAL: |
| 299 |
childTop = mPaddingTop + (bottom - top - totalLength) / 2; |
| 300 |
break; |
| 301 |
|
| 302 |
case Gravity.TOP: |
| 303 |
default: |
| 304 |
childTop = mPaddingTop; |
| 305 |
break; |
| 306 |
} |
| 307 |
|
| 308 |
final Drawable dividerDrawable = getDividerDrawable(); |
| 309 |
final int dividerHeight = dividerDrawable == null ? |
| 310 |
0 : dividerDrawable.getIntrinsicHeight(); |
| 311 |
|
| 312 |
for (int i = 0; i < count; i++) { |
| 313 |
final View child = getChildAt(i); |
| 314 |
if (child != null && child.getVisibility() != GONE) { |
| 315 |
final int childWidth = child.getMeasuredWidth(); |
| 316 |
final int childHeight = child.getMeasuredHeight(); |
| 317 |
|
| 318 |
final LinearLayout.LayoutParams lp = |
| 319 |
(LinearLayout.LayoutParams) child.getLayoutParams(); |
| 320 |
|
| 321 |
int layoutGravity = lp.gravity; |
| 322 |
if (layoutGravity < 0) { |
| 323 |
layoutGravity = minorGravity; |
| 324 |
} |
| 325 |
final int layoutDirection = getLayoutDirection(); |
| 326 |
final int absoluteGravity = Gravity.getAbsoluteGravity( |
| 327 |
layoutGravity, layoutDirection); |
| 328 |
|
| 329 |
final int childLeft; |
| 330 |
switch (absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) { |
| 331 |
case Gravity.CENTER_HORIZONTAL: |
| 332 |
childLeft = paddingLeft + ((childSpace - childWidth) / 2) |
| 333 |
+ lp.leftMargin - lp.rightMargin; |
| 334 |
break; |
| 335 |
|
| 336 |
case Gravity.RIGHT: |
| 337 |
childLeft = childRight - childWidth - lp.rightMargin; |
| 338 |
break; |
| 339 |
|
| 340 |
case Gravity.LEFT: |
| 341 |
default: |
| 342 |
childLeft = paddingLeft + lp.leftMargin; |
| 343 |
break; |
| 344 |
} |
| 345 |
|
| 346 |
if (hasDividerBeforeChildAt(i)) { |
| 347 |
childTop += dividerHeight; |
| 348 |
} |
| 349 |
|
| 350 |
childTop += lp.topMargin; |
| 351 |
setChildFrame(child, childLeft, childTop, childWidth, childHeight); |
| 352 |
childTop += childHeight + lp.bottomMargin; |
| 353 |
} |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
private void setChildFrame(View child, int left, int top, int width, int height) { |
| 358 |
child.layout(left, top, left + width, top + height); |
| 359 |
} |
| 360 |
} |