blob: c2922ab377d8df81db98b1090d524c3a6ae429fb [file] [log] [blame]
Patrick Dubroy4ed62782010-08-17 15:11:18 -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
Adam Lesinski6b879f02010-11-04 16:15:23 -070019import com.android.launcher.R;
20
Patrick Dubroybc6840b2010-09-01 11:54:27 -070021import android.content.ComponentName;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070022import android.content.Context;
23import android.graphics.Paint;
24import android.graphics.PorterDuff;
25import android.graphics.PorterDuffColorFilter;
Michael Jurka3adcb0c2010-10-15 18:07:21 -070026import android.graphics.Rect;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070027import android.util.AttributeSet;
28import android.view.View;
29import android.widget.ImageView;
30
31/**
32 * Implements a DropTarget which allows applications to be dropped on it,
33 * in order to launch the application info for that app.
34 */
35public class ApplicationInfoDropTarget extends ImageView implements DropTarget, DragController.DragListener {
36 private Launcher mLauncher;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070037 private boolean mActive = false;
38
Patrick Dubroydea9e932010-09-22 15:04:29 -070039 /**
40 * If true, this View responsible for managing its own visibility, and that of its handle.
41 * This is generally the case, but it will be set to false when this is part of the
42 * Contextual Action Bar.
43 */
44 private boolean mManageVisibility = true;
45
46 /** The view that this view should appear in the place of. */
47 private View mHandle = null;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070048
49 private final Paint mPaint = new Paint();
50
51 public ApplicationInfoDropTarget(Context context, AttributeSet attrs) {
52 this(context, attrs, 0);
53 }
54
55 public ApplicationInfoDropTarget(Context context, AttributeSet attrs, int defStyle) {
56 super(context, attrs, defStyle);
57 }
58
Patrick Dubroy4ed62782010-08-17 15:11:18 -070059 /**
60 * Set the color that will be used as a filter over objects dragged over this object.
61 */
62 public void setDragColor(int color) {
63 mPaint.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_ATOP));
64 }
65
66 public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset,
67 DragView dragView, Object dragInfo) {
68
69 // acceptDrop is called just before onDrop. We do the work here, rather than
70 // in onDrop, because it allows us to reject the drop (by returning false)
71 // so that the object being dragged isn't removed from the home screen.
72
Patrick Dubroybc6840b2010-09-01 11:54:27 -070073 ComponentName componentName = null;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070074 if (dragInfo instanceof ApplicationInfo) {
Patrick Dubroybc6840b2010-09-01 11:54:27 -070075 componentName = ((ApplicationInfo)dragInfo).componentName;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070076 } else if (dragInfo instanceof ShortcutInfo) {
Patrick Dubroybc6840b2010-09-01 11:54:27 -070077 componentName = ((ShortcutInfo)dragInfo).intent.getComponent();
Patrick Dubroy4ed62782010-08-17 15:11:18 -070078 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -070079 mLauncher.startApplicationDetailsActivity(componentName);
Patrick Dubroy4ed62782010-08-17 15:11:18 -070080 return false;
81 }
82
Patrick Dubroy4ed62782010-08-17 15:11:18 -070083 public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset,
84 DragView dragView, Object dragInfo) {
85
86 }
87
88 public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset,
89 DragView dragView, Object dragInfo) {
90 dragView.setPaint(mPaint);
91 }
92
93 public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset,
94 DragView dragView, Object dragInfo) {
95 }
96
97 public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset,
98 DragView dragView, Object dragInfo) {
Patrick Dubroy4ed62782010-08-17 15:11:18 -070099 dragView.setPaint(null);
100 }
101
102 public void onDragStart(DragSource source, Object info, int dragAction) {
103 if (info != null) {
Michael Jurka774bd372010-10-22 13:40:50 -0700104 final int itemType = ((ItemInfo)info).itemType;
105 mActive = (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION);
Patrick Dubroydea9e932010-09-22 15:04:29 -0700106 if (mManageVisibility) {
107 // Only show the info icon when an application is selected
Michael Jurka774bd372010-10-22 13:40:50 -0700108 if (mActive) {
Patrick Dubroydea9e932010-09-22 15:04:29 -0700109 setVisibility(VISIBLE);
110 }
111 mHandle.setVisibility(INVISIBLE);
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700112 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700113 }
114 }
115
Michael Jurka0280c3b2010-09-17 15:00:07 -0700116 public boolean isDropEnabled() {
Michael Jurka3adcb0c2010-10-15 18:07:21 -0700117 return mActive;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700118 }
119
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700120 public void onDragEnd() {
121 if (mActive) {
122 mActive = false;
Michael Jurka466810b2010-10-25 13:41:02 -0700123 }
124 if (mManageVisibility) {
125 setVisibility(GONE);
126 mHandle.setVisibility(VISIBLE);
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700127 }
128 }
129
Michael Jurka3adcb0c2010-10-15 18:07:21 -0700130 @Override
131 public void getHitRect(Rect outRect) {
132 super.getHitRect(outRect);
133 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka774bd372010-10-22 13:40:50 -0700134 // TODO: This is a temporary hack. mManageVisiblity = false when you're in CAB mode.
135 // In that case, this icon is more tightly spaced next to the delete icon so we want
136 // it to have a smaller drag region. When the new drag&drop system comes in, we'll
137 // dispatch the drag/drop by calculating what target you're overlapping
Adam Lesinski6b879f02010-11-04 16:15:23 -0700138 final int minPadding = R.dimen.delete_zone_min_padding;
139 final int maxPadding = R.dimen.delete_zone_max_padding;
140 final int outerDragPadding =
141 getResources().getDimensionPixelSize(R.dimen.delete_zone_size);
142 final int innerDragPadding = getResources().getDimensionPixelSize(
143 mManageVisibility ? maxPadding : minPadding);
144 outRect.top -= outerDragPadding;
145 outRect.left -= innerDragPadding;
146 outRect.bottom += outerDragPadding;
147 outRect.right += outerDragPadding;
Michael Jurka3adcb0c2010-10-15 18:07:21 -0700148 }
149 }
150
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700151 void setLauncher(Launcher launcher) {
152 mLauncher = launcher;
153 }
154
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700155 void setHandle(View view) {
156 mHandle = view;
157 }
158
Patrick Dubroydea9e932010-09-22 15:04:29 -0700159 void setManageVisibility(boolean value) {
160 mManageVisibility = value;
161 }
162
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700163 @Override
164 public DropTarget getDropTargetDelegate(DragSource source, int x, int y, int xOffset, int yOffset,
165 DragView dragView, Object dragInfo) {
166 return null;
167 }
168}