blob: ff0813add0caa3d30b8f3afe4e75dd6e39b0e9dd [file] [log] [blame]
Winson Chung61fa4192011-06-12 15:15:29 -07001/*
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
17package com.android.launcher2;
18
19import android.content.Context;
Winson Chunga62e9fd2011-07-11 15:20:48 -070020import android.content.res.Resources;
Winson Chung043f2af2012-03-01 16:09:54 -080021import android.graphics.PointF;
Winson Chung61967cb2012-02-28 18:11:33 -080022import android.graphics.Rect;
Winson Chung947245b2012-05-15 16:34:19 -070023import android.graphics.drawable.Drawable;
Winson Chung61fa4192011-06-12 15:15:29 -070024import android.util.AttributeSet;
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -080025import android.view.View;
Adam Cohend4d7aa52011-07-19 21:47:37 -070026import android.widget.TextView;
Winson Chung61fa4192011-06-12 15:15:29 -070027
28import com.android.launcher.R;
29
30
31/**
32 * Implements a DropTarget.
33 */
Winson Chunga6427b12011-07-27 10:53:39 -070034public class ButtonDropTarget extends TextView implements DropTarget, DragController.DragListener {
Winson Chung61fa4192011-06-12 15:15:29 -070035
36 protected final int mTransitionDuration;
37
38 protected Launcher mLauncher;
Winson Chunga62e9fd2011-07-11 15:20:48 -070039 private int mBottomDragPadding;
Adam Cohend4d7aa52011-07-19 21:47:37 -070040 protected TextView mText;
41 protected SearchDropTargetBar mSearchDropTargetBar;
Winson Chung61fa4192011-06-12 15:15:29 -070042
43 /** Whether this drop target is active for the current drag */
44 protected boolean mActive;
45
46 /** The paint applied to the drag view on hover */
Winson Chung61967cb2012-02-28 18:11:33 -080047 protected int mHoverColor = 0;
Winson Chung61fa4192011-06-12 15:15:29 -070048
49 public ButtonDropTarget(Context context, AttributeSet attrs) {
50 this(context, attrs, 0);
51 }
52
53 public ButtonDropTarget(Context context, AttributeSet attrs, int defStyle) {
54 super(context, attrs, defStyle);
55
Winson Chunga62e9fd2011-07-11 15:20:48 -070056 Resources r = getResources();
57 mTransitionDuration = r.getInteger(R.integer.config_dropTargetBgTransitionDuration);
58 mBottomDragPadding = r.getDimensionPixelSize(R.dimen.drop_target_drag_padding);
Winson Chung61fa4192011-06-12 15:15:29 -070059 }
60
61 void setLauncher(Launcher launcher) {
62 mLauncher = launcher;
63 }
64
65 public boolean acceptDrop(DragObject d) {
66 return false;
67 }
68
Adam Cohend4d7aa52011-07-19 21:47:37 -070069 public void setSearchDropTargetBar(SearchDropTargetBar searchDropTargetBar) {
70 mSearchDropTargetBar = searchDropTargetBar;
71 }
72
Winson Chung947245b2012-05-15 16:34:19 -070073 protected Drawable getCurrentDrawable() {
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -080074 Drawable[] drawables = getCompoundDrawablesRelative();
Winson Chung947245b2012-05-15 16:34:19 -070075 for (int i = 0; i < drawables.length; ++i) {
76 if (drawables[i] != null) {
77 return drawables[i];
78 }
79 }
80 return null;
81 }
82
Winson Chung61fa4192011-06-12 15:15:29 -070083 public void onDrop(DragObject d) {
Winson Chung61fa4192011-06-12 15:15:29 -070084 }
85
Winson Chung043f2af2012-03-01 16:09:54 -080086 public void onFlingToDelete(DragObject d, int x, int y, PointF vec) {
87 // Do nothing
88 }
89
Winson Chung61fa4192011-06-12 15:15:29 -070090 public void onDragEnter(DragObject d) {
Winson Chung61967cb2012-02-28 18:11:33 -080091 d.dragView.setColor(mHoverColor);
Winson Chung61fa4192011-06-12 15:15:29 -070092 }
93
94 public void onDragOver(DragObject d) {
95 // Do nothing
96 }
97
98 public void onDragExit(DragObject d) {
Winson Chung61967cb2012-02-28 18:11:33 -080099 d.dragView.setColor(0);
Winson Chung61fa4192011-06-12 15:15:29 -0700100 }
101
102 public void onDragStart(DragSource source, Object info, int dragAction) {
103 // Do nothing
104 }
105
106 public boolean isDropEnabled() {
107 return mActive;
108 }
109
110 public void onDragEnd() {
111 // Do nothing
112 }
113
114 @Override
Winson Chunga62e9fd2011-07-11 15:20:48 -0700115 public void getHitRect(android.graphics.Rect outRect) {
116 super.getHitRect(outRect);
117 outRect.bottom += mBottomDragPadding;
118 }
119
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800120 private boolean isRtl() {
121 return (getLayoutDirection() == View.LAYOUT_DIRECTION_RTL);
122 }
123
124 Rect getIconRect(int viewWidth, int viewHeight, int drawableWidth, int drawableHeight) {
Winson Chung61967cb2012-02-28 18:11:33 -0800125 DragLayer dragLayer = mLauncher.getDragLayer();
126
127 // Find the rect to animate to (the view is center aligned)
128 Rect to = new Rect();
129 dragLayer.getViewRectRelativeToSelf(this, to);
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800130
131 final int width = drawableWidth;
132 final int height = drawableHeight;
133
134 final int left;
135 final int right;
136
137 if (isRtl()) {
138 right = to.right - getPaddingRight();
139 left = right - width;
140 } else {
141 left = to.left + getPaddingLeft();
142 right = left + width;
143 }
144
145 final int top = to.top + (getMeasuredHeight() - height) / 2;
146 final int bottom = top + height;
147
148 to.set(left, top, right, bottom);
Winson Chung61967cb2012-02-28 18:11:33 -0800149
150 // Center the destination rect about the trash icon
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800151 final int xOffset = (int) -(viewWidth - width) / 2;
152 final int yOffset = (int) -(viewHeight - height) / 2;
Winson Chung61967cb2012-02-28 18:11:33 -0800153 to.offset(xOffset, yOffset);
154
155 return to;
156 }
157
Winson Chunga62e9fd2011-07-11 15:20:48 -0700158 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700159 public DropTarget getDropTargetDelegate(DragObject d) {
160 return null;
161 }
Adam Cohen8dfcba42011-07-07 16:38:18 -0700162
163 public void getLocationInDragLayer(int[] loc) {
164 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
165 }
Winson Chung61fa4192011-06-12 15:15:29 -0700166}