blob: e613b3b2ff84264690a4b56b4bdfcd04c5674f09 [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 Chung61967cb2012-02-28 18:11:33 -080031import android.graphics.Rect;
Sunny Goyal3a644ed2015-05-21 10:28:02 -070032import android.graphics.drawable.Drawable;
Sunny Goyalfe0d1f22015-04-28 22:01:31 -070033import android.os.Build;
Winson Chung61fa4192011-06-12 15:15:29 -070034import android.util.AttributeSet;
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -080035import android.view.View;
Sunny Goyal1a70cef2015-04-22 11:29:51 -070036import android.view.View.OnClickListener;
Sunny Goyalfa401a12015-04-10 13:45:42 -070037import android.view.ViewGroup;
Sunny Goyale78e3d72015-09-24 11:23:31 -070038import android.view.accessibility.AccessibilityEvent;
Sunny Goyalfa401a12015-04-10 13:45:42 -070039import android.view.animation.DecelerateInterpolator;
40import android.view.animation.LinearInterpolator;
Adam Cohend4d7aa52011-07-19 21:47:37 -070041import android.widget.TextView;
Winson Chung61fa4192011-06-12 15:15:29 -070042
Vadim Tryshevfedca432015-08-19 17:55:02 -070043import com.android.launcher3.dragndrop.DragController;
44import com.android.launcher3.dragndrop.DragLayer;
Sunny Goyal94b510c2016-08-16 15:36:48 -070045import com.android.launcher3.dragndrop.DragOptions;
Vadim Tryshevfedca432015-08-19 17:55:02 -070046import com.android.launcher3.dragndrop.DragView;
Sunny Goyalfa401a12015-04-10 13:45:42 -070047import com.android.launcher3.util.Thunk;
Winson Chung61fa4192011-06-12 15:15:29 -070048
49/**
50 * Implements a DropTarget.
51 */
Sunny Goyal1a70cef2015-04-22 11:29:51 -070052public abstract class ButtonDropTarget extends TextView
53 implements DropTarget, DragController.DragListener, OnClickListener {
Sunny Goyalfa401a12015-04-10 13:45:42 -070054
Tony Wickhamb54c4a32015-09-11 08:40:20 -070055 private static final int DRAG_VIEW_DROP_DURATION = 285;
Winson Chung61fa4192011-06-12 15:15:29 -070056
Sunny Goyalf37a2142016-03-24 17:28:25 -070057 private final boolean mHideParentOnDisable;
Sunny Goyal47328fd2016-05-25 18:56:41 -070058 protected final Launcher mLauncher;
Sunny Goyalf37a2142016-03-24 17:28:25 -070059
Winson Chunga62e9fd2011-07-11 15:20:48 -070060 private int mBottomDragPadding;
Sunny Goyal47328fd2016-05-25 18:56:41 -070061 protected DropTargetBar mDropTargetBar;
Winson Chung61fa4192011-06-12 15:15:29 -070062
63 /** Whether this drop target is active for the current drag */
64 protected boolean mActive;
Sunny Goyal4583d092016-08-17 11:11:48 -070065 /** Whether an accessible drag is in progress */
66 private boolean mAccessibleDrag;
Tony Wickham855b1b52016-03-28 16:56:30 -070067 /** An item must be dragged at least this many pixels before this drop target is enabled. */
68 private final int mDragDistanceThreshold;
Winson Chung61fa4192011-06-12 15:15:29 -070069
70 /** The paint applied to the drag view on hover */
Winson Chung61967cb2012-02-28 18:11:33 -080071 protected int mHoverColor = 0;
Winson Chung61fa4192011-06-12 15:15:29 -070072
Sunny Goyalfa401a12015-04-10 13:45:42 -070073 protected ColorStateList mOriginalTextColor;
Sunny Goyal3a644ed2015-05-21 10:28:02 -070074 protected Drawable mDrawable;
Sunny Goyalfa401a12015-04-10 13:45:42 -070075
Sunny Goyal3a644ed2015-05-21 10:28:02 -070076 private AnimatorSet mCurrentColorAnim;
Sunny Goyal316490e2015-06-02 09:38:28 -070077 @Thunk ColorMatrix mSrcFilter, mDstFilter, mCurrentFilter;
Sunny Goyal3a644ed2015-05-21 10:28:02 -070078
Winson Chung61fa4192011-06-12 15:15:29 -070079 public ButtonDropTarget(Context context, AttributeSet attrs) {
80 this(context, attrs, 0);
81 }
82
83 public ButtonDropTarget(Context context, AttributeSet attrs, int defStyle) {
84 super(context, attrs, defStyle);
Tony2fd02082016-10-07 12:50:01 -070085 mLauncher = Launcher.getLauncher(context);
Sunny Goyal47328fd2016-05-25 18:56:41 -070086
Tony Wickham855b1b52016-03-28 16:56:30 -070087 Resources resources = getResources();
88 mBottomDragPadding = resources.getDimensionPixelSize(R.dimen.drop_target_drag_padding);
Sunny Goyalf37a2142016-03-24 17:28:25 -070089
90 TypedArray a = context.obtainStyledAttributes(attrs,
91 R.styleable.ButtonDropTarget, defStyle, 0);
92 mHideParentOnDisable = a.getBoolean(R.styleable.ButtonDropTarget_hideParentOnDisable, false);
93 a.recycle();
Tony Wickham855b1b52016-03-28 16:56:30 -070094 mDragDistanceThreshold = resources.getDimensionPixelSize(R.dimen.drag_distanceThreshold);
Winson Chung61fa4192011-06-12 15:15:29 -070095 }
96
Sunny Goyalfa401a12015-04-10 13:45:42 -070097 @Override
98 protected void onFinishInflate() {
99 super.onFinishInflate();
100 mOriginalTextColor = getTextColors();
Winson Chung61fa4192011-06-12 15:15:29 -0700101 }
102
Sunny Goyal70660032015-05-14 00:07:08 -0700103 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Sunny Goyalfa401a12015-04-10 13:45:42 -0700104 protected void setDrawable(int resId) {
Sunny Goyale0cab302015-05-20 16:40:30 -0700105 // We do not set the drawable in the xml as that inflates two drawables corresponding to
106 // drawableLeft and drawableStart.
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700107 mDrawable = getResources().getDrawable(resId);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700108
Sunny Goyal9fc953b2015-08-17 12:24:25 -0700109 if (Utilities.ATLEAST_JB_MR1) {
Sunny Goyale0cab302015-05-20 16:40:30 -0700110 setCompoundDrawablesRelativeWithIntrinsicBounds(mDrawable, null, null, null);
111 } else {
112 setCompoundDrawablesWithIntrinsicBounds(mDrawable, null, null, null);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700113 }
114 }
115
Sunny Goyal47328fd2016-05-25 18:56:41 -0700116 public void setDropTargetBar(DropTargetBar dropTargetBar) {
Tony Wickhamb54c4a32015-09-11 08:40:20 -0700117 mDropTargetBar = dropTargetBar;
Adam Cohend4d7aa52011-07-19 21:47:37 -0700118 }
119
Sunny Goyalfa401a12015-04-10 13:45:42 -0700120 @Override
Sunny Goyalfa401a12015-04-10 13:45:42 -0700121 public final void onDragEnter(DragObject d) {
Winson Chung61967cb2012-02-28 18:11:33 -0800122 d.dragView.setColor(mHoverColor);
Sunny Goyal9fc953b2015-08-17 12:24:25 -0700123 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700124 animateTextColor(mHoverColor);
125 } else {
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700126 if (mCurrentFilter == null) {
127 mCurrentFilter = new ColorMatrix();
128 }
129 DragView.setColorScale(mHoverColor, mCurrentFilter);
130 mDrawable.setColorFilter(new ColorMatrixColorFilter(mCurrentFilter));
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700131 setTextColor(mHoverColor);
132 }
Sunny Goyald21301e2015-09-25 12:17:08 -0700133 if (d.stateAnnouncer != null) {
134 d.stateAnnouncer.cancel();
135 }
Sunny Goyale78e3d72015-09-24 11:23:31 -0700136 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Winson Chung61fa4192011-06-12 15:15:29 -0700137 }
138
Sunny Goyalfa401a12015-04-10 13:45:42 -0700139 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700140 public void onDragOver(DragObject d) {
141 // Do nothing
142 }
143
Sunny Goyal9fc953b2015-08-17 12:24:25 -0700144 protected void resetHoverColor() {
145 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700146 animateTextColor(mOriginalTextColor.getDefaultColor());
147 } else {
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700148 mDrawable.setColorFilter(null);
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700149 setTextColor(mOriginalTextColor);
150 }
151 }
152
153 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
154 private void animateTextColor(int targetColor) {
155 if (mCurrentColorAnim != null) {
156 mCurrentColorAnim.cancel();
157 }
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700158
159 mCurrentColorAnim = new AnimatorSet();
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700160 mCurrentColorAnim.setDuration(DragView.COLOR_CHANGE_DURATION);
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700161
162 if (mSrcFilter == null) {
163 mSrcFilter = new ColorMatrix();
164 mDstFilter = new ColorMatrix();
165 mCurrentFilter = new ColorMatrix();
166 }
167
168 DragView.setColorScale(getTextColor(), mSrcFilter);
169 DragView.setColorScale(targetColor, mDstFilter);
170 ValueAnimator anim1 = ValueAnimator.ofObject(
171 new FloatArrayEvaluator(mCurrentFilter.getArray()),
172 mSrcFilter.getArray(), mDstFilter.getArray());
173 anim1.addUpdateListener(new AnimatorUpdateListener() {
174
175 @Override
176 public void onAnimationUpdate(ValueAnimator animation) {
177 mDrawable.setColorFilter(new ColorMatrixColorFilter(mCurrentFilter));
178 invalidate();
179 }
180 });
181
182 mCurrentColorAnim.play(anim1);
183 mCurrentColorAnim.play(ObjectAnimator.ofArgb(this, "textColor", targetColor));
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700184 mCurrentColorAnim.start();
Winson Chung61fa4192011-06-12 15:15:29 -0700185 }
186
Sunny Goyalfa401a12015-04-10 13:45:42 -0700187 @Override
188 public final void onDragExit(DragObject d) {
189 if (!d.dragComplete) {
190 d.dragView.setColor(0);
191 resetHoverColor();
192 } else {
193 // Restore the hover color
194 d.dragView.setColor(mHoverColor);
195 }
Winson Chung61fa4192011-06-12 15:15:29 -0700196 }
197
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700198 @Override
Sunny Goyal94b510c2016-08-16 15:36:48 -0700199 public void onDragStart(DropTarget.DragObject dragObject, DragOptions options) {
200 mActive = supportsDrop(dragObject.dragSource, dragObject.dragInfo);
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700201 mDrawable.setColorFilter(null);
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700202 if (mCurrentColorAnim != null) {
203 mCurrentColorAnim.cancel();
204 mCurrentColorAnim = null;
205 }
Sunny Goyalfa401a12015-04-10 13:45:42 -0700206 setTextColor(mOriginalTextColor);
Sunny Goyalf37a2142016-03-24 17:28:25 -0700207 (mHideParentOnDisable ? ((ViewGroup) getParent()) : this)
208 .setVisibility(mActive ? View.VISIBLE : View.GONE);
Sunny Goyal94b510c2016-08-16 15:36:48 -0700209
210 mAccessibleDrag = options.isAccessibleDrag;
211 setOnClickListener(mAccessibleDrag ? this : null);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700212 }
213
214 @Override
215 public final boolean acceptDrop(DragObject dragObject) {
216 return supportsDrop(dragObject.dragSource, dragObject.dragInfo);
217 }
218
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700219 protected abstract boolean supportsDrop(DragSource source, ItemInfo info);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700220
221 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700222 public boolean isDropEnabled() {
Sunny Goyal4583d092016-08-17 11:11:48 -0700223 return mActive && (mAccessibleDrag ||
224 mLauncher.getDragController().getDistanceDragged() >= mDragDistanceThreshold);
Winson Chung61fa4192011-06-12 15:15:29 -0700225 }
226
Sunny Goyalfa401a12015-04-10 13:45:42 -0700227 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700228 public void onDragEnd() {
Sunny Goyalfa401a12015-04-10 13:45:42 -0700229 mActive = false;
Sunny Goyal94b510c2016-08-16 15:36:48 -0700230 setOnClickListener(null);
Winson Chung61fa4192011-06-12 15:15:29 -0700231 }
232
Sunny Goyalfa401a12015-04-10 13:45:42 -0700233 /**
234 * On drop animate the dropView to the icon.
235 */
236 @Override
237 public void onDrop(final DragObject d) {
238 final DragLayer dragLayer = mLauncher.getDragLayer();
239 final Rect from = new Rect();
240 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
241
Sunny Goyal0f76b562016-12-13 19:37:10 -0800242 final Rect to = getIconRect(d);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700243 final float scale = (float) to.width() / from.width();
Tony Wickhamb54c4a32015-09-11 08:40:20 -0700244 mDropTargetBar.deferOnDragEnd();
Sunny Goyalfa401a12015-04-10 13:45:42 -0700245
246 Runnable onAnimationEndRunnable = new Runnable() {
247 @Override
248 public void run() {
249 completeDrop(d);
Tony Wickhamb54c4a32015-09-11 08:40:20 -0700250 mDropTargetBar.onDragEnd();
Sunny Goyalfa401a12015-04-10 13:45:42 -0700251 mLauncher.exitSpringLoadedDragModeDelayed(true, 0, null);
252 }
253 };
254 dragLayer.animateView(d.dragView, from, to, scale, 1f, 1f, 0.1f, 0.1f,
Sunny Goyald139b0a2016-09-12 14:36:30 -0700255 mLauncher.getDragController().isExternalDrag() ? 1 : DRAG_VIEW_DROP_DURATION,
256 new DecelerateInterpolator(2),
Sunny Goyalfa401a12015-04-10 13:45:42 -0700257 new LinearInterpolator(), onAnimationEndRunnable,
258 DragLayer.ANIMATION_END_DISAPPEAR, null);
259 }
260
Sunny Goyale9b651e2015-04-24 11:44:51 -0700261 @Override
262 public void prepareAccessibilityDrop() { }
263
Sunny Goyal0f76b562016-12-13 19:37:10 -0800264 public abstract void completeDrop(DragObject d);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700265
Winson Chung61fa4192011-06-12 15:15:29 -0700266 @Override
Adam Cohen7d30a372013-07-01 17:03:59 -0700267 public void getHitRectRelativeToDragLayer(android.graphics.Rect outRect) {
Winson Chunga62e9fd2011-07-11 15:20:48 -0700268 super.getHitRect(outRect);
269 outRect.bottom += mBottomDragPadding;
Winson Chung156ab5b2013-07-12 14:14:16 -0700270
271 int[] coords = new int[2];
272 mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(this, coords);
273 outRect.offsetTo(coords[0], coords[1]);
Winson Chunga62e9fd2011-07-11 15:20:48 -0700274 }
275
Sunny Goyal0f76b562016-12-13 19:37:10 -0800276 public Rect getIconRect(DragObject dragObject) {
277 int viewWidth = dragObject.dragView.getMeasuredWidth();
278 int viewHeight = dragObject.dragView.getMeasuredHeight();
279 int drawableWidth = mDrawable.getIntrinsicWidth();
280 int drawableHeight = mDrawable.getIntrinsicHeight();
Winson Chung61967cb2012-02-28 18:11:33 -0800281 DragLayer dragLayer = mLauncher.getDragLayer();
282
283 // Find the rect to animate to (the view is center aligned)
284 Rect to = new Rect();
285 dragLayer.getViewRectRelativeToSelf(this, to);
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800286
287 final int width = drawableWidth;
288 final int height = drawableHeight;
289
290 final int left;
291 final int right;
292
Sunny Goyal70660032015-05-14 00:07:08 -0700293 if (Utilities.isRtl(getResources())) {
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800294 right = to.right - getPaddingRight();
295 left = right - width;
296 } else {
297 left = to.left + getPaddingLeft();
298 right = left + width;
299 }
300
301 final int top = to.top + (getMeasuredHeight() - height) / 2;
302 final int bottom = top + height;
303
304 to.set(left, top, right, bottom);
Winson Chung61967cb2012-02-28 18:11:33 -0800305
306 // Center the destination rect about the trash icon
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800307 final int xOffset = (int) -(viewWidth - width) / 2;
308 final int yOffset = (int) -(viewHeight - height) / 2;
Winson Chung61967cb2012-02-28 18:11:33 -0800309 to.offset(xOffset, yOffset);
310
311 return to;
312 }
313
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700314 @Override
315 public void onClick(View v) {
Sunny Goyalae502842016-06-17 08:43:56 -0700316 mLauncher.getAccessibilityDelegate().handleAccessibleDrop(this, null, null);
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700317 }
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700318
319 public int getTextColor() {
320 return getTextColors().getDefaultColor();
321 }
Winson Chung61fa4192011-06-12 15:15:29 -0700322}