ButtonDropTarget.java revision 94b510cc683a6436ae82c6d323cbd8b429561b06
161fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung/*
261fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung * Copyright (C) 2010 The Android Open Source Project
361fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung *
461fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung * Licensed under the Apache License, Version 2.0 (the "License");
561fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung * you may not use this file except in compliance with the License.
661fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung * You may obtain a copy of the License at
761fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung *
861fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung *      http://www.apache.org/licenses/LICENSE-2.0
961fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung *
1061fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung * Unless required by applicable law or agreed to in writing, software
1161fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung * distributed under the License is distributed on an "AS IS" BASIS,
1261fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1361fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung * See the License for the specific language governing permissions and
1461fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung * limitations under the License.
1561fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung */
1661fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung
17325dc23624160689e59fbac708cf6f222b20d025Daniel Sandlerpackage com.android.launcher3;
1861fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung
193a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyalimport android.animation.AnimatorSet;
203a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyalimport android.animation.FloatArrayEvaluator;
21fe0d1f2458e6a442613b070ae549124a4780e759Sunny Goyalimport android.animation.ObjectAnimator;
223a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyalimport android.animation.ValueAnimator;
233a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyalimport android.animation.ValueAnimator.AnimatorUpdateListener;
24fe0d1f2458e6a442613b070ae549124a4780e759Sunny Goyalimport android.annotation.TargetApi;
2561fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chungimport android.content.Context;
26fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyalimport android.content.res.ColorStateList;
27855b1b5fff5f6f03a641b3b6973780a24fd9641eTony Wickhamimport android.content.res.Resources;
28f37a21412afb16f61ca23b5aca2e90691b57e171Sunny Goyalimport android.content.res.TypedArray;
293a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyalimport android.graphics.ColorMatrix;
303a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyalimport android.graphics.ColorMatrixColorFilter;
31043f2af567178b82b0b41f12d379e7dd12da2936Winson Chungimport android.graphics.PointF;
3261967cb17f8fa9ee01f93e9f25a1074559b54cf3Winson Chungimport android.graphics.Rect;
333a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyalimport android.graphics.drawable.Drawable;
34fe0d1f2458e6a442613b070ae549124a4780e759Sunny Goyalimport android.os.Build;
3561fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chungimport android.util.AttributeSet;
36d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglioimport android.view.View;
371a70cef9884270f2f0a760f079a10fdfb1544c98Sunny Goyalimport android.view.View.OnClickListener;
38fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyalimport android.view.ViewGroup;
39e78e3d734b577c1ab6dc0738a83600374908ea52Sunny Goyalimport android.view.accessibility.AccessibilityEvent;
40fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyalimport android.view.animation.DecelerateInterpolator;
41fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyalimport android.view.animation.LinearInterpolator;
42d4d7aa551ffdc80d810ff970fa72a6509960401eAdam Cohenimport android.widget.TextView;
4361fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung
44fedca43d396d6fd7c46fbb2f37dfa7cfe3b31834Vadim Tryshevimport com.android.launcher3.dragndrop.DragController;
45fedca43d396d6fd7c46fbb2f37dfa7cfe3b31834Vadim Tryshevimport com.android.launcher3.dragndrop.DragLayer;
4694b510cc683a6436ae82c6d323cbd8b429561b06Sunny Goyalimport com.android.launcher3.dragndrop.DragOptions;
47fedca43d396d6fd7c46fbb2f37dfa7cfe3b31834Vadim Tryshevimport com.android.launcher3.dragndrop.DragView;
48fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyalimport com.android.launcher3.util.Thunk;
4961fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung
5061fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung/**
5161fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung * Implements a DropTarget.
5261fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung */
531a70cef9884270f2f0a760f079a10fdfb1544c98Sunny Goyalpublic abstract class ButtonDropTarget extends TextView
541a70cef9884270f2f0a760f079a10fdfb1544c98Sunny Goyal        implements DropTarget, DragController.DragListener, OnClickListener {
55fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal
56b54c4a3d864110a222e12683d88e6d46c27bf7a1Tony Wickham    private static final int DRAG_VIEW_DROP_DURATION = 285;
5761fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung
58f37a21412afb16f61ca23b5aca2e90691b57e171Sunny Goyal    private final boolean mHideParentOnDisable;
5947328fd53f43fd0c2ed14ad925dd04483f9229a0Sunny Goyal    protected final Launcher mLauncher;
60f37a21412afb16f61ca23b5aca2e90691b57e171Sunny Goyal
61a62e9fd95ae10cff4676f2cc1f4d68334ca27a0bWinson Chung    private int mBottomDragPadding;
6247328fd53f43fd0c2ed14ad925dd04483f9229a0Sunny Goyal    protected DropTargetBar mDropTargetBar;
6361fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung
6461fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    /** Whether this drop target is active for the current drag */
6561fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    protected boolean mActive;
664583d09c0560a83bfd0320299482cc41ac460b0cSunny Goyal    /** Whether an accessible drag is in progress */
674583d09c0560a83bfd0320299482cc41ac460b0cSunny Goyal    private boolean mAccessibleDrag;
68855b1b5fff5f6f03a641b3b6973780a24fd9641eTony Wickham    /** An item must be dragged at least this many pixels before this drop target is enabled. */
69855b1b5fff5f6f03a641b3b6973780a24fd9641eTony Wickham    private final int mDragDistanceThreshold;
7061fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung
7161fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    /** The paint applied to the drag view on hover */
7261967cb17f8fa9ee01f93e9f25a1074559b54cf3Winson Chung    protected int mHoverColor = 0;
7361fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung
74fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal    protected ColorStateList mOriginalTextColor;
753a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal    protected Drawable mDrawable;
763a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal
773a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal    private AnimatorSet mCurrentColorAnim;
78316490e636aad788fcfbfc2e04dd4f0e145bdd00Sunny Goyal    @Thunk ColorMatrix mSrcFilter, mDstFilter, mCurrentFilter;
79fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal
8061fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    public ButtonDropTarget(Context context, AttributeSet attrs) {
8161fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung        this(context, attrs, 0);
8261fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    }
8361fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung
8461fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    public ButtonDropTarget(Context context, AttributeSet attrs, int defStyle) {
8561fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung        super(context, attrs, defStyle);
8647328fd53f43fd0c2ed14ad925dd04483f9229a0Sunny Goyal        mLauncher = (Launcher) context;
8747328fd53f43fd0c2ed14ad925dd04483f9229a0Sunny Goyal
88855b1b5fff5f6f03a641b3b6973780a24fd9641eTony Wickham        Resources resources = getResources();
89855b1b5fff5f6f03a641b3b6973780a24fd9641eTony Wickham        mBottomDragPadding = resources.getDimensionPixelSize(R.dimen.drop_target_drag_padding);
90f37a21412afb16f61ca23b5aca2e90691b57e171Sunny Goyal
91f37a21412afb16f61ca23b5aca2e90691b57e171Sunny Goyal        TypedArray a = context.obtainStyledAttributes(attrs,
92f37a21412afb16f61ca23b5aca2e90691b57e171Sunny Goyal                R.styleable.ButtonDropTarget, defStyle, 0);
93f37a21412afb16f61ca23b5aca2e90691b57e171Sunny Goyal        mHideParentOnDisable = a.getBoolean(R.styleable.ButtonDropTarget_hideParentOnDisable, false);
94f37a21412afb16f61ca23b5aca2e90691b57e171Sunny Goyal        a.recycle();
95855b1b5fff5f6f03a641b3b6973780a24fd9641eTony Wickham        mDragDistanceThreshold = resources.getDimensionPixelSize(R.dimen.drag_distanceThreshold);
9661fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    }
9761fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung
98fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal    @Override
99fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal    protected void onFinishInflate() {
100fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal        super.onFinishInflate();
101fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal        mOriginalTextColor = getTextColors();
10261fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    }
10361fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung
1047066003b2032a49ae5e59dab9b706259bdeb7e6eSunny Goyal    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
105fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal    protected void setDrawable(int resId) {
106e0cab303b8cf1fd00b152219aa56663608582f5aSunny Goyal        // We do not set the drawable in the xml as that inflates two drawables corresponding to
107e0cab303b8cf1fd00b152219aa56663608582f5aSunny Goyal        // drawableLeft and drawableStart.
1083a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal        mDrawable = getResources().getDrawable(resId);
109fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal
1109fc953b94dbc6b99e6de08c9dcc80a0cb8e3e319Sunny Goyal        if (Utilities.ATLEAST_JB_MR1) {
111e0cab303b8cf1fd00b152219aa56663608582f5aSunny Goyal            setCompoundDrawablesRelativeWithIntrinsicBounds(mDrawable, null, null, null);
112e0cab303b8cf1fd00b152219aa56663608582f5aSunny Goyal        } else {
113e0cab303b8cf1fd00b152219aa56663608582f5aSunny Goyal            setCompoundDrawablesWithIntrinsicBounds(mDrawable, null, null, null);
114fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal        }
115fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal    }
116fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal
11747328fd53f43fd0c2ed14ad925dd04483f9229a0Sunny Goyal    public void setDropTargetBar(DropTargetBar dropTargetBar) {
118b54c4a3d864110a222e12683d88e6d46c27bf7a1Tony Wickham        mDropTargetBar = dropTargetBar;
119d4d7aa551ffdc80d810ff970fa72a6509960401eAdam Cohen    }
120d4d7aa551ffdc80d810ff970fa72a6509960401eAdam Cohen
121fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal    @Override
122ddec73471eb6cc1f15eb9421a205bb2362509075Sunny Goyal    public void onFlingToDelete(DragObject d, PointF vec) { }
123fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal
124fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal    @Override
125fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal    public final void onDragEnter(DragObject d) {
126fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal        d.dragView.setColor(mHoverColor);
1279fc953b94dbc6b99e6de08c9dcc80a0cb8e3e319Sunny Goyal        if (Utilities.ATLEAST_LOLLIPOP) {
128fe0d1f2458e6a442613b070ae549124a4780e759Sunny Goyal            animateTextColor(mHoverColor);
129fe0d1f2458e6a442613b070ae549124a4780e759Sunny Goyal        } else {
1303a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal            if (mCurrentFilter == null) {
1313a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal                mCurrentFilter = new ColorMatrix();
1323a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal            }
1333a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal            DragView.setColorScale(mHoverColor, mCurrentFilter);
1343a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal            mDrawable.setColorFilter(new ColorMatrixColorFilter(mCurrentFilter));
135fe0d1f2458e6a442613b070ae549124a4780e759Sunny Goyal            setTextColor(mHoverColor);
136fe0d1f2458e6a442613b070ae549124a4780e759Sunny Goyal        }
137d21301e674377ecd72bd14f32b52c68fabcfd3beSunny Goyal        if (d.stateAnnouncer != null) {
138d21301e674377ecd72bd14f32b52c68fabcfd3beSunny Goyal            d.stateAnnouncer.cancel();
139d21301e674377ecd72bd14f32b52c68fabcfd3beSunny Goyal        }
140e78e3d734b577c1ab6dc0738a83600374908ea52Sunny Goyal        sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
14161fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    }
14261fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung
143fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal    @Override
144fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal    public void onDragOver(DragObject d) {
145043f2af567178b82b0b41f12d379e7dd12da2936Winson Chung        // Do nothing
146043f2af567178b82b0b41f12d379e7dd12da2936Winson Chung    }
147043f2af567178b82b0b41f12d379e7dd12da2936Winson Chung
1489fc953b94dbc6b99e6de08c9dcc80a0cb8e3e319Sunny Goyal    protected void resetHoverColor() {
1499fc953b94dbc6b99e6de08c9dcc80a0cb8e3e319Sunny Goyal        if (Utilities.ATLEAST_LOLLIPOP) {
150fe0d1f2458e6a442613b070ae549124a4780e759Sunny Goyal            animateTextColor(mOriginalTextColor.getDefaultColor());
151fe0d1f2458e6a442613b070ae549124a4780e759Sunny Goyal        } else {
1523a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal            mDrawable.setColorFilter(null);
153fe0d1f2458e6a442613b070ae549124a4780e759Sunny Goyal            setTextColor(mOriginalTextColor);
154fe0d1f2458e6a442613b070ae549124a4780e759Sunny Goyal        }
155fe0d1f2458e6a442613b070ae549124a4780e759Sunny Goyal    }
156fe0d1f2458e6a442613b070ae549124a4780e759Sunny Goyal
157fe0d1f2458e6a442613b070ae549124a4780e759Sunny Goyal    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
158fe0d1f2458e6a442613b070ae549124a4780e759Sunny Goyal    private void animateTextColor(int targetColor) {
159fe0d1f2458e6a442613b070ae549124a4780e759Sunny Goyal        if (mCurrentColorAnim != null) {
160fe0d1f2458e6a442613b070ae549124a4780e759Sunny Goyal            mCurrentColorAnim.cancel();
161fe0d1f2458e6a442613b070ae549124a4780e759Sunny Goyal        }
1623a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal
1633a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal        mCurrentColorAnim = new AnimatorSet();
164fe0d1f2458e6a442613b070ae549124a4780e759Sunny Goyal        mCurrentColorAnim.setDuration(DragView.COLOR_CHANGE_DURATION);
1653a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal
1663a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal        if (mSrcFilter == null) {
1673a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal            mSrcFilter = new ColorMatrix();
1683a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal            mDstFilter = new ColorMatrix();
1693a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal            mCurrentFilter = new ColorMatrix();
1703a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal        }
1713a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal
1723a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal        DragView.setColorScale(getTextColor(), mSrcFilter);
1733a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal        DragView.setColorScale(targetColor, mDstFilter);
1743a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal        ValueAnimator anim1 = ValueAnimator.ofObject(
1753a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal                new FloatArrayEvaluator(mCurrentFilter.getArray()),
1763a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal                mSrcFilter.getArray(), mDstFilter.getArray());
1773a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal        anim1.addUpdateListener(new AnimatorUpdateListener() {
1783a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal
1793a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal            @Override
1803a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal            public void onAnimationUpdate(ValueAnimator animation) {
1813a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal                mDrawable.setColorFilter(new ColorMatrixColorFilter(mCurrentFilter));
1823a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal                invalidate();
1833a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal            }
1843a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal        });
1853a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal
1863a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal        mCurrentColorAnim.play(anim1);
1873a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal        mCurrentColorAnim.play(ObjectAnimator.ofArgb(this, "textColor", targetColor));
188fe0d1f2458e6a442613b070ae549124a4780e759Sunny Goyal        mCurrentColorAnim.start();
18961fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    }
19061fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung
191fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal    @Override
192fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal    public final void onDragExit(DragObject d) {
193fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal        if (!d.dragComplete) {
194fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal            d.dragView.setColor(0);
195fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal            resetHoverColor();
196fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal        } else {
197fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal            // Restore the hover color
198fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal            d.dragView.setColor(mHoverColor);
199fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal        }
20061fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    }
20161fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung
202aa8ef119f18864f4ab41c12f9c2ad6d7f643a0a9Sunny Goyal    @Override
20394b510cc683a6436ae82c6d323cbd8b429561b06Sunny Goyal    public void onDragStart(DropTarget.DragObject dragObject, DragOptions options) {
20494b510cc683a6436ae82c6d323cbd8b429561b06Sunny Goyal        mActive = supportsDrop(dragObject.dragSource, dragObject.dragInfo);
2053a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal        mDrawable.setColorFilter(null);
206fe0d1f2458e6a442613b070ae549124a4780e759Sunny Goyal        if (mCurrentColorAnim != null) {
207fe0d1f2458e6a442613b070ae549124a4780e759Sunny Goyal            mCurrentColorAnim.cancel();
208fe0d1f2458e6a442613b070ae549124a4780e759Sunny Goyal            mCurrentColorAnim = null;
209fe0d1f2458e6a442613b070ae549124a4780e759Sunny Goyal        }
210fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal        setTextColor(mOriginalTextColor);
211f37a21412afb16f61ca23b5aca2e90691b57e171Sunny Goyal        (mHideParentOnDisable ? ((ViewGroup) getParent()) : this)
212f37a21412afb16f61ca23b5aca2e90691b57e171Sunny Goyal                .setVisibility(mActive ? View.VISIBLE : View.GONE);
21394b510cc683a6436ae82c6d323cbd8b429561b06Sunny Goyal
21494b510cc683a6436ae82c6d323cbd8b429561b06Sunny Goyal        mAccessibleDrag = options.isAccessibleDrag;
21594b510cc683a6436ae82c6d323cbd8b429561b06Sunny Goyal        setOnClickListener(mAccessibleDrag ? this : null);
21661fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    }
21761fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung
218fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal    @Override
219fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal    public final boolean acceptDrop(DragObject dragObject) {
220fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal        return supportsDrop(dragObject.dragSource, dragObject.dragInfo);
22161fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    }
22261fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung
223aa8ef119f18864f4ab41c12f9c2ad6d7f643a0a9Sunny Goyal    protected abstract boolean supportsDrop(DragSource source, ItemInfo info);
224fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal
225fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal    @Override
22661fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    public boolean isDropEnabled() {
2274583d09c0560a83bfd0320299482cc41ac460b0cSunny Goyal        return mActive && (mAccessibleDrag ||
2284583d09c0560a83bfd0320299482cc41ac460b0cSunny Goyal                mLauncher.getDragController().getDistanceDragged() >= mDragDistanceThreshold);
22961fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    }
23061fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung
231fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal    @Override
23261fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    public void onDragEnd() {
233fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal        mActive = false;
23494b510cc683a6436ae82c6d323cbd8b429561b06Sunny Goyal        setOnClickListener(null);
23561fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    }
23661fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung
237fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal    /**
238fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal     * On drop animate the dropView to the icon.
239fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal     */
240fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal    @Override
241fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal    public void onDrop(final DragObject d) {
242fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal        final DragLayer dragLayer = mLauncher.getDragLayer();
243fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal        final Rect from = new Rect();
244fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal        dragLayer.getViewRectRelativeToSelf(d.dragView, from);
245fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal
246fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal        int width = mDrawable.getIntrinsicWidth();
247fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal        int height = mDrawable.getIntrinsicHeight();
248fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal        final Rect to = getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(),
249fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal                width, height);
250fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal        final float scale = (float) to.width() / from.width();
251b54c4a3d864110a222e12683d88e6d46c27bf7a1Tony Wickham        mDropTargetBar.deferOnDragEnd();
252fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal
253fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal        Runnable onAnimationEndRunnable = new Runnable() {
254fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal            @Override
255fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal            public void run() {
256fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal                completeDrop(d);
257b54c4a3d864110a222e12683d88e6d46c27bf7a1Tony Wickham                mDropTargetBar.onDragEnd();
258fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal                mLauncher.exitSpringLoadedDragModeDelayed(true, 0, null);
259fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal            }
260fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal        };
261fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal        dragLayer.animateView(d.dragView, from, to, scale, 1f, 1f, 0.1f, 0.1f,
262fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal                DRAG_VIEW_DROP_DURATION, new DecelerateInterpolator(2),
263fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal                new LinearInterpolator(), onAnimationEndRunnable,
264fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal                DragLayer.ANIMATION_END_DISAPPEAR, null);
265fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal    }
266fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal
267e9b651eef1b9f3647eba94f833bff3fc52f5956bSunny Goyal    @Override
268e9b651eef1b9f3647eba94f833bff3fc52f5956bSunny Goyal    public void prepareAccessibilityDrop() { }
269e9b651eef1b9f3647eba94f833bff3fc52f5956bSunny Goyal
270fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal    @Thunk abstract void completeDrop(DragObject d);
271fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal
27261fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    @Override
2737d30a37007bac318db1c9af47a9af12d348042a5Adam Cohen    public void getHitRectRelativeToDragLayer(android.graphics.Rect outRect) {
274a62e9fd95ae10cff4676f2cc1f4d68334ca27a0bWinson Chung        super.getHitRect(outRect);
275a62e9fd95ae10cff4676f2cc1f4d68334ca27a0bWinson Chung        outRect.bottom += mBottomDragPadding;
276156ab5b22e45b36a1c5edbe5accccf6aefcb4907Winson Chung
277156ab5b22e45b36a1c5edbe5accccf6aefcb4907Winson Chung        int[] coords = new int[2];
278156ab5b22e45b36a1c5edbe5accccf6aefcb4907Winson Chung        mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(this, coords);
279156ab5b22e45b36a1c5edbe5accccf6aefcb4907Winson Chung        outRect.offsetTo(coords[0], coords[1]);
280a62e9fd95ae10cff4676f2cc1f4d68334ca27a0bWinson Chung    }
281a62e9fd95ae10cff4676f2cc1f4d68334ca27a0bWinson Chung
282fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal    protected Rect getIconRect(int viewWidth, int viewHeight, int drawableWidth, int drawableHeight) {
28361967cb17f8fa9ee01f93e9f25a1074559b54cf3Winson Chung        DragLayer dragLayer = mLauncher.getDragLayer();
28461967cb17f8fa9ee01f93e9f25a1074559b54cf3Winson Chung
28561967cb17f8fa9ee01f93e9f25a1074559b54cf3Winson Chung        // Find the rect to animate to (the view is center aligned)
28661967cb17f8fa9ee01f93e9f25a1074559b54cf3Winson Chung        Rect to = new Rect();
28761967cb17f8fa9ee01f93e9f25a1074559b54cf3Winson Chung        dragLayer.getViewRectRelativeToSelf(this, to);
288d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio
289d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio        final int width = drawableWidth;
290d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio        final int height = drawableHeight;
291d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio
292d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio        final int left;
293d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio        final int right;
294d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio
2957066003b2032a49ae5e59dab9b706259bdeb7e6eSunny Goyal        if (Utilities.isRtl(getResources())) {
296d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio            right = to.right - getPaddingRight();
297d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio            left = right - width;
298d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio        } else {
299d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio            left = to.left + getPaddingLeft();
300d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio            right = left + width;
301d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio        }
302d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio
303d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio        final int top = to.top + (getMeasuredHeight() - height) / 2;
304d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio        final int bottom = top +  height;
305d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio
306d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio        to.set(left, top, right, bottom);
30761967cb17f8fa9ee01f93e9f25a1074559b54cf3Winson Chung
30861967cb17f8fa9ee01f93e9f25a1074559b54cf3Winson Chung        // Center the destination rect about the trash icon
309d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio        final int xOffset = (int) -(viewWidth - width) / 2;
310d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio        final int yOffset = (int) -(viewHeight - height) / 2;
31161967cb17f8fa9ee01f93e9f25a1074559b54cf3Winson Chung        to.offset(xOffset, yOffset);
31261967cb17f8fa9ee01f93e9f25a1074559b54cf3Winson Chung
31361967cb17f8fa9ee01f93e9f25a1074559b54cf3Winson Chung        return to;
31461967cb17f8fa9ee01f93e9f25a1074559b54cf3Winson Chung    }
31561967cb17f8fa9ee01f93e9f25a1074559b54cf3Winson Chung
3161a70cef9884270f2f0a760f079a10fdfb1544c98Sunny Goyal    @Override
3171a70cef9884270f2f0a760f079a10fdfb1544c98Sunny Goyal    public void onClick(View v) {
318ae50284e0a838139c67caf0064a0977c871497faSunny Goyal        mLauncher.getAccessibilityDelegate().handleAccessibleDrop(this, null, null);
3191a70cef9884270f2f0a760f079a10fdfb1544c98Sunny Goyal    }
320fe0d1f2458e6a442613b070ae549124a4780e759Sunny Goyal
321fe0d1f2458e6a442613b070ae549124a4780e759Sunny Goyal    public int getTextColor() {
322fe0d1f2458e6a442613b070ae549124a4780e759Sunny Goyal        return getTextColors().getDefaultColor();
323fe0d1f2458e6a442613b070ae549124a4780e759Sunny Goyal    }
32461fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung}
325