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