summaryrefslogtreecommitdiffstats
path: root/src/com/android/launcher2/DeleteZone.java
blob: 5d9b5db2de6ad49cdbe906dd6a29b01347154bd4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.launcher2;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.TransitionDrawable;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AccelerateInterpolator;

import com.android.launcher.R;

public class DeleteZone extends IconDropTarget {
    private static final int ORIENTATION_HORIZONTAL = 1;
    private static final int TRANSITION_DURATION = 250;
    private static final int ANIMATION_DURATION = 200;
    private static final int XLARGE_TRANSITION_DURATION = 150;
    private static final int XLARGE_ANIMATION_DURATION = 200;
    private static final int LEFT_DRAWABLE = 0;

    private AnimatorSet mInAnimation;
    private AnimatorSet mOutAnimation;

    private int mOrientation;
    private DragController mDragController;

    private final RectF mRegionF = new RectF();
    private final Rect mRegion = new Rect();
    private TransitionDrawable mTransition;
    private int mTextColor;
    private int mDragTextColor;

    public DeleteZone(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DeleteZone(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        final int srcColor = context.getResources().getColor(R.color.delete_color_filter);
        mHoverPaint.setColorFilter(new PorterDuffColorFilter(srcColor, PorterDuff.Mode.SRC_ATOP));

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DeleteZone, defStyle, 0);
        mOrientation = a.getInt(R.styleable.DeleteZone_direction, ORIENTATION_HORIZONTAL);
        a.recycle();

        if (LauncherApplication.isScreenLarge()) {
            int tb = getResources().getDimensionPixelSize(
                    R.dimen.delete_zone_vertical_drag_padding);
            int lr = getResources().getDimensionPixelSize(
                    R.dimen.delete_zone_horizontal_drag_padding);
            setDragPadding(tb, lr, tb, lr);
        }
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mTransition = (TransitionDrawable) getCompoundDrawables()[LEFT_DRAWABLE];
        if (LauncherApplication.isScreenLarge()) {
            mTransition.setCrossFadeEnabled(false);
        }

        Resources r = getResources();
        mTextColor = r.getColor(R.color.workspace_all_apps_and_delete_zone_text_color);
        mDragTextColor = r.getColor(R.color.workspace_delete_zone_drag_text_color);
    }

    public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset,
            DragView dragView, Object dragInfo) {
        return true;
    }

    public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset,
            DragView dragView, Object dragInfo) {
        if (!mDragAndDropEnabled) return;

        final ItemInfo item = (ItemInfo) dragInfo;

        // On x-large screens, you can uninstall an app by dragging from all apps
        if (item instanceof ApplicationInfo && LauncherApplication.isScreenLarge()) {
            mLauncher.startApplicationUninstallActivity((ApplicationInfo) item);
        }

        if (item.container == -1) return;

        if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
            if (item instanceof LauncherAppWidgetInfo) {
                mLauncher.removeAppWidget((LauncherAppWidgetInfo) item);
            }
        }

        if (item instanceof FolderInfo) {
            final FolderInfo folderInfo = (FolderInfo)item;
            LauncherModel.deleteFolderContentsFromDatabase(mLauncher, folderInfo);
            mLauncher.removeFolder(folderInfo);
        } else if (item instanceof LauncherAppWidgetInfo) {
            final LauncherAppWidgetInfo launcherAppWidgetInfo = (LauncherAppWidgetInfo) item;
            final LauncherAppWidgetHost appWidgetHost = mLauncher.getAppWidgetHost();
            if (appWidgetHost != null) {
                // Deleting an app widget ID is a void call but writes to disk before returning
                // to the caller...
                new Thread("deleteAppWidgetId") {
                    public void run() {
                        appWidgetHost.deleteAppWidgetId(launcherAppWidgetInfo.appWidgetId);
                    }
                }.start();
            }
        }

        LauncherModel.deleteItemFromDatabase(mLauncher, item);
    }

    public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset,
            DragView dragView, Object dragInfo) {
        if (mDragAndDropEnabled) {
            mTransition.reverseTransition(getTransitionAnimationDuration());
            setTextColor(mDragTextColor);
            super.onDragEnter(source, x, y, xOffset, yOffset, dragView, dragInfo);
        }
    }

    public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset,
            DragView dragView, Object dragInfo) {
        if (mDragAndDropEnabled) {
            mTransition.reverseTransition(getTransitionAnimationDuration());
            setTextColor(mTextColor);
            super.onDragExit(source, x, y, xOffset, yOffset, dragView, dragInfo);
        }
    }

    public void onDragStart(DragSource source, Object info, int dragAction) {
        final ItemInfo item = (ItemInfo) info;
        if (item != null && mDragAndDropEnabled) {
            mActive = true;
            getHitRect(mRegion);
            mRegionF.set(mRegion);

            if (LauncherApplication.isScreenLarge()) {
                // This region will be a "dead zone" for scrolling; make it extend to the edge of
                // the screen so users don't accidentally trigger a scroll while deleting items
                mRegionF.top = mLauncher.getWorkspace().getTop();
                mRegionF.right = mLauncher.getWorkspace().getRight();
            }

            mDragController.setDeleteRegion(mRegionF);

            // Make sure the icon is set to the default drawable, not the hover drawable
            mTransition.resetTransition();

            createAnimations();
            mInAnimation.start();
            if (mOverlappingViews != null) {
                for (View view : mOverlappingViews) {
                    createOutAlphaAnim(view).start();
                }
            }
            setVisibility(VISIBLE);
        }
    }

    public void onDragEnd() {
        if (mActive && mDragAndDropEnabled) {
            mActive = false;
            mDragController.setDeleteRegion(null);

            mOutAnimation.start();
            if (mOverlappingViews != null) {
                for (View view : mOverlappingViews) {
                    createInAlphaAnim(view).start();
                }
            }
        }
    }

    private Animator createAlphaAnim(View v, float start, float end) {
        Animator anim = ObjectAnimator.ofFloat(v, "alpha", start, end);
        anim.setDuration(getAnimationDuration());
        return anim;
    }
    private Animator createInAlphaAnim(View v) {
        return createAlphaAnim(v, 0f, 1f);
    }
    private Animator createOutAlphaAnim(View v) {
        return createAlphaAnim(v, 1f, 0f);
    }

    private void createAnimations() {
        int duration = getAnimationDuration();

        Animator inAlphaAnim = createInAlphaAnim(this);
        if (mInAnimation == null) {
            mInAnimation = new AnimatorSet();
            mInAnimation.setInterpolator(new AccelerateInterpolator());
            mInAnimation.setDuration(duration);
            if (!LauncherApplication.isScreenLarge()) {
                Animator translateAnim;
                if (mOrientation == ORIENTATION_HORIZONTAL) {
                    translateAnim = ObjectAnimator.ofFloat(this, "translationY", 
                            getMeasuredWidth(), 0f);
                } else {
                    translateAnim = ObjectAnimator.ofFloat(this, "translationX", 
                            getMeasuredHeight(), 0f);
                }
                mInAnimation.playTogether(translateAnim, inAlphaAnim);
            } else {
                mInAnimation.play(inAlphaAnim);
            }
        }

        Animator outAlphaAnim = createOutAlphaAnim(this);
        if (mOutAnimation == null) {
            mOutAnimation = new AnimatorSet();
            mOutAnimation.setInterpolator(new AccelerateInterpolator());
            mOutAnimation.setDuration(duration);
            if (!LauncherApplication.isScreenLarge()) {
                Animator translateAnim;
                if (mOrientation == ORIENTATION_HORIZONTAL) {
                    translateAnim = ObjectAnimator.ofFloat(this, "translationY", 0f, 
                            getMeasuredWidth());
                } else {
                    translateAnim = ObjectAnimator.ofFloat(this, "translationX", 0f, 
                            getMeasuredHeight());
                }
                mOutAnimation.playTogether(translateAnim, outAlphaAnim);
            } else {
                mOutAnimation.addListener(new AnimatorListenerAdapter() {
                    public void onAnimationEnd(Animator animation) {
                        setVisibility(GONE);
                    }
                });
                mOutAnimation.play(outAlphaAnim);
            }
        }
    }

    void setDragController(DragController dragController) {
        mDragController = dragController;
    }

    private int getTransitionAnimationDuration() {
        return LauncherApplication.isScreenLarge() ?
                XLARGE_TRANSITION_DURATION : TRANSITION_DURATION;
    }

    private int getAnimationDuration() {
        return LauncherApplication.isScreenLarge() ?
                XLARGE_ANIMATION_DURATION : ANIMATION_DURATION;
    }
}