blob: 60a2cc3259215bbc12568a7b2210d20ae41f7fc8 [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;
Sunny Goyalfe0d1f22015-04-28 22:01:31 -070024import android.annotation.TargetApi;
Winson Chung61fa4192011-06-12 15:15:29 -070025import android.content.Context;
Sunny Goyalfa401a12015-04-10 13:45:42 -070026import android.content.res.ColorStateList;
Tony Wickham855b1b52016-03-28 16:56:30 -070027import android.content.res.Resources;
Sunny Goyalf37a2142016-03-24 17:28:25 -070028import android.content.res.TypedArray;
Sunny Goyal3a644ed2015-05-21 10:28:02 -070029import android.graphics.ColorMatrix;
30import android.graphics.ColorMatrixColorFilter;
Winson Chung043f2af2012-03-01 16:09:54 -080031import android.graphics.PointF;
Winson Chung61967cb2012-02-28 18:11:33 -080032import android.graphics.Rect;
Sunny Goyal3a644ed2015-05-21 10:28:02 -070033import android.graphics.drawable.Drawable;
Sunny Goyalfe0d1f22015-04-28 22:01:31 -070034import android.os.Build;
Winson Chung61fa4192011-06-12 15:15:29 -070035import android.util.AttributeSet;
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -080036import android.view.View;
Sunny Goyal1a70cef2015-04-22 11:29:51 -070037import android.view.View.OnClickListener;
Sunny Goyalfa401a12015-04-10 13:45:42 -070038import android.view.ViewGroup;
Sunny Goyale78e3d72015-09-24 11:23:31 -070039import android.view.accessibility.AccessibilityEvent;
Sunny Goyalfa401a12015-04-10 13:45:42 -070040import android.view.animation.DecelerateInterpolator;
41import android.view.animation.LinearInterpolator;
Adam Cohend4d7aa52011-07-19 21:47:37 -070042import android.widget.TextView;
Winson Chung61fa4192011-06-12 15:15:29 -070043
Vadim Tryshevfedca432015-08-19 17:55:02 -070044import com.android.launcher3.dragndrop.DragController;
45import com.android.launcher3.dragndrop.DragLayer;
Sunny Goyal94b510c2016-08-16 15:36:48 -070046import com.android.launcher3.dragndrop.DragOptions;
Vadim Tryshevfedca432015-08-19 17:55:02 -070047import com.android.launcher3.dragndrop.DragView;
Sunny Goyalfa401a12015-04-10 13:45:42 -070048import com.android.launcher3.util.Thunk;
Winson Chung61fa4192011-06-12 15:15:29 -070049
50/**
51 * Implements a DropTarget.
52 */
Sunny Goyal1a70cef2015-04-22 11:29:51 -070053public abstract class ButtonDropTarget extends TextView
54 implements DropTarget, DragController.DragListener, OnClickListener {
Sunny Goyalfa401a12015-04-10 13:45:42 -070055
Tony Wickhamb54c4a32015-09-11 08:40:20 -070056 private static final int DRAG_VIEW_DROP_DURATION = 285;
Winson Chung61fa4192011-06-12 15:15:29 -070057
Sunny Goyalf37a2142016-03-24 17:28:25 -070058 private final boolean mHideParentOnDisable;
Sunny Goyal47328fd2016-05-25 18:56:41 -070059 protected final Launcher mLauncher;
Sunny Goyalf37a2142016-03-24 17:28:25 -070060
Winson Chunga62e9fd2011-07-11 15:20:48 -070061 private int mBottomDragPadding;
Sunny Goyal47328fd2016-05-25 18:56:41 -070062 protected DropTargetBar mDropTargetBar;
Winson Chung61fa4192011-06-12 15:15:29 -070063
64 /** Whether this drop target is active for the current drag */
65 protected boolean mActive;
Sunny Goyal4583d092016-08-17 11:11:48 -070066 /** Whether an accessible drag is in progress */
67 private boolean mAccessibleDrag;
Tony Wickham855b1b52016-03-28 16:56:30 -070068 /** An item must be dragged at least this many pixels before this drop target is enabled. */
69 private final int mDragDistanceThreshold;
Winson Chung61fa4192011-06-12 15:15:29 -070070
71 /** The paint applied to the drag view on hover */
Winson Chung61967cb2012-02-28 18:11:33 -080072 protected int mHoverColor = 0;
Winson Chung61fa4192011-06-12 15:15:29 -070073
Sunny Goyalfa401a12015-04-10 13:45:42 -070074 protected ColorStateList mOriginalTextColor;
Sunny Goyal3a644ed2015-05-21 10:28:02 -070075 protected Drawable mDrawable;
Sunny Goyalfa401a12015-04-10 13:45:42 -070076
Sunny Goyal3a644ed2015-05-21 10:28:02 -070077 private AnimatorSet mCurrentColorAnim;
Sunny Goyal316490e2015-06-02 09:38:28 -070078 @Thunk ColorMatrix mSrcFilter, mDstFilter, mCurrentFilter;
Sunny Goyal3a644ed2015-05-21 10:28:02 -070079
Winson Chung61fa4192011-06-12 15:15:29 -070080 public ButtonDropTarget(Context context, AttributeSet attrs) {
81 this(context, attrs, 0);
82 }
83
84 public ButtonDropTarget(Context context, AttributeSet attrs, int defStyle) {
85 super(context, attrs, defStyle);
Tony2fd02082016-10-07 12:50:01 -070086 mLauncher = Launcher.getLauncher(context);
Sunny Goyal47328fd2016-05-25 18:56:41 -070087
Tony Wickham855b1b52016-03-28 16:56:30 -070088 Resources resources = getResources();
89 mBottomDragPadding = resources.getDimensionPixelSize(R.dimen.drop_target_drag_padding);
Sunny Goyalf37a2142016-03-24 17:28:25 -070090
91 TypedArray a = context.obtainStyledAttributes(attrs,
92 R.styleable.ButtonDropTarget, defStyle, 0);
93 mHideParentOnDisable = a.getBoolean(R.styleable.ButtonDropTarget_hideParentOnDisable, false);
94 a.recycle();
Tony Wickham855b1b52016-03-28 16:56:30 -070095 mDragDistanceThreshold = resources.getDimensionPixelSize(R.dimen.drag_distanceThreshold);
Winson Chung61fa4192011-06-12 15:15:29 -070096 }
97
Sunny Goyalfa401a12015-04-10 13:45:42 -070098 @Override
99 protected void onFinishInflate() {
100 super.onFinishInflate();
101 mOriginalTextColor = getTextColors();
Winson Chung61fa4192011-06-12 15:15:29 -0700102 }
103
Sunny Goyal70660032015-05-14 00:07:08 -0700104 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Sunny Goyalfa401a12015-04-10 13:45:42 -0700105 protected void setDrawable(int resId) {
Sunny Goyale0cab302015-05-20 16:40:30 -0700106 // We do not set the drawable in the xml as that inflates two drawables corresponding to
107 // drawableLeft and drawableStart.
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700108 mDrawable = getResources().getDrawable(resId);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700109
Sunny Goyal9fc953b2015-08-17 12:24:25 -0700110 if (Utilities.ATLEAST_JB_MR1) {
Sunny Goyale0cab302015-05-20 16:40:30 -0700111 setCompoundDrawablesRelativeWithIntrinsicBounds(mDrawable, null, null, null);
112 } else {
113 setCompoundDrawablesWithIntrinsicBounds(mDrawable, null, null, null);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700114 }
115 }
116
Sunny Goyal47328fd2016-05-25 18:56:41 -0700117 public void setDropTargetBar(DropTargetBar dropTargetBar) {
Tony Wickhamb54c4a32015-09-11 08:40:20 -0700118 mDropTargetBar = dropTargetBar;
Adam Cohend4d7aa52011-07-19 21:47:37 -0700119 }
120
Sunny Goyalfa401a12015-04-10 13:45:42 -0700121 @Override
Sunny Goyalddec7342015-04-29 18:12:37 -0700122 public void onFlingToDelete(DragObject d, PointF vec) { }
Winson Chung61fa4192011-06-12 15:15:29 -0700123
Sunny Goyalfa401a12015-04-10 13:45:42 -0700124 @Override
125 public final void onDragEnter(DragObject d) {
Winson Chung61967cb2012-02-28 18:11:33 -0800126 d.dragView.setColor(mHoverColor);
Sunny Goyal9fc953b2015-08-17 12:24:25 -0700127 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700128 animateTextColor(mHoverColor);
129 } else {
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700130 if (mCurrentFilter == null) {
131 mCurrentFilter = new ColorMatrix();
132 }
133 DragView.setColorScale(mHoverColor, mCurrentFilter);
134 mDrawable.setColorFilter(new ColorMatrixColorFilter(mCurrentFilter));
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700135 setTextColor(mHoverColor);
136 }
Sunny Goyald21301e2015-09-25 12:17:08 -0700137 if (d.stateAnnouncer != null) {
138 d.stateAnnouncer.cancel();
139 }
Sunny Goyale78e3d72015-09-24 11:23:31 -0700140 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Winson Chung61fa4192011-06-12 15:15:29 -0700141 }
142
Sunny Goyalfa401a12015-04-10 13:45:42 -0700143 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700144 public void onDragOver(DragObject d) {
145 // Do nothing
146 }
147
Sunny Goyal9fc953b2015-08-17 12:24:25 -0700148 protected void resetHoverColor() {
149 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700150 animateTextColor(mOriginalTextColor.getDefaultColor());
151 } else {
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700152 mDrawable.setColorFilter(null);
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700153 setTextColor(mOriginalTextColor);
154 }
155 }
156
157 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
158 private void animateTextColor(int targetColor) {
159 if (mCurrentColorAnim != null) {
160 mCurrentColorAnim.cancel();
161 }
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700162
163 mCurrentColorAnim = new AnimatorSet();
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700164 mCurrentColorAnim.setDuration(DragView.COLOR_CHANGE_DURATION);
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700165
166 if (mSrcFilter == null) {
167 mSrcFilter = new ColorMatrix();
168 mDstFilter = new ColorMatrix();
169 mCurrentFilter = new ColorMatrix();
170 }
171
172 DragView.setColorScale(getTextColor(), mSrcFilter);
173 DragView.setColorScale(targetColor, mDstFilter);
174 ValueAnimator anim1 = ValueAnimator.ofObject(
175 new FloatArrayEvaluator(mCurrentFilter.getArray()),
176 mSrcFilter.getArray(), mDstFilter.getArray());
177 anim1.addUpdateListener(new AnimatorUpdateListener() {
178
179 @Override
180 public void onAnimationUpdate(ValueAnimator animation) {
181 mDrawable.setColorFilter(new ColorMatrixColorFilter(mCurrentFilter));
182 invalidate();
183 }
184 });
185
186 mCurrentColorAnim.play(anim1);
187 mCurrentColorAnim.play(ObjectAnimator.ofArgb(this, "textColor", targetColor));
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700188 mCurrentColorAnim.start();
Winson Chung61fa4192011-06-12 15:15:29 -0700189 }
190
Sunny Goyalfa401a12015-04-10 13:45:42 -0700191 @Override
192 public final void onDragExit(DragObject d) {
193 if (!d.dragComplete) {
194 d.dragView.setColor(0);
195 resetHoverColor();
196 } else {
197 // Restore the hover color
198 d.dragView.setColor(mHoverColor);
199 }
Winson Chung61fa4192011-06-12 15:15:29 -0700200 }
201
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700202 @Override
Sunny Goyal94b510c2016-08-16 15:36:48 -0700203 public void onDragStart(DropTarget.DragObject dragObject, DragOptions options) {
204 mActive = supportsDrop(dragObject.dragSource, dragObject.dragInfo);
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700205 mDrawable.setColorFilter(null);
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700206 if (mCurrentColorAnim != null) {
207 mCurrentColorAnim.cancel();
208 mCurrentColorAnim = null;
209 }
Sunny Goyalfa401a12015-04-10 13:45:42 -0700210 setTextColor(mOriginalTextColor);
Sunny Goyalf37a2142016-03-24 17:28:25 -0700211 (mHideParentOnDisable ? ((ViewGroup) getParent()) : this)
212 .setVisibility(mActive ? View.VISIBLE : View.GONE);
Sunny Goyal94b510c2016-08-16 15:36:48 -0700213
214 mAccessibleDrag = options.isAccessibleDrag;
215 setOnClickListener(mAccessibleDrag ? this : null);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700216 }
217
218 @Override
219 public final boolean acceptDrop(DragObject dragObject) {
220 return supportsDrop(dragObject.dragSource, dragObject.dragInfo);
221 }
222
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700223 protected abstract boolean supportsDrop(DragSource source, ItemInfo info);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700224
225 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700226 public boolean isDropEnabled() {
Sunny Goyal4583d092016-08-17 11:11:48 -0700227 return mActive && (mAccessibleDrag ||
228 mLauncher.getDragController().getDistanceDragged() >= mDragDistanceThreshold);
Winson Chung61fa4192011-06-12 15:15:29 -0700229 }
230
Sunny Goyalfa401a12015-04-10 13:45:42 -0700231 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700232 public void onDragEnd() {
Sunny Goyalfa401a12015-04-10 13:45:42 -0700233 mActive = false;
Sunny Goyal94b510c2016-08-16 15:36:48 -0700234 setOnClickListener(null);
Winson Chung61fa4192011-06-12 15:15:29 -0700235 }
236
Sunny Goyalfa401a12015-04-10 13:45:42 -0700237 /**
238 * On drop animate the dropView to the icon.
239 */
240 @Override
241 public void onDrop(final DragObject d) {
242 final DragLayer dragLayer = mLauncher.getDragLayer();
243 final Rect from = new Rect();
244 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
245
246 int width = mDrawable.getIntrinsicWidth();
247 int height = mDrawable.getIntrinsicHeight();
248 final Rect to = getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(),
249 width, height);
250 final float scale = (float) to.width() / from.width();
Tony Wickhamb54c4a32015-09-11 08:40:20 -0700251 mDropTargetBar.deferOnDragEnd();
Sunny Goyalfa401a12015-04-10 13:45:42 -0700252
253 Runnable onAnimationEndRunnable = new Runnable() {
254 @Override
255 public void run() {
256 completeDrop(d);
Tony Wickhamb54c4a32015-09-11 08:40:20 -0700257 mDropTargetBar.onDragEnd();
Sunny Goyalfa401a12015-04-10 13:45:42 -0700258 mLauncher.exitSpringLoadedDragModeDelayed(true, 0, null);
259 }
260 };
261 dragLayer.animateView(d.dragView, from, to, scale, 1f, 1f, 0.1f, 0.1f,
Sunny Goyald139b0a2016-09-12 14:36:30 -0700262 mLauncher.getDragController().isExternalDrag() ? 1 : DRAG_VIEW_DROP_DURATION,
263 new DecelerateInterpolator(2),
Sunny Goyalfa401a12015-04-10 13:45:42 -0700264 new LinearInterpolator(), onAnimationEndRunnable,
265 DragLayer.ANIMATION_END_DISAPPEAR, null);
266 }
267
Sunny Goyale9b651e2015-04-24 11:44:51 -0700268 @Override
269 public void prepareAccessibilityDrop() { }
270
Sunny Goyalfa401a12015-04-10 13:45:42 -0700271 @Thunk abstract void completeDrop(DragObject d);
272
Winson Chung61fa4192011-06-12 15:15:29 -0700273 @Override
Adam Cohen7d30a372013-07-01 17:03:59 -0700274 public void getHitRectRelativeToDragLayer(android.graphics.Rect outRect) {
Winson Chunga62e9fd2011-07-11 15:20:48 -0700275 super.getHitRect(outRect);
276 outRect.bottom += mBottomDragPadding;
Winson Chung156ab5b2013-07-12 14:14:16 -0700277
278 int[] coords = new int[2];
279 mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(this, coords);
280 outRect.offsetTo(coords[0], coords[1]);
Winson Chunga62e9fd2011-07-11 15:20:48 -0700281 }
282
Sunny Goyalfa401a12015-04-10 13:45:42 -0700283 protected Rect getIconRect(int viewWidth, int viewHeight, int drawableWidth, int drawableHeight) {
Winson Chung61967cb2012-02-28 18:11:33 -0800284 DragLayer dragLayer = mLauncher.getDragLayer();
285
286 // Find the rect to animate to (the view is center aligned)
287 Rect to = new Rect();
288 dragLayer.getViewRectRelativeToSelf(this, to);
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800289
290 final int width = drawableWidth;
291 final int height = drawableHeight;
292
293 final int left;
294 final int right;
295
Sunny Goyal70660032015-05-14 00:07:08 -0700296 if (Utilities.isRtl(getResources())) {
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800297 right = to.right - getPaddingRight();
298 left = right - width;
299 } else {
300 left = to.left + getPaddingLeft();
301 right = left + width;
302 }
303
304 final int top = to.top + (getMeasuredHeight() - height) / 2;
305 final int bottom = top + height;
306
307 to.set(left, top, right, bottom);
Winson Chung61967cb2012-02-28 18:11:33 -0800308
309 // Center the destination rect about the trash icon
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800310 final int xOffset = (int) -(viewWidth - width) / 2;
311 final int yOffset = (int) -(viewHeight - height) / 2;
Winson Chung61967cb2012-02-28 18:11:33 -0800312 to.offset(xOffset, yOffset);
313
314 return to;
315 }
316
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700317 @Override
318 public void onClick(View v) {
Sunny Goyalae502842016-06-17 08:43:56 -0700319 mLauncher.getAccessibilityDelegate().handleAccessibleDrop(this, null, null);
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700320 }
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700321
322 public int getTextColor() {
323 return getTextColors().getDefaultColor();
324 }
Winson Chung61fa4192011-06-12 15:15:29 -0700325}