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;
27fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyalimport android.content.res.Configuration;
283a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyalimport android.graphics.ColorMatrix;
293a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyalimport android.graphics.ColorMatrixColorFilter;
30043f2af567178b82b0b41f12d379e7dd12da2936Winson Chungimport android.graphics.PointF;
3161967cb17f8fa9ee01f93e9f25a1074559b54cf3Winson Chungimport android.graphics.Rect;
323a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyalimport android.graphics.drawable.Drawable;
33fe0d1f2458e6a442613b070ae549124a4780e759Sunny Goyalimport android.os.Build;
3461fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chungimport android.util.AttributeSet;
35d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglioimport android.view.View;
361a70cef9884270f2f0a760f079a10fdfb1544c98Sunny Goyalimport android.view.View.OnClickListener;
37fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyalimport android.view.ViewGroup;
38fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyalimport android.view.animation.DecelerateInterpolator;
39fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyalimport android.view.animation.LinearInterpolator;
40d4d7aa551ffdc80d810ff970fa72a6509960401eAdam Cohenimport android.widget.TextView;
4161fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung
42fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyalimport com.android.launcher3.util.Thunk;
4361fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung
4461fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung/**
4561fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung * Implements a DropTarget.
4661fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung */
471a70cef9884270f2f0a760f079a10fdfb1544c98Sunny Goyalpublic abstract class ButtonDropTarget extends TextView
481a70cef9884270f2f0a760f079a10fdfb1544c98Sunny Goyal        implements DropTarget, DragController.DragListener, OnClickListener {
49fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal
50fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal    private static int DRAG_VIEW_DROP_DURATION = 285;
5161fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung
5261fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    protected Launcher mLauncher;
53a62e9fd95ae10cff4676f2cc1f4d68334ca27a0bWinson Chung    private int mBottomDragPadding;
54d4d7aa551ffdc80d810ff970fa72a6509960401eAdam Cohen    protected SearchDropTargetBar mSearchDropTargetBar;
5561fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung
5661fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    /** Whether this drop target is active for the current drag */
5761fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    protected boolean mActive;
5861fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung
5961fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    /** The paint applied to the drag view on hover */
6061967cb17f8fa9ee01f93e9f25a1074559b54cf3Winson Chung    protected int mHoverColor = 0;
6161fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung
62fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal    protected ColorStateList mOriginalTextColor;
633a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal    protected Drawable mDrawable;
643a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal
653a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal    private AnimatorSet mCurrentColorAnim;
66316490e636aad788fcfbfc2e04dd4f0e145bdd00Sunny Goyal    @Thunk ColorMatrix mSrcFilter, mDstFilter, mCurrentFilter;
67fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal
68fe0d1f2458e6a442613b070ae549124a4780e759Sunny Goyal
6961fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    public ButtonDropTarget(Context context, AttributeSet attrs) {
7061fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung        this(context, attrs, 0);
7161fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    }
7261fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung
7361fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    public ButtonDropTarget(Context context, AttributeSet attrs, int defStyle) {
7461fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung        super(context, attrs, defStyle);
75fe0d1f2458e6a442613b070ae549124a4780e759Sunny Goyal        mBottomDragPadding = getResources().getDimensionPixelSize(R.dimen.drop_target_drag_padding);
7661fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    }
7761fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung
78fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal    @Override
79fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal    protected void onFinishInflate() {
80fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal        super.onFinishInflate();
81fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal        mOriginalTextColor = getTextColors();
82fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal
83fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal        // Remove the text in the Phone UI in landscape
847501adf4b71bb17d0785a5939efbcac4b39125c3Winson Chung        DeviceProfile grid = ((Launcher) getContext()).getDeviceProfile();
857501adf4b71bb17d0785a5939efbcac4b39125c3Winson Chung        if (grid.isVerticalBarLayout()) {
867501adf4b71bb17d0785a5939efbcac4b39125c3Winson Chung            setText("");
87fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal        }
8861fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    }
8961fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung
907066003b2032a49ae5e59dab9b706259bdeb7e6eSunny Goyal    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
91fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal    protected void setDrawable(int resId) {
92e0cab303b8cf1fd00b152219aa56663608582f5aSunny Goyal        // We do not set the drawable in the xml as that inflates two drawables corresponding to
93e0cab303b8cf1fd00b152219aa56663608582f5aSunny Goyal        // drawableLeft and drawableStart.
943a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal        mDrawable = getResources().getDrawable(resId);
95fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal
96e0cab303b8cf1fd00b152219aa56663608582f5aSunny Goyal        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
97e0cab303b8cf1fd00b152219aa56663608582f5aSunny Goyal            setCompoundDrawablesRelativeWithIntrinsicBounds(mDrawable, null, null, null);
98e0cab303b8cf1fd00b152219aa56663608582f5aSunny Goyal        } else {
99e0cab303b8cf1fd00b152219aa56663608582f5aSunny Goyal            setCompoundDrawablesWithIntrinsicBounds(mDrawable, null, null, null);
100fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal        }
101fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal    }
102fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal
103fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal    public void setLauncher(Launcher launcher) {
104fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal        mLauncher = launcher;
10561fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    }
10661fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung
107d4d7aa551ffdc80d810ff970fa72a6509960401eAdam Cohen    public void setSearchDropTargetBar(SearchDropTargetBar searchDropTargetBar) {
108d4d7aa551ffdc80d810ff970fa72a6509960401eAdam Cohen        mSearchDropTargetBar = searchDropTargetBar;
109d4d7aa551ffdc80d810ff970fa72a6509960401eAdam Cohen    }
110d4d7aa551ffdc80d810ff970fa72a6509960401eAdam Cohen
111fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal    @Override
112ddec73471eb6cc1f15eb9421a205bb2362509075Sunny Goyal    public void onFlingToDelete(DragObject d, PointF vec) { }
113fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal
114fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal    @Override
115fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal    public final void onDragEnter(DragObject d) {
116fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal        d.dragView.setColor(mHoverColor);
117fe0d1f2458e6a442613b070ae549124a4780e759Sunny Goyal        if (Utilities.isLmpOrAbove()) {
118fe0d1f2458e6a442613b070ae549124a4780e759Sunny Goyal            animateTextColor(mHoverColor);
119fe0d1f2458e6a442613b070ae549124a4780e759Sunny Goyal        } else {
1203a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal            if (mCurrentFilter == null) {
1213a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal                mCurrentFilter = new ColorMatrix();
1223a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal            }
1233a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal            DragView.setColorScale(mHoverColor, mCurrentFilter);
1243a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal            mDrawable.setColorFilter(new ColorMatrixColorFilter(mCurrentFilter));
125fe0d1f2458e6a442613b070ae549124a4780e759Sunny Goyal            setTextColor(mHoverColor);
126fe0d1f2458e6a442613b070ae549124a4780e759Sunny Goyal        }
12761fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    }
12861fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung
129fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal    @Override
130fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal    public void onDragOver(DragObject d) {
131043f2af567178b82b0b41f12d379e7dd12da2936Winson Chung        // Do nothing
132043f2af567178b82b0b41f12d379e7dd12da2936Winson Chung    }
133043f2af567178b82b0b41f12d379e7dd12da2936Winson Chung
1343a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal	protected void resetHoverColor() {
135fe0d1f2458e6a442613b070ae549124a4780e759Sunny Goyal        if (Utilities.isLmpOrAbove()) {
136fe0d1f2458e6a442613b070ae549124a4780e759Sunny Goyal            animateTextColor(mOriginalTextColor.getDefaultColor());
137fe0d1f2458e6a442613b070ae549124a4780e759Sunny Goyal        } else {
1383a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal            mDrawable.setColorFilter(null);
139fe0d1f2458e6a442613b070ae549124a4780e759Sunny Goyal            setTextColor(mOriginalTextColor);
140fe0d1f2458e6a442613b070ae549124a4780e759Sunny Goyal        }
141fe0d1f2458e6a442613b070ae549124a4780e759Sunny Goyal    }
142fe0d1f2458e6a442613b070ae549124a4780e759Sunny Goyal
143fe0d1f2458e6a442613b070ae549124a4780e759Sunny Goyal    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
144fe0d1f2458e6a442613b070ae549124a4780e759Sunny Goyal    private void animateTextColor(int targetColor) {
145fe0d1f2458e6a442613b070ae549124a4780e759Sunny Goyal        if (mCurrentColorAnim != null) {
146fe0d1f2458e6a442613b070ae549124a4780e759Sunny Goyal            mCurrentColorAnim.cancel();
147fe0d1f2458e6a442613b070ae549124a4780e759Sunny Goyal        }
1483a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal
1493a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal        mCurrentColorAnim = new AnimatorSet();
150fe0d1f2458e6a442613b070ae549124a4780e759Sunny Goyal        mCurrentColorAnim.setDuration(DragView.COLOR_CHANGE_DURATION);
1513a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal
1523a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal        if (mSrcFilter == null) {
1533a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal            mSrcFilter = new ColorMatrix();
1543a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal            mDstFilter = new ColorMatrix();
1553a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal            mCurrentFilter = new ColorMatrix();
1563a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal        }
1573a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal
1583a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal        DragView.setColorScale(getTextColor(), mSrcFilter);
1593a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal        DragView.setColorScale(targetColor, mDstFilter);
1603a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal        ValueAnimator anim1 = ValueAnimator.ofObject(
1613a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal                new FloatArrayEvaluator(mCurrentFilter.getArray()),
1623a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal                mSrcFilter.getArray(), mDstFilter.getArray());
1633a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal        anim1.addUpdateListener(new AnimatorUpdateListener() {
1643a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal
1653a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal            @Override
1663a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal            public void onAnimationUpdate(ValueAnimator animation) {
1673a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal                mDrawable.setColorFilter(new ColorMatrixColorFilter(mCurrentFilter));
1683a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal                invalidate();
1693a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal            }
1703a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal        });
1713a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal
1723a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal        mCurrentColorAnim.play(anim1);
1733a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal        mCurrentColorAnim.play(ObjectAnimator.ofArgb(this, "textColor", targetColor));
174fe0d1f2458e6a442613b070ae549124a4780e759Sunny Goyal        mCurrentColorAnim.start();
17561fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    }
17661fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung
177fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal    @Override
178fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal    public final void onDragExit(DragObject d) {
179fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal        if (!d.dragComplete) {
180fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal            d.dragView.setColor(0);
181fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal            resetHoverColor();
182fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal        } else {
183fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal            // Restore the hover color
184fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal            d.dragView.setColor(mHoverColor);
185fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal        }
18661fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    }
18761fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung
1883a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal	@Override
189fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal    public final void onDragStart(DragSource source, Object info, int dragAction) {
190fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal        mActive = supportsDrop(source, info);
1913a644ed1ce12554fcdf5c499e959bda986d10551Sunny Goyal        mDrawable.setColorFilter(null);
192fe0d1f2458e6a442613b070ae549124a4780e759Sunny Goyal        if (mCurrentColorAnim != null) {
193fe0d1f2458e6a442613b070ae549124a4780e759Sunny Goyal            mCurrentColorAnim.cancel();
194fe0d1f2458e6a442613b070ae549124a4780e759Sunny Goyal            mCurrentColorAnim = null;
195fe0d1f2458e6a442613b070ae549124a4780e759Sunny Goyal        }
196fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal        setTextColor(mOriginalTextColor);
197fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal        ((ViewGroup) getParent()).setVisibility(mActive ? View.VISIBLE : View.GONE);
19861fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    }
19961fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung
200fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal    @Override
201fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal    public final boolean acceptDrop(DragObject dragObject) {
202fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal        return supportsDrop(dragObject.dragSource, dragObject.dragInfo);
20361fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    }
20461fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung
205fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal    protected abstract boolean supportsDrop(DragSource source, Object info);
206fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal
207fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal    @Override
20861fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    public boolean isDropEnabled() {
20961fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung        return mActive;
21061fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    }
21161fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung
212fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal    @Override
21361fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    public void onDragEnd() {
214fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal        mActive = false;
21561fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    }
21661fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung
217fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal    /**
218fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal     * On drop animate the dropView to the icon.
219fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal     */
220fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal    @Override
221fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal    public void onDrop(final DragObject d) {
222fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal        final DragLayer dragLayer = mLauncher.getDragLayer();
223fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal        final Rect from = new Rect();
224fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal        dragLayer.getViewRectRelativeToSelf(d.dragView, from);
225fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal
226fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal        int width = mDrawable.getIntrinsicWidth();
227fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal        int height = mDrawable.getIntrinsicHeight();
228fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal        final Rect to = getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(),
229fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal                width, height);
230fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal        final float scale = (float) to.width() / from.width();
231fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal        mSearchDropTargetBar.deferOnDragEnd();
232fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal
233fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal        Runnable onAnimationEndRunnable = new Runnable() {
234fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal            @Override
235fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal            public void run() {
236fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal                completeDrop(d);
237fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal                mSearchDropTargetBar.onDragEnd();
238fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal                mLauncher.exitSpringLoadedDragModeDelayed(true, 0, null);
239fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal            }
240fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal        };
241fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal        dragLayer.animateView(d.dragView, from, to, scale, 1f, 1f, 0.1f, 0.1f,
242fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal                DRAG_VIEW_DROP_DURATION, new DecelerateInterpolator(2),
243fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal                new LinearInterpolator(), onAnimationEndRunnable,
244fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal                DragLayer.ANIMATION_END_DISAPPEAR, null);
245fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal    }
246fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal
247e9b651eef1b9f3647eba94f833bff3fc52f5956bSunny Goyal    @Override
248e9b651eef1b9f3647eba94f833bff3fc52f5956bSunny Goyal    public void prepareAccessibilityDrop() { }
249e9b651eef1b9f3647eba94f833bff3fc52f5956bSunny Goyal
250fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal    @Thunk abstract void completeDrop(DragObject d);
251fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal
25261fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    @Override
2537d30a37007bac318db1c9af47a9af12d348042a5Adam Cohen    public void getHitRectRelativeToDragLayer(android.graphics.Rect outRect) {
254a62e9fd95ae10cff4676f2cc1f4d68334ca27a0bWinson Chung        super.getHitRect(outRect);
255a62e9fd95ae10cff4676f2cc1f4d68334ca27a0bWinson Chung        outRect.bottom += mBottomDragPadding;
256156ab5b22e45b36a1c5edbe5accccf6aefcb4907Winson Chung
257156ab5b22e45b36a1c5edbe5accccf6aefcb4907Winson Chung        int[] coords = new int[2];
258156ab5b22e45b36a1c5edbe5accccf6aefcb4907Winson Chung        mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(this, coords);
259156ab5b22e45b36a1c5edbe5accccf6aefcb4907Winson Chung        outRect.offsetTo(coords[0], coords[1]);
260a62e9fd95ae10cff4676f2cc1f4d68334ca27a0bWinson Chung    }
261a62e9fd95ae10cff4676f2cc1f4d68334ca27a0bWinson Chung
262fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal    protected Rect getIconRect(int viewWidth, int viewHeight, int drawableWidth, int drawableHeight) {
26361967cb17f8fa9ee01f93e9f25a1074559b54cf3Winson Chung        DragLayer dragLayer = mLauncher.getDragLayer();
26461967cb17f8fa9ee01f93e9f25a1074559b54cf3Winson Chung
26561967cb17f8fa9ee01f93e9f25a1074559b54cf3Winson Chung        // Find the rect to animate to (the view is center aligned)
26661967cb17f8fa9ee01f93e9f25a1074559b54cf3Winson Chung        Rect to = new Rect();
26761967cb17f8fa9ee01f93e9f25a1074559b54cf3Winson Chung        dragLayer.getViewRectRelativeToSelf(this, to);
268d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio
269d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio        final int width = drawableWidth;
270d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio        final int height = drawableHeight;
271d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio
272d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio        final int left;
273d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio        final int right;
274d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio
2757066003b2032a49ae5e59dab9b706259bdeb7e6eSunny Goyal        if (Utilities.isRtl(getResources())) {
276d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio            right = to.right - getPaddingRight();
277d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio            left = right - width;
278d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio        } else {
279d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio            left = to.left + getPaddingLeft();
280d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio            right = left + width;
281d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio        }
282d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio
283d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio        final int top = to.top + (getMeasuredHeight() - height) / 2;
284d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio        final int bottom = top +  height;
285d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio
286d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio        to.set(left, top, right, bottom);
28761967cb17f8fa9ee01f93e9f25a1074559b54cf3Winson Chung
28861967cb17f8fa9ee01f93e9f25a1074559b54cf3Winson Chung        // Center the destination rect about the trash icon
289d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio        final int xOffset = (int) -(viewWidth - width) / 2;
290d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio        final int yOffset = (int) -(viewHeight - height) / 2;
29161967cb17f8fa9ee01f93e9f25a1074559b54cf3Winson Chung        to.offset(xOffset, yOffset);
29261967cb17f8fa9ee01f93e9f25a1074559b54cf3Winson Chung
29361967cb17f8fa9ee01f93e9f25a1074559b54cf3Winson Chung        return to;
29461967cb17f8fa9ee01f93e9f25a1074559b54cf3Winson Chung    }
29561967cb17f8fa9ee01f93e9f25a1074559b54cf3Winson Chung
296fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal    @Override
2978dfcba4af7a7ece09e8c7d96053e54f3a383e905Adam Cohen    public void getLocationInDragLayer(int[] loc) {
2988dfcba4af7a7ece09e8c7d96053e54f3a383e905Adam Cohen        mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
2998dfcba4af7a7ece09e8c7d96053e54f3a383e905Adam Cohen    }
3001a70cef9884270f2f0a760f079a10fdfb1544c98Sunny Goyal
3011a70cef9884270f2f0a760f079a10fdfb1544c98Sunny Goyal    public void enableAccessibleDrag(boolean enable) {
3021a70cef9884270f2f0a760f079a10fdfb1544c98Sunny Goyal        setOnClickListener(enable ? this : null);
3031a70cef9884270f2f0a760f079a10fdfb1544c98Sunny Goyal    }
3041a70cef9884270f2f0a760f079a10fdfb1544c98Sunny Goyal
3051a70cef9884270f2f0a760f079a10fdfb1544c98Sunny Goyal    protected String getAccessibilityDropConfirmation() {
3061a70cef9884270f2f0a760f079a10fdfb1544c98Sunny Goyal        return null;
3071a70cef9884270f2f0a760f079a10fdfb1544c98Sunny Goyal    }
3081a70cef9884270f2f0a760f079a10fdfb1544c98Sunny Goyal
3091a70cef9884270f2f0a760f079a10fdfb1544c98Sunny Goyal    @Override
3101a70cef9884270f2f0a760f079a10fdfb1544c98Sunny Goyal    public void onClick(View v) {
3111a70cef9884270f2f0a760f079a10fdfb1544c98Sunny Goyal        LauncherAppState.getInstance().getAccessibilityDelegate()
3121a70cef9884270f2f0a760f079a10fdfb1544c98Sunny Goyal            .handleAccessibleDrop(this, null, getAccessibilityDropConfirmation());
3131a70cef9884270f2f0a760f079a10fdfb1544c98Sunny Goyal    }
314fe0d1f2458e6a442613b070ae549124a4780e759Sunny Goyal
315fe0d1f2458e6a442613b070ae549124a4780e759Sunny Goyal    public int getTextColor() {
316fe0d1f2458e6a442613b070ae549124a4780e759Sunny Goyal        return getTextColors().getDefaultColor();
317fe0d1f2458e6a442613b070ae549124a4780e759Sunny Goyal    }
31861fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung}
319