blob: bfc46cf422e96fd763dd55f673068ad13b2b5897 [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 /**
Michael Jurkab8e14472010-12-20 16:06:10 -080038 * If true, this View responsible for managing its own visibility, and that of its overlapping
39 * views. This is generally the case, but it will be set to false when this is part of the
Winson Chung760e5372010-12-15 13:14:23 -080040 * 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
Michael Jurkab8e14472010-12-20 16:06:10 -080047 /** The views that this view should appear in the place of. */
48 protected View[] mOverlappingViews = null;
Winson Chung760e5372010-12-15 13:14:23 -080049
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
Michael Jurkab8e14472010-12-20 16:06:10 -080076 void setOverlappingView(View view) {
77 mOverlappingViews = new View[] { view };
78 }
79
80 void setOverlappingViews(View[] views) {
81 mOverlappingViews = views;
Winson Chung760e5372010-12-15 13:14:23 -080082 }
83
84 void setDragAndDropEnabled(boolean enabled) {
85 mDragAndDropEnabled = enabled;
86 }
87
88 public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset,
89 DragView dragView, Object dragInfo) {
90 return false;
91 }
92
93 public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset,
94 DragView dragView, Object dragInfo) {
95 // Do nothing
96 }
97
98 public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset,
99 DragView dragView, Object dragInfo) {
100 if (mDragAndDropEnabled) {
101 dragView.setPaint(mHoverPaint);
102 }
103 }
104
105 public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset,
106 DragView dragView, Object dragInfo) {
107 // Do nothing
108 }
109
110 public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset,
111 DragView dragView, Object dragInfo) {
112 if (mDragAndDropEnabled) {
113 dragView.setPaint(null);
114 }
115 }
116
117 public void onDragStart(DragSource source, Object info, int dragAction) {
118 // Do nothing
119 }
120
121 public boolean isDropEnabled() {
122 return mDragAndDropEnabled && mActive;
123 }
124
125 public void onDragEnd() {
126 // Do nothing
127 }
128
129 @Override
130 public void getHitRect(Rect outRect) {
131 super.getHitRect(outRect);
132 if (LauncherApplication.isScreenXLarge()) {
Winson Chungbe5212b2010-12-20 11:36:33 -0800133 outRect.top -= mDragPadding[0];
134 outRect.right += mDragPadding[1];
135 outRect.bottom += mDragPadding[2];
136 outRect.left -= mDragPadding[3];
Winson Chung760e5372010-12-15 13:14:23 -0800137 }
138 }
139
140 @Override
141 public DropTarget getDropTargetDelegate(DragSource source, int x, int y, int xOffset,
142 int yOffset, DragView dragView, Object dragInfo) {
143 return null;
144 }
145}