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
1761fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chungpackage com.android.launcher2;
1861fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung
1961fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chungimport android.content.Context;
20a62e9fd95ae10cff4676f2cc1f4d68334ca27a0bWinson Chungimport android.content.res.Resources;
21043f2af567178b82b0b41f12d379e7dd12da2936Winson Chungimport android.graphics.PointF;
2261967cb17f8fa9ee01f93e9f25a1074559b54cf3Winson Chungimport android.graphics.Rect;
23947245ba8ad74aee3dadd02693514765dbb4b5a2Winson Chungimport android.graphics.drawable.Drawable;
2461fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chungimport android.util.AttributeSet;
25d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglioimport android.view.View;
26d4d7aa551ffdc80d810ff970fa72a6509960401eAdam Cohenimport android.widget.TextView;
2761fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung
2861fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chungimport com.android.launcher.R;
2961fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung
3061fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung
3161fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung/**
3261fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung * Implements a DropTarget.
3361fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung */
34a6427b15c18d5b8f3078f553d78f8432de9f46e9Winson Chungpublic class ButtonDropTarget extends TextView implements DropTarget, DragController.DragListener {
3561fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung
3661fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    protected final int mTransitionDuration;
3761fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung
3861fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    protected Launcher mLauncher;
39a62e9fd95ae10cff4676f2cc1f4d68334ca27a0bWinson Chung    private int mBottomDragPadding;
40d4d7aa551ffdc80d810ff970fa72a6509960401eAdam Cohen    protected TextView mText;
41d4d7aa551ffdc80d810ff970fa72a6509960401eAdam Cohen    protected SearchDropTargetBar mSearchDropTargetBar;
4261fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung
4361fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    /** Whether this drop target is active for the current drag */
4461fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    protected boolean mActive;
4561fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung
4661fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    /** The paint applied to the drag view on hover */
4761967cb17f8fa9ee01f93e9f25a1074559b54cf3Winson Chung    protected int mHoverColor = 0;
4861fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung
4961fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    public ButtonDropTarget(Context context, AttributeSet attrs) {
5061fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung        this(context, attrs, 0);
5161fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    }
5261fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung
5361fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    public ButtonDropTarget(Context context, AttributeSet attrs, int defStyle) {
5461fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung        super(context, attrs, defStyle);
5561fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung
56a62e9fd95ae10cff4676f2cc1f4d68334ca27a0bWinson Chung        Resources r = getResources();
57a62e9fd95ae10cff4676f2cc1f4d68334ca27a0bWinson Chung        mTransitionDuration = r.getInteger(R.integer.config_dropTargetBgTransitionDuration);
58a62e9fd95ae10cff4676f2cc1f4d68334ca27a0bWinson Chung        mBottomDragPadding = r.getDimensionPixelSize(R.dimen.drop_target_drag_padding);
5961fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    }
6061fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung
6161fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    void setLauncher(Launcher launcher) {
6261fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung        mLauncher = launcher;
6361fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    }
6461fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung
6561fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    public boolean acceptDrop(DragObject d) {
6661fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung        return false;
6761fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    }
6861fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung
69d4d7aa551ffdc80d810ff970fa72a6509960401eAdam Cohen    public void setSearchDropTargetBar(SearchDropTargetBar searchDropTargetBar) {
70d4d7aa551ffdc80d810ff970fa72a6509960401eAdam Cohen        mSearchDropTargetBar = searchDropTargetBar;
71d4d7aa551ffdc80d810ff970fa72a6509960401eAdam Cohen    }
72d4d7aa551ffdc80d810ff970fa72a6509960401eAdam Cohen
73947245ba8ad74aee3dadd02693514765dbb4b5a2Winson Chung    protected Drawable getCurrentDrawable() {
74d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio        Drawable[] drawables = getCompoundDrawablesRelative();
75947245ba8ad74aee3dadd02693514765dbb4b5a2Winson Chung        for (int i = 0; i < drawables.length; ++i) {
76947245ba8ad74aee3dadd02693514765dbb4b5a2Winson Chung            if (drawables[i] != null) {
77947245ba8ad74aee3dadd02693514765dbb4b5a2Winson Chung                return drawables[i];
78947245ba8ad74aee3dadd02693514765dbb4b5a2Winson Chung            }
79947245ba8ad74aee3dadd02693514765dbb4b5a2Winson Chung        }
80947245ba8ad74aee3dadd02693514765dbb4b5a2Winson Chung        return null;
81947245ba8ad74aee3dadd02693514765dbb4b5a2Winson Chung    }
82947245ba8ad74aee3dadd02693514765dbb4b5a2Winson Chung
8361fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    public void onDrop(DragObject d) {
8461fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    }
8561fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung
86043f2af567178b82b0b41f12d379e7dd12da2936Winson Chung    public void onFlingToDelete(DragObject d, int x, int y, PointF vec) {
87043f2af567178b82b0b41f12d379e7dd12da2936Winson Chung        // Do nothing
88043f2af567178b82b0b41f12d379e7dd12da2936Winson Chung    }
89043f2af567178b82b0b41f12d379e7dd12da2936Winson Chung
9061fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    public void onDragEnter(DragObject d) {
9161967cb17f8fa9ee01f93e9f25a1074559b54cf3Winson Chung        d.dragView.setColor(mHoverColor);
9261fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    }
9361fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung
9461fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    public void onDragOver(DragObject d) {
9561fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung        // Do nothing
9661fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    }
9761fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung
9861fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    public void onDragExit(DragObject d) {
9961967cb17f8fa9ee01f93e9f25a1074559b54cf3Winson Chung        d.dragView.setColor(0);
10061fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    }
10161fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung
10261fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    public void onDragStart(DragSource source, Object info, int dragAction) {
10361fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung        // Do nothing
10461fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    }
10561fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung
10661fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    public boolean isDropEnabled() {
10761fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung        return mActive;
10861fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    }
10961fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung
11061fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    public void onDragEnd() {
11161fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung        // Do nothing
11261fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    }
11361fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung
11461fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    @Override
115a62e9fd95ae10cff4676f2cc1f4d68334ca27a0bWinson Chung    public void getHitRect(android.graphics.Rect outRect) {
116a62e9fd95ae10cff4676f2cc1f4d68334ca27a0bWinson Chung        super.getHitRect(outRect);
117a62e9fd95ae10cff4676f2cc1f4d68334ca27a0bWinson Chung        outRect.bottom += mBottomDragPadding;
118a62e9fd95ae10cff4676f2cc1f4d68334ca27a0bWinson Chung    }
119a62e9fd95ae10cff4676f2cc1f4d68334ca27a0bWinson Chung
120d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio    private boolean isRtl() {
121d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio        return (getLayoutDirection() == View.LAYOUT_DIRECTION_RTL);
122d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio    }
123d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio
124d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio    Rect getIconRect(int viewWidth, int viewHeight, int drawableWidth, int drawableHeight) {
12561967cb17f8fa9ee01f93e9f25a1074559b54cf3Winson Chung        DragLayer dragLayer = mLauncher.getDragLayer();
12661967cb17f8fa9ee01f93e9f25a1074559b54cf3Winson Chung
12761967cb17f8fa9ee01f93e9f25a1074559b54cf3Winson Chung        // Find the rect to animate to (the view is center aligned)
12861967cb17f8fa9ee01f93e9f25a1074559b54cf3Winson Chung        Rect to = new Rect();
12961967cb17f8fa9ee01f93e9f25a1074559b54cf3Winson Chung        dragLayer.getViewRectRelativeToSelf(this, to);
130d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio
131d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio        final int width = drawableWidth;
132d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio        final int height = drawableHeight;
133d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio
134d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio        final int left;
135d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio        final int right;
136d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio
137d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio        if (isRtl()) {
138d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio            right = to.right - getPaddingRight();
139d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio            left = right - width;
140d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio        } else {
141d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio            left = to.left + getPaddingLeft();
142d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio            right = left + width;
143d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio        }
144d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio
145d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio        final int top = to.top + (getMeasuredHeight() - height) / 2;
146d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio        final int bottom = top +  height;
147d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio
148d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio        to.set(left, top, right, bottom);
14961967cb17f8fa9ee01f93e9f25a1074559b54cf3Winson Chung
15061967cb17f8fa9ee01f93e9f25a1074559b54cf3Winson Chung        // Center the destination rect about the trash icon
151d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio        final int xOffset = (int) -(viewWidth - width) / 2;
152d6a33c6f348d1316e0fdc519eda43468fcdcbfe7Fabrice Di Meglio        final int yOffset = (int) -(viewHeight - height) / 2;
15361967cb17f8fa9ee01f93e9f25a1074559b54cf3Winson Chung        to.offset(xOffset, yOffset);
15461967cb17f8fa9ee01f93e9f25a1074559b54cf3Winson Chung
15561967cb17f8fa9ee01f93e9f25a1074559b54cf3Winson Chung        return to;
15661967cb17f8fa9ee01f93e9f25a1074559b54cf3Winson Chung    }
15761967cb17f8fa9ee01f93e9f25a1074559b54cf3Winson Chung
158a62e9fd95ae10cff4676f2cc1f4d68334ca27a0bWinson Chung    @Override
15961fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    public DropTarget getDropTargetDelegate(DragObject d) {
16061fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung        return null;
16161fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung    }
1628dfcba4af7a7ece09e8c7d96053e54f3a383e905Adam Cohen
1638dfcba4af7a7ece09e8c7d96053e54f3a383e905Adam Cohen    public void getLocationInDragLayer(int[] loc) {
1648dfcba4af7a7ece09e8c7d96053e54f3a383e905Adam Cohen        mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
1658dfcba4af7a7ece09e8c7d96053e54f3a383e905Adam Cohen    }
16661fa4197c4316bb0f9b05fcefb676f86197a2273Winson Chung}
167