blob: fb49df5dff3ffd816bb007f8af68cd1c0357d343 [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
19import android.content.Context;
Sunny Goyalfa401a12015-04-10 13:45:42 -070020import android.content.res.ColorStateList;
21import android.content.res.Configuration;
Winson Chunga62e9fd2011-07-11 15:20:48 -070022import android.content.res.Resources;
Winson Chung043f2af2012-03-01 16:09:54 -080023import android.graphics.PointF;
Winson Chung61967cb2012-02-28 18:11:33 -080024import android.graphics.Rect;
Winson Chung947245b2012-05-15 16:34:19 -070025import android.graphics.drawable.Drawable;
Sunny Goyalfa401a12015-04-10 13:45:42 -070026import android.graphics.drawable.TransitionDrawable;
Winson Chung61fa4192011-06-12 15:15:29 -070027import android.util.AttributeSet;
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -080028import android.view.View;
Sunny Goyal1a70cef2015-04-22 11:29:51 -070029import android.view.View.OnClickListener;
Sunny Goyalfa401a12015-04-10 13:45:42 -070030import android.view.ViewGroup;
31import android.view.animation.DecelerateInterpolator;
32import android.view.animation.LinearInterpolator;
Adam Cohend4d7aa52011-07-19 21:47:37 -070033import android.widget.TextView;
Winson Chung61fa4192011-06-12 15:15:29 -070034
Sunny Goyalfa401a12015-04-10 13:45:42 -070035import com.android.launcher3.util.Thunk;
Winson Chung61fa4192011-06-12 15:15:29 -070036
37/**
38 * Implements a DropTarget.
39 */
Sunny Goyal1a70cef2015-04-22 11:29:51 -070040public abstract class ButtonDropTarget extends TextView
41 implements DropTarget, DragController.DragListener, OnClickListener {
Sunny Goyalfa401a12015-04-10 13:45:42 -070042
43 private static int DRAG_VIEW_DROP_DURATION = 285;
Winson Chung61fa4192011-06-12 15:15:29 -070044
45 protected final int mTransitionDuration;
46
47 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
Winson Chung61fa4192011-06-12 15:15:29 -070061 public ButtonDropTarget(Context context, AttributeSet attrs) {
62 this(context, attrs, 0);
63 }
64
65 public ButtonDropTarget(Context context, AttributeSet attrs, int defStyle) {
66 super(context, attrs, defStyle);
67
Winson Chunga62e9fd2011-07-11 15:20:48 -070068 Resources r = getResources();
69 mTransitionDuration = r.getInteger(R.integer.config_dropTargetBgTransitionDuration);
70 mBottomDragPadding = r.getDimensionPixelSize(R.dimen.drop_target_drag_padding);
Winson Chung61fa4192011-06-12 15:15:29 -070071 }
72
Sunny Goyalfa401a12015-04-10 13:45:42 -070073 @Override
74 protected void onFinishInflate() {
75 super.onFinishInflate();
76 mOriginalTextColor = getTextColors();
77
78 // Remove the text in the Phone UI in landscape
79 int orientation = getResources().getConfiguration().orientation;
80 if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
81 if (!LauncherAppState.getInstance().isScreenLarge()) {
82 setText("");
83 }
84 }
Winson Chung61fa4192011-06-12 15:15:29 -070085 }
86
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);
94 setCompoundDrawablesRelativeWithIntrinsicBounds(mDrawable, null, null, null);
95 }
96
97 if (null != mDrawable) {
98 mDrawable.setCrossFadeEnabled(true);
99 }
100 }
101
102 public void setLauncher(Launcher launcher) {
103 mLauncher = launcher;
Winson Chung61fa4192011-06-12 15:15:29 -0700104 }
105
Adam Cohend4d7aa52011-07-19 21:47:37 -0700106 public void setSearchDropTargetBar(SearchDropTargetBar searchDropTargetBar) {
107 mSearchDropTargetBar = searchDropTargetBar;
108 }
109
Winson Chung947245b2012-05-15 16:34:19 -0700110 protected Drawable getCurrentDrawable() {
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800111 Drawable[] drawables = getCompoundDrawablesRelative();
Winson Chung947245b2012-05-15 16:34:19 -0700112 for (int i = 0; i < drawables.length; ++i) {
113 if (drawables[i] != null) {
114 return drawables[i];
115 }
116 }
117 return null;
118 }
119
Sunny Goyalfa401a12015-04-10 13:45:42 -0700120 @Override
121 public void onFlingToDelete(DragObject d, int x, int y, PointF vec) { }
Winson Chung61fa4192011-06-12 15:15:29 -0700122
Sunny Goyalfa401a12015-04-10 13:45:42 -0700123 @Override
124 public final void onDragEnter(DragObject d) {
Winson Chung61967cb2012-02-28 18:11:33 -0800125 d.dragView.setColor(mHoverColor);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700126 mDrawable.startTransition(mTransitionDuration);
127 setTextColor(mHoverColor);
Winson Chung61fa4192011-06-12 15:15:29 -0700128 }
129
Sunny Goyalfa401a12015-04-10 13:45:42 -0700130 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700131 public void onDragOver(DragObject d) {
132 // Do nothing
133 }
134
Sunny Goyalfa401a12015-04-10 13:45:42 -0700135 protected void resetHoverColor() {
136 mDrawable.resetTransition();
137 setTextColor(mOriginalTextColor);
Winson Chung61fa4192011-06-12 15:15:29 -0700138 }
139
Sunny Goyalfa401a12015-04-10 13:45:42 -0700140 @Override
141 public final void onDragExit(DragObject d) {
142 if (!d.dragComplete) {
143 d.dragView.setColor(0);
144 resetHoverColor();
145 } else {
146 // Restore the hover color
147 d.dragView.setColor(mHoverColor);
148 }
Winson Chung61fa4192011-06-12 15:15:29 -0700149 }
150
Sunny Goyalfa401a12015-04-10 13:45:42 -0700151 @Override
152 public final void onDragStart(DragSource source, Object info, int dragAction) {
153 mActive = supportsDrop(source, info);
154 mDrawable.resetTransition();
155 setTextColor(mOriginalTextColor);
156 ((ViewGroup) getParent()).setVisibility(mActive ? View.VISIBLE : View.GONE);
157 }
158
159 @Override
160 public final boolean acceptDrop(DragObject dragObject) {
161 return supportsDrop(dragObject.dragSource, dragObject.dragInfo);
162 }
163
164 protected abstract boolean supportsDrop(DragSource source, Object info);
165
166 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700167 public boolean isDropEnabled() {
168 return mActive;
169 }
170
Sunny Goyalfa401a12015-04-10 13:45:42 -0700171 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700172 public void onDragEnd() {
Sunny Goyalfa401a12015-04-10 13:45:42 -0700173 mActive = false;
Winson Chung61fa4192011-06-12 15:15:29 -0700174 }
175
Sunny Goyalfa401a12015-04-10 13:45:42 -0700176 /**
177 * On drop animate the dropView to the icon.
178 */
179 @Override
180 public void onDrop(final DragObject d) {
181 final DragLayer dragLayer = mLauncher.getDragLayer();
182 final Rect from = new Rect();
183 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
184
185 int width = mDrawable.getIntrinsicWidth();
186 int height = mDrawable.getIntrinsicHeight();
187 final Rect to = getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(),
188 width, height);
189 final float scale = (float) to.width() / from.width();
190 mSearchDropTargetBar.deferOnDragEnd();
191
192 Runnable onAnimationEndRunnable = new Runnable() {
193 @Override
194 public void run() {
195 completeDrop(d);
196 mSearchDropTargetBar.onDragEnd();
197 mLauncher.exitSpringLoadedDragModeDelayed(true, 0, null);
198 }
199 };
200 dragLayer.animateView(d.dragView, from, to, scale, 1f, 1f, 0.1f, 0.1f,
201 DRAG_VIEW_DROP_DURATION, new DecelerateInterpolator(2),
202 new LinearInterpolator(), onAnimationEndRunnable,
203 DragLayer.ANIMATION_END_DISAPPEAR, null);
204 }
205
206 @Thunk abstract void completeDrop(DragObject d);
207
Winson Chung61fa4192011-06-12 15:15:29 -0700208 @Override
Adam Cohen7d30a372013-07-01 17:03:59 -0700209 public void getHitRectRelativeToDragLayer(android.graphics.Rect outRect) {
Winson Chunga62e9fd2011-07-11 15:20:48 -0700210 super.getHitRect(outRect);
211 outRect.bottom += mBottomDragPadding;
Winson Chung156ab5b2013-07-12 14:14:16 -0700212
213 int[] coords = new int[2];
214 mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(this, coords);
215 outRect.offsetTo(coords[0], coords[1]);
Winson Chunga62e9fd2011-07-11 15:20:48 -0700216 }
217
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800218 private boolean isRtl() {
Sunny Goyalfa401a12015-04-10 13:45:42 -0700219 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800220 }
221
Sunny Goyalfa401a12015-04-10 13:45:42 -0700222 protected Rect getIconRect(int viewWidth, int viewHeight, int drawableWidth, int drawableHeight) {
Winson Chung61967cb2012-02-28 18:11:33 -0800223 DragLayer dragLayer = mLauncher.getDragLayer();
224
225 // Find the rect to animate to (the view is center aligned)
226 Rect to = new Rect();
227 dragLayer.getViewRectRelativeToSelf(this, to);
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800228
229 final int width = drawableWidth;
230 final int height = drawableHeight;
231
232 final int left;
233 final int right;
234
235 if (isRtl()) {
236 right = to.right - getPaddingRight();
237 left = right - width;
238 } else {
239 left = to.left + getPaddingLeft();
240 right = left + width;
241 }
242
243 final int top = to.top + (getMeasuredHeight() - height) / 2;
244 final int bottom = top + height;
245
246 to.set(left, top, right, bottom);
Winson Chung61967cb2012-02-28 18:11:33 -0800247
248 // Center the destination rect about the trash icon
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800249 final int xOffset = (int) -(viewWidth - width) / 2;
250 final int yOffset = (int) -(viewHeight - height) / 2;
Winson Chung61967cb2012-02-28 18:11:33 -0800251 to.offset(xOffset, yOffset);
252
253 return to;
254 }
255
Sunny Goyalfa401a12015-04-10 13:45:42 -0700256 @Override
Adam Cohen8dfcba42011-07-07 16:38:18 -0700257 public void getLocationInDragLayer(int[] loc) {
258 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
259 }
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700260
261 public void enableAccessibleDrag(boolean enable) {
262 setOnClickListener(enable ? this : null);
263 }
264
265 protected String getAccessibilityDropConfirmation() {
266 return null;
267 }
268
269 @Override
270 public void onClick(View v) {
271 LauncherAppState.getInstance().getAccessibilityDelegate()
272 .handleAccessibleDrop(this, null, getAccessibilityDropConfirmation());
273 }
Winson Chung61fa4192011-06-12 15:15:29 -0700274}