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