blob: 80b542f1d355c70ad15fefd0661febc307642654 [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;
Sunny Goyalfa401a12015-04-10 13:45:42 -070026import android.graphics.drawable.TransitionDrawable;
Sunny Goyalfe0d1f22015-04-28 22:01:31 -070027import android.os.Build;
Winson Chung61fa4192011-06-12 15:15:29 -070028import android.util.AttributeSet;
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -080029import android.view.View;
Sunny Goyal1a70cef2015-04-22 11:29:51 -070030import android.view.View.OnClickListener;
Sunny Goyalfa401a12015-04-10 13:45:42 -070031import android.view.ViewGroup;
32import android.view.animation.DecelerateInterpolator;
33import android.view.animation.LinearInterpolator;
Adam Cohend4d7aa52011-07-19 21:47:37 -070034import android.widget.TextView;
Winson Chung61fa4192011-06-12 15:15:29 -070035
Sunny Goyalfa401a12015-04-10 13:45:42 -070036import com.android.launcher3.util.Thunk;
Winson Chung61fa4192011-06-12 15:15:29 -070037
38/**
39 * Implements a DropTarget.
40 */
Sunny Goyal1a70cef2015-04-22 11:29:51 -070041public abstract class ButtonDropTarget extends TextView
42 implements DropTarget, DragController.DragListener, OnClickListener {
Sunny Goyalfa401a12015-04-10 13:45:42 -070043
44 private static int DRAG_VIEW_DROP_DURATION = 285;
Winson Chung61fa4192011-06-12 15:15:29 -070045
Winson Chung61fa4192011-06-12 15:15:29 -070046 protected Launcher mLauncher;
Winson Chunga62e9fd2011-07-11 15:20:48 -070047 private int mBottomDragPadding;
Adam Cohend4d7aa52011-07-19 21:47:37 -070048 protected TextView mText;
49 protected SearchDropTargetBar mSearchDropTargetBar;
Winson Chung61fa4192011-06-12 15:15:29 -070050
51 /** Whether this drop target is active for the current drag */
52 protected boolean mActive;
53
54 /** The paint applied to the drag view on hover */
Winson Chung61967cb2012-02-28 18:11:33 -080055 protected int mHoverColor = 0;
Winson Chung61fa4192011-06-12 15:15:29 -070056
Sunny Goyalfa401a12015-04-10 13:45:42 -070057 protected ColorStateList mOriginalTextColor;
58 protected TransitionDrawable mDrawable;
59
Sunny Goyalfe0d1f22015-04-28 22:01:31 -070060 private ObjectAnimator mCurrentColorAnim;
61
Winson Chung61fa4192011-06-12 15:15:29 -070062 public ButtonDropTarget(Context context, AttributeSet attrs) {
63 this(context, attrs, 0);
64 }
65
66 public ButtonDropTarget(Context context, AttributeSet attrs, int defStyle) {
67 super(context, attrs, defStyle);
Sunny Goyalfe0d1f22015-04-28 22:01:31 -070068 mBottomDragPadding = getResources().getDimensionPixelSize(R.dimen.drop_target_drag_padding);
Winson Chung61fa4192011-06-12 15:15:29 -070069 }
70
Sunny Goyalfa401a12015-04-10 13:45:42 -070071 @Override
72 protected void onFinishInflate() {
73 super.onFinishInflate();
74 mOriginalTextColor = getTextColors();
75
76 // Remove the text in the Phone UI in landscape
77 int orientation = getResources().getConfiguration().orientation;
78 if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
79 if (!LauncherAppState.getInstance().isScreenLarge()) {
80 setText("");
81 }
82 }
Winson Chung61fa4192011-06-12 15:15:29 -070083 }
84
Sunny Goyal70660032015-05-14 00:07:08 -070085 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Sunny Goyalfa401a12015-04-10 13:45:42 -070086 protected void setDrawable(int resId) {
Sunny Goyale0cab302015-05-20 16:40:30 -070087 // We do not set the drawable in the xml as that inflates two drawables corresponding to
88 // drawableLeft and drawableStart.
89 mDrawable = (TransitionDrawable) getResources().getDrawable(resId);
90 mDrawable.setCrossFadeEnabled(true);
Sunny Goyalfa401a12015-04-10 13:45:42 -070091
Sunny Goyale0cab302015-05-20 16:40:30 -070092 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
93 setCompoundDrawablesRelativeWithIntrinsicBounds(mDrawable, null, null, null);
94 } else {
95 setCompoundDrawablesWithIntrinsicBounds(mDrawable, null, null, null);
Sunny Goyalfa401a12015-04-10 13:45:42 -070096 }
97 }
98
99 public void setLauncher(Launcher launcher) {
100 mLauncher = launcher;
Winson Chung61fa4192011-06-12 15:15:29 -0700101 }
102
Adam Cohend4d7aa52011-07-19 21:47:37 -0700103 public void setSearchDropTargetBar(SearchDropTargetBar searchDropTargetBar) {
104 mSearchDropTargetBar = searchDropTargetBar;
105 }
106
Sunny Goyalfa401a12015-04-10 13:45:42 -0700107 @Override
Sunny Goyalddec7342015-04-29 18:12:37 -0700108 public void onFlingToDelete(DragObject d, PointF vec) { }
Winson Chung61fa4192011-06-12 15:15:29 -0700109
Sunny Goyalfa401a12015-04-10 13:45:42 -0700110 @Override
111 public final void onDragEnter(DragObject d) {
Winson Chung61967cb2012-02-28 18:11:33 -0800112 d.dragView.setColor(mHoverColor);
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700113 if (Utilities.isLmpOrAbove()) {
114 mDrawable.startTransition(DragView.COLOR_CHANGE_DURATION);
115 animateTextColor(mHoverColor);
116 } else {
117 mDrawable.startTransition(0);
118 setTextColor(mHoverColor);
119 }
Winson Chung61fa4192011-06-12 15:15:29 -0700120 }
121
Sunny Goyalfa401a12015-04-10 13:45:42 -0700122 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700123 public void onDragOver(DragObject d) {
124 // Do nothing
125 }
126
Sunny Goyalfa401a12015-04-10 13:45:42 -0700127 protected void resetHoverColor() {
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700128 if (Utilities.isLmpOrAbove()) {
129 mDrawable.reverseTransition(DragView.COLOR_CHANGE_DURATION);
130 animateTextColor(mOriginalTextColor.getDefaultColor());
131 } else {
132 mDrawable.resetTransition();
133 setTextColor(mOriginalTextColor);
134 }
135 }
136
137 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
138 private void animateTextColor(int targetColor) {
139 if (mCurrentColorAnim != null) {
140 mCurrentColorAnim.cancel();
141 }
142 mCurrentColorAnim = ObjectAnimator.ofArgb(this, "textColor", targetColor);
143 mCurrentColorAnim.setDuration(DragView.COLOR_CHANGE_DURATION);
144 mCurrentColorAnim.start();
Winson Chung61fa4192011-06-12 15:15:29 -0700145 }
146
Sunny Goyalfa401a12015-04-10 13:45:42 -0700147 @Override
148 public final void onDragExit(DragObject d) {
149 if (!d.dragComplete) {
150 d.dragView.setColor(0);
151 resetHoverColor();
152 } else {
153 // Restore the hover color
154 d.dragView.setColor(mHoverColor);
155 }
Winson Chung61fa4192011-06-12 15:15:29 -0700156 }
157
Sunny Goyalfa401a12015-04-10 13:45:42 -0700158 @Override
159 public final void onDragStart(DragSource source, Object info, int dragAction) {
160 mActive = supportsDrop(source, info);
161 mDrawable.resetTransition();
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700162 if (mCurrentColorAnim != null) {
163 mCurrentColorAnim.cancel();
164 mCurrentColorAnim = null;
165 }
Sunny Goyalfa401a12015-04-10 13:45:42 -0700166 setTextColor(mOriginalTextColor);
167 ((ViewGroup) getParent()).setVisibility(mActive ? View.VISIBLE : View.GONE);
168 }
169
170 @Override
171 public final boolean acceptDrop(DragObject dragObject) {
172 return supportsDrop(dragObject.dragSource, dragObject.dragInfo);
173 }
174
175 protected abstract boolean supportsDrop(DragSource source, Object info);
176
177 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700178 public boolean isDropEnabled() {
179 return mActive;
180 }
181
Sunny Goyalfa401a12015-04-10 13:45:42 -0700182 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700183 public void onDragEnd() {
Sunny Goyalfa401a12015-04-10 13:45:42 -0700184 mActive = false;
Winson Chung61fa4192011-06-12 15:15:29 -0700185 }
186
Sunny Goyalfa401a12015-04-10 13:45:42 -0700187 /**
188 * On drop animate the dropView to the icon.
189 */
190 @Override
191 public void onDrop(final DragObject d) {
192 final DragLayer dragLayer = mLauncher.getDragLayer();
193 final Rect from = new Rect();
194 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
195
196 int width = mDrawable.getIntrinsicWidth();
197 int height = mDrawable.getIntrinsicHeight();
198 final Rect to = getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(),
199 width, height);
200 final float scale = (float) to.width() / from.width();
201 mSearchDropTargetBar.deferOnDragEnd();
202
203 Runnable onAnimationEndRunnable = new Runnable() {
204 @Override
205 public void run() {
206 completeDrop(d);
207 mSearchDropTargetBar.onDragEnd();
208 mLauncher.exitSpringLoadedDragModeDelayed(true, 0, null);
209 }
210 };
211 dragLayer.animateView(d.dragView, from, to, scale, 1f, 1f, 0.1f, 0.1f,
212 DRAG_VIEW_DROP_DURATION, new DecelerateInterpolator(2),
213 new LinearInterpolator(), onAnimationEndRunnable,
214 DragLayer.ANIMATION_END_DISAPPEAR, null);
215 }
216
Sunny Goyale9b651e2015-04-24 11:44:51 -0700217 @Override
218 public void prepareAccessibilityDrop() { }
219
Sunny Goyalfa401a12015-04-10 13:45:42 -0700220 @Thunk abstract void completeDrop(DragObject d);
221
Winson Chung61fa4192011-06-12 15:15:29 -0700222 @Override
Adam Cohen7d30a372013-07-01 17:03:59 -0700223 public void getHitRectRelativeToDragLayer(android.graphics.Rect outRect) {
Winson Chunga62e9fd2011-07-11 15:20:48 -0700224 super.getHitRect(outRect);
225 outRect.bottom += mBottomDragPadding;
Winson Chung156ab5b2013-07-12 14:14:16 -0700226
227 int[] coords = new int[2];
228 mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(this, coords);
229 outRect.offsetTo(coords[0], coords[1]);
Winson Chunga62e9fd2011-07-11 15:20:48 -0700230 }
231
Sunny Goyalfa401a12015-04-10 13:45:42 -0700232 protected Rect getIconRect(int viewWidth, int viewHeight, int drawableWidth, int drawableHeight) {
Winson Chung61967cb2012-02-28 18:11:33 -0800233 DragLayer dragLayer = mLauncher.getDragLayer();
234
235 // Find the rect to animate to (the view is center aligned)
236 Rect to = new Rect();
237 dragLayer.getViewRectRelativeToSelf(this, to);
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800238
239 final int width = drawableWidth;
240 final int height = drawableHeight;
241
242 final int left;
243 final int right;
244
Sunny Goyal70660032015-05-14 00:07:08 -0700245 if (Utilities.isRtl(getResources())) {
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800246 right = to.right - getPaddingRight();
247 left = right - width;
248 } else {
249 left = to.left + getPaddingLeft();
250 right = left + width;
251 }
252
253 final int top = to.top + (getMeasuredHeight() - height) / 2;
254 final int bottom = top + height;
255
256 to.set(left, top, right, bottom);
Winson Chung61967cb2012-02-28 18:11:33 -0800257
258 // Center the destination rect about the trash icon
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800259 final int xOffset = (int) -(viewWidth - width) / 2;
260 final int yOffset = (int) -(viewHeight - height) / 2;
Winson Chung61967cb2012-02-28 18:11:33 -0800261 to.offset(xOffset, yOffset);
262
263 return to;
264 }
265
Sunny Goyalfa401a12015-04-10 13:45:42 -0700266 @Override
Adam Cohen8dfcba42011-07-07 16:38:18 -0700267 public void getLocationInDragLayer(int[] loc) {
268 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
269 }
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700270
271 public void enableAccessibleDrag(boolean enable) {
272 setOnClickListener(enable ? this : null);
273 }
274
275 protected String getAccessibilityDropConfirmation() {
276 return null;
277 }
278
279 @Override
280 public void onClick(View v) {
281 LauncherAppState.getInstance().getAccessibilityDelegate()
282 .handleAccessibleDrop(this, null, getAccessibilityDropConfirmation());
283 }
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700284
285 public int getTextColor() {
286 return getTextColors().getDefaultColor();
287 }
Winson Chung61fa4192011-06-12 15:15:29 -0700288}