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