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 | |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame^] | 19 | import android.animation.ObjectAnimator; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 20 | import android.content.Context; |
| 21 | import android.content.res.Resources; |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame^] | 22 | import android.graphics.Color; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 23 | import android.graphics.PorterDuff; |
| 24 | import android.graphics.PorterDuffColorFilter; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 25 | import android.util.AttributeSet; |
| 26 | import android.view.View; |
| 27 | import android.view.ViewGroup; |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame^] | 28 | import android.widget.TextView; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 29 | |
| 30 | import com.android.launcher.R; |
| 31 | |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame^] | 32 | public class DeleteDropTarget extends ButtonDropTarget { |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 33 | |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame^] | 34 | private TextView mText; |
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 | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame^] | 57 | setBackgroundColor(mHoverColor); |
| 58 | getBackground().setAlpha(0); |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | private boolean isAllAppsApplication(DragSource source, Object info) { |
| 62 | return (source instanceof AppsCustomizePagedView) && (info instanceof ApplicationInfo); |
| 63 | } |
| 64 | private boolean isAllAppsWidget(DragSource source, Object info) { |
| 65 | return (source instanceof AppsCustomizePagedView) && (info instanceof PendingAddWidgetInfo); |
| 66 | } |
| 67 | private boolean isWorkspaceApplication(DragObject d) { |
| 68 | return (d.dragSource instanceof Workspace) && (d.dragInfo instanceof ShortcutInfo); |
| 69 | } |
| 70 | private boolean isWorkspaceWidget(DragObject d) { |
| 71 | return (d.dragSource instanceof Workspace) && (d.dragInfo instanceof LauncherAppWidgetInfo); |
| 72 | } |
| 73 | private boolean isWorkspaceFolder(DragObject d) { |
| 74 | return (d.dragSource instanceof Workspace) && (d.dragInfo instanceof FolderInfo); |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public boolean acceptDrop(DragObject d) { |
| 79 | // We can remove everything including App shortcuts, folders, widgets, etc. |
| 80 | return true; |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public void onDragStart(DragSource source, Object info, int dragAction) { |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 85 | boolean isVisible = true; |
| 86 | boolean isUninstall = false; |
| 87 | |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 88 | // If we are dragging a widget from AppsCustomize, hide the delete target |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 89 | if (isAllAppsWidget(source, info)) { |
| 90 | isVisible = false; |
| 91 | } |
| 92 | |
| 93 | // If we are dragging an application from AppsCustomize, only show the control if we can |
| 94 | // delete the app (it was downloaded), and rename the string to "uninstall" in such a case |
| 95 | if (isAllAppsApplication(source, info)) { |
| 96 | ApplicationInfo appInfo = (ApplicationInfo) info; |
| 97 | if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) != 0) { |
| 98 | isUninstall = true; |
| 99 | } else { |
| 100 | isVisible = false; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | mActive = isVisible; |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame^] | 105 | setVisibility(isVisible ? View.VISIBLE : View.GONE); |
| 106 | if (mText.getText().length() > 0) { |
| 107 | mText.setText(isUninstall ? R.string.delete_target_uninstall_label |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 108 | : R.string.delete_target_label); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public void onDragEnd() { |
| 114 | super.onDragEnd(); |
| 115 | mActive = false; |
| 116 | } |
| 117 | |
| 118 | public void onDragEnter(DragObject d) { |
| 119 | super.onDragEnter(d); |
| 120 | |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame^] | 121 | ObjectAnimator anim = ObjectAnimator.ofInt(getBackground(), "alpha", |
| 122 | Color.alpha(mHoverColor)); |
| 123 | anim.setDuration(mTransitionDuration); |
| 124 | anim.start(); |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | public void onDragExit(DragObject d) { |
| 128 | super.onDragExit(d); |
| 129 | |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame^] | 130 | ObjectAnimator anim = ObjectAnimator.ofInt(getBackground(), "alpha", 0); |
| 131 | anim.setDuration(mTransitionDuration); |
| 132 | anim.start(); |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | public void onDrop(DragObject d) { |
| 136 | ItemInfo item = (ItemInfo) d.dragInfo; |
| 137 | |
| 138 | if (isAllAppsApplication(d.dragSource, item)) { |
| 139 | // Uninstall the application if it is being dragged from AppsCustomize |
| 140 | mLauncher.startApplicationUninstallActivity((ApplicationInfo) item); |
| 141 | } else if (isWorkspaceApplication(d)) { |
| 142 | LauncherModel.deleteItemFromDatabase(mLauncher, item); |
| 143 | } else if (isWorkspaceFolder(d)) { |
| 144 | // Remove the folder from the workspace and delete the contents from launcher model |
| 145 | FolderInfo folderInfo = (FolderInfo) item; |
| 146 | mLauncher.removeFolder(folderInfo); |
| 147 | LauncherModel.deleteFolderContentsFromDatabase(mLauncher, folderInfo); |
| 148 | } else if (isWorkspaceWidget(d)) { |
| 149 | // Remove the widget from the workspace |
| 150 | mLauncher.removeAppWidget((LauncherAppWidgetInfo) item); |
| 151 | LauncherModel.deleteItemFromDatabase(mLauncher, item); |
| 152 | |
| 153 | final LauncherAppWidgetInfo launcherAppWidgetInfo = (LauncherAppWidgetInfo) item; |
| 154 | final LauncherAppWidgetHost appWidgetHost = mLauncher.getAppWidgetHost(); |
| 155 | if (appWidgetHost != null) { |
| 156 | // Deleting an app widget ID is a void call but writes to disk before returning |
| 157 | // to the caller... |
| 158 | new Thread("deleteAppWidgetId") { |
| 159 | public void run() { |
| 160 | appWidgetHost.deleteAppWidgetId(launcherAppWidgetInfo.appWidgetId); |
| 161 | } |
| 162 | }.start(); |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | } |