Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 1 | package com.android.launcher2; |
| 2 | |
| 3 | import android.animation.AnimatorSet; |
| 4 | import android.animation.ObjectAnimator; |
| 5 | import android.animation.PropertyValuesHolder; |
| 6 | import android.animation.ValueAnimator; |
| 7 | import android.animation.ValueAnimator.AnimatorUpdateListener; |
| 8 | import android.appwidget.AppWidgetProviderInfo; |
| 9 | import android.content.Context; |
| 10 | import android.view.Gravity; |
| 11 | import android.widget.FrameLayout; |
| 12 | import android.widget.ImageView; |
| 13 | |
| 14 | import com.android.launcher.R; |
| 15 | |
| 16 | public class AppWidgetResizeFrame extends FrameLayout { |
| 17 | |
| 18 | private ItemInfo mItemInfo; |
| 19 | private LauncherAppWidgetHostView mWidgetView; |
| 20 | private CellLayout mCellLayout; |
Adam Cohen | 6788269 | 2011-03-11 15:29:03 -0800 | [diff] [blame^] | 21 | private DragLayer mDragLayer; |
| 22 | private Workspace mWorkspace; |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 23 | private ImageView mLeftHandle; |
| 24 | private ImageView mRightHandle; |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 25 | private ImageView mTopHandle; |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 26 | private ImageView mBottomHandle; |
| 27 | |
| 28 | private boolean mLeftBorderActive; |
| 29 | private boolean mRightBorderActive; |
| 30 | private boolean mTopBorderActive; |
| 31 | private boolean mBottomBorderActive; |
| 32 | |
| 33 | private int mBaselineWidth; |
| 34 | private int mBaselineHeight; |
| 35 | private int mBaselineX; |
| 36 | private int mBaselineY; |
| 37 | private int mResizeMode; |
Adam Cohen | 3cba722 | 2011-03-02 19:03:11 -0800 | [diff] [blame] | 38 | |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 39 | private int mRunningHInc; |
| 40 | private int mRunningVInc; |
| 41 | private int mMinHSpan; |
| 42 | private int mMinVSpan; |
| 43 | private int mDeltaX; |
| 44 | private int mDeltaY; |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 45 | |
Adam Cohen | 3cba722 | 2011-03-02 19:03:11 -0800 | [diff] [blame] | 46 | private int mBackgroundPadding; |
| 47 | private int mTouchTargetWidth; |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 48 | |
| 49 | private int mExpandability[] = new int[4]; |
| 50 | |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 51 | final int SNAP_DURATION = 150; |
Adam Cohen | 3cba722 | 2011-03-02 19:03:11 -0800 | [diff] [blame] | 52 | final int BACKGROUND_PADDING = 24; |
Adam Cohen | e4b7729 | 2011-03-08 18:35:52 -0800 | [diff] [blame] | 53 | final float DIMMED_HANDLE_ALPHA = 0f; |
| 54 | final float RESIZE_THRESHOLD = 0.66f; |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 55 | |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 56 | public static final int LEFT = 0; |
| 57 | public static final int TOP = 1; |
| 58 | public static final int RIGHT = 2; |
| 59 | public static final int BOTTOM = 3; |
| 60 | |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 61 | public AppWidgetResizeFrame(Context context, ItemInfo itemInfo, |
Adam Cohen | 6788269 | 2011-03-11 15:29:03 -0800 | [diff] [blame^] | 62 | LauncherAppWidgetHostView widgetView, CellLayout cellLayout, DragLayer dragLayer) { |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 63 | |
| 64 | super(context); |
| 65 | mContext = context; |
| 66 | mItemInfo = itemInfo; |
| 67 | mCellLayout = cellLayout; |
| 68 | mWidgetView = widgetView; |
Adam Cohen | 27c09b0 | 2011-02-28 14:45:11 -0800 | [diff] [blame] | 69 | mResizeMode = widgetView.getAppWidgetInfo().resizeMode; |
Adam Cohen | 6788269 | 2011-03-11 15:29:03 -0800 | [diff] [blame^] | 70 | mDragLayer = dragLayer; |
| 71 | mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace); |
Adam Cohen | 3cba722 | 2011-03-02 19:03:11 -0800 | [diff] [blame] | 72 | |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 73 | final AppWidgetProviderInfo info = widgetView.getAppWidgetInfo(); |
| 74 | int[] result = mCellLayout.rectToCell(info.minWidth, info.minHeight, null); |
| 75 | mMinHSpan = result[0]; |
| 76 | mMinVSpan = result[1]; |
| 77 | |
Adam Cohen | 3cba722 | 2011-03-02 19:03:11 -0800 | [diff] [blame] | 78 | setBackgroundResource(R.drawable.widget_resize_frame_holo); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 79 | setPadding(0, 0, 0, 0); |
| 80 | |
| 81 | LayoutParams lp; |
| 82 | mLeftHandle = new ImageView(context); |
Adam Cohen | 3cba722 | 2011-03-02 19:03:11 -0800 | [diff] [blame] | 83 | mLeftHandle.setImageResource(R.drawable.widget_resize_handle_left); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 84 | lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, |
| 85 | Gravity.LEFT | Gravity.CENTER_VERTICAL); |
| 86 | addView(mLeftHandle, lp); |
| 87 | |
| 88 | mRightHandle = new ImageView(context); |
Adam Cohen | 3cba722 | 2011-03-02 19:03:11 -0800 | [diff] [blame] | 89 | mRightHandle.setImageResource(R.drawable.widget_resize_handle_right); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 90 | lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, |
| 91 | Gravity.RIGHT | Gravity.CENTER_VERTICAL); |
| 92 | addView(mRightHandle, lp); |
| 93 | |
| 94 | mTopHandle = new ImageView(context); |
Adam Cohen | 3cba722 | 2011-03-02 19:03:11 -0800 | [diff] [blame] | 95 | mTopHandle.setImageResource(R.drawable.widget_resize_handle_top); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 96 | lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, |
| 97 | Gravity.CENTER_HORIZONTAL | Gravity.TOP); |
| 98 | addView(mTopHandle, lp); |
| 99 | |
| 100 | mBottomHandle = new ImageView(context); |
Adam Cohen | 3cba722 | 2011-03-02 19:03:11 -0800 | [diff] [blame] | 101 | mBottomHandle.setImageResource(R.drawable.widget_resize_handle_bottom); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 102 | lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, |
| 103 | Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM); |
| 104 | addView(mBottomHandle, lp); |
| 105 | |
| 106 | if (mResizeMode == AppWidgetProviderInfo.RESIZE_HORIZONTAL) { |
| 107 | mTopHandle.setVisibility(GONE); |
| 108 | mBottomHandle.setVisibility(GONE); |
| 109 | } else if (mResizeMode == AppWidgetProviderInfo.RESIZE_VERTICAL) { |
| 110 | mLeftHandle.setVisibility(GONE); |
| 111 | mRightHandle.setVisibility(GONE); |
Adam Cohen | 3cba722 | 2011-03-02 19:03:11 -0800 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | final float density = mContext.getResources().getDisplayMetrics().density; |
| 115 | mBackgroundPadding = (int) Math.ceil(density * BACKGROUND_PADDING); |
| 116 | mTouchTargetWidth = 2 * mBackgroundPadding; |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | public boolean beginResizeIfPointInRegion(int x, int y) { |
| 120 | boolean horizontalActive = (mResizeMode & AppWidgetProviderInfo.RESIZE_HORIZONTAL) != 0; |
| 121 | boolean verticalActive = (mResizeMode & AppWidgetProviderInfo.RESIZE_VERTICAL) != 0; |
Adam Cohen | 3cba722 | 2011-03-02 19:03:11 -0800 | [diff] [blame] | 122 | mLeftBorderActive = (x < mTouchTargetWidth) && horizontalActive; |
| 123 | mRightBorderActive = (x > getWidth() - mTouchTargetWidth) && horizontalActive; |
| 124 | mTopBorderActive = (y < mTouchTargetWidth) && verticalActive; |
| 125 | mBottomBorderActive = (y > getHeight() - mTouchTargetWidth) && verticalActive; |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 126 | |
| 127 | boolean anyBordersActive = mLeftBorderActive || mRightBorderActive |
| 128 | || mTopBorderActive || mBottomBorderActive; |
| 129 | |
| 130 | mBaselineWidth = getMeasuredWidth(); |
| 131 | mBaselineHeight = getMeasuredHeight(); |
| 132 | mBaselineX = getLeft(); |
| 133 | mBaselineY = getTop(); |
| 134 | mRunningHInc = 0; |
| 135 | mRunningVInc = 0; |
| 136 | |
| 137 | if (anyBordersActive) { |
Adam Cohen | 3cba722 | 2011-03-02 19:03:11 -0800 | [diff] [blame] | 138 | mLeftHandle.setAlpha(mLeftBorderActive ? 1.0f : DIMMED_HANDLE_ALPHA); |
| 139 | mRightHandle.setAlpha(mRightBorderActive ? 1.0f :DIMMED_HANDLE_ALPHA); |
| 140 | mTopHandle.setAlpha(mTopBorderActive ? 1.0f : DIMMED_HANDLE_ALPHA); |
| 141 | mBottomHandle.setAlpha(mBottomBorderActive ? 1.0f : DIMMED_HANDLE_ALPHA); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 142 | } |
| 143 | mCellLayout.getExpandabilityArrayForView(mWidgetView, mExpandability); |
Adam Cohen | 3cba722 | 2011-03-02 19:03:11 -0800 | [diff] [blame] | 144 | |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 145 | return anyBordersActive; |
| 146 | } |
| 147 | |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 148 | /** |
| 149 | * Here we bound the deltas such that the frame cannot be stretched beyond the extents |
| 150 | * of the CellLayout, and such that the frame's borders can't cross. |
| 151 | */ |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 152 | public void updateDeltas(int deltaX, int deltaY) { |
| 153 | if (mLeftBorderActive) { |
| 154 | mDeltaX = Math.max(-mBaselineX, deltaX); |
Adam Cohen | 3cba722 | 2011-03-02 19:03:11 -0800 | [diff] [blame] | 155 | mDeltaX = Math.min(mBaselineWidth - 2 * mTouchTargetWidth, mDeltaX); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 156 | } else if (mRightBorderActive) { |
Adam Cohen | 6788269 | 2011-03-11 15:29:03 -0800 | [diff] [blame^] | 157 | mDeltaX = Math.min(mDragLayer.getWidth() - (mBaselineX + mBaselineWidth), deltaX); |
Adam Cohen | 3cba722 | 2011-03-02 19:03:11 -0800 | [diff] [blame] | 158 | mDeltaX = Math.max(-mBaselineWidth + 2 * mTouchTargetWidth, mDeltaX); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | if (mTopBorderActive) { |
| 162 | mDeltaY = Math.max(-mBaselineY, deltaY); |
Adam Cohen | 3cba722 | 2011-03-02 19:03:11 -0800 | [diff] [blame] | 163 | mDeltaY = Math.min(mBaselineHeight - 2 * mTouchTargetWidth, mDeltaY); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 164 | } else if (mBottomBorderActive) { |
Adam Cohen | 6788269 | 2011-03-11 15:29:03 -0800 | [diff] [blame^] | 165 | mDeltaY = Math.min(mDragLayer.getHeight() - (mBaselineY + mBaselineHeight), deltaY); |
Adam Cohen | 3cba722 | 2011-03-02 19:03:11 -0800 | [diff] [blame] | 166 | mDeltaY = Math.max(-mBaselineHeight + 2 * mTouchTargetWidth, mDeltaY); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 170 | /** |
| 171 | * Based on the deltas, we resize the frame, and, if needed, we resize the widget. |
| 172 | */ |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 173 | public void visualizeResizeForDelta(int deltaX, int deltaY) { |
| 174 | updateDeltas(deltaX, deltaY); |
Adam Cohen | 6788269 | 2011-03-11 15:29:03 -0800 | [diff] [blame^] | 175 | DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams(); |
| 176 | |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 177 | if (mLeftBorderActive) { |
| 178 | lp.x = mBaselineX + mDeltaX; |
| 179 | lp.width = mBaselineWidth - mDeltaX; |
| 180 | } else if (mRightBorderActive) { |
| 181 | lp.width = mBaselineWidth + mDeltaX; |
| 182 | } |
| 183 | |
| 184 | if (mTopBorderActive) { |
| 185 | lp.y = mBaselineY + mDeltaY; |
| 186 | lp.height = mBaselineHeight - mDeltaY; |
| 187 | } else if (mBottomBorderActive) { |
| 188 | lp.height = mBaselineHeight + mDeltaY; |
| 189 | } |
| 190 | |
| 191 | resizeWidgetIfNeeded(); |
| 192 | requestLayout(); |
| 193 | } |
| 194 | |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 195 | /** |
| 196 | * Based on the current deltas, we determine if and how to resize the widget. |
| 197 | */ |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 198 | private void resizeWidgetIfNeeded() { |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 199 | int xThreshold = mCellLayout.getCellWidth() + mCellLayout.getWidthGap(); |
| 200 | int yThreshold = mCellLayout.getCellHeight() + mCellLayout.getHeightGap(); |
| 201 | |
Adam Cohen | e4b7729 | 2011-03-08 18:35:52 -0800 | [diff] [blame] | 202 | float hSpanIncF = 1.0f * mDeltaX / xThreshold - mRunningHInc; |
| 203 | float vSpanIncF = 1.0f * mDeltaY / yThreshold - mRunningVInc; |
| 204 | |
| 205 | int hSpanInc = 0; |
| 206 | int vSpanInc = 0; |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 207 | int cellXInc = 0; |
| 208 | int cellYInc = 0; |
| 209 | |
Adam Cohen | e4b7729 | 2011-03-08 18:35:52 -0800 | [diff] [blame] | 210 | if (Math.abs(hSpanIncF) > RESIZE_THRESHOLD) { |
| 211 | hSpanInc = Math.round(hSpanIncF); |
| 212 | } |
| 213 | if (Math.abs(vSpanIncF) > RESIZE_THRESHOLD) { |
| 214 | vSpanInc = Math.round(vSpanIncF); |
| 215 | } |
| 216 | |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 217 | if (hSpanInc == 0 && vSpanInc == 0) return; |
| 218 | |
| 219 | // Before we change the widget, we clear the occupied cells associated with it. |
| 220 | // The new set of occupied cells is marked below, once the layout params are updated. |
| 221 | mCellLayout.markCellsAsUnoccupiedForView(mWidgetView); |
| 222 | |
| 223 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) mWidgetView.getLayoutParams(); |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 224 | |
| 225 | // For each border, we bound the resizing based on the minimum width, and the maximum |
| 226 | // expandability. |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 227 | if (mLeftBorderActive) { |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 228 | cellXInc = Math.max(-mExpandability[LEFT], hSpanInc); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 229 | cellXInc = Math.min(lp.cellHSpan - mMinHSpan, cellXInc); |
| 230 | hSpanInc *= -1; |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 231 | hSpanInc = Math.min(mExpandability[LEFT], hSpanInc); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 232 | hSpanInc = Math.max(-(lp.cellHSpan - mMinHSpan), hSpanInc); |
| 233 | mRunningHInc -= hSpanInc; |
| 234 | } else if (mRightBorderActive) { |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 235 | hSpanInc = Math.min(mExpandability[RIGHT], hSpanInc); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 236 | hSpanInc = Math.max(-(lp.cellHSpan - mMinHSpan), hSpanInc); |
| 237 | mRunningHInc += hSpanInc; |
| 238 | } |
| 239 | |
| 240 | if (mTopBorderActive) { |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 241 | cellYInc = Math.max(-mExpandability[TOP], vSpanInc); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 242 | cellYInc = Math.min(lp.cellVSpan - mMinVSpan, cellYInc); |
| 243 | vSpanInc *= -1; |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 244 | vSpanInc = Math.min(mExpandability[TOP], vSpanInc); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 245 | vSpanInc = Math.max(-(lp.cellVSpan - mMinVSpan), vSpanInc); |
| 246 | mRunningVInc -= vSpanInc; |
| 247 | } else if (mBottomBorderActive) { |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 248 | vSpanInc = Math.min(mExpandability[BOTTOM], vSpanInc); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 249 | vSpanInc = Math.max(-(lp.cellVSpan - mMinVSpan), vSpanInc); |
| 250 | mRunningVInc += vSpanInc; |
| 251 | } |
| 252 | |
| 253 | // Update the widget's dimensions and position according to the deltas computed above |
| 254 | if (mLeftBorderActive || mRightBorderActive) { |
| 255 | lp.cellHSpan += hSpanInc; |
| 256 | lp.cellX += cellXInc; |
| 257 | } |
| 258 | |
| 259 | if (mTopBorderActive || mBottomBorderActive) { |
| 260 | lp.cellVSpan += vSpanInc; |
| 261 | lp.cellY += cellYInc; |
| 262 | } |
| 263 | |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 264 | // Update the expandability array, as we have changed the widget's size. |
Adam Cohen | 3cba722 | 2011-03-02 19:03:11 -0800 | [diff] [blame] | 265 | mCellLayout.getExpandabilityArrayForView(mWidgetView, mExpandability); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 266 | |
| 267 | // Update the cells occupied by this widget |
| 268 | mCellLayout.markCellsAsOccupiedForView(mWidgetView); |
Adam Cohen | 6788269 | 2011-03-11 15:29:03 -0800 | [diff] [blame^] | 269 | mWidgetView.requestLayout(); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 270 | } |
| 271 | |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 272 | /** |
| 273 | * This is the final step of the resize. Here we save the new widget size and position |
| 274 | * to LauncherModel and animate the resize frame. |
| 275 | */ |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 276 | public void commitResizeForDelta(int deltaX, int deltaY) { |
| 277 | visualizeResizeForDelta(deltaX, deltaY); |
| 278 | |
| 279 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) mWidgetView.getLayoutParams(); |
| 280 | LauncherModel.resizeItemInDatabase(getContext(), mItemInfo, lp.cellX, lp.cellY, |
| 281 | lp.cellHSpan, lp.cellVSpan); |
| 282 | mWidgetView.requestLayout(); |
| 283 | |
| 284 | // Once our widget resizes (hence the post), we want to snap the resize frame to it |
| 285 | post(new Runnable() { |
| 286 | public void run() { |
| 287 | snapToWidget(true); |
| 288 | } |
| 289 | }); |
| 290 | } |
| 291 | |
| 292 | public void snapToWidget(boolean animate) { |
Adam Cohen | 6788269 | 2011-03-11 15:29:03 -0800 | [diff] [blame^] | 293 | final DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams(); |
| 294 | int xOffset = mCellLayout.getLeft() - mWorkspace.getScrollX(); |
| 295 | int yOffset = mCellLayout.getTop() - mWorkspace.getScrollY(); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 296 | |
Adam Cohen | 3cba722 | 2011-03-02 19:03:11 -0800 | [diff] [blame] | 297 | int newWidth = mWidgetView.getWidth() + 2 * mBackgroundPadding; |
| 298 | int newHeight = mWidgetView.getHeight() + 2 * mBackgroundPadding; |
Adam Cohen | 6788269 | 2011-03-11 15:29:03 -0800 | [diff] [blame^] | 299 | int newX = mWidgetView.getLeft() - mBackgroundPadding + xOffset; |
| 300 | int newY = mWidgetView.getTop() - mBackgroundPadding + yOffset; |
Adam Cohen | 3cba722 | 2011-03-02 19:03:11 -0800 | [diff] [blame] | 301 | |
| 302 | // We need to make sure the frame stays within the bounds of the CellLayout |
| 303 | if (newY < 0) { |
| 304 | newHeight -= -newY; |
| 305 | newY = 0; |
| 306 | } |
Adam Cohen | 6788269 | 2011-03-11 15:29:03 -0800 | [diff] [blame^] | 307 | if (newY + newHeight > mDragLayer.getHeight()) { |
| 308 | newHeight -= newY + newHeight - mDragLayer.getHeight(); |
Adam Cohen | 3cba722 | 2011-03-02 19:03:11 -0800 | [diff] [blame] | 309 | } |
| 310 | |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 311 | if (!animate) { |
| 312 | lp.width = newWidth; |
| 313 | lp.height = newHeight; |
| 314 | lp.x = newX; |
| 315 | lp.y = newY; |
| 316 | mLeftHandle.setAlpha(1.0f); |
| 317 | mRightHandle.setAlpha(1.0f); |
| 318 | mTopHandle.setAlpha(1.0f); |
| 319 | mBottomHandle.setAlpha(1.0f); |
| 320 | requestLayout(); |
| 321 | } else { |
| 322 | PropertyValuesHolder width = PropertyValuesHolder.ofInt("width", lp.width, newWidth); |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 323 | PropertyValuesHolder height = PropertyValuesHolder.ofInt("height", lp.height, |
| 324 | newHeight); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 325 | PropertyValuesHolder x = PropertyValuesHolder.ofInt("x", lp.x, newX); |
| 326 | PropertyValuesHolder y = PropertyValuesHolder.ofInt("y", lp.y, newY); |
| 327 | ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(lp, width, height, x, y); |
| 328 | ObjectAnimator leftOa = ObjectAnimator.ofFloat(mLeftHandle, "alpha", 1.0f); |
| 329 | ObjectAnimator rightOa = ObjectAnimator.ofFloat(mRightHandle, "alpha", 1.0f); |
| 330 | ObjectAnimator topOa = ObjectAnimator.ofFloat(mTopHandle, "alpha", 1.0f); |
| 331 | ObjectAnimator bottomOa = ObjectAnimator.ofFloat(mBottomHandle, "alpha", 1.0f); |
| 332 | oa.addUpdateListener(new AnimatorUpdateListener() { |
| 333 | public void onAnimationUpdate(ValueAnimator animation) { |
| 334 | requestLayout(); |
| 335 | } |
| 336 | }); |
| 337 | AnimatorSet set = new AnimatorSet(); |
| 338 | if (mResizeMode == AppWidgetProviderInfo.RESIZE_VERTICAL) { |
| 339 | set.playTogether(oa, topOa, bottomOa); |
| 340 | } else if (mResizeMode == AppWidgetProviderInfo.RESIZE_HORIZONTAL) { |
| 341 | set.playTogether(oa, leftOa, rightOa); |
| 342 | } else { |
| 343 | set.playTogether(oa, leftOa, rightOa, topOa, bottomOa); |
| 344 | } |
| 345 | |
| 346 | set.setDuration(SNAP_DURATION); |
| 347 | set.start(); |
| 348 | } |
| 349 | } |
| 350 | } |