blob: e5bfe19c1cb00ba183b7daf8311e4a8b8452cfe9 [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
Sunny Goyalfa401a12015-04-10 13:45:42 -070042import com.android.launcher3.util.Thunk;
Winson Chung61fa4192011-06-12 15:15:29 -070043
44/**
45 * Implements a DropTarget.
46 */
Sunny Goyal1a70cef2015-04-22 11:29:51 -070047public abstract class ButtonDropTarget extends TextView
48 implements DropTarget, DragController.DragListener, OnClickListener {
Sunny Goyalfa401a12015-04-10 13:45:42 -070049
50 private static int DRAG_VIEW_DROP_DURATION = 285;
Winson Chung61fa4192011-06-12 15:15:29 -070051
Winson Chung61fa4192011-06-12 15:15:29 -070052 protected Launcher mLauncher;
Winson Chunga62e9fd2011-07-11 15:20:48 -070053 private int mBottomDragPadding;
Adam Cohend4d7aa52011-07-19 21:47:37 -070054 protected SearchDropTargetBar mSearchDropTargetBar;
Winson Chung61fa4192011-06-12 15:15:29 -070055
56 /** Whether this drop target is active for the current drag */
57 protected boolean mActive;
58
59 /** The paint applied to the drag view on hover */
Winson Chung61967cb2012-02-28 18:11:33 -080060 protected int mHoverColor = 0;
Winson Chung61fa4192011-06-12 15:15:29 -070061
Sunny Goyalfa401a12015-04-10 13:45:42 -070062 protected ColorStateList mOriginalTextColor;
Sunny Goyal3a644ed2015-05-21 10:28:02 -070063 protected Drawable mDrawable;
Sunny Goyalfa401a12015-04-10 13:45:42 -070064
Sunny Goyal3a644ed2015-05-21 10:28:02 -070065 private AnimatorSet mCurrentColorAnim;
Sunny Goyal316490e2015-06-02 09:38:28 -070066 @Thunk ColorMatrix mSrcFilter, mDstFilter, mCurrentFilter;
Sunny Goyal3a644ed2015-05-21 10:28:02 -070067
Sunny Goyalfe0d1f22015-04-28 22:01:31 -070068
Winson Chung61fa4192011-06-12 15:15:29 -070069 public ButtonDropTarget(Context context, AttributeSet attrs) {
70 this(context, attrs, 0);
71 }
72
73 public ButtonDropTarget(Context context, AttributeSet attrs, int defStyle) {
74 super(context, attrs, defStyle);
Sunny Goyalfe0d1f22015-04-28 22:01:31 -070075 mBottomDragPadding = getResources().getDimensionPixelSize(R.dimen.drop_target_drag_padding);
Winson Chung61fa4192011-06-12 15:15:29 -070076 }
77
Sunny Goyalfa401a12015-04-10 13:45:42 -070078 @Override
79 protected void onFinishInflate() {
80 super.onFinishInflate();
81 mOriginalTextColor = getTextColors();
82
83 // Remove the text in the Phone UI in landscape
Winson Chung7501adf2015-06-02 11:24:28 -070084 DeviceProfile grid = ((Launcher) getContext()).getDeviceProfile();
85 if (grid.isVerticalBarLayout()) {
86 setText("");
Sunny Goyalfa401a12015-04-10 13:45:42 -070087 }
Winson Chung61fa4192011-06-12 15:15:29 -070088 }
89
Sunny Goyal70660032015-05-14 00:07:08 -070090 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Sunny Goyalfa401a12015-04-10 13:45:42 -070091 protected void setDrawable(int resId) {
Sunny Goyale0cab302015-05-20 16:40:30 -070092 // We do not set the drawable in the xml as that inflates two drawables corresponding to
93 // drawableLeft and drawableStart.
Sunny Goyal3a644ed2015-05-21 10:28:02 -070094 mDrawable = getResources().getDrawable(resId);
Sunny Goyalfa401a12015-04-10 13:45:42 -070095
Sunny Goyal9fc953b2015-08-17 12:24:25 -070096 if (Utilities.ATLEAST_JB_MR1) {
Sunny Goyale0cab302015-05-20 16:40:30 -070097 setCompoundDrawablesRelativeWithIntrinsicBounds(mDrawable, null, null, null);
98 } else {
99 setCompoundDrawablesWithIntrinsicBounds(mDrawable, null, null, null);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700100 }
101 }
102
103 public void setLauncher(Launcher launcher) {
104 mLauncher = launcher;
Winson Chung61fa4192011-06-12 15:15:29 -0700105 }
106
Adam Cohend4d7aa52011-07-19 21:47:37 -0700107 public void setSearchDropTargetBar(SearchDropTargetBar searchDropTargetBar) {
108 mSearchDropTargetBar = searchDropTargetBar;
109 }
110
Sunny Goyalfa401a12015-04-10 13:45:42 -0700111 @Override
Sunny Goyalddec7342015-04-29 18:12:37 -0700112 public void onFlingToDelete(DragObject d, PointF vec) { }
Winson Chung61fa4192011-06-12 15:15:29 -0700113
Sunny Goyalfa401a12015-04-10 13:45:42 -0700114 @Override
115 public final void onDragEnter(DragObject d) {
Winson Chung61967cb2012-02-28 18:11:33 -0800116 d.dragView.setColor(mHoverColor);
Sunny Goyal9fc953b2015-08-17 12:24:25 -0700117 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700118 animateTextColor(mHoverColor);
119 } else {
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700120 if (mCurrentFilter == null) {
121 mCurrentFilter = new ColorMatrix();
122 }
123 DragView.setColorScale(mHoverColor, mCurrentFilter);
124 mDrawable.setColorFilter(new ColorMatrixColorFilter(mCurrentFilter));
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700125 setTextColor(mHoverColor);
126 }
Sunny Goyald21301e2015-09-25 12:17:08 -0700127 if (d.stateAnnouncer != null) {
128 d.stateAnnouncer.cancel();
129 }
Sunny Goyale78e3d72015-09-24 11:23:31 -0700130 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Winson Chung61fa4192011-06-12 15:15:29 -0700131 }
132
Sunny Goyalfa401a12015-04-10 13:45:42 -0700133 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700134 public void onDragOver(DragObject d) {
135 // Do nothing
136 }
137
Sunny Goyal9fc953b2015-08-17 12:24:25 -0700138 protected void resetHoverColor() {
139 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700140 animateTextColor(mOriginalTextColor.getDefaultColor());
141 } else {
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700142 mDrawable.setColorFilter(null);
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700143 setTextColor(mOriginalTextColor);
144 }
145 }
146
147 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
148 private void animateTextColor(int targetColor) {
149 if (mCurrentColorAnim != null) {
150 mCurrentColorAnim.cancel();
151 }
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700152
153 mCurrentColorAnim = new AnimatorSet();
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700154 mCurrentColorAnim.setDuration(DragView.COLOR_CHANGE_DURATION);
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700155
156 if (mSrcFilter == null) {
157 mSrcFilter = new ColorMatrix();
158 mDstFilter = new ColorMatrix();
159 mCurrentFilter = new ColorMatrix();
160 }
161
162 DragView.setColorScale(getTextColor(), mSrcFilter);
163 DragView.setColorScale(targetColor, mDstFilter);
164 ValueAnimator anim1 = ValueAnimator.ofObject(
165 new FloatArrayEvaluator(mCurrentFilter.getArray()),
166 mSrcFilter.getArray(), mDstFilter.getArray());
167 anim1.addUpdateListener(new AnimatorUpdateListener() {
168
169 @Override
170 public void onAnimationUpdate(ValueAnimator animation) {
171 mDrawable.setColorFilter(new ColorMatrixColorFilter(mCurrentFilter));
172 invalidate();
173 }
174 });
175
176 mCurrentColorAnim.play(anim1);
177 mCurrentColorAnim.play(ObjectAnimator.ofArgb(this, "textColor", targetColor));
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700178 mCurrentColorAnim.start();
Winson Chung61fa4192011-06-12 15:15:29 -0700179 }
180
Sunny Goyalfa401a12015-04-10 13:45:42 -0700181 @Override
182 public final void onDragExit(DragObject d) {
183 if (!d.dragComplete) {
184 d.dragView.setColor(0);
185 resetHoverColor();
186 } else {
187 // Restore the hover color
188 d.dragView.setColor(mHoverColor);
189 }
Winson Chung61fa4192011-06-12 15:15:29 -0700190 }
191
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700192 @Override
Sunny Goyalfa401a12015-04-10 13:45:42 -0700193 public final void onDragStart(DragSource source, Object info, int dragAction) {
194 mActive = supportsDrop(source, info);
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700195 mDrawable.setColorFilter(null);
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700196 if (mCurrentColorAnim != null) {
197 mCurrentColorAnim.cancel();
198 mCurrentColorAnim = null;
199 }
Sunny Goyalfa401a12015-04-10 13:45:42 -0700200 setTextColor(mOriginalTextColor);
201 ((ViewGroup) getParent()).setVisibility(mActive ? View.VISIBLE : View.GONE);
202 }
203
204 @Override
205 public final boolean acceptDrop(DragObject dragObject) {
206 return supportsDrop(dragObject.dragSource, dragObject.dragInfo);
207 }
208
209 protected abstract boolean supportsDrop(DragSource source, Object info);
210
211 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700212 public boolean isDropEnabled() {
213 return mActive;
214 }
215
Sunny Goyalfa401a12015-04-10 13:45:42 -0700216 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700217 public void onDragEnd() {
Sunny Goyalfa401a12015-04-10 13:45:42 -0700218 mActive = false;
Winson Chung61fa4192011-06-12 15:15:29 -0700219 }
220
Sunny Goyalfa401a12015-04-10 13:45:42 -0700221 /**
222 * On drop animate the dropView to the icon.
223 */
224 @Override
225 public void onDrop(final DragObject d) {
226 final DragLayer dragLayer = mLauncher.getDragLayer();
227 final Rect from = new Rect();
228 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
229
230 int width = mDrawable.getIntrinsicWidth();
231 int height = mDrawable.getIntrinsicHeight();
232 final Rect to = getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(),
233 width, height);
234 final float scale = (float) to.width() / from.width();
235 mSearchDropTargetBar.deferOnDragEnd();
236
237 Runnable onAnimationEndRunnable = new Runnable() {
238 @Override
239 public void run() {
240 completeDrop(d);
241 mSearchDropTargetBar.onDragEnd();
242 mLauncher.exitSpringLoadedDragModeDelayed(true, 0, null);
243 }
244 };
245 dragLayer.animateView(d.dragView, from, to, scale, 1f, 1f, 0.1f, 0.1f,
246 DRAG_VIEW_DROP_DURATION, new DecelerateInterpolator(2),
247 new LinearInterpolator(), onAnimationEndRunnable,
248 DragLayer.ANIMATION_END_DISAPPEAR, null);
249 }
250
Sunny Goyale9b651e2015-04-24 11:44:51 -0700251 @Override
252 public void prepareAccessibilityDrop() { }
253
Sunny Goyalfa401a12015-04-10 13:45:42 -0700254 @Thunk abstract void completeDrop(DragObject d);
255
Winson Chung61fa4192011-06-12 15:15:29 -0700256 @Override
Adam Cohen7d30a372013-07-01 17:03:59 -0700257 public void getHitRectRelativeToDragLayer(android.graphics.Rect outRect) {
Winson Chunga62e9fd2011-07-11 15:20:48 -0700258 super.getHitRect(outRect);
259 outRect.bottom += mBottomDragPadding;
Winson Chung156ab5b2013-07-12 14:14:16 -0700260
261 int[] coords = new int[2];
262 mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(this, coords);
263 outRect.offsetTo(coords[0], coords[1]);
Winson Chunga62e9fd2011-07-11 15:20:48 -0700264 }
265
Sunny Goyalfa401a12015-04-10 13:45:42 -0700266 protected Rect getIconRect(int viewWidth, int viewHeight, int drawableWidth, int drawableHeight) {
Winson Chung61967cb2012-02-28 18:11:33 -0800267 DragLayer dragLayer = mLauncher.getDragLayer();
268
269 // Find the rect to animate to (the view is center aligned)
270 Rect to = new Rect();
271 dragLayer.getViewRectRelativeToSelf(this, to);
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800272
273 final int width = drawableWidth;
274 final int height = drawableHeight;
275
276 final int left;
277 final int right;
278
Sunny Goyal70660032015-05-14 00:07:08 -0700279 if (Utilities.isRtl(getResources())) {
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800280 right = to.right - getPaddingRight();
281 left = right - width;
282 } else {
283 left = to.left + getPaddingLeft();
284 right = left + width;
285 }
286
287 final int top = to.top + (getMeasuredHeight() - height) / 2;
288 final int bottom = top + height;
289
290 to.set(left, top, right, bottom);
Winson Chung61967cb2012-02-28 18:11:33 -0800291
292 // Center the destination rect about the trash icon
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800293 final int xOffset = (int) -(viewWidth - width) / 2;
294 final int yOffset = (int) -(viewHeight - height) / 2;
Winson Chung61967cb2012-02-28 18:11:33 -0800295 to.offset(xOffset, yOffset);
296
297 return to;
298 }
299
Sunny Goyalfa401a12015-04-10 13:45:42 -0700300 @Override
Adam Cohen8dfcba42011-07-07 16:38:18 -0700301 public void getLocationInDragLayer(int[] loc) {
302 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
303 }
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700304
305 public void enableAccessibleDrag(boolean enable) {
306 setOnClickListener(enable ? this : null);
307 }
308
309 protected String getAccessibilityDropConfirmation() {
310 return null;
311 }
312
313 @Override
314 public void onClick(View v) {
315 LauncherAppState.getInstance().getAccessibilityDelegate()
316 .handleAccessibleDrop(this, null, getAccessibilityDropConfirmation());
317 }
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}