Daniel Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 1 | package com.android.launcher3; |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 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 | |
Daniel Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 16 | import com.android.launcher3.R; |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 17 | |
| 18 | public class AppWidgetResizeFrame extends FrameLayout { |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 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 | |
Adam Cohen | 37b59ff | 2011-06-13 17:13:42 -0700 | [diff] [blame] | 33 | private int mWidgetPaddingLeft; |
| 34 | private int mWidgetPaddingRight; |
| 35 | private int mWidgetPaddingTop; |
| 36 | private int mWidgetPaddingBottom; |
| 37 | |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 38 | private int mBaselineWidth; |
| 39 | private int mBaselineHeight; |
| 40 | private int mBaselineX; |
| 41 | private int mBaselineY; |
| 42 | private int mResizeMode; |
Adam Cohen | 3cba722 | 2011-03-02 19:03:11 -0800 | [diff] [blame] | 43 | |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 44 | private int mRunningHInc; |
| 45 | private int mRunningVInc; |
| 46 | private int mMinHSpan; |
| 47 | private int mMinVSpan; |
| 48 | private int mDeltaX; |
| 49 | private int mDeltaY; |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 50 | private int mDeltaXAddOn; |
| 51 | private int mDeltaYAddOn; |
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 | |
Adam Cohen | 4459d6b | 2012-07-13 15:59:15 -0700 | [diff] [blame] | 56 | private int mTopTouchRegionAdjustment = 0; |
| 57 | private int mBottomTouchRegionAdjustment = 0; |
| 58 | |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 59 | int[] mDirectionVector = new int[2]; |
Adam Cohen | e048950 | 2012-08-27 15:18:53 -0700 | [diff] [blame] | 60 | int[] mLastDirectionVector = new int[2]; |
Adam Cohen | d6e7aa3 | 2013-07-09 15:32:37 -0700 | [diff] [blame^] | 61 | int[] mTmpPt = new int[2]; |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 62 | |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 63 | final int SNAP_DURATION = 150; |
Adam Cohen | 3cba722 | 2011-03-02 19:03:11 -0800 | [diff] [blame] | 64 | final int BACKGROUND_PADDING = 24; |
Adam Cohen | e4b7729 | 2011-03-08 18:35:52 -0800 | [diff] [blame] | 65 | final float DIMMED_HANDLE_ALPHA = 0f; |
| 66 | final float RESIZE_THRESHOLD = 0.66f; |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 67 | |
Adam Cohen | 9e05a5e | 2012-09-10 15:53:09 -0700 | [diff] [blame] | 68 | private static Rect mTmpRect = new Rect(); |
| 69 | |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 70 | public static final int LEFT = 0; |
| 71 | public static final int TOP = 1; |
| 72 | public static final int RIGHT = 2; |
| 73 | public static final int BOTTOM = 3; |
| 74 | |
Adam Cohen | cbf47e3 | 2011-09-16 17:32:37 -0700 | [diff] [blame] | 75 | private Launcher mLauncher; |
| 76 | |
Michael Jurka | 3a9fced | 2012-04-13 14:44:29 -0700 | [diff] [blame] | 77 | public AppWidgetResizeFrame(Context context, |
Adam Cohen | 6788269 | 2011-03-11 15:29:03 -0800 | [diff] [blame] | 78 | LauncherAppWidgetHostView widgetView, CellLayout cellLayout, DragLayer dragLayer) { |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 79 | |
| 80 | super(context); |
Adam Cohen | cbf47e3 | 2011-09-16 17:32:37 -0700 | [diff] [blame] | 81 | mLauncher = (Launcher) context; |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 82 | mCellLayout = cellLayout; |
| 83 | mWidgetView = widgetView; |
Adam Cohen | 27c09b0 | 2011-02-28 14:45:11 -0800 | [diff] [blame] | 84 | mResizeMode = widgetView.getAppWidgetInfo().resizeMode; |
Adam Cohen | 6788269 | 2011-03-11 15:29:03 -0800 | [diff] [blame] | 85 | mDragLayer = dragLayer; |
| 86 | mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace); |
Adam Cohen | 3cba722 | 2011-03-02 19:03:11 -0800 | [diff] [blame] | 87 | |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 88 | final AppWidgetProviderInfo info = widgetView.getAppWidgetInfo(); |
Adam Cohen | 2f093b6 | 2012-04-30 18:59:53 -0700 | [diff] [blame] | 89 | int[] result = Launcher.getMinSpanForWidget(mLauncher, info); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 90 | mMinHSpan = result[0]; |
| 91 | mMinVSpan = result[1]; |
| 92 | |
Adam Cohen | 3cba722 | 2011-03-02 19:03:11 -0800 | [diff] [blame] | 93 | setBackgroundResource(R.drawable.widget_resize_frame_holo); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 94 | setPadding(0, 0, 0, 0); |
| 95 | |
| 96 | LayoutParams lp; |
| 97 | mLeftHandle = new ImageView(context); |
Adam Cohen | 3cba722 | 2011-03-02 19:03:11 -0800 | [diff] [blame] | 98 | mLeftHandle.setImageResource(R.drawable.widget_resize_handle_left); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 99 | lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, |
Fabrice Di Meglio | cc11f74 | 2012-12-18 16:25:49 -0800 | [diff] [blame] | 100 | Gravity.START | Gravity.CENTER_VERTICAL); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 101 | addView(mLeftHandle, lp); |
| 102 | |
| 103 | mRightHandle = new ImageView(context); |
Adam Cohen | 3cba722 | 2011-03-02 19:03:11 -0800 | [diff] [blame] | 104 | mRightHandle.setImageResource(R.drawable.widget_resize_handle_right); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 105 | lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, |
Fabrice Di Meglio | cc11f74 | 2012-12-18 16:25:49 -0800 | [diff] [blame] | 106 | Gravity.END | Gravity.CENTER_VERTICAL); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 107 | addView(mRightHandle, lp); |
| 108 | |
| 109 | mTopHandle = new ImageView(context); |
Adam Cohen | 3cba722 | 2011-03-02 19:03:11 -0800 | [diff] [blame] | 110 | mTopHandle.setImageResource(R.drawable.widget_resize_handle_top); |
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.TOP); |
| 113 | addView(mTopHandle, lp); |
| 114 | |
| 115 | mBottomHandle = new ImageView(context); |
Adam Cohen | 3cba722 | 2011-03-02 19:03:11 -0800 | [diff] [blame] | 116 | mBottomHandle.setImageResource(R.drawable.widget_resize_handle_bottom); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 117 | lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, |
| 118 | Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM); |
| 119 | addView(mBottomHandle, lp); |
| 120 | |
Adam Cohen | 0cf2a7c | 2011-11-08 15:07:01 -0800 | [diff] [blame] | 121 | Rect p = AppWidgetHostView.getDefaultPaddingForWidget(context, |
| 122 | widgetView.getAppWidgetInfo().provider, null); |
Adam Cohen | 82ebbd2 | 2011-09-30 15:05:40 -0700 | [diff] [blame] | 123 | mWidgetPaddingLeft = p.left; |
| 124 | mWidgetPaddingTop = p.top; |
| 125 | mWidgetPaddingRight = p.right; |
| 126 | mWidgetPaddingBottom = p.bottom; |
Adam Cohen | 37b59ff | 2011-06-13 17:13:42 -0700 | [diff] [blame] | 127 | |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 128 | if (mResizeMode == AppWidgetProviderInfo.RESIZE_HORIZONTAL) { |
| 129 | mTopHandle.setVisibility(GONE); |
| 130 | mBottomHandle.setVisibility(GONE); |
| 131 | } else if (mResizeMode == AppWidgetProviderInfo.RESIZE_VERTICAL) { |
| 132 | mLeftHandle.setVisibility(GONE); |
| 133 | mRightHandle.setVisibility(GONE); |
Adam Cohen | 3cba722 | 2011-03-02 19:03:11 -0800 | [diff] [blame] | 134 | } |
| 135 | |
Adam Cohen | cbf47e3 | 2011-09-16 17:32:37 -0700 | [diff] [blame] | 136 | final float density = mLauncher.getResources().getDisplayMetrics().density; |
Adam Cohen | 3cba722 | 2011-03-02 19:03:11 -0800 | [diff] [blame] | 137 | mBackgroundPadding = (int) Math.ceil(density * BACKGROUND_PADDING); |
| 138 | mTouchTargetWidth = 2 * mBackgroundPadding; |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 139 | |
| 140 | // When we create the resize frame, we first mark all cells as unoccupied. The appropriate |
| 141 | // cells (same if not resized, or different) will be marked as occupied when the resize |
| 142 | // frame is dismissed. |
| 143 | mCellLayout.markCellsAsUnoccupiedForView(mWidgetView); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | public boolean beginResizeIfPointInRegion(int x, int y) { |
| 147 | boolean horizontalActive = (mResizeMode & AppWidgetProviderInfo.RESIZE_HORIZONTAL) != 0; |
| 148 | boolean verticalActive = (mResizeMode & AppWidgetProviderInfo.RESIZE_VERTICAL) != 0; |
Adam Cohen | 4459d6b | 2012-07-13 15:59:15 -0700 | [diff] [blame] | 149 | |
Adam Cohen | 3cba722 | 2011-03-02 19:03:11 -0800 | [diff] [blame] | 150 | mLeftBorderActive = (x < mTouchTargetWidth) && horizontalActive; |
| 151 | mRightBorderActive = (x > getWidth() - mTouchTargetWidth) && horizontalActive; |
Adam Cohen | 4459d6b | 2012-07-13 15:59:15 -0700 | [diff] [blame] | 152 | mTopBorderActive = (y < mTouchTargetWidth + mTopTouchRegionAdjustment) && verticalActive; |
| 153 | mBottomBorderActive = (y > getHeight() - mTouchTargetWidth + mBottomTouchRegionAdjustment) |
| 154 | && verticalActive; |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 155 | |
| 156 | boolean anyBordersActive = mLeftBorderActive || mRightBorderActive |
| 157 | || mTopBorderActive || mBottomBorderActive; |
| 158 | |
| 159 | mBaselineWidth = getMeasuredWidth(); |
| 160 | mBaselineHeight = getMeasuredHeight(); |
| 161 | mBaselineX = getLeft(); |
| 162 | mBaselineY = getTop(); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 163 | |
| 164 | if (anyBordersActive) { |
Adam Cohen | 3cba722 | 2011-03-02 19:03:11 -0800 | [diff] [blame] | 165 | mLeftHandle.setAlpha(mLeftBorderActive ? 1.0f : DIMMED_HANDLE_ALPHA); |
| 166 | mRightHandle.setAlpha(mRightBorderActive ? 1.0f :DIMMED_HANDLE_ALPHA); |
| 167 | mTopHandle.setAlpha(mTopBorderActive ? 1.0f : DIMMED_HANDLE_ALPHA); |
| 168 | mBottomHandle.setAlpha(mBottomBorderActive ? 1.0f : DIMMED_HANDLE_ALPHA); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 169 | } |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 170 | return anyBordersActive; |
| 171 | } |
| 172 | |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 173 | /** |
| 174 | * Here we bound the deltas such that the frame cannot be stretched beyond the extents |
| 175 | * of the CellLayout, and such that the frame's borders can't cross. |
| 176 | */ |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 177 | public void updateDeltas(int deltaX, int deltaY) { |
| 178 | if (mLeftBorderActive) { |
| 179 | mDeltaX = Math.max(-mBaselineX, deltaX); |
Adam Cohen | 3cba722 | 2011-03-02 19:03:11 -0800 | [diff] [blame] | 180 | mDeltaX = Math.min(mBaselineWidth - 2 * mTouchTargetWidth, mDeltaX); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 181 | } else if (mRightBorderActive) { |
Adam Cohen | 6788269 | 2011-03-11 15:29:03 -0800 | [diff] [blame] | 182 | mDeltaX = Math.min(mDragLayer.getWidth() - (mBaselineX + mBaselineWidth), deltaX); |
Adam Cohen | 3cba722 | 2011-03-02 19:03:11 -0800 | [diff] [blame] | 183 | mDeltaX = Math.max(-mBaselineWidth + 2 * mTouchTargetWidth, mDeltaX); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | if (mTopBorderActive) { |
| 187 | mDeltaY = Math.max(-mBaselineY, deltaY); |
Adam Cohen | 3cba722 | 2011-03-02 19:03:11 -0800 | [diff] [blame] | 188 | mDeltaY = Math.min(mBaselineHeight - 2 * mTouchTargetWidth, mDeltaY); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 189 | } else if (mBottomBorderActive) { |
Adam Cohen | 6788269 | 2011-03-11 15:29:03 -0800 | [diff] [blame] | 190 | mDeltaY = Math.min(mDragLayer.getHeight() - (mBaselineY + mBaselineHeight), deltaY); |
Adam Cohen | 3cba722 | 2011-03-02 19:03:11 -0800 | [diff] [blame] | 191 | mDeltaY = Math.max(-mBaselineHeight + 2 * mTouchTargetWidth, mDeltaY); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 195 | public void visualizeResizeForDelta(int deltaX, int deltaY) { |
| 196 | visualizeResizeForDelta(deltaX, deltaY, false); |
| 197 | } |
| 198 | |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 199 | /** |
| 200 | * Based on the deltas, we resize the frame, and, if needed, we resize the widget. |
| 201 | */ |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 202 | private void visualizeResizeForDelta(int deltaX, int deltaY, boolean onDismiss) { |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 203 | updateDeltas(deltaX, deltaY); |
Adam Cohen | 6788269 | 2011-03-11 15:29:03 -0800 | [diff] [blame] | 204 | DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams(); |
| 205 | |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 206 | if (mLeftBorderActive) { |
| 207 | lp.x = mBaselineX + mDeltaX; |
| 208 | lp.width = mBaselineWidth - mDeltaX; |
| 209 | } else if (mRightBorderActive) { |
| 210 | lp.width = mBaselineWidth + mDeltaX; |
| 211 | } |
| 212 | |
| 213 | if (mTopBorderActive) { |
| 214 | lp.y = mBaselineY + mDeltaY; |
| 215 | lp.height = mBaselineHeight - mDeltaY; |
| 216 | } else if (mBottomBorderActive) { |
| 217 | lp.height = mBaselineHeight + mDeltaY; |
| 218 | } |
| 219 | |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 220 | resizeWidgetIfNeeded(onDismiss); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 221 | requestLayout(); |
| 222 | } |
| 223 | |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 224 | /** |
| 225 | * Based on the current deltas, we determine if and how to resize the widget. |
| 226 | */ |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 227 | private void resizeWidgetIfNeeded(boolean onDismiss) { |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 228 | int xThreshold = mCellLayout.getCellWidth() + mCellLayout.getWidthGap(); |
| 229 | int yThreshold = mCellLayout.getCellHeight() + mCellLayout.getHeightGap(); |
| 230 | |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 231 | int deltaX = mDeltaX + mDeltaXAddOn; |
| 232 | int deltaY = mDeltaY + mDeltaYAddOn; |
| 233 | |
| 234 | float hSpanIncF = 1.0f * deltaX / xThreshold - mRunningHInc; |
| 235 | float vSpanIncF = 1.0f * deltaY / yThreshold - mRunningVInc; |
Adam Cohen | e4b7729 | 2011-03-08 18:35:52 -0800 | [diff] [blame] | 236 | |
| 237 | int hSpanInc = 0; |
| 238 | int vSpanInc = 0; |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 239 | int cellXInc = 0; |
| 240 | int cellYInc = 0; |
| 241 | |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 242 | int countX = mCellLayout.getCountX(); |
| 243 | int countY = mCellLayout.getCountY(); |
| 244 | |
Adam Cohen | e4b7729 | 2011-03-08 18:35:52 -0800 | [diff] [blame] | 245 | if (Math.abs(hSpanIncF) > RESIZE_THRESHOLD) { |
| 246 | hSpanInc = Math.round(hSpanIncF); |
| 247 | } |
| 248 | if (Math.abs(vSpanIncF) > RESIZE_THRESHOLD) { |
| 249 | vSpanInc = Math.round(vSpanIncF); |
| 250 | } |
| 251 | |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 252 | if (!onDismiss && (hSpanInc == 0 && vSpanInc == 0)) return; |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 253 | |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 254 | |
| 255 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) mWidgetView.getLayoutParams(); |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 256 | |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 257 | int spanX = lp.cellHSpan; |
| 258 | int spanY = lp.cellVSpan; |
| 259 | int cellX = lp.useTmpCoords ? lp.tmpCellX : lp.cellX; |
| 260 | int cellY = lp.useTmpCoords ? lp.tmpCellY : lp.cellY; |
| 261 | |
| 262 | int hSpanDelta = 0; |
| 263 | int vSpanDelta = 0; |
| 264 | |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 265 | // For each border, we bound the resizing based on the minimum width, and the maximum |
| 266 | // expandability. |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 267 | if (mLeftBorderActive) { |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 268 | cellXInc = Math.max(-cellX, hSpanInc); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 269 | cellXInc = Math.min(lp.cellHSpan - mMinHSpan, cellXInc); |
| 270 | hSpanInc *= -1; |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 271 | hSpanInc = Math.min(cellX, hSpanInc); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 272 | hSpanInc = Math.max(-(lp.cellHSpan - mMinHSpan), hSpanInc); |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 273 | hSpanDelta = -hSpanInc; |
| 274 | |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 275 | } else if (mRightBorderActive) { |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 276 | hSpanInc = Math.min(countX - (cellX + spanX), hSpanInc); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 277 | hSpanInc = Math.max(-(lp.cellHSpan - mMinHSpan), hSpanInc); |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 278 | hSpanDelta = hSpanInc; |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | if (mTopBorderActive) { |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 282 | cellYInc = Math.max(-cellY, vSpanInc); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 283 | cellYInc = Math.min(lp.cellVSpan - mMinVSpan, cellYInc); |
| 284 | vSpanInc *= -1; |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 285 | vSpanInc = Math.min(cellY, vSpanInc); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 286 | vSpanInc = Math.max(-(lp.cellVSpan - mMinVSpan), vSpanInc); |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 287 | vSpanDelta = -vSpanInc; |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 288 | } else if (mBottomBorderActive) { |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 289 | vSpanInc = Math.min(countY - (cellY + spanY), vSpanInc); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 290 | vSpanInc = Math.max(-(lp.cellVSpan - mMinVSpan), vSpanInc); |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 291 | vSpanDelta = vSpanInc; |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 292 | } |
| 293 | |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 294 | mDirectionVector[0] = 0; |
| 295 | mDirectionVector[1] = 0; |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 296 | // Update the widget's dimensions and position according to the deltas computed above |
| 297 | if (mLeftBorderActive || mRightBorderActive) { |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 298 | spanX += hSpanInc; |
| 299 | cellX += cellXInc; |
Adam Cohen | e048950 | 2012-08-27 15:18:53 -0700 | [diff] [blame] | 300 | if (hSpanDelta != 0) { |
| 301 | mDirectionVector[0] = mLeftBorderActive ? -1 : 1; |
| 302 | } |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | if (mTopBorderActive || mBottomBorderActive) { |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 306 | spanY += vSpanInc; |
| 307 | cellY += cellYInc; |
Adam Cohen | e048950 | 2012-08-27 15:18:53 -0700 | [diff] [blame] | 308 | if (vSpanDelta != 0) { |
| 309 | mDirectionVector[1] = mTopBorderActive ? -1 : 1; |
| 310 | } |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 311 | } |
| 312 | |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 313 | if (!onDismiss && vSpanDelta == 0 && hSpanDelta == 0) return; |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 314 | |
Adam Cohen | e048950 | 2012-08-27 15:18:53 -0700 | [diff] [blame] | 315 | // We always want the final commit to match the feedback, so we make sure to use the |
| 316 | // last used direction vector when committing the resize / reorder. |
| 317 | if (onDismiss) { |
| 318 | mDirectionVector[0] = mLastDirectionVector[0]; |
| 319 | mDirectionVector[1] = mLastDirectionVector[1]; |
| 320 | } else { |
| 321 | mLastDirectionVector[0] = mDirectionVector[0]; |
| 322 | mLastDirectionVector[1] = mDirectionVector[1]; |
| 323 | } |
| 324 | |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 325 | if (mCellLayout.createAreaForResize(cellX, cellY, spanX, spanY, mWidgetView, |
| 326 | mDirectionVector, onDismiss)) { |
| 327 | lp.tmpCellX = cellX; |
| 328 | lp.tmpCellY = cellY; |
| 329 | lp.cellHSpan = spanX; |
| 330 | lp.cellVSpan = spanY; |
| 331 | mRunningVInc += vSpanDelta; |
| 332 | mRunningHInc += hSpanDelta; |
Adam Cohen | a897f39 | 2012-04-27 18:12:05 -0700 | [diff] [blame] | 333 | if (!onDismiss) { |
| 334 | updateWidgetSizeRanges(mWidgetView, mLauncher, spanX, spanY); |
| 335 | } |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 336 | } |
Adam Cohen | 6788269 | 2011-03-11 15:29:03 -0800 | [diff] [blame] | 337 | mWidgetView.requestLayout(); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 338 | } |
| 339 | |
Adam Cohen | a897f39 | 2012-04-27 18:12:05 -0700 | [diff] [blame] | 340 | static void updateWidgetSizeRanges(AppWidgetHostView widgetView, Launcher launcher, |
| 341 | int spanX, int spanY) { |
Adam Cohen | 9e05a5e | 2012-09-10 15:53:09 -0700 | [diff] [blame] | 342 | |
| 343 | getWidgetSizeRanges(launcher, spanX, spanY, mTmpRect); |
| 344 | widgetView.updateAppWidgetSize(null, mTmpRect.left, mTmpRect.top, |
| 345 | mTmpRect.right, mTmpRect.bottom); |
| 346 | } |
| 347 | |
| 348 | static Rect getWidgetSizeRanges(Launcher launcher, int spanX, int spanY, Rect rect) { |
| 349 | if (rect == null) { |
| 350 | rect = new Rect(); |
| 351 | } |
Adam Cohen | a897f39 | 2012-04-27 18:12:05 -0700 | [diff] [blame] | 352 | Rect landMetrics = Workspace.getCellLayoutMetrics(launcher, CellLayout.LANDSCAPE); |
| 353 | Rect portMetrics = Workspace.getCellLayoutMetrics(launcher, CellLayout.PORTRAIT); |
| 354 | final float density = launcher.getResources().getDisplayMetrics().density; |
| 355 | |
| 356 | // Compute landscape size |
| 357 | int cellWidth = landMetrics.left; |
| 358 | int cellHeight = landMetrics.top; |
| 359 | int widthGap = landMetrics.right; |
| 360 | int heightGap = landMetrics.bottom; |
| 361 | int landWidth = (int) ((spanX * cellWidth + (spanX - 1) * widthGap) / density); |
| 362 | int landHeight = (int) ((spanY * cellHeight + (spanY - 1) * heightGap) / density); |
| 363 | |
| 364 | // Compute portrait size |
| 365 | cellWidth = portMetrics.left; |
| 366 | cellHeight = portMetrics.top; |
| 367 | widthGap = portMetrics.right; |
| 368 | heightGap = portMetrics.bottom; |
| 369 | int portWidth = (int) ((spanX * cellWidth + (spanX - 1) * widthGap) / density); |
| 370 | int portHeight = (int) ((spanY * cellHeight + (spanY - 1) * heightGap) / density); |
Adam Cohen | 9e05a5e | 2012-09-10 15:53:09 -0700 | [diff] [blame] | 371 | rect.set(portWidth, landHeight, landWidth, portHeight); |
| 372 | return rect; |
Adam Cohen | a897f39 | 2012-04-27 18:12:05 -0700 | [diff] [blame] | 373 | } |
| 374 | |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 375 | /** |
| 376 | * This is the final step of the resize. Here we save the new widget size and position |
| 377 | * to LauncherModel and animate the resize frame. |
| 378 | */ |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 379 | public void commitResize() { |
| 380 | resizeWidgetIfNeeded(true); |
| 381 | requestLayout(); |
| 382 | } |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 383 | |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 384 | public void onTouchUp() { |
| 385 | int xThreshold = mCellLayout.getCellWidth() + mCellLayout.getWidthGap(); |
| 386 | int yThreshold = mCellLayout.getCellHeight() + mCellLayout.getHeightGap(); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 387 | |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 388 | mDeltaXAddOn = mRunningHInc * xThreshold; |
| 389 | mDeltaYAddOn = mRunningVInc * yThreshold; |
| 390 | mDeltaX = 0; |
| 391 | mDeltaY = 0; |
| 392 | |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 393 | post(new Runnable() { |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 394 | @Override |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 395 | public void run() { |
| 396 | snapToWidget(true); |
| 397 | } |
| 398 | }); |
| 399 | } |
| 400 | |
| 401 | public void snapToWidget(boolean animate) { |
Adam Cohen | 6788269 | 2011-03-11 15:29:03 -0800 | [diff] [blame] | 402 | final DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams(); |
Adam Cohen | 37b59ff | 2011-06-13 17:13:42 -0700 | [diff] [blame] | 403 | int newWidth = mWidgetView.getWidth() + 2 * mBackgroundPadding - mWidgetPaddingLeft - |
| 404 | mWidgetPaddingRight; |
| 405 | int newHeight = mWidgetView.getHeight() + 2 * mBackgroundPadding - mWidgetPaddingTop - |
| 406 | mWidgetPaddingBottom; |
| 407 | |
Adam Cohen | d6e7aa3 | 2013-07-09 15:32:37 -0700 | [diff] [blame^] | 408 | mTmpPt[0] = mWidgetView.getLeft(); |
| 409 | mTmpPt[1] = mWidgetView.getTop(); |
| 410 | mDragLayer.getDescendantCoordRelativeToSelf(mCellLayout.getShortcutsAndWidgets(), mTmpPt); |
| 411 | |
| 412 | int newX = mTmpPt[0] - mBackgroundPadding + mWidgetPaddingLeft; |
| 413 | int newY = mTmpPt[1] - mBackgroundPadding + mWidgetPaddingTop; |
Adam Cohen | 3cba722 | 2011-03-02 19:03:11 -0800 | [diff] [blame] | 414 | |
Adam Cohen | 4459d6b | 2012-07-13 15:59:15 -0700 | [diff] [blame] | 415 | // We need to make sure the frame's touchable regions lie fully within the bounds of the |
| 416 | // DragLayer. We allow the actual handles to be clipped, but we shift the touch regions |
| 417 | // down accordingly to provide a proper touch target. |
Adam Cohen | 3cba722 | 2011-03-02 19:03:11 -0800 | [diff] [blame] | 418 | if (newY < 0) { |
Adam Cohen | 4459d6b | 2012-07-13 15:59:15 -0700 | [diff] [blame] | 419 | // In this case we shift the touch region down to start at the top of the DragLayer |
| 420 | mTopTouchRegionAdjustment = -newY; |
| 421 | } else { |
| 422 | mTopTouchRegionAdjustment = 0; |
Adam Cohen | 3cba722 | 2011-03-02 19:03:11 -0800 | [diff] [blame] | 423 | } |
Adam Cohen | 6788269 | 2011-03-11 15:29:03 -0800 | [diff] [blame] | 424 | if (newY + newHeight > mDragLayer.getHeight()) { |
Adam Cohen | 4459d6b | 2012-07-13 15:59:15 -0700 | [diff] [blame] | 425 | // In this case we shift the touch region up to end at the bottom of the DragLayer |
| 426 | mBottomTouchRegionAdjustment = -(newY + newHeight - mDragLayer.getHeight()); |
| 427 | } else { |
| 428 | mBottomTouchRegionAdjustment = 0; |
Adam Cohen | 3cba722 | 2011-03-02 19:03:11 -0800 | [diff] [blame] | 429 | } |
| 430 | |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 431 | if (!animate) { |
| 432 | lp.width = newWidth; |
| 433 | lp.height = newHeight; |
| 434 | lp.x = newX; |
| 435 | lp.y = newY; |
| 436 | mLeftHandle.setAlpha(1.0f); |
| 437 | mRightHandle.setAlpha(1.0f); |
| 438 | mTopHandle.setAlpha(1.0f); |
| 439 | mBottomHandle.setAlpha(1.0f); |
| 440 | requestLayout(); |
| 441 | } else { |
| 442 | PropertyValuesHolder width = PropertyValuesHolder.ofInt("width", lp.width, newWidth); |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 443 | PropertyValuesHolder height = PropertyValuesHolder.ofInt("height", lp.height, |
| 444 | newHeight); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 445 | PropertyValuesHolder x = PropertyValuesHolder.ofInt("x", lp.x, newX); |
| 446 | PropertyValuesHolder y = PropertyValuesHolder.ofInt("y", lp.y, newY); |
Michael Jurka | f1ad608 | 2013-03-13 12:55:46 +0100 | [diff] [blame] | 447 | ObjectAnimator oa = |
| 448 | LauncherAnimUtils.ofPropertyValuesHolder(lp, this, width, height, x, y); |
Michael Jurka | 2ecf995 | 2012-06-18 12:52:28 -0700 | [diff] [blame] | 449 | ObjectAnimator leftOa = LauncherAnimUtils.ofFloat(mLeftHandle, "alpha", 1.0f); |
| 450 | ObjectAnimator rightOa = LauncherAnimUtils.ofFloat(mRightHandle, "alpha", 1.0f); |
| 451 | ObjectAnimator topOa = LauncherAnimUtils.ofFloat(mTopHandle, "alpha", 1.0f); |
| 452 | ObjectAnimator bottomOa = LauncherAnimUtils.ofFloat(mBottomHandle, "alpha", 1.0f); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 453 | oa.addUpdateListener(new AnimatorUpdateListener() { |
| 454 | public void onAnimationUpdate(ValueAnimator animation) { |
| 455 | requestLayout(); |
| 456 | } |
| 457 | }); |
Michael Jurka | 2ecf995 | 2012-06-18 12:52:28 -0700 | [diff] [blame] | 458 | AnimatorSet set = LauncherAnimUtils.createAnimatorSet(); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 459 | if (mResizeMode == AppWidgetProviderInfo.RESIZE_VERTICAL) { |
| 460 | set.playTogether(oa, topOa, bottomOa); |
| 461 | } else if (mResizeMode == AppWidgetProviderInfo.RESIZE_HORIZONTAL) { |
| 462 | set.playTogether(oa, leftOa, rightOa); |
| 463 | } else { |
| 464 | set.playTogether(oa, leftOa, rightOa, topOa, bottomOa); |
| 465 | } |
| 466 | |
| 467 | set.setDuration(SNAP_DURATION); |
| 468 | set.start(); |
| 469 | } |
| 470 | } |
| 471 | } |