blob: a6b2b5c8bfd87b33b3eb7bab565b456866ca7967 [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 Chung4c98d922011-05-31 16:50:48 -070038 private int mHoverColor = 0xFFFF0000;
Adam Cohenebea84d2011-11-09 17:20:41 -080039 private TransitionDrawable mUninstallDrawable;
40 private TransitionDrawable mRemoveDrawable;
41 private TransitionDrawable mCurrentDrawable;
Winson Chung4c98d922011-05-31 16:50:48 -070042
43 public DeleteDropTarget(Context context, AttributeSet attrs) {
44 this(context, attrs, 0);
45 }
46
47 public DeleteDropTarget(Context context, AttributeSet attrs, int defStyle) {
48 super(context, attrs, defStyle);
49 }
50
51 @Override
52 protected void onFinishInflate() {
53 super.onFinishInflate();
54
55 // Get the drawable
Winson Chunga6427b12011-07-27 10:53:39 -070056 mOriginalTextColor = getTextColors();
Winson Chung4c98d922011-05-31 16:50:48 -070057
58 // Get the hover color
59 Resources r = getResources();
Winson Chung4c98d922011-05-31 16:50:48 -070060 mHoverColor = r.getColor(R.color.delete_target_hover_tint);
61 mHoverPaint.setColorFilter(new PorterDuffColorFilter(
62 mHoverColor, PorterDuff.Mode.SRC_ATOP));
Adam Cohenebea84d2011-11-09 17:20:41 -080063 mUninstallDrawable = (TransitionDrawable)
64 r.getDrawable(R.drawable.uninstall_target_selector);
65 mRemoveDrawable = (TransitionDrawable) r.getDrawable(R.drawable.remove_target_selector);
66
67 mRemoveDrawable.setCrossFadeEnabled(true);
68 mUninstallDrawable.setCrossFadeEnabled(true);
69
70 // The current drawable is set to either the remove drawable or the uninstall drawable
71 // and is initially set to the remove drawable, as set in the layout xml.
72 mCurrentDrawable = (TransitionDrawable) getCompoundDrawables()[0];
Winson Chung201bc822011-06-20 15:41:53 -070073
74 // Remove the text in the Phone UI in landscape
75 int orientation = getResources().getConfiguration().orientation;
76 if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
77 if (!LauncherApplication.isScreenLarge()) {
Winson Chunga6427b12011-07-27 10:53:39 -070078 setText("");
Winson Chung201bc822011-06-20 15:41:53 -070079 }
80 }
Winson Chung4c98d922011-05-31 16:50:48 -070081 }
82
83 private boolean isAllAppsApplication(DragSource source, Object info) {
84 return (source instanceof AppsCustomizePagedView) && (info instanceof ApplicationInfo);
85 }
86 private boolean isAllAppsWidget(DragSource source, Object info) {
87 return (source instanceof AppsCustomizePagedView) && (info instanceof PendingAddWidgetInfo);
88 }
Michael Jurka0b4870d2011-07-10 13:39:08 -070089 private boolean isDragSourceWorkspaceOrFolder(DragObject d) {
90 return (d.dragSource instanceof Workspace) || (d.dragSource instanceof Folder);
Winson Chung4c98d922011-05-31 16:50:48 -070091 }
Michael Jurka0b4870d2011-07-10 13:39:08 -070092 private boolean isWorkspaceOrFolderApplication(DragObject d) {
93 return isDragSourceWorkspaceOrFolder(d) && (d.dragInfo instanceof ShortcutInfo);
94 }
95 private boolean isWorkspaceOrFolderWidget(DragObject d) {
96 return isDragSourceWorkspaceOrFolder(d) && (d.dragInfo instanceof LauncherAppWidgetInfo);
Winson Chung4c98d922011-05-31 16:50:48 -070097 }
98 private boolean isWorkspaceFolder(DragObject d) {
99 return (d.dragSource instanceof Workspace) && (d.dragInfo instanceof FolderInfo);
100 }
101
102 @Override
103 public boolean acceptDrop(DragObject d) {
104 // We can remove everything including App shortcuts, folders, widgets, etc.
105 return true;
106 }
107
108 @Override
109 public void onDragStart(DragSource source, Object info, int dragAction) {
Winson Chung4c98d922011-05-31 16:50:48 -0700110 boolean isVisible = true;
111 boolean isUninstall = false;
112
Winson Chungf0ea4d32011-06-06 14:27:16 -0700113 // If we are dragging a widget from AppsCustomize, hide the delete target
Winson Chung4c98d922011-05-31 16:50:48 -0700114 if (isAllAppsWidget(source, info)) {
115 isVisible = false;
116 }
117
118 // If we are dragging an application from AppsCustomize, only show the control if we can
119 // delete the app (it was downloaded), and rename the string to "uninstall" in such a case
120 if (isAllAppsApplication(source, info)) {
121 ApplicationInfo appInfo = (ApplicationInfo) info;
122 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) != 0) {
123 isUninstall = true;
124 } else {
125 isVisible = false;
126 }
127 }
128
Adam Cohenebea84d2011-11-09 17:20:41 -0800129 if (isUninstall) {
130 setCompoundDrawablesWithIntrinsicBounds(mUninstallDrawable, null, null, null);
131 } else {
132 setCompoundDrawablesWithIntrinsicBounds(mRemoveDrawable, null, null, null);
133 }
134 mCurrentDrawable = (TransitionDrawable) getCompoundDrawables()[0];
135
Winson Chung4c98d922011-05-31 16:50:48 -0700136 mActive = isVisible;
Adam Cohenebea84d2011-11-09 17:20:41 -0800137 mCurrentDrawable.resetTransition();
Winson Chunga6427b12011-07-27 10:53:39 -0700138 setTextColor(mOriginalTextColor);
139 ((ViewGroup) getParent()).setVisibility(isVisible ? View.VISIBLE : View.GONE);
140 if (getText().length() > 0) {
141 setText(isUninstall ? R.string.delete_target_uninstall_label
Winson Chung4c98d922011-05-31 16:50:48 -0700142 : R.string.delete_target_label);
143 }
144 }
145
146 @Override
147 public void onDragEnd() {
148 super.onDragEnd();
149 mActive = false;
150 }
151
152 public void onDragEnter(DragObject d) {
153 super.onDragEnter(d);
154
Adam Cohenebea84d2011-11-09 17:20:41 -0800155 mCurrentDrawable.startTransition(mTransitionDuration);
Winson Chunga6427b12011-07-27 10:53:39 -0700156 setTextColor(mHoverColor);
Winson Chung4c98d922011-05-31 16:50:48 -0700157 }
158
159 public void onDragExit(DragObject d) {
160 super.onDragExit(d);
161
Winson Chungaaa530a2011-07-11 21:06:30 -0700162 if (!d.dragComplete) {
Adam Cohenebea84d2011-11-09 17:20:41 -0800163 mCurrentDrawable.resetTransition();
Winson Chunga6427b12011-07-27 10:53:39 -0700164 setTextColor(mOriginalTextColor);
Winson Chungaaa530a2011-07-11 21:06:30 -0700165 }
Winson Chung4c98d922011-05-31 16:50:48 -0700166 }
167
Adam Cohened66b2b2012-01-23 17:28:51 -0800168 Rect getDeleteRect(int deleteItemWidth, int deleteItemHeight) {
Adam Cohend4d7aa52011-07-19 21:47:37 -0700169 DragLayer dragLayer = mLauncher.getDragLayer();
Adam Cohend4d7aa52011-07-19 21:47:37 -0700170
Adam Cohened66b2b2012-01-23 17:28:51 -0800171 Rect to = new Rect();
172 dragLayer.getViewRectRelativeToSelf(this, to);
Adam Cohenebea84d2011-11-09 17:20:41 -0800173 int width = mCurrentDrawable.getIntrinsicWidth();
174 int height = mCurrentDrawable.getIntrinsicHeight();
Winson Chunga6427b12011-07-27 10:53:39 -0700175 to.set(to.left + getPaddingLeft(), to.top + getPaddingTop(),
176 to.left + getPaddingLeft() + width, to.bottom);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700177
178 // Center the destination rect about the trash icon
Adam Cohened66b2b2012-01-23 17:28:51 -0800179 int xOffset = (int) -(deleteItemWidth - width) / 2;
180 int yOffset = (int) -(deleteItemHeight - height) / 2;
Adam Cohend4d7aa52011-07-19 21:47:37 -0700181 to.offset(xOffset, yOffset);
182
Adam Cohened66b2b2012-01-23 17:28:51 -0800183 return to;
184 }
185
186 private void animateToTrashAndCompleteDrop(final DragObject d) {
187 DragLayer dragLayer = mLauncher.getDragLayer();
188 Rect from = new Rect();
189 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
190 Rect to = getDeleteRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight());
191
Adam Cohend4d7aa52011-07-19 21:47:37 -0700192 mSearchDropTargetBar.deferOnDragEnd();
193 Runnable onAnimationEndRunnable = new Runnable() {
194 @Override
195 public void run() {
196 mSearchDropTargetBar.onDragEnd();
197 mLauncher.exitSpringLoadedDragMode();
198 completeDrop(d);
199 }
200 };
Adam Cohened66b2b2012-01-23 17:28:51 -0800201 dragLayer.animateView(d.dragView, from, to, 0.1f, 1, 1, 0.1f, 0.1f,
Adam Cohend4d7aa52011-07-19 21:47:37 -0700202 DELETE_ANIMATION_DURATION, new DecelerateInterpolator(2),
Adam Cohened66b2b2012-01-23 17:28:51 -0800203 new DecelerateInterpolator(1.5f), onAnimationEndRunnable,
204 DragLayer.ANIMATION_END_DISAPPEAR, null);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700205 }
206
207 private void completeDrop(DragObject d) {
Winson Chung4c98d922011-05-31 16:50:48 -0700208 ItemInfo item = (ItemInfo) d.dragInfo;
209
210 if (isAllAppsApplication(d.dragSource, item)) {
211 // Uninstall the application if it is being dragged from AppsCustomize
212 mLauncher.startApplicationUninstallActivity((ApplicationInfo) item);
Michael Jurka0b4870d2011-07-10 13:39:08 -0700213 } else if (isWorkspaceOrFolderApplication(d)) {
Winson Chung4c98d922011-05-31 16:50:48 -0700214 LauncherModel.deleteItemFromDatabase(mLauncher, item);
215 } else if (isWorkspaceFolder(d)) {
216 // Remove the folder from the workspace and delete the contents from launcher model
217 FolderInfo folderInfo = (FolderInfo) item;
218 mLauncher.removeFolder(folderInfo);
219 LauncherModel.deleteFolderContentsFromDatabase(mLauncher, folderInfo);
Michael Jurka0b4870d2011-07-10 13:39:08 -0700220 } else if (isWorkspaceOrFolderWidget(d)) {
Winson Chung4c98d922011-05-31 16:50:48 -0700221 // Remove the widget from the workspace
222 mLauncher.removeAppWidget((LauncherAppWidgetInfo) item);
223 LauncherModel.deleteItemFromDatabase(mLauncher, item);
224
225 final LauncherAppWidgetInfo launcherAppWidgetInfo = (LauncherAppWidgetInfo) item;
226 final LauncherAppWidgetHost appWidgetHost = mLauncher.getAppWidgetHost();
227 if (appWidgetHost != null) {
228 // Deleting an app widget ID is a void call but writes to disk before returning
229 // to the caller...
230 new Thread("deleteAppWidgetId") {
231 public void run() {
232 appWidgetHost.deleteAppWidgetId(launcherAppWidgetInfo.appWidgetId);
233 }
234 }.start();
235 }
236 }
237 }
Adam Cohend4d7aa52011-07-19 21:47:37 -0700238
239 public void onDrop(DragObject d) {
240 animateToTrashAndCompleteDrop(d);
241 }
Winson Chung4c98d922011-05-31 16:50:48 -0700242}