blob: 4b94a1ab6ae2ce88e1d1d611d58cb2271b15241e [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;
Sunny Goyal3a644ed2015-05-21 10:28:02 -070027import android.graphics.ColorMatrix;
28import android.graphics.ColorMatrixColorFilter;
Winson Chung043f2af2012-03-01 16:09:54 -080029import android.graphics.PointF;
Winson Chung61967cb2012-02-28 18:11:33 -080030import android.graphics.Rect;
Sunny Goyal3a644ed2015-05-21 10:28:02 -070031import android.graphics.drawable.Drawable;
Sunny Goyalfe0d1f22015-04-28 22:01:31 -070032import android.os.Build;
Winson Chung61fa4192011-06-12 15:15:29 -070033import android.util.AttributeSet;
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -080034import android.view.View;
Sunny Goyal1a70cef2015-04-22 11:29:51 -070035import android.view.View.OnClickListener;
Sunny Goyalfa401a12015-04-10 13:45:42 -070036import android.view.ViewGroup;
Sunny Goyale78e3d72015-09-24 11:23:31 -070037import android.view.accessibility.AccessibilityEvent;
Sunny Goyalfa401a12015-04-10 13:45:42 -070038import android.view.animation.DecelerateInterpolator;
39import android.view.animation.LinearInterpolator;
Adam Cohend4d7aa52011-07-19 21:47:37 -070040import android.widget.TextView;
Winson Chung61fa4192011-06-12 15:15:29 -070041
Vadim Tryshevfedca432015-08-19 17:55:02 -070042import com.android.launcher3.dragndrop.DragController;
43import com.android.launcher3.dragndrop.DragLayer;
44import 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
Winson Chung61fa4192011-06-12 15:15:29 -070055 protected Launcher mLauncher;
Winson Chunga62e9fd2011-07-11 15:20:48 -070056 private int mBottomDragPadding;
Tony Wickhamb54c4a32015-09-11 08:40:20 -070057 protected BaseDropTargetBar mDropTargetBar;
Winson Chung61fa4192011-06-12 15:15:29 -070058
59 /** Whether this drop target is active for the current drag */
60 protected boolean mActive;
61
62 /** The paint applied to the drag view on hover */
Winson Chung61967cb2012-02-28 18:11:33 -080063 protected int mHoverColor = 0;
Winson Chung61fa4192011-06-12 15:15:29 -070064
Sunny Goyalfa401a12015-04-10 13:45:42 -070065 protected ColorStateList mOriginalTextColor;
Sunny Goyal3a644ed2015-05-21 10:28:02 -070066 protected Drawable mDrawable;
Sunny Goyalfa401a12015-04-10 13:45:42 -070067
Tony Wickham9aae47f2015-10-01 13:04:22 -070068 protected DeviceProfile mDeviceProfile;
69
Sunny Goyal3a644ed2015-05-21 10:28:02 -070070 private AnimatorSet mCurrentColorAnim;
Sunny Goyal316490e2015-06-02 09:38:28 -070071 @Thunk ColorMatrix mSrcFilter, mDstFilter, mCurrentFilter;
Sunny Goyal3a644ed2015-05-21 10:28:02 -070072
Sunny Goyalfe0d1f22015-04-28 22:01:31 -070073
Winson Chung61fa4192011-06-12 15:15:29 -070074 public ButtonDropTarget(Context context, AttributeSet attrs) {
75 this(context, attrs, 0);
76 }
77
78 public ButtonDropTarget(Context context, AttributeSet attrs, int defStyle) {
79 super(context, attrs, defStyle);
Sunny Goyalfe0d1f22015-04-28 22:01:31 -070080 mBottomDragPadding = getResources().getDimensionPixelSize(R.dimen.drop_target_drag_padding);
Winson Chung61fa4192011-06-12 15:15:29 -070081 }
82
Sunny Goyalfa401a12015-04-10 13:45:42 -070083 @Override
84 protected void onFinishInflate() {
85 super.onFinishInflate();
86 mOriginalTextColor = getTextColors();
87
88 // Remove the text in the Phone UI in landscape
Tony Wickham9aae47f2015-10-01 13:04:22 -070089 mDeviceProfile = ((Launcher) getContext()).getDeviceProfile();
90 if (mDeviceProfile.isVerticalBarLayout()) {
Winson Chung7501adf2015-06-02 11:24:28 -070091 setText("");
Sunny Goyalfa401a12015-04-10 13:45:42 -070092 }
Winson Chung61fa4192011-06-12 15:15:29 -070093 }
94
Sunny Goyal70660032015-05-14 00:07:08 -070095 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Sunny Goyalfa401a12015-04-10 13:45:42 -070096 protected void setDrawable(int resId) {
Sunny Goyale0cab302015-05-20 16:40:30 -070097 // We do not set the drawable in the xml as that inflates two drawables corresponding to
98 // drawableLeft and drawableStart.
Sunny Goyal3a644ed2015-05-21 10:28:02 -070099 mDrawable = getResources().getDrawable(resId);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700100
Sunny Goyal9fc953b2015-08-17 12:24:25 -0700101 if (Utilities.ATLEAST_JB_MR1) {
Sunny Goyale0cab302015-05-20 16:40:30 -0700102 setCompoundDrawablesRelativeWithIntrinsicBounds(mDrawable, null, null, null);
103 } else {
104 setCompoundDrawablesWithIntrinsicBounds(mDrawable, null, null, null);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700105 }
106 }
107
108 public void setLauncher(Launcher launcher) {
109 mLauncher = launcher;
Winson Chung61fa4192011-06-12 15:15:29 -0700110 }
111
Tony Wickhamb54c4a32015-09-11 08:40:20 -0700112 public void setDropTargetBar(BaseDropTargetBar dropTargetBar) {
113 mDropTargetBar = dropTargetBar;
Adam Cohend4d7aa52011-07-19 21:47:37 -0700114 }
115
Sunny Goyalfa401a12015-04-10 13:45:42 -0700116 @Override
Sunny Goyalddec7342015-04-29 18:12:37 -0700117 public void onFlingToDelete(DragObject d, PointF vec) { }
Winson Chung61fa4192011-06-12 15:15:29 -0700118
Sunny Goyalfa401a12015-04-10 13:45:42 -0700119 @Override
120 public final void onDragEnter(DragObject d) {
Winson Chung61967cb2012-02-28 18:11:33 -0800121 d.dragView.setColor(mHoverColor);
Sunny Goyal9fc953b2015-08-17 12:24:25 -0700122 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700123 animateTextColor(mHoverColor);
124 } else {
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700125 if (mCurrentFilter == null) {
126 mCurrentFilter = new ColorMatrix();
127 }
128 DragView.setColorScale(mHoverColor, mCurrentFilter);
129 mDrawable.setColorFilter(new ColorMatrixColorFilter(mCurrentFilter));
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700130 setTextColor(mHoverColor);
131 }
Sunny Goyald21301e2015-09-25 12:17:08 -0700132 if (d.stateAnnouncer != null) {
133 d.stateAnnouncer.cancel();
134 }
Sunny Goyale78e3d72015-09-24 11:23:31 -0700135 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Winson Chung61fa4192011-06-12 15:15:29 -0700136 }
137
Sunny Goyalfa401a12015-04-10 13:45:42 -0700138 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700139 public void onDragOver(DragObject d) {
140 // Do nothing
141 }
142
Sunny Goyal9fc953b2015-08-17 12:24:25 -0700143 protected void resetHoverColor() {
144 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700145 animateTextColor(mOriginalTextColor.getDefaultColor());
146 } else {
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700147 mDrawable.setColorFilter(null);
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700148 setTextColor(mOriginalTextColor);
149 }
150 }
151
152 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
153 private void animateTextColor(int targetColor) {
154 if (mCurrentColorAnim != null) {
155 mCurrentColorAnim.cancel();
156 }
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700157
158 mCurrentColorAnim = new AnimatorSet();
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700159 mCurrentColorAnim.setDuration(DragView.COLOR_CHANGE_DURATION);
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700160
161 if (mSrcFilter == null) {
162 mSrcFilter = new ColorMatrix();
163 mDstFilter = new ColorMatrix();
164 mCurrentFilter = new ColorMatrix();
165 }
166
167 DragView.setColorScale(getTextColor(), mSrcFilter);
168 DragView.setColorScale(targetColor, mDstFilter);
169 ValueAnimator anim1 = ValueAnimator.ofObject(
170 new FloatArrayEvaluator(mCurrentFilter.getArray()),
171 mSrcFilter.getArray(), mDstFilter.getArray());
172 anim1.addUpdateListener(new AnimatorUpdateListener() {
173
174 @Override
175 public void onAnimationUpdate(ValueAnimator animation) {
176 mDrawable.setColorFilter(new ColorMatrixColorFilter(mCurrentFilter));
177 invalidate();
178 }
179 });
180
181 mCurrentColorAnim.play(anim1);
182 mCurrentColorAnim.play(ObjectAnimator.ofArgb(this, "textColor", targetColor));
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700183 mCurrentColorAnim.start();
Winson Chung61fa4192011-06-12 15:15:29 -0700184 }
185
Sunny Goyalfa401a12015-04-10 13:45:42 -0700186 @Override
187 public final void onDragExit(DragObject d) {
188 if (!d.dragComplete) {
189 d.dragView.setColor(0);
190 resetHoverColor();
191 } else {
192 // Restore the hover color
193 d.dragView.setColor(mHoverColor);
194 }
Winson Chung61fa4192011-06-12 15:15:29 -0700195 }
196
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700197 @Override
Tony Wickham9aae47f2015-10-01 13:04:22 -0700198 public void onDragStart(DragSource source, ItemInfo info, int dragAction) {
Sunny Goyalfa401a12015-04-10 13:45:42 -0700199 mActive = supportsDrop(source, info);
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700200 mDrawable.setColorFilter(null);
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700201 if (mCurrentColorAnim != null) {
202 mCurrentColorAnim.cancel();
203 mCurrentColorAnim = null;
204 }
Sunny Goyalfa401a12015-04-10 13:45:42 -0700205 setTextColor(mOriginalTextColor);
206 ((ViewGroup) getParent()).setVisibility(mActive ? View.VISIBLE : View.GONE);
207 }
208
209 @Override
210 public final boolean acceptDrop(DragObject dragObject) {
211 return supportsDrop(dragObject.dragSource, dragObject.dragInfo);
212 }
213
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700214 protected abstract boolean supportsDrop(DragSource source, ItemInfo info);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700215
216 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700217 public boolean isDropEnabled() {
218 return mActive;
219 }
220
Sunny Goyalfa401a12015-04-10 13:45:42 -0700221 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700222 public void onDragEnd() {
Sunny Goyalfa401a12015-04-10 13:45:42 -0700223 mActive = false;
Winson Chung61fa4192011-06-12 15:15:29 -0700224 }
225
Sunny Goyalfa401a12015-04-10 13:45:42 -0700226 /**
227 * On drop animate the dropView to the icon.
228 */
229 @Override
230 public void onDrop(final DragObject d) {
231 final DragLayer dragLayer = mLauncher.getDragLayer();
232 final Rect from = new Rect();
233 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
234
235 int width = mDrawable.getIntrinsicWidth();
236 int height = mDrawable.getIntrinsicHeight();
237 final Rect to = getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(),
238 width, height);
239 final float scale = (float) to.width() / from.width();
Tony Wickhamb54c4a32015-09-11 08:40:20 -0700240 mDropTargetBar.deferOnDragEnd();
Sunny Goyalfa401a12015-04-10 13:45:42 -0700241
242 Runnable onAnimationEndRunnable = new Runnable() {
243 @Override
244 public void run() {
245 completeDrop(d);
Tony Wickhamb54c4a32015-09-11 08:40:20 -0700246 mDropTargetBar.onDragEnd();
Sunny Goyalfa401a12015-04-10 13:45:42 -0700247 mLauncher.exitSpringLoadedDragModeDelayed(true, 0, null);
248 }
249 };
250 dragLayer.animateView(d.dragView, from, to, scale, 1f, 1f, 0.1f, 0.1f,
251 DRAG_VIEW_DROP_DURATION, new DecelerateInterpolator(2),
252 new LinearInterpolator(), onAnimationEndRunnable,
253 DragLayer.ANIMATION_END_DISAPPEAR, null);
254 }
255
Sunny Goyale9b651e2015-04-24 11:44:51 -0700256 @Override
257 public void prepareAccessibilityDrop() { }
258
Sunny Goyalfa401a12015-04-10 13:45:42 -0700259 @Thunk abstract void completeDrop(DragObject d);
260
Winson Chung61fa4192011-06-12 15:15:29 -0700261 @Override
Adam Cohen7d30a372013-07-01 17:03:59 -0700262 public void getHitRectRelativeToDragLayer(android.graphics.Rect outRect) {
Winson Chunga62e9fd2011-07-11 15:20:48 -0700263 super.getHitRect(outRect);
264 outRect.bottom += mBottomDragPadding;
Winson Chung156ab5b2013-07-12 14:14:16 -0700265
266 int[] coords = new int[2];
267 mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(this, coords);
268 outRect.offsetTo(coords[0], coords[1]);
Winson Chunga62e9fd2011-07-11 15:20:48 -0700269 }
270
Sunny Goyalfa401a12015-04-10 13:45:42 -0700271 protected Rect getIconRect(int viewWidth, int viewHeight, int drawableWidth, int drawableHeight) {
Winson Chung61967cb2012-02-28 18:11:33 -0800272 DragLayer dragLayer = mLauncher.getDragLayer();
273
274 // Find the rect to animate to (the view is center aligned)
275 Rect to = new Rect();
276 dragLayer.getViewRectRelativeToSelf(this, to);
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800277
278 final int width = drawableWidth;
279 final int height = drawableHeight;
280
281 final int left;
282 final int right;
283
Sunny Goyal70660032015-05-14 00:07:08 -0700284 if (Utilities.isRtl(getResources())) {
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800285 right = to.right - getPaddingRight();
286 left = right - width;
287 } else {
288 left = to.left + getPaddingLeft();
289 right = left + width;
290 }
291
292 final int top = to.top + (getMeasuredHeight() - height) / 2;
293 final int bottom = top + height;
294
295 to.set(left, top, right, bottom);
Winson Chung61967cb2012-02-28 18:11:33 -0800296
297 // Center the destination rect about the trash icon
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800298 final int xOffset = (int) -(viewWidth - width) / 2;
299 final int yOffset = (int) -(viewHeight - height) / 2;
Winson Chung61967cb2012-02-28 18:11:33 -0800300 to.offset(xOffset, yOffset);
301
302 return to;
303 }
304
Sunny Goyalfa401a12015-04-10 13:45:42 -0700305 @Override
Adam Cohen8dfcba42011-07-07 16:38:18 -0700306 public void getLocationInDragLayer(int[] loc) {
307 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
308 }
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700309
310 public void enableAccessibleDrag(boolean enable) {
311 setOnClickListener(enable ? this : null);
312 }
313
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700314 @Override
315 public void onClick(View v) {
316 LauncherAppState.getInstance().getAccessibilityDelegate()
Sunny Goyal7aaa8412015-09-25 12:06:13 -0700317 .handleAccessibleDrop(this, null, null);
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700318 }
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700319
320 public int getTextColor() {
321 return getTextColors().getDefaultColor();
322 }
Winson Chung61fa4192011-06-12 15:15:29 -0700323}