blob: 4e93b22b132b7f6dd90c2ceb5ae0bf805b0ca461 [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;
Adam Cohenebea84d2011-11-09 17:20:41 -080026import android.graphics.drawable.Drawable;
Winson Chung967289b2011-06-30 18:09:30 -070027import android.graphics.drawable.TransitionDrawable;
Winson Chung4c98d922011-05-31 16:50:48 -070028import android.util.AttributeSet;
29import android.view.View;
Winson Chunga6427b12011-07-27 10:53:39 -070030import android.view.ViewGroup;
Adam Cohend4d7aa52011-07-19 21:47:37 -070031import android.view.animation.DecelerateInterpolator;
Winson Chung4c98d922011-05-31 16:50:48 -070032
33import com.android.launcher.R;
34
Winson Chung61fa4192011-06-12 15:15:29 -070035public class DeleteDropTarget extends ButtonDropTarget {
Winson Chung4c98d922011-05-31 16:50:48 -070036
Adam Cohen4b285c52011-07-21 14:24:06 -070037 private static int DELETE_ANIMATION_DURATION = 250;
Winson Chunga62e9fd2011-07-11 15:20:48 -070038 private ColorStateList mOriginalTextColor;
Winson Chung4c98d922011-05-31 16:50:48 -070039 private int mHoverColor = 0xFFFF0000;
Adam Cohenebea84d2011-11-09 17:20:41 -080040 private TransitionDrawable mUninstallDrawable;
41 private TransitionDrawable mRemoveDrawable;
42 private TransitionDrawable mCurrentDrawable;
Winson Chung4c98d922011-05-31 16:50:48 -070043
44 public DeleteDropTarget(Context context, AttributeSet attrs) {
45 this(context, attrs, 0);
46 }
47
48 public DeleteDropTarget(Context context, AttributeSet attrs, int defStyle) {
49 super(context, attrs, defStyle);
50 }
51
52 @Override
53 protected void onFinishInflate() {
54 super.onFinishInflate();
55
56 // Get the drawable
Winson Chunga6427b12011-07-27 10:53:39 -070057 mOriginalTextColor = getTextColors();
Winson Chung4c98d922011-05-31 16:50:48 -070058
59 // Get the hover color
60 Resources r = getResources();
Winson Chung4c98d922011-05-31 16:50:48 -070061 mHoverColor = r.getColor(R.color.delete_target_hover_tint);
62 mHoverPaint.setColorFilter(new PorterDuffColorFilter(
63 mHoverColor, PorterDuff.Mode.SRC_ATOP));
Adam Cohenebea84d2011-11-09 17:20:41 -080064 mUninstallDrawable = (TransitionDrawable)
65 r.getDrawable(R.drawable.uninstall_target_selector);
66 mRemoveDrawable = (TransitionDrawable) r.getDrawable(R.drawable.remove_target_selector);
67
68 mRemoveDrawable.setCrossFadeEnabled(true);
69 mUninstallDrawable.setCrossFadeEnabled(true);
70
71 // The current drawable is set to either the remove drawable or the uninstall drawable
72 // and is initially set to the remove drawable, as set in the layout xml.
73 mCurrentDrawable = (TransitionDrawable) getCompoundDrawables()[0];
Winson Chung201bc822011-06-20 15:41:53 -070074
75 // Remove the text in the Phone UI in landscape
76 int orientation = getResources().getConfiguration().orientation;
77 if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
78 if (!LauncherApplication.isScreenLarge()) {
Winson Chunga6427b12011-07-27 10:53:39 -070079 setText("");
Winson Chung201bc822011-06-20 15:41:53 -070080 }
81 }
Winson Chung4c98d922011-05-31 16:50:48 -070082 }
83
84 private boolean isAllAppsApplication(DragSource source, Object info) {
85 return (source instanceof AppsCustomizePagedView) && (info instanceof ApplicationInfo);
86 }
87 private boolean isAllAppsWidget(DragSource source, Object info) {
88 return (source instanceof AppsCustomizePagedView) && (info instanceof PendingAddWidgetInfo);
89 }
Michael Jurka0b4870d2011-07-10 13:39:08 -070090 private boolean isDragSourceWorkspaceOrFolder(DragObject d) {
91 return (d.dragSource instanceof Workspace) || (d.dragSource instanceof Folder);
Winson Chung4c98d922011-05-31 16:50:48 -070092 }
Michael Jurka0b4870d2011-07-10 13:39:08 -070093 private boolean isWorkspaceOrFolderApplication(DragObject d) {
94 return isDragSourceWorkspaceOrFolder(d) && (d.dragInfo instanceof ShortcutInfo);
95 }
96 private boolean isWorkspaceOrFolderWidget(DragObject d) {
97 return isDragSourceWorkspaceOrFolder(d) && (d.dragInfo instanceof LauncherAppWidgetInfo);
Winson Chung4c98d922011-05-31 16:50:48 -070098 }
99 private boolean isWorkspaceFolder(DragObject d) {
100 return (d.dragSource instanceof Workspace) && (d.dragInfo instanceof FolderInfo);
101 }
102
103 @Override
104 public boolean acceptDrop(DragObject d) {
105 // We can remove everything including App shortcuts, folders, widgets, etc.
106 return true;
107 }
108
109 @Override
110 public void onDragStart(DragSource source, Object info, int dragAction) {
Winson Chung4c98d922011-05-31 16:50:48 -0700111 boolean isVisible = true;
112 boolean isUninstall = false;
113
Winson Chungf0ea4d32011-06-06 14:27:16 -0700114 // If we are dragging a widget from AppsCustomize, hide the delete target
Winson Chung4c98d922011-05-31 16:50:48 -0700115 if (isAllAppsWidget(source, info)) {
116 isVisible = false;
117 }
118
119 // If we are dragging an application from AppsCustomize, only show the control if we can
120 // delete the app (it was downloaded), and rename the string to "uninstall" in such a case
121 if (isAllAppsApplication(source, info)) {
122 ApplicationInfo appInfo = (ApplicationInfo) info;
123 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) != 0) {
124 isUninstall = true;
125 } else {
126 isVisible = false;
127 }
128 }
129
Adam Cohenebea84d2011-11-09 17:20:41 -0800130 if (isUninstall) {
131 setCompoundDrawablesWithIntrinsicBounds(mUninstallDrawable, null, null, null);
132 } else {
133 setCompoundDrawablesWithIntrinsicBounds(mRemoveDrawable, null, null, null);
134 }
135 mCurrentDrawable = (TransitionDrawable) getCompoundDrawables()[0];
136
Winson Chung4c98d922011-05-31 16:50:48 -0700137 mActive = isVisible;
Adam Cohenebea84d2011-11-09 17:20:41 -0800138 mCurrentDrawable.resetTransition();
Winson Chunga6427b12011-07-27 10:53:39 -0700139 setTextColor(mOriginalTextColor);
140 ((ViewGroup) getParent()).setVisibility(isVisible ? View.VISIBLE : View.GONE);
141 if (getText().length() > 0) {
142 setText(isUninstall ? R.string.delete_target_uninstall_label
Winson Chung4c98d922011-05-31 16:50:48 -0700143 : R.string.delete_target_label);
144 }
145 }
146
147 @Override
148 public void onDragEnd() {
149 super.onDragEnd();
150 mActive = false;
151 }
152
153 public void onDragEnter(DragObject d) {
154 super.onDragEnter(d);
155
Adam Cohenebea84d2011-11-09 17:20:41 -0800156 mCurrentDrawable.startTransition(mTransitionDuration);
Winson Chunga6427b12011-07-27 10:53:39 -0700157 setTextColor(mHoverColor);
Winson Chung4c98d922011-05-31 16:50:48 -0700158 }
159
160 public void onDragExit(DragObject d) {
161 super.onDragExit(d);
162
Winson Chungaaa530a2011-07-11 21:06:30 -0700163 if (!d.dragComplete) {
Adam Cohenebea84d2011-11-09 17:20:41 -0800164 mCurrentDrawable.resetTransition();
Winson Chunga6427b12011-07-27 10:53:39 -0700165 setTextColor(mOriginalTextColor);
Winson Chungaaa530a2011-07-11 21:06:30 -0700166 }
Winson Chung4c98d922011-05-31 16:50:48 -0700167 }
168
Adam Cohend4d7aa52011-07-19 21:47:37 -0700169 private void animateToTrashAndCompleteDrop(final DragObject d) {
170 DragLayer dragLayer = mLauncher.getDragLayer();
171 Rect from = new Rect();
172 Rect to = new Rect();
173 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
Winson Chunga6427b12011-07-27 10:53:39 -0700174 dragLayer.getViewRectRelativeToSelf(this, to);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700175
Adam Cohenebea84d2011-11-09 17:20:41 -0800176 int width = mCurrentDrawable.getIntrinsicWidth();
177 int height = mCurrentDrawable.getIntrinsicHeight();
Winson Chunga6427b12011-07-27 10:53:39 -0700178 to.set(to.left + getPaddingLeft(), to.top + getPaddingTop(),
179 to.left + getPaddingLeft() + width, to.bottom);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700180
181 // Center the destination rect about the trash icon
182 int xOffset = (int) -(d.dragView.getMeasuredWidth() - width) / 2;
183 int yOffset = (int) -(d.dragView.getMeasuredHeight() - height) / 2;
184 to.offset(xOffset, yOffset);
185
186 mSearchDropTargetBar.deferOnDragEnd();
187 Runnable onAnimationEndRunnable = new Runnable() {
188 @Override
189 public void run() {
190 mSearchDropTargetBar.onDragEnd();
191 mLauncher.exitSpringLoadedDragMode();
192 completeDrop(d);
193 }
194 };
Adam Cohen4b285c52011-07-21 14:24:06 -0700195 dragLayer.animateView(d.dragView, from, to, 0.1f, 0.1f,
Adam Cohend4d7aa52011-07-19 21:47:37 -0700196 DELETE_ANIMATION_DURATION, new DecelerateInterpolator(2),
Adam Cohen4b285c52011-07-21 14:24:06 -0700197 new DecelerateInterpolator(1.5f), onAnimationEndRunnable, false);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700198 }
199
200 private void completeDrop(DragObject d) {
Winson Chung4c98d922011-05-31 16:50:48 -0700201 ItemInfo item = (ItemInfo) d.dragInfo;
202
203 if (isAllAppsApplication(d.dragSource, item)) {
204 // Uninstall the application if it is being dragged from AppsCustomize
205 mLauncher.startApplicationUninstallActivity((ApplicationInfo) item);
Michael Jurka0b4870d2011-07-10 13:39:08 -0700206 } else if (isWorkspaceOrFolderApplication(d)) {
Winson Chung4c98d922011-05-31 16:50:48 -0700207 LauncherModel.deleteItemFromDatabase(mLauncher, item);
208 } else if (isWorkspaceFolder(d)) {
209 // Remove the folder from the workspace and delete the contents from launcher model
210 FolderInfo folderInfo = (FolderInfo) item;
211 mLauncher.removeFolder(folderInfo);
212 LauncherModel.deleteFolderContentsFromDatabase(mLauncher, folderInfo);
Michael Jurka0b4870d2011-07-10 13:39:08 -0700213 } else if (isWorkspaceOrFolderWidget(d)) {
Winson Chung4c98d922011-05-31 16:50:48 -0700214 // Remove the widget from the workspace
215 mLauncher.removeAppWidget((LauncherAppWidgetInfo) item);
216 LauncherModel.deleteItemFromDatabase(mLauncher, item);
217
218 final LauncherAppWidgetInfo launcherAppWidgetInfo = (LauncherAppWidgetInfo) item;
219 final LauncherAppWidgetHost appWidgetHost = mLauncher.getAppWidgetHost();
220 if (appWidgetHost != null) {
221 // Deleting an app widget ID is a void call but writes to disk before returning
222 // to the caller...
223 new Thread("deleteAppWidgetId") {
224 public void run() {
225 appWidgetHost.deleteAppWidgetId(launcherAppWidgetInfo.appWidgetId);
226 }
227 }.start();
228 }
229 }
230 }
Adam Cohend4d7aa52011-07-19 21:47:37 -0700231
232 public void onDrop(DragObject d) {
233 animateToTrashAndCompleteDrop(d);
234 }
Winson Chung4c98d922011-05-31 16:50:48 -0700235}