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; |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 26 | import android.graphics.drawable.TransitionDrawable; |
Sunny Goyal | fe0d1f2 | 2015-04-28 22:01:31 -0700 | [diff] [blame] | 27 | import android.os.Build; |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 28 | import android.util.AttributeSet; |
Fabrice Di Meglio | d6a33c6 | 2013-02-06 15:40:46 -0800 | [diff] [blame] | 29 | import android.view.View; |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 30 | import android.view.View.OnClickListener; |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 31 | import android.view.ViewGroup; |
| 32 | import android.view.animation.DecelerateInterpolator; |
| 33 | import android.view.animation.LinearInterpolator; |
Adam Cohen | d4d7aa5 | 2011-07-19 21:47:37 -0700 | [diff] [blame] | 34 | import android.widget.TextView; |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 35 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 36 | import com.android.launcher3.util.Thunk; |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 37 | |
| 38 | /** |
| 39 | * Implements a DropTarget. |
| 40 | */ |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 41 | public abstract class ButtonDropTarget extends TextView |
| 42 | implements DropTarget, DragController.DragListener, OnClickListener { |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 43 | |
| 44 | private static int DRAG_VIEW_DROP_DURATION = 285; |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 45 | |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 46 | protected Launcher mLauncher; |
Winson Chung | a62e9fd | 2011-07-11 15:20:48 -0700 | [diff] [blame] | 47 | private int mBottomDragPadding; |
Adam Cohen | d4d7aa5 | 2011-07-19 21:47:37 -0700 | [diff] [blame] | 48 | protected TextView mText; |
| 49 | protected SearchDropTargetBar mSearchDropTargetBar; |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 50 | |
| 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 Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame] | 55 | protected int mHoverColor = 0; |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 56 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 57 | protected ColorStateList mOriginalTextColor; |
| 58 | protected TransitionDrawable mDrawable; |
| 59 | |
Sunny Goyal | fe0d1f2 | 2015-04-28 22:01:31 -0700 | [diff] [blame] | 60 | private ObjectAnimator mCurrentColorAnim; |
| 61 | |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 62 | 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 Goyal | fe0d1f2 | 2015-04-28 22:01:31 -0700 | [diff] [blame] | 68 | mBottomDragPadding = getResources().getDimensionPixelSize(R.dimen.drop_target_drag_padding); |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 71 | @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 Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 85 | @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 86 | protected void setDrawable(int resId) { |
Sunny Goyal | e0cab30 | 2015-05-20 16:40:30 -0700 | [diff] [blame^] | 87 | // 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 Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 91 | |
Sunny Goyal | e0cab30 | 2015-05-20 16:40:30 -0700 | [diff] [blame^] | 92 | 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 Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
| 99 | public void setLauncher(Launcher launcher) { |
| 100 | mLauncher = launcher; |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Adam Cohen | d4d7aa5 | 2011-07-19 21:47:37 -0700 | [diff] [blame] | 103 | public void setSearchDropTargetBar(SearchDropTargetBar searchDropTargetBar) { |
| 104 | mSearchDropTargetBar = searchDropTargetBar; |
| 105 | } |
| 106 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 107 | @Override |
Sunny Goyal | ddec734 | 2015-04-29 18:12:37 -0700 | [diff] [blame] | 108 | public void onFlingToDelete(DragObject d, PointF vec) { } |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 109 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 110 | @Override |
| 111 | public final void onDragEnter(DragObject d) { |
Winson Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame] | 112 | d.dragView.setColor(mHoverColor); |
Sunny Goyal | fe0d1f2 | 2015-04-28 22:01:31 -0700 | [diff] [blame] | 113 | if (Utilities.isLmpOrAbove()) { |
| 114 | mDrawable.startTransition(DragView.COLOR_CHANGE_DURATION); |
| 115 | animateTextColor(mHoverColor); |
| 116 | } else { |
| 117 | mDrawable.startTransition(0); |
| 118 | setTextColor(mHoverColor); |
| 119 | } |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 122 | @Override |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 123 | public void onDragOver(DragObject d) { |
| 124 | // Do nothing |
| 125 | } |
| 126 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 127 | protected void resetHoverColor() { |
Sunny Goyal | fe0d1f2 | 2015-04-28 22:01:31 -0700 | [diff] [blame] | 128 | 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 Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 147 | @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 Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 158 | @Override |
| 159 | public final void onDragStart(DragSource source, Object info, int dragAction) { |
| 160 | mActive = supportsDrop(source, info); |
| 161 | mDrawable.resetTransition(); |
Sunny Goyal | fe0d1f2 | 2015-04-28 22:01:31 -0700 | [diff] [blame] | 162 | if (mCurrentColorAnim != null) { |
| 163 | mCurrentColorAnim.cancel(); |
| 164 | mCurrentColorAnim = null; |
| 165 | } |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 166 | 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 Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 178 | public boolean isDropEnabled() { |
| 179 | return mActive; |
| 180 | } |
| 181 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 182 | @Override |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 183 | public void onDragEnd() { |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 184 | mActive = false; |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 187 | /** |
| 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 Goyal | e9b651e | 2015-04-24 11:44:51 -0700 | [diff] [blame] | 217 | @Override |
| 218 | public void prepareAccessibilityDrop() { } |
| 219 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 220 | @Thunk abstract void completeDrop(DragObject d); |
| 221 | |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 222 | @Override |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 223 | public void getHitRectRelativeToDragLayer(android.graphics.Rect outRect) { |
Winson Chung | a62e9fd | 2011-07-11 15:20:48 -0700 | [diff] [blame] | 224 | super.getHitRect(outRect); |
| 225 | outRect.bottom += mBottomDragPadding; |
Winson Chung | 156ab5b | 2013-07-12 14:14:16 -0700 | [diff] [blame] | 226 | |
| 227 | int[] coords = new int[2]; |
| 228 | mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(this, coords); |
| 229 | outRect.offsetTo(coords[0], coords[1]); |
Winson Chung | a62e9fd | 2011-07-11 15:20:48 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 232 | protected Rect getIconRect(int viewWidth, int viewHeight, int drawableWidth, int drawableHeight) { |
Winson Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame] | 233 | 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 Meglio | d6a33c6 | 2013-02-06 15:40:46 -0800 | [diff] [blame] | 238 | |
| 239 | final int width = drawableWidth; |
| 240 | final int height = drawableHeight; |
| 241 | |
| 242 | final int left; |
| 243 | final int right; |
| 244 | |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 245 | if (Utilities.isRtl(getResources())) { |
Fabrice Di Meglio | d6a33c6 | 2013-02-06 15:40:46 -0800 | [diff] [blame] | 246 | 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 Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame] | 257 | |
| 258 | // Center the destination rect about the trash icon |
Fabrice Di Meglio | d6a33c6 | 2013-02-06 15:40:46 -0800 | [diff] [blame] | 259 | final int xOffset = (int) -(viewWidth - width) / 2; |
| 260 | final int yOffset = (int) -(viewHeight - height) / 2; |
Winson Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame] | 261 | to.offset(xOffset, yOffset); |
| 262 | |
| 263 | return to; |
| 264 | } |
| 265 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 266 | @Override |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 267 | public void getLocationInDragLayer(int[] loc) { |
| 268 | mLauncher.getDragLayer().getLocationInDragLayer(this, loc); |
| 269 | } |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 270 | |
| 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 Goyal | fe0d1f2 | 2015-04-28 22:01:31 -0700 | [diff] [blame] | 284 | |
| 285 | public int getTextColor() { |
| 286 | return getTextColors().getDefaultColor(); |
| 287 | } |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 288 | } |