blob: a19f00d1fac262011a7b1a68fc821c79e40e8cbd [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
Sunny Goyal3a644ed2015-05-21 10:28:02 -070068 private AnimatorSet mCurrentColorAnim;
Sunny Goyal316490e2015-06-02 09:38:28 -070069 @Thunk ColorMatrix mSrcFilter, mDstFilter, mCurrentFilter;
Sunny Goyal3a644ed2015-05-21 10:28:02 -070070
Sunny Goyalfe0d1f22015-04-28 22:01:31 -070071
Winson Chung61fa4192011-06-12 15:15:29 -070072 public ButtonDropTarget(Context context, AttributeSet attrs) {
73 this(context, attrs, 0);
74 }
75
76 public ButtonDropTarget(Context context, AttributeSet attrs, int defStyle) {
77 super(context, attrs, defStyle);
Sunny Goyalfe0d1f22015-04-28 22:01:31 -070078 mBottomDragPadding = getResources().getDimensionPixelSize(R.dimen.drop_target_drag_padding);
Winson Chung61fa4192011-06-12 15:15:29 -070079 }
80
Sunny Goyalfa401a12015-04-10 13:45:42 -070081 @Override
82 protected void onFinishInflate() {
83 super.onFinishInflate();
84 mOriginalTextColor = getTextColors();
85
86 // Remove the text in the Phone UI in landscape
Winson Chung7501adf2015-06-02 11:24:28 -070087 DeviceProfile grid = ((Launcher) getContext()).getDeviceProfile();
88 if (grid.isVerticalBarLayout()) {
89 setText("");
Sunny Goyalfa401a12015-04-10 13:45:42 -070090 }
Winson Chung61fa4192011-06-12 15:15:29 -070091 }
92
Sunny Goyal70660032015-05-14 00:07:08 -070093 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Sunny Goyalfa401a12015-04-10 13:45:42 -070094 protected void setDrawable(int resId) {
Sunny Goyale0cab302015-05-20 16:40:30 -070095 // We do not set the drawable in the xml as that inflates two drawables corresponding to
96 // drawableLeft and drawableStart.
Sunny Goyal3a644ed2015-05-21 10:28:02 -070097 mDrawable = getResources().getDrawable(resId);
Sunny Goyalfa401a12015-04-10 13:45:42 -070098
Sunny Goyal9fc953b2015-08-17 12:24:25 -070099 if (Utilities.ATLEAST_JB_MR1) {
Sunny Goyale0cab302015-05-20 16:40:30 -0700100 setCompoundDrawablesRelativeWithIntrinsicBounds(mDrawable, null, null, null);
101 } else {
102 setCompoundDrawablesWithIntrinsicBounds(mDrawable, null, null, null);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700103 }
104 }
105
106 public void setLauncher(Launcher launcher) {
107 mLauncher = launcher;
Winson Chung61fa4192011-06-12 15:15:29 -0700108 }
109
Tony Wickhamb54c4a32015-09-11 08:40:20 -0700110 public void setDropTargetBar(BaseDropTargetBar dropTargetBar) {
111 mDropTargetBar = dropTargetBar;
Adam Cohend4d7aa52011-07-19 21:47:37 -0700112 }
113
Sunny Goyalfa401a12015-04-10 13:45:42 -0700114 @Override
Sunny Goyalddec7342015-04-29 18:12:37 -0700115 public void onFlingToDelete(DragObject d, PointF vec) { }
Winson Chung61fa4192011-06-12 15:15:29 -0700116
Sunny Goyalfa401a12015-04-10 13:45:42 -0700117 @Override
118 public final void onDragEnter(DragObject d) {
Winson Chung61967cb2012-02-28 18:11:33 -0800119 d.dragView.setColor(mHoverColor);
Sunny Goyal9fc953b2015-08-17 12:24:25 -0700120 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700121 animateTextColor(mHoverColor);
122 } else {
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700123 if (mCurrentFilter == null) {
124 mCurrentFilter = new ColorMatrix();
125 }
126 DragView.setColorScale(mHoverColor, mCurrentFilter);
127 mDrawable.setColorFilter(new ColorMatrixColorFilter(mCurrentFilter));
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700128 setTextColor(mHoverColor);
129 }
Sunny Goyald21301e2015-09-25 12:17:08 -0700130 if (d.stateAnnouncer != null) {
131 d.stateAnnouncer.cancel();
132 }
Sunny Goyale78e3d72015-09-24 11:23:31 -0700133 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Winson Chung61fa4192011-06-12 15:15:29 -0700134 }
135
Sunny Goyalfa401a12015-04-10 13:45:42 -0700136 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700137 public void onDragOver(DragObject d) {
138 // Do nothing
139 }
140
Sunny Goyal9fc953b2015-08-17 12:24:25 -0700141 protected void resetHoverColor() {
142 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700143 animateTextColor(mOriginalTextColor.getDefaultColor());
144 } else {
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700145 mDrawable.setColorFilter(null);
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700146 setTextColor(mOriginalTextColor);
147 }
148 }
149
150 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
151 private void animateTextColor(int targetColor) {
152 if (mCurrentColorAnim != null) {
153 mCurrentColorAnim.cancel();
154 }
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700155
156 mCurrentColorAnim = new AnimatorSet();
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700157 mCurrentColorAnim.setDuration(DragView.COLOR_CHANGE_DURATION);
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700158
159 if (mSrcFilter == null) {
160 mSrcFilter = new ColorMatrix();
161 mDstFilter = new ColorMatrix();
162 mCurrentFilter = new ColorMatrix();
163 }
164
165 DragView.setColorScale(getTextColor(), mSrcFilter);
166 DragView.setColorScale(targetColor, mDstFilter);
167 ValueAnimator anim1 = ValueAnimator.ofObject(
168 new FloatArrayEvaluator(mCurrentFilter.getArray()),
169 mSrcFilter.getArray(), mDstFilter.getArray());
170 anim1.addUpdateListener(new AnimatorUpdateListener() {
171
172 @Override
173 public void onAnimationUpdate(ValueAnimator animation) {
174 mDrawable.setColorFilter(new ColorMatrixColorFilter(mCurrentFilter));
175 invalidate();
176 }
177 });
178
179 mCurrentColorAnim.play(anim1);
180 mCurrentColorAnim.play(ObjectAnimator.ofArgb(this, "textColor", targetColor));
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700181 mCurrentColorAnim.start();
Winson Chung61fa4192011-06-12 15:15:29 -0700182 }
183
Sunny Goyalfa401a12015-04-10 13:45:42 -0700184 @Override
185 public final void onDragExit(DragObject d) {
186 if (!d.dragComplete) {
187 d.dragView.setColor(0);
188 resetHoverColor();
189 } else {
190 // Restore the hover color
191 d.dragView.setColor(mHoverColor);
192 }
Winson Chung61fa4192011-06-12 15:15:29 -0700193 }
194
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700195 @Override
196 public final void onDragStart(DragSource source, ItemInfo info, int dragAction) {
Sunny Goyalfa401a12015-04-10 13:45:42 -0700197 mActive = supportsDrop(source, info);
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700198 mDrawable.setColorFilter(null);
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700199 if (mCurrentColorAnim != null) {
200 mCurrentColorAnim.cancel();
201 mCurrentColorAnim = null;
202 }
Sunny Goyalfa401a12015-04-10 13:45:42 -0700203 setTextColor(mOriginalTextColor);
204 ((ViewGroup) getParent()).setVisibility(mActive ? View.VISIBLE : View.GONE);
205 }
206
207 @Override
208 public final boolean acceptDrop(DragObject dragObject) {
209 return supportsDrop(dragObject.dragSource, dragObject.dragInfo);
210 }
211
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700212 protected abstract boolean supportsDrop(DragSource source, ItemInfo info);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700213
214 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700215 public boolean isDropEnabled() {
216 return mActive;
217 }
218
Sunny Goyalfa401a12015-04-10 13:45:42 -0700219 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700220 public void onDragEnd() {
Sunny Goyalfa401a12015-04-10 13:45:42 -0700221 mActive = false;
Winson Chung61fa4192011-06-12 15:15:29 -0700222 }
223
Sunny Goyalfa401a12015-04-10 13:45:42 -0700224 /**
225 * On drop animate the dropView to the icon.
226 */
227 @Override
228 public void onDrop(final DragObject d) {
229 final DragLayer dragLayer = mLauncher.getDragLayer();
230 final Rect from = new Rect();
231 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
232
233 int width = mDrawable.getIntrinsicWidth();
234 int height = mDrawable.getIntrinsicHeight();
235 final Rect to = getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(),
236 width, height);
237 final float scale = (float) to.width() / from.width();
Tony Wickhamb54c4a32015-09-11 08:40:20 -0700238 mDropTargetBar.deferOnDragEnd();
Sunny Goyalfa401a12015-04-10 13:45:42 -0700239
240 Runnable onAnimationEndRunnable = new Runnable() {
241 @Override
242 public void run() {
243 completeDrop(d);
Tony Wickhamb54c4a32015-09-11 08:40:20 -0700244 mDropTargetBar.onDragEnd();
Sunny Goyalfa401a12015-04-10 13:45:42 -0700245 mLauncher.exitSpringLoadedDragModeDelayed(true, 0, null);
246 }
247 };
248 dragLayer.animateView(d.dragView, from, to, scale, 1f, 1f, 0.1f, 0.1f,
249 DRAG_VIEW_DROP_DURATION, new DecelerateInterpolator(2),
250 new LinearInterpolator(), onAnimationEndRunnable,
251 DragLayer.ANIMATION_END_DISAPPEAR, null);
252 }
253
Sunny Goyale9b651e2015-04-24 11:44:51 -0700254 @Override
255 public void prepareAccessibilityDrop() { }
256
Sunny Goyalfa401a12015-04-10 13:45:42 -0700257 @Thunk abstract void completeDrop(DragObject d);
258
Winson Chung61fa4192011-06-12 15:15:29 -0700259 @Override
Adam Cohen7d30a372013-07-01 17:03:59 -0700260 public void getHitRectRelativeToDragLayer(android.graphics.Rect outRect) {
Winson Chunga62e9fd2011-07-11 15:20:48 -0700261 super.getHitRect(outRect);
262 outRect.bottom += mBottomDragPadding;
Winson Chung156ab5b2013-07-12 14:14:16 -0700263
264 int[] coords = new int[2];
265 mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(this, coords);
266 outRect.offsetTo(coords[0], coords[1]);
Winson Chunga62e9fd2011-07-11 15:20:48 -0700267 }
268
Sunny Goyalfa401a12015-04-10 13:45:42 -0700269 protected Rect getIconRect(int viewWidth, int viewHeight, int drawableWidth, int drawableHeight) {
Winson Chung61967cb2012-02-28 18:11:33 -0800270 DragLayer dragLayer = mLauncher.getDragLayer();
271
272 // Find the rect to animate to (the view is center aligned)
273 Rect to = new Rect();
274 dragLayer.getViewRectRelativeToSelf(this, to);
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800275
276 final int width = drawableWidth;
277 final int height = drawableHeight;
278
279 final int left;
280 final int right;
281
Sunny Goyal70660032015-05-14 00:07:08 -0700282 if (Utilities.isRtl(getResources())) {
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800283 right = to.right - getPaddingRight();
284 left = right - width;
285 } else {
286 left = to.left + getPaddingLeft();
287 right = left + width;
288 }
289
290 final int top = to.top + (getMeasuredHeight() - height) / 2;
291 final int bottom = top + height;
292
293 to.set(left, top, right, bottom);
Winson Chung61967cb2012-02-28 18:11:33 -0800294
295 // Center the destination rect about the trash icon
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800296 final int xOffset = (int) -(viewWidth - width) / 2;
297 final int yOffset = (int) -(viewHeight - height) / 2;
Winson Chung61967cb2012-02-28 18:11:33 -0800298 to.offset(xOffset, yOffset);
299
300 return to;
301 }
302
Sunny Goyalfa401a12015-04-10 13:45:42 -0700303 @Override
Adam Cohen8dfcba42011-07-07 16:38:18 -0700304 public void getLocationInDragLayer(int[] loc) {
305 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
306 }
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700307
308 public void enableAccessibleDrag(boolean enable) {
309 setOnClickListener(enable ? this : null);
310 }
311
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700312 @Override
313 public void onClick(View v) {
314 LauncherAppState.getInstance().getAccessibilityDelegate()
Sunny Goyal7aaa8412015-09-25 12:06:13 -0700315 .handleAccessibleDrop(this, null, null);
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700316 }
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700317
318 public int getTextColor() {
319 return getTextColors().getDefaultColor();
320 }
Winson Chung61fa4192011-06-12 15:15:29 -0700321}