DeleteDropTarget.java revision f0ea4d3378be7b962c8e0bce2392df5e82491fb8
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.Resources;
21import android.graphics.PorterDuff;
22import android.graphics.PorterDuffColorFilter;
23import android.graphics.drawable.TransitionDrawable;
24import android.util.AttributeSet;
25import android.view.View;
26import android.view.ViewGroup;
27
28import com.android.launcher.R;
29
30public class DeleteDropTarget extends IconDropTarget {
31
32    private static final int sTransitionDuration = 0;
33
34    private TransitionDrawable mIcon;
35    private int mDefaultTextColor;
36    private int mHoverColor = 0xFFFF0000;
37
38    public DeleteDropTarget(Context context, AttributeSet attrs) {
39        this(context, attrs, 0);
40    }
41
42    public DeleteDropTarget(Context context, AttributeSet attrs, int defStyle) {
43        super(context, attrs, defStyle);
44    }
45
46    @Override
47    protected void onFinishInflate() {
48        super.onFinishInflate();
49
50        // Get the drawable
51        mIcon = (TransitionDrawable) getCompoundDrawables()[0];
52
53        // Get the hover color
54        Resources r = getResources();
55        mDefaultTextColor = getTextColors().getDefaultColor();
56        mHoverColor = r.getColor(R.color.delete_target_hover_tint);
57        mHoverPaint.setColorFilter(new PorterDuffColorFilter(
58                mHoverColor, PorterDuff.Mode.SRC_ATOP));
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) {
85        ItemInfo item = (ItemInfo) info;
86        boolean isVisible = true;
87        boolean isUninstall = false;
88
89        // If we are dragging a widget from AppsCustomize, hide the delete target
90        if (isAllAppsWidget(source, info)) {
91            isVisible = false;
92        }
93
94        // If we are dragging an application from AppsCustomize, only show the control if we can
95        // delete the app (it was downloaded), and rename the string to "uninstall" in such a case
96        if (isAllAppsApplication(source, info)) {
97            ApplicationInfo appInfo = (ApplicationInfo) info;
98            if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) != 0) {
99                isUninstall = true;
100            } else {
101                isVisible = false;
102            }
103        }
104
105        mActive = isVisible;
106        ((ViewGroup) getParent()).setVisibility(isVisible ? View.VISIBLE : View.GONE);
107        if (getText().length() > 0) {
108            setText(isUninstall ? R.string.delete_target_uninstall_label
109                : R.string.delete_target_label);
110        }
111    }
112
113    @Override
114    public void onDragEnd() {
115        super.onDragEnd();
116        mActive = false;
117    }
118
119    public void onDragEnter(DragObject d) {
120        super.onDragEnter(d);
121
122        mIcon.startTransition(sTransitionDuration);
123        setTextColor(mHoverColor);
124    }
125
126    public void onDragExit(DragObject d) {
127        super.onDragExit(d);
128
129        mIcon.resetTransition();
130        setTextColor(mDefaultTextColor);
131    }
132
133    public void onDrop(DragObject d) {
134        ItemInfo item = (ItemInfo) d.dragInfo;
135
136        if (isAllAppsApplication(d.dragSource, item)) {
137            // Uninstall the application if it is being dragged from AppsCustomize
138            mLauncher.startApplicationUninstallActivity((ApplicationInfo) item);
139        } else if (isWorkspaceApplication(d)) {
140            LauncherModel.deleteItemFromDatabase(mLauncher, item);
141        } else if (isWorkspaceFolder(d)) {
142            // Remove the folder from the workspace and delete the contents from launcher model
143            FolderInfo folderInfo = (FolderInfo) item;
144            mLauncher.removeFolder(folderInfo);
145            LauncherModel.deleteFolderContentsFromDatabase(mLauncher, folderInfo);
146        } else if (isWorkspaceWidget(d)) {
147            // Remove the widget from the workspace
148            mLauncher.removeAppWidget((LauncherAppWidgetInfo) item);
149            LauncherModel.deleteItemFromDatabase(mLauncher, item);
150
151            final LauncherAppWidgetInfo launcherAppWidgetInfo = (LauncherAppWidgetInfo) item;
152            final LauncherAppWidgetHost appWidgetHost = mLauncher.getAppWidgetHost();
153            if (appWidgetHost != null) {
154                // Deleting an app widget ID is a void call but writes to disk before returning
155                // to the caller...
156                new Thread("deleteAppWidgetId") {
157                    public void run() {
158                        appWidgetHost.deleteAppWidgetId(launcherAppWidgetInfo.appWidgetId);
159                    }
160                }.start();
161            }
162        }
163    }
164}
165