blob: dc29f7d1e6527d3cb92b1e03cef68b75fb354287 [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;
37import 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;
43import com.android.launcher3.dragndrop.DragView;
Sunny Goyalfa401a12015-04-10 13:45:42 -070044import com.android.launcher3.util.Thunk;
Winson Chung61fa4192011-06-12 15:15:29 -070045
46/**
47 * Implements a DropTarget.
48 */
Sunny Goyal1a70cef2015-04-22 11:29:51 -070049public abstract class ButtonDropTarget extends TextView
50 implements DropTarget, DragController.DragListener, OnClickListener {
Sunny Goyalfa401a12015-04-10 13:45:42 -070051
52 private static int DRAG_VIEW_DROP_DURATION = 285;
Winson Chung61fa4192011-06-12 15:15:29 -070053
Winson Chung61fa4192011-06-12 15:15:29 -070054 protected Launcher mLauncher;
Winson Chunga62e9fd2011-07-11 15:20:48 -070055 private int mBottomDragPadding;
Adam Cohend4d7aa52011-07-19 21:47:37 -070056 protected SearchDropTargetBar mSearchDropTargetBar;
Winson Chung61fa4192011-06-12 15:15:29 -070057
58 /** Whether this drop target is active for the current drag */
59 protected boolean mActive;
60
61 /** The paint applied to the drag view on hover */
Winson Chung61967cb2012-02-28 18:11:33 -080062 protected int mHoverColor = 0;
Winson Chung61fa4192011-06-12 15:15:29 -070063
Sunny Goyalfa401a12015-04-10 13:45:42 -070064 protected ColorStateList mOriginalTextColor;
Sunny Goyal3a644ed2015-05-21 10:28:02 -070065 protected Drawable mDrawable;
Sunny Goyalfa401a12015-04-10 13:45:42 -070066
Sunny Goyal3a644ed2015-05-21 10:28:02 -070067 private AnimatorSet mCurrentColorAnim;
Sunny Goyal316490e2015-06-02 09:38:28 -070068 @Thunk ColorMatrix mSrcFilter, mDstFilter, mCurrentFilter;
Sunny Goyal3a644ed2015-05-21 10:28:02 -070069
Sunny Goyalfe0d1f22015-04-28 22:01:31 -070070
Winson Chung61fa4192011-06-12 15:15:29 -070071 public ButtonDropTarget(Context context, AttributeSet attrs) {
72 this(context, attrs, 0);
73 }
74
75 public ButtonDropTarget(Context context, AttributeSet attrs, int defStyle) {
76 super(context, attrs, defStyle);
Sunny Goyalfe0d1f22015-04-28 22:01:31 -070077 mBottomDragPadding = getResources().getDimensionPixelSize(R.dimen.drop_target_drag_padding);
Winson Chung61fa4192011-06-12 15:15:29 -070078 }
79
Sunny Goyalfa401a12015-04-10 13:45:42 -070080 @Override
81 protected void onFinishInflate() {
82 super.onFinishInflate();
83 mOriginalTextColor = getTextColors();
84
85 // Remove the text in the Phone UI in landscape
Winson Chung7501adf2015-06-02 11:24:28 -070086 DeviceProfile grid = ((Launcher) getContext()).getDeviceProfile();
87 if (grid.isVerticalBarLayout()) {
88 setText("");
Sunny Goyalfa401a12015-04-10 13:45:42 -070089 }
Winson Chung61fa4192011-06-12 15:15:29 -070090 }
91
Sunny Goyal70660032015-05-14 00:07:08 -070092 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Sunny Goyalfa401a12015-04-10 13:45:42 -070093 protected void setDrawable(int resId) {
Sunny Goyale0cab302015-05-20 16:40:30 -070094 // We do not set the drawable in the xml as that inflates two drawables corresponding to
95 // drawableLeft and drawableStart.
Sunny Goyal3a644ed2015-05-21 10:28:02 -070096 mDrawable = getResources().getDrawable(resId);
Sunny Goyalfa401a12015-04-10 13:45:42 -070097
Sunny Goyal9fc953b2015-08-17 12:24:25 -070098 if (Utilities.ATLEAST_JB_MR1) {
Sunny Goyale0cab302015-05-20 16:40:30 -070099 setCompoundDrawablesRelativeWithIntrinsicBounds(mDrawable, null, null, null);
100 } else {
101 setCompoundDrawablesWithIntrinsicBounds(mDrawable, null, null, null);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700102 }
103 }
104
105 public void setLauncher(Launcher launcher) {
106 mLauncher = launcher;
Winson Chung61fa4192011-06-12 15:15:29 -0700107 }
108
Adam Cohend4d7aa52011-07-19 21:47:37 -0700109 public void setSearchDropTargetBar(SearchDropTargetBar searchDropTargetBar) {
110 mSearchDropTargetBar = searchDropTargetBar;
111 }
112
Sunny Goyalfa401a12015-04-10 13:45:42 -0700113 @Override
Sunny Goyalddec7342015-04-29 18:12:37 -0700114 public void onFlingToDelete(DragObject d, PointF vec) { }
Winson Chung61fa4192011-06-12 15:15:29 -0700115
Sunny Goyalfa401a12015-04-10 13:45:42 -0700116 @Override
117 public final void onDragEnter(DragObject d) {
Winson Chung61967cb2012-02-28 18:11:33 -0800118 d.dragView.setColor(mHoverColor);
Sunny Goyal9fc953b2015-08-17 12:24:25 -0700119 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700120 animateTextColor(mHoverColor);
121 } else {
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700122 if (mCurrentFilter == null) {
123 mCurrentFilter = new ColorMatrix();
124 }
125 DragView.setColorScale(mHoverColor, mCurrentFilter);
126 mDrawable.setColorFilter(new ColorMatrixColorFilter(mCurrentFilter));
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700127 setTextColor(mHoverColor);
128 }
Winson Chung61fa4192011-06-12 15:15:29 -0700129 }
130
Sunny Goyalfa401a12015-04-10 13:45:42 -0700131 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700132 public void onDragOver(DragObject d) {
133 // Do nothing
134 }
135
Sunny Goyal9fc953b2015-08-17 12:24:25 -0700136 protected void resetHoverColor() {
137 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700138 animateTextColor(mOriginalTextColor.getDefaultColor());
139 } else {
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700140 mDrawable.setColorFilter(null);
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700141 setTextColor(mOriginalTextColor);
142 }
143 }
144
145 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
146 private void animateTextColor(int targetColor) {
147 if (mCurrentColorAnim != null) {
148 mCurrentColorAnim.cancel();
149 }
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700150
151 mCurrentColorAnim = new AnimatorSet();
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700152 mCurrentColorAnim.setDuration(DragView.COLOR_CHANGE_DURATION);
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700153
154 if (mSrcFilter == null) {
155 mSrcFilter = new ColorMatrix();
156 mDstFilter = new ColorMatrix();
157 mCurrentFilter = new ColorMatrix();
158 }
159
160 DragView.setColorScale(getTextColor(), mSrcFilter);
161 DragView.setColorScale(targetColor, mDstFilter);
162 ValueAnimator anim1 = ValueAnimator.ofObject(
163 new FloatArrayEvaluator(mCurrentFilter.getArray()),
164 mSrcFilter.getArray(), mDstFilter.getArray());
165 anim1.addUpdateListener(new AnimatorUpdateListener() {
166
167 @Override
168 public void onAnimationUpdate(ValueAnimator animation) {
169 mDrawable.setColorFilter(new ColorMatrixColorFilter(mCurrentFilter));
170 invalidate();
171 }
172 });
173
174 mCurrentColorAnim.play(anim1);
175 mCurrentColorAnim.play(ObjectAnimator.ofArgb(this, "textColor", targetColor));
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700176 mCurrentColorAnim.start();
Winson Chung61fa4192011-06-12 15:15:29 -0700177 }
178
Sunny Goyalfa401a12015-04-10 13:45:42 -0700179 @Override
180 public final void onDragExit(DragObject d) {
181 if (!d.dragComplete) {
182 d.dragView.setColor(0);
183 resetHoverColor();
184 } else {
185 // Restore the hover color
186 d.dragView.setColor(mHoverColor);
187 }
Winson Chung61fa4192011-06-12 15:15:29 -0700188 }
189
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700190 @Override
191 public final void onDragStart(DragSource source, ItemInfo info, int dragAction) {
Sunny Goyalfa401a12015-04-10 13:45:42 -0700192 mActive = supportsDrop(source, info);
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700193 mDrawable.setColorFilter(null);
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700194 if (mCurrentColorAnim != null) {
195 mCurrentColorAnim.cancel();
196 mCurrentColorAnim = null;
197 }
Sunny Goyalfa401a12015-04-10 13:45:42 -0700198 setTextColor(mOriginalTextColor);
199 ((ViewGroup) getParent()).setVisibility(mActive ? View.VISIBLE : View.GONE);
200 }
201
202 @Override
203 public final boolean acceptDrop(DragObject dragObject) {
204 return supportsDrop(dragObject.dragSource, dragObject.dragInfo);
205 }
206
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700207 protected abstract boolean supportsDrop(DragSource source, ItemInfo info);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700208
209 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700210 public boolean isDropEnabled() {
211 return mActive;
212 }
213
Sunny Goyalfa401a12015-04-10 13:45:42 -0700214 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700215 public void onDragEnd() {
Sunny Goyalfa401a12015-04-10 13:45:42 -0700216 mActive = false;
Winson Chung61fa4192011-06-12 15:15:29 -0700217 }
218
Sunny Goyalfa401a12015-04-10 13:45:42 -0700219 /**
220 * On drop animate the dropView to the icon.
221 */
222 @Override
223 public void onDrop(final DragObject d) {
224 final DragLayer dragLayer = mLauncher.getDragLayer();
225 final Rect from = new Rect();
226 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
227
228 int width = mDrawable.getIntrinsicWidth();
229 int height = mDrawable.getIntrinsicHeight();
230 final Rect to = getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(),
231 width, height);
232 final float scale = (float) to.width() / from.width();
233 mSearchDropTargetBar.deferOnDragEnd();
234
235 Runnable onAnimationEndRunnable = new Runnable() {
236 @Override
237 public void run() {
238 completeDrop(d);
239 mSearchDropTargetBar.onDragEnd();
240 mLauncher.exitSpringLoadedDragModeDelayed(true, 0, null);
241 }
242 };
243 dragLayer.animateView(d.dragView, from, to, scale, 1f, 1f, 0.1f, 0.1f,
244 DRAG_VIEW_DROP_DURATION, new DecelerateInterpolator(2),
245 new LinearInterpolator(), onAnimationEndRunnable,
246 DragLayer.ANIMATION_END_DISAPPEAR, null);
247 }
248
Sunny Goyale9b651e2015-04-24 11:44:51 -0700249 @Override
250 public void prepareAccessibilityDrop() { }
251
Sunny Goyalfa401a12015-04-10 13:45:42 -0700252 @Thunk abstract void completeDrop(DragObject d);
253
Winson Chung61fa4192011-06-12 15:15:29 -0700254 @Override
Adam Cohen7d30a372013-07-01 17:03:59 -0700255 public void getHitRectRelativeToDragLayer(android.graphics.Rect outRect) {
Winson Chunga62e9fd2011-07-11 15:20:48 -0700256 super.getHitRect(outRect);
257 outRect.bottom += mBottomDragPadding;
Winson Chung156ab5b2013-07-12 14:14:16 -0700258
259 int[] coords = new int[2];
260 mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(this, coords);
261 outRect.offsetTo(coords[0], coords[1]);
Winson Chunga62e9fd2011-07-11 15:20:48 -0700262 }
263
Sunny Goyalfa401a12015-04-10 13:45:42 -0700264 protected Rect getIconRect(int viewWidth, int viewHeight, int drawableWidth, int drawableHeight) {
Winson Chung61967cb2012-02-28 18:11:33 -0800265 DragLayer dragLayer = mLauncher.getDragLayer();
266
267 // Find the rect to animate to (the view is center aligned)
268 Rect to = new Rect();
269 dragLayer.getViewRectRelativeToSelf(this, to);
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800270
271 final int width = drawableWidth;
272 final int height = drawableHeight;
273
274 final int left;
275 final int right;
276
Sunny Goyal70660032015-05-14 00:07:08 -0700277 if (Utilities.isRtl(getResources())) {
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800278 right = to.right - getPaddingRight();
279 left = right - width;
280 } else {
281 left = to.left + getPaddingLeft();
282 right = left + width;
283 }
284
285 final int top = to.top + (getMeasuredHeight() - height) / 2;
286 final int bottom = top + height;
287
288 to.set(left, top, right, bottom);
Winson Chung61967cb2012-02-28 18:11:33 -0800289
290 // Center the destination rect about the trash icon
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800291 final int xOffset = (int) -(viewWidth - width) / 2;
292 final int yOffset = (int) -(viewHeight - height) / 2;
Winson Chung61967cb2012-02-28 18:11:33 -0800293 to.offset(xOffset, yOffset);
294
295 return to;
296 }
297
Sunny Goyalfa401a12015-04-10 13:45:42 -0700298 @Override
Adam Cohen8dfcba42011-07-07 16:38:18 -0700299 public void getLocationInDragLayer(int[] loc) {
300 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
301 }
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700302
303 public void enableAccessibleDrag(boolean enable) {
304 setOnClickListener(enable ? this : null);
305 }
306
307 protected String getAccessibilityDropConfirmation() {
308 return null;
309 }
310
311 @Override
312 public void onClick(View v) {
313 LauncherAppState.getInstance().getAccessibilityDelegate()
314 .handleAccessibleDrop(this, null, getAccessibilityDropConfirmation());
315 }
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700316
317 public int getTextColor() {
318 return getTextColors().getDefaultColor();
319 }
Winson Chung61fa4192011-06-12 15:15:29 -0700320}