blob: 8a477d809f91ed3dd806add15bbff8ae3852359e [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;
Tony Wickham6c828672017-02-27 13:30:20 -080045import com.android.launcher3.util.Themes;
Sunny Goyalfa401a12015-04-10 13:45:42 -070046import com.android.launcher3.util.Thunk;
Winson Chung61fa4192011-06-12 15:15:29 -070047
48/**
49 * Implements a DropTarget.
50 */
Sunny Goyal1a70cef2015-04-22 11:29:51 -070051public abstract class ButtonDropTarget extends TextView
52 implements DropTarget, DragController.DragListener, OnClickListener {
Sunny Goyalfa401a12015-04-10 13:45:42 -070053
Tony Wickhamb54c4a32015-09-11 08:40:20 -070054 private static final int DRAG_VIEW_DROP_DURATION = 285;
Winson Chung61fa4192011-06-12 15:15:29 -070055
Sunny Goyalf37a2142016-03-24 17:28:25 -070056 private final boolean mHideParentOnDisable;
Sunny Goyal47328fd2016-05-25 18:56:41 -070057 protected final Launcher mLauncher;
Sunny Goyalf37a2142016-03-24 17:28:25 -070058
Winson Chunga62e9fd2011-07-11 15:20:48 -070059 private int mBottomDragPadding;
Sunny Goyal47328fd2016-05-25 18:56:41 -070060 protected DropTargetBar mDropTargetBar;
Winson Chung61fa4192011-06-12 15:15:29 -070061
62 /** Whether this drop target is active for the current drag */
63 protected boolean mActive;
Sunny Goyal4583d092016-08-17 11:11:48 -070064 /** Whether an accessible drag is in progress */
65 private boolean mAccessibleDrag;
Tony Wickham855b1b52016-03-28 16:56:30 -070066 /** An item must be dragged at least this many pixels before this drop target is enabled. */
67 private final int mDragDistanceThreshold;
Winson Chung61fa4192011-06-12 15:15:29 -070068
69 /** The paint applied to the drag view on hover */
Winson Chung61967cb2012-02-28 18:11:33 -080070 protected int mHoverColor = 0;
Winson Chung61fa4192011-06-12 15:15:29 -070071
Sunny Goyalfa401a12015-04-10 13:45:42 -070072 protected ColorStateList mOriginalTextColor;
Sunny Goyal3a644ed2015-05-21 10:28:02 -070073 protected Drawable mDrawable;
Sunny Goyalfa401a12015-04-10 13:45:42 -070074
Sunny Goyal3a644ed2015-05-21 10:28:02 -070075 private AnimatorSet mCurrentColorAnim;
Sunny Goyal316490e2015-06-02 09:38:28 -070076 @Thunk ColorMatrix mSrcFilter, mDstFilter, mCurrentFilter;
Sunny Goyal3a644ed2015-05-21 10:28:02 -070077
Winson Chung61fa4192011-06-12 15:15:29 -070078 public ButtonDropTarget(Context context, AttributeSet attrs) {
79 this(context, attrs, 0);
80 }
81
82 public ButtonDropTarget(Context context, AttributeSet attrs, int defStyle) {
83 super(context, attrs, defStyle);
Tony2fd02082016-10-07 12:50:01 -070084 mLauncher = Launcher.getLauncher(context);
Sunny Goyal47328fd2016-05-25 18:56:41 -070085
Tony Wickham855b1b52016-03-28 16:56:30 -070086 Resources resources = getResources();
87 mBottomDragPadding = resources.getDimensionPixelSize(R.dimen.drop_target_drag_padding);
Sunny Goyalf37a2142016-03-24 17:28:25 -070088
89 TypedArray a = context.obtainStyledAttributes(attrs,
90 R.styleable.ButtonDropTarget, defStyle, 0);
91 mHideParentOnDisable = a.getBoolean(R.styleable.ButtonDropTarget_hideParentOnDisable, false);
92 a.recycle();
Tony Wickham855b1b52016-03-28 16:56:30 -070093 mDragDistanceThreshold = resources.getDimensionPixelSize(R.dimen.drag_distanceThreshold);
Winson Chung61fa4192011-06-12 15:15:29 -070094 }
95
Sunny Goyalfa401a12015-04-10 13:45:42 -070096 @Override
97 protected void onFinishInflate() {
98 super.onFinishInflate();
99 mOriginalTextColor = getTextColors();
Winson Chung61fa4192011-06-12 15:15:29 -0700100 }
101
Sunny Goyalfa401a12015-04-10 13:45:42 -0700102 protected void setDrawable(int resId) {
Sunny Goyale0cab302015-05-20 16:40:30 -0700103 // We do not set the drawable in the xml as that inflates two drawables corresponding to
104 // drawableLeft and drawableStart.
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700105 mDrawable = getResources().getDrawable(resId);
Sunny Goyala52ecb02016-12-16 15:04:51 -0800106 setCompoundDrawablesRelativeWithIntrinsicBounds(mDrawable, null, null, null);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700107 }
108
Sunny Goyal47328fd2016-05-25 18:56:41 -0700109 public void setDropTargetBar(DropTargetBar dropTargetBar) {
Tony Wickhamb54c4a32015-09-11 08:40:20 -0700110 mDropTargetBar = dropTargetBar;
Adam Cohend4d7aa52011-07-19 21:47:37 -0700111 }
112
Sunny Goyalfa401a12015-04-10 13:45:42 -0700113 @Override
Sunny Goyalfa401a12015-04-10 13:45:42 -0700114 public final void onDragEnter(DragObject d) {
Winson Chung61967cb2012-02-28 18:11:33 -0800115 d.dragView.setColor(mHoverColor);
Sunny Goyala52ecb02016-12-16 15:04:51 -0800116 animateTextColor(mHoverColor);
Sunny Goyald21301e2015-09-25 12:17:08 -0700117 if (d.stateAnnouncer != null) {
118 d.stateAnnouncer.cancel();
119 }
Sunny Goyale78e3d72015-09-24 11:23:31 -0700120 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Winson Chung61fa4192011-06-12 15:15:29 -0700121 }
122
Sunny Goyalfa401a12015-04-10 13:45:42 -0700123 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700124 public void onDragOver(DragObject d) {
125 // Do nothing
126 }
127
Sunny Goyal9fc953b2015-08-17 12:24:25 -0700128 protected void resetHoverColor() {
Sunny Goyala52ecb02016-12-16 15:04:51 -0800129 animateTextColor(mOriginalTextColor.getDefaultColor());
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700130 }
131
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700132 private void animateTextColor(int targetColor) {
133 if (mCurrentColorAnim != null) {
134 mCurrentColorAnim.cancel();
135 }
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700136
137 mCurrentColorAnim = new AnimatorSet();
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700138 mCurrentColorAnim.setDuration(DragView.COLOR_CHANGE_DURATION);
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700139
140 if (mSrcFilter == null) {
141 mSrcFilter = new ColorMatrix();
142 mDstFilter = new ColorMatrix();
143 mCurrentFilter = new ColorMatrix();
144 }
145
Tony Wickham6c828672017-02-27 13:30:20 -0800146 Themes.setColorScaleOnMatrix(getTextColor(), mSrcFilter);
147 Themes.setColorScaleOnMatrix(targetColor, mDstFilter);
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700148 ValueAnimator anim1 = ValueAnimator.ofObject(
149 new FloatArrayEvaluator(mCurrentFilter.getArray()),
150 mSrcFilter.getArray(), mDstFilter.getArray());
151 anim1.addUpdateListener(new AnimatorUpdateListener() {
152
153 @Override
154 public void onAnimationUpdate(ValueAnimator animation) {
155 mDrawable.setColorFilter(new ColorMatrixColorFilter(mCurrentFilter));
156 invalidate();
157 }
158 });
159
160 mCurrentColorAnim.play(anim1);
161 mCurrentColorAnim.play(ObjectAnimator.ofArgb(this, "textColor", targetColor));
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700162 mCurrentColorAnim.start();
Winson Chung61fa4192011-06-12 15:15:29 -0700163 }
164
Sunny Goyalfa401a12015-04-10 13:45:42 -0700165 @Override
166 public final void onDragExit(DragObject d) {
167 if (!d.dragComplete) {
168 d.dragView.setColor(0);
169 resetHoverColor();
170 } else {
171 // Restore the hover color
172 d.dragView.setColor(mHoverColor);
173 }
Winson Chung61fa4192011-06-12 15:15:29 -0700174 }
175
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700176 @Override
Sunny Goyal94b510c2016-08-16 15:36:48 -0700177 public void onDragStart(DropTarget.DragObject dragObject, DragOptions options) {
178 mActive = supportsDrop(dragObject.dragSource, dragObject.dragInfo);
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700179 mDrawable.setColorFilter(null);
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700180 if (mCurrentColorAnim != null) {
181 mCurrentColorAnim.cancel();
182 mCurrentColorAnim = null;
183 }
Sunny Goyalfa401a12015-04-10 13:45:42 -0700184 setTextColor(mOriginalTextColor);
Sunny Goyalf37a2142016-03-24 17:28:25 -0700185 (mHideParentOnDisable ? ((ViewGroup) getParent()) : this)
186 .setVisibility(mActive ? View.VISIBLE : View.GONE);
Sunny Goyal94b510c2016-08-16 15:36:48 -0700187
188 mAccessibleDrag = options.isAccessibleDrag;
189 setOnClickListener(mAccessibleDrag ? this : null);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700190 }
191
192 @Override
193 public final boolean acceptDrop(DragObject dragObject) {
194 return supportsDrop(dragObject.dragSource, dragObject.dragInfo);
195 }
196
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700197 protected abstract boolean supportsDrop(DragSource source, ItemInfo info);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700198
199 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700200 public boolean isDropEnabled() {
Sunny Goyal4583d092016-08-17 11:11:48 -0700201 return mActive && (mAccessibleDrag ||
202 mLauncher.getDragController().getDistanceDragged() >= mDragDistanceThreshold);
Winson Chung61fa4192011-06-12 15:15:29 -0700203 }
204
Sunny Goyalfa401a12015-04-10 13:45:42 -0700205 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700206 public void onDragEnd() {
Sunny Goyalfa401a12015-04-10 13:45:42 -0700207 mActive = false;
Sunny Goyal94b510c2016-08-16 15:36:48 -0700208 setOnClickListener(null);
Winson Chung61fa4192011-06-12 15:15:29 -0700209 }
210
Sunny Goyalfa401a12015-04-10 13:45:42 -0700211 /**
212 * On drop animate the dropView to the icon.
213 */
214 @Override
215 public void onDrop(final DragObject d) {
216 final DragLayer dragLayer = mLauncher.getDragLayer();
217 final Rect from = new Rect();
218 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
219
Sunny Goyal0f76b562016-12-13 19:37:10 -0800220 final Rect to = getIconRect(d);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700221 final float scale = (float) to.width() / from.width();
Tony Wickhamb54c4a32015-09-11 08:40:20 -0700222 mDropTargetBar.deferOnDragEnd();
Sunny Goyalfa401a12015-04-10 13:45:42 -0700223
224 Runnable onAnimationEndRunnable = new Runnable() {
225 @Override
226 public void run() {
227 completeDrop(d);
Tony Wickhamb54c4a32015-09-11 08:40:20 -0700228 mDropTargetBar.onDragEnd();
Sunny Goyalfa401a12015-04-10 13:45:42 -0700229 mLauncher.exitSpringLoadedDragModeDelayed(true, 0, null);
230 }
231 };
232 dragLayer.animateView(d.dragView, from, to, scale, 1f, 1f, 0.1f, 0.1f,
Sunny Goyaldec3a902017-01-25 18:23:36 -0800233 DRAG_VIEW_DROP_DURATION,
Sunny Goyald139b0a2016-09-12 14:36:30 -0700234 new DecelerateInterpolator(2),
Sunny Goyalfa401a12015-04-10 13:45:42 -0700235 new LinearInterpolator(), onAnimationEndRunnable,
236 DragLayer.ANIMATION_END_DISAPPEAR, null);
237 }
238
Sunny Goyale9b651e2015-04-24 11:44:51 -0700239 @Override
240 public void prepareAccessibilityDrop() { }
241
Sunny Goyal0f76b562016-12-13 19:37:10 -0800242 public abstract void completeDrop(DragObject d);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700243
Winson Chung61fa4192011-06-12 15:15:29 -0700244 @Override
Adam Cohen7d30a372013-07-01 17:03:59 -0700245 public void getHitRectRelativeToDragLayer(android.graphics.Rect outRect) {
Winson Chunga62e9fd2011-07-11 15:20:48 -0700246 super.getHitRect(outRect);
247 outRect.bottom += mBottomDragPadding;
Winson Chung156ab5b2013-07-12 14:14:16 -0700248
249 int[] coords = new int[2];
250 mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(this, coords);
251 outRect.offsetTo(coords[0], coords[1]);
Winson Chunga62e9fd2011-07-11 15:20:48 -0700252 }
253
Sunny Goyal0f76b562016-12-13 19:37:10 -0800254 public Rect getIconRect(DragObject dragObject) {
255 int viewWidth = dragObject.dragView.getMeasuredWidth();
256 int viewHeight = dragObject.dragView.getMeasuredHeight();
257 int drawableWidth = mDrawable.getIntrinsicWidth();
258 int drawableHeight = mDrawable.getIntrinsicHeight();
Winson Chung61967cb2012-02-28 18:11:33 -0800259 DragLayer dragLayer = mLauncher.getDragLayer();
260
261 // Find the rect to animate to (the view is center aligned)
262 Rect to = new Rect();
263 dragLayer.getViewRectRelativeToSelf(this, to);
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800264
265 final int width = drawableWidth;
266 final int height = drawableHeight;
267
268 final int left;
269 final int right;
270
Sunny Goyal70660032015-05-14 00:07:08 -0700271 if (Utilities.isRtl(getResources())) {
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800272 right = to.right - getPaddingRight();
273 left = right - width;
274 } else {
275 left = to.left + getPaddingLeft();
276 right = left + width;
277 }
278
279 final int top = to.top + (getMeasuredHeight() - height) / 2;
280 final int bottom = top + height;
281
282 to.set(left, top, right, bottom);
Winson Chung61967cb2012-02-28 18:11:33 -0800283
284 // Center the destination rect about the trash icon
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800285 final int xOffset = (int) -(viewWidth - width) / 2;
286 final int yOffset = (int) -(viewHeight - height) / 2;
Winson Chung61967cb2012-02-28 18:11:33 -0800287 to.offset(xOffset, yOffset);
288
289 return to;
290 }
291
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700292 @Override
293 public void onClick(View v) {
Sunny Goyalae502842016-06-17 08:43:56 -0700294 mLauncher.getAccessibilityDelegate().handleAccessibleDrop(this, null, null);
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700295 }
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700296
297 public int getTextColor() {
298 return getTextColors().getDefaultColor();
299 }
Winson Chung61fa4192011-06-12 15:15:29 -0700300}