Skip to content

Commit 7136544

Browse files
adampAndroid (Google) Code Review
authored andcommitted
Merge "Keyguard - MultiPaneChallengeLayout measurement/layout changes" into jb-mr1-lockscreen-dev
2 parents 8d80325 + 9d34f9d commit 7136544

File tree

1 file changed

+60
-42
lines changed

1 file changed

+60
-42
lines changed

policy/src/com/android/internal/policy/impl/keyguard/MultiPaneChallengeLayout.java

Lines changed: 60 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,22 @@
1616

1717
package com.android.internal.policy.impl.keyguard;
1818

19+
import com.android.internal.R;
20+
1921
import android.animation.Animator;
20-
import android.animation.AnimatorSet;
2122
import android.animation.AnimatorListenerAdapter;
2223
import android.animation.ObjectAnimator;
2324
import android.content.Context;
25+
import android.content.res.Resources;
2426
import android.content.res.TypedArray;
2527
import android.graphics.Rect;
2628
import android.util.AttributeSet;
29+
import android.util.DisplayMetrics;
2730
import android.view.Gravity;
2831
import android.view.View;
2932
import android.view.ViewGroup;
3033
import android.widget.LinearLayout;
3134

32-
import com.android.internal.R;
33-
3435
public class MultiPaneChallengeLayout extends ViewGroup implements ChallengeLayout {
3536
private static final String TAG = "MultiPaneChallengeLayout";
3637

@@ -47,7 +48,8 @@ public class MultiPaneChallengeLayout extends ViewGroup implements ChallengeLayo
4748
private OnBouncerStateChangedListener mBouncerListener;
4849

4950
private final Rect mTempRect = new Rect();
50-
private final Context mContext;
51+
52+
private final DisplayMetrics mDisplayMetrics;
5153

5254
private final OnClickListener mScrimClickListener = new OnClickListener() {
5355
@Override
@@ -67,13 +69,14 @@ public MultiPaneChallengeLayout(Context context, AttributeSet attrs) {
6769
public MultiPaneChallengeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
6870
super(context, attrs, defStyleAttr);
6971

70-
mContext = context;
71-
7272
final TypedArray a = context.obtainStyledAttributes(attrs,
7373
R.styleable.MultiPaneChallengeLayout, defStyleAttr, 0);
7474
mOrientation = a.getInt(R.styleable.MultiPaneChallengeLayout_orientation,
7575
HORIZONTAL);
7676
a.recycle();
77+
78+
final Resources res = getResources();
79+
mDisplayMetrics = res.getDisplayMetrics();
7780
}
7881

7982
@Override
@@ -169,15 +172,32 @@ void setScrimView(View scrim) {
169172
mScrimView.setOnClickListener(mScrimClickListener);
170173
}
171174

175+
private int getVirtualHeight(LayoutParams lp, int height, int heightUsed) {
176+
int virtualHeight = height;
177+
final View root = getRootView();
178+
if (root != null) {
179+
// This calculation is super dodgy and relies on several assumptions.
180+
// Specifically that the root of the window will be padded in for insets
181+
// and that the window is LAYOUT_IN_SCREEN.
182+
virtualHeight = mDisplayMetrics.heightPixels - root.getPaddingTop();
183+
}
184+
if (lp.childType == LayoutParams.CHILD_TYPE_WIDGET ||
185+
lp.childType == LayoutParams.CHILD_TYPE_USER_SWITCHER) {
186+
// Always measure the widget pager/user switcher as if there were no IME insets
187+
// on the window. We want to avoid resizing widgets when possible as it can
188+
// be ugly/expensive. This lets us simply clip them instead.
189+
return virtualHeight - heightUsed;
190+
}
191+
return Math.min(virtualHeight - heightUsed, height);
192+
}
193+
172194
@Override
173-
protected void onMeasure(int widthSpec, int heightSpec) {
195+
protected void onMeasure(final int widthSpec, final int heightSpec) {
174196
if (MeasureSpec.getMode(widthSpec) != MeasureSpec.EXACTLY ||
175197
MeasureSpec.getMode(heightSpec) != MeasureSpec.EXACTLY) {
176198
throw new IllegalArgumentException(
177199
"MultiPaneChallengeLayout must be measured with an exact size");
178200
}
179-
float squashedLayoutThreshold =
180-
mContext.getResources().getDimension(R.dimen.kg_squashed_layout_threshold);
181201

182202
final int width = MeasureSpec.getSize(widthSpec);
183203
final int height = MeasureSpec.getSize(heightSpec);
@@ -213,32 +233,26 @@ protected void onMeasure(int widthSpec, int heightSpec) {
213233
mUserSwitcherView = child;
214234

215235
if (child.getVisibility() == GONE) continue;
216-
if (height < squashedLayoutThreshold) {
217-
int zero = MeasureSpec.makeMeasureSpec(0, MeasureSpec.EXACTLY);
218-
measureChild(child, zero, zero);
219-
} else {
220-
int adjustedWidthSpec = widthSpec;
221-
int adjustedHeightSpec = heightSpec;
222-
if (lp.maxWidth >= 0) {
223-
adjustedWidthSpec = MeasureSpec.makeMeasureSpec(
224-
Math.min(lp.maxWidth, MeasureSpec.getSize(widthSpec)),
225-
MeasureSpec.EXACTLY);
226-
}
227-
if (lp.maxHeight >= 0) {
228-
adjustedHeightSpec = MeasureSpec.makeMeasureSpec(
229-
Math.min(lp.maxHeight, MeasureSpec.getSize(heightSpec)),
230-
MeasureSpec.EXACTLY);
231-
}
232-
// measureChildWithMargins will resolve layout direction for the LayoutParams
233-
measureChildWithMargins(child, adjustedWidthSpec, 0, adjustedHeightSpec, 0);
234-
235-
// Only subtract out space from one dimension. Favor vertical.
236-
// Offset by 1.5x to add some balance along the other edge.
237-
if (Gravity.isVertical(lp.gravity)) {
238-
heightUsed += child.getMeasuredHeight() * 1.5f;
239-
} else if (Gravity.isHorizontal(lp.gravity)) {
240-
widthUsed += child.getMeasuredWidth() * 1.5f;
241-
}
236+
237+
int adjustedWidthSpec = widthSpec;
238+
int adjustedHeightSpec = heightSpec;
239+
if (lp.maxWidth >= 0) {
240+
adjustedWidthSpec = MeasureSpec.makeMeasureSpec(
241+
Math.min(lp.maxWidth, width), MeasureSpec.EXACTLY);
242+
}
243+
if (lp.maxHeight >= 0) {
244+
adjustedHeightSpec = MeasureSpec.makeMeasureSpec(
245+
Math.min(lp.maxHeight, height), MeasureSpec.EXACTLY);
246+
}
247+
// measureChildWithMargins will resolve layout direction for the LayoutParams
248+
measureChildWithMargins(child, adjustedWidthSpec, 0, adjustedHeightSpec, 0);
249+
250+
// Only subtract out space from one dimension. Favor vertical.
251+
// Offset by 1.5x to add some balance along the other edge.
252+
if (Gravity.isVertical(lp.gravity)) {
253+
heightUsed += child.getMeasuredHeight() * 1.5f;
254+
} else if (Gravity.isHorizontal(lp.gravity)) {
255+
widthUsed += child.getMeasuredWidth() * 1.5f;
242256
}
243257
} else if (lp.childType == LayoutParams.CHILD_TYPE_SCRIM) {
244258
setScrimView(child);
@@ -258,6 +272,8 @@ protected void onMeasure(int widthSpec, int heightSpec) {
258272
continue;
259273
}
260274

275+
final int virtualHeight = getVirtualHeight(lp, height, heightUsed);
276+
261277
int adjustedWidthSpec;
262278
int adjustedHeightSpec;
263279
if (lp.centerWithinArea > 0) {
@@ -266,19 +282,19 @@ protected void onMeasure(int widthSpec, int heightSpec) {
266282
(int) ((width - widthUsed) * lp.centerWithinArea + 0.5f),
267283
MeasureSpec.EXACTLY);
268284
adjustedHeightSpec = MeasureSpec.makeMeasureSpec(
269-
MeasureSpec.getSize(heightSpec) - heightUsed, MeasureSpec.EXACTLY);
285+
virtualHeight, MeasureSpec.EXACTLY);
270286
} else {
271287
adjustedWidthSpec = MeasureSpec.makeMeasureSpec(
272-
MeasureSpec.getSize(widthSpec) - widthUsed, MeasureSpec.EXACTLY);
288+
width - widthUsed, MeasureSpec.EXACTLY);
273289
adjustedHeightSpec = MeasureSpec.makeMeasureSpec(
274-
(int) ((height - heightUsed) * lp.centerWithinArea + 0.5f),
290+
(int) (virtualHeight * lp.centerWithinArea + 0.5f),
275291
MeasureSpec.EXACTLY);
276292
}
277293
} else {
278294
adjustedWidthSpec = MeasureSpec.makeMeasureSpec(
279-
MeasureSpec.getSize(widthSpec) - widthUsed, MeasureSpec.EXACTLY);
295+
width - widthUsed, MeasureSpec.EXACTLY);
280296
adjustedHeightSpec = MeasureSpec.makeMeasureSpec(
281-
MeasureSpec.getSize(heightSpec) - heightUsed, MeasureSpec.EXACTLY);
297+
virtualHeight, MeasureSpec.EXACTLY);
282298
}
283299
if (lp.maxWidth >= 0) {
284300
adjustedWidthSpec = MeasureSpec.makeMeasureSpec(
@@ -331,6 +347,9 @@ private void layoutWithGravity(int width, int height, View child, Rect padding,
331347
boolean adjustPadding) {
332348
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
333349

350+
final int heightUsed = padding.top + padding.bottom - getPaddingTop() - getPaddingBottom();
351+
height = getVirtualHeight(lp, height, heightUsed);
352+
334353
final int gravity = Gravity.getAbsoluteGravity(lp.gravity, getLayoutDirection());
335354

336355
final boolean fixedLayoutSize = lp.centerWithinArea > 0;
@@ -382,8 +401,7 @@ private void layoutWithGravity(int width, int height, View child, Rect padding,
382401
}
383402
break;
384403
case Gravity.CENTER_VERTICAL:
385-
final int paddedHeight = height - padding.top - padding.bottom;
386-
top = padding.top + (paddedHeight - childHeight) / 2;
404+
top = padding.top + (height - childHeight) / 2;
387405
bottom = top + childHeight;
388406
break;
389407
}

0 commit comments

Comments
 (0)