Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.launcher2; |
| 18 | |
| 19 | import android.content.Context; |
Winson Chung | a62e9fd | 2011-07-11 15:20:48 -0700 | [diff] [blame] | 20 | import android.content.res.ColorStateList; |
Winson Chung | 201bc82 | 2011-06-20 15:41:53 -0700 | [diff] [blame] | 21 | import android.content.res.Configuration; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 22 | import android.content.res.Resources; |
Adam Cohen | d4d7aa5 | 2011-07-19 21:47:37 -0700 | [diff] [blame] | 23 | import android.graphics.Rect; |
Winson Chung | 967289b | 2011-06-30 18:09:30 -0700 | [diff] [blame] | 24 | import android.graphics.drawable.TransitionDrawable; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 25 | import android.util.AttributeSet; |
| 26 | import android.view.View; |
Winson Chung | a6427b1 | 2011-07-27 10:53:39 -0700 | [diff] [blame] | 27 | import android.view.ViewGroup; |
Winson Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame^] | 28 | import android.view.animation.AccelerateInterpolator; |
Adam Cohen | d4d7aa5 | 2011-07-19 21:47:37 -0700 | [diff] [blame] | 29 | import android.view.animation.DecelerateInterpolator; |
Winson Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame^] | 30 | import android.view.animation.LinearInterpolator; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 31 | |
| 32 | import com.android.launcher.R; |
| 33 | |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 34 | public class DeleteDropTarget extends ButtonDropTarget { |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 35 | |
Winson Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame^] | 36 | private static int DELETE_ANIMATION_DURATION = 300; |
Winson Chung | a62e9fd | 2011-07-11 15:20:48 -0700 | [diff] [blame] | 37 | private ColorStateList mOriginalTextColor; |
Adam Cohen | ebea84d | 2011-11-09 17:20:41 -0800 | [diff] [blame] | 38 | private TransitionDrawable mUninstallDrawable; |
| 39 | private TransitionDrawable mRemoveDrawable; |
| 40 | private TransitionDrawable mCurrentDrawable; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 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 Chung | a6427b1 | 2011-07-27 10:53:39 -0700 | [diff] [blame] | 55 | mOriginalTextColor = getTextColors(); |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 56 | |
| 57 | // Get the hover color |
| 58 | Resources r = getResources(); |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 59 | mHoverColor = r.getColor(R.color.delete_target_hover_tint); |
Adam Cohen | ebea84d | 2011-11-09 17:20:41 -0800 | [diff] [blame] | 60 | 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 Chung | 201bc82 | 2011-06-20 15:41:53 -0700 | [diff] [blame] | 70 | |
| 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 Chung | a6427b1 | 2011-07-27 10:53:39 -0700 | [diff] [blame] | 75 | setText(""); |
Winson Chung | 201bc82 | 2011-06-20 15:41:53 -0700 | [diff] [blame] | 76 | } |
| 77 | } |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 78 | } |
| 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 Jurka | 0b4870d | 2011-07-10 13:39:08 -0700 | [diff] [blame] | 86 | private boolean isDragSourceWorkspaceOrFolder(DragObject d) { |
| 87 | return (d.dragSource instanceof Workspace) || (d.dragSource instanceof Folder); |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 88 | } |
Michael Jurka | 0b4870d | 2011-07-10 13:39:08 -0700 | [diff] [blame] | 89 | 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 Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 94 | } |
| 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 Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 107 | boolean isVisible = true; |
| 108 | boolean isUninstall = false; |
| 109 | |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 110 | // If we are dragging a widget from AppsCustomize, hide the delete target |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 111 | 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 Cohen | ebea84d | 2011-11-09 17:20:41 -0800 | [diff] [blame] | 126 | if (isUninstall) { |
| 127 | setCompoundDrawablesWithIntrinsicBounds(mUninstallDrawable, null, null, null); |
| 128 | } else { |
| 129 | setCompoundDrawablesWithIntrinsicBounds(mRemoveDrawable, null, null, null); |
| 130 | } |
| 131 | mCurrentDrawable = (TransitionDrawable) getCompoundDrawables()[0]; |
| 132 | |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 133 | mActive = isVisible; |
Adam Cohen | ebea84d | 2011-11-09 17:20:41 -0800 | [diff] [blame] | 134 | mCurrentDrawable.resetTransition(); |
Winson Chung | a6427b1 | 2011-07-27 10:53:39 -0700 | [diff] [blame] | 135 | 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 Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 139 | : 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 Cohen | ebea84d | 2011-11-09 17:20:41 -0800 | [diff] [blame] | 152 | mCurrentDrawable.startTransition(mTransitionDuration); |
Winson Chung | a6427b1 | 2011-07-27 10:53:39 -0700 | [diff] [blame] | 153 | setTextColor(mHoverColor); |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | public void onDragExit(DragObject d) { |
| 157 | super.onDragExit(d); |
| 158 | |
Winson Chung | aaa530a | 2011-07-11 21:06:30 -0700 | [diff] [blame] | 159 | if (!d.dragComplete) { |
Adam Cohen | ebea84d | 2011-11-09 17:20:41 -0800 | [diff] [blame] | 160 | mCurrentDrawable.resetTransition(); |
Winson Chung | a6427b1 | 2011-07-27 10:53:39 -0700 | [diff] [blame] | 161 | setTextColor(mOriginalTextColor); |
Winson Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame^] | 162 | } else { |
| 163 | // Restore the hover color if we are deleting |
| 164 | d.dragView.setColor(mHoverColor); |
Winson Chung | aaa530a | 2011-07-11 21:06:30 -0700 | [diff] [blame] | 165 | } |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 168 | private void animateToTrashAndCompleteDrop(final DragObject d) { |
| 169 | DragLayer dragLayer = mLauncher.getDragLayer(); |
| 170 | Rect from = new Rect(); |
| 171 | dragLayer.getViewRectRelativeToSelf(d.dragView, from); |
Winson Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame^] | 172 | Rect to = getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(), |
| 173 | mCurrentDrawable.getIntrinsicWidth(), mCurrentDrawable.getIntrinsicHeight()); |
| 174 | float scale = (float) to.width() / from.width(); |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 175 | |
Adam Cohen | d4d7aa5 | 2011-07-19 21:47:37 -0700 | [diff] [blame] | 176 | 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 Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame^] | 185 | dragLayer.animateView(d.dragView, from, to, scale, 1f, 1f, 0.1f, 0.1f, |
Adam Cohen | d4d7aa5 | 2011-07-19 21:47:37 -0700 | [diff] [blame] | 186 | DELETE_ANIMATION_DURATION, new DecelerateInterpolator(2), |
Winson Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame^] | 187 | new LinearInterpolator(), onAnimationEndRunnable, |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 188 | DragLayer.ANIMATION_END_DISAPPEAR, null); |
Adam Cohen | d4d7aa5 | 2011-07-19 21:47:37 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | private void completeDrop(DragObject d) { |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 192 | 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 Jurka | 0b4870d | 2011-07-10 13:39:08 -0700 | [diff] [blame] | 197 | } else if (isWorkspaceOrFolderApplication(d)) { |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 198 | 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 Jurka | 0b4870d | 2011-07-10 13:39:08 -0700 | [diff] [blame] | 204 | } else if (isWorkspaceOrFolderWidget(d)) { |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 205 | // 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 Cohen | d4d7aa5 | 2011-07-19 21:47:37 -0700 | [diff] [blame] | 222 | |
| 223 | public void onDrop(DragObject d) { |
| 224 | animateToTrashAndCompleteDrop(d); |
| 225 | } |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 226 | } |