2011-06-12 15:15:29 -07:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2010 The Android Open Source Project
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
2013-06-05 22:57:57 -04:00
|
|
|
package com.android.launcher3;
|
2011-06-12 15:15:29 -07:00
|
|
|
|
|
|
|
|
import android.content.Context;
|
2011-07-11 15:20:48 -07:00
|
|
|
import android.content.res.Resources;
|
2012-03-01 16:09:54 -08:00
|
|
|
import android.graphics.PointF;
|
2012-02-28 18:11:33 -08:00
|
|
|
import android.graphics.Rect;
|
2012-05-15 16:34:19 -07:00
|
|
|
import android.graphics.drawable.Drawable;
|
2011-06-12 15:15:29 -07:00
|
|
|
import android.util.AttributeSet;
|
2013-02-06 15:40:46 -08:00
|
|
|
import android.view.View;
|
2011-07-19 21:47:37 -07:00
|
|
|
import android.widget.TextView;
|
2011-06-12 15:15:29 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Implements a DropTarget.
|
|
|
|
|
*/
|
2011-07-27 10:53:39 -07:00
|
|
|
public class ButtonDropTarget extends TextView implements DropTarget, DragController.DragListener {
|
2011-06-12 15:15:29 -07:00
|
|
|
|
|
|
|
|
protected final int mTransitionDuration;
|
|
|
|
|
|
|
|
|
|
protected Launcher mLauncher;
|
2011-07-11 15:20:48 -07:00
|
|
|
private int mBottomDragPadding;
|
2011-07-19 21:47:37 -07:00
|
|
|
protected TextView mText;
|
|
|
|
|
protected SearchDropTargetBar mSearchDropTargetBar;
|
2011-06-12 15:15:29 -07:00
|
|
|
|
|
|
|
|
/** Whether this drop target is active for the current drag */
|
|
|
|
|
protected boolean mActive;
|
|
|
|
|
|
|
|
|
|
/** The paint applied to the drag view on hover */
|
2012-02-28 18:11:33 -08:00
|
|
|
protected int mHoverColor = 0;
|
2011-06-12 15:15:29 -07:00
|
|
|
|
|
|
|
|
public ButtonDropTarget(Context context, AttributeSet attrs) {
|
|
|
|
|
this(context, attrs, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ButtonDropTarget(Context context, AttributeSet attrs, int defStyle) {
|
|
|
|
|
super(context, attrs, defStyle);
|
|
|
|
|
|
2011-07-11 15:20:48 -07:00
|
|
|
Resources r = getResources();
|
|
|
|
|
mTransitionDuration = r.getInteger(R.integer.config_dropTargetBgTransitionDuration);
|
|
|
|
|
mBottomDragPadding = r.getDimensionPixelSize(R.dimen.drop_target_drag_padding);
|
2011-06-12 15:15:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void setLauncher(Launcher launcher) {
|
|
|
|
|
mLauncher = launcher;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean acceptDrop(DragObject d) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-19 21:47:37 -07:00
|
|
|
public void setSearchDropTargetBar(SearchDropTargetBar searchDropTargetBar) {
|
|
|
|
|
mSearchDropTargetBar = searchDropTargetBar;
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-15 16:34:19 -07:00
|
|
|
protected Drawable getCurrentDrawable() {
|
2013-02-06 15:40:46 -08:00
|
|
|
Drawable[] drawables = getCompoundDrawablesRelative();
|
2012-05-15 16:34:19 -07:00
|
|
|
for (int i = 0; i < drawables.length; ++i) {
|
|
|
|
|
if (drawables[i] != null) {
|
|
|
|
|
return drawables[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-12 15:15:29 -07:00
|
|
|
public void onDrop(DragObject d) {
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-01 16:09:54 -08:00
|
|
|
public void onFlingToDelete(DragObject d, int x, int y, PointF vec) {
|
|
|
|
|
// Do nothing
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-12 15:15:29 -07:00
|
|
|
public void onDragEnter(DragObject d) {
|
2012-02-28 18:11:33 -08:00
|
|
|
d.dragView.setColor(mHoverColor);
|
2011-06-12 15:15:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void onDragOver(DragObject d) {
|
|
|
|
|
// Do nothing
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void onDragExit(DragObject d) {
|
2012-02-28 18:11:33 -08:00
|
|
|
d.dragView.setColor(0);
|
2011-06-12 15:15:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void onDragStart(DragSource source, Object info, int dragAction) {
|
|
|
|
|
// Do nothing
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean isDropEnabled() {
|
|
|
|
|
return mActive;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void onDragEnd() {
|
|
|
|
|
// Do nothing
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-11 15:20:48 -07:00
|
|
|
@Override
|
2013-07-01 17:03:59 -07:00
|
|
|
public void getHitRectRelativeToDragLayer(android.graphics.Rect outRect) {
|
2011-07-11 15:20:48 -07:00
|
|
|
super.getHitRect(outRect);
|
|
|
|
|
outRect.bottom += mBottomDragPadding;
|
2013-07-12 14:14:16 -07:00
|
|
|
|
|
|
|
|
int[] coords = new int[2];
|
|
|
|
|
mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(this, coords);
|
|
|
|
|
outRect.offsetTo(coords[0], coords[1]);
|
2011-07-11 15:20:48 -07:00
|
|
|
}
|
|
|
|
|
|
2013-02-06 15:40:46 -08:00
|
|
|
private boolean isRtl() {
|
|
|
|
|
return (getLayoutDirection() == View.LAYOUT_DIRECTION_RTL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Rect getIconRect(int viewWidth, int viewHeight, int drawableWidth, int drawableHeight) {
|
2012-02-28 18:11:33 -08:00
|
|
|
DragLayer dragLayer = mLauncher.getDragLayer();
|
|
|
|
|
|
|
|
|
|
// Find the rect to animate to (the view is center aligned)
|
|
|
|
|
Rect to = new Rect();
|
|
|
|
|
dragLayer.getViewRectRelativeToSelf(this, to);
|
2013-02-06 15:40:46 -08:00
|
|
|
|
|
|
|
|
final int width = drawableWidth;
|
|
|
|
|
final int height = drawableHeight;
|
|
|
|
|
|
|
|
|
|
final int left;
|
|
|
|
|
final int right;
|
|
|
|
|
|
|
|
|
|
if (isRtl()) {
|
|
|
|
|
right = to.right - getPaddingRight();
|
|
|
|
|
left = right - width;
|
|
|
|
|
} else {
|
|
|
|
|
left = to.left + getPaddingLeft();
|
|
|
|
|
right = left + width;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final int top = to.top + (getMeasuredHeight() - height) / 2;
|
|
|
|
|
final int bottom = top + height;
|
|
|
|
|
|
|
|
|
|
to.set(left, top, right, bottom);
|
2012-02-28 18:11:33 -08:00
|
|
|
|
|
|
|
|
// Center the destination rect about the trash icon
|
2013-02-06 15:40:46 -08:00
|
|
|
final int xOffset = (int) -(viewWidth - width) / 2;
|
|
|
|
|
final int yOffset = (int) -(viewHeight - height) / 2;
|
2012-02-28 18:11:33 -08:00
|
|
|
to.offset(xOffset, yOffset);
|
|
|
|
|
|
|
|
|
|
return to;
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-07 16:38:18 -07:00
|
|
|
public void getLocationInDragLayer(int[] loc) {
|
|
|
|
|
mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
|
|
|
|
|
}
|
2011-06-12 15:15:29 -07:00
|
|
|
}
|