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; |
Sunny Goyal | 756cd26 | 2015-08-20 12:33:21 -0700 | [diff] [blame] | 12 | import android.graphics.Point; |
Adam Cohen | 0cf2a7c | 2011-11-08 15:07:01 -0800 | [diff] [blame] | 13 | import android.graphics.Rect; |
Sunny Goyal | 08ca40f | 2016-09-13 21:07:31 -0700 | [diff] [blame] | 14 | import android.util.AttributeSet; |
Tony Wickham | 71255bb | 2016-02-10 16:18:15 -0800 | [diff] [blame] | 15 | import android.view.KeyEvent; |
Sunny Goyal | 6ad72f0 | 2016-09-23 11:01:10 -0700 | [diff] [blame] | 16 | import android.view.MotionEvent; |
Tony Wickham | 71255bb | 2016-02-10 16:18:15 -0800 | [diff] [blame] | 17 | import android.view.View; |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 18 | import android.widget.FrameLayout; |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 19 | |
Sunny Goyal | e78e3d7 | 2015-09-24 11:23:31 -0700 | [diff] [blame] | 20 | import com.android.launcher3.accessibility.DragViewStateAnnouncer; |
Sunny Goyal | 08ca40f | 2016-09-13 21:07:31 -0700 | [diff] [blame] | 21 | import com.android.launcher3.dragndrop.DragLayer; |
Tony Wickham | 71255bb | 2016-02-10 16:18:15 -0800 | [diff] [blame] | 22 | import com.android.launcher3.util.FocusLogic; |
Sunny Goyal | 6ad72f0 | 2016-09-23 11:01:10 -0700 | [diff] [blame] | 23 | import com.android.launcher3.util.TouchController; |
Sunny Goyal | e78e3d7 | 2015-09-24 11:23:31 -0700 | [diff] [blame] | 24 | |
Sunny Goyal | 6ad72f0 | 2016-09-23 11:01:10 -0700 | [diff] [blame] | 25 | public class AppWidgetResizeFrame extends FrameLayout |
| 26 | implements View.OnKeyListener, TouchController { |
Sunny Goyal | ba776d5 | 2015-05-18 20:52:57 -0700 | [diff] [blame] | 27 | private static final int SNAP_DURATION = 150; |
| 28 | private static final float DIMMED_HANDLE_ALPHA = 0f; |
| 29 | private static final float RESIZE_THRESHOLD = 0.66f; |
| 30 | |
Sunny Goyal | 756cd26 | 2015-08-20 12:33:21 -0700 | [diff] [blame] | 31 | private static final Rect sTmpRect = new Rect(); |
| 32 | |
| 33 | // Represents the cell size on the grid in the two orientations. |
| 34 | private static Point[] sCellSize; |
Sunny Goyal | ba776d5 | 2015-05-18 20:52:57 -0700 | [diff] [blame] | 35 | |
Sunny Goyal | 08ca40f | 2016-09-13 21:07:31 -0700 | [diff] [blame] | 36 | private static final int HANDLE_COUNT = 4; |
| 37 | private static final int INDEX_LEFT = 0; |
| 38 | private static final int INDEX_TOP = 1; |
| 39 | private static final int INDEX_RIGHT = 2; |
| 40 | private static final int INDEX_BOTTOM = 3; |
| 41 | |
Sunny Goyal | ba776d5 | 2015-05-18 20:52:57 -0700 | [diff] [blame] | 42 | private final Launcher mLauncher; |
Sunny Goyal | 08ca40f | 2016-09-13 21:07:31 -0700 | [diff] [blame] | 43 | private final DragViewStateAnnouncer mStateAnnouncer; |
Sunny Goyal | ba776d5 | 2015-05-18 20:52:57 -0700 | [diff] [blame] | 44 | |
Sunny Goyal | 08ca40f | 2016-09-13 21:07:31 -0700 | [diff] [blame] | 45 | private final View[] mDragHandles = new View[HANDLE_COUNT]; |
Sunny Goyal | ba776d5 | 2015-05-18 20:52:57 -0700 | [diff] [blame] | 46 | |
Sunny Goyal | 08ca40f | 2016-09-13 21:07:31 -0700 | [diff] [blame] | 47 | private LauncherAppWidgetHostView mWidgetView; |
| 48 | private CellLayout mCellLayout; |
| 49 | private DragLayer mDragLayer; |
| 50 | |
| 51 | private Rect mWidgetPadding; |
Sunny Goyal | ba776d5 | 2015-05-18 20:52:57 -0700 | [diff] [blame] | 52 | |
| 53 | private final int mBackgroundPadding; |
| 54 | private final int mTouchTargetWidth; |
| 55 | |
| 56 | private final int[] mDirectionVector = new int[2]; |
| 57 | private final int[] mLastDirectionVector = new int[2]; |
| 58 | private final int[] mTmpPt = new int[2]; |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 59 | |
Sunny Goyal | 08ca40f | 2016-09-13 21:07:31 -0700 | [diff] [blame] | 60 | private final IntRange mTempRange1 = new IntRange(); |
| 61 | private final IntRange mTempRange2 = new IntRange(); |
| 62 | |
| 63 | private final IntRange mDeltaXRange = new IntRange(); |
| 64 | private final IntRange mBaselineX = new IntRange(); |
| 65 | |
| 66 | private final IntRange mDeltaYRange = new IntRange(); |
| 67 | private final IntRange mBaselineY = new IntRange(); |
Sunny Goyal | e78e3d7 | 2015-09-24 11:23:31 -0700 | [diff] [blame] | 68 | |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 69 | private boolean mLeftBorderActive; |
| 70 | private boolean mRightBorderActive; |
| 71 | private boolean mTopBorderActive; |
| 72 | private boolean mBottomBorderActive; |
| 73 | |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 74 | private int mResizeMode; |
Adam Cohen | 3cba722 | 2011-03-02 19:03:11 -0800 | [diff] [blame] | 75 | |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 76 | private int mRunningHInc; |
| 77 | private int mRunningVInc; |
| 78 | private int mMinHSpan; |
| 79 | private int mMinVSpan; |
| 80 | private int mDeltaX; |
| 81 | private int mDeltaY; |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 82 | private int mDeltaXAddOn; |
| 83 | private int mDeltaYAddOn; |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 84 | |
Adam Cohen | 4459d6b | 2012-07-13 15:59:15 -0700 | [diff] [blame] | 85 | private int mTopTouchRegionAdjustment = 0; |
| 86 | private int mBottomTouchRegionAdjustment = 0; |
| 87 | |
Sunny Goyal | 6ad72f0 | 2016-09-23 11:01:10 -0700 | [diff] [blame] | 88 | private int mXDown, mYDown; |
| 89 | |
Sunny Goyal | 08ca40f | 2016-09-13 21:07:31 -0700 | [diff] [blame] | 90 | public AppWidgetResizeFrame(Context context) { |
| 91 | this(context, null); |
| 92 | } |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 93 | |
Sunny Goyal | 08ca40f | 2016-09-13 21:07:31 -0700 | [diff] [blame] | 94 | public AppWidgetResizeFrame(Context context, AttributeSet attrs) { |
| 95 | this(context, attrs, 0); |
| 96 | } |
| 97 | |
| 98 | public AppWidgetResizeFrame(Context context, AttributeSet attrs, int defStyleAttr) { |
| 99 | super(context, attrs, defStyleAttr); |
| 100 | |
| 101 | mLauncher = Launcher.getLauncher(context); |
| 102 | mStateAnnouncer = DragViewStateAnnouncer.createFor(this); |
| 103 | |
| 104 | mBackgroundPadding = getResources() |
| 105 | .getDimensionPixelSize(R.dimen.resize_frame_background_padding); |
| 106 | mTouchTargetWidth = 2 * mBackgroundPadding; |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | protected void onFinishInflate() { |
| 111 | super.onFinishInflate(); |
| 112 | |
| 113 | for (int i = 0; i < HANDLE_COUNT; i ++) { |
| 114 | mDragHandles[i] = getChildAt(i); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | public void setupForWidget(LauncherAppWidgetHostView widgetView, CellLayout cellLayout, |
| 119 | DragLayer dragLayer) { |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 120 | mCellLayout = cellLayout; |
| 121 | mWidgetView = widgetView; |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 122 | LauncherAppWidgetProviderInfo info = (LauncherAppWidgetProviderInfo) |
| 123 | widgetView.getAppWidgetInfo(); |
| 124 | mResizeMode = info.resizeMode; |
Adam Cohen | 6788269 | 2011-03-11 15:29:03 -0800 | [diff] [blame] | 125 | mDragLayer = dragLayer; |
Adam Cohen | 3cba722 | 2011-03-02 19:03:11 -0800 | [diff] [blame] | 126 | |
Sunny Goyal | 233ee96 | 2015-08-03 13:05:01 -0700 | [diff] [blame] | 127 | mMinHSpan = info.minSpanX; |
| 128 | mMinVSpan = info.minSpanY; |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 129 | |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 130 | if (!info.isCustomWidget) { |
Sunny Goyal | 08ca40f | 2016-09-13 21:07:31 -0700 | [diff] [blame] | 131 | mWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(getContext(), |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 132 | widgetView.getAppWidgetInfo().provider, null); |
| 133 | } else { |
Sunny Goyal | 08ca40f | 2016-09-13 21:07:31 -0700 | [diff] [blame] | 134 | Resources r = getContext().getResources(); |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 135 | int padding = r.getDimensionPixelSize(R.dimen.default_widget_padding); |
Sunny Goyal | ba776d5 | 2015-05-18 20:52:57 -0700 | [diff] [blame] | 136 | mWidgetPadding = new Rect(padding, padding, padding, padding); |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 137 | } |
| 138 | |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 139 | if (mResizeMode == AppWidgetProviderInfo.RESIZE_HORIZONTAL) { |
Sunny Goyal | 08ca40f | 2016-09-13 21:07:31 -0700 | [diff] [blame] | 140 | mDragHandles[INDEX_TOP].setVisibility(GONE); |
| 141 | mDragHandles[INDEX_BOTTOM].setVisibility(GONE); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 142 | } else if (mResizeMode == AppWidgetProviderInfo.RESIZE_VERTICAL) { |
Sunny Goyal | 08ca40f | 2016-09-13 21:07:31 -0700 | [diff] [blame] | 143 | mDragHandles[INDEX_LEFT].setVisibility(GONE); |
| 144 | mDragHandles[INDEX_RIGHT].setVisibility(GONE); |
Adam Cohen | 3cba722 | 2011-03-02 19:03:11 -0800 | [diff] [blame] | 145 | } |
| 146 | |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 147 | // When we create the resize frame, we first mark all cells as unoccupied. The appropriate |
| 148 | // cells (same if not resized, or different) will be marked as occupied when the resize |
| 149 | // frame is dismissed. |
| 150 | mCellLayout.markCellsAsUnoccupiedForView(mWidgetView); |
Tony Wickham | 71255bb | 2016-02-10 16:18:15 -0800 | [diff] [blame] | 151 | |
| 152 | setOnKeyListener(this); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | public boolean beginResizeIfPointInRegion(int x, int y) { |
| 156 | boolean horizontalActive = (mResizeMode & AppWidgetProviderInfo.RESIZE_HORIZONTAL) != 0; |
| 157 | boolean verticalActive = (mResizeMode & AppWidgetProviderInfo.RESIZE_VERTICAL) != 0; |
Adam Cohen | 4459d6b | 2012-07-13 15:59:15 -0700 | [diff] [blame] | 158 | |
Adam Cohen | 3cba722 | 2011-03-02 19:03:11 -0800 | [diff] [blame] | 159 | mLeftBorderActive = (x < mTouchTargetWidth) && horizontalActive; |
| 160 | mRightBorderActive = (x > getWidth() - mTouchTargetWidth) && horizontalActive; |
Adam Cohen | 4459d6b | 2012-07-13 15:59:15 -0700 | [diff] [blame] | 161 | mTopBorderActive = (y < mTouchTargetWidth + mTopTouchRegionAdjustment) && verticalActive; |
| 162 | mBottomBorderActive = (y > getHeight() - mTouchTargetWidth + mBottomTouchRegionAdjustment) |
| 163 | && verticalActive; |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 164 | |
| 165 | boolean anyBordersActive = mLeftBorderActive || mRightBorderActive |
| 166 | || mTopBorderActive || mBottomBorderActive; |
| 167 | |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 168 | if (anyBordersActive) { |
Sunny Goyal | 08ca40f | 2016-09-13 21:07:31 -0700 | [diff] [blame] | 169 | mDragHandles[INDEX_LEFT].setAlpha(mLeftBorderActive ? 1.0f : DIMMED_HANDLE_ALPHA); |
| 170 | mDragHandles[INDEX_RIGHT].setAlpha(mRightBorderActive ? 1.0f :DIMMED_HANDLE_ALPHA); |
| 171 | mDragHandles[INDEX_TOP].setAlpha(mTopBorderActive ? 1.0f : DIMMED_HANDLE_ALPHA); |
| 172 | mDragHandles[INDEX_BOTTOM].setAlpha(mBottomBorderActive ? 1.0f : DIMMED_HANDLE_ALPHA); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 173 | } |
Sunny Goyal | 08ca40f | 2016-09-13 21:07:31 -0700 | [diff] [blame] | 174 | |
| 175 | if (mLeftBorderActive) { |
| 176 | mDeltaXRange.set(-getLeft(), getWidth() - 2 * mTouchTargetWidth); |
| 177 | } else if (mRightBorderActive) { |
| 178 | mDeltaXRange.set(2 * mTouchTargetWidth - getWidth(), mDragLayer.getWidth() - getRight()); |
| 179 | } else { |
| 180 | mDeltaXRange.set(0, 0); |
| 181 | } |
| 182 | mBaselineX.set(getLeft(), getRight()); |
| 183 | |
| 184 | if (mTopBorderActive) { |
| 185 | mDeltaYRange.set(-getTop(), getHeight() - 2 * mTouchTargetWidth); |
| 186 | } else if (mBottomBorderActive) { |
| 187 | mDeltaYRange.set(2 * mTouchTargetWidth - getHeight(), mDragLayer.getHeight() - getBottom()); |
| 188 | } else { |
| 189 | mDeltaYRange.set(0, 0); |
| 190 | } |
| 191 | mBaselineY.set(getTop(), getBottom()); |
| 192 | |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 193 | return anyBordersActive; |
| 194 | } |
| 195 | |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 196 | /** |
Sunny Goyal | 08ca40f | 2016-09-13 21:07:31 -0700 | [diff] [blame] | 197 | * Based on the deltas, we resize the frame. |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 198 | */ |
Sunny Goyal | 08ca40f | 2016-09-13 21:07:31 -0700 | [diff] [blame] | 199 | public void visualizeResizeForDelta(int deltaX, int deltaY) { |
| 200 | mDeltaX = mDeltaXRange.clamp(deltaX); |
| 201 | mDeltaY = mDeltaYRange.clamp(deltaY); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 202 | |
Adam Cohen | 6788269 | 2011-03-11 15:29:03 -0800 | [diff] [blame] | 203 | DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams(); |
Sunny Goyal | 08ca40f | 2016-09-13 21:07:31 -0700 | [diff] [blame] | 204 | mDeltaX = mDeltaXRange.clamp(deltaX); |
| 205 | mBaselineX.applyDelta(mLeftBorderActive, mRightBorderActive, mDeltaX, mTempRange1); |
| 206 | lp.x = mTempRange1.start; |
| 207 | lp.width = mTempRange1.size(); |
Adam Cohen | 6788269 | 2011-03-11 15:29:03 -0800 | [diff] [blame] | 208 | |
Sunny Goyal | 08ca40f | 2016-09-13 21:07:31 -0700 | [diff] [blame] | 209 | mDeltaY = mDeltaYRange.clamp(deltaY); |
| 210 | mBaselineY.applyDelta(mTopBorderActive, mBottomBorderActive, mDeltaY, mTempRange1); |
| 211 | lp.y = mTempRange1.start; |
| 212 | lp.height = mTempRange1.size(); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 213 | |
Sunny Goyal | 08ca40f | 2016-09-13 21:07:31 -0700 | [diff] [blame] | 214 | resizeWidgetIfNeeded(false); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 215 | requestLayout(); |
| 216 | } |
| 217 | |
Sunny Goyal | 08ca40f | 2016-09-13 21:07:31 -0700 | [diff] [blame] | 218 | private static int getSpanIncrement(float deltaFrac) { |
| 219 | return Math.abs(deltaFrac) > RESIZE_THRESHOLD ? Math.round(deltaFrac) : 0; |
| 220 | } |
| 221 | |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 222 | /** |
| 223 | * Based on the current deltas, we determine if and how to resize the widget. |
| 224 | */ |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 225 | private void resizeWidgetIfNeeded(boolean onDismiss) { |
Sunny Goyal | aa8a871 | 2016-11-20 15:26:01 +0530 | [diff] [blame] | 226 | float xThreshold = mCellLayout.getCellWidth(); |
| 227 | float yThreshold = mCellLayout.getCellHeight(); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 228 | |
Sunny Goyal | 08ca40f | 2016-09-13 21:07:31 -0700 | [diff] [blame] | 229 | int hSpanInc = getSpanIncrement((mDeltaX + mDeltaXAddOn) / xThreshold - mRunningHInc); |
| 230 | int vSpanInc = getSpanIncrement((mDeltaY + mDeltaYAddOn) / yThreshold - mRunningVInc); |
Adam Cohen | e4b7729 | 2011-03-08 18:35:52 -0800 | [diff] [blame] | 231 | |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 232 | if (!onDismiss && (hSpanInc == 0 && vSpanInc == 0)) return; |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 233 | |
Sunny Goyal | 08ca40f | 2016-09-13 21:07:31 -0700 | [diff] [blame] | 234 | mDirectionVector[0] = 0; |
| 235 | mDirectionVector[1] = 0; |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 236 | |
| 237 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) mWidgetView.getLayoutParams(); |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 238 | |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 239 | int spanX = lp.cellHSpan; |
| 240 | int spanY = lp.cellVSpan; |
| 241 | int cellX = lp.useTmpCoords ? lp.tmpCellX : lp.cellX; |
| 242 | int cellY = lp.useTmpCoords ? lp.tmpCellY : lp.cellY; |
| 243 | |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 244 | // For each border, we bound the resizing based on the minimum width, and the maximum |
| 245 | // expandability. |
Sunny Goyal | 08ca40f | 2016-09-13 21:07:31 -0700 | [diff] [blame] | 246 | mTempRange1.set(cellX, spanX + cellX); |
| 247 | int hSpanDelta = mTempRange1.applyDeltaAndBound(mLeftBorderActive, mRightBorderActive, |
| 248 | hSpanInc, mMinHSpan, mCellLayout.getCountX(), mTempRange2); |
| 249 | cellX = mTempRange2.start; |
| 250 | spanX = mTempRange2.size(); |
| 251 | if (hSpanDelta != 0) { |
| 252 | mDirectionVector[0] = mLeftBorderActive ? -1 : 1; |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 253 | } |
| 254 | |
Sunny Goyal | 08ca40f | 2016-09-13 21:07:31 -0700 | [diff] [blame] | 255 | mTempRange1.set(cellY, spanY + cellY); |
| 256 | int vSpanDelta = mTempRange1.applyDeltaAndBound(mTopBorderActive, mBottomBorderActive, |
| 257 | vSpanInc, mMinVSpan, mCellLayout.getCountY(), mTempRange2); |
| 258 | cellY = mTempRange2.start; |
| 259 | spanY = mTempRange2.size(); |
| 260 | if (vSpanDelta != 0) { |
| 261 | mDirectionVector[1] = mTopBorderActive ? -1 : 1; |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 262 | } |
| 263 | |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 264 | if (!onDismiss && vSpanDelta == 0 && hSpanDelta == 0) return; |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 265 | |
Adam Cohen | e048950 | 2012-08-27 15:18:53 -0700 | [diff] [blame] | 266 | // We always want the final commit to match the feedback, so we make sure to use the |
| 267 | // last used direction vector when committing the resize / reorder. |
| 268 | if (onDismiss) { |
| 269 | mDirectionVector[0] = mLastDirectionVector[0]; |
| 270 | mDirectionVector[1] = mLastDirectionVector[1]; |
| 271 | } else { |
| 272 | mLastDirectionVector[0] = mDirectionVector[0]; |
| 273 | mLastDirectionVector[1] = mDirectionVector[1]; |
| 274 | } |
| 275 | |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 276 | if (mCellLayout.createAreaForResize(cellX, cellY, spanX, spanY, mWidgetView, |
| 277 | mDirectionVector, onDismiss)) { |
Sunny Goyal | e78e3d7 | 2015-09-24 11:23:31 -0700 | [diff] [blame] | 278 | if (mStateAnnouncer != null && (lp.cellHSpan != spanX || lp.cellVSpan != spanY) ) { |
| 279 | mStateAnnouncer.announce( |
| 280 | mLauncher.getString(R.string.widget_resized, spanX, spanY)); |
| 281 | } |
| 282 | |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 283 | lp.tmpCellX = cellX; |
| 284 | lp.tmpCellY = cellY; |
| 285 | lp.cellHSpan = spanX; |
| 286 | lp.cellVSpan = spanY; |
| 287 | mRunningVInc += vSpanDelta; |
| 288 | mRunningHInc += hSpanDelta; |
Sunny Goyal | e78e3d7 | 2015-09-24 11:23:31 -0700 | [diff] [blame] | 289 | |
Adam Cohen | a897f39 | 2012-04-27 18:12:05 -0700 | [diff] [blame] | 290 | if (!onDismiss) { |
| 291 | updateWidgetSizeRanges(mWidgetView, mLauncher, spanX, spanY); |
| 292 | } |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 293 | } |
Adam Cohen | 6788269 | 2011-03-11 15:29:03 -0800 | [diff] [blame] | 294 | mWidgetView.requestLayout(); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 295 | } |
| 296 | |
Adam Cohen | a897f39 | 2012-04-27 18:12:05 -0700 | [diff] [blame] | 297 | static void updateWidgetSizeRanges(AppWidgetHostView widgetView, Launcher launcher, |
| 298 | int spanX, int spanY) { |
Sunny Goyal | ba776d5 | 2015-05-18 20:52:57 -0700 | [diff] [blame] | 299 | getWidgetSizeRanges(launcher, spanX, spanY, sTmpRect); |
| 300 | widgetView.updateAppWidgetSize(null, sTmpRect.left, sTmpRect.top, |
| 301 | sTmpRect.right, sTmpRect.bottom); |
Adam Cohen | 9e05a5e | 2012-09-10 15:53:09 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Sunny Goyal | 16466f1 | 2016-03-10 05:34:30 -0800 | [diff] [blame] | 304 | public static Rect getWidgetSizeRanges(Context context, int spanX, int spanY, Rect rect) { |
Sunny Goyal | 756cd26 | 2015-08-20 12:33:21 -0700 | [diff] [blame] | 305 | if (sCellSize == null) { |
| 306 | InvariantDeviceProfile inv = LauncherAppState.getInstance().getInvariantDeviceProfile(); |
| 307 | |
| 308 | // Initiate cell sizes. |
| 309 | sCellSize = new Point[2]; |
| 310 | sCellSize[0] = inv.landscapeProfile.getCellSize(); |
| 311 | sCellSize[1] = inv.portraitProfile.getCellSize(); |
| 312 | } |
| 313 | |
Adam Cohen | 9e05a5e | 2012-09-10 15:53:09 -0700 | [diff] [blame] | 314 | if (rect == null) { |
| 315 | rect = new Rect(); |
| 316 | } |
Sunny Goyal | 16466f1 | 2016-03-10 05:34:30 -0800 | [diff] [blame] | 317 | final float density = context.getResources().getDisplayMetrics().density; |
Adam Cohen | a897f39 | 2012-04-27 18:12:05 -0700 | [diff] [blame] | 318 | |
| 319 | // Compute landscape size |
Sunny Goyal | 756cd26 | 2015-08-20 12:33:21 -0700 | [diff] [blame] | 320 | int landWidth = (int) ((spanX * sCellSize[0].x) / density); |
| 321 | int landHeight = (int) ((spanY * sCellSize[0].y) / density); |
Adam Cohen | a897f39 | 2012-04-27 18:12:05 -0700 | [diff] [blame] | 322 | |
| 323 | // Compute portrait size |
Sunny Goyal | 756cd26 | 2015-08-20 12:33:21 -0700 | [diff] [blame] | 324 | int portWidth = (int) ((spanX * sCellSize[1].x) / density); |
| 325 | int portHeight = (int) ((spanY * sCellSize[1].y) / density); |
Adam Cohen | 9e05a5e | 2012-09-10 15:53:09 -0700 | [diff] [blame] | 326 | rect.set(portWidth, landHeight, landWidth, portHeight); |
| 327 | return rect; |
Adam Cohen | a897f39 | 2012-04-27 18:12:05 -0700 | [diff] [blame] | 328 | } |
| 329 | |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 330 | /** |
| 331 | * This is the final step of the resize. Here we save the new widget size and position |
| 332 | * to LauncherModel and animate the resize frame. |
| 333 | */ |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 334 | public void commitResize() { |
| 335 | resizeWidgetIfNeeded(true); |
| 336 | requestLayout(); |
| 337 | } |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 338 | |
Sunny Goyal | 6ad72f0 | 2016-09-23 11:01:10 -0700 | [diff] [blame] | 339 | private void onTouchUp() { |
Sunny Goyal | aa8a871 | 2016-11-20 15:26:01 +0530 | [diff] [blame] | 340 | int xThreshold = mCellLayout.getCellWidth(); |
| 341 | int yThreshold = mCellLayout.getCellHeight(); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 342 | |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 343 | mDeltaXAddOn = mRunningHInc * xThreshold; |
| 344 | mDeltaYAddOn = mRunningVInc * yThreshold; |
| 345 | mDeltaX = 0; |
| 346 | mDeltaY = 0; |
| 347 | |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 348 | post(new Runnable() { |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 349 | @Override |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 350 | public void run() { |
| 351 | snapToWidget(true); |
| 352 | } |
| 353 | }); |
| 354 | } |
| 355 | |
| 356 | public void snapToWidget(boolean animate) { |
Jon Miranda | 7ae64ff | 2016-11-21 16:18:46 -0800 | [diff] [blame] | 357 | DeviceProfile profile = mLauncher.getDeviceProfile(); |
Jon Miranda | 26e4d17 | 2016-12-12 15:43:18 -0800 | [diff] [blame^] | 358 | float scale = Math.min(profile.appWidgetScale.x, profile.appWidgetScale.y); |
Adam Cohen | 37b59ff | 2011-06-13 17:13:42 -0700 | [diff] [blame] | 359 | |
Jon Miranda | 26e4d17 | 2016-12-12 15:43:18 -0800 | [diff] [blame^] | 360 | mDragLayer.getViewRectRelativeToSelf(mWidgetView, sTmpRect); |
Adam Cohen | d6e7aa3 | 2013-07-09 15:32:37 -0700 | [diff] [blame] | 361 | |
Jon Miranda | 26e4d17 | 2016-12-12 15:43:18 -0800 | [diff] [blame^] | 362 | int newWidth = 2 * mBackgroundPadding |
| 363 | + (int) (scale * (sTmpRect.width() - mWidgetPadding.left - mWidgetPadding.right)); |
| 364 | int newHeight = 2 * mBackgroundPadding |
| 365 | + (int) (scale * (sTmpRect.height() - mWidgetPadding.top - mWidgetPadding.bottom)); |
| 366 | |
| 367 | int newX = (int) (sTmpRect.left - mBackgroundPadding + scale * mWidgetPadding.left); |
| 368 | int newY = (int) (sTmpRect.top - mBackgroundPadding + scale * mWidgetPadding.top); |
Adam Cohen | 3cba722 | 2011-03-02 19:03:11 -0800 | [diff] [blame] | 369 | |
Sunny Goyal | ba776d5 | 2015-05-18 20:52:57 -0700 | [diff] [blame] | 370 | // We need to make sure the frame's touchable regions lie fully within the bounds of the |
Adam Cohen | 4459d6b | 2012-07-13 15:59:15 -0700 | [diff] [blame] | 371 | // DragLayer. We allow the actual handles to be clipped, but we shift the touch regions |
| 372 | // down accordingly to provide a proper touch target. |
Adam Cohen | 3cba722 | 2011-03-02 19:03:11 -0800 | [diff] [blame] | 373 | if (newY < 0) { |
Adam Cohen | 4459d6b | 2012-07-13 15:59:15 -0700 | [diff] [blame] | 374 | // In this case we shift the touch region down to start at the top of the DragLayer |
| 375 | mTopTouchRegionAdjustment = -newY; |
| 376 | } else { |
| 377 | mTopTouchRegionAdjustment = 0; |
Adam Cohen | 3cba722 | 2011-03-02 19:03:11 -0800 | [diff] [blame] | 378 | } |
Adam Cohen | 6788269 | 2011-03-11 15:29:03 -0800 | [diff] [blame] | 379 | if (newY + newHeight > mDragLayer.getHeight()) { |
Adam Cohen | 4459d6b | 2012-07-13 15:59:15 -0700 | [diff] [blame] | 380 | // In this case we shift the touch region up to end at the bottom of the DragLayer |
| 381 | mBottomTouchRegionAdjustment = -(newY + newHeight - mDragLayer.getHeight()); |
| 382 | } else { |
| 383 | mBottomTouchRegionAdjustment = 0; |
Adam Cohen | 3cba722 | 2011-03-02 19:03:11 -0800 | [diff] [blame] | 384 | } |
| 385 | |
Jon Miranda | 26e4d17 | 2016-12-12 15:43:18 -0800 | [diff] [blame^] | 386 | final DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams(); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 387 | if (!animate) { |
| 388 | lp.width = newWidth; |
| 389 | lp.height = newHeight; |
| 390 | lp.x = newX; |
| 391 | lp.y = newY; |
Sunny Goyal | 08ca40f | 2016-09-13 21:07:31 -0700 | [diff] [blame] | 392 | for (int i = 0; i < HANDLE_COUNT; i++) { |
| 393 | mDragHandles[i].setAlpha(1.0f); |
| 394 | } |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 395 | requestLayout(); |
| 396 | } else { |
| 397 | PropertyValuesHolder width = PropertyValuesHolder.ofInt("width", lp.width, newWidth); |
Adam Cohen | 1b607ed | 2011-03-03 17:26:50 -0800 | [diff] [blame] | 398 | PropertyValuesHolder height = PropertyValuesHolder.ofInt("height", lp.height, |
| 399 | newHeight); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 400 | PropertyValuesHolder x = PropertyValuesHolder.ofInt("x", lp.x, newX); |
| 401 | PropertyValuesHolder y = PropertyValuesHolder.ofInt("y", lp.y, newY); |
Michael Jurka | f1ad608 | 2013-03-13 12:55:46 +0100 | [diff] [blame] | 402 | ObjectAnimator oa = |
| 403 | LauncherAnimUtils.ofPropertyValuesHolder(lp, this, width, height, x, y); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 404 | oa.addUpdateListener(new AnimatorUpdateListener() { |
| 405 | public void onAnimationUpdate(ValueAnimator animation) { |
| 406 | requestLayout(); |
| 407 | } |
| 408 | }); |
Michael Jurka | 2ecf995 | 2012-06-18 12:52:28 -0700 | [diff] [blame] | 409 | AnimatorSet set = LauncherAnimUtils.createAnimatorSet(); |
Sunny Goyal | 08ca40f | 2016-09-13 21:07:31 -0700 | [diff] [blame] | 410 | set.play(oa); |
| 411 | for (int i = 0; i < HANDLE_COUNT; i++) { |
| 412 | set.play(LauncherAnimUtils.ofFloat(mDragHandles[i], ALPHA, 1.0f)); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | set.setDuration(SNAP_DURATION); |
| 416 | set.start(); |
| 417 | } |
Tony Wickham | 71255bb | 2016-02-10 16:18:15 -0800 | [diff] [blame] | 418 | |
| 419 | setFocusableInTouchMode(true); |
| 420 | requestFocus(); |
| 421 | } |
| 422 | |
| 423 | @Override |
| 424 | public boolean onKey(View v, int keyCode, KeyEvent event) { |
| 425 | // Clear the frame and give focus to the widget host view when a directional key is pressed. |
| 426 | if (FocusLogic.shouldConsume(keyCode)) { |
Sunny Goyal | 6ad72f0 | 2016-09-23 11:01:10 -0700 | [diff] [blame] | 427 | mDragLayer.clearResizeFrame(); |
Tony Wickham | 71255bb | 2016-02-10 16:18:15 -0800 | [diff] [blame] | 428 | mWidgetView.requestFocus(); |
| 429 | return true; |
| 430 | } |
| 431 | return false; |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 432 | } |
Sunny Goyal | 6ad72f0 | 2016-09-23 11:01:10 -0700 | [diff] [blame] | 433 | |
| 434 | private boolean handleTouchDown(MotionEvent ev) { |
| 435 | Rect hitRect = new Rect(); |
| 436 | int x = (int) ev.getX(); |
| 437 | int y = (int) ev.getY(); |
| 438 | |
| 439 | getHitRect(hitRect); |
| 440 | if (hitRect.contains(x, y)) { |
| 441 | if (beginResizeIfPointInRegion(x - getLeft(), y - getTop())) { |
| 442 | mXDown = x; |
| 443 | mYDown = y; |
| 444 | return true; |
| 445 | } |
| 446 | } |
| 447 | return false; |
| 448 | } |
| 449 | |
| 450 | @Override |
| 451 | public boolean onControllerTouchEvent(MotionEvent ev) { |
| 452 | int action = ev.getAction(); |
| 453 | int x = (int) ev.getX(); |
| 454 | int y = (int) ev.getY(); |
| 455 | |
| 456 | switch (action) { |
| 457 | case MotionEvent.ACTION_DOWN: |
| 458 | return handleTouchDown(ev); |
| 459 | case MotionEvent.ACTION_MOVE: |
| 460 | visualizeResizeForDelta(x - mXDown, y - mYDown); |
| 461 | break; |
| 462 | case MotionEvent.ACTION_CANCEL: |
| 463 | case MotionEvent.ACTION_UP: |
| 464 | visualizeResizeForDelta(x - mXDown, y - mYDown); |
| 465 | onTouchUp(); |
| 466 | mXDown = mYDown = 0; |
| 467 | break; |
| 468 | } |
| 469 | return true; |
| 470 | } |
| 471 | |
| 472 | @Override |
| 473 | public boolean onControllerInterceptTouchEvent(MotionEvent ev) { |
| 474 | if (ev.getAction() == MotionEvent.ACTION_DOWN && handleTouchDown(ev)) { |
| 475 | return true; |
| 476 | } |
| 477 | return false; |
| 478 | } |
Sunny Goyal | 08ca40f | 2016-09-13 21:07:31 -0700 | [diff] [blame] | 479 | |
| 480 | /** |
| 481 | * A mutable class for describing the range of two int values. |
| 482 | */ |
| 483 | private static class IntRange { |
| 484 | |
| 485 | public int start, end; |
| 486 | |
| 487 | public int clamp(int value) { |
| 488 | return Utilities.boundToRange(value, start, end); |
| 489 | } |
| 490 | |
| 491 | public void set(int s, int e) { |
| 492 | start = s; |
| 493 | end = e; |
| 494 | } |
| 495 | |
| 496 | public int size() { |
| 497 | return end - start; |
| 498 | } |
| 499 | |
| 500 | /** |
| 501 | * Moves either the start or end edge (but never both) by {@param delta} and sets the |
| 502 | * result in {@param out} |
| 503 | */ |
| 504 | public void applyDelta(boolean moveStart, boolean moveEnd, int delta, IntRange out) { |
| 505 | out.start = moveStart ? start + delta : start; |
| 506 | out.end = moveEnd ? end + delta : end; |
| 507 | } |
| 508 | |
| 509 | /** |
| 510 | * Applies delta similar to {@link #applyDelta(boolean, boolean, int, IntRange)}, |
| 511 | * with extra conditions. |
| 512 | * @param minSize minimum size after with the moving edge should not be shifted any further. |
| 513 | * For eg, if delta = -3 when moving the endEdge brings the size to less than |
| 514 | * minSize, only delta = -2 will applied |
| 515 | * @param maxEnd The maximum value to the end edge (start edge is always restricted to 0) |
| 516 | * @return the amount of increase when endEdge was moves and the amount of decrease when |
| 517 | * the start edge was moved. |
| 518 | */ |
| 519 | public int applyDeltaAndBound(boolean moveStart, boolean moveEnd, int delta, |
| 520 | int minSize, int maxEnd, IntRange out) { |
| 521 | applyDelta(moveStart, moveEnd, delta, out); |
| 522 | if (start < 0) { |
| 523 | out.start = 0; |
| 524 | } |
| 525 | if (end > maxEnd) { |
| 526 | out.end = maxEnd; |
| 527 | } |
| 528 | if (out.size() < minSize) { |
| 529 | if (moveStart) { |
| 530 | out.start = out.end - minSize; |
| 531 | } else if (moveEnd) { |
| 532 | out.end = out.start + minSize; |
| 533 | } |
| 534 | } |
| 535 | return moveEnd ? out.size() - size() : size() - out.size(); |
| 536 | } |
| 537 | } |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 538 | } |