blob: e76ab31b75785e90eca8d33c367eb606928f407a [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;
Adam Cohend4d7aa52011-07-19 21:47:37 -070029import android.view.animation.AccelerateInterpolator;
30import android.view.animation.DecelerateInterpolator;
Winson Chung61fa4192011-06-12 15:15:29 -070031import android.widget.TextView;
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 Chung967289b2011-06-30 18:09:30 -070039 private TransitionDrawable mDrawable;
Winson Chung4c98d922011-05-31 16:50:48 -070040 private int mHoverColor = 0xFFFF0000;
41
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 Chung61fa4192011-06-12 15:15:29 -070055 mText = (TextView) findViewById(R.id.delete_target_text);
Winson Chunga62e9fd2011-07-11 15:20:48 -070056 mOriginalTextColor = mText.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));
Winson Chung967289b2011-06-30 18:09:30 -070063 mDrawable = (TransitionDrawable) mText.getCompoundDrawables()[0];
64 mDrawable.setCrossFadeEnabled(true);
Winson Chung201bc822011-06-20 15:41:53 -070065
66 // Remove the text in the Phone UI in landscape
67 int orientation = getResources().getConfiguration().orientation;
68 if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
69 if (!LauncherApplication.isScreenLarge()) {
70 mText.setText("");
71 }
72 }
Winson Chung4c98d922011-05-31 16:50:48 -070073 }
74
75 private boolean isAllAppsApplication(DragSource source, Object info) {
76 return (source instanceof AppsCustomizePagedView) && (info instanceof ApplicationInfo);
77 }
78 private boolean isAllAppsWidget(DragSource source, Object info) {
79 return (source instanceof AppsCustomizePagedView) && (info instanceof PendingAddWidgetInfo);
80 }
Michael Jurka0b4870d2011-07-10 13:39:08 -070081 private boolean isDragSourceWorkspaceOrFolder(DragObject d) {
82 return (d.dragSource instanceof Workspace) || (d.dragSource instanceof Folder);
Winson Chung4c98d922011-05-31 16:50:48 -070083 }
Michael Jurka0b4870d2011-07-10 13:39:08 -070084 private boolean isWorkspaceOrFolderApplication(DragObject d) {
85 return isDragSourceWorkspaceOrFolder(d) && (d.dragInfo instanceof ShortcutInfo);
86 }
87 private boolean isWorkspaceOrFolderWidget(DragObject d) {
88 return isDragSourceWorkspaceOrFolder(d) && (d.dragInfo instanceof LauncherAppWidgetInfo);
Winson Chung4c98d922011-05-31 16:50:48 -070089 }
90 private boolean isWorkspaceFolder(DragObject d) {
91 return (d.dragSource instanceof Workspace) && (d.dragInfo instanceof FolderInfo);
92 }
93
94 @Override
95 public boolean acceptDrop(DragObject d) {
96 // We can remove everything including App shortcuts, folders, widgets, etc.
97 return true;
98 }
99
100 @Override
101 public void onDragStart(DragSource source, Object info, int dragAction) {
Winson Chung4c98d922011-05-31 16:50:48 -0700102 boolean isVisible = true;
103 boolean isUninstall = false;
104
Winson Chungf0ea4d32011-06-06 14:27:16 -0700105 // If we are dragging a widget from AppsCustomize, hide the delete target
Winson Chung4c98d922011-05-31 16:50:48 -0700106 if (isAllAppsWidget(source, info)) {
107 isVisible = false;
108 }
109
110 // If we are dragging an application from AppsCustomize, only show the control if we can
111 // delete the app (it was downloaded), and rename the string to "uninstall" in such a case
112 if (isAllAppsApplication(source, info)) {
113 ApplicationInfo appInfo = (ApplicationInfo) info;
114 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) != 0) {
115 isUninstall = true;
116 } else {
117 isVisible = false;
118 }
119 }
120
121 mActive = isVisible;
Winson Chungaaa530a2011-07-11 21:06:30 -0700122 mDrawable.resetTransition();
Winson Chunga62e9fd2011-07-11 15:20:48 -0700123 mText.setTextColor(mOriginalTextColor);
Winson Chung61fa4192011-06-12 15:15:29 -0700124 setVisibility(isVisible ? View.VISIBLE : View.GONE);
125 if (mText.getText().length() > 0) {
126 mText.setText(isUninstall ? R.string.delete_target_uninstall_label
Winson Chung4c98d922011-05-31 16:50:48 -0700127 : R.string.delete_target_label);
128 }
129 }
130
131 @Override
132 public void onDragEnd() {
133 super.onDragEnd();
134 mActive = false;
135 }
136
137 public void onDragEnter(DragObject d) {
138 super.onDragEnter(d);
139
Winson Chung967289b2011-06-30 18:09:30 -0700140 mDrawable.startTransition(mTransitionDuration);
Winson Chunga62e9fd2011-07-11 15:20:48 -0700141 mText.setTextColor(mHoverColor);
Winson Chung4c98d922011-05-31 16:50:48 -0700142 }
143
144 public void onDragExit(DragObject d) {
145 super.onDragExit(d);
146
Winson Chungaaa530a2011-07-11 21:06:30 -0700147 if (!d.dragComplete) {
148 mDrawable.resetTransition();
Winson Chunga62e9fd2011-07-11 15:20:48 -0700149 mText.setTextColor(mOriginalTextColor);
Winson Chungaaa530a2011-07-11 21:06:30 -0700150 }
Winson Chung4c98d922011-05-31 16:50:48 -0700151 }
152
Adam Cohend4d7aa52011-07-19 21:47:37 -0700153 private void animateToTrashAndCompleteDrop(final DragObject d) {
154 DragLayer dragLayer = mLauncher.getDragLayer();
155 Rect from = new Rect();
156 Rect to = new Rect();
157 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
158 dragLayer.getViewRectRelativeToSelf(mText, to);
159
160 int width = mDrawable.getIntrinsicWidth();
161 int height = mDrawable.getIntrinsicHeight();
162 to.set(to.left, to.top, to.left + width, to.bottom);
163
164 // Center the destination rect about the trash icon
165 int xOffset = (int) -(d.dragView.getMeasuredWidth() - width) / 2;
166 int yOffset = (int) -(d.dragView.getMeasuredHeight() - height) / 2;
167 to.offset(xOffset, yOffset);
168
169 mSearchDropTargetBar.deferOnDragEnd();
170 Runnable onAnimationEndRunnable = new Runnable() {
171 @Override
172 public void run() {
173 mSearchDropTargetBar.onDragEnd();
174 mLauncher.exitSpringLoadedDragMode();
175 completeDrop(d);
176 }
177 };
Adam Cohen4b285c52011-07-21 14:24:06 -0700178 dragLayer.animateView(d.dragView, from, to, 0.1f, 0.1f,
Adam Cohend4d7aa52011-07-19 21:47:37 -0700179 DELETE_ANIMATION_DURATION, new DecelerateInterpolator(2),
Adam Cohen4b285c52011-07-21 14:24:06 -0700180 new DecelerateInterpolator(1.5f), onAnimationEndRunnable, false);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700181 }
182
183 private void completeDrop(DragObject d) {
Winson Chung4c98d922011-05-31 16:50:48 -0700184 ItemInfo item = (ItemInfo) d.dragInfo;
185
186 if (isAllAppsApplication(d.dragSource, item)) {
187 // Uninstall the application if it is being dragged from AppsCustomize
188 mLauncher.startApplicationUninstallActivity((ApplicationInfo) item);
Michael Jurka0b4870d2011-07-10 13:39:08 -0700189 } else if (isWorkspaceOrFolderApplication(d)) {
Winson Chung4c98d922011-05-31 16:50:48 -0700190 LauncherModel.deleteItemFromDatabase(mLauncher, item);
191 } else if (isWorkspaceFolder(d)) {
192 // Remove the folder from the workspace and delete the contents from launcher model
193 FolderInfo folderInfo = (FolderInfo) item;
194 mLauncher.removeFolder(folderInfo);
195 LauncherModel.deleteFolderContentsFromDatabase(mLauncher, folderInfo);
Michael Jurka0b4870d2011-07-10 13:39:08 -0700196 } else if (isWorkspaceOrFolderWidget(d)) {
Winson Chung4c98d922011-05-31 16:50:48 -0700197 // Remove the widget from the workspace
198 mLauncher.removeAppWidget((LauncherAppWidgetInfo) item);
199 LauncherModel.deleteItemFromDatabase(mLauncher, item);
200
201 final LauncherAppWidgetInfo launcherAppWidgetInfo = (LauncherAppWidgetInfo) item;
202 final LauncherAppWidgetHost appWidgetHost = mLauncher.getAppWidgetHost();
203 if (appWidgetHost != null) {
204 // Deleting an app widget ID is a void call but writes to disk before returning
205 // to the caller...
206 new Thread("deleteAppWidgetId") {
207 public void run() {
208 appWidgetHost.deleteAppWidgetId(launcherAppWidgetInfo.appWidgetId);
209 }
210 }.start();
211 }
212 }
213 }
Adam Cohend4d7aa52011-07-19 21:47:37 -0700214
215 public void onDrop(DragObject d) {
216 animateToTrashAndCompleteDrop(d);
217 }
Winson Chung4c98d922011-05-31 16:50:48 -0700218}