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 | 201bc82 | 2011-06-20 15:41:53 -0700 | [diff] [blame] | 20 | import android.content.res.Configuration; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 21 | import android.content.res.Resources; |
| 22 | import android.graphics.PorterDuff; |
| 23 | import android.graphics.PorterDuffColorFilter; |
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 | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 27 | import android.widget.TextView; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 28 | |
| 29 | import com.android.launcher.R; |
| 30 | |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 31 | public class DeleteDropTarget extends ButtonDropTarget { |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 32 | |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 33 | private TextView mText; |
Winson Chung | 967289b | 2011-06-30 18:09:30 -0700 | [diff] [blame] | 34 | private TransitionDrawable mDrawable; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 35 | private int mHoverColor = 0xFFFF0000; |
| 36 | |
| 37 | public DeleteDropTarget(Context context, AttributeSet attrs) { |
| 38 | this(context, attrs, 0); |
| 39 | } |
| 40 | |
| 41 | public DeleteDropTarget(Context context, AttributeSet attrs, int defStyle) { |
| 42 | super(context, attrs, defStyle); |
| 43 | } |
| 44 | |
| 45 | @Override |
| 46 | protected void onFinishInflate() { |
| 47 | super.onFinishInflate(); |
| 48 | |
| 49 | // Get the drawable |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 50 | mText = (TextView) findViewById(R.id.delete_target_text); |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 51 | |
| 52 | // Get the hover color |
| 53 | Resources r = getResources(); |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 54 | mHoverColor = r.getColor(R.color.delete_target_hover_tint); |
| 55 | mHoverPaint.setColorFilter(new PorterDuffColorFilter( |
| 56 | mHoverColor, PorterDuff.Mode.SRC_ATOP)); |
Winson Chung | 967289b | 2011-06-30 18:09:30 -0700 | [diff] [blame] | 57 | mDrawable = (TransitionDrawable) mText.getCompoundDrawables()[0]; |
| 58 | mDrawable.setCrossFadeEnabled(true); |
Winson Chung | 201bc82 | 2011-06-20 15:41:53 -0700 | [diff] [blame] | 59 | |
| 60 | // Remove the text in the Phone UI in landscape |
| 61 | int orientation = getResources().getConfiguration().orientation; |
| 62 | if (orientation == Configuration.ORIENTATION_LANDSCAPE) { |
| 63 | if (!LauncherApplication.isScreenLarge()) { |
| 64 | mText.setText(""); |
| 65 | } |
| 66 | } |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | private boolean isAllAppsApplication(DragSource source, Object info) { |
| 70 | return (source instanceof AppsCustomizePagedView) && (info instanceof ApplicationInfo); |
| 71 | } |
| 72 | private boolean isAllAppsWidget(DragSource source, Object info) { |
| 73 | return (source instanceof AppsCustomizePagedView) && (info instanceof PendingAddWidgetInfo); |
| 74 | } |
Michael Jurka | 0b4870d | 2011-07-10 13:39:08 -0700 | [diff] [blame] | 75 | private boolean isDragSourceWorkspaceOrFolder(DragObject d) { |
| 76 | return (d.dragSource instanceof Workspace) || (d.dragSource instanceof Folder); |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 77 | } |
Michael Jurka | 0b4870d | 2011-07-10 13:39:08 -0700 | [diff] [blame] | 78 | private boolean isWorkspaceOrFolderApplication(DragObject d) { |
| 79 | return isDragSourceWorkspaceOrFolder(d) && (d.dragInfo instanceof ShortcutInfo); |
| 80 | } |
| 81 | private boolean isWorkspaceOrFolderWidget(DragObject d) { |
| 82 | return isDragSourceWorkspaceOrFolder(d) && (d.dragInfo instanceof LauncherAppWidgetInfo); |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 83 | } |
| 84 | private boolean isWorkspaceFolder(DragObject d) { |
| 85 | return (d.dragSource instanceof Workspace) && (d.dragInfo instanceof FolderInfo); |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public boolean acceptDrop(DragObject d) { |
| 90 | // We can remove everything including App shortcuts, folders, widgets, etc. |
| 91 | return true; |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public void onDragStart(DragSource source, Object info, int dragAction) { |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 96 | boolean isVisible = true; |
| 97 | boolean isUninstall = false; |
| 98 | |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 99 | // If we are dragging a widget from AppsCustomize, hide the delete target |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 100 | if (isAllAppsWidget(source, info)) { |
| 101 | isVisible = false; |
| 102 | } |
| 103 | |
| 104 | // If we are dragging an application from AppsCustomize, only show the control if we can |
| 105 | // delete the app (it was downloaded), and rename the string to "uninstall" in such a case |
| 106 | if (isAllAppsApplication(source, info)) { |
| 107 | ApplicationInfo appInfo = (ApplicationInfo) info; |
| 108 | if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) != 0) { |
| 109 | isUninstall = true; |
| 110 | } else { |
| 111 | isVisible = false; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | mActive = isVisible; |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 116 | setVisibility(isVisible ? View.VISIBLE : View.GONE); |
| 117 | if (mText.getText().length() > 0) { |
| 118 | mText.setText(isUninstall ? R.string.delete_target_uninstall_label |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 119 | : R.string.delete_target_label); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public void onDragEnd() { |
| 125 | super.onDragEnd(); |
| 126 | mActive = false; |
| 127 | } |
| 128 | |
| 129 | public void onDragEnter(DragObject d) { |
| 130 | super.onDragEnter(d); |
| 131 | |
Winson Chung | 967289b | 2011-06-30 18:09:30 -0700 | [diff] [blame] | 132 | mDrawable.startTransition(mTransitionDuration); |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | public void onDragExit(DragObject d) { |
| 136 | super.onDragExit(d); |
| 137 | |
Winson Chung | 967289b | 2011-06-30 18:09:30 -0700 | [diff] [blame] | 138 | mDrawable.resetTransition(); |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | public void onDrop(DragObject d) { |
| 142 | ItemInfo item = (ItemInfo) d.dragInfo; |
| 143 | |
| 144 | if (isAllAppsApplication(d.dragSource, item)) { |
| 145 | // Uninstall the application if it is being dragged from AppsCustomize |
| 146 | mLauncher.startApplicationUninstallActivity((ApplicationInfo) item); |
Michael Jurka | 0b4870d | 2011-07-10 13:39:08 -0700 | [diff] [blame] | 147 | } else if (isWorkspaceOrFolderApplication(d)) { |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 148 | LauncherModel.deleteItemFromDatabase(mLauncher, item); |
| 149 | } else if (isWorkspaceFolder(d)) { |
| 150 | // Remove the folder from the workspace and delete the contents from launcher model |
| 151 | FolderInfo folderInfo = (FolderInfo) item; |
| 152 | mLauncher.removeFolder(folderInfo); |
| 153 | LauncherModel.deleteFolderContentsFromDatabase(mLauncher, folderInfo); |
Michael Jurka | 0b4870d | 2011-07-10 13:39:08 -0700 | [diff] [blame] | 154 | } else if (isWorkspaceOrFolderWidget(d)) { |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 155 | // Remove the widget from the workspace |
| 156 | mLauncher.removeAppWidget((LauncherAppWidgetInfo) item); |
| 157 | LauncherModel.deleteItemFromDatabase(mLauncher, item); |
| 158 | |
| 159 | final LauncherAppWidgetInfo launcherAppWidgetInfo = (LauncherAppWidgetInfo) item; |
| 160 | final LauncherAppWidgetHost appWidgetHost = mLauncher.getAppWidgetHost(); |
| 161 | if (appWidgetHost != null) { |
| 162 | // Deleting an app widget ID is a void call but writes to disk before returning |
| 163 | // to the caller... |
| 164 | new Thread("deleteAppWidgetId") { |
| 165 | public void run() { |
| 166 | appWidgetHost.deleteAppWidgetId(launcherAppWidgetInfo.appWidgetId); |
| 167 | } |
| 168 | }.start(); |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | } |