blob: ffe453319a48d1eadd0e02234bb1316293cd992b [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;
Winson Chung967289b2011-06-30 18:09:30 -070025import android.graphics.drawable.TransitionDrawable;
Winson Chung4c98d922011-05-31 16:50:48 -070026import android.util.AttributeSet;
27import android.view.View;
Winson Chung61fa4192011-06-12 15:15:29 -070028import android.widget.TextView;
Winson Chung4c98d922011-05-31 16:50:48 -070029
30import com.android.launcher.R;
31
Winson Chung61fa4192011-06-12 15:15:29 -070032public class DeleteDropTarget extends ButtonDropTarget {
Winson Chung4c98d922011-05-31 16:50:48 -070033
Winson Chung61fa4192011-06-12 15:15:29 -070034 private TextView mText;
Winson Chunga62e9fd2011-07-11 15:20:48 -070035 private ColorStateList mOriginalTextColor;
Winson Chung967289b2011-06-30 18:09:30 -070036 private TransitionDrawable mDrawable;
Winson Chung4c98d922011-05-31 16:50:48 -070037 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 Chung61fa4192011-06-12 15:15:29 -070052 mText = (TextView) findViewById(R.id.delete_target_text);
Winson Chunga62e9fd2011-07-11 15:20:48 -070053 mOriginalTextColor = mText.getTextColors();
Winson Chung4c98d922011-05-31 16:50:48 -070054
55 // Get the hover color
56 Resources r = getResources();
Winson Chung4c98d922011-05-31 16:50:48 -070057 mHoverColor = r.getColor(R.color.delete_target_hover_tint);
58 mHoverPaint.setColorFilter(new PorterDuffColorFilter(
59 mHoverColor, PorterDuff.Mode.SRC_ATOP));
Winson Chung967289b2011-06-30 18:09:30 -070060 mDrawable = (TransitionDrawable) mText.getCompoundDrawables()[0];
61 mDrawable.setCrossFadeEnabled(true);
Winson Chung201bc822011-06-20 15:41:53 -070062
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 Chung4c98d922011-05-31 16:50:48 -070070 }
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 Jurka0b4870d2011-07-10 13:39:08 -070078 private boolean isDragSourceWorkspaceOrFolder(DragObject d) {
79 return (d.dragSource instanceof Workspace) || (d.dragSource instanceof Folder);
Winson Chung4c98d922011-05-31 16:50:48 -070080 }
Michael Jurka0b4870d2011-07-10 13:39:08 -070081 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 Chung4c98d922011-05-31 16:50:48 -070086 }
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 Chung4c98d922011-05-31 16:50:48 -070099 boolean isVisible = true;
100 boolean isUninstall = false;
101
Winson Chungf0ea4d32011-06-06 14:27:16 -0700102 // If we are dragging a widget from AppsCustomize, hide the delete target
Winson Chung4c98d922011-05-31 16:50:48 -0700103 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 Chungaaa530a2011-07-11 21:06:30 -0700119 mDrawable.resetTransition();
Winson Chunga62e9fd2011-07-11 15:20:48 -0700120 mText.setTextColor(mOriginalTextColor);
Winson Chung61fa4192011-06-12 15:15:29 -0700121 setVisibility(isVisible ? View.VISIBLE : View.GONE);
122 if (mText.getText().length() > 0) {
123 mText.setText(isUninstall ? R.string.delete_target_uninstall_label
Winson Chung4c98d922011-05-31 16:50:48 -0700124 : 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 Chung967289b2011-06-30 18:09:30 -0700137 mDrawable.startTransition(mTransitionDuration);
Winson Chunga62e9fd2011-07-11 15:20:48 -0700138 mText.setTextColor(mHoverColor);
Winson Chung4c98d922011-05-31 16:50:48 -0700139 }
140
141 public void onDragExit(DragObject d) {
142 super.onDragExit(d);
143
Winson Chungaaa530a2011-07-11 21:06:30 -0700144 if (!d.dragComplete) {
145 mDrawable.resetTransition();
Winson Chunga62e9fd2011-07-11 15:20:48 -0700146 mText.setTextColor(mOriginalTextColor);
Winson Chungaaa530a2011-07-11 21:06:30 -0700147 }
Winson Chung4c98d922011-05-31 16:50:48 -0700148 }
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 Jurka0b4870d2011-07-10 13:39:08 -0700156 } else if (isWorkspaceOrFolderApplication(d)) {
Winson Chung4c98d922011-05-31 16:50:48 -0700157 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 Jurka0b4870d2011-07-10 13:39:08 -0700163 } else if (isWorkspaceOrFolderWidget(d)) {
Winson Chung4c98d922011-05-31 16:50:48 -0700164 // 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}