blob: f1997082326b69a20c4754c5abb59392cde51bd3 [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 Cohend4d7aa52011-07-19 21:47:37 -0700168 private void animateToTrashAndCompleteDrop(final DragObject d) {
169 DragLayer dragLayer = mLauncher.getDragLayer();
170 Rect from = new Rect();
171 Rect to = new Rect();
172 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
Winson Chunga6427b12011-07-27 10:53:39 -0700173 dragLayer.getViewRectRelativeToSelf(this, to);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700174
Adam Cohenebea84d2011-11-09 17:20:41 -0800175 int width = mCurrentDrawable.getIntrinsicWidth();
176 int height = mCurrentDrawable.getIntrinsicHeight();
Winson Chunga6427b12011-07-27 10:53:39 -0700177 to.set(to.left + getPaddingLeft(), to.top + getPaddingTop(),
178 to.left + getPaddingLeft() + width, to.bottom);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700179
180 // Center the destination rect about the trash icon
181 int xOffset = (int) -(d.dragView.getMeasuredWidth() - width) / 2;
182 int yOffset = (int) -(d.dragView.getMeasuredHeight() - height) / 2;
183 to.offset(xOffset, yOffset);
184
185 mSearchDropTargetBar.deferOnDragEnd();
186 Runnable onAnimationEndRunnable = new Runnable() {
187 @Override
188 public void run() {
189 mSearchDropTargetBar.onDragEnd();
190 mLauncher.exitSpringLoadedDragMode();
191 completeDrop(d);
192 }
193 };
Adam Cohen4b285c52011-07-21 14:24:06 -0700194 dragLayer.animateView(d.dragView, from, to, 0.1f, 0.1f,
Adam Cohend4d7aa52011-07-19 21:47:37 -0700195 DELETE_ANIMATION_DURATION, new DecelerateInterpolator(2),
Adam Cohen4b285c52011-07-21 14:24:06 -0700196 new DecelerateInterpolator(1.5f), onAnimationEndRunnable, false);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700197 }
198
199 private void completeDrop(DragObject d) {
Winson Chung4c98d922011-05-31 16:50:48 -0700200 ItemInfo item = (ItemInfo) d.dragInfo;
201
202 if (isAllAppsApplication(d.dragSource, item)) {
203 // Uninstall the application if it is being dragged from AppsCustomize
204 mLauncher.startApplicationUninstallActivity((ApplicationInfo) item);
Michael Jurka0b4870d2011-07-10 13:39:08 -0700205 } else if (isWorkspaceOrFolderApplication(d)) {
Winson Chung4c98d922011-05-31 16:50:48 -0700206 LauncherModel.deleteItemFromDatabase(mLauncher, item);
207 } else if (isWorkspaceFolder(d)) {
208 // Remove the folder from the workspace and delete the contents from launcher model
209 FolderInfo folderInfo = (FolderInfo) item;
210 mLauncher.removeFolder(folderInfo);
211 LauncherModel.deleteFolderContentsFromDatabase(mLauncher, folderInfo);
Michael Jurka0b4870d2011-07-10 13:39:08 -0700212 } else if (isWorkspaceOrFolderWidget(d)) {
Winson Chung4c98d922011-05-31 16:50:48 -0700213 // Remove the widget from the workspace
214 mLauncher.removeAppWidget((LauncherAppWidgetInfo) item);
215 LauncherModel.deleteItemFromDatabase(mLauncher, item);
216
217 final LauncherAppWidgetInfo launcherAppWidgetInfo = (LauncherAppWidgetInfo) item;
218 final LauncherAppWidgetHost appWidgetHost = mLauncher.getAppWidgetHost();
219 if (appWidgetHost != null) {
220 // Deleting an app widget ID is a void call but writes to disk before returning
221 // to the caller...
222 new Thread("deleteAppWidgetId") {
223 public void run() {
224 appWidgetHost.deleteAppWidgetId(launcherAppWidgetInfo.appWidgetId);
225 }
226 }.start();
227 }
228 }
229 }
Adam Cohend4d7aa52011-07-19 21:47:37 -0700230
231 public void onDrop(DragObject d) {
232 animateToTrashAndCompleteDrop(d);
233 }
Winson Chung4c98d922011-05-31 16:50:48 -0700234}