Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 1 | /* |
| 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 Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 17 | package com.android.launcher3; |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 18 | |
Sunny Goyal | fe0d1f2 | 2015-04-28 22:01:31 -0700 | [diff] [blame] | 19 | import android.animation.ObjectAnimator; |
| 20 | import android.annotation.TargetApi; |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 21 | import android.content.Context; |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 22 | import android.content.res.ColorStateList; |
| 23 | import android.content.res.Configuration; |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 24 | import android.graphics.PointF; |
Winson Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame] | 25 | import android.graphics.Rect; |
Winson Chung | 947245b | 2012-05-15 16:34:19 -0700 | [diff] [blame] | 26 | import android.graphics.drawable.Drawable; |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 27 | import android.graphics.drawable.TransitionDrawable; |
Sunny Goyal | fe0d1f2 | 2015-04-28 22:01:31 -0700 | [diff] [blame] | 28 | import android.os.Build; |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 29 | import android.util.AttributeSet; |
Fabrice Di Meglio | d6a33c6 | 2013-02-06 15:40:46 -0800 | [diff] [blame] | 30 | import android.view.View; |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 31 | import android.view.View.OnClickListener; |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 32 | import android.view.ViewGroup; |
| 33 | import android.view.animation.DecelerateInterpolator; |
| 34 | import android.view.animation.LinearInterpolator; |
Adam Cohen | d4d7aa5 | 2011-07-19 21:47:37 -0700 | [diff] [blame] | 35 | import android.widget.TextView; |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 36 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 37 | import com.android.launcher3.util.Thunk; |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * Implements a DropTarget. |
| 41 | */ |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 42 | public abstract class ButtonDropTarget extends TextView |
| 43 | implements DropTarget, DragController.DragListener, OnClickListener { |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 44 | |
| 45 | private static int DRAG_VIEW_DROP_DURATION = 285; |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 46 | |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 47 | protected Launcher mLauncher; |
Winson Chung | a62e9fd | 2011-07-11 15:20:48 -0700 | [diff] [blame] | 48 | private int mBottomDragPadding; |
Adam Cohen | d4d7aa5 | 2011-07-19 21:47:37 -0700 | [diff] [blame] | 49 | protected TextView mText; |
| 50 | protected SearchDropTargetBar mSearchDropTargetBar; |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 51 | |
| 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 Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame] | 56 | protected int mHoverColor = 0; |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 57 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 58 | protected ColorStateList mOriginalTextColor; |
| 59 | protected TransitionDrawable mDrawable; |
| 60 | |
Sunny Goyal | fe0d1f2 | 2015-04-28 22:01:31 -0700 | [diff] [blame] | 61 | private ObjectAnimator mCurrentColorAnim; |
| 62 | |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 63 | 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 Goyal | fe0d1f2 | 2015-04-28 22:01:31 -0700 | [diff] [blame] | 69 | mBottomDragPadding = getResources().getDimensionPixelSize(R.dimen.drop_target_drag_padding); |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 72 | @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 Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame^] | 86 | @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 87 | 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 Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame^] | 94 | 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 Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | if (null != mDrawable) { |
| 102 | mDrawable.setCrossFadeEnabled(true); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | public void setLauncher(Launcher launcher) { |
| 107 | mLauncher = launcher; |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Adam Cohen | d4d7aa5 | 2011-07-19 21:47:37 -0700 | [diff] [blame] | 110 | public void setSearchDropTargetBar(SearchDropTargetBar searchDropTargetBar) { |
| 111 | mSearchDropTargetBar = searchDropTargetBar; |
| 112 | } |
| 113 | |
Winson Chung | 947245b | 2012-05-15 16:34:19 -0700 | [diff] [blame] | 114 | protected Drawable getCurrentDrawable() { |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame^] | 115 | Drawable[] drawables = getCompoundDrawables(); |
Winson Chung | 947245b | 2012-05-15 16:34:19 -0700 | [diff] [blame] | 116 | 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 Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 124 | @Override |
Sunny Goyal | ddec734 | 2015-04-29 18:12:37 -0700 | [diff] [blame] | 125 | public void onFlingToDelete(DragObject d, PointF vec) { } |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 126 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 127 | @Override |
| 128 | public final void onDragEnter(DragObject d) { |
Winson Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame] | 129 | d.dragView.setColor(mHoverColor); |
Sunny Goyal | fe0d1f2 | 2015-04-28 22:01:31 -0700 | [diff] [blame] | 130 | if (Utilities.isLmpOrAbove()) { |
| 131 | mDrawable.startTransition(DragView.COLOR_CHANGE_DURATION); |
| 132 | animateTextColor(mHoverColor); |
| 133 | } else { |
| 134 | mDrawable.startTransition(0); |
| 135 | setTextColor(mHoverColor); |
| 136 | } |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 139 | @Override |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 140 | public void onDragOver(DragObject d) { |
| 141 | // Do nothing |
| 142 | } |
| 143 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 144 | protected void resetHoverColor() { |
Sunny Goyal | fe0d1f2 | 2015-04-28 22:01:31 -0700 | [diff] [blame] | 145 | 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 Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 164 | @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 Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 175 | @Override |
| 176 | public final void onDragStart(DragSource source, Object info, int dragAction) { |
| 177 | mActive = supportsDrop(source, info); |
| 178 | mDrawable.resetTransition(); |
Sunny Goyal | fe0d1f2 | 2015-04-28 22:01:31 -0700 | [diff] [blame] | 179 | if (mCurrentColorAnim != null) { |
| 180 | mCurrentColorAnim.cancel(); |
| 181 | mCurrentColorAnim = null; |
| 182 | } |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 183 | 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 Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 195 | public boolean isDropEnabled() { |
| 196 | return mActive; |
| 197 | } |
| 198 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 199 | @Override |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 200 | public void onDragEnd() { |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 201 | mActive = false; |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 204 | /** |
| 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 Goyal | e9b651e | 2015-04-24 11:44:51 -0700 | [diff] [blame] | 234 | @Override |
| 235 | public void prepareAccessibilityDrop() { } |
| 236 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 237 | @Thunk abstract void completeDrop(DragObject d); |
| 238 | |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 239 | @Override |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 240 | public void getHitRectRelativeToDragLayer(android.graphics.Rect outRect) { |
Winson Chung | a62e9fd | 2011-07-11 15:20:48 -0700 | [diff] [blame] | 241 | super.getHitRect(outRect); |
| 242 | outRect.bottom += mBottomDragPadding; |
Winson Chung | 156ab5b | 2013-07-12 14:14:16 -0700 | [diff] [blame] | 243 | |
| 244 | int[] coords = new int[2]; |
| 245 | mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(this, coords); |
| 246 | outRect.offsetTo(coords[0], coords[1]); |
Winson Chung | a62e9fd | 2011-07-11 15:20:48 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 249 | protected Rect getIconRect(int viewWidth, int viewHeight, int drawableWidth, int drawableHeight) { |
Winson Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame] | 250 | 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 Meglio | d6a33c6 | 2013-02-06 15:40:46 -0800 | [diff] [blame] | 255 | |
| 256 | final int width = drawableWidth; |
| 257 | final int height = drawableHeight; |
| 258 | |
| 259 | final int left; |
| 260 | final int right; |
| 261 | |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame^] | 262 | if (Utilities.isRtl(getResources())) { |
Fabrice Di Meglio | d6a33c6 | 2013-02-06 15:40:46 -0800 | [diff] [blame] | 263 | 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 Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame] | 274 | |
| 275 | // Center the destination rect about the trash icon |
Fabrice Di Meglio | d6a33c6 | 2013-02-06 15:40:46 -0800 | [diff] [blame] | 276 | final int xOffset = (int) -(viewWidth - width) / 2; |
| 277 | final int yOffset = (int) -(viewHeight - height) / 2; |
Winson Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame] | 278 | to.offset(xOffset, yOffset); |
| 279 | |
| 280 | return to; |
| 281 | } |
| 282 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 283 | @Override |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 284 | public void getLocationInDragLayer(int[] loc) { |
| 285 | mLauncher.getDragLayer().getLocationInDragLayer(this, loc); |
| 286 | } |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 287 | |
| 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 Goyal | fe0d1f2 | 2015-04-28 22:01:31 -0700 | [diff] [blame] | 301 | |
| 302 | public int getTextColor() { |
| 303 | return getTextColors().getDefaultColor(); |
| 304 | } |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 305 | } |