blob: b8214d1ef5f1f1e6ca42b6181eefeda2db525814 [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 Goyalfe0d1f22015-04-28 22:01:31 -070019import android.animation.ObjectAnimator;
20import android.annotation.TargetApi;
Winson Chung61fa4192011-06-12 15:15:29 -070021import android.content.Context;
Sunny Goyalfa401a12015-04-10 13:45:42 -070022import android.content.res.ColorStateList;
23import android.content.res.Configuration;
Winson Chung043f2af2012-03-01 16:09:54 -080024import android.graphics.PointF;
Winson Chung61967cb2012-02-28 18:11:33 -080025import android.graphics.Rect;
Winson Chung947245b2012-05-15 16:34:19 -070026import android.graphics.drawable.Drawable;
Sunny Goyalfa401a12015-04-10 13:45:42 -070027import android.graphics.drawable.TransitionDrawable;
Sunny Goyalfe0d1f22015-04-28 22:01:31 -070028import android.os.Build;
Winson Chung61fa4192011-06-12 15:15:29 -070029import android.util.AttributeSet;
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -080030import android.view.View;
Sunny Goyal1a70cef2015-04-22 11:29:51 -070031import android.view.View.OnClickListener;
Sunny Goyalfa401a12015-04-10 13:45:42 -070032import android.view.ViewGroup;
33import android.view.animation.DecelerateInterpolator;
34import android.view.animation.LinearInterpolator;
Adam Cohend4d7aa52011-07-19 21:47:37 -070035import android.widget.TextView;
Winson Chung61fa4192011-06-12 15:15:29 -070036
Sunny Goyalfa401a12015-04-10 13:45:42 -070037import com.android.launcher3.util.Thunk;
Winson Chung61fa4192011-06-12 15:15:29 -070038
39/**
40 * Implements a DropTarget.
41 */
Sunny Goyal1a70cef2015-04-22 11:29:51 -070042public abstract class ButtonDropTarget extends TextView
43 implements DropTarget, DragController.DragListener, OnClickListener {
Sunny Goyalfa401a12015-04-10 13:45:42 -070044
45 private static int DRAG_VIEW_DROP_DURATION = 285;
Winson Chung61fa4192011-06-12 15:15:29 -070046
Winson Chung61fa4192011-06-12 15:15:29 -070047 protected Launcher mLauncher;
Winson Chunga62e9fd2011-07-11 15:20:48 -070048 private int mBottomDragPadding;
Adam Cohend4d7aa52011-07-19 21:47:37 -070049 protected TextView mText;
50 protected SearchDropTargetBar mSearchDropTargetBar;
Winson Chung61fa4192011-06-12 15:15:29 -070051
52 /** Whether this drop target is active for the current drag */
53 protected boolean mActive;
54
55 /** The paint applied to the drag view on hover */
Winson Chung61967cb2012-02-28 18:11:33 -080056 protected int mHoverColor = 0;
Winson Chung61fa4192011-06-12 15:15:29 -070057
Sunny Goyalfa401a12015-04-10 13:45:42 -070058 protected ColorStateList mOriginalTextColor;
59 protected TransitionDrawable mDrawable;
60
Sunny Goyalfe0d1f22015-04-28 22:01:31 -070061 private ObjectAnimator mCurrentColorAnim;
62
Winson Chung61fa4192011-06-12 15:15:29 -070063 public ButtonDropTarget(Context context, AttributeSet attrs) {
64 this(context, attrs, 0);
65 }
66
67 public ButtonDropTarget(Context context, AttributeSet attrs, int defStyle) {
68 super(context, attrs, defStyle);
Sunny Goyalfe0d1f22015-04-28 22:01:31 -070069 mBottomDragPadding = getResources().getDimensionPixelSize(R.dimen.drop_target_drag_padding);
Winson Chung61fa4192011-06-12 15:15:29 -070070 }
71
Sunny Goyalfa401a12015-04-10 13:45:42 -070072 @Override
73 protected void onFinishInflate() {
74 super.onFinishInflate();
75 mOriginalTextColor = getTextColors();
76
77 // Remove the text in the Phone UI in landscape
78 int orientation = getResources().getConfiguration().orientation;
79 if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
80 if (!LauncherAppState.getInstance().isScreenLarge()) {
81 setText("");
82 }
83 }
Winson Chung61fa4192011-06-12 15:15:29 -070084 }
85
Sunny Goyal70660032015-05-14 00:07:08 -070086 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Sunny Goyalfa401a12015-04-10 13:45:42 -070087 protected void setDrawable(int resId) {
88 // Get the hover color
89 mDrawable = (TransitionDrawable) getCurrentDrawable();
90
91 if (mDrawable == null) {
92 // TODO: investigate why this is ever happening. Presently only on one known device.
93 mDrawable = (TransitionDrawable) getResources().getDrawable(resId);
Sunny Goyal70660032015-05-14 00:07:08 -070094 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
95 setCompoundDrawablesRelativeWithIntrinsicBounds(mDrawable, null, null, null);
96 } else {
97 setCompoundDrawablesWithIntrinsicBounds(mDrawable, null, null, null);
98 }
Sunny Goyalfa401a12015-04-10 13:45:42 -070099 }
100
101 if (null != mDrawable) {
102 mDrawable.setCrossFadeEnabled(true);
103 }
104 }
105
106 public void setLauncher(Launcher launcher) {
107 mLauncher = launcher;
Winson Chung61fa4192011-06-12 15:15:29 -0700108 }
109
Adam Cohend4d7aa52011-07-19 21:47:37 -0700110 public void setSearchDropTargetBar(SearchDropTargetBar searchDropTargetBar) {
111 mSearchDropTargetBar = searchDropTargetBar;
112 }
113
Winson Chung947245b2012-05-15 16:34:19 -0700114 protected Drawable getCurrentDrawable() {
Sunny Goyal70660032015-05-14 00:07:08 -0700115 Drawable[] drawables = getCompoundDrawables();
Winson Chung947245b2012-05-15 16:34:19 -0700116 for (int i = 0; i < drawables.length; ++i) {
117 if (drawables[i] != null) {
118 return drawables[i];
119 }
120 }
121 return null;
122 }
123
Sunny Goyalfa401a12015-04-10 13:45:42 -0700124 @Override
Sunny Goyalddec7342015-04-29 18:12:37 -0700125 public void onFlingToDelete(DragObject d, PointF vec) { }
Winson Chung61fa4192011-06-12 15:15:29 -0700126
Sunny Goyalfa401a12015-04-10 13:45:42 -0700127 @Override
128 public final void onDragEnter(DragObject d) {
Winson Chung61967cb2012-02-28 18:11:33 -0800129 d.dragView.setColor(mHoverColor);
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700130 if (Utilities.isLmpOrAbove()) {
131 mDrawable.startTransition(DragView.COLOR_CHANGE_DURATION);
132 animateTextColor(mHoverColor);
133 } else {
134 mDrawable.startTransition(0);
135 setTextColor(mHoverColor);
136 }
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 Goyalfa401a12015-04-10 13:45:42 -0700144 protected void resetHoverColor() {
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700145 if (Utilities.isLmpOrAbove()) {
146 mDrawable.reverseTransition(DragView.COLOR_CHANGE_DURATION);
147 animateTextColor(mOriginalTextColor.getDefaultColor());
148 } else {
149 mDrawable.resetTransition();
150 setTextColor(mOriginalTextColor);
151 }
152 }
153
154 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
155 private void animateTextColor(int targetColor) {
156 if (mCurrentColorAnim != null) {
157 mCurrentColorAnim.cancel();
158 }
159 mCurrentColorAnim = ObjectAnimator.ofArgb(this, "textColor", targetColor);
160 mCurrentColorAnim.setDuration(DragView.COLOR_CHANGE_DURATION);
161 mCurrentColorAnim.start();
Winson Chung61fa4192011-06-12 15:15:29 -0700162 }
163
Sunny Goyalfa401a12015-04-10 13:45:42 -0700164 @Override
165 public final void onDragExit(DragObject d) {
166 if (!d.dragComplete) {
167 d.dragView.setColor(0);
168 resetHoverColor();
169 } else {
170 // Restore the hover color
171 d.dragView.setColor(mHoverColor);
172 }
Winson Chung61fa4192011-06-12 15:15:29 -0700173 }
174
Sunny Goyalfa401a12015-04-10 13:45:42 -0700175 @Override
176 public final void onDragStart(DragSource source, Object info, int dragAction) {
177 mActive = supportsDrop(source, info);
178 mDrawable.resetTransition();
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700179 if (mCurrentColorAnim != null) {
180 mCurrentColorAnim.cancel();
181 mCurrentColorAnim = null;
182 }
Sunny Goyalfa401a12015-04-10 13:45:42 -0700183 setTextColor(mOriginalTextColor);
184 ((ViewGroup) getParent()).setVisibility(mActive ? View.VISIBLE : View.GONE);
185 }
186
187 @Override
188 public final boolean acceptDrop(DragObject dragObject) {
189 return supportsDrop(dragObject.dragSource, dragObject.dragInfo);
190 }
191
192 protected abstract boolean supportsDrop(DragSource source, Object info);
193
194 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700195 public boolean isDropEnabled() {
196 return mActive;
197 }
198
Sunny Goyalfa401a12015-04-10 13:45:42 -0700199 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700200 public void onDragEnd() {
Sunny Goyalfa401a12015-04-10 13:45:42 -0700201 mActive = false;
Winson Chung61fa4192011-06-12 15:15:29 -0700202 }
203
Sunny Goyalfa401a12015-04-10 13:45:42 -0700204 /**
205 * On drop animate the dropView to the icon.
206 */
207 @Override
208 public void onDrop(final DragObject d) {
209 final DragLayer dragLayer = mLauncher.getDragLayer();
210 final Rect from = new Rect();
211 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
212
213 int width = mDrawable.getIntrinsicWidth();
214 int height = mDrawable.getIntrinsicHeight();
215 final Rect to = getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(),
216 width, height);
217 final float scale = (float) to.width() / from.width();
218 mSearchDropTargetBar.deferOnDragEnd();
219
220 Runnable onAnimationEndRunnable = new Runnable() {
221 @Override
222 public void run() {
223 completeDrop(d);
224 mSearchDropTargetBar.onDragEnd();
225 mLauncher.exitSpringLoadedDragModeDelayed(true, 0, null);
226 }
227 };
228 dragLayer.animateView(d.dragView, from, to, scale, 1f, 1f, 0.1f, 0.1f,
229 DRAG_VIEW_DROP_DURATION, new DecelerateInterpolator(2),
230 new LinearInterpolator(), onAnimationEndRunnable,
231 DragLayer.ANIMATION_END_DISAPPEAR, null);
232 }
233
Sunny Goyale9b651e2015-04-24 11:44:51 -0700234 @Override
235 public void prepareAccessibilityDrop() { }
236
Sunny Goyalfa401a12015-04-10 13:45:42 -0700237 @Thunk abstract void completeDrop(DragObject d);
238
Winson Chung61fa4192011-06-12 15:15:29 -0700239 @Override
Adam Cohen7d30a372013-07-01 17:03:59 -0700240 public void getHitRectRelativeToDragLayer(android.graphics.Rect outRect) {
Winson Chunga62e9fd2011-07-11 15:20:48 -0700241 super.getHitRect(outRect);
242 outRect.bottom += mBottomDragPadding;
Winson Chung156ab5b2013-07-12 14:14:16 -0700243
244 int[] coords = new int[2];
245 mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(this, coords);
246 outRect.offsetTo(coords[0], coords[1]);
Winson Chunga62e9fd2011-07-11 15:20:48 -0700247 }
248
Sunny Goyalfa401a12015-04-10 13:45:42 -0700249 protected Rect getIconRect(int viewWidth, int viewHeight, int drawableWidth, int drawableHeight) {
Winson Chung61967cb2012-02-28 18:11:33 -0800250 DragLayer dragLayer = mLauncher.getDragLayer();
251
252 // Find the rect to animate to (the view is center aligned)
253 Rect to = new Rect();
254 dragLayer.getViewRectRelativeToSelf(this, to);
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800255
256 final int width = drawableWidth;
257 final int height = drawableHeight;
258
259 final int left;
260 final int right;
261
Sunny Goyal70660032015-05-14 00:07:08 -0700262 if (Utilities.isRtl(getResources())) {
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800263 right = to.right - getPaddingRight();
264 left = right - width;
265 } else {
266 left = to.left + getPaddingLeft();
267 right = left + width;
268 }
269
270 final int top = to.top + (getMeasuredHeight() - height) / 2;
271 final int bottom = top + height;
272
273 to.set(left, top, right, bottom);
Winson Chung61967cb2012-02-28 18:11:33 -0800274
275 // Center the destination rect about the trash icon
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800276 final int xOffset = (int) -(viewWidth - width) / 2;
277 final int yOffset = (int) -(viewHeight - height) / 2;
Winson Chung61967cb2012-02-28 18:11:33 -0800278 to.offset(xOffset, yOffset);
279
280 return to;
281 }
282
Sunny Goyalfa401a12015-04-10 13:45:42 -0700283 @Override
Adam Cohen8dfcba42011-07-07 16:38:18 -0700284 public void getLocationInDragLayer(int[] loc) {
285 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
286 }
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700287
288 public void enableAccessibleDrag(boolean enable) {
289 setOnClickListener(enable ? this : null);
290 }
291
292 protected String getAccessibilityDropConfirmation() {
293 return null;
294 }
295
296 @Override
297 public void onClick(View v) {
298 LauncherAppState.getInstance().getAccessibilityDelegate()
299 .handleAccessibleDrop(this, null, getAccessibilityDropConfirmation());
300 }
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700301
302 public int getTextColor() {
303 return getTextColors().getDefaultColor();
304 }
Winson Chung61fa4192011-06-12 15:15:29 -0700305}