blob: 4621dea85740398c8ce49d15b3b2106517142d66 [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;
Adam Cohend4d7aa52011-07-19 21:47:37 -070023import android.graphics.Rect;
Winson Chung967289b2011-06-30 18:09:30 -070024import android.graphics.drawable.TransitionDrawable;
Winson Chung4c98d922011-05-31 16:50:48 -070025import android.util.AttributeSet;
26import android.view.View;
Winson Chunga6427b12011-07-27 10:53:39 -070027import android.view.ViewGroup;
Winson Chung61967cb2012-02-28 18:11:33 -080028import android.view.animation.AccelerateInterpolator;
Adam Cohend4d7aa52011-07-19 21:47:37 -070029import android.view.animation.DecelerateInterpolator;
Winson Chung61967cb2012-02-28 18:11:33 -080030import android.view.animation.LinearInterpolator;
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
Winson Chung61967cb2012-02-28 18:11:33 -080036 private static int DELETE_ANIMATION_DURATION = 300;
Winson Chunga62e9fd2011-07-11 15:20:48 -070037 private ColorStateList mOriginalTextColor;
Adam Cohenebea84d2011-11-09 17:20:41 -080038 private TransitionDrawable mUninstallDrawable;
39 private TransitionDrawable mRemoveDrawable;
40 private TransitionDrawable mCurrentDrawable;
Winson Chung4c98d922011-05-31 16:50:48 -070041
42 public DeleteDropTarget(Context context, AttributeSet attrs) {
43 this(context, attrs, 0);
44 }
45
46 public DeleteDropTarget(Context context, AttributeSet attrs, int defStyle) {
47 super(context, attrs, defStyle);
48 }
49
50 @Override
51 protected void onFinishInflate() {
52 super.onFinishInflate();
53
54 // Get the drawable
Winson Chunga6427b12011-07-27 10:53:39 -070055 mOriginalTextColor = getTextColors();
Winson Chung4c98d922011-05-31 16:50:48 -070056
57 // Get the hover color
58 Resources r = getResources();
Winson Chung4c98d922011-05-31 16:50:48 -070059 mHoverColor = r.getColor(R.color.delete_target_hover_tint);
Adam Cohenebea84d2011-11-09 17:20:41 -080060 mUninstallDrawable = (TransitionDrawable)
61 r.getDrawable(R.drawable.uninstall_target_selector);
62 mRemoveDrawable = (TransitionDrawable) r.getDrawable(R.drawable.remove_target_selector);
63
64 mRemoveDrawable.setCrossFadeEnabled(true);
65 mUninstallDrawable.setCrossFadeEnabled(true);
66
67 // The current drawable is set to either the remove drawable or the uninstall drawable
68 // and is initially set to the remove drawable, as set in the layout xml.
69 mCurrentDrawable = (TransitionDrawable) getCompoundDrawables()[0];
Winson Chung201bc822011-06-20 15:41:53 -070070
71 // Remove the text in the Phone UI in landscape
72 int orientation = getResources().getConfiguration().orientation;
73 if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
74 if (!LauncherApplication.isScreenLarge()) {
Winson Chunga6427b12011-07-27 10:53:39 -070075 setText("");
Winson Chung201bc822011-06-20 15:41:53 -070076 }
77 }
Winson Chung4c98d922011-05-31 16:50:48 -070078 }
79
80 private boolean isAllAppsApplication(DragSource source, Object info) {
81 return (source instanceof AppsCustomizePagedView) && (info instanceof ApplicationInfo);
82 }
83 private boolean isAllAppsWidget(DragSource source, Object info) {
84 return (source instanceof AppsCustomizePagedView) && (info instanceof PendingAddWidgetInfo);
85 }
Michael Jurka0b4870d2011-07-10 13:39:08 -070086 private boolean isDragSourceWorkspaceOrFolder(DragObject d) {
87 return (d.dragSource instanceof Workspace) || (d.dragSource instanceof Folder);
Winson Chung4c98d922011-05-31 16:50:48 -070088 }
Michael Jurka0b4870d2011-07-10 13:39:08 -070089 private boolean isWorkspaceOrFolderApplication(DragObject d) {
90 return isDragSourceWorkspaceOrFolder(d) && (d.dragInfo instanceof ShortcutInfo);
91 }
92 private boolean isWorkspaceOrFolderWidget(DragObject d) {
93 return isDragSourceWorkspaceOrFolder(d) && (d.dragInfo instanceof LauncherAppWidgetInfo);
Winson Chung4c98d922011-05-31 16:50:48 -070094 }
95 private boolean isWorkspaceFolder(DragObject d) {
96 return (d.dragSource instanceof Workspace) && (d.dragInfo instanceof FolderInfo);
97 }
98
99 @Override
100 public boolean acceptDrop(DragObject d) {
101 // We can remove everything including App shortcuts, folders, widgets, etc.
102 return true;
103 }
104
105 @Override
106 public void onDragStart(DragSource source, Object info, int dragAction) {
Winson Chung4c98d922011-05-31 16:50:48 -0700107 boolean isVisible = true;
108 boolean isUninstall = false;
109
Winson Chungf0ea4d32011-06-06 14:27:16 -0700110 // If we are dragging a widget from AppsCustomize, hide the delete target
Winson Chung4c98d922011-05-31 16:50:48 -0700111 if (isAllAppsWidget(source, info)) {
112 isVisible = false;
113 }
114
115 // If we are dragging an application from AppsCustomize, only show the control if we can
116 // delete the app (it was downloaded), and rename the string to "uninstall" in such a case
117 if (isAllAppsApplication(source, info)) {
118 ApplicationInfo appInfo = (ApplicationInfo) info;
119 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) != 0) {
120 isUninstall = true;
121 } else {
122 isVisible = false;
123 }
124 }
125
Adam Cohenebea84d2011-11-09 17:20:41 -0800126 if (isUninstall) {
127 setCompoundDrawablesWithIntrinsicBounds(mUninstallDrawable, null, null, null);
128 } else {
129 setCompoundDrawablesWithIntrinsicBounds(mRemoveDrawable, null, null, null);
130 }
131 mCurrentDrawable = (TransitionDrawable) getCompoundDrawables()[0];
132
Winson Chung4c98d922011-05-31 16:50:48 -0700133 mActive = isVisible;
Adam Cohenebea84d2011-11-09 17:20:41 -0800134 mCurrentDrawable.resetTransition();
Winson Chunga6427b12011-07-27 10:53:39 -0700135 setTextColor(mOriginalTextColor);
136 ((ViewGroup) getParent()).setVisibility(isVisible ? View.VISIBLE : View.GONE);
137 if (getText().length() > 0) {
138 setText(isUninstall ? R.string.delete_target_uninstall_label
Winson Chung4c98d922011-05-31 16:50:48 -0700139 : R.string.delete_target_label);
140 }
141 }
142
143 @Override
144 public void onDragEnd() {
145 super.onDragEnd();
146 mActive = false;
147 }
148
149 public void onDragEnter(DragObject d) {
150 super.onDragEnter(d);
151
Adam Cohenebea84d2011-11-09 17:20:41 -0800152 mCurrentDrawable.startTransition(mTransitionDuration);
Winson Chunga6427b12011-07-27 10:53:39 -0700153 setTextColor(mHoverColor);
Winson Chung4c98d922011-05-31 16:50:48 -0700154 }
155
156 public void onDragExit(DragObject d) {
157 super.onDragExit(d);
158
Winson Chungaaa530a2011-07-11 21:06:30 -0700159 if (!d.dragComplete) {
Adam Cohenebea84d2011-11-09 17:20:41 -0800160 mCurrentDrawable.resetTransition();
Winson Chunga6427b12011-07-27 10:53:39 -0700161 setTextColor(mOriginalTextColor);
Winson Chung61967cb2012-02-28 18:11:33 -0800162 } else {
163 // Restore the hover color if we are deleting
164 d.dragView.setColor(mHoverColor);
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 private void animateToTrashAndCompleteDrop(final DragObject d) {
169 DragLayer dragLayer = mLauncher.getDragLayer();
170 Rect from = new Rect();
171 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
Winson Chung61967cb2012-02-28 18:11:33 -0800172 Rect to = getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(),
173 mCurrentDrawable.getIntrinsicWidth(), mCurrentDrawable.getIntrinsicHeight());
174 float scale = (float) to.width() / from.width();
Adam Cohened66b2b2012-01-23 17:28:51 -0800175
Adam Cohend4d7aa52011-07-19 21:47:37 -0700176 mSearchDropTargetBar.deferOnDragEnd();
177 Runnable onAnimationEndRunnable = new Runnable() {
178 @Override
179 public void run() {
180 mSearchDropTargetBar.onDragEnd();
181 mLauncher.exitSpringLoadedDragMode();
182 completeDrop(d);
183 }
184 };
Winson Chung61967cb2012-02-28 18:11:33 -0800185 dragLayer.animateView(d.dragView, from, to, scale, 1f, 1f, 0.1f, 0.1f,
Adam Cohend4d7aa52011-07-19 21:47:37 -0700186 DELETE_ANIMATION_DURATION, new DecelerateInterpolator(2),
Winson Chung61967cb2012-02-28 18:11:33 -0800187 new LinearInterpolator(), onAnimationEndRunnable,
Adam Cohened66b2b2012-01-23 17:28:51 -0800188 DragLayer.ANIMATION_END_DISAPPEAR, null);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700189 }
190
191 private void completeDrop(DragObject d) {
Winson Chung4c98d922011-05-31 16:50:48 -0700192 ItemInfo item = (ItemInfo) d.dragInfo;
193
194 if (isAllAppsApplication(d.dragSource, item)) {
195 // Uninstall the application if it is being dragged from AppsCustomize
196 mLauncher.startApplicationUninstallActivity((ApplicationInfo) item);
Michael Jurka0b4870d2011-07-10 13:39:08 -0700197 } else if (isWorkspaceOrFolderApplication(d)) {
Winson Chung4c98d922011-05-31 16:50:48 -0700198 LauncherModel.deleteItemFromDatabase(mLauncher, item);
199 } else if (isWorkspaceFolder(d)) {
200 // Remove the folder from the workspace and delete the contents from launcher model
201 FolderInfo folderInfo = (FolderInfo) item;
202 mLauncher.removeFolder(folderInfo);
203 LauncherModel.deleteFolderContentsFromDatabase(mLauncher, folderInfo);
Michael Jurka0b4870d2011-07-10 13:39:08 -0700204 } else if (isWorkspaceOrFolderWidget(d)) {
Winson Chung4c98d922011-05-31 16:50:48 -0700205 // Remove the widget from the workspace
206 mLauncher.removeAppWidget((LauncherAppWidgetInfo) item);
207 LauncherModel.deleteItemFromDatabase(mLauncher, item);
208
209 final LauncherAppWidgetInfo launcherAppWidgetInfo = (LauncherAppWidgetInfo) item;
210 final LauncherAppWidgetHost appWidgetHost = mLauncher.getAppWidgetHost();
211 if (appWidgetHost != null) {
212 // Deleting an app widget ID is a void call but writes to disk before returning
213 // to the caller...
214 new Thread("deleteAppWidgetId") {
215 public void run() {
216 appWidgetHost.deleteAppWidgetId(launcherAppWidgetInfo.appWidgetId);
217 }
218 }.start();
219 }
220 }
221 }
Adam Cohend4d7aa52011-07-19 21:47:37 -0700222
223 public void onDrop(DragObject d) {
224 animateToTrashAndCompleteDrop(d);
225 }
Winson Chung4c98d922011-05-31 16:50:48 -0700226}