blob: 6cd784794edb380e6b3503cf372fc2b8d9b9430d [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
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
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;
30import android.util.AttributeSet;
31import android.view.LayoutInflater;
Adam Cohen7c693212011-05-18 15:26:57 -070032import android.view.View;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.view.ViewGroup;
Adam Cohen3e8f8112011-07-02 18:03:00 -070034import android.view.animation.AccelerateInterpolator;
35import android.view.animation.DecelerateInterpolator;
Adam Cohen76fc0852011-06-17 13:26:23 -070036import android.widget.ImageView;
37import android.widget.LinearLayout;
Adam Cohena9cf38f2011-05-02 15:36:58 -070038import android.widget.TextView;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039
Romain Guyedcce092010-03-04 13:03:17 -080040import com.android.launcher.R;
Adam Cohenc0dcf592011-06-01 15:30:43 -070041import com.android.launcher2.DropTarget.DragObject;
Adam Cohena9cf38f2011-05-02 15:36:58 -070042import com.android.launcher2.FolderInfo.FolderListener;
Romain Guyedcce092010-03-04 13:03:17 -080043
Adam Cohenc0dcf592011-06-01 15:30:43 -070044import java.util.ArrayList;
45
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046/**
47 * An icon that can appear on in the workspace representing an {@link UserFolder}.
48 */
Adam Cohen76fc0852011-06-17 13:26:23 -070049public class FolderIcon extends LinearLayout implements FolderListener {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050 private Launcher mLauncher;
Adam Cohena9cf38f2011-05-02 15:36:58 -070051 Folder mFolder;
52 FolderInfo mInfo;
53
Adam Cohenbadf71e2011-05-26 19:08:29 -070054 // The number of icons to display in the
Adam Cohen76fc0852011-06-17 13:26:23 -070055 private static final int NUM_ITEMS_IN_PREVIEW = 3;
Adam Cohenf4b08912011-05-17 18:45:47 -070056 private static final int CONSUMPTION_ANIMATION_DURATION = 100;
Adam Cohend0445262011-07-04 23:53:22 -070057 private static final int DROP_IN_ANIMATION_DURATION = 400;
Adam Cohen8dfcba42011-07-07 16:38:18 -070058 private static final int INITIAL_ITEM_ANIMATION_DURATION = 350;
Adam Cohenbadf71e2011-05-26 19:08:29 -070059
60 // The degree to which the inner ring grows when accepting drop
Adam Cohen69ce2e52011-07-03 19:25:21 -070061 private static final float INNER_RING_GROWTH_FACTOR = 0.15f;
Adam Cohenbadf71e2011-05-26 19:08:29 -070062
Adam Cohenbadf71e2011-05-26 19:08:29 -070063 // The degree to which the outer ring is scaled in its natural state
Adam Cohen69ce2e52011-07-03 19:25:21 -070064 private static final float OUTER_RING_GROWTH_FACTOR = 0.3f;
Adam Cohenbadf71e2011-05-26 19:08:29 -070065
66 // The amount of vertical spread between items in the stack [0...1]
Adam Cohen76fc0852011-06-17 13:26:23 -070067 private static final float PERSPECTIVE_SHIFT_FACTOR = 0.24f;
Adam Cohenbadf71e2011-05-26 19:08:29 -070068
69 // The degree to which the item in the back of the stack is scaled [0...1]
70 // (0 means it's not scaled at all, 1 means it's scaled to nothing)
Adam Cohen76fc0852011-06-17 13:26:23 -070071 private static final float PERSPECTIVE_SCALE_FACTOR = 0.35f;
Adam Cohenbadf71e2011-05-26 19:08:29 -070072
Adam Cohenc51934b2011-07-26 21:07:43 -070073 public static Drawable sSharedFolderLeaveBehind = null;
74
Adam Cohen76fc0852011-06-17 13:26:23 -070075 private ImageView mPreviewBackground;
76 private BubbleTextView mFolderName;
Adam Cohen073a46f2011-05-17 16:28:09 -070077
Adam Cohen19072da2011-05-31 14:30:45 -070078 FolderRingAnimator mFolderRingAnimator = null;
Adam Cohen2801caf2011-05-13 20:57:39 -070079
Adam Cohend0445262011-07-04 23:53:22 -070080 // These variables are all associated with the drawing of the preview; they are stored
81 // as member variables for shared usage and to avoid computation on each frame
82 private int mIntrinsicIconSize;
83 private float mBaselineIconScale;
84 private int mBaselineIconSize;
85 private int mAvailableSpaceInPreview;
86 private int mTotalWidth = -1;
87 private int mPreviewOffsetX;
88 private int mPreviewOffsetY;
89 private float mMaxPerspectiveShift;
90 boolean mAnimating = false;
91 private PreviewItemDrawingParams mParams = new PreviewItemDrawingParams(0, 0, 0, 0);
92 private PreviewItemDrawingParams mAnimParams = new PreviewItemDrawingParams(0, 0, 0, 0);
93
The Android Open Source Project31dd5032009-03-03 19:32:27 -080094 public FolderIcon(Context context, AttributeSet attrs) {
95 super(context, attrs);
96 }
97
98 public FolderIcon(Context context) {
99 super(context);
100 }
101
Michael Jurka0280c3b2010-09-17 15:00:07 -0700102 public boolean isDropEnabled() {
Winson Chung7a1d1652011-03-18 15:56:01 -0700103 final ViewGroup cellLayoutChildren = (ViewGroup) getParent();
104 final ViewGroup cellLayout = (ViewGroup) cellLayoutChildren.getParent();
105 final Workspace workspace = (Workspace) cellLayout.getParent();
106 return !workspace.isSmall();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700107 }
108
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800109 static FolderIcon fromXml(int resId, Launcher launcher, ViewGroup group,
Adam Cohendf2cc412011-04-27 16:56:57 -0700110 FolderInfo folderInfo, IconCache iconCache) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800111
Adam Cohend0445262011-07-04 23:53:22 -0700112 if (INITIAL_ITEM_ANIMATION_DURATION >= DROP_IN_ANIMATION_DURATION) {
113 throw new IllegalStateException("DROP_IN_ANIMATION_DURATION must be greater than " +
114 "INITIAL_ITEM_ANIMATION_DURATION, as sequencing of adding first two items " +
115 "is dependent on this");
116 }
117
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800118 FolderIcon icon = (FolderIcon) LayoutInflater.from(launcher).inflate(resId, group, false);
119
Adam Cohend2eca6b2011-07-26 22:49:13 -0700120 icon.mFolderName = (BubbleTextView) icon.findViewById(R.id.folder_icon_name);
Adam Cohen76fc0852011-06-17 13:26:23 -0700121 icon.mFolderName.setText(folderInfo.title);
122 icon.mPreviewBackground = (ImageView) icon.findViewById(R.id.preview_background);
Winson Chung6a0f57d2011-06-29 20:10:49 -0700123 icon.mPreviewBackground.setContentDescription(folderInfo.title);
Adam Cohen76fc0852011-06-17 13:26:23 -0700124
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125 icon.setTag(folderInfo);
126 icon.setOnClickListener(launcher);
127 icon.mInfo = folderInfo;
128 icon.mLauncher = launcher;
Adam Cohena9cf38f2011-05-02 15:36:58 -0700129
130 Folder folder = Folder.fromXml(launcher);
131 folder.setDragController(launcher.getDragController());
Adam Cohen2801caf2011-05-13 20:57:39 -0700132 folder.setFolderIcon(icon);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700133 folder.bind(folderInfo);
134 icon.mFolder = folder;
Adam Cohen19072da2011-05-31 14:30:45 -0700135 icon.mFolderRingAnimator = new FolderRingAnimator(launcher, icon);
Adam Cohen69ce2e52011-07-03 19:25:21 -0700136
Adam Cohena9cf38f2011-05-02 15:36:58 -0700137 folderInfo.addListener(icon);
Adam Cohen19072da2011-05-31 14:30:45 -0700138
Adam Cohenc51934b2011-07-26 21:07:43 -0700139 Resources res = launcher.getResources();
140 if (sSharedFolderLeaveBehind == null) {
141 sSharedFolderLeaveBehind = res.getDrawable(R.drawable.portal_ring_rest);
142 }
143
Adam Cohen19072da2011-05-31 14:30:45 -0700144 return icon;
145 }
146
147 public static class FolderRingAnimator {
Adam Cohen69ce2e52011-07-03 19:25:21 -0700148 public int mCellX;
149 public int mCellY;
150 private CellLayout mCellLayout;
Adam Cohen76fc0852011-06-17 13:26:23 -0700151 public float mOuterRingSize;
152 public float mInnerRingSize;
Adam Cohen19072da2011-05-31 14:30:45 -0700153 public FolderIcon mFolderIcon = null;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700154 public Drawable mOuterRingDrawable = null;
155 public Drawable mInnerRingDrawable = null;
156 public static Drawable sSharedOuterRingDrawable = null;
157 public static Drawable sSharedInnerRingDrawable = null;
Adam Cohen76fc0852011-06-17 13:26:23 -0700158 public static int sPreviewSize = -1;
159 public static int sPreviewPadding = -1;
160
Adam Cohenc0dcf592011-06-01 15:30:43 -0700161 private ValueAnimator mAcceptAnimator;
162 private ValueAnimator mNeutralAnimator;
Adam Cohen19072da2011-05-31 14:30:45 -0700163
164 public FolderRingAnimator(Launcher launcher, FolderIcon folderIcon) {
Adam Cohen19072da2011-05-31 14:30:45 -0700165 mFolderIcon = folderIcon;
Adam Cohen76fc0852011-06-17 13:26:23 -0700166 Resources res = launcher.getResources();
167 mOuterRingDrawable = res.getDrawable(R.drawable.portal_ring_outer_holo);
168 mInnerRingDrawable = res.getDrawable(R.drawable.portal_ring_inner_holo);
169
170 if (sPreviewSize < 0 || sPreviewPadding < 0) {
171 sPreviewSize = res.getDimensionPixelSize(R.dimen.folder_preview_size);
172 sPreviewPadding = res.getDimensionPixelSize(R.dimen.folder_preview_padding);
173 }
Adam Cohenc0dcf592011-06-01 15:30:43 -0700174 if (sSharedOuterRingDrawable == null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700175 sSharedOuterRingDrawable = res.getDrawable(R.drawable.portal_ring_outer_holo);
Adam Cohen19072da2011-05-31 14:30:45 -0700176 }
Adam Cohenc0dcf592011-06-01 15:30:43 -0700177 if (sSharedInnerRingDrawable == null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700178 sSharedInnerRingDrawable = res.getDrawable(R.drawable.portal_ring_inner_holo);
Adam Cohen19072da2011-05-31 14:30:45 -0700179 }
Adam Cohen073a46f2011-05-17 16:28:09 -0700180 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700181
Adam Cohen19072da2011-05-31 14:30:45 -0700182 public void animateToAcceptState() {
Adam Cohenc0dcf592011-06-01 15:30:43 -0700183 if (mNeutralAnimator != null) {
184 mNeutralAnimator.cancel();
185 }
186 mAcceptAnimator = ValueAnimator.ofFloat(0f, 1f);
187 mAcceptAnimator.setDuration(CONSUMPTION_ANIMATION_DURATION);
188 mAcceptAnimator.addUpdateListener(new AnimatorUpdateListener() {
Adam Cohen19072da2011-05-31 14:30:45 -0700189 public void onAnimationUpdate(ValueAnimator animation) {
190 final float percent = (Float) animation.getAnimatedValue();
Adam Cohen76fc0852011-06-17 13:26:23 -0700191 mOuterRingSize = (1 + percent * OUTER_RING_GROWTH_FACTOR) * sPreviewSize;
192 mInnerRingSize = (1 + percent * INNER_RING_GROWTH_FACTOR) * sPreviewSize;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700193 if (mCellLayout != null) {
194 mCellLayout.invalidate();
Adam Cohen19072da2011-05-31 14:30:45 -0700195 }
196 }
197 });
Adam Cohenc0dcf592011-06-01 15:30:43 -0700198 mAcceptAnimator.addListener(new AnimatorListenerAdapter() {
Adam Cohen19072da2011-05-31 14:30:45 -0700199 @Override
Adam Cohenc0dcf592011-06-01 15:30:43 -0700200 public void onAnimationStart(Animator animation) {
Adam Cohen19072da2011-05-31 14:30:45 -0700201 if (mFolderIcon != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700202 mFolderIcon.mPreviewBackground.setVisibility(INVISIBLE);
Adam Cohen19072da2011-05-31 14:30:45 -0700203 }
204 }
205 });
Adam Cohenc0dcf592011-06-01 15:30:43 -0700206 mAcceptAnimator.start();
Adam Cohen19072da2011-05-31 14:30:45 -0700207 }
208
209 public void animateToNaturalState() {
Adam Cohenc0dcf592011-06-01 15:30:43 -0700210 if (mAcceptAnimator != null) {
211 mAcceptAnimator.cancel();
212 }
213 mNeutralAnimator = ValueAnimator.ofFloat(0f, 1f);
214 mNeutralAnimator.setDuration(CONSUMPTION_ANIMATION_DURATION);
215 mNeutralAnimator.addUpdateListener(new AnimatorUpdateListener() {
Adam Cohen19072da2011-05-31 14:30:45 -0700216 public void onAnimationUpdate(ValueAnimator animation) {
217 final float percent = (Float) animation.getAnimatedValue();
Adam Cohen76fc0852011-06-17 13:26:23 -0700218 mOuterRingSize = (1 + (1 - percent) * OUTER_RING_GROWTH_FACTOR) * sPreviewSize;
219 mInnerRingSize = (1 + (1 - percent) * INNER_RING_GROWTH_FACTOR) * sPreviewSize;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700220 if (mCellLayout != null) {
221 mCellLayout.invalidate();
Adam Cohen19072da2011-05-31 14:30:45 -0700222 }
223 }
224 });
Adam Cohenc0dcf592011-06-01 15:30:43 -0700225 mNeutralAnimator.addListener(new AnimatorListenerAdapter() {
Adam Cohen19072da2011-05-31 14:30:45 -0700226 @Override
227 public void onAnimationEnd(Animator animation) {
Adam Cohen69ce2e52011-07-03 19:25:21 -0700228 if (mCellLayout != null) {
229 mCellLayout.hideFolderAccept(FolderRingAnimator.this);
230 }
Adam Cohen19072da2011-05-31 14:30:45 -0700231 if (mFolderIcon != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700232 mFolderIcon.mPreviewBackground.setVisibility(VISIBLE);
Adam Cohen19072da2011-05-31 14:30:45 -0700233 }
Adam Cohen19072da2011-05-31 14:30:45 -0700234 }
235 });
Adam Cohenc0dcf592011-06-01 15:30:43 -0700236 mNeutralAnimator.start();
Adam Cohen19072da2011-05-31 14:30:45 -0700237 }
238
Adam Cohenc0dcf592011-06-01 15:30:43 -0700239 // Location is expressed in window coordinates
Adam Cohen69ce2e52011-07-03 19:25:21 -0700240 public void getCell(int[] loc) {
241 loc[0] = mCellX;
242 loc[1] = mCellY;
243 }
244
245 // Location is expressed in window coordinates
246 public void setCell(int x, int y) {
247 mCellX = x;
248 mCellY = y;
249 }
250
251 public void setCellLayout(CellLayout layout) {
252 mCellLayout = layout;
Adam Cohen19072da2011-05-31 14:30:45 -0700253 }
254
Adam Cohen76fc0852011-06-17 13:26:23 -0700255 public float getOuterRingSize() {
256 return mOuterRingSize;
Adam Cohen19072da2011-05-31 14:30:45 -0700257 }
258
Adam Cohen76fc0852011-06-17 13:26:23 -0700259 public float getInnerRingSize() {
260 return mInnerRingSize;
Adam Cohen19072da2011-05-31 14:30:45 -0700261 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800262 }
263
Adam Cohen073a46f2011-05-17 16:28:09 -0700264 private boolean willAcceptItem(ItemInfo item) {
265 final int itemType = item.itemType;
266 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
267 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
Adam Cohenc51934b2011-07-26 21:07:43 -0700268 !mFolder.isFull() && item != mInfo && !mInfo.opened);
Adam Cohen073a46f2011-05-17 16:28:09 -0700269 }
270
Adam Cohenc0dcf592011-06-01 15:30:43 -0700271 public boolean acceptDrop(Object dragInfo) {
272 final ItemInfo item = (ItemInfo) dragInfo;
Adam Cohen073a46f2011-05-17 16:28:09 -0700273 return willAcceptItem(item);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800274 }
275
Adam Cohendf035382011-04-11 17:22:04 -0700276 public void addItem(ShortcutInfo item) {
277 mInfo.add(item);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700278 LauncherModel.addOrMoveItemInDatabase(mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
Adam Cohendf035382011-04-11 17:22:04 -0700279 }
280
Adam Cohenc0dcf592011-06-01 15:30:43 -0700281 public void onDragEnter(Object dragInfo) {
282 if (!willAcceptItem((ItemInfo) dragInfo)) return;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700283 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) getLayoutParams();
284 CellLayout layout = (CellLayout) getParent().getParent();
285 mFolderRingAnimator.setCell(lp.cellX, lp.cellY);
286 mFolderRingAnimator.setCellLayout(layout);
Adam Cohen19072da2011-05-31 14:30:45 -0700287 mFolderRingAnimator.animateToAcceptState();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700288 layout.showFolderAccept(mFolderRingAnimator);
Adam Cohen073a46f2011-05-17 16:28:09 -0700289 }
290
Adam Cohenc0dcf592011-06-01 15:30:43 -0700291 public void onDragOver(Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800292 }
293
Adam Cohend0445262011-07-04 23:53:22 -0700294 public void performCreateAnimation(final ShortcutInfo destInfo, final View destView,
Winson Chung557d6ed2011-07-08 15:34:52 -0700295 final ShortcutInfo srcInfo, final View srcView, Rect dstRect,
Adam Cohenac8c8762011-07-13 11:15:27 -0700296 float scaleRelativeToDragLayer, Runnable postAnimationRunnable) {
Adam Cohend0445262011-07-04 23:53:22 -0700297
298 Drawable animateDrawable = ((TextView) destView).getCompoundDrawables()[1];
299 computePreviewDrawingParams(animateDrawable.getIntrinsicWidth(), destView.getMeasuredWidth());
Adam Cohenac8c8762011-07-13 11:15:27 -0700300
Adam Cohend0445262011-07-04 23:53:22 -0700301 // This will animate the dragView (srcView) into the new folder
Adam Cohenac8c8762011-07-13 11:15:27 -0700302 onDrop(srcInfo, srcView, dstRect, scaleRelativeToDragLayer, 1, postAnimationRunnable);
Adam Cohend0445262011-07-04 23:53:22 -0700303
304 // This will animate the first item from it's position as an icon into its
305 // position as the first item in the preview
Adam Cohen8dfcba42011-07-07 16:38:18 -0700306 animateFirstItem(animateDrawable, INITIAL_ITEM_ANIMATION_DURATION);
Adam Cohend0445262011-07-04 23:53:22 -0700307
308 postDelayed(new Runnable() {
309 public void run() {
310 addItem(destInfo);
311 }
312 }, INITIAL_ITEM_ANIMATION_DURATION);
313 }
314
Adam Cohenc0dcf592011-06-01 15:30:43 -0700315 public void onDragExit(Object dragInfo) {
316 if (!willAcceptItem((ItemInfo) dragInfo)) return;
Adam Cohen19072da2011-05-31 14:30:45 -0700317 mFolderRingAnimator.animateToNaturalState();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800318 }
Patrick Dubroy440c3602010-07-13 17:50:32 -0700319
Adam Cohenac8c8762011-07-13 11:15:27 -0700320 private void onDrop(final ShortcutInfo item, View animateView, Rect finalRect,
321 float scaleRelativeToDragLayer, int index, Runnable postAnimationRunnable) {
Adam Cohend0445262011-07-04 23:53:22 -0700322 item.cellX = -1;
323 item.cellY = -1;
324 DragLayer dragLayer = mLauncher.getDragLayer();
325 Rect from = new Rect();
326 dragLayer.getViewRectRelativeToSelf(animateView, from);
327 Rect to = finalRect;
328 if (to == null) {
329 to = new Rect();
Adam Cohen4b285c52011-07-21 14:24:06 -0700330 Workspace workspace = mLauncher.getWorkspace();
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700331 // Set cellLayout and this to it's final state to compute final animation locations
Adam Cohen4b285c52011-07-21 14:24:06 -0700332 workspace.setFinalTransitionTransform((CellLayout) getParent().getParent());
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700333 float scaleX = getScaleX();
334 float scaleY = getScaleY();
335 setScaleX(1.0f);
336 setScaleY(1.0f);
Adam Cohenac8c8762011-07-13 11:15:27 -0700337 scaleRelativeToDragLayer = dragLayer.getDescendantRectRelativeToSelf(this, to);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700338 // Finished computing final animation locations, restore current state
339 setScaleX(scaleX);
340 setScaleY(scaleY);
Adam Cohen4b285c52011-07-21 14:24:06 -0700341 workspace.resetTransitionTransform((CellLayout) getParent().getParent());
Adam Cohend0445262011-07-04 23:53:22 -0700342 }
343
Adam Cohenac8c8762011-07-13 11:15:27 -0700344 int[] center = new int[2];
345 float scale = getLocalCenterForIndex(index, center);
346 center[0] = (int) Math.round(scaleRelativeToDragLayer * center[0]);
347 center[1] = (int) Math.round(scaleRelativeToDragLayer * center[1]);
348
349 to.offset(center[0] - animateView.getMeasuredWidth() / 2,
350 center[1] - animateView.getMeasuredHeight() / 2);
Adam Cohend0445262011-07-04 23:53:22 -0700351
352 float finalAlpha = index < NUM_ITEMS_IN_PREVIEW ? 0.5f : 0f;
353
Adam Cohenac8c8762011-07-13 11:15:27 -0700354 dragLayer.animateView(animateView, from, to, finalAlpha, scale * scaleRelativeToDragLayer,
355 DROP_IN_ANIMATION_DURATION, new DecelerateInterpolator(2),
356 new AccelerateInterpolator(2), postAnimationRunnable, false);
Adam Cohend0445262011-07-04 23:53:22 -0700357 postDelayed(new Runnable() {
358 public void run() {
359 addItem(item);
360 }
361 }, DROP_IN_ANIMATION_DURATION);
362 }
363
Adam Cohen3e8f8112011-07-02 18:03:00 -0700364 public void onDrop(DragObject d) {
Adam Cohenc0dcf592011-06-01 15:30:43 -0700365 ShortcutInfo item;
Adam Cohen3e8f8112011-07-02 18:03:00 -0700366 if (d.dragInfo instanceof ApplicationInfo) {
Adam Cohenc0dcf592011-06-01 15:30:43 -0700367 // Came from all apps -- make a copy
Adam Cohen3e8f8112011-07-02 18:03:00 -0700368 item = ((ApplicationInfo) d.dragInfo).makeShortcut();
Adam Cohenc0dcf592011-06-01 15:30:43 -0700369 } else {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700370 item = (ShortcutInfo) d.dragInfo;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700371 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700372 mFolder.notifyDrop();
Adam Cohenac8c8762011-07-13 11:15:27 -0700373 onDrop(item, d.dragView, null, 1.0f, mInfo.contents.size(), d.postAnimationRunnable);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700374 }
375
Adam Cohencb3382b2011-05-24 14:07:08 -0700376 public DropTarget getDropTargetDelegate(DragObject d) {
Patrick Dubroy440c3602010-07-13 17:50:32 -0700377 return null;
378 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700379
Adam Cohend0445262011-07-04 23:53:22 -0700380 private void computePreviewDrawingParams(int drawableSize, int totalSize) {
381 if (mIntrinsicIconSize != drawableSize || mTotalWidth != totalSize) {
382 mIntrinsicIconSize = drawableSize;
383 mTotalWidth = totalSize;
384
385 final int previewSize = FolderRingAnimator.sPreviewSize;
386 final int previewPadding = FolderRingAnimator.sPreviewPadding;
387
388 mAvailableSpaceInPreview = (previewSize - 2 * previewPadding);
389 // cos(45) = 0.707 + ~= 0.1) = 0.8f
390 int adjustedAvailableSpace = (int) ((mAvailableSpaceInPreview / 2) * (1 + 0.8f));
391
392 int unscaledHeight = (int) (mIntrinsicIconSize * (1 + PERSPECTIVE_SHIFT_FACTOR));
393 mBaselineIconScale = (1.0f * adjustedAvailableSpace / unscaledHeight);
394
395 mBaselineIconSize = (int) (mIntrinsicIconSize * mBaselineIconScale);
396 mMaxPerspectiveShift = mBaselineIconSize * PERSPECTIVE_SHIFT_FACTOR;
397
398 mPreviewOffsetX = (mTotalWidth - mAvailableSpaceInPreview) / 2;
399 mPreviewOffsetY = previewPadding;
400 }
401 }
402
403 private void computePreviewDrawingParams(Drawable d) {
404 computePreviewDrawingParams(d.getIntrinsicWidth(), getMeasuredWidth());
405 }
406
407 class PreviewItemDrawingParams {
408 PreviewItemDrawingParams(float transX, float transY, float scale, int overlayAlpha) {
409 this.transX = transX;
410 this.transY = transY;
411 this.scale = scale;
412 this.overlayAlpha = overlayAlpha;
413 }
414 float transX;
415 float transY;
416 float scale;
417 int overlayAlpha;
418 Drawable drawable;
419 }
420
Adam Cohenac8c8762011-07-13 11:15:27 -0700421 private float getLocalCenterForIndex(int index, int[] center) {
Adam Cohend0445262011-07-04 23:53:22 -0700422 mParams = computePreviewItemDrawingParams(Math.min(NUM_ITEMS_IN_PREVIEW, index), mParams);
423
424 mParams.transX += mPreviewOffsetX;
425 mParams.transY += mPreviewOffsetY;
Adam Cohenac8c8762011-07-13 11:15:27 -0700426 float offsetX = mParams.transX + (mParams.scale * mIntrinsicIconSize) / 2;
427 float offsetY = mParams.transY + (mParams.scale * mIntrinsicIconSize) / 2;
Adam Cohend0445262011-07-04 23:53:22 -0700428
Adam Cohenac8c8762011-07-13 11:15:27 -0700429 center[0] = (int) Math.round(offsetX);
430 center[1] = (int) Math.round(offsetY);
Adam Cohend0445262011-07-04 23:53:22 -0700431 return mParams.scale;
432 }
433
434 private PreviewItemDrawingParams computePreviewItemDrawingParams(int index,
435 PreviewItemDrawingParams params) {
436 index = NUM_ITEMS_IN_PREVIEW - index - 1;
437 float r = (index * 1.0f) / (NUM_ITEMS_IN_PREVIEW - 1);
438 float scale = (1 - PERSPECTIVE_SCALE_FACTOR * (1 - r));
439
440 float offset = (1 - r) * mMaxPerspectiveShift;
441 float scaledSize = scale * mBaselineIconSize;
442 float scaleOffsetCorrection = (1 - scale) * mBaselineIconSize;
443
444 // We want to imagine our coordinates from the bottom left, growing up and to the
445 // right. This is natural for the x-axis, but for the y-axis, we have to invert things.
446 float transY = mAvailableSpaceInPreview - (offset + scaledSize + scaleOffsetCorrection);
447 float transX = offset + scaleOffsetCorrection;
448 float totalScale = mBaselineIconScale * scale;
449 final int overlayAlpha = (int) (80 * (1 - r));
450
451 if (params == null) {
452 params = new PreviewItemDrawingParams(transX, transY, totalScale, overlayAlpha);
453 } else {
454 params.transX = transX;
455 params.transY = transY;
456 params.scale = totalScale;
457 params.overlayAlpha = overlayAlpha;
458 }
459 return params;
460 }
461
462 private void drawPreviewItem(Canvas canvas, PreviewItemDrawingParams params) {
463 canvas.save();
464 canvas.translate(params.transX + mPreviewOffsetX, params.transY + mPreviewOffsetY);
465 canvas.scale(params.scale, params.scale);
466 Drawable d = params.drawable;
467
468 if (d != null) {
469 d.setBounds(0, 0, mIntrinsicIconSize, mIntrinsicIconSize);
470 d.setFilterBitmap(true);
471 d.setColorFilter(Color.argb(params.overlayAlpha, 0, 0, 0), PorterDuff.Mode.SRC_ATOP);
472 d.draw(canvas);
473 d.clearColorFilter();
474 d.setFilterBitmap(false);
475 }
476 canvas.restore();
477 }
478
Adam Cohena9cf38f2011-05-02 15:36:58 -0700479 @Override
Adam Cohenc0dcf592011-06-01 15:30:43 -0700480 protected void dispatchDraw(Canvas canvas) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700481 super.dispatchDraw(canvas);
482
Adam Cohena9cf38f2011-05-02 15:36:58 -0700483 if (mFolder == null) return;
Adam Cohend0445262011-07-04 23:53:22 -0700484 if (mFolder.getItemCount() == 0 && !mAnimating) return;
Adam Cohena9cf38f2011-05-02 15:36:58 -0700485
Adam Cohen76078c42011-06-09 15:06:52 -0700486 ArrayList<View> items = mFolder.getItemsInReadingOrder(false);
Adam Cohend0445262011-07-04 23:53:22 -0700487 Drawable d;
488 TextView v;
Adam Cohenbadf71e2011-05-26 19:08:29 -0700489
Adam Cohend0445262011-07-04 23:53:22 -0700490 // Update our drawing parameters if necessary
491 if (mAnimating) {
492 computePreviewDrawingParams(mAnimParams.drawable);
493 } else {
494 v = (TextView) items.get(0);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700495 d = v.getCompoundDrawables()[1];
Adam Cohend0445262011-07-04 23:53:22 -0700496 computePreviewDrawingParams(d);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700497 }
Adam Cohend0445262011-07-04 23:53:22 -0700498
499 int nItemsInPreview = Math.min(items.size(), NUM_ITEMS_IN_PREVIEW);
500 if (!mAnimating) {
501 for (int i = nItemsInPreview - 1; i >= 0; i--) {
502 v = (TextView) items.get(i);
503 d = v.getCompoundDrawables()[1];
504
505 mParams = computePreviewItemDrawingParams(i, mParams);
506 mParams.drawable = d;
507 drawPreviewItem(canvas, mParams);
508 }
509 } else {
510 drawPreviewItem(canvas, mAnimParams);
511 }
512 }
513
514 private void animateFirstItem(final Drawable d, int duration) {
515 computePreviewDrawingParams(d);
516 final PreviewItemDrawingParams finalParams = computePreviewItemDrawingParams(0, null);
517
518 final float scale0 = 1.0f;
519 final float transX0 = 0;
520 final float transY0 = 0;
521 mAnimParams.drawable = d;
522
523 ValueAnimator va = ValueAnimator.ofFloat(0f, 1.0f);
524 va.addUpdateListener(new AnimatorUpdateListener(){
525 public void onAnimationUpdate(ValueAnimator animation) {
526 float progress = (Float) animation.getAnimatedValue();
527
528 mAnimParams.transX = transX0 + progress * (finalParams.transX - transX0);
529 mAnimParams.transY = transY0 + progress * (finalParams.transY - transY0);
530 mAnimParams.scale = scale0 + progress * (finalParams.scale - scale0);
531 invalidate();
532 }
533 });
534 va.addListener(new AnimatorListenerAdapter() {
535 @Override
536 public void onAnimationStart(Animator animation) {
537 mAnimating = true;
538 }
539 @Override
540 public void onAnimationEnd(Animator animation) {
541 mAnimating = false;
542 }
543 });
544 va.setDuration(duration);
545 va.start();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700546 }
547
Adam Cohen76078c42011-06-09 15:06:52 -0700548 public void onItemsChanged() {
549 invalidate();
550 requestLayout();
551 }
552
Adam Cohena9cf38f2011-05-02 15:36:58 -0700553 public void onAdd(ShortcutInfo item) {
554 invalidate();
555 requestLayout();
556 }
557
558 public void onRemove(ShortcutInfo item) {
559 invalidate();
560 requestLayout();
561 }
Adam Cohen76fc0852011-06-17 13:26:23 -0700562
563 public void onTitleChanged(CharSequence title) {
Adam Cohend63cfa92011-06-24 14:17:17 -0700564 mFolderName.setText(title.toString());
Winson Chung6a0f57d2011-06-29 20:10:49 -0700565 mPreviewBackground.setContentDescription(title.toString());
Adam Cohen76fc0852011-06-17 13:26:23 -0700566 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800567}