Skip to content

Commit 2df294e

Browse files
Adam CohenAndroid (Google) Code Review
authored andcommitted
Merge "Widget size policy, size callbacks" into jb-mr1-lockscreen-dev
2 parents d6fe75a + 4ddcd57 commit 2df294e

File tree

4 files changed

+112
-45
lines changed

4 files changed

+112
-45
lines changed

core/java/android/appwidget/AppWidgetHostView.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -244,14 +244,22 @@ protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
244244
*
245245
* @param newOptions The bundle of options, in addition to the size information,
246246
* can be null.
247-
* @param minWidth The minimum width that the widget will be displayed at.
248-
* @param minHeight The maximum height that the widget will be displayed at.
249-
* @param maxWidth The maximum width that the widget will be displayed at.
250-
* @param maxHeight The maximum height that the widget will be displayed at.
247+
* @param minWidth The minimum width in dips that the widget will be displayed at.
248+
* @param minHeight The maximum height in dips that the widget will be displayed at.
249+
* @param maxWidth The maximum width in dips that the widget will be displayed at.
250+
* @param maxHeight The maximum height in dips that the widget will be displayed at.
251251
*
252252
*/
253253
public void updateAppWidgetSize(Bundle newOptions, int minWidth, int minHeight, int maxWidth,
254254
int maxHeight) {
255+
updateAppWidgetSize(newOptions, minWidth, minHeight, maxWidth, maxHeight, false);
256+
}
257+
258+
/**
259+
* @hide
260+
*/
261+
public void updateAppWidgetSize(Bundle newOptions, int minWidth, int minHeight, int maxWidth,
262+
int maxHeight, boolean ignorePadding) {
255263
if (newOptions == null) {
256264
newOptions = new Bundle();
257265
}
@@ -265,10 +273,10 @@ public void updateAppWidgetSize(Bundle newOptions, int minWidth, int minHeight,
265273
int xPaddingDips = (int) ((padding.left + padding.right) / density);
266274
int yPaddingDips = (int) ((padding.top + padding.bottom) / density);
267275

268-
int newMinWidth = minWidth - xPaddingDips;
269-
int newMinHeight = minHeight - yPaddingDips;
270-
int newMaxWidth = maxWidth - xPaddingDips;
271-
int newMaxHeight = maxHeight - yPaddingDips;
276+
int newMinWidth = minWidth - (ignorePadding ? 0 : xPaddingDips);
277+
int newMinHeight = minHeight - (ignorePadding ? 0 : yPaddingDips);
278+
int newMaxWidth = maxWidth - (ignorePadding ? 0 : xPaddingDips);
279+
int newMaxHeight = maxHeight - (ignorePadding ? 0 : yPaddingDips);
272280

273281
AppWidgetManager widgetManager = AppWidgetManager.getInstance(mContext);
274282

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ public void onPageSwitch(View newPage, int newPageIndex) {
105105
// Reset the previous page size and ensure the current page is sized appropriately.
106106
// We only modify the page state if it is not currently under control by the slider.
107107
// This prevents conflicts.
108+
109+
// If the page hasn't switched, don't bother with any of this
110+
if (mCurrentPage != newPageIndex) return;
111+
108112
if (mPagedView != null && mChallengeLayout != null) {
109113
KeyguardWidgetFrame prevPage = mPagedView.getWidgetPageAt(mCurrentPage);
110114
if (prevPage != null && mCurrentPage != mPageListeningToSlider) {

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

Lines changed: 66 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,18 @@ public class KeyguardWidgetFrame extends FrameLayout {
6363
private float mBackgroundAlphaMultiplier = 1.0f;
6464
private Drawable mBackgroundDrawable;
6565
private Rect mBackgroundRect = new Rect();
66+
private int mLastMeasuredWidth = -1;
67+
private int mLastMeasuredHeight = 1;
68+
69+
// These variables are all needed in order to size things properly before we're actually
70+
// measured.
6671
private int mSmallWidgetHeight;
72+
private int mSmallFrameHeight;
73+
private boolean mWidgetLockedSmall = false;
74+
private int mMaxChallengeTop = -1;
75+
76+
// This will hold the width value before we've actually been measured
77+
private int mFrameHeight;
6778

6879
// Multiple callers may try and adjust the alpha of the frame. When a caller shows
6980
// the outlines, we give that caller control, and nobody else can fade them out.
@@ -99,10 +110,6 @@ protected void onDetachedFromWindow() {
99110
cancelLongPress();
100111
}
101112

102-
public void setMaxChallengeTop(int top) {
103-
mSmallWidgetHeight = top - getPaddingTop();
104-
}
105-
106113
@Override
107114
public boolean onInterceptTouchEvent(MotionEvent ev) {
108115
// Watch for longpress events at this level to make sure
@@ -262,22 +269,6 @@ public void setContentAlpha(float alpha) {
262269
}
263270
}
264271

265-
/**
266-
* Set the top location of the challenge.
267-
*
268-
* @param top The top of the challenge, in _local_ coordinates, or -1 to indicate the challenge
269-
* is down.
270-
*/
271-
private void setChallengeTop(int top, boolean updateWidgetSize) {
272-
// The widget starts below the padding, and extends to the top of the challengs.
273-
int widgetHeight = top - getPaddingTop();
274-
int frameHeight = top + getPaddingBottom();
275-
setFrameHeight(frameHeight);
276-
if (updateWidgetSize) {
277-
setWidgetHeight(widgetHeight);
278-
}
279-
}
280-
281272
/**
282273
* Depending on whether the security is up, the widget size needs to change
283274
*
@@ -298,28 +289,49 @@ private void setWidgetHeight(int height) {
298289
}
299290
}
300291

292+
public void setMaxChallengeTop(int top) {
293+
boolean dirty = mMaxChallengeTop != top;
294+
mSmallWidgetHeight = top - getPaddingTop();
295+
mSmallFrameHeight = top + getPaddingBottom();
296+
if (dirty && mIsSmall) {
297+
setWidgetHeight(mSmallWidgetHeight);
298+
setFrameHeight(mSmallFrameHeight);
299+
} else if (dirty && mWidgetLockedSmall) {
300+
setWidgetHeight(mSmallWidgetHeight);
301+
}
302+
}
303+
301304
public boolean isSmall() {
302305
return mIsSmall;
303306
}
304307

305308
public void adjustFrame(int challengeTop) {
306-
setChallengeTop(challengeTop, false);
309+
int frameHeight = challengeTop + getPaddingBottom();
310+
setFrameHeight(frameHeight);
307311
}
308312

309313
public void shrinkWidget() {
310314
mIsSmall = true;
311-
setChallengeTop(mSmallWidgetHeight, true);
315+
setWidgetHeight(mSmallWidgetHeight);
316+
setFrameHeight(mSmallFrameHeight);
317+
}
318+
319+
public void setWidgetLockedSmall(boolean locked) {
320+
setWidgetHeight(mSmallWidgetHeight);
321+
mWidgetLockedSmall = locked;
312322
}
313323

314324
public void resetSize() {
315325
mIsSmall = false;
326+
if (!mWidgetLockedSmall) {
327+
setWidgetHeight(LayoutParams.MATCH_PARENT);
328+
}
316329
setFrameHeight(getMeasuredHeight());
317-
setWidgetHeight(LayoutParams.MATCH_PARENT);
318330
}
319331

320332
public void setFrameHeight(int height) {
321-
height = Math.min(height, getMeasuredHeight());
322-
mBackgroundRect.set(0, 0, getMeasuredWidth(), height);
333+
mFrameHeight = height;
334+
mBackgroundRect.set(0, 0, getMeasuredWidth(), Math.min(mFrameHeight, getMeasuredHeight()));
323335
invalidate();
324336
}
325337

@@ -359,10 +371,38 @@ protected void onSizeChanged(int w, int h, int oldw, int oldh) {
359371
mGradientColor, 0, Shader.TileMode.CLAMP);
360372
mRightToLeftGradient = new LinearGradient(x1, 0f, x0, 0f,
361373
mGradientColor, 0, Shader.TileMode.CLAMP);
362-
mBackgroundRect.set(0, 0, w, h);
374+
375+
if (!mIsSmall) {
376+
mFrameHeight = h;
377+
}
378+
379+
mBackgroundRect.set(0, 0, getMeasuredWidth(), Math.min(h, mFrameHeight));
363380
invalidate();
364381
}
365382

383+
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
384+
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
385+
performAppWidgetSizeCallbacksIfNecessary();
386+
}
387+
388+
private void performAppWidgetSizeCallbacksIfNecessary() {
389+
View content = getContent();
390+
if (!(content instanceof AppWidgetHostView)) return;
391+
392+
boolean sizeDirty = content.getMeasuredWidth() != mLastMeasuredWidth ||
393+
content.getMeasuredHeight() != mLastMeasuredHeight;
394+
if (sizeDirty) {
395+
396+
}
397+
398+
AppWidgetHostView awhv = (AppWidgetHostView) content;
399+
float density = getResources().getDisplayMetrics().density;
400+
401+
int width = (int) (content.getMeasuredWidth() / density);
402+
int height = (int) (content.getMeasuredHeight() / density);
403+
awhv.updateAppWidgetSize(null, width, height, width, height, true);
404+
}
405+
366406
void setOverScrollAmount(float r, boolean left) {
367407
if (Float.compare(mOverScrollAmount, r) != 0) {
368408
mOverScrollAmount = r;

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

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import android.animation.ObjectAnimator;
2222
import android.animation.PropertyValuesHolder;
2323
import android.animation.TimeInterpolator;
24+
import android.appwidget.AppWidgetHostView;
25+
import android.appwidget.AppWidgetProviderInfo;
2426
import android.content.Context;
2527
import android.content.res.Resources;
2628
import android.os.Handler;
@@ -31,9 +33,9 @@
3133
import android.view.MotionEvent;
3234
import android.view.View;
3335
import android.view.View.OnLongClickListener;
36+
import android.view.ViewGroup;
3437
import android.view.accessibility.AccessibilityEvent;
3538
import android.view.accessibility.AccessibilityManager;
36-
import android.view.ViewGroup;
3739
import android.widget.FrameLayout;
3840

3941
import com.android.internal.R;
@@ -250,10 +252,23 @@ public void addWidget(View widget, int pageIndex) {
250252
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
251253
LayoutParams.MATCH_PARENT);
252254
lp.gravity = Gravity.TOP;
255+
253256
// The framework adds a default padding to AppWidgetHostView. We don't need this padding
254257
// for the Keyguard, so we override it to be 0.
255258
widget.setPadding(0, 0, 0, 0);
256259
frame.addView(widget, lp);
260+
261+
// We set whether or not this widget supports vertical resizing.
262+
if (widget instanceof AppWidgetHostView) {
263+
AppWidgetHostView awhv = (AppWidgetHostView) widget;
264+
AppWidgetProviderInfo info = awhv.getAppWidgetInfo();
265+
if ((info.resizeMode & AppWidgetProviderInfo.RESIZE_VERTICAL) != 0) {
266+
frame.setWidgetLockedSmall(false);
267+
} else {
268+
// Lock the widget to be small.
269+
frame.setWidgetLockedSmall(true);
270+
}
271+
}
257272
} else {
258273
frame = (KeyguardWidgetFrame) widget;
259274
}
@@ -549,20 +564,20 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
549564
// coordinate relative to our children, hence we subtract the top padding.s
550565
maxChallengeTop = top - getPaddingTop();
551566
challengeShowing = scl.isChallengeShowing();
552-
}
553-
554-
int count = getChildCount();
555-
for (int i = 0; i < count; i++) {
556-
KeyguardWidgetFrame frame = getWidgetPageAt(i);
557-
frame.setMaxChallengeTop(maxChallengeTop);
558567

559-
// On the very first measure pass, if the challenge is showing, we need to make sure
560-
// that the widget on the current page is small.
561-
if (challengeShowing && i == mCurrentPage && !mHasMeasure) {
562-
frame.shrinkWidget();
568+
int count = getChildCount();
569+
for (int i = 0; i < count; i++) {
570+
KeyguardWidgetFrame frame = getWidgetPageAt(i);
571+
frame.setMaxChallengeTop(maxChallengeTop);
572+
// On the very first measure pass, if the challenge is showing, we need to make sure
573+
// that the widget on the current page is small.
574+
if (challengeShowing && i == mCurrentPage && !mHasMeasure) {
575+
frame.shrinkWidget();
576+
}
563577
}
564578
}
565579
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
580+
mHasMeasure = true;
566581
}
567582

568583
void animateOutlinesAndSidePages(final boolean show) {

0 commit comments

Comments
 (0)