blob: ec08c1e81e85cc1c8c7aa89bffd101964a897f99 [file] [log] [blame]
Winson Chung760e5372010-12-15 13:14:23 -08001/*
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
Winson Chungbe5212b2010-12-20 11:36:33 -080019import com.android.launcher.R;
20
Winson Chung760e5372010-12-15 13:14:23 -080021import android.content.Context;
Winson Chungbe5212b2010-12-20 11:36:33 -080022import android.graphics.Canvas;
23import android.graphics.Color;
Winson Chung760e5372010-12-15 13:14:23 -080024import android.graphics.Paint;
Winson Chung760e5372010-12-15 13:14:23 -080025import android.graphics.Rect;
26import android.util.AttributeSet;
27import android.view.View;
28import android.widget.ImageView;
29
Winson Chung760e5372010-12-15 13:14:23 -080030/**
31 * Implements a DropTarget which allows applications to be dropped on it,
32 * in order to launch the application info for that app.
33 */
34public class IconDropTarget extends ImageView implements DropTarget, DragController.DragListener {
35 protected Launcher mLauncher;
36
37 /**
38 * If true, this View responsible for managing its own visibility, and that of its handle.
39 * This is generally the case, but it will be set to false when this is part of the
40 * Contextual Action Bar.
41 */
42 protected boolean mDragAndDropEnabled;
43
44 /** Whether this drop target is active for the current drag */
45 protected boolean mActive;
46
47 /** The view that this view should appear in the place of. */
48 protected View mHandle = null;
49
50 /** The paint applied to the drag view on hover */
51 protected final Paint mHoverPaint = new Paint();
52
Winson Chungbe5212b2010-12-20 11:36:33 -080053 /** Drag zone padding [T, R, B, L] */
54 protected final int mDragPadding[] = new int[4];
Winson Chung760e5372010-12-15 13:14:23 -080055
56 public IconDropTarget(Context context, AttributeSet attrs) {
57 this(context, attrs, 0);
58 }
59
60 public IconDropTarget(Context context, AttributeSet attrs, int defStyle) {
61 super(context, attrs, defStyle);
62 mDragAndDropEnabled = true;
63 }
64
Winson Chungbe5212b2010-12-20 11:36:33 -080065 protected void setDragPadding(int t, int r, int b, int l) {
66 mDragPadding[0] = t;
67 mDragPadding[1] = r;
68 mDragPadding[2] = b;
69 mDragPadding[3] = l;
70 }
71
Winson Chung760e5372010-12-15 13:14:23 -080072 void setLauncher(Launcher launcher) {
73 mLauncher = launcher;
74 }
75
76 void setHandle(View view) {
77 mHandle = view;
78 }
79
80 void setDragAndDropEnabled(boolean enabled) {
81 mDragAndDropEnabled = enabled;
82 }
83
84 public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset,
85 DragView dragView, Object dragInfo) {
86 return false;
87 }
88
89 public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset,
90 DragView dragView, Object dragInfo) {
91 // Do nothing
92 }
93
94 public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset,
95 DragView dragView, Object dragInfo) {
96 if (mDragAndDropEnabled) {
97 dragView.setPaint(mHoverPaint);
98 }
99 }
100
101 public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset,
102 DragView dragView, Object dragInfo) {
103 // Do nothing
104 }
105
106 public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset,
107 DragView dragView, Object dragInfo) {
108 if (mDragAndDropEnabled) {
109 dragView.setPaint(null);
110 }
111 }
112
113 public void onDragStart(DragSource source, Object info, int dragAction) {
114 // Do nothing
115 }
116
117 public boolean isDropEnabled() {
118 return mDragAndDropEnabled && mActive;
119 }
120
121 public void onDragEnd() {
122 // Do nothing
123 }
124
125 @Override
126 public void getHitRect(Rect outRect) {
127 super.getHitRect(outRect);
128 if (LauncherApplication.isScreenXLarge()) {
Winson Chungbe5212b2010-12-20 11:36:33 -0800129 outRect.top -= mDragPadding[0];
130 outRect.right += mDragPadding[1];
131 outRect.bottom += mDragPadding[2];
132 outRect.left -= mDragPadding[3];
Winson Chung760e5372010-12-15 13:14:23 -0800133 }
134 }
135
136 @Override
137 public DropTarget getDropTargetDelegate(DragSource source, int x, int y, int xOffset,
138 int yOffset, DragView dragView, Object dragInfo) {
139 return null;
140 }
141}