blob: f1a1508567f4a41049e5582536151f34d8faf0e2 [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;
Adam Cohend0445262011-07-04 23:53:22 -0700324
Adam Cohen558baaf2011-08-15 15:22:57 -0700325 // Typically, the animateView corresponds to the DragView; however, if this is being done
326 // after a configuration activity (ie. for a Shortcut being dragged from AllApps) we
327 // will not have a view to animate
328 if (animateView != null) {
329 DragLayer dragLayer = mLauncher.getDragLayer();
330 Rect from = new Rect();
331 dragLayer.getViewRectRelativeToSelf(animateView, from);
332 Rect to = finalRect;
333 if (to == null) {
334 to = new Rect();
335 Workspace workspace = mLauncher.getWorkspace();
336 // Set cellLayout and this to it's final state to compute final animation locations
337 workspace.setFinalTransitionTransform((CellLayout) getParent().getParent());
338 float scaleX = getScaleX();
339 float scaleY = getScaleY();
340 setScaleX(1.0f);
341 setScaleY(1.0f);
342 scaleRelativeToDragLayer = dragLayer.getDescendantRectRelativeToSelf(this, to);
343 // Finished computing final animation locations, restore current state
344 setScaleX(scaleX);
345 setScaleY(scaleY);
346 workspace.resetTransitionTransform((CellLayout) getParent().getParent());
Adam Cohend0445262011-07-04 23:53:22 -0700347 }
Adam Cohen558baaf2011-08-15 15:22:57 -0700348
349 int[] center = new int[2];
350 float scale = getLocalCenterForIndex(index, center);
351 center[0] = (int) Math.round(scaleRelativeToDragLayer * center[0]);
352 center[1] = (int) Math.round(scaleRelativeToDragLayer * center[1]);
353
354 to.offset(center[0] - animateView.getMeasuredWidth() / 2,
355 center[1] - animateView.getMeasuredHeight() / 2);
356
357 float finalAlpha = index < NUM_ITEMS_IN_PREVIEW ? 0.5f : 0f;
358
359 dragLayer.animateView(animateView, from, to, finalAlpha,
360 scale * scaleRelativeToDragLayer, DROP_IN_ANIMATION_DURATION,
361 new DecelerateInterpolator(2), new AccelerateInterpolator(2),
362 postAnimationRunnable, false);
363 postDelayed(new Runnable() {
364 public void run() {
365 addItem(item);
366 }
367 }, DROP_IN_ANIMATION_DURATION);
368 } else {
369 addItem(item);
370 }
Adam Cohend0445262011-07-04 23:53:22 -0700371 }
372
Adam Cohen3e8f8112011-07-02 18:03:00 -0700373 public void onDrop(DragObject d) {
Adam Cohenc0dcf592011-06-01 15:30:43 -0700374 ShortcutInfo item;
Adam Cohen3e8f8112011-07-02 18:03:00 -0700375 if (d.dragInfo instanceof ApplicationInfo) {
Adam Cohenc0dcf592011-06-01 15:30:43 -0700376 // Came from all apps -- make a copy
Adam Cohen3e8f8112011-07-02 18:03:00 -0700377 item = ((ApplicationInfo) d.dragInfo).makeShortcut();
Adam Cohenc0dcf592011-06-01 15:30:43 -0700378 } else {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700379 item = (ShortcutInfo) d.dragInfo;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700380 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700381 mFolder.notifyDrop();
Adam Cohenac8c8762011-07-13 11:15:27 -0700382 onDrop(item, d.dragView, null, 1.0f, mInfo.contents.size(), d.postAnimationRunnable);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700383 }
384
Adam Cohencb3382b2011-05-24 14:07:08 -0700385 public DropTarget getDropTargetDelegate(DragObject d) {
Patrick Dubroy440c3602010-07-13 17:50:32 -0700386 return null;
387 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700388
Adam Cohend0445262011-07-04 23:53:22 -0700389 private void computePreviewDrawingParams(int drawableSize, int totalSize) {
390 if (mIntrinsicIconSize != drawableSize || mTotalWidth != totalSize) {
391 mIntrinsicIconSize = drawableSize;
392 mTotalWidth = totalSize;
393
394 final int previewSize = FolderRingAnimator.sPreviewSize;
395 final int previewPadding = FolderRingAnimator.sPreviewPadding;
396
397 mAvailableSpaceInPreview = (previewSize - 2 * previewPadding);
398 // cos(45) = 0.707 + ~= 0.1) = 0.8f
399 int adjustedAvailableSpace = (int) ((mAvailableSpaceInPreview / 2) * (1 + 0.8f));
400
401 int unscaledHeight = (int) (mIntrinsicIconSize * (1 + PERSPECTIVE_SHIFT_FACTOR));
402 mBaselineIconScale = (1.0f * adjustedAvailableSpace / unscaledHeight);
403
404 mBaselineIconSize = (int) (mIntrinsicIconSize * mBaselineIconScale);
405 mMaxPerspectiveShift = mBaselineIconSize * PERSPECTIVE_SHIFT_FACTOR;
406
407 mPreviewOffsetX = (mTotalWidth - mAvailableSpaceInPreview) / 2;
408 mPreviewOffsetY = previewPadding;
409 }
410 }
411
412 private void computePreviewDrawingParams(Drawable d) {
413 computePreviewDrawingParams(d.getIntrinsicWidth(), getMeasuredWidth());
414 }
415
416 class PreviewItemDrawingParams {
417 PreviewItemDrawingParams(float transX, float transY, float scale, int overlayAlpha) {
418 this.transX = transX;
419 this.transY = transY;
420 this.scale = scale;
421 this.overlayAlpha = overlayAlpha;
422 }
423 float transX;
424 float transY;
425 float scale;
426 int overlayAlpha;
427 Drawable drawable;
428 }
429
Adam Cohenac8c8762011-07-13 11:15:27 -0700430 private float getLocalCenterForIndex(int index, int[] center) {
Adam Cohend0445262011-07-04 23:53:22 -0700431 mParams = computePreviewItemDrawingParams(Math.min(NUM_ITEMS_IN_PREVIEW, index), mParams);
432
433 mParams.transX += mPreviewOffsetX;
434 mParams.transY += mPreviewOffsetY;
Adam Cohenac8c8762011-07-13 11:15:27 -0700435 float offsetX = mParams.transX + (mParams.scale * mIntrinsicIconSize) / 2;
436 float offsetY = mParams.transY + (mParams.scale * mIntrinsicIconSize) / 2;
Adam Cohend0445262011-07-04 23:53:22 -0700437
Adam Cohenac8c8762011-07-13 11:15:27 -0700438 center[0] = (int) Math.round(offsetX);
439 center[1] = (int) Math.round(offsetY);
Adam Cohend0445262011-07-04 23:53:22 -0700440 return mParams.scale;
441 }
442
443 private PreviewItemDrawingParams computePreviewItemDrawingParams(int index,
444 PreviewItemDrawingParams params) {
445 index = NUM_ITEMS_IN_PREVIEW - index - 1;
446 float r = (index * 1.0f) / (NUM_ITEMS_IN_PREVIEW - 1);
447 float scale = (1 - PERSPECTIVE_SCALE_FACTOR * (1 - r));
448
449 float offset = (1 - r) * mMaxPerspectiveShift;
450 float scaledSize = scale * mBaselineIconSize;
451 float scaleOffsetCorrection = (1 - scale) * mBaselineIconSize;
452
453 // We want to imagine our coordinates from the bottom left, growing up and to the
454 // right. This is natural for the x-axis, but for the y-axis, we have to invert things.
455 float transY = mAvailableSpaceInPreview - (offset + scaledSize + scaleOffsetCorrection);
456 float transX = offset + scaleOffsetCorrection;
457 float totalScale = mBaselineIconScale * scale;
458 final int overlayAlpha = (int) (80 * (1 - r));
459
460 if (params == null) {
461 params = new PreviewItemDrawingParams(transX, transY, totalScale, overlayAlpha);
462 } else {
463 params.transX = transX;
464 params.transY = transY;
465 params.scale = totalScale;
466 params.overlayAlpha = overlayAlpha;
467 }
468 return params;
469 }
470
471 private void drawPreviewItem(Canvas canvas, PreviewItemDrawingParams params) {
472 canvas.save();
473 canvas.translate(params.transX + mPreviewOffsetX, params.transY + mPreviewOffsetY);
474 canvas.scale(params.scale, params.scale);
475 Drawable d = params.drawable;
476
477 if (d != null) {
478 d.setBounds(0, 0, mIntrinsicIconSize, mIntrinsicIconSize);
479 d.setFilterBitmap(true);
480 d.setColorFilter(Color.argb(params.overlayAlpha, 0, 0, 0), PorterDuff.Mode.SRC_ATOP);
481 d.draw(canvas);
482 d.clearColorFilter();
483 d.setFilterBitmap(false);
484 }
485 canvas.restore();
486 }
487
Adam Cohena9cf38f2011-05-02 15:36:58 -0700488 @Override
Adam Cohenc0dcf592011-06-01 15:30:43 -0700489 protected void dispatchDraw(Canvas canvas) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700490 super.dispatchDraw(canvas);
491
Adam Cohena9cf38f2011-05-02 15:36:58 -0700492 if (mFolder == null) return;
Adam Cohend0445262011-07-04 23:53:22 -0700493 if (mFolder.getItemCount() == 0 && !mAnimating) return;
Adam Cohena9cf38f2011-05-02 15:36:58 -0700494
Adam Cohen76078c42011-06-09 15:06:52 -0700495 ArrayList<View> items = mFolder.getItemsInReadingOrder(false);
Adam Cohend0445262011-07-04 23:53:22 -0700496 Drawable d;
497 TextView v;
Adam Cohenbadf71e2011-05-26 19:08:29 -0700498
Adam Cohend0445262011-07-04 23:53:22 -0700499 // Update our drawing parameters if necessary
500 if (mAnimating) {
501 computePreviewDrawingParams(mAnimParams.drawable);
502 } else {
503 v = (TextView) items.get(0);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700504 d = v.getCompoundDrawables()[1];
Adam Cohend0445262011-07-04 23:53:22 -0700505 computePreviewDrawingParams(d);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700506 }
Adam Cohend0445262011-07-04 23:53:22 -0700507
508 int nItemsInPreview = Math.min(items.size(), NUM_ITEMS_IN_PREVIEW);
509 if (!mAnimating) {
510 for (int i = nItemsInPreview - 1; i >= 0; i--) {
511 v = (TextView) items.get(i);
512 d = v.getCompoundDrawables()[1];
513
514 mParams = computePreviewItemDrawingParams(i, mParams);
515 mParams.drawable = d;
516 drawPreviewItem(canvas, mParams);
517 }
518 } else {
519 drawPreviewItem(canvas, mAnimParams);
520 }
521 }
522
523 private void animateFirstItem(final Drawable d, int duration) {
524 computePreviewDrawingParams(d);
525 final PreviewItemDrawingParams finalParams = computePreviewItemDrawingParams(0, null);
526
527 final float scale0 = 1.0f;
Adam Cohen1d4ee4e2011-08-12 15:51:59 -0700528 final float transX0 = (mAvailableSpaceInPreview - d.getIntrinsicWidth()) / 2;
529 final float transY0 = (mAvailableSpaceInPreview - d.getIntrinsicHeight()) / 2;
Adam Cohend0445262011-07-04 23:53:22 -0700530 mAnimParams.drawable = d;
531
532 ValueAnimator va = ValueAnimator.ofFloat(0f, 1.0f);
533 va.addUpdateListener(new AnimatorUpdateListener(){
534 public void onAnimationUpdate(ValueAnimator animation) {
535 float progress = (Float) animation.getAnimatedValue();
536
537 mAnimParams.transX = transX0 + progress * (finalParams.transX - transX0);
538 mAnimParams.transY = transY0 + progress * (finalParams.transY - transY0);
539 mAnimParams.scale = scale0 + progress * (finalParams.scale - scale0);
540 invalidate();
541 }
542 });
543 va.addListener(new AnimatorListenerAdapter() {
544 @Override
545 public void onAnimationStart(Animator animation) {
546 mAnimating = true;
547 }
548 @Override
549 public void onAnimationEnd(Animator animation) {
550 mAnimating = false;
551 }
552 });
553 va.setDuration(duration);
554 va.start();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700555 }
556
Adam Cohen76078c42011-06-09 15:06:52 -0700557 public void onItemsChanged() {
558 invalidate();
559 requestLayout();
560 }
561
Adam Cohena9cf38f2011-05-02 15:36:58 -0700562 public void onAdd(ShortcutInfo item) {
563 invalidate();
564 requestLayout();
565 }
566
567 public void onRemove(ShortcutInfo item) {
568 invalidate();
569 requestLayout();
570 }
Adam Cohen76fc0852011-06-17 13:26:23 -0700571
572 public void onTitleChanged(CharSequence title) {
Adam Cohend63cfa92011-06-24 14:17:17 -0700573 mFolderName.setText(title.toString());
Winson Chung6a0f57d2011-06-29 20:10:49 -0700574 mPreviewBackground.setContentDescription(title.toString());
Adam Cohen76fc0852011-06-17 13:26:23 -0700575 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800576}