blob: fe5ffd190fc1282cdd88fe56cb17d72a04064ec5 [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
Patrick Dubroybc6840b2010-09-01 11:54:27 -070019import android.content.ComponentName;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070020import android.content.Context;
21import android.graphics.Paint;
22import android.graphics.PorterDuff;
23import android.graphics.PorterDuffColorFilter;
Michael Jurka3adcb0c2010-10-15 18:07:21 -070024import android.graphics.Rect;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070025import android.util.AttributeSet;
26import android.view.View;
27import android.widget.ImageView;
28
29/**
30 * Implements a DropTarget which allows applications to be dropped on it,
31 * in order to launch the application info for that app.
32 */
33public class ApplicationInfoDropTarget extends ImageView implements DropTarget, DragController.DragListener {
34 private Launcher mLauncher;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070035 private boolean mActive = false;
36
Patrick Dubroydea9e932010-09-22 15:04:29 -070037 /**
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 private boolean mManageVisibility = true;
43
44 /** The view that this view should appear in the place of. */
45 private View mHandle = null;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070046
47 private final Paint mPaint = new Paint();
48
49 public ApplicationInfoDropTarget(Context context, AttributeSet attrs) {
50 this(context, attrs, 0);
51 }
52
53 public ApplicationInfoDropTarget(Context context, AttributeSet attrs, int defStyle) {
54 super(context, attrs, defStyle);
55 }
56
Patrick Dubroy4ed62782010-08-17 15:11:18 -070057 /**
58 * Set the color that will be used as a filter over objects dragged over this object.
59 */
60 public void setDragColor(int color) {
61 mPaint.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_ATOP));
62 }
63
64 public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset,
65 DragView dragView, Object dragInfo) {
66
67 // acceptDrop is called just before onDrop. We do the work here, rather than
68 // in onDrop, because it allows us to reject the drop (by returning false)
69 // so that the object being dragged isn't removed from the home screen.
70
Patrick Dubroybc6840b2010-09-01 11:54:27 -070071 ComponentName componentName = null;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070072 if (dragInfo instanceof ApplicationInfo) {
Patrick Dubroybc6840b2010-09-01 11:54:27 -070073 componentName = ((ApplicationInfo)dragInfo).componentName;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070074 } else if (dragInfo instanceof ShortcutInfo) {
Patrick Dubroybc6840b2010-09-01 11:54:27 -070075 componentName = ((ShortcutInfo)dragInfo).intent.getComponent();
Patrick Dubroy4ed62782010-08-17 15:11:18 -070076 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -070077 mLauncher.startApplicationDetailsActivity(componentName);
Patrick Dubroy4ed62782010-08-17 15:11:18 -070078 return false;
79 }
80
Patrick Dubroy4ed62782010-08-17 15:11:18 -070081 public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset,
82 DragView dragView, Object dragInfo) {
83
84 }
85
86 public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset,
87 DragView dragView, Object dragInfo) {
88 dragView.setPaint(mPaint);
89 }
90
91 public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset,
92 DragView dragView, Object dragInfo) {
93 }
94
95 public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset,
96 DragView dragView, Object dragInfo) {
Patrick Dubroy4ed62782010-08-17 15:11:18 -070097 dragView.setPaint(null);
98 }
99
100 public void onDragStart(DragSource source, Object info, int dragAction) {
101 if (info != null) {
Michael Jurka774bd372010-10-22 13:40:50 -0700102 final int itemType = ((ItemInfo)info).itemType;
103 mActive = (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION);
Patrick Dubroydea9e932010-09-22 15:04:29 -0700104 if (mManageVisibility) {
105 // Only show the info icon when an application is selected
Michael Jurka774bd372010-10-22 13:40:50 -0700106 if (mActive) {
Patrick Dubroydea9e932010-09-22 15:04:29 -0700107 setVisibility(VISIBLE);
108 }
109 mHandle.setVisibility(INVISIBLE);
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700110 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700111 }
112 }
113
Michael Jurka0280c3b2010-09-17 15:00:07 -0700114 public boolean isDropEnabled() {
Michael Jurka3adcb0c2010-10-15 18:07:21 -0700115 return mActive;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700116 }
117
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700118 public void onDragEnd() {
119 if (mActive) {
120 mActive = false;
Patrick Dubroydea9e932010-09-22 15:04:29 -0700121 if (mManageVisibility) {
122 setVisibility(GONE);
123 mHandle.setVisibility(VISIBLE);
124 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700125 }
126 }
127
Michael Jurka3adcb0c2010-10-15 18:07:21 -0700128 @Override
129 public void getHitRect(Rect outRect) {
130 super.getHitRect(outRect);
131 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka774bd372010-10-22 13:40:50 -0700132 // TODO: This is a temporary hack. mManageVisiblity = false when you're in CAB mode.
133 // In that case, this icon is more tightly spaced next to the delete icon so we want
134 // it to have a smaller drag region. When the new drag&drop system comes in, we'll
135 // dispatch the drag/drop by calculating what target you're overlapping
136 final int dragPadding = mManageVisibility ? 50 : 10;
137 outRect.top -= dragPadding;
138 outRect.left -= dragPadding;
139 outRect.bottom += dragPadding;
140 outRect.right += dragPadding;
Michael Jurka3adcb0c2010-10-15 18:07:21 -0700141 }
142 }
143
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700144 void setLauncher(Launcher launcher) {
145 mLauncher = launcher;
146 }
147
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700148 void setHandle(View view) {
149 mHandle = view;
150 }
151
Patrick Dubroydea9e932010-09-22 15:04:29 -0700152 void setManageVisibility(boolean value) {
153 mManageVisibility = value;
154 }
155
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700156 @Override
157 public DropTarget getDropTargetDelegate(DragSource source, int x, int y, int xOffset, int yOffset,
158 DragView dragView, Object dragInfo) {
159 return null;
160 }
161}