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