blob: 85b08d11646b685d1b8cd2832fbe194c82515a2d [file] [log] [blame]
Winson Chung61fa4192011-06-12 15:15:29 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung61fa4192011-06-12 15:15:29 -070018
Sunny Goyal3a644ed2015-05-21 10:28:02 -070019import android.animation.AnimatorSet;
20import android.animation.FloatArrayEvaluator;
Sunny Goyalfe0d1f22015-04-28 22:01:31 -070021import android.animation.ObjectAnimator;
Sunny Goyal3a644ed2015-05-21 10:28:02 -070022import android.animation.ValueAnimator;
23import android.animation.ValueAnimator.AnimatorUpdateListener;
Winson Chung61fa4192011-06-12 15:15:29 -070024import android.content.Context;
Sunny Goyalfa401a12015-04-10 13:45:42 -070025import android.content.res.ColorStateList;
Tony Wickham855b1b52016-03-28 16:56:30 -070026import android.content.res.Resources;
Sunny Goyalf37a2142016-03-24 17:28:25 -070027import android.content.res.TypedArray;
Sunny Goyal3a644ed2015-05-21 10:28:02 -070028import android.graphics.ColorMatrix;
29import android.graphics.ColorMatrixColorFilter;
Winson Chung61967cb2012-02-28 18:11:33 -080030import android.graphics.Rect;
Sunny Goyal3a644ed2015-05-21 10:28:02 -070031import android.graphics.drawable.Drawable;
Winson Chung61fa4192011-06-12 15:15:29 -070032import android.util.AttributeSet;
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -080033import android.view.View;
Sunny Goyal1a70cef2015-04-22 11:29:51 -070034import android.view.View.OnClickListener;
Sunny Goyalfa401a12015-04-10 13:45:42 -070035import android.view.ViewGroup;
Sunny Goyale78e3d72015-09-24 11:23:31 -070036import android.view.accessibility.AccessibilityEvent;
Sunny Goyalfa401a12015-04-10 13:45:42 -070037import android.view.animation.DecelerateInterpolator;
38import android.view.animation.LinearInterpolator;
Adam Cohend4d7aa52011-07-19 21:47:37 -070039import android.widget.TextView;
Winson Chung61fa4192011-06-12 15:15:29 -070040
Vadim Tryshevfedca432015-08-19 17:55:02 -070041import com.android.launcher3.dragndrop.DragController;
42import com.android.launcher3.dragndrop.DragLayer;
Sunny Goyal94b510c2016-08-16 15:36:48 -070043import com.android.launcher3.dragndrop.DragOptions;
Vadim Tryshevfedca432015-08-19 17:55:02 -070044import com.android.launcher3.dragndrop.DragView;
Sunny Goyalfa401a12015-04-10 13:45:42 -070045import com.android.launcher3.util.Thunk;
Winson Chung61fa4192011-06-12 15:15:29 -070046
47/**
48 * Implements a DropTarget.
49 */
Sunny Goyal1a70cef2015-04-22 11:29:51 -070050public abstract class ButtonDropTarget extends TextView
51 implements DropTarget, DragController.DragListener, OnClickListener {
Sunny Goyalfa401a12015-04-10 13:45:42 -070052
Tony Wickhamb54c4a32015-09-11 08:40:20 -070053 private static final int DRAG_VIEW_DROP_DURATION = 285;
Winson Chung61fa4192011-06-12 15:15:29 -070054
Sunny Goyalf37a2142016-03-24 17:28:25 -070055 private final boolean mHideParentOnDisable;
Sunny Goyal47328fd2016-05-25 18:56:41 -070056 protected final Launcher mLauncher;
Sunny Goyalf37a2142016-03-24 17:28:25 -070057
Winson Chunga62e9fd2011-07-11 15:20:48 -070058 private int mBottomDragPadding;
Sunny Goyal47328fd2016-05-25 18:56:41 -070059 protected DropTargetBar mDropTargetBar;
Winson Chung61fa4192011-06-12 15:15:29 -070060
61 /** Whether this drop target is active for the current drag */
62 protected boolean mActive;
Sunny Goyal4583d092016-08-17 11:11:48 -070063 /** Whether an accessible drag is in progress */
64 private boolean mAccessibleDrag;
Tony Wickham855b1b52016-03-28 16:56:30 -070065 /** An item must be dragged at least this many pixels before this drop target is enabled. */
66 private final int mDragDistanceThreshold;
Winson Chung61fa4192011-06-12 15:15:29 -070067
68 /** The paint applied to the drag view on hover */
Winson Chung61967cb2012-02-28 18:11:33 -080069 protected int mHoverColor = 0;
Winson Chung61fa4192011-06-12 15:15:29 -070070
Sunny Goyalfa401a12015-04-10 13:45:42 -070071 protected ColorStateList mOriginalTextColor;
Sunny Goyal3a644ed2015-05-21 10:28:02 -070072 protected Drawable mDrawable;
Sunny Goyalfa401a12015-04-10 13:45:42 -070073
Sunny Goyal3a644ed2015-05-21 10:28:02 -070074 private AnimatorSet mCurrentColorAnim;
Sunny Goyal316490e2015-06-02 09:38:28 -070075 @Thunk ColorMatrix mSrcFilter, mDstFilter, mCurrentFilter;
Sunny Goyal3a644ed2015-05-21 10:28:02 -070076
Winson Chung61fa4192011-06-12 15:15:29 -070077 public ButtonDropTarget(Context context, AttributeSet attrs) {
78 this(context, attrs, 0);
79 }
80
81 public ButtonDropTarget(Context context, AttributeSet attrs, int defStyle) {
82 super(context, attrs, defStyle);
Tony2fd02082016-10-07 12:50:01 -070083 mLauncher = Launcher.getLauncher(context);
Sunny Goyal47328fd2016-05-25 18:56:41 -070084
Tony Wickham855b1b52016-03-28 16:56:30 -070085 Resources resources = getResources();
86 mBottomDragPadding = resources.getDimensionPixelSize(R.dimen.drop_target_drag_padding);
Sunny Goyalf37a2142016-03-24 17:28:25 -070087
88 TypedArray a = context.obtainStyledAttributes(attrs,
89 R.styleable.ButtonDropTarget, defStyle, 0);
90 mHideParentOnDisable = a.getBoolean(R.styleable.ButtonDropTarget_hideParentOnDisable, false);
91 a.recycle();
Tony Wickham855b1b52016-03-28 16:56:30 -070092 mDragDistanceThreshold = resources.getDimensionPixelSize(R.dimen.drag_distanceThreshold);
Winson Chung61fa4192011-06-12 15:15:29 -070093 }
94
Sunny Goyalfa401a12015-04-10 13:45:42 -070095 @Override
96 protected void onFinishInflate() {
97 super.onFinishInflate();
98 mOriginalTextColor = getTextColors();
Winson Chung61fa4192011-06-12 15:15:29 -070099 }
100
Sunny Goyalfa401a12015-04-10 13:45:42 -0700101 protected void setDrawable(int resId) {
Sunny Goyale0cab302015-05-20 16:40:30 -0700102 // We do not set the drawable in the xml as that inflates two drawables corresponding to
103 // drawableLeft and drawableStart.
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700104 mDrawable = getResources().getDrawable(resId);
Sunny Goyala52ecb02016-12-16 15:04:51 -0800105 setCompoundDrawablesRelativeWithIntrinsicBounds(mDrawable, null, null, null);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700106 }
107
Sunny Goyal47328fd2016-05-25 18:56:41 -0700108 public void setDropTargetBar(DropTargetBar dropTargetBar) {
Tony Wickhamb54c4a32015-09-11 08:40:20 -0700109 mDropTargetBar = dropTargetBar;
Adam Cohend4d7aa52011-07-19 21:47:37 -0700110 }
111
Sunny Goyalfa401a12015-04-10 13:45:42 -0700112 @Override
Sunny Goyalfa401a12015-04-10 13:45:42 -0700113 public final void onDragEnter(DragObject d) {
Winson Chung61967cb2012-02-28 18:11:33 -0800114 d.dragView.setColor(mHoverColor);
Sunny Goyala52ecb02016-12-16 15:04:51 -0800115 animateTextColor(mHoverColor);
Sunny Goyald21301e2015-09-25 12:17:08 -0700116 if (d.stateAnnouncer != null) {
117 d.stateAnnouncer.cancel();
118 }
Sunny Goyale78e3d72015-09-24 11:23:31 -0700119 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Winson Chung61fa4192011-06-12 15:15:29 -0700120 }
121
Sunny Goyalfa401a12015-04-10 13:45:42 -0700122 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700123 public void onDragOver(DragObject d) {
124 // Do nothing
125 }
126
Sunny Goyal9fc953b2015-08-17 12:24:25 -0700127 protected void resetHoverColor() {
Sunny Goyala52ecb02016-12-16 15:04:51 -0800128 animateTextColor(mOriginalTextColor.getDefaultColor());
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700129 }
130
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700131 private void animateTextColor(int targetColor) {
132 if (mCurrentColorAnim != null) {
133 mCurrentColorAnim.cancel();
134 }
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700135
136 mCurrentColorAnim = new AnimatorSet();
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700137 mCurrentColorAnim.setDuration(DragView.COLOR_CHANGE_DURATION);
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700138
139 if (mSrcFilter == null) {
140 mSrcFilter = new ColorMatrix();
141 mDstFilter = new ColorMatrix();
142 mCurrentFilter = new ColorMatrix();
143 }
144
145 DragView.setColorScale(getTextColor(), mSrcFilter);
146 DragView.setColorScale(targetColor, mDstFilter);
147 ValueAnimator anim1 = ValueAnimator.ofObject(
148 new FloatArrayEvaluator(mCurrentFilter.getArray()),
149 mSrcFilter.getArray(), mDstFilter.getArray());
150 anim1.addUpdateListener(new AnimatorUpdateListener() {
151
152 @Override
153 public void onAnimationUpdate(ValueAnimator animation) {
154 mDrawable.setColorFilter(new ColorMatrixColorFilter(mCurrentFilter));
155 invalidate();
156 }
157 });
158
159 mCurrentColorAnim.play(anim1);
160 mCurrentColorAnim.play(ObjectAnimator.ofArgb(this, "textColor", targetColor));
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700161 mCurrentColorAnim.start();
Winson Chung61fa4192011-06-12 15:15:29 -0700162 }
163
Sunny Goyalfa401a12015-04-10 13:45:42 -0700164 @Override
165 public final void onDragExit(DragObject d) {
166 if (!d.dragComplete) {
167 d.dragView.setColor(0);
168 resetHoverColor();
169 } else {
170 // Restore the hover color
171 d.dragView.setColor(mHoverColor);
172 }
Winson Chung61fa4192011-06-12 15:15:29 -0700173 }
174
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700175 @Override
Sunny Goyal94b510c2016-08-16 15:36:48 -0700176 public void onDragStart(DropTarget.DragObject dragObject, DragOptions options) {
177 mActive = supportsDrop(dragObject.dragSource, dragObject.dragInfo);
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700178 mDrawable.setColorFilter(null);
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700179 if (mCurrentColorAnim != null) {
180 mCurrentColorAnim.cancel();
181 mCurrentColorAnim = null;
182 }
Sunny Goyalfa401a12015-04-10 13:45:42 -0700183 setTextColor(mOriginalTextColor);
Sunny Goyalf37a2142016-03-24 17:28:25 -0700184 (mHideParentOnDisable ? ((ViewGroup) getParent()) : this)
185 .setVisibility(mActive ? View.VISIBLE : View.GONE);
Sunny Goyal94b510c2016-08-16 15:36:48 -0700186
187 mAccessibleDrag = options.isAccessibleDrag;
188 setOnClickListener(mAccessibleDrag ? this : null);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700189 }
190
191 @Override
192 public final boolean acceptDrop(DragObject dragObject) {
193 return supportsDrop(dragObject.dragSource, dragObject.dragInfo);
194 }
195
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700196 protected abstract boolean supportsDrop(DragSource source, ItemInfo info);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700197
198 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700199 public boolean isDropEnabled() {
Sunny Goyal4583d092016-08-17 11:11:48 -0700200 return mActive && (mAccessibleDrag ||
201 mLauncher.getDragController().getDistanceDragged() >= mDragDistanceThreshold);
Winson Chung61fa4192011-06-12 15:15:29 -0700202 }
203
Sunny Goyalfa401a12015-04-10 13:45:42 -0700204 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700205 public void onDragEnd() {
Sunny Goyalfa401a12015-04-10 13:45:42 -0700206 mActive = false;
Sunny Goyal94b510c2016-08-16 15:36:48 -0700207 setOnClickListener(null);
Winson Chung61fa4192011-06-12 15:15:29 -0700208 }
209
Sunny Goyalfa401a12015-04-10 13:45:42 -0700210 /**
211 * On drop animate the dropView to the icon.
212 */
213 @Override
214 public void onDrop(final DragObject d) {
215 final DragLayer dragLayer = mLauncher.getDragLayer();
216 final Rect from = new Rect();
217 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
218
Sunny Goyal0f76b562016-12-13 19:37:10 -0800219 final Rect to = getIconRect(d);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700220 final float scale = (float) to.width() / from.width();
Tony Wickhamb54c4a32015-09-11 08:40:20 -0700221 mDropTargetBar.deferOnDragEnd();
Sunny Goyalfa401a12015-04-10 13:45:42 -0700222
223 Runnable onAnimationEndRunnable = new Runnable() {
224 @Override
225 public void run() {
226 completeDrop(d);
Tony Wickhamb54c4a32015-09-11 08:40:20 -0700227 mDropTargetBar.onDragEnd();
Sunny Goyalfa401a12015-04-10 13:45:42 -0700228 mLauncher.exitSpringLoadedDragModeDelayed(true, 0, null);
229 }
230 };
231 dragLayer.animateView(d.dragView, from, to, scale, 1f, 1f, 0.1f, 0.1f,
Sunny Goyald139b0a2016-09-12 14:36:30 -0700232 mLauncher.getDragController().isExternalDrag() ? 1 : DRAG_VIEW_DROP_DURATION,
233 new DecelerateInterpolator(2),
Sunny Goyalfa401a12015-04-10 13:45:42 -0700234 new LinearInterpolator(), onAnimationEndRunnable,
235 DragLayer.ANIMATION_END_DISAPPEAR, null);
236 }
237
Sunny Goyale9b651e2015-04-24 11:44:51 -0700238 @Override
239 public void prepareAccessibilityDrop() { }
240
Sunny Goyal0f76b562016-12-13 19:37:10 -0800241 public abstract void completeDrop(DragObject d);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700242
Winson Chung61fa4192011-06-12 15:15:29 -0700243 @Override
Adam Cohen7d30a372013-07-01 17:03:59 -0700244 public void getHitRectRelativeToDragLayer(android.graphics.Rect outRect) {
Winson Chunga62e9fd2011-07-11 15:20:48 -0700245 super.getHitRect(outRect);
246 outRect.bottom += mBottomDragPadding;
Winson Chung156ab5b2013-07-12 14:14:16 -0700247
248 int[] coords = new int[2];
249 mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(this, coords);
250 outRect.offsetTo(coords[0], coords[1]);
Winson Chunga62e9fd2011-07-11 15:20:48 -0700251 }
252
Sunny Goyal0f76b562016-12-13 19:37:10 -0800253 public Rect getIconRect(DragObject dragObject) {
254 int viewWidth = dragObject.dragView.getMeasuredWidth();
255 int viewHeight = dragObject.dragView.getMeasuredHeight();
256 int drawableWidth = mDrawable.getIntrinsicWidth();
257 int drawableHeight = mDrawable.getIntrinsicHeight();
Winson Chung61967cb2012-02-28 18:11:33 -0800258 DragLayer dragLayer = mLauncher.getDragLayer();
259
260 // Find the rect to animate to (the view is center aligned)
261 Rect to = new Rect();
262 dragLayer.getViewRectRelativeToSelf(this, to);
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800263
264 final int width = drawableWidth;
265 final int height = drawableHeight;
266
267 final int left;
268 final int right;
269
Sunny Goyal70660032015-05-14 00:07:08 -0700270 if (Utilities.isRtl(getResources())) {
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800271 right = to.right - getPaddingRight();
272 left = right - width;
273 } else {
274 left = to.left + getPaddingLeft();
275 right = left + width;
276 }
277
278 final int top = to.top + (getMeasuredHeight() - height) / 2;
279 final int bottom = top + height;
280
281 to.set(left, top, right, bottom);
Winson Chung61967cb2012-02-28 18:11:33 -0800282
283 // Center the destination rect about the trash icon
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800284 final int xOffset = (int) -(viewWidth - width) / 2;
285 final int yOffset = (int) -(viewHeight - height) / 2;
Winson Chung61967cb2012-02-28 18:11:33 -0800286 to.offset(xOffset, yOffset);
287
288 return to;
289 }
290
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700291 @Override
292 public void onClick(View v) {
Sunny Goyalae502842016-06-17 08:43:56 -0700293 mLauncher.getAccessibilityDelegate().handleAccessibleDrop(this, null, null);
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700294 }
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700295
296 public int getTextColor() {
297 return getTextColors().getDefaultColor();
298 }
Winson Chung61fa4192011-06-12 15:15:29 -0700299}