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