DeleteDropTarget.java revision 4b285c5a60028f7c9ce39cfb318a730d0ae95c27
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
17package com.android.launcher2;
18
19import android.content.Context;
20import android.content.res.ColorStateList;
21import android.content.res.Configuration;
22import android.content.res.Resources;
23import android.graphics.PorterDuff;
24import android.graphics.PorterDuffColorFilter;
25import android.graphics.Rect;
26import android.graphics.drawable.TransitionDrawable;
27import android.util.AttributeSet;
28import android.view.View;
29import android.view.animation.AccelerateInterpolator;
30import android.view.animation.DecelerateInterpolator;
31import android.widget.TextView;
32
33import com.android.launcher.R;
34
35public class DeleteDropTarget extends ButtonDropTarget {
36
37    private static int DELETE_ANIMATION_DURATION = 250;
38    private ColorStateList mOriginalTextColor;
39    private TransitionDrawable mDrawable;
40    private int mHoverColor = 0xFFFF0000;
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
55        mText = (TextView) findViewById(R.id.delete_target_text);
56        mOriginalTextColor = mText.getTextColors();
57
58        // Get the hover color
59        Resources r = getResources();
60        mHoverColor = r.getColor(R.color.delete_target_hover_tint);
61        mHoverPaint.setColorFilter(new PorterDuffColorFilter(
62                mHoverColor, PorterDuff.Mode.SRC_ATOP));
63        mDrawable = (TransitionDrawable) mText.getCompoundDrawables()[0];
64        mDrawable.setCrossFadeEnabled(true);
65
66        // Remove the text in the Phone UI in landscape
67        int orientation = getResources().getConfiguration().orientation;
68        if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
69            if (!LauncherApplication.isScreenLarge()) {
70                mText.setText("");
71            }
72        }
73    }
74
75    private boolean isAllAppsApplication(DragSource source, Object info) {
76        return (source instanceof AppsCustomizePagedView) && (info instanceof ApplicationInfo);
77    }
78    private boolean isAllAppsWidget(DragSource source, Object info) {
79        return (source instanceof AppsCustomizePagedView) && (info instanceof PendingAddWidgetInfo);
80    }
81    private boolean isDragSourceWorkspaceOrFolder(DragObject d) {
82        return (d.dragSource instanceof Workspace) || (d.dragSource instanceof Folder);
83    }
84    private boolean isWorkspaceOrFolderApplication(DragObject d) {
85        return isDragSourceWorkspaceOrFolder(d) && (d.dragInfo instanceof ShortcutInfo);
86    }
87    private boolean isWorkspaceOrFolderWidget(DragObject d) {
88        return isDragSourceWorkspaceOrFolder(d) && (d.dragInfo instanceof LauncherAppWidgetInfo);
89    }
90    private boolean isWorkspaceFolder(DragObject d) {
91        return (d.dragSource instanceof Workspace) && (d.dragInfo instanceof FolderInfo);
92    }
93
94    @Override
95    public boolean acceptDrop(DragObject d) {
96        // We can remove everything including App shortcuts, folders, widgets, etc.
97        return true;
98    }
99
100    @Override
101    public void onDragStart(DragSource source, Object info, int dragAction) {
102        boolean isVisible = true;
103        boolean isUninstall = false;
104
105        // If we are dragging a widget from AppsCustomize, hide the delete target
106        if (isAllAppsWidget(source, info)) {
107            isVisible = false;
108        }
109
110        // If we are dragging an application from AppsCustomize, only show the control if we can
111        // delete the app (it was downloaded), and rename the string to "uninstall" in such a case
112        if (isAllAppsApplication(source, info)) {
113            ApplicationInfo appInfo = (ApplicationInfo) info;
114            if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) != 0) {
115                isUninstall = true;
116            } else {
117                isVisible = false;
118            }
119        }
120
121        mActive = isVisible;
122        mDrawable.resetTransition();
123        mText.setTextColor(mOriginalTextColor);
124        setVisibility(isVisible ? View.VISIBLE : View.GONE);
125        if (mText.getText().length() > 0) {
126            mText.setText(isUninstall ? R.string.delete_target_uninstall_label
127                : R.string.delete_target_label);
128        }
129    }
130
131    @Override
132    public void onDragEnd() {
133        super.onDragEnd();
134        mActive = false;
135    }
136
137    public void onDragEnter(DragObject d) {
138        super.onDragEnter(d);
139
140        mDrawable.startTransition(mTransitionDuration);
141        mText.setTextColor(mHoverColor);
142    }
143
144    public void onDragExit(DragObject d) {
145        super.onDragExit(d);
146
147        if (!d.dragComplete) {
148            mDrawable.resetTransition();
149            mText.setTextColor(mOriginalTextColor);
150        }
151    }
152
153    private void animateToTrashAndCompleteDrop(final DragObject d) {
154        DragLayer dragLayer = mLauncher.getDragLayer();
155        Rect from = new Rect();
156        Rect to = new Rect();
157        dragLayer.getViewRectRelativeToSelf(d.dragView, from);
158        dragLayer.getViewRectRelativeToSelf(mText, to);
159
160        int width = mDrawable.getIntrinsicWidth();
161        int height = mDrawable.getIntrinsicHeight();
162        to.set(to.left, to.top, to.left + width, to.bottom);
163
164        // Center the destination rect about the trash icon
165        int xOffset = (int) -(d.dragView.getMeasuredWidth() - width) / 2;
166        int yOffset = (int) -(d.dragView.getMeasuredHeight() - height) / 2;
167        to.offset(xOffset, yOffset);
168
169        mSearchDropTargetBar.deferOnDragEnd();
170        Runnable onAnimationEndRunnable = new Runnable() {
171            @Override
172            public void run() {
173                mSearchDropTargetBar.onDragEnd();
174                mLauncher.exitSpringLoadedDragMode();
175                completeDrop(d);
176            }
177        };
178        dragLayer.animateView(d.dragView, from, to, 0.1f, 0.1f,
179                DELETE_ANIMATION_DURATION, new DecelerateInterpolator(2),
180                new DecelerateInterpolator(1.5f), onAnimationEndRunnable, false);
181    }
182
183    private void completeDrop(DragObject d) {
184        ItemInfo item = (ItemInfo) d.dragInfo;
185
186        if (isAllAppsApplication(d.dragSource, item)) {
187            // Uninstall the application if it is being dragged from AppsCustomize
188            mLauncher.startApplicationUninstallActivity((ApplicationInfo) item);
189        } else if (isWorkspaceOrFolderApplication(d)) {
190            LauncherModel.deleteItemFromDatabase(mLauncher, item);
191        } else if (isWorkspaceFolder(d)) {
192            // Remove the folder from the workspace and delete the contents from launcher model
193            FolderInfo folderInfo = (FolderInfo) item;
194            mLauncher.removeFolder(folderInfo);
195            LauncherModel.deleteFolderContentsFromDatabase(mLauncher, folderInfo);
196        } else if (isWorkspaceOrFolderWidget(d)) {
197            // Remove the widget from the workspace
198            mLauncher.removeAppWidget((LauncherAppWidgetInfo) item);
199            LauncherModel.deleteItemFromDatabase(mLauncher, item);
200
201            final LauncherAppWidgetInfo launcherAppWidgetInfo = (LauncherAppWidgetInfo) item;
202            final LauncherAppWidgetHost appWidgetHost = mLauncher.getAppWidgetHost();
203            if (appWidgetHost != null) {
204                // Deleting an app widget ID is a void call but writes to disk before returning
205                // to the caller...
206                new Thread("deleteAppWidgetId") {
207                    public void run() {
208                        appWidgetHost.deleteAppWidgetId(launcherAppWidgetInfo.appWidgetId);
209                    }
210                }.start();
211            }
212        }
213    }
214
215    public void onDrop(DragObject d) {
216        animateToTrashAndCompleteDrop(d);
217    }
218}
219