blob: fa713a41ae1f5f6625f1acf29cb859fb4c415ee8 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 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
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Adam Cohen2801caf2011-05-13 20:57:39 -070019import android.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
Adam Cohen2801caf2011-05-13 20:57:39 -070021import android.animation.ValueAnimator;
22import android.animation.ValueAnimator.AnimatorUpdateListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080023import android.content.Context;
24import android.content.res.Resources;
Adam Cohena9cf38f2011-05-02 15:36:58 -070025import android.graphics.Canvas;
Adam Cohenf4b08912011-05-17 18:45:47 -070026import android.graphics.Color;
Adam Cohenbadf71e2011-05-26 19:08:29 -070027import android.graphics.PorterDuff;
Adam Cohen3e8f8112011-07-02 18:03:00 -070028import android.graphics.Rect;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.graphics.drawable.Drawable;
Daniel Sandler24ad0f02013-07-31 00:14:58 -040030import android.os.Looper;
Adam Cohen099f60d2011-08-23 21:07:26 -070031import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.util.AttributeSet;
33import android.view.LayoutInflater;
Winson Chung88f33452012-02-23 15:23:44 -080034import android.view.MotionEvent;
Adam Cohen7c693212011-05-18 15:26:57 -070035import android.view.View;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.view.ViewGroup;
Adam Cohen3e8f8112011-07-02 18:03:00 -070037import android.view.animation.AccelerateInterpolator;
38import android.view.animation.DecelerateInterpolator;
Adam Cohen76fc0852011-06-17 13:26:23 -070039import android.widget.ImageView;
40import android.widget.LinearLayout;
Adam Cohena9cf38f2011-05-02 15:36:58 -070041import android.widget.TextView;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042
Daniel Sandler325dc232013-06-05 22:57:57 -040043import com.android.launcher3.R;
44import com.android.launcher3.DropTarget.DragObject;
45import com.android.launcher3.FolderInfo.FolderListener;
Romain Guyedcce092010-03-04 13:03:17 -080046
Adam Cohenc0dcf592011-06-01 15:30:43 -070047import java.util.ArrayList;
48
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049/**
50 * An icon that can appear on in the workspace representing an {@link UserFolder}.
51 */
Adam Cohen76fc0852011-06-17 13:26:23 -070052public class FolderIcon extends LinearLayout implements FolderListener {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053 private Launcher mLauncher;
Adam Cohenfb91f302012-06-11 15:45:18 -070054 private Folder mFolder;
55 private FolderInfo mInfo;
Adam Cohen099f60d2011-08-23 21:07:26 -070056 private static boolean sStaticValuesDirty = true;
Adam Cohena9cf38f2011-05-02 15:36:58 -070057
Winson Chung88f33452012-02-23 15:23:44 -080058 private CheckLongPressHelper mLongPressHelper;
59
Adam Cohenbadf71e2011-05-26 19:08:29 -070060 // The number of icons to display in the
Adam Cohen76fc0852011-06-17 13:26:23 -070061 private static final int NUM_ITEMS_IN_PREVIEW = 3;
Adam Cohenf4b08912011-05-17 18:45:47 -070062 private static final int CONSUMPTION_ANIMATION_DURATION = 100;
Adam Cohend0445262011-07-04 23:53:22 -070063 private static final int DROP_IN_ANIMATION_DURATION = 400;
Adam Cohen8dfcba42011-07-07 16:38:18 -070064 private static final int INITIAL_ITEM_ANIMATION_DURATION = 350;
Adam Cohenfb91f302012-06-11 15:45:18 -070065 private static final int FINAL_ITEM_ANIMATION_DURATION = 200;
Adam Cohenbadf71e2011-05-26 19:08:29 -070066
67 // The degree to which the inner ring grows when accepting drop
Adam Cohen69ce2e52011-07-03 19:25:21 -070068 private static final float INNER_RING_GROWTH_FACTOR = 0.15f;
Adam Cohenbadf71e2011-05-26 19:08:29 -070069
Adam Cohenbadf71e2011-05-26 19:08:29 -070070 // The degree to which the outer ring is scaled in its natural state
Adam Cohen69ce2e52011-07-03 19:25:21 -070071 private static final float OUTER_RING_GROWTH_FACTOR = 0.3f;
Adam Cohenbadf71e2011-05-26 19:08:29 -070072
73 // The amount of vertical spread between items in the stack [0...1]
Adam Cohen76fc0852011-06-17 13:26:23 -070074 private static final float PERSPECTIVE_SHIFT_FACTOR = 0.24f;
Adam Cohenbadf71e2011-05-26 19:08:29 -070075
76 // The degree to which the item in the back of the stack is scaled [0...1]
77 // (0 means it's not scaled at all, 1 means it's scaled to nothing)
Adam Cohen76fc0852011-06-17 13:26:23 -070078 private static final float PERSPECTIVE_SCALE_FACTOR = 0.35f;
Adam Cohenbadf71e2011-05-26 19:08:29 -070079
Adam Cohenc51934b2011-07-26 21:07:43 -070080 public static Drawable sSharedFolderLeaveBehind = null;
81
Adam Cohen76fc0852011-06-17 13:26:23 -070082 private ImageView mPreviewBackground;
83 private BubbleTextView mFolderName;
Adam Cohen073a46f2011-05-17 16:28:09 -070084
Adam Cohen19072da2011-05-31 14:30:45 -070085 FolderRingAnimator mFolderRingAnimator = null;
Adam Cohen2801caf2011-05-13 20:57:39 -070086
Adam Cohend0445262011-07-04 23:53:22 -070087 // These variables are all associated with the drawing of the preview; they are stored
88 // as member variables for shared usage and to avoid computation on each frame
89 private int mIntrinsicIconSize;
90 private float mBaselineIconScale;
91 private int mBaselineIconSize;
92 private int mAvailableSpaceInPreview;
93 private int mTotalWidth = -1;
94 private int mPreviewOffsetX;
95 private int mPreviewOffsetY;
96 private float mMaxPerspectiveShift;
97 boolean mAnimating = false;
Adam Cohenfb91f302012-06-11 15:45:18 -070098
Adam Cohend0445262011-07-04 23:53:22 -070099 private PreviewItemDrawingParams mParams = new PreviewItemDrawingParams(0, 0, 0, 0);
100 private PreviewItemDrawingParams mAnimParams = new PreviewItemDrawingParams(0, 0, 0, 0);
Adam Cohenfb91f302012-06-11 15:45:18 -0700101 private ArrayList<ShortcutInfo> mHiddenItems = new ArrayList<ShortcutInfo>();
Adam Cohend0445262011-07-04 23:53:22 -0700102
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800103 public FolderIcon(Context context, AttributeSet attrs) {
104 super(context, attrs);
Winson Chung88f33452012-02-23 15:23:44 -0800105 init();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800106 }
107
108 public FolderIcon(Context context) {
109 super(context);
Winson Chung88f33452012-02-23 15:23:44 -0800110 init();
111 }
112
113 private void init() {
114 mLongPressHelper = new CheckLongPressHelper(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800115 }
116
Michael Jurka0280c3b2010-09-17 15:00:07 -0700117 public boolean isDropEnabled() {
Winson Chung7a1d1652011-03-18 15:56:01 -0700118 final ViewGroup cellLayoutChildren = (ViewGroup) getParent();
119 final ViewGroup cellLayout = (ViewGroup) cellLayoutChildren.getParent();
120 final Workspace workspace = (Workspace) cellLayout.getParent();
121 return !workspace.isSmall();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700122 }
123
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800124 static FolderIcon fromXml(int resId, Launcher launcher, ViewGroup group,
Adam Cohendf2cc412011-04-27 16:56:57 -0700125 FolderInfo folderInfo, IconCache iconCache) {
Michael Jurka3a9fced2012-04-13 14:44:29 -0700126 @SuppressWarnings("all") // suppress dead code warning
127 final boolean error = INITIAL_ITEM_ANIMATION_DURATION >= DROP_IN_ANIMATION_DURATION;
128 if (error) {
Adam Cohend0445262011-07-04 23:53:22 -0700129 throw new IllegalStateException("DROP_IN_ANIMATION_DURATION must be greater than " +
130 "INITIAL_ITEM_ANIMATION_DURATION, as sequencing of adding first two items " +
131 "is dependent on this");
132 }
133
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134 FolderIcon icon = (FolderIcon) LayoutInflater.from(launcher).inflate(resId, group, false);
135
Adam Cohend2eca6b2011-07-26 22:49:13 -0700136 icon.mFolderName = (BubbleTextView) icon.findViewById(R.id.folder_icon_name);
Adam Cohen76fc0852011-06-17 13:26:23 -0700137 icon.mFolderName.setText(folderInfo.title);
138 icon.mPreviewBackground = (ImageView) icon.findViewById(R.id.preview_background);
139
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140 icon.setTag(folderInfo);
141 icon.setOnClickListener(launcher);
142 icon.mInfo = folderInfo;
143 icon.mLauncher = launcher;
Adam Cohen3371da02011-10-25 21:38:29 -0700144 icon.setContentDescription(String.format(launcher.getString(R.string.folder_name_format),
145 folderInfo.title));
Adam Cohena9cf38f2011-05-02 15:36:58 -0700146 Folder folder = Folder.fromXml(launcher);
147 folder.setDragController(launcher.getDragController());
Adam Cohen2801caf2011-05-13 20:57:39 -0700148 folder.setFolderIcon(icon);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700149 folder.bind(folderInfo);
150 icon.mFolder = folder;
Adam Cohen099f60d2011-08-23 21:07:26 -0700151
Adam Cohen099f60d2011-08-23 21:07:26 -0700152 icon.mFolderRingAnimator = new FolderRingAnimator(launcher, icon);
153 folderInfo.addListener(icon);
154
Adam Cohen19072da2011-05-31 14:30:45 -0700155 return icon;
156 }
157
Adam Cohen099f60d2011-08-23 21:07:26 -0700158 @Override
159 protected Parcelable onSaveInstanceState() {
160 sStaticValuesDirty = true;
161 return super.onSaveInstanceState();
162 }
163
Adam Cohen19072da2011-05-31 14:30:45 -0700164 public static class FolderRingAnimator {
Adam Cohen69ce2e52011-07-03 19:25:21 -0700165 public int mCellX;
166 public int mCellY;
167 private CellLayout mCellLayout;
Adam Cohen76fc0852011-06-17 13:26:23 -0700168 public float mOuterRingSize;
169 public float mInnerRingSize;
Adam Cohen19072da2011-05-31 14:30:45 -0700170 public FolderIcon mFolderIcon = null;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700171 public static Drawable sSharedOuterRingDrawable = null;
172 public static Drawable sSharedInnerRingDrawable = null;
Adam Cohen76fc0852011-06-17 13:26:23 -0700173 public static int sPreviewSize = -1;
174 public static int sPreviewPadding = -1;
175
Adam Cohenc0dcf592011-06-01 15:30:43 -0700176 private ValueAnimator mAcceptAnimator;
177 private ValueAnimator mNeutralAnimator;
Adam Cohen19072da2011-05-31 14:30:45 -0700178
179 public FolderRingAnimator(Launcher launcher, FolderIcon folderIcon) {
Adam Cohen19072da2011-05-31 14:30:45 -0700180 mFolderIcon = folderIcon;
Adam Cohen76fc0852011-06-17 13:26:23 -0700181 Resources res = launcher.getResources();
Adam Cohen76fc0852011-06-17 13:26:23 -0700182
Adam Cohen099f60d2011-08-23 21:07:26 -0700183 // We need to reload the static values when configuration changes in case they are
184 // different in another configuration
185 if (sStaticValuesDirty) {
Daniel Sandler24ad0f02013-07-31 00:14:58 -0400186 if (Looper.myLooper() != Looper.getMainLooper()) {
187 throw new RuntimeException("FolderRingAnimator loading drawables on non-UI thread "
188 + Thread.currentThread());
189 }
Adam Cohen76fc0852011-06-17 13:26:23 -0700190 sPreviewSize = res.getDimensionPixelSize(R.dimen.folder_preview_size);
191 sPreviewPadding = res.getDimensionPixelSize(R.dimen.folder_preview_padding);
Adam Cohen76fc0852011-06-17 13:26:23 -0700192 sSharedOuterRingDrawable = res.getDrawable(R.drawable.portal_ring_outer_holo);
Adam Cohen76fc0852011-06-17 13:26:23 -0700193 sSharedInnerRingDrawable = res.getDrawable(R.drawable.portal_ring_inner_holo);
Adam Cohendf6af572011-10-19 14:38:16 -0700194 sSharedFolderLeaveBehind = res.getDrawable(R.drawable.portal_ring_rest);
Adam Cohen099f60d2011-08-23 21:07:26 -0700195 sStaticValuesDirty = false;
Adam Cohen19072da2011-05-31 14:30:45 -0700196 }
Adam Cohen073a46f2011-05-17 16:28:09 -0700197 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700198
Adam Cohen19072da2011-05-31 14:30:45 -0700199 public void animateToAcceptState() {
Adam Cohenc0dcf592011-06-01 15:30:43 -0700200 if (mNeutralAnimator != null) {
201 mNeutralAnimator.cancel();
202 }
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100203 mAcceptAnimator = LauncherAnimUtils.ofFloat(mCellLayout, 0f, 1f);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700204 mAcceptAnimator.setDuration(CONSUMPTION_ANIMATION_DURATION);
Andrew Flynn850d2e72012-04-26 16:51:20 -0700205
206 final int previewSize = sPreviewSize;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700207 mAcceptAnimator.addUpdateListener(new AnimatorUpdateListener() {
Adam Cohen19072da2011-05-31 14:30:45 -0700208 public void onAnimationUpdate(ValueAnimator animation) {
209 final float percent = (Float) animation.getAnimatedValue();
Andrew Flynn850d2e72012-04-26 16:51:20 -0700210 mOuterRingSize = (1 + percent * OUTER_RING_GROWTH_FACTOR) * previewSize;
211 mInnerRingSize = (1 + percent * INNER_RING_GROWTH_FACTOR) * previewSize;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700212 if (mCellLayout != null) {
213 mCellLayout.invalidate();
Adam Cohen19072da2011-05-31 14:30:45 -0700214 }
215 }
216 });
Adam Cohenc0dcf592011-06-01 15:30:43 -0700217 mAcceptAnimator.addListener(new AnimatorListenerAdapter() {
Adam Cohen19072da2011-05-31 14:30:45 -0700218 @Override
Adam Cohenc0dcf592011-06-01 15:30:43 -0700219 public void onAnimationStart(Animator animation) {
Adam Cohen19072da2011-05-31 14:30:45 -0700220 if (mFolderIcon != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700221 mFolderIcon.mPreviewBackground.setVisibility(INVISIBLE);
Adam Cohen19072da2011-05-31 14:30:45 -0700222 }
223 }
224 });
Adam Cohenc0dcf592011-06-01 15:30:43 -0700225 mAcceptAnimator.start();
Adam Cohen19072da2011-05-31 14:30:45 -0700226 }
227
228 public void animateToNaturalState() {
Adam Cohenc0dcf592011-06-01 15:30:43 -0700229 if (mAcceptAnimator != null) {
230 mAcceptAnimator.cancel();
231 }
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100232 mNeutralAnimator = LauncherAnimUtils.ofFloat(mCellLayout, 0f, 1f);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700233 mNeutralAnimator.setDuration(CONSUMPTION_ANIMATION_DURATION);
Andrew Flynn850d2e72012-04-26 16:51:20 -0700234
235 final int previewSize = sPreviewSize;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700236 mNeutralAnimator.addUpdateListener(new AnimatorUpdateListener() {
Adam Cohen19072da2011-05-31 14:30:45 -0700237 public void onAnimationUpdate(ValueAnimator animation) {
238 final float percent = (Float) animation.getAnimatedValue();
Andrew Flynn850d2e72012-04-26 16:51:20 -0700239 mOuterRingSize = (1 + (1 - percent) * OUTER_RING_GROWTH_FACTOR) * previewSize;
240 mInnerRingSize = (1 + (1 - percent) * INNER_RING_GROWTH_FACTOR) * previewSize;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700241 if (mCellLayout != null) {
242 mCellLayout.invalidate();
Adam Cohen19072da2011-05-31 14:30:45 -0700243 }
244 }
245 });
Adam Cohenc0dcf592011-06-01 15:30:43 -0700246 mNeutralAnimator.addListener(new AnimatorListenerAdapter() {
Adam Cohen19072da2011-05-31 14:30:45 -0700247 @Override
248 public void onAnimationEnd(Animator animation) {
Adam Cohen69ce2e52011-07-03 19:25:21 -0700249 if (mCellLayout != null) {
250 mCellLayout.hideFolderAccept(FolderRingAnimator.this);
251 }
Adam Cohen19072da2011-05-31 14:30:45 -0700252 if (mFolderIcon != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700253 mFolderIcon.mPreviewBackground.setVisibility(VISIBLE);
Adam Cohen19072da2011-05-31 14:30:45 -0700254 }
Adam Cohen19072da2011-05-31 14:30:45 -0700255 }
256 });
Adam Cohenc0dcf592011-06-01 15:30:43 -0700257 mNeutralAnimator.start();
Adam Cohen19072da2011-05-31 14:30:45 -0700258 }
259
Adam Cohenc0dcf592011-06-01 15:30:43 -0700260 // Location is expressed in window coordinates
Adam Cohen69ce2e52011-07-03 19:25:21 -0700261 public void getCell(int[] loc) {
262 loc[0] = mCellX;
263 loc[1] = mCellY;
264 }
265
266 // Location is expressed in window coordinates
267 public void setCell(int x, int y) {
268 mCellX = x;
269 mCellY = y;
270 }
271
272 public void setCellLayout(CellLayout layout) {
273 mCellLayout = layout;
Adam Cohen19072da2011-05-31 14:30:45 -0700274 }
275
Adam Cohen76fc0852011-06-17 13:26:23 -0700276 public float getOuterRingSize() {
277 return mOuterRingSize;
Adam Cohen19072da2011-05-31 14:30:45 -0700278 }
279
Adam Cohen76fc0852011-06-17 13:26:23 -0700280 public float getInnerRingSize() {
281 return mInnerRingSize;
Adam Cohen19072da2011-05-31 14:30:45 -0700282 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800283 }
284
Adam Cohenfb91f302012-06-11 15:45:18 -0700285 Folder getFolder() {
286 return mFolder;
287 }
288
289 FolderInfo getFolderInfo() {
290 return mInfo;
291 }
292
Adam Cohen073a46f2011-05-17 16:28:09 -0700293 private boolean willAcceptItem(ItemInfo item) {
294 final int itemType = item.itemType;
295 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
296 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
Adam Cohenc51934b2011-07-26 21:07:43 -0700297 !mFolder.isFull() && item != mInfo && !mInfo.opened);
Adam Cohen073a46f2011-05-17 16:28:09 -0700298 }
299
Adam Cohenc0dcf592011-06-01 15:30:43 -0700300 public boolean acceptDrop(Object dragInfo) {
301 final ItemInfo item = (ItemInfo) dragInfo;
Adam Cohenfb91f302012-06-11 15:45:18 -0700302 return !mFolder.isDestroyed() && willAcceptItem(item);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800303 }
304
Adam Cohendf035382011-04-11 17:22:04 -0700305 public void addItem(ShortcutInfo item) {
306 mInfo.add(item);
Adam Cohendf035382011-04-11 17:22:04 -0700307 }
308
Adam Cohenc0dcf592011-06-01 15:30:43 -0700309 public void onDragEnter(Object dragInfo) {
Adam Cohenfb91f302012-06-11 15:45:18 -0700310 if (mFolder.isDestroyed() || !willAcceptItem((ItemInfo) dragInfo)) return;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700311 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) getLayoutParams();
312 CellLayout layout = (CellLayout) getParent().getParent();
313 mFolderRingAnimator.setCell(lp.cellX, lp.cellY);
314 mFolderRingAnimator.setCellLayout(layout);
Adam Cohen19072da2011-05-31 14:30:45 -0700315 mFolderRingAnimator.animateToAcceptState();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700316 layout.showFolderAccept(mFolderRingAnimator);
Adam Cohen073a46f2011-05-17 16:28:09 -0700317 }
318
Adam Cohenc0dcf592011-06-01 15:30:43 -0700319 public void onDragOver(Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800320 }
321
Adam Cohend0445262011-07-04 23:53:22 -0700322 public void performCreateAnimation(final ShortcutInfo destInfo, final View destView,
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800323 final ShortcutInfo srcInfo, final DragView srcView, Rect dstRect,
Adam Cohenac8c8762011-07-13 11:15:27 -0700324 float scaleRelativeToDragLayer, Runnable postAnimationRunnable) {
Adam Cohend0445262011-07-04 23:53:22 -0700325
Adam Cohenfb91f302012-06-11 15:45:18 -0700326 // These correspond two the drawable and view that the icon was dropped _onto_
Adam Cohend0445262011-07-04 23:53:22 -0700327 Drawable animateDrawable = ((TextView) destView).getCompoundDrawables()[1];
Adam Cohenfb91f302012-06-11 15:45:18 -0700328 computePreviewDrawingParams(animateDrawable.getIntrinsicWidth(),
329 destView.getMeasuredWidth());
Adam Cohend0445262011-07-04 23:53:22 -0700330
331 // This will animate the first item from it's position as an icon into its
332 // position as the first item in the preview
Adam Cohenfb91f302012-06-11 15:45:18 -0700333 animateFirstItem(animateDrawable, INITIAL_ITEM_ANIMATION_DURATION, false, null);
Adam Cohen268c4752012-06-06 17:47:33 -0700334 addItem(destInfo);
Adam Cohenfb91f302012-06-11 15:45:18 -0700335
336 // This will animate the dragView (srcView) into the new folder
337 onDrop(srcInfo, srcView, dstRect, scaleRelativeToDragLayer, 1, postAnimationRunnable, null);
338 }
339
340 public void performDestroyAnimation(final View finalView, Runnable onCompleteRunnable) {
341 Drawable animateDrawable = ((TextView) finalView).getCompoundDrawables()[1];
342 computePreviewDrawingParams(animateDrawable.getIntrinsicWidth(),
343 finalView.getMeasuredWidth());
344
345 // This will animate the first item from it's position as an icon into its
346 // position as the first item in the preview
347 animateFirstItem(animateDrawable, FINAL_ITEM_ANIMATION_DURATION, true,
348 onCompleteRunnable);
Adam Cohend0445262011-07-04 23:53:22 -0700349 }
350
Adam Cohenc0dcf592011-06-01 15:30:43 -0700351 public void onDragExit(Object dragInfo) {
Adam Cohenc6cc61d2012-04-04 12:47:08 -0700352 onDragExit();
353 }
354
355 public void onDragExit() {
Adam Cohen19072da2011-05-31 14:30:45 -0700356 mFolderRingAnimator.animateToNaturalState();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800357 }
Patrick Dubroy440c3602010-07-13 17:50:32 -0700358
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800359 private void onDrop(final ShortcutInfo item, DragView animateView, Rect finalRect,
360 float scaleRelativeToDragLayer, int index, Runnable postAnimationRunnable,
361 DragObject d) {
Adam Cohend0445262011-07-04 23:53:22 -0700362 item.cellX = -1;
363 item.cellY = -1;
Adam Cohend0445262011-07-04 23:53:22 -0700364
Adam Cohen558baaf2011-08-15 15:22:57 -0700365 // Typically, the animateView corresponds to the DragView; however, if this is being done
366 // after a configuration activity (ie. for a Shortcut being dragged from AllApps) we
367 // will not have a view to animate
368 if (animateView != null) {
369 DragLayer dragLayer = mLauncher.getDragLayer();
370 Rect from = new Rect();
371 dragLayer.getViewRectRelativeToSelf(animateView, from);
372 Rect to = finalRect;
373 if (to == null) {
374 to = new Rect();
375 Workspace workspace = mLauncher.getWorkspace();
376 // Set cellLayout and this to it's final state to compute final animation locations
377 workspace.setFinalTransitionTransform((CellLayout) getParent().getParent());
378 float scaleX = getScaleX();
379 float scaleY = getScaleY();
380 setScaleX(1.0f);
381 setScaleY(1.0f);
382 scaleRelativeToDragLayer = dragLayer.getDescendantRectRelativeToSelf(this, to);
383 // Finished computing final animation locations, restore current state
384 setScaleX(scaleX);
385 setScaleY(scaleY);
386 workspace.resetTransitionTransform((CellLayout) getParent().getParent());
Adam Cohend0445262011-07-04 23:53:22 -0700387 }
Adam Cohen558baaf2011-08-15 15:22:57 -0700388
389 int[] center = new int[2];
390 float scale = getLocalCenterForIndex(index, center);
391 center[0] = (int) Math.round(scaleRelativeToDragLayer * center[0]);
392 center[1] = (int) Math.round(scaleRelativeToDragLayer * center[1]);
393
394 to.offset(center[0] - animateView.getMeasuredWidth() / 2,
395 center[1] - animateView.getMeasuredHeight() / 2);
396
397 float finalAlpha = index < NUM_ITEMS_IN_PREVIEW ? 0.5f : 0f;
398
Adam Cohened66b2b2012-01-23 17:28:51 -0800399 float finalScale = scale * scaleRelativeToDragLayer;
Adam Cohen558baaf2011-08-15 15:22:57 -0700400 dragLayer.animateView(animateView, from, to, finalAlpha,
Adam Cohened66b2b2012-01-23 17:28:51 -0800401 1, 1, finalScale, finalScale, DROP_IN_ANIMATION_DURATION,
Adam Cohen558baaf2011-08-15 15:22:57 -0700402 new DecelerateInterpolator(2), new AccelerateInterpolator(2),
Adam Cohened66b2b2012-01-23 17:28:51 -0800403 postAnimationRunnable, DragLayer.ANIMATION_END_DISAPPEAR, null);
Adam Cohenfb91f302012-06-11 15:45:18 -0700404 addItem(item);
405 mHiddenItems.add(item);
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700406 mFolder.hideItem(item);
Adam Cohen558baaf2011-08-15 15:22:57 -0700407 postDelayed(new Runnable() {
408 public void run() {
Adam Cohenfb91f302012-06-11 15:45:18 -0700409 mHiddenItems.remove(item);
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700410 mFolder.showItem(item);
Adam Cohenfb91f302012-06-11 15:45:18 -0700411 invalidate();
Adam Cohen558baaf2011-08-15 15:22:57 -0700412 }
413 }, DROP_IN_ANIMATION_DURATION);
414 } else {
415 addItem(item);
416 }
Adam Cohend0445262011-07-04 23:53:22 -0700417 }
418
Adam Cohen3e8f8112011-07-02 18:03:00 -0700419 public void onDrop(DragObject d) {
Adam Cohenc0dcf592011-06-01 15:30:43 -0700420 ShortcutInfo item;
Adam Cohen3e8f8112011-07-02 18:03:00 -0700421 if (d.dragInfo instanceof ApplicationInfo) {
Adam Cohenc0dcf592011-06-01 15:30:43 -0700422 // Came from all apps -- make a copy
Adam Cohen3e8f8112011-07-02 18:03:00 -0700423 item = ((ApplicationInfo) d.dragInfo).makeShortcut();
Adam Cohenc0dcf592011-06-01 15:30:43 -0700424 } else {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700425 item = (ShortcutInfo) d.dragInfo;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700426 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700427 mFolder.notifyDrop();
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800428 onDrop(item, d.dragView, null, 1.0f, mInfo.contents.size(), d.postAnimationRunnable, d);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700429 }
430
Adam Cohend0445262011-07-04 23:53:22 -0700431 private void computePreviewDrawingParams(int drawableSize, int totalSize) {
432 if (mIntrinsicIconSize != drawableSize || mTotalWidth != totalSize) {
433 mIntrinsicIconSize = drawableSize;
434 mTotalWidth = totalSize;
435
436 final int previewSize = FolderRingAnimator.sPreviewSize;
437 final int previewPadding = FolderRingAnimator.sPreviewPadding;
438
439 mAvailableSpaceInPreview = (previewSize - 2 * previewPadding);
440 // cos(45) = 0.707 + ~= 0.1) = 0.8f
441 int adjustedAvailableSpace = (int) ((mAvailableSpaceInPreview / 2) * (1 + 0.8f));
442
443 int unscaledHeight = (int) (mIntrinsicIconSize * (1 + PERSPECTIVE_SHIFT_FACTOR));
444 mBaselineIconScale = (1.0f * adjustedAvailableSpace / unscaledHeight);
445
446 mBaselineIconSize = (int) (mIntrinsicIconSize * mBaselineIconScale);
447 mMaxPerspectiveShift = mBaselineIconSize * PERSPECTIVE_SHIFT_FACTOR;
448
449 mPreviewOffsetX = (mTotalWidth - mAvailableSpaceInPreview) / 2;
450 mPreviewOffsetY = previewPadding;
451 }
452 }
453
454 private void computePreviewDrawingParams(Drawable d) {
455 computePreviewDrawingParams(d.getIntrinsicWidth(), getMeasuredWidth());
456 }
457
458 class PreviewItemDrawingParams {
459 PreviewItemDrawingParams(float transX, float transY, float scale, int overlayAlpha) {
460 this.transX = transX;
461 this.transY = transY;
462 this.scale = scale;
463 this.overlayAlpha = overlayAlpha;
464 }
465 float transX;
466 float transY;
467 float scale;
468 int overlayAlpha;
469 Drawable drawable;
470 }
471
Adam Cohenac8c8762011-07-13 11:15:27 -0700472 private float getLocalCenterForIndex(int index, int[] center) {
Adam Cohend0445262011-07-04 23:53:22 -0700473 mParams = computePreviewItemDrawingParams(Math.min(NUM_ITEMS_IN_PREVIEW, index), mParams);
474
475 mParams.transX += mPreviewOffsetX;
476 mParams.transY += mPreviewOffsetY;
Adam Cohenac8c8762011-07-13 11:15:27 -0700477 float offsetX = mParams.transX + (mParams.scale * mIntrinsicIconSize) / 2;
478 float offsetY = mParams.transY + (mParams.scale * mIntrinsicIconSize) / 2;
Adam Cohend0445262011-07-04 23:53:22 -0700479
Adam Cohenac8c8762011-07-13 11:15:27 -0700480 center[0] = (int) Math.round(offsetX);
481 center[1] = (int) Math.round(offsetY);
Adam Cohend0445262011-07-04 23:53:22 -0700482 return mParams.scale;
483 }
484
485 private PreviewItemDrawingParams computePreviewItemDrawingParams(int index,
486 PreviewItemDrawingParams params) {
487 index = NUM_ITEMS_IN_PREVIEW - index - 1;
488 float r = (index * 1.0f) / (NUM_ITEMS_IN_PREVIEW - 1);
489 float scale = (1 - PERSPECTIVE_SCALE_FACTOR * (1 - r));
490
491 float offset = (1 - r) * mMaxPerspectiveShift;
492 float scaledSize = scale * mBaselineIconSize;
493 float scaleOffsetCorrection = (1 - scale) * mBaselineIconSize;
494
495 // We want to imagine our coordinates from the bottom left, growing up and to the
496 // right. This is natural for the x-axis, but for the y-axis, we have to invert things.
497 float transY = mAvailableSpaceInPreview - (offset + scaledSize + scaleOffsetCorrection);
498 float transX = offset + scaleOffsetCorrection;
499 float totalScale = mBaselineIconScale * scale;
500 final int overlayAlpha = (int) (80 * (1 - r));
501
502 if (params == null) {
503 params = new PreviewItemDrawingParams(transX, transY, totalScale, overlayAlpha);
504 } else {
505 params.transX = transX;
506 params.transY = transY;
507 params.scale = totalScale;
508 params.overlayAlpha = overlayAlpha;
509 }
510 return params;
511 }
512
513 private void drawPreviewItem(Canvas canvas, PreviewItemDrawingParams params) {
514 canvas.save();
515 canvas.translate(params.transX + mPreviewOffsetX, params.transY + mPreviewOffsetY);
516 canvas.scale(params.scale, params.scale);
517 Drawable d = params.drawable;
518
519 if (d != null) {
520 d.setBounds(0, 0, mIntrinsicIconSize, mIntrinsicIconSize);
521 d.setFilterBitmap(true);
522 d.setColorFilter(Color.argb(params.overlayAlpha, 0, 0, 0), PorterDuff.Mode.SRC_ATOP);
523 d.draw(canvas);
524 d.clearColorFilter();
525 d.setFilterBitmap(false);
526 }
527 canvas.restore();
528 }
529
Adam Cohena9cf38f2011-05-02 15:36:58 -0700530 @Override
Adam Cohenc0dcf592011-06-01 15:30:43 -0700531 protected void dispatchDraw(Canvas canvas) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700532 super.dispatchDraw(canvas);
533
Adam Cohena9cf38f2011-05-02 15:36:58 -0700534 if (mFolder == null) return;
Adam Cohend0445262011-07-04 23:53:22 -0700535 if (mFolder.getItemCount() == 0 && !mAnimating) return;
Adam Cohena9cf38f2011-05-02 15:36:58 -0700536
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700537 ArrayList<View> items = mFolder.getItemsInReadingOrder();
Adam Cohend0445262011-07-04 23:53:22 -0700538 Drawable d;
539 TextView v;
Adam Cohenbadf71e2011-05-26 19:08:29 -0700540
Adam Cohend0445262011-07-04 23:53:22 -0700541 // Update our drawing parameters if necessary
542 if (mAnimating) {
543 computePreviewDrawingParams(mAnimParams.drawable);
544 } else {
545 v = (TextView) items.get(0);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700546 d = v.getCompoundDrawables()[1];
Adam Cohend0445262011-07-04 23:53:22 -0700547 computePreviewDrawingParams(d);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700548 }
Adam Cohend0445262011-07-04 23:53:22 -0700549
550 int nItemsInPreview = Math.min(items.size(), NUM_ITEMS_IN_PREVIEW);
551 if (!mAnimating) {
552 for (int i = nItemsInPreview - 1; i >= 0; i--) {
553 v = (TextView) items.get(i);
Adam Cohenfb91f302012-06-11 15:45:18 -0700554 if (!mHiddenItems.contains(v.getTag())) {
555 d = v.getCompoundDrawables()[1];
556 mParams = computePreviewItemDrawingParams(i, mParams);
557 mParams.drawable = d;
558 drawPreviewItem(canvas, mParams);
559 }
Adam Cohend0445262011-07-04 23:53:22 -0700560 }
561 } else {
562 drawPreviewItem(canvas, mAnimParams);
563 }
564 }
565
Adam Cohenfb91f302012-06-11 15:45:18 -0700566 private void animateFirstItem(final Drawable d, int duration, final boolean reverse,
567 final Runnable onCompleteRunnable) {
Adam Cohend0445262011-07-04 23:53:22 -0700568 final PreviewItemDrawingParams finalParams = computePreviewItemDrawingParams(0, null);
569
570 final float scale0 = 1.0f;
Adam Cohen1d4ee4e2011-08-12 15:51:59 -0700571 final float transX0 = (mAvailableSpaceInPreview - d.getIntrinsicWidth()) / 2;
572 final float transY0 = (mAvailableSpaceInPreview - d.getIntrinsicHeight()) / 2;
Adam Cohend0445262011-07-04 23:53:22 -0700573 mAnimParams.drawable = d;
574
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100575 ValueAnimator va = LauncherAnimUtils.ofFloat(this, 0f, 1.0f);
Adam Cohend0445262011-07-04 23:53:22 -0700576 va.addUpdateListener(new AnimatorUpdateListener(){
577 public void onAnimationUpdate(ValueAnimator animation) {
578 float progress = (Float) animation.getAnimatedValue();
Adam Cohenfb91f302012-06-11 15:45:18 -0700579 if (reverse) {
580 progress = 1 - progress;
581 mPreviewBackground.setAlpha(progress);
582 }
Adam Cohend0445262011-07-04 23:53:22 -0700583
584 mAnimParams.transX = transX0 + progress * (finalParams.transX - transX0);
585 mAnimParams.transY = transY0 + progress * (finalParams.transY - transY0);
586 mAnimParams.scale = scale0 + progress * (finalParams.scale - scale0);
587 invalidate();
588 }
589 });
590 va.addListener(new AnimatorListenerAdapter() {
591 @Override
592 public void onAnimationStart(Animator animation) {
593 mAnimating = true;
594 }
595 @Override
596 public void onAnimationEnd(Animator animation) {
597 mAnimating = false;
Adam Cohenfb91f302012-06-11 15:45:18 -0700598 if (onCompleteRunnable != null) {
599 onCompleteRunnable.run();
600 }
Adam Cohend0445262011-07-04 23:53:22 -0700601 }
602 });
603 va.setDuration(duration);
604 va.start();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700605 }
606
Adam Cohen099f60d2011-08-23 21:07:26 -0700607 public void setTextVisible(boolean visible) {
608 if (visible) {
609 mFolderName.setVisibility(VISIBLE);
610 } else {
611 mFolderName.setVisibility(INVISIBLE);
612 }
613 }
614
615 public boolean getTextVisible() {
616 return mFolderName.getVisibility() == VISIBLE;
617 }
618
Adam Cohen76078c42011-06-09 15:06:52 -0700619 public void onItemsChanged() {
620 invalidate();
621 requestLayout();
622 }
623
Adam Cohena9cf38f2011-05-02 15:36:58 -0700624 public void onAdd(ShortcutInfo item) {
625 invalidate();
626 requestLayout();
627 }
628
629 public void onRemove(ShortcutInfo item) {
630 invalidate();
631 requestLayout();
632 }
Adam Cohen76fc0852011-06-17 13:26:23 -0700633
634 public void onTitleChanged(CharSequence title) {
Adam Cohend63cfa92011-06-24 14:17:17 -0700635 mFolderName.setText(title.toString());
Michael Jurka8b805b12012-04-18 14:23:14 -0700636 setContentDescription(String.format(getContext().getString(R.string.folder_name_format),
Adam Cohen3371da02011-10-25 21:38:29 -0700637 title));
Adam Cohen76fc0852011-06-17 13:26:23 -0700638 }
Winson Chung88f33452012-02-23 15:23:44 -0800639
640 @Override
641 public boolean onTouchEvent(MotionEvent event) {
642 // Call the superclass onTouchEvent first, because sometimes it changes the state to
643 // isPressed() on an ACTION_UP
644 boolean result = super.onTouchEvent(event);
645
646 switch (event.getAction()) {
647 case MotionEvent.ACTION_DOWN:
648 mLongPressHelper.postCheckForLongPress();
649 break;
650 case MotionEvent.ACTION_CANCEL:
651 case MotionEvent.ACTION_UP:
652 mLongPressHelper.cancelLongPress();
653 break;
654 }
655 return result;
656 }
657
658 @Override
659 public void cancelLongPress() {
660 super.cancelLongPress();
661
662 mLongPressHelper.cancelLongPress();
663 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800664}