blob: 1553d3cc69b316a849bbb63d91e0eef86a49ddbe [file] [log] [blame]
Winson Chung4c98d922011-05-31 16:50:48 -07001/*
2 * Copyright (C) 2011 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.ColorStateList;
Winson Chung201bc822011-06-20 15:41:53 -070021import android.content.res.Configuration;
Winson Chung4c98d922011-05-31 16:50:48 -070022import android.content.res.Resources;
23import android.graphics.PorterDuff;
24import android.graphics.PorterDuffColorFilter;
Adam Cohend4d7aa52011-07-19 21:47:37 -070025import android.graphics.Rect;
Winson Chung967289b2011-06-30 18:09:30 -070026import android.graphics.drawable.TransitionDrawable;
Winson Chung4c98d922011-05-31 16:50:48 -070027import android.util.AttributeSet;
28import android.view.View;
Winson Chunga6427b12011-07-27 10:53:39 -070029import android.view.ViewGroup;
Adam Cohend4d7aa52011-07-19 21:47:37 -070030import android.view.animation.DecelerateInterpolator;
Winson Chung4c98d922011-05-31 16:50:48 -070031
32import com.android.launcher.R;
33
Winson Chung61fa4192011-06-12 15:15:29 -070034public class DeleteDropTarget extends ButtonDropTarget {
Winson Chung4c98d922011-05-31 16:50:48 -070035
Adam Cohen4b285c52011-07-21 14:24:06 -070036 private static int DELETE_ANIMATION_DURATION = 250;
Winson Chunga62e9fd2011-07-11 15:20:48 -070037 private ColorStateList mOriginalTextColor;
Winson Chung967289b2011-06-30 18:09:30 -070038 private TransitionDrawable mDrawable;
Winson Chung4c98d922011-05-31 16:50:48 -070039 private int mHoverColor = 0xFFFF0000;
40
41 public DeleteDropTarget(Context context, AttributeSet attrs) {
42 this(context, attrs, 0);
43 }
44
45 public DeleteDropTarget(Context context, AttributeSet attrs, int defStyle) {
46 super(context, attrs, defStyle);
47 }
48
49 @Override
50 protected void onFinishInflate() {
51 super.onFinishInflate();
52
53 // Get the drawable
Winson Chunga6427b12011-07-27 10:53:39 -070054 mOriginalTextColor = getTextColors();
Winson Chung4c98d922011-05-31 16:50:48 -070055
56 // Get the hover color
57 Resources r = getResources();
Winson Chung4c98d922011-05-31 16:50:48 -070058 mHoverColor = r.getColor(R.color.delete_target_hover_tint);
59 mHoverPaint.setColorFilter(new PorterDuffColorFilter(
60 mHoverColor, PorterDuff.Mode.SRC_ATOP));
Winson Chunga6427b12011-07-27 10:53:39 -070061 mDrawable = (TransitionDrawable) getCompoundDrawables()[0];
Winson Chung967289b2011-06-30 18:09:30 -070062 mDrawable.setCrossFadeEnabled(true);
Winson Chung201bc822011-06-20 15:41:53 -070063
64 // Remove the text in the Phone UI in landscape
65 int orientation = getResources().getConfiguration().orientation;
66 if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
67 if (!LauncherApplication.isScreenLarge()) {
Winson Chunga6427b12011-07-27 10:53:39 -070068 setText("");
Winson Chung201bc822011-06-20 15:41:53 -070069 }
70 }
Winson Chung4c98d922011-05-31 16:50:48 -070071 }
72
73 private boolean isAllAppsApplication(DragSource source, Object info) {
74 return (source instanceof AppsCustomizePagedView) && (info instanceof ApplicationInfo);
75 }
76 private boolean isAllAppsWidget(DragSource source, Object info) {
77 return (source instanceof AppsCustomizePagedView) && (info instanceof PendingAddWidgetInfo);
78 }
Michael Jurka0b4870d2011-07-10 13:39:08 -070079 private boolean isDragSourceWorkspaceOrFolder(DragObject d) {
80 return (d.dragSource instanceof Workspace) || (d.dragSource instanceof Folder);
Winson Chung4c98d922011-05-31 16:50:48 -070081 }
Michael Jurka0b4870d2011-07-10 13:39:08 -070082 private boolean isWorkspaceOrFolderApplication(DragObject d) {
83 return isDragSourceWorkspaceOrFolder(d) && (d.dragInfo instanceof ShortcutInfo);
84 }
85 private boolean isWorkspaceOrFolderWidget(DragObject d) {
86 return isDragSourceWorkspaceOrFolder(d) && (d.dragInfo instanceof LauncherAppWidgetInfo);
Winson Chung4c98d922011-05-31 16:50:48 -070087 }
88 private boolean isWorkspaceFolder(DragObject d) {
89 return (d.dragSource instanceof Workspace) && (d.dragInfo instanceof FolderInfo);
90 }
91
92 @Override
93 public boolean acceptDrop(DragObject d) {
94 // We can remove everything including App shortcuts, folders, widgets, etc.
95 return true;
96 }
97
98 @Override
99 public void onDragStart(DragSource source, Object info, int dragAction) {
Winson Chung4c98d922011-05-31 16:50:48 -0700100 boolean isVisible = true;
101 boolean isUninstall = false;
102
Winson Chungf0ea4d32011-06-06 14:27:16 -0700103 // If we are dragging a widget from AppsCustomize, hide the delete target
Winson Chung4c98d922011-05-31 16:50:48 -0700104 if (isAllAppsWidget(source, info)) {
105 isVisible = false;
106 }
107
108 // If we are dragging an application from AppsCustomize, only show the control if we can
109 // delete the app (it was downloaded), and rename the string to "uninstall" in such a case
110 if (isAllAppsApplication(source, info)) {
111 ApplicationInfo appInfo = (ApplicationInfo) info;
112 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) != 0) {
113 isUninstall = true;
114 } else {
115 isVisible = false;
116 }
117 }
118
119 mActive = isVisible;
Winson Chungaaa530a2011-07-11 21:06:30 -0700120 mDrawable.resetTransition();
Winson Chunga6427b12011-07-27 10:53:39 -0700121 setTextColor(mOriginalTextColor);
122 ((ViewGroup) getParent()).setVisibility(isVisible ? View.VISIBLE : View.GONE);
123 if (getText().length() > 0) {
124 setText(isUninstall ? R.string.delete_target_uninstall_label
Winson Chung4c98d922011-05-31 16:50:48 -0700125 : R.string.delete_target_label);
126 }
127 }
128
129 @Override
130 public void onDragEnd() {
131 super.onDragEnd();
132 mActive = false;
133 }
134
135 public void onDragEnter(DragObject d) {
136 super.onDragEnter(d);
137
Winson Chung967289b2011-06-30 18:09:30 -0700138 mDrawable.startTransition(mTransitionDuration);
Winson Chunga6427b12011-07-27 10:53:39 -0700139 setTextColor(mHoverColor);
Winson Chung4c98d922011-05-31 16:50:48 -0700140 }
141
142 public void onDragExit(DragObject d) {
143 super.onDragExit(d);
144
Winson Chungaaa530a2011-07-11 21:06:30 -0700145 if (!d.dragComplete) {
146 mDrawable.resetTransition();
Winson Chunga6427b12011-07-27 10:53:39 -0700147 setTextColor(mOriginalTextColor);
Winson Chungaaa530a2011-07-11 21:06:30 -0700148 }
Winson Chung4c98d922011-05-31 16:50:48 -0700149 }
150
Adam Cohend4d7aa52011-07-19 21:47:37 -0700151 private void animateToTrashAndCompleteDrop(final DragObject d) {
152 DragLayer dragLayer = mLauncher.getDragLayer();
153 Rect from = new Rect();
154 Rect to = new Rect();
155 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
Winson Chunga6427b12011-07-27 10:53:39 -0700156 dragLayer.getViewRectRelativeToSelf(this, to);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700157
158 int width = mDrawable.getIntrinsicWidth();
159 int height = mDrawable.getIntrinsicHeight();
Winson Chunga6427b12011-07-27 10:53:39 -0700160 to.set(to.left + getPaddingLeft(), to.top + getPaddingTop(),
161 to.left + getPaddingLeft() + width, to.bottom);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700162
163 // Center the destination rect about the trash icon
164 int xOffset = (int) -(d.dragView.getMeasuredWidth() - width) / 2;
165 int yOffset = (int) -(d.dragView.getMeasuredHeight() - height) / 2;
166 to.offset(xOffset, yOffset);
167
168 mSearchDropTargetBar.deferOnDragEnd();
169 Runnable onAnimationEndRunnable = new Runnable() {
170 @Override
171 public void run() {
172 mSearchDropTargetBar.onDragEnd();
173 mLauncher.exitSpringLoadedDragMode();
174 completeDrop(d);
175 }
176 };
Adam Cohen4b285c52011-07-21 14:24:06 -0700177 dragLayer.animateView(d.dragView, from, to, 0.1f, 0.1f,
Adam Cohend4d7aa52011-07-19 21:47:37 -0700178 DELETE_ANIMATION_DURATION, new DecelerateInterpolator(2),
Adam Cohen4b285c52011-07-21 14:24:06 -0700179 new DecelerateInterpolator(1.5f), onAnimationEndRunnable, false);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700180 }
181
182 private void completeDrop(DragObject d) {
Winson Chung4c98d922011-05-31 16:50:48 -0700183 ItemInfo item = (ItemInfo) d.dragInfo;
184
185 if (isAllAppsApplication(d.dragSource, item)) {
186 // Uninstall the application if it is being dragged from AppsCustomize
187 mLauncher.startApplicationUninstallActivity((ApplicationInfo) item);
Michael Jurka0b4870d2011-07-10 13:39:08 -0700188 } else if (isWorkspaceOrFolderApplication(d)) {
Winson Chung4c98d922011-05-31 16:50:48 -0700189 LauncherModel.deleteItemFromDatabase(mLauncher, item);
190 } else if (isWorkspaceFolder(d)) {
191 // Remove the folder from the workspace and delete the contents from launcher model
192 FolderInfo folderInfo = (FolderInfo) item;
193 mLauncher.removeFolder(folderInfo);
194 LauncherModel.deleteFolderContentsFromDatabase(mLauncher, folderInfo);
Michael Jurka0b4870d2011-07-10 13:39:08 -0700195 } else if (isWorkspaceOrFolderWidget(d)) {
Winson Chung4c98d922011-05-31 16:50:48 -0700196 // Remove the widget from the workspace
197 mLauncher.removeAppWidget((LauncherAppWidgetInfo) item);
198 LauncherModel.deleteItemFromDatabase(mLauncher, item);
199
200 final LauncherAppWidgetInfo launcherAppWidgetInfo = (LauncherAppWidgetInfo) item;
201 final LauncherAppWidgetHost appWidgetHost = mLauncher.getAppWidgetHost();
202 if (appWidgetHost != null) {
203 // Deleting an app widget ID is a void call but writes to disk before returning
204 // to the caller...
205 new Thread("deleteAppWidgetId") {
206 public void run() {
207 appWidgetHost.deleteAppWidgetId(launcherAppWidgetInfo.appWidgetId);
208 }
209 }.start();
210 }
211 }
212 }
Adam Cohend4d7aa52011-07-19 21:47:37 -0700213
214 public void onDrop(DragObject d) {
215 animateToTrashAndCompleteDrop(d);
216 }
Winson Chung4c98d922011-05-31 16:50:48 -0700217}