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