blob: 2cc12400d82b38442f4ab6bf191a9a2c858c0004 [file] [log] [blame]
Adam Cohend4844c32011-02-18 19:25:06 -08001package com.android.launcher2;
2
3import android.animation.AnimatorSet;
4import android.animation.ObjectAnimator;
5import android.animation.PropertyValuesHolder;
6import android.animation.ValueAnimator;
7import android.animation.ValueAnimator.AnimatorUpdateListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -08008import android.appwidget.AppWidgetHostView;
Adam Cohend4844c32011-02-18 19:25:06 -08009import android.appwidget.AppWidgetProviderInfo;
10import android.content.Context;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080011import android.graphics.Rect;
Adam Cohend4844c32011-02-18 19:25:06 -080012import android.view.Gravity;
13import android.widget.FrameLayout;
14import android.widget.ImageView;
15
16import com.android.launcher.R;
17
18public class AppWidgetResizeFrame extends FrameLayout {
19
20 private ItemInfo mItemInfo;
21 private LauncherAppWidgetHostView mWidgetView;
22 private CellLayout mCellLayout;
Adam Cohen67882692011-03-11 15:29:03 -080023 private DragLayer mDragLayer;
24 private Workspace mWorkspace;
Adam Cohend4844c32011-02-18 19:25:06 -080025 private ImageView mLeftHandle;
26 private ImageView mRightHandle;
Adam Cohen1b607ed2011-03-03 17:26:50 -080027 private ImageView mTopHandle;
Adam Cohend4844c32011-02-18 19:25:06 -080028 private ImageView mBottomHandle;
29
30 private boolean mLeftBorderActive;
31 private boolean mRightBorderActive;
32 private boolean mTopBorderActive;
33 private boolean mBottomBorderActive;
34
Adam Cohen37b59ff2011-06-13 17:13:42 -070035 private int mWidgetPaddingLeft;
36 private int mWidgetPaddingRight;
37 private int mWidgetPaddingTop;
38 private int mWidgetPaddingBottom;
39
Adam Cohend4844c32011-02-18 19:25:06 -080040 private int mBaselineWidth;
41 private int mBaselineHeight;
42 private int mBaselineX;
43 private int mBaselineY;
44 private int mResizeMode;
Adam Cohen3cba7222011-03-02 19:03:11 -080045
Adam Cohend4844c32011-02-18 19:25:06 -080046 private int mRunningHInc;
47 private int mRunningVInc;
48 private int mMinHSpan;
49 private int mMinVSpan;
50 private int mDeltaX;
51 private int mDeltaY;
Adam Cohenbebf0422012-04-11 18:06:28 -070052 private int mDeltaXAddOn;
53 private int mDeltaYAddOn;
Adam Cohen1b607ed2011-03-03 17:26:50 -080054
Adam Cohen3cba7222011-03-02 19:03:11 -080055 private int mBackgroundPadding;
56 private int mTouchTargetWidth;
Adam Cohend4844c32011-02-18 19:25:06 -080057
Adam Cohenbebf0422012-04-11 18:06:28 -070058 int[] mDirectionVector = new int[2];
Adam Cohend4844c32011-02-18 19:25:06 -080059
Adam Cohend4844c32011-02-18 19:25:06 -080060 final int SNAP_DURATION = 150;
Adam Cohen3cba7222011-03-02 19:03:11 -080061 final int BACKGROUND_PADDING = 24;
Adam Cohene4b77292011-03-08 18:35:52 -080062 final float DIMMED_HANDLE_ALPHA = 0f;
63 final float RESIZE_THRESHOLD = 0.66f;
Adam Cohend4844c32011-02-18 19:25:06 -080064
Adam Cohen1b607ed2011-03-03 17:26:50 -080065 public static final int LEFT = 0;
66 public static final int TOP = 1;
67 public static final int RIGHT = 2;
68 public static final int BOTTOM = 3;
69
Adam Cohencbf47e32011-09-16 17:32:37 -070070 private Launcher mLauncher;
71
Adam Cohend4844c32011-02-18 19:25:06 -080072 public AppWidgetResizeFrame(Context context, ItemInfo itemInfo,
Adam Cohen67882692011-03-11 15:29:03 -080073 LauncherAppWidgetHostView widgetView, CellLayout cellLayout, DragLayer dragLayer) {
Adam Cohend4844c32011-02-18 19:25:06 -080074
75 super(context);
Adam Cohencbf47e32011-09-16 17:32:37 -070076 mLauncher = (Launcher) context;
Adam Cohend4844c32011-02-18 19:25:06 -080077 mItemInfo = itemInfo;
78 mCellLayout = cellLayout;
79 mWidgetView = widgetView;
Adam Cohen27c09b02011-02-28 14:45:11 -080080 mResizeMode = widgetView.getAppWidgetInfo().resizeMode;
Adam Cohen67882692011-03-11 15:29:03 -080081 mDragLayer = dragLayer;
82 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
Adam Cohen3cba7222011-03-02 19:03:11 -080083
Adam Cohend4844c32011-02-18 19:25:06 -080084 final AppWidgetProviderInfo info = widgetView.getAppWidgetInfo();
Adam Cohend41fbf52012-02-16 23:53:59 -080085 int[] result = mLauncher.getMinSpanForWidget(info, null);
Adam Cohend4844c32011-02-18 19:25:06 -080086 mMinHSpan = result[0];
87 mMinVSpan = result[1];
88
Adam Cohen3cba7222011-03-02 19:03:11 -080089 setBackgroundResource(R.drawable.widget_resize_frame_holo);
Adam Cohend4844c32011-02-18 19:25:06 -080090 setPadding(0, 0, 0, 0);
91
92 LayoutParams lp;
93 mLeftHandle = new ImageView(context);
Adam Cohen3cba7222011-03-02 19:03:11 -080094 mLeftHandle.setImageResource(R.drawable.widget_resize_handle_left);
Adam Cohend4844c32011-02-18 19:25:06 -080095 lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
96 Gravity.LEFT | Gravity.CENTER_VERTICAL);
97 addView(mLeftHandle, lp);
98
99 mRightHandle = new ImageView(context);
Adam Cohen3cba7222011-03-02 19:03:11 -0800100 mRightHandle.setImageResource(R.drawable.widget_resize_handle_right);
Adam Cohend4844c32011-02-18 19:25:06 -0800101 lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
102 Gravity.RIGHT | Gravity.CENTER_VERTICAL);
103 addView(mRightHandle, lp);
104
105 mTopHandle = new ImageView(context);
Adam Cohen3cba7222011-03-02 19:03:11 -0800106 mTopHandle.setImageResource(R.drawable.widget_resize_handle_top);
Adam Cohend4844c32011-02-18 19:25:06 -0800107 lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
108 Gravity.CENTER_HORIZONTAL | Gravity.TOP);
109 addView(mTopHandle, lp);
110
111 mBottomHandle = new ImageView(context);
Adam Cohen3cba7222011-03-02 19:03:11 -0800112 mBottomHandle.setImageResource(R.drawable.widget_resize_handle_bottom);
Adam Cohend4844c32011-02-18 19:25:06 -0800113 lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
114 Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);
115 addView(mBottomHandle, lp);
116
Adam Cohen0cf2a7c2011-11-08 15:07:01 -0800117 Rect p = AppWidgetHostView.getDefaultPaddingForWidget(context,
118 widgetView.getAppWidgetInfo().provider, null);
Adam Cohen82ebbd22011-09-30 15:05:40 -0700119 mWidgetPaddingLeft = p.left;
120 mWidgetPaddingTop = p.top;
121 mWidgetPaddingRight = p.right;
122 mWidgetPaddingBottom = p.bottom;
Adam Cohen37b59ff2011-06-13 17:13:42 -0700123
Adam Cohend4844c32011-02-18 19:25:06 -0800124 if (mResizeMode == AppWidgetProviderInfo.RESIZE_HORIZONTAL) {
125 mTopHandle.setVisibility(GONE);
126 mBottomHandle.setVisibility(GONE);
127 } else if (mResizeMode == AppWidgetProviderInfo.RESIZE_VERTICAL) {
128 mLeftHandle.setVisibility(GONE);
129 mRightHandle.setVisibility(GONE);
Adam Cohen3cba7222011-03-02 19:03:11 -0800130 }
131
Adam Cohencbf47e32011-09-16 17:32:37 -0700132 final float density = mLauncher.getResources().getDisplayMetrics().density;
Adam Cohen3cba7222011-03-02 19:03:11 -0800133 mBackgroundPadding = (int) Math.ceil(density * BACKGROUND_PADDING);
134 mTouchTargetWidth = 2 * mBackgroundPadding;
Adam Cohenbebf0422012-04-11 18:06:28 -0700135
136 // When we create the resize frame, we first mark all cells as unoccupied. The appropriate
137 // cells (same if not resized, or different) will be marked as occupied when the resize
138 // frame is dismissed.
139 mCellLayout.markCellsAsUnoccupiedForView(mWidgetView);
Adam Cohend4844c32011-02-18 19:25:06 -0800140 }
141
142 public boolean beginResizeIfPointInRegion(int x, int y) {
143 boolean horizontalActive = (mResizeMode & AppWidgetProviderInfo.RESIZE_HORIZONTAL) != 0;
144 boolean verticalActive = (mResizeMode & AppWidgetProviderInfo.RESIZE_VERTICAL) != 0;
Adam Cohen3cba7222011-03-02 19:03:11 -0800145 mLeftBorderActive = (x < mTouchTargetWidth) && horizontalActive;
146 mRightBorderActive = (x > getWidth() - mTouchTargetWidth) && horizontalActive;
147 mTopBorderActive = (y < mTouchTargetWidth) && verticalActive;
148 mBottomBorderActive = (y > getHeight() - mTouchTargetWidth) && verticalActive;
Adam Cohend4844c32011-02-18 19:25:06 -0800149
150 boolean anyBordersActive = mLeftBorderActive || mRightBorderActive
151 || mTopBorderActive || mBottomBorderActive;
152
153 mBaselineWidth = getMeasuredWidth();
154 mBaselineHeight = getMeasuredHeight();
155 mBaselineX = getLeft();
156 mBaselineY = getTop();
Adam Cohend4844c32011-02-18 19:25:06 -0800157
158 if (anyBordersActive) {
Adam Cohen3cba7222011-03-02 19:03:11 -0800159 mLeftHandle.setAlpha(mLeftBorderActive ? 1.0f : DIMMED_HANDLE_ALPHA);
160 mRightHandle.setAlpha(mRightBorderActive ? 1.0f :DIMMED_HANDLE_ALPHA);
161 mTopHandle.setAlpha(mTopBorderActive ? 1.0f : DIMMED_HANDLE_ALPHA);
162 mBottomHandle.setAlpha(mBottomBorderActive ? 1.0f : DIMMED_HANDLE_ALPHA);
Adam Cohend4844c32011-02-18 19:25:06 -0800163 }
Adam Cohend4844c32011-02-18 19:25:06 -0800164 return anyBordersActive;
165 }
166
Adam Cohen1b607ed2011-03-03 17:26:50 -0800167 /**
168 * Here we bound the deltas such that the frame cannot be stretched beyond the extents
169 * of the CellLayout, and such that the frame's borders can't cross.
170 */
Adam Cohend4844c32011-02-18 19:25:06 -0800171 public void updateDeltas(int deltaX, int deltaY) {
172 if (mLeftBorderActive) {
173 mDeltaX = Math.max(-mBaselineX, deltaX);
Adam Cohen3cba7222011-03-02 19:03:11 -0800174 mDeltaX = Math.min(mBaselineWidth - 2 * mTouchTargetWidth, mDeltaX);
Adam Cohend4844c32011-02-18 19:25:06 -0800175 } else if (mRightBorderActive) {
Adam Cohen67882692011-03-11 15:29:03 -0800176 mDeltaX = Math.min(mDragLayer.getWidth() - (mBaselineX + mBaselineWidth), deltaX);
Adam Cohen3cba7222011-03-02 19:03:11 -0800177 mDeltaX = Math.max(-mBaselineWidth + 2 * mTouchTargetWidth, mDeltaX);
Adam Cohend4844c32011-02-18 19:25:06 -0800178 }
179
180 if (mTopBorderActive) {
181 mDeltaY = Math.max(-mBaselineY, deltaY);
Adam Cohen3cba7222011-03-02 19:03:11 -0800182 mDeltaY = Math.min(mBaselineHeight - 2 * mTouchTargetWidth, mDeltaY);
Adam Cohend4844c32011-02-18 19:25:06 -0800183 } else if (mBottomBorderActive) {
Adam Cohen67882692011-03-11 15:29:03 -0800184 mDeltaY = Math.min(mDragLayer.getHeight() - (mBaselineY + mBaselineHeight), deltaY);
Adam Cohen3cba7222011-03-02 19:03:11 -0800185 mDeltaY = Math.max(-mBaselineHeight + 2 * mTouchTargetWidth, mDeltaY);
Adam Cohend4844c32011-02-18 19:25:06 -0800186 }
187 }
188
Adam Cohenbebf0422012-04-11 18:06:28 -0700189 public void visualizeResizeForDelta(int deltaX, int deltaY) {
190 visualizeResizeForDelta(deltaX, deltaY, false);
191 }
192
Adam Cohen1b607ed2011-03-03 17:26:50 -0800193 /**
194 * Based on the deltas, we resize the frame, and, if needed, we resize the widget.
195 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700196 private void visualizeResizeForDelta(int deltaX, int deltaY, boolean onDismiss) {
Adam Cohend4844c32011-02-18 19:25:06 -0800197 updateDeltas(deltaX, deltaY);
Adam Cohen67882692011-03-11 15:29:03 -0800198 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
199
Adam Cohend4844c32011-02-18 19:25:06 -0800200 if (mLeftBorderActive) {
201 lp.x = mBaselineX + mDeltaX;
202 lp.width = mBaselineWidth - mDeltaX;
203 } else if (mRightBorderActive) {
204 lp.width = mBaselineWidth + mDeltaX;
205 }
206
207 if (mTopBorderActive) {
208 lp.y = mBaselineY + mDeltaY;
209 lp.height = mBaselineHeight - mDeltaY;
210 } else if (mBottomBorderActive) {
211 lp.height = mBaselineHeight + mDeltaY;
212 }
213
Adam Cohenbebf0422012-04-11 18:06:28 -0700214 resizeWidgetIfNeeded(onDismiss);
Adam Cohend4844c32011-02-18 19:25:06 -0800215 requestLayout();
216 }
217
Adam Cohen1b607ed2011-03-03 17:26:50 -0800218 /**
219 * Based on the current deltas, we determine if and how to resize the widget.
220 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700221 private void resizeWidgetIfNeeded(boolean onDismiss) {
Adam Cohend4844c32011-02-18 19:25:06 -0800222 int xThreshold = mCellLayout.getCellWidth() + mCellLayout.getWidthGap();
223 int yThreshold = mCellLayout.getCellHeight() + mCellLayout.getHeightGap();
224
Adam Cohenbebf0422012-04-11 18:06:28 -0700225 int deltaX = mDeltaX + mDeltaXAddOn;
226 int deltaY = mDeltaY + mDeltaYAddOn;
227
228 float hSpanIncF = 1.0f * deltaX / xThreshold - mRunningHInc;
229 float vSpanIncF = 1.0f * deltaY / yThreshold - mRunningVInc;
Adam Cohene4b77292011-03-08 18:35:52 -0800230
231 int hSpanInc = 0;
232 int vSpanInc = 0;
Adam Cohend4844c32011-02-18 19:25:06 -0800233 int cellXInc = 0;
234 int cellYInc = 0;
235
Adam Cohenbebf0422012-04-11 18:06:28 -0700236 int countX = mCellLayout.getCountX();
237 int countY = mCellLayout.getCountY();
238
Adam Cohene4b77292011-03-08 18:35:52 -0800239 if (Math.abs(hSpanIncF) > RESIZE_THRESHOLD) {
240 hSpanInc = Math.round(hSpanIncF);
241 }
242 if (Math.abs(vSpanIncF) > RESIZE_THRESHOLD) {
243 vSpanInc = Math.round(vSpanIncF);
244 }
245
Adam Cohenbebf0422012-04-11 18:06:28 -0700246 if (!onDismiss && (hSpanInc == 0 && vSpanInc == 0)) return;
Adam Cohend4844c32011-02-18 19:25:06 -0800247
Adam Cohend4844c32011-02-18 19:25:06 -0800248
249 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) mWidgetView.getLayoutParams();
Adam Cohen1b607ed2011-03-03 17:26:50 -0800250
Adam Cohenbebf0422012-04-11 18:06:28 -0700251 int spanX = lp.cellHSpan;
252 int spanY = lp.cellVSpan;
253 int cellX = lp.useTmpCoords ? lp.tmpCellX : lp.cellX;
254 int cellY = lp.useTmpCoords ? lp.tmpCellY : lp.cellY;
255
256 int hSpanDelta = 0;
257 int vSpanDelta = 0;
258
Adam Cohen1b607ed2011-03-03 17:26:50 -0800259 // For each border, we bound the resizing based on the minimum width, and the maximum
260 // expandability.
Adam Cohend4844c32011-02-18 19:25:06 -0800261 if (mLeftBorderActive) {
Adam Cohenbebf0422012-04-11 18:06:28 -0700262 cellXInc = Math.max(-cellX, hSpanInc);
Adam Cohend4844c32011-02-18 19:25:06 -0800263 cellXInc = Math.min(lp.cellHSpan - mMinHSpan, cellXInc);
264 hSpanInc *= -1;
Adam Cohenbebf0422012-04-11 18:06:28 -0700265 hSpanInc = Math.min(cellX, hSpanInc);
Adam Cohend4844c32011-02-18 19:25:06 -0800266 hSpanInc = Math.max(-(lp.cellHSpan - mMinHSpan), hSpanInc);
Adam Cohenbebf0422012-04-11 18:06:28 -0700267 hSpanDelta = -hSpanInc;
268
Adam Cohend4844c32011-02-18 19:25:06 -0800269 } else if (mRightBorderActive) {
Adam Cohenbebf0422012-04-11 18:06:28 -0700270 hSpanInc = Math.min(countX - (cellX + spanX), hSpanInc);
Adam Cohend4844c32011-02-18 19:25:06 -0800271 hSpanInc = Math.max(-(lp.cellHSpan - mMinHSpan), hSpanInc);
Adam Cohenbebf0422012-04-11 18:06:28 -0700272 hSpanDelta = hSpanInc;
Adam Cohend4844c32011-02-18 19:25:06 -0800273 }
274
275 if (mTopBorderActive) {
Adam Cohenbebf0422012-04-11 18:06:28 -0700276 cellYInc = Math.max(-cellY, vSpanInc);
Adam Cohend4844c32011-02-18 19:25:06 -0800277 cellYInc = Math.min(lp.cellVSpan - mMinVSpan, cellYInc);
278 vSpanInc *= -1;
Adam Cohenbebf0422012-04-11 18:06:28 -0700279 vSpanInc = Math.min(cellY, vSpanInc);
Adam Cohend4844c32011-02-18 19:25:06 -0800280 vSpanInc = Math.max(-(lp.cellVSpan - mMinVSpan), vSpanInc);
Adam Cohenbebf0422012-04-11 18:06:28 -0700281 vSpanDelta = -vSpanInc;
Adam Cohend4844c32011-02-18 19:25:06 -0800282 } else if (mBottomBorderActive) {
Adam Cohenbebf0422012-04-11 18:06:28 -0700283 vSpanInc = Math.min(countY - (cellY + spanY), vSpanInc);
Adam Cohend4844c32011-02-18 19:25:06 -0800284 vSpanInc = Math.max(-(lp.cellVSpan - mMinVSpan), vSpanInc);
Adam Cohenbebf0422012-04-11 18:06:28 -0700285 vSpanDelta = vSpanInc;
Adam Cohend4844c32011-02-18 19:25:06 -0800286 }
287
Adam Cohenbebf0422012-04-11 18:06:28 -0700288 mDirectionVector[0] = 0;
289 mDirectionVector[1] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -0800290 // Update the widget's dimensions and position according to the deltas computed above
291 if (mLeftBorderActive || mRightBorderActive) {
Adam Cohenbebf0422012-04-11 18:06:28 -0700292 spanX += hSpanInc;
293 cellX += cellXInc;
294 mDirectionVector[0] = mLeftBorderActive ? -1 : 1;
Adam Cohend4844c32011-02-18 19:25:06 -0800295 }
296
297 if (mTopBorderActive || mBottomBorderActive) {
Adam Cohenbebf0422012-04-11 18:06:28 -0700298 spanY += vSpanInc;
299 cellY += cellYInc;
300 mDirectionVector[1] = mTopBorderActive ? -1 : 1;
Adam Cohend4844c32011-02-18 19:25:06 -0800301 }
302
Adam Cohenbebf0422012-04-11 18:06:28 -0700303 if (!onDismiss && vSpanDelta == 0 && hSpanDelta == 0) return;
Adam Cohend4844c32011-02-18 19:25:06 -0800304
Adam Cohenbebf0422012-04-11 18:06:28 -0700305 if (mCellLayout.createAreaForResize(cellX, cellY, spanX, spanY, mWidgetView,
306 mDirectionVector, onDismiss)) {
307 lp.tmpCellX = cellX;
308 lp.tmpCellY = cellY;
309 lp.cellHSpan = spanX;
310 lp.cellVSpan = spanY;
311 mRunningVInc += vSpanDelta;
312 mRunningHInc += hSpanDelta;
313 }
314
Adam Cohen67882692011-03-11 15:29:03 -0800315 mWidgetView.requestLayout();
Adam Cohend4844c32011-02-18 19:25:06 -0800316 }
317
Adam Cohen1b607ed2011-03-03 17:26:50 -0800318 /**
319 * This is the final step of the resize. Here we save the new widget size and position
320 * to LauncherModel and animate the resize frame.
321 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700322 public void commitResize() {
323 resizeWidgetIfNeeded(true);
324 requestLayout();
325 }
Adam Cohend4844c32011-02-18 19:25:06 -0800326
Adam Cohenbebf0422012-04-11 18:06:28 -0700327 public void onTouchUp() {
328 int xThreshold = mCellLayout.getCellWidth() + mCellLayout.getWidthGap();
329 int yThreshold = mCellLayout.getCellHeight() + mCellLayout.getHeightGap();
Adam Cohend4844c32011-02-18 19:25:06 -0800330
Adam Cohenbebf0422012-04-11 18:06:28 -0700331 mDeltaXAddOn = mRunningHInc * xThreshold;
332 mDeltaYAddOn = mRunningVInc * yThreshold;
333 mDeltaX = 0;
334 mDeltaY = 0;
335
Adam Cohend4844c32011-02-18 19:25:06 -0800336 post(new Runnable() {
Adam Cohenbebf0422012-04-11 18:06:28 -0700337 @Override
Adam Cohend4844c32011-02-18 19:25:06 -0800338 public void run() {
339 snapToWidget(true);
340 }
341 });
342 }
343
344 public void snapToWidget(boolean animate) {
Adam Cohen67882692011-03-11 15:29:03 -0800345 final DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Winson Chung4b825dcd2011-06-19 12:41:22 -0700346 int xOffset = mCellLayout.getLeft() + mCellLayout.getPaddingLeft() - mWorkspace.getScrollX();
347 int yOffset = mCellLayout.getTop() + mCellLayout.getPaddingTop() - mWorkspace.getScrollY();
Adam Cohend4844c32011-02-18 19:25:06 -0800348
Adam Cohen37b59ff2011-06-13 17:13:42 -0700349 int newWidth = mWidgetView.getWidth() + 2 * mBackgroundPadding - mWidgetPaddingLeft -
350 mWidgetPaddingRight;
351 int newHeight = mWidgetView.getHeight() + 2 * mBackgroundPadding - mWidgetPaddingTop -
352 mWidgetPaddingBottom;
353
354 int newX = mWidgetView.getLeft() - mBackgroundPadding + xOffset + mWidgetPaddingLeft;
355 int newY = mWidgetView.getTop() - mBackgroundPadding + yOffset + mWidgetPaddingTop;
Adam Cohen3cba7222011-03-02 19:03:11 -0800356
357 // We need to make sure the frame stays within the bounds of the CellLayout
358 if (newY < 0) {
359 newHeight -= -newY;
360 newY = 0;
361 }
Adam Cohen67882692011-03-11 15:29:03 -0800362 if (newY + newHeight > mDragLayer.getHeight()) {
363 newHeight -= newY + newHeight - mDragLayer.getHeight();
Adam Cohen3cba7222011-03-02 19:03:11 -0800364 }
365
Adam Cohend4844c32011-02-18 19:25:06 -0800366 if (!animate) {
367 lp.width = newWidth;
368 lp.height = newHeight;
369 lp.x = newX;
370 lp.y = newY;
371 mLeftHandle.setAlpha(1.0f);
372 mRightHandle.setAlpha(1.0f);
373 mTopHandle.setAlpha(1.0f);
374 mBottomHandle.setAlpha(1.0f);
375 requestLayout();
376 } else {
377 PropertyValuesHolder width = PropertyValuesHolder.ofInt("width", lp.width, newWidth);
Adam Cohen1b607ed2011-03-03 17:26:50 -0800378 PropertyValuesHolder height = PropertyValuesHolder.ofInt("height", lp.height,
379 newHeight);
Adam Cohend4844c32011-02-18 19:25:06 -0800380 PropertyValuesHolder x = PropertyValuesHolder.ofInt("x", lp.x, newX);
381 PropertyValuesHolder y = PropertyValuesHolder.ofInt("y", lp.y, newY);
382 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(lp, width, height, x, y);
383 ObjectAnimator leftOa = ObjectAnimator.ofFloat(mLeftHandle, "alpha", 1.0f);
384 ObjectAnimator rightOa = ObjectAnimator.ofFloat(mRightHandle, "alpha", 1.0f);
385 ObjectAnimator topOa = ObjectAnimator.ofFloat(mTopHandle, "alpha", 1.0f);
386 ObjectAnimator bottomOa = ObjectAnimator.ofFloat(mBottomHandle, "alpha", 1.0f);
387 oa.addUpdateListener(new AnimatorUpdateListener() {
388 public void onAnimationUpdate(ValueAnimator animation) {
389 requestLayout();
390 }
391 });
392 AnimatorSet set = new AnimatorSet();
393 if (mResizeMode == AppWidgetProviderInfo.RESIZE_VERTICAL) {
394 set.playTogether(oa, topOa, bottomOa);
395 } else if (mResizeMode == AppWidgetProviderInfo.RESIZE_HORIZONTAL) {
396 set.playTogether(oa, leftOa, rightOa);
397 } else {
398 set.playTogether(oa, leftOa, rightOa, topOa, bottomOa);
399 }
400
401 set.setDuration(SNAP_DURATION);
402 set.start();
403 }
404 }
405}