blob: 92f126c3f7134bcaeef7036b082b4eec9693072f [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;
Adam Cohen099f60d2011-08-23 21:07:26 -070030import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.util.AttributeSet;
32import android.view.LayoutInflater;
Winson Chung88f33452012-02-23 15:23:44 -080033import android.view.MotionEvent;
Adam Cohen7c693212011-05-18 15:26:57 -070034import android.view.View;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.view.ViewGroup;
Adam Cohen3e8f8112011-07-02 18:03:00 -070036import android.view.animation.AccelerateInterpolator;
37import android.view.animation.DecelerateInterpolator;
Adam Cohen76fc0852011-06-17 13:26:23 -070038import android.widget.ImageView;
39import android.widget.LinearLayout;
Adam Cohena9cf38f2011-05-02 15:36:58 -070040import android.widget.TextView;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041
Daniel Sandler325dc232013-06-05 22:57:57 -040042import com.android.launcher3.R;
43import com.android.launcher3.DropTarget.DragObject;
44import com.android.launcher3.FolderInfo.FolderListener;
Romain Guyedcce092010-03-04 13:03:17 -080045
Adam Cohenc0dcf592011-06-01 15:30:43 -070046import java.util.ArrayList;
47
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048/**
49 * An icon that can appear on in the workspace representing an {@link UserFolder}.
50 */
Adam Cohen76fc0852011-06-17 13:26:23 -070051public class FolderIcon extends LinearLayout implements FolderListener {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080052 private Launcher mLauncher;
Adam Cohenfb91f302012-06-11 15:45:18 -070053 private Folder mFolder;
54 private FolderInfo mInfo;
Adam Cohen099f60d2011-08-23 21:07:26 -070055 private static boolean sStaticValuesDirty = true;
Adam Cohena9cf38f2011-05-02 15:36:58 -070056
Winson Chung88f33452012-02-23 15:23:44 -080057 private CheckLongPressHelper mLongPressHelper;
58
Adam Cohenbadf71e2011-05-26 19:08:29 -070059 // The number of icons to display in the
Adam Cohen76fc0852011-06-17 13:26:23 -070060 private static final int NUM_ITEMS_IN_PREVIEW = 3;
Adam Cohenf4b08912011-05-17 18:45:47 -070061 private static final int CONSUMPTION_ANIMATION_DURATION = 100;
Adam Cohend0445262011-07-04 23:53:22 -070062 private static final int DROP_IN_ANIMATION_DURATION = 400;
Adam Cohen8dfcba42011-07-07 16:38:18 -070063 private static final int INITIAL_ITEM_ANIMATION_DURATION = 350;
Adam Cohenfb91f302012-06-11 15:45:18 -070064 private static final int FINAL_ITEM_ANIMATION_DURATION = 200;
Adam Cohenbadf71e2011-05-26 19:08:29 -070065
66 // The degree to which the inner ring grows when accepting drop
Adam Cohen69ce2e52011-07-03 19:25:21 -070067 private static final float INNER_RING_GROWTH_FACTOR = 0.15f;
Adam Cohenbadf71e2011-05-26 19:08:29 -070068
Adam Cohenbadf71e2011-05-26 19:08:29 -070069 // The degree to which the outer ring is scaled in its natural state
Adam Cohen69ce2e52011-07-03 19:25:21 -070070 private static final float OUTER_RING_GROWTH_FACTOR = 0.3f;
Adam Cohenbadf71e2011-05-26 19:08:29 -070071
72 // The amount of vertical spread between items in the stack [0...1]
Adam Cohen76fc0852011-06-17 13:26:23 -070073 private static final float PERSPECTIVE_SHIFT_FACTOR = 0.24f;
Adam Cohenbadf71e2011-05-26 19:08:29 -070074
75 // The degree to which the item in the back of the stack is scaled [0...1]
76 // (0 means it's not scaled at all, 1 means it's scaled to nothing)
Adam Cohen76fc0852011-06-17 13:26:23 -070077 private static final float PERSPECTIVE_SCALE_FACTOR = 0.35f;
Adam Cohenbadf71e2011-05-26 19:08:29 -070078
Adam Cohenc51934b2011-07-26 21:07:43 -070079 public static Drawable sSharedFolderLeaveBehind = null;
80
Adam Cohen76fc0852011-06-17 13:26:23 -070081 private ImageView mPreviewBackground;
82 private BubbleTextView mFolderName;
Adam Cohen073a46f2011-05-17 16:28:09 -070083
Adam Cohen19072da2011-05-31 14:30:45 -070084 FolderRingAnimator mFolderRingAnimator = null;
Adam Cohen2801caf2011-05-13 20:57:39 -070085
Adam Cohend0445262011-07-04 23:53:22 -070086 // These variables are all associated with the drawing of the preview; they are stored
87 // as member variables for shared usage and to avoid computation on each frame
88 private int mIntrinsicIconSize;
89 private float mBaselineIconScale;
90 private int mBaselineIconSize;
91 private int mAvailableSpaceInPreview;
92 private int mTotalWidth = -1;
93 private int mPreviewOffsetX;
94 private int mPreviewOffsetY;
95 private float mMaxPerspectiveShift;
96 boolean mAnimating = false;
Adam Cohenfb91f302012-06-11 15:45:18 -070097
Adam Cohend0445262011-07-04 23:53:22 -070098 private PreviewItemDrawingParams mParams = new PreviewItemDrawingParams(0, 0, 0, 0);
99 private PreviewItemDrawingParams mAnimParams = new PreviewItemDrawingParams(0, 0, 0, 0);
Adam Cohenfb91f302012-06-11 15:45:18 -0700100 private ArrayList<ShortcutInfo> mHiddenItems = new ArrayList<ShortcutInfo>();
Adam Cohend0445262011-07-04 23:53:22 -0700101
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800102 public FolderIcon(Context context, AttributeSet attrs) {
103 super(context, attrs);
Winson Chung88f33452012-02-23 15:23:44 -0800104 init();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800105 }
106
107 public FolderIcon(Context context) {
108 super(context);
Winson Chung88f33452012-02-23 15:23:44 -0800109 init();
110 }
111
112 private void init() {
113 mLongPressHelper = new CheckLongPressHelper(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800114 }
115
Michael Jurka0280c3b2010-09-17 15:00:07 -0700116 public boolean isDropEnabled() {
Winson Chung7a1d1652011-03-18 15:56:01 -0700117 final ViewGroup cellLayoutChildren = (ViewGroup) getParent();
118 final ViewGroup cellLayout = (ViewGroup) cellLayoutChildren.getParent();
119 final Workspace workspace = (Workspace) cellLayout.getParent();
120 return !workspace.isSmall();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700121 }
122
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123 static FolderIcon fromXml(int resId, Launcher launcher, ViewGroup group,
Adam Cohendf2cc412011-04-27 16:56:57 -0700124 FolderInfo folderInfo, IconCache iconCache) {
Michael Jurka3a9fced2012-04-13 14:44:29 -0700125 @SuppressWarnings("all") // suppress dead code warning
126 final boolean error = INITIAL_ITEM_ANIMATION_DURATION >= DROP_IN_ANIMATION_DURATION;
127 if (error) {
Adam Cohend0445262011-07-04 23:53:22 -0700128 throw new IllegalStateException("DROP_IN_ANIMATION_DURATION must be greater than " +
129 "INITIAL_ITEM_ANIMATION_DURATION, as sequencing of adding first two items " +
130 "is dependent on this");
131 }
132
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133 FolderIcon icon = (FolderIcon) LayoutInflater.from(launcher).inflate(resId, group, false);
134
Adam Cohend2eca6b2011-07-26 22:49:13 -0700135 icon.mFolderName = (BubbleTextView) icon.findViewById(R.id.folder_icon_name);
Adam Cohen76fc0852011-06-17 13:26:23 -0700136 icon.mFolderName.setText(folderInfo.title);
137 icon.mPreviewBackground = (ImageView) icon.findViewById(R.id.preview_background);
138
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139 icon.setTag(folderInfo);
140 icon.setOnClickListener(launcher);
141 icon.mInfo = folderInfo;
142 icon.mLauncher = launcher;
Adam Cohen3371da02011-10-25 21:38:29 -0700143 icon.setContentDescription(String.format(launcher.getString(R.string.folder_name_format),
144 folderInfo.title));
Adam Cohena9cf38f2011-05-02 15:36:58 -0700145 Folder folder = Folder.fromXml(launcher);
146 folder.setDragController(launcher.getDragController());
Adam Cohen2801caf2011-05-13 20:57:39 -0700147 folder.setFolderIcon(icon);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700148 folder.bind(folderInfo);
149 icon.mFolder = folder;
Adam Cohen099f60d2011-08-23 21:07:26 -0700150
Adam Cohen099f60d2011-08-23 21:07:26 -0700151 icon.mFolderRingAnimator = new FolderRingAnimator(launcher, icon);
152 folderInfo.addListener(icon);
153
Adam Cohen19072da2011-05-31 14:30:45 -0700154 return icon;
155 }
156
Adam Cohen099f60d2011-08-23 21:07:26 -0700157 @Override
158 protected Parcelable onSaveInstanceState() {
159 sStaticValuesDirty = true;
160 return super.onSaveInstanceState();
161 }
162
Adam Cohen19072da2011-05-31 14:30:45 -0700163 public static class FolderRingAnimator {
Adam Cohen69ce2e52011-07-03 19:25:21 -0700164 public int mCellX;
165 public int mCellY;
166 private CellLayout mCellLayout;
Adam Cohen76fc0852011-06-17 13:26:23 -0700167 public float mOuterRingSize;
168 public float mInnerRingSize;
Adam Cohen19072da2011-05-31 14:30:45 -0700169 public FolderIcon mFolderIcon = null;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700170 public Drawable mOuterRingDrawable = null;
171 public Drawable mInnerRingDrawable = null;
172 public static Drawable sSharedOuterRingDrawable = null;
173 public static Drawable sSharedInnerRingDrawable = null;
Adam Cohen76fc0852011-06-17 13:26:23 -0700174 public static int sPreviewSize = -1;
175 public static int sPreviewPadding = -1;
176
Adam Cohenc0dcf592011-06-01 15:30:43 -0700177 private ValueAnimator mAcceptAnimator;
178 private ValueAnimator mNeutralAnimator;
Adam Cohen19072da2011-05-31 14:30:45 -0700179
180 public FolderRingAnimator(Launcher launcher, FolderIcon folderIcon) {
Adam Cohen19072da2011-05-31 14:30:45 -0700181 mFolderIcon = folderIcon;
Adam Cohen76fc0852011-06-17 13:26:23 -0700182 Resources res = launcher.getResources();
183 mOuterRingDrawable = res.getDrawable(R.drawable.portal_ring_outer_holo);
184 mInnerRingDrawable = res.getDrawable(R.drawable.portal_ring_inner_holo);
185
Adam Cohen099f60d2011-08-23 21:07:26 -0700186 // We need to reload the static values when configuration changes in case they are
187 // different in another configuration
188 if (sStaticValuesDirty) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700189 sPreviewSize = res.getDimensionPixelSize(R.dimen.folder_preview_size);
190 sPreviewPadding = res.getDimensionPixelSize(R.dimen.folder_preview_padding);
Adam Cohen76fc0852011-06-17 13:26:23 -0700191 sSharedOuterRingDrawable = res.getDrawable(R.drawable.portal_ring_outer_holo);
Adam Cohen76fc0852011-06-17 13:26:23 -0700192 sSharedInnerRingDrawable = res.getDrawable(R.drawable.portal_ring_inner_holo);
Adam Cohendf6af572011-10-19 14:38:16 -0700193 sSharedFolderLeaveBehind = res.getDrawable(R.drawable.portal_ring_rest);
Adam Cohen099f60d2011-08-23 21:07:26 -0700194 sStaticValuesDirty = false;
Adam Cohen19072da2011-05-31 14:30:45 -0700195 }
Adam Cohen073a46f2011-05-17 16:28:09 -0700196 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700197
Adam Cohen19072da2011-05-31 14:30:45 -0700198 public void animateToAcceptState() {
Adam Cohenc0dcf592011-06-01 15:30:43 -0700199 if (mNeutralAnimator != null) {
200 mNeutralAnimator.cancel();
201 }
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100202 mAcceptAnimator = LauncherAnimUtils.ofFloat(mCellLayout, 0f, 1f);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700203 mAcceptAnimator.setDuration(CONSUMPTION_ANIMATION_DURATION);
Andrew Flynn850d2e72012-04-26 16:51:20 -0700204
205 final int previewSize = sPreviewSize;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700206 mAcceptAnimator.addUpdateListener(new AnimatorUpdateListener() {
Adam Cohen19072da2011-05-31 14:30:45 -0700207 public void onAnimationUpdate(ValueAnimator animation) {
208 final float percent = (Float) animation.getAnimatedValue();
Andrew Flynn850d2e72012-04-26 16:51:20 -0700209 mOuterRingSize = (1 + percent * OUTER_RING_GROWTH_FACTOR) * previewSize;
210 mInnerRingSize = (1 + percent * INNER_RING_GROWTH_FACTOR) * previewSize;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700211 if (mCellLayout != null) {
212 mCellLayout.invalidate();
Adam Cohen19072da2011-05-31 14:30:45 -0700213 }
214 }
215 });
Adam Cohenc0dcf592011-06-01 15:30:43 -0700216 mAcceptAnimator.addListener(new AnimatorListenerAdapter() {
Adam Cohen19072da2011-05-31 14:30:45 -0700217 @Override
Adam Cohenc0dcf592011-06-01 15:30:43 -0700218 public void onAnimationStart(Animator animation) {
Adam Cohen19072da2011-05-31 14:30:45 -0700219 if (mFolderIcon != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700220 mFolderIcon.mPreviewBackground.setVisibility(INVISIBLE);
Adam Cohen19072da2011-05-31 14:30:45 -0700221 }
222 }
223 });
Adam Cohenc0dcf592011-06-01 15:30:43 -0700224 mAcceptAnimator.start();
Adam Cohen19072da2011-05-31 14:30:45 -0700225 }
226
227 public void animateToNaturalState() {
Adam Cohenc0dcf592011-06-01 15:30:43 -0700228 if (mAcceptAnimator != null) {
229 mAcceptAnimator.cancel();
230 }
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100231 mNeutralAnimator = LauncherAnimUtils.ofFloat(mCellLayout, 0f, 1f);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700232 mNeutralAnimator.setDuration(CONSUMPTION_ANIMATION_DURATION);
Andrew Flynn850d2e72012-04-26 16:51:20 -0700233
234 final int previewSize = sPreviewSize;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700235 mNeutralAnimator.addUpdateListener(new AnimatorUpdateListener() {
Adam Cohen19072da2011-05-31 14:30:45 -0700236 public void onAnimationUpdate(ValueAnimator animation) {
237 final float percent = (Float) animation.getAnimatedValue();
Andrew Flynn850d2e72012-04-26 16:51:20 -0700238 mOuterRingSize = (1 + (1 - percent) * OUTER_RING_GROWTH_FACTOR) * previewSize;
239 mInnerRingSize = (1 + (1 - percent) * INNER_RING_GROWTH_FACTOR) * previewSize;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700240 if (mCellLayout != null) {
241 mCellLayout.invalidate();
Adam Cohen19072da2011-05-31 14:30:45 -0700242 }
243 }
244 });
Adam Cohenc0dcf592011-06-01 15:30:43 -0700245 mNeutralAnimator.addListener(new AnimatorListenerAdapter() {
Adam Cohen19072da2011-05-31 14:30:45 -0700246 @Override
247 public void onAnimationEnd(Animator animation) {
Adam Cohen69ce2e52011-07-03 19:25:21 -0700248 if (mCellLayout != null) {
249 mCellLayout.hideFolderAccept(FolderRingAnimator.this);
250 }
Adam Cohen19072da2011-05-31 14:30:45 -0700251 if (mFolderIcon != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700252 mFolderIcon.mPreviewBackground.setVisibility(VISIBLE);
Adam Cohen19072da2011-05-31 14:30:45 -0700253 }
Adam Cohen19072da2011-05-31 14:30:45 -0700254 }
255 });
Adam Cohenc0dcf592011-06-01 15:30:43 -0700256 mNeutralAnimator.start();
Adam Cohen19072da2011-05-31 14:30:45 -0700257 }
258
Adam Cohenc0dcf592011-06-01 15:30:43 -0700259 // Location is expressed in window coordinates
Adam Cohen69ce2e52011-07-03 19:25:21 -0700260 public void getCell(int[] loc) {
261 loc[0] = mCellX;
262 loc[1] = mCellY;
263 }
264
265 // Location is expressed in window coordinates
266 public void setCell(int x, int y) {
267 mCellX = x;
268 mCellY = y;
269 }
270
271 public void setCellLayout(CellLayout layout) {
272 mCellLayout = layout;
Adam Cohen19072da2011-05-31 14:30:45 -0700273 }
274
Adam Cohen76fc0852011-06-17 13:26:23 -0700275 public float getOuterRingSize() {
276 return mOuterRingSize;
Adam Cohen19072da2011-05-31 14:30:45 -0700277 }
278
Adam Cohen76fc0852011-06-17 13:26:23 -0700279 public float getInnerRingSize() {
280 return mInnerRingSize;
Adam Cohen19072da2011-05-31 14:30:45 -0700281 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800282 }
283
Adam Cohenfb91f302012-06-11 15:45:18 -0700284 Folder getFolder() {
285 return mFolder;
286 }
287
288 FolderInfo getFolderInfo() {
289 return mInfo;
290 }
291
Adam Cohen073a46f2011-05-17 16:28:09 -0700292 private boolean willAcceptItem(ItemInfo item) {
293 final int itemType = item.itemType;
294 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
295 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
Adam Cohenc51934b2011-07-26 21:07:43 -0700296 !mFolder.isFull() && item != mInfo && !mInfo.opened);
Adam Cohen073a46f2011-05-17 16:28:09 -0700297 }
298
Adam Cohenc0dcf592011-06-01 15:30:43 -0700299 public boolean acceptDrop(Object dragInfo) {
300 final ItemInfo item = (ItemInfo) dragInfo;
Adam Cohenfb91f302012-06-11 15:45:18 -0700301 return !mFolder.isDestroyed() && willAcceptItem(item);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800302 }
303
Adam Cohendf035382011-04-11 17:22:04 -0700304 public void addItem(ShortcutInfo item) {
305 mInfo.add(item);
Adam Cohendf035382011-04-11 17:22:04 -0700306 }
307
Adam Cohenc0dcf592011-06-01 15:30:43 -0700308 public void onDragEnter(Object dragInfo) {
Adam Cohenfb91f302012-06-11 15:45:18 -0700309 if (mFolder.isDestroyed() || !willAcceptItem((ItemInfo) dragInfo)) return;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700310 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) getLayoutParams();
311 CellLayout layout = (CellLayout) getParent().getParent();
312 mFolderRingAnimator.setCell(lp.cellX, lp.cellY);
313 mFolderRingAnimator.setCellLayout(layout);
Adam Cohen19072da2011-05-31 14:30:45 -0700314 mFolderRingAnimator.animateToAcceptState();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700315 layout.showFolderAccept(mFolderRingAnimator);
Adam Cohen073a46f2011-05-17 16:28:09 -0700316 }
317
Adam Cohenc0dcf592011-06-01 15:30:43 -0700318 public void onDragOver(Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800319 }
320
Adam Cohend0445262011-07-04 23:53:22 -0700321 public void performCreateAnimation(final ShortcutInfo destInfo, final View destView,
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800322 final ShortcutInfo srcInfo, final DragView srcView, Rect dstRect,
Adam Cohenac8c8762011-07-13 11:15:27 -0700323 float scaleRelativeToDragLayer, Runnable postAnimationRunnable) {
Adam Cohend0445262011-07-04 23:53:22 -0700324
Adam Cohenfb91f302012-06-11 15:45:18 -0700325 // These correspond two the drawable and view that the icon was dropped _onto_
Adam Cohend0445262011-07-04 23:53:22 -0700326 Drawable animateDrawable = ((TextView) destView).getCompoundDrawables()[1];
Adam Cohenfb91f302012-06-11 15:45:18 -0700327 computePreviewDrawingParams(animateDrawable.getIntrinsicWidth(),
328 destView.getMeasuredWidth());
Adam Cohend0445262011-07-04 23:53:22 -0700329
330 // This will animate the first item from it's position as an icon into its
331 // position as the first item in the preview
Adam Cohenfb91f302012-06-11 15:45:18 -0700332 animateFirstItem(animateDrawable, INITIAL_ITEM_ANIMATION_DURATION, false, null);
Adam Cohen268c4752012-06-06 17:47:33 -0700333 addItem(destInfo);
Adam Cohenfb91f302012-06-11 15:45:18 -0700334
335 // This will animate the dragView (srcView) into the new folder
336 onDrop(srcInfo, srcView, dstRect, scaleRelativeToDragLayer, 1, postAnimationRunnable, null);
337 }
338
339 public void performDestroyAnimation(final View finalView, Runnable onCompleteRunnable) {
340 Drawable animateDrawable = ((TextView) finalView).getCompoundDrawables()[1];
341 computePreviewDrawingParams(animateDrawable.getIntrinsicWidth(),
342 finalView.getMeasuredWidth());
343
344 // This will animate the first item from it's position as an icon into its
345 // position as the first item in the preview
346 animateFirstItem(animateDrawable, FINAL_ITEM_ANIMATION_DURATION, true,
347 onCompleteRunnable);
Adam Cohend0445262011-07-04 23:53:22 -0700348 }
349
Adam Cohenc0dcf592011-06-01 15:30:43 -0700350 public void onDragExit(Object dragInfo) {
Adam Cohenc6cc61d2012-04-04 12:47:08 -0700351 onDragExit();
352 }
353
354 public void onDragExit() {
Adam Cohen19072da2011-05-31 14:30:45 -0700355 mFolderRingAnimator.animateToNaturalState();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800356 }
Patrick Dubroy440c3602010-07-13 17:50:32 -0700357
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800358 private void onDrop(final ShortcutInfo item, DragView animateView, Rect finalRect,
359 float scaleRelativeToDragLayer, int index, Runnable postAnimationRunnable,
360 DragObject d) {
Adam Cohend0445262011-07-04 23:53:22 -0700361 item.cellX = -1;
362 item.cellY = -1;
Adam Cohend0445262011-07-04 23:53:22 -0700363
Adam Cohen558baaf2011-08-15 15:22:57 -0700364 // Typically, the animateView corresponds to the DragView; however, if this is being done
365 // after a configuration activity (ie. for a Shortcut being dragged from AllApps) we
366 // will not have a view to animate
367 if (animateView != null) {
368 DragLayer dragLayer = mLauncher.getDragLayer();
369 Rect from = new Rect();
370 dragLayer.getViewRectRelativeToSelf(animateView, from);
371 Rect to = finalRect;
372 if (to == null) {
373 to = new Rect();
374 Workspace workspace = mLauncher.getWorkspace();
375 // Set cellLayout and this to it's final state to compute final animation locations
376 workspace.setFinalTransitionTransform((CellLayout) getParent().getParent());
377 float scaleX = getScaleX();
378 float scaleY = getScaleY();
379 setScaleX(1.0f);
380 setScaleY(1.0f);
381 scaleRelativeToDragLayer = dragLayer.getDescendantRectRelativeToSelf(this, to);
382 // Finished computing final animation locations, restore current state
383 setScaleX(scaleX);
384 setScaleY(scaleY);
385 workspace.resetTransitionTransform((CellLayout) getParent().getParent());
Adam Cohend0445262011-07-04 23:53:22 -0700386 }
Adam Cohen558baaf2011-08-15 15:22:57 -0700387
388 int[] center = new int[2];
389 float scale = getLocalCenterForIndex(index, center);
390 center[0] = (int) Math.round(scaleRelativeToDragLayer * center[0]);
391 center[1] = (int) Math.round(scaleRelativeToDragLayer * center[1]);
392
393 to.offset(center[0] - animateView.getMeasuredWidth() / 2,
394 center[1] - animateView.getMeasuredHeight() / 2);
395
396 float finalAlpha = index < NUM_ITEMS_IN_PREVIEW ? 0.5f : 0f;
397
Adam Cohened66b2b2012-01-23 17:28:51 -0800398 float finalScale = scale * scaleRelativeToDragLayer;
Adam Cohen558baaf2011-08-15 15:22:57 -0700399 dragLayer.animateView(animateView, from, to, finalAlpha,
Adam Cohened66b2b2012-01-23 17:28:51 -0800400 1, 1, finalScale, finalScale, DROP_IN_ANIMATION_DURATION,
Adam Cohen558baaf2011-08-15 15:22:57 -0700401 new DecelerateInterpolator(2), new AccelerateInterpolator(2),
Adam Cohened66b2b2012-01-23 17:28:51 -0800402 postAnimationRunnable, DragLayer.ANIMATION_END_DISAPPEAR, null);
Adam Cohenfb91f302012-06-11 15:45:18 -0700403 addItem(item);
404 mHiddenItems.add(item);
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700405 mFolder.hideItem(item);
Adam Cohen558baaf2011-08-15 15:22:57 -0700406 postDelayed(new Runnable() {
407 public void run() {
Adam Cohenfb91f302012-06-11 15:45:18 -0700408 mHiddenItems.remove(item);
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700409 mFolder.showItem(item);
Adam Cohenfb91f302012-06-11 15:45:18 -0700410 invalidate();
Adam Cohen558baaf2011-08-15 15:22:57 -0700411 }
412 }, DROP_IN_ANIMATION_DURATION);
413 } else {
414 addItem(item);
415 }
Adam Cohend0445262011-07-04 23:53:22 -0700416 }
417
Adam Cohen3e8f8112011-07-02 18:03:00 -0700418 public void onDrop(DragObject d) {
Adam Cohenc0dcf592011-06-01 15:30:43 -0700419 ShortcutInfo item;
Adam Cohen3e8f8112011-07-02 18:03:00 -0700420 if (d.dragInfo instanceof ApplicationInfo) {
Adam Cohenc0dcf592011-06-01 15:30:43 -0700421 // Came from all apps -- make a copy
Adam Cohen3e8f8112011-07-02 18:03:00 -0700422 item = ((ApplicationInfo) d.dragInfo).makeShortcut();
Adam Cohenc0dcf592011-06-01 15:30:43 -0700423 } else {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700424 item = (ShortcutInfo) d.dragInfo;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700425 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700426 mFolder.notifyDrop();
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800427 onDrop(item, d.dragView, null, 1.0f, mInfo.contents.size(), d.postAnimationRunnable, d);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700428 }
429
Adam Cohend0445262011-07-04 23:53:22 -0700430 private void computePreviewDrawingParams(int drawableSize, int totalSize) {
431 if (mIntrinsicIconSize != drawableSize || mTotalWidth != totalSize) {
432 mIntrinsicIconSize = drawableSize;
433 mTotalWidth = totalSize;
434
435 final int previewSize = FolderRingAnimator.sPreviewSize;
436 final int previewPadding = FolderRingAnimator.sPreviewPadding;
437
438 mAvailableSpaceInPreview = (previewSize - 2 * previewPadding);
439 // cos(45) = 0.707 + ~= 0.1) = 0.8f
440 int adjustedAvailableSpace = (int) ((mAvailableSpaceInPreview / 2) * (1 + 0.8f));
441
442 int unscaledHeight = (int) (mIntrinsicIconSize * (1 + PERSPECTIVE_SHIFT_FACTOR));
443 mBaselineIconScale = (1.0f * adjustedAvailableSpace / unscaledHeight);
444
445 mBaselineIconSize = (int) (mIntrinsicIconSize * mBaselineIconScale);
446 mMaxPerspectiveShift = mBaselineIconSize * PERSPECTIVE_SHIFT_FACTOR;
447
448 mPreviewOffsetX = (mTotalWidth - mAvailableSpaceInPreview) / 2;
449 mPreviewOffsetY = previewPadding;
450 }
451 }
452
453 private void computePreviewDrawingParams(Drawable d) {
454 computePreviewDrawingParams(d.getIntrinsicWidth(), getMeasuredWidth());
455 }
456
457 class PreviewItemDrawingParams {
458 PreviewItemDrawingParams(float transX, float transY, float scale, int overlayAlpha) {
459 this.transX = transX;
460 this.transY = transY;
461 this.scale = scale;
462 this.overlayAlpha = overlayAlpha;
463 }
464 float transX;
465 float transY;
466 float scale;
467 int overlayAlpha;
468 Drawable drawable;
469 }
470
Adam Cohenac8c8762011-07-13 11:15:27 -0700471 private float getLocalCenterForIndex(int index, int[] center) {
Adam Cohend0445262011-07-04 23:53:22 -0700472 mParams = computePreviewItemDrawingParams(Math.min(NUM_ITEMS_IN_PREVIEW, index), mParams);
473
474 mParams.transX += mPreviewOffsetX;
475 mParams.transY += mPreviewOffsetY;
Adam Cohenac8c8762011-07-13 11:15:27 -0700476 float offsetX = mParams.transX + (mParams.scale * mIntrinsicIconSize) / 2;
477 float offsetY = mParams.transY + (mParams.scale * mIntrinsicIconSize) / 2;
Adam Cohend0445262011-07-04 23:53:22 -0700478
Adam Cohenac8c8762011-07-13 11:15:27 -0700479 center[0] = (int) Math.round(offsetX);
480 center[1] = (int) Math.round(offsetY);
Adam Cohend0445262011-07-04 23:53:22 -0700481 return mParams.scale;
482 }
483
484 private PreviewItemDrawingParams computePreviewItemDrawingParams(int index,
485 PreviewItemDrawingParams params) {
486 index = NUM_ITEMS_IN_PREVIEW - index - 1;
487 float r = (index * 1.0f) / (NUM_ITEMS_IN_PREVIEW - 1);
488 float scale = (1 - PERSPECTIVE_SCALE_FACTOR * (1 - r));
489
490 float offset = (1 - r) * mMaxPerspectiveShift;
491 float scaledSize = scale * mBaselineIconSize;
492 float scaleOffsetCorrection = (1 - scale) * mBaselineIconSize;
493
494 // We want to imagine our coordinates from the bottom left, growing up and to the
495 // right. This is natural for the x-axis, but for the y-axis, we have to invert things.
496 float transY = mAvailableSpaceInPreview - (offset + scaledSize + scaleOffsetCorrection);
497 float transX = offset + scaleOffsetCorrection;
498 float totalScale = mBaselineIconScale * scale;
499 final int overlayAlpha = (int) (80 * (1 - r));
500
501 if (params == null) {
502 params = new PreviewItemDrawingParams(transX, transY, totalScale, overlayAlpha);
503 } else {
504 params.transX = transX;
505 params.transY = transY;
506 params.scale = totalScale;
507 params.overlayAlpha = overlayAlpha;
508 }
509 return params;
510 }
511
512 private void drawPreviewItem(Canvas canvas, PreviewItemDrawingParams params) {
513 canvas.save();
514 canvas.translate(params.transX + mPreviewOffsetX, params.transY + mPreviewOffsetY);
515 canvas.scale(params.scale, params.scale);
516 Drawable d = params.drawable;
517
518 if (d != null) {
519 d.setBounds(0, 0, mIntrinsicIconSize, mIntrinsicIconSize);
520 d.setFilterBitmap(true);
521 d.setColorFilter(Color.argb(params.overlayAlpha, 0, 0, 0), PorterDuff.Mode.SRC_ATOP);
522 d.draw(canvas);
523 d.clearColorFilter();
524 d.setFilterBitmap(false);
525 }
526 canvas.restore();
527 }
528
Adam Cohena9cf38f2011-05-02 15:36:58 -0700529 @Override
Adam Cohenc0dcf592011-06-01 15:30:43 -0700530 protected void dispatchDraw(Canvas canvas) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700531 super.dispatchDraw(canvas);
532
Adam Cohena9cf38f2011-05-02 15:36:58 -0700533 if (mFolder == null) return;
Adam Cohend0445262011-07-04 23:53:22 -0700534 if (mFolder.getItemCount() == 0 && !mAnimating) return;
Adam Cohena9cf38f2011-05-02 15:36:58 -0700535
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700536 ArrayList<View> items = mFolder.getItemsInReadingOrder();
Adam Cohend0445262011-07-04 23:53:22 -0700537 Drawable d;
538 TextView v;
Adam Cohenbadf71e2011-05-26 19:08:29 -0700539
Adam Cohend0445262011-07-04 23:53:22 -0700540 // Update our drawing parameters if necessary
541 if (mAnimating) {
542 computePreviewDrawingParams(mAnimParams.drawable);
543 } else {
544 v = (TextView) items.get(0);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700545 d = v.getCompoundDrawables()[1];
Adam Cohend0445262011-07-04 23:53:22 -0700546 computePreviewDrawingParams(d);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700547 }
Adam Cohend0445262011-07-04 23:53:22 -0700548
549 int nItemsInPreview = Math.min(items.size(), NUM_ITEMS_IN_PREVIEW);
550 if (!mAnimating) {
551 for (int i = nItemsInPreview - 1; i >= 0; i--) {
552 v = (TextView) items.get(i);
Adam Cohenfb91f302012-06-11 15:45:18 -0700553 if (!mHiddenItems.contains(v.getTag())) {
554 d = v.getCompoundDrawables()[1];
555 mParams = computePreviewItemDrawingParams(i, mParams);
556 mParams.drawable = d;
557 drawPreviewItem(canvas, mParams);
558 }
Adam Cohend0445262011-07-04 23:53:22 -0700559 }
560 } else {
561 drawPreviewItem(canvas, mAnimParams);
562 }
563 }
564
Adam Cohenfb91f302012-06-11 15:45:18 -0700565 private void animateFirstItem(final Drawable d, int duration, final boolean reverse,
566 final Runnable onCompleteRunnable) {
Adam Cohend0445262011-07-04 23:53:22 -0700567 final PreviewItemDrawingParams finalParams = computePreviewItemDrawingParams(0, null);
568
569 final float scale0 = 1.0f;
Adam Cohen1d4ee4e2011-08-12 15:51:59 -0700570 final float transX0 = (mAvailableSpaceInPreview - d.getIntrinsicWidth()) / 2;
571 final float transY0 = (mAvailableSpaceInPreview - d.getIntrinsicHeight()) / 2;
Adam Cohend0445262011-07-04 23:53:22 -0700572 mAnimParams.drawable = d;
573
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100574 ValueAnimator va = LauncherAnimUtils.ofFloat(this, 0f, 1.0f);
Adam Cohend0445262011-07-04 23:53:22 -0700575 va.addUpdateListener(new AnimatorUpdateListener(){
576 public void onAnimationUpdate(ValueAnimator animation) {
577 float progress = (Float) animation.getAnimatedValue();
Adam Cohenfb91f302012-06-11 15:45:18 -0700578 if (reverse) {
579 progress = 1 - progress;
580 mPreviewBackground.setAlpha(progress);
581 }
Adam Cohend0445262011-07-04 23:53:22 -0700582
583 mAnimParams.transX = transX0 + progress * (finalParams.transX - transX0);
584 mAnimParams.transY = transY0 + progress * (finalParams.transY - transY0);
585 mAnimParams.scale = scale0 + progress * (finalParams.scale - scale0);
586 invalidate();
587 }
588 });
589 va.addListener(new AnimatorListenerAdapter() {
590 @Override
591 public void onAnimationStart(Animator animation) {
592 mAnimating = true;
593 }
594 @Override
595 public void onAnimationEnd(Animator animation) {
596 mAnimating = false;
Adam Cohenfb91f302012-06-11 15:45:18 -0700597 if (onCompleteRunnable != null) {
598 onCompleteRunnable.run();
599 }
Adam Cohend0445262011-07-04 23:53:22 -0700600 }
601 });
602 va.setDuration(duration);
603 va.start();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700604 }
605
Adam Cohen099f60d2011-08-23 21:07:26 -0700606 public void setTextVisible(boolean visible) {
607 if (visible) {
608 mFolderName.setVisibility(VISIBLE);
609 } else {
610 mFolderName.setVisibility(INVISIBLE);
611 }
612 }
613
614 public boolean getTextVisible() {
615 return mFolderName.getVisibility() == VISIBLE;
616 }
617
Adam Cohen76078c42011-06-09 15:06:52 -0700618 public void onItemsChanged() {
619 invalidate();
620 requestLayout();
621 }
622
Adam Cohena9cf38f2011-05-02 15:36:58 -0700623 public void onAdd(ShortcutInfo item) {
624 invalidate();
625 requestLayout();
626 }
627
628 public void onRemove(ShortcutInfo item) {
629 invalidate();
630 requestLayout();
631 }
Adam Cohen76fc0852011-06-17 13:26:23 -0700632
633 public void onTitleChanged(CharSequence title) {
Adam Cohend63cfa92011-06-24 14:17:17 -0700634 mFolderName.setText(title.toString());
Michael Jurka8b805b12012-04-18 14:23:14 -0700635 setContentDescription(String.format(getContext().getString(R.string.folder_name_format),
Adam Cohen3371da02011-10-25 21:38:29 -0700636 title));
Adam Cohen76fc0852011-06-17 13:26:23 -0700637 }
Winson Chung88f33452012-02-23 15:23:44 -0800638
639 @Override
640 public boolean onTouchEvent(MotionEvent event) {
641 // Call the superclass onTouchEvent first, because sometimes it changes the state to
642 // isPressed() on an ACTION_UP
643 boolean result = super.onTouchEvent(event);
644
645 switch (event.getAction()) {
646 case MotionEvent.ACTION_DOWN:
647 mLongPressHelper.postCheckForLongPress();
648 break;
649 case MotionEvent.ACTION_CANCEL:
650 case MotionEvent.ACTION_UP:
651 mLongPressHelper.cancelLongPress();
652 break;
653 }
654 return result;
655 }
656
657 @Override
658 public void cancelLongPress() {
659 super.cancelLongPress();
660
661 mLongPressHelper.cancelLongPress();
662 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800663}