blob: cde5d947fc7a37aaf97474003953d013a34ed85d [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;
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;
Adam Cohen7c693212011-05-18 15:26:57 -070033import android.view.View;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.view.ViewGroup;
Adam Cohen3e8f8112011-07-02 18:03:00 -070035import android.view.animation.AccelerateInterpolator;
36import android.view.animation.DecelerateInterpolator;
Adam Cohen76fc0852011-06-17 13:26:23 -070037import android.widget.ImageView;
38import android.widget.LinearLayout;
Adam Cohena9cf38f2011-05-02 15:36:58 -070039import android.widget.TextView;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040
Romain Guyedcce092010-03-04 13:03:17 -080041import com.android.launcher.R;
Adam Cohenc0dcf592011-06-01 15:30:43 -070042import com.android.launcher2.DropTarget.DragObject;
Adam Cohena9cf38f2011-05-02 15:36:58 -070043import com.android.launcher2.FolderInfo.FolderListener;
Romain Guyedcce092010-03-04 13:03:17 -080044
Adam Cohenc0dcf592011-06-01 15:30:43 -070045import java.util.ArrayList;
46
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047/**
48 * An icon that can appear on in the workspace representing an {@link UserFolder}.
49 */
Adam Cohen76fc0852011-06-17 13:26:23 -070050public class FolderIcon extends LinearLayout implements FolderListener {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080051 private Launcher mLauncher;
Adam Cohena9cf38f2011-05-02 15:36:58 -070052 Folder mFolder;
53 FolderInfo mInfo;
Adam Cohen099f60d2011-08-23 21:07:26 -070054 private static boolean sStaticValuesDirty = true;
Adam Cohena9cf38f2011-05-02 15:36:58 -070055
Adam Cohenbadf71e2011-05-26 19:08:29 -070056 // The number of icons to display in the
Adam Cohen76fc0852011-06-17 13:26:23 -070057 private static final int NUM_ITEMS_IN_PREVIEW = 3;
Adam Cohenf4b08912011-05-17 18:45:47 -070058 private static final int CONSUMPTION_ANIMATION_DURATION = 100;
Adam Cohend0445262011-07-04 23:53:22 -070059 private static final int DROP_IN_ANIMATION_DURATION = 400;
Adam Cohen8dfcba42011-07-07 16:38:18 -070060 private static final int INITIAL_ITEM_ANIMATION_DURATION = 350;
Adam Cohenbadf71e2011-05-26 19:08:29 -070061
62 // The degree to which the inner ring grows when accepting drop
Adam Cohen69ce2e52011-07-03 19:25:21 -070063 private static final float INNER_RING_GROWTH_FACTOR = 0.15f;
Adam Cohenbadf71e2011-05-26 19:08:29 -070064
Adam Cohenbadf71e2011-05-26 19:08:29 -070065 // The degree to which the outer ring is scaled in its natural state
Adam Cohen69ce2e52011-07-03 19:25:21 -070066 private static final float OUTER_RING_GROWTH_FACTOR = 0.3f;
Adam Cohenbadf71e2011-05-26 19:08:29 -070067
68 // The amount of vertical spread between items in the stack [0...1]
Adam Cohen76fc0852011-06-17 13:26:23 -070069 private static final float PERSPECTIVE_SHIFT_FACTOR = 0.24f;
Adam Cohenbadf71e2011-05-26 19:08:29 -070070
71 // The degree to which the item in the back of the stack is scaled [0...1]
72 // (0 means it's not scaled at all, 1 means it's scaled to nothing)
Adam Cohen76fc0852011-06-17 13:26:23 -070073 private static final float PERSPECTIVE_SCALE_FACTOR = 0.35f;
Adam Cohenbadf71e2011-05-26 19:08:29 -070074
Adam Cohenc51934b2011-07-26 21:07:43 -070075 public static Drawable sSharedFolderLeaveBehind = null;
76
Adam Cohen76fc0852011-06-17 13:26:23 -070077 private ImageView mPreviewBackground;
78 private BubbleTextView mFolderName;
Adam Cohen073a46f2011-05-17 16:28:09 -070079
Adam Cohen19072da2011-05-31 14:30:45 -070080 FolderRingAnimator mFolderRingAnimator = null;
Adam Cohen2801caf2011-05-13 20:57:39 -070081
Adam Cohend0445262011-07-04 23:53:22 -070082 // These variables are all associated with the drawing of the preview; they are stored
83 // as member variables for shared usage and to avoid computation on each frame
84 private int mIntrinsicIconSize;
85 private float mBaselineIconScale;
86 private int mBaselineIconSize;
87 private int mAvailableSpaceInPreview;
88 private int mTotalWidth = -1;
89 private int mPreviewOffsetX;
90 private int mPreviewOffsetY;
91 private float mMaxPerspectiveShift;
92 boolean mAnimating = false;
93 private PreviewItemDrawingParams mParams = new PreviewItemDrawingParams(0, 0, 0, 0);
94 private PreviewItemDrawingParams mAnimParams = new PreviewItemDrawingParams(0, 0, 0, 0);
95
The Android Open Source Project31dd5032009-03-03 19:32:27 -080096 public FolderIcon(Context context, AttributeSet attrs) {
97 super(context, attrs);
98 }
99
100 public FolderIcon(Context context) {
101 super(context);
102 }
103
Michael Jurka0280c3b2010-09-17 15:00:07 -0700104 public boolean isDropEnabled() {
Winson Chung7a1d1652011-03-18 15:56:01 -0700105 final ViewGroup cellLayoutChildren = (ViewGroup) getParent();
106 final ViewGroup cellLayout = (ViewGroup) cellLayoutChildren.getParent();
107 final Workspace workspace = (Workspace) cellLayout.getParent();
108 return !workspace.isSmall();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700109 }
110
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800111 static FolderIcon fromXml(int resId, Launcher launcher, ViewGroup group,
Adam Cohendf2cc412011-04-27 16:56:57 -0700112 FolderInfo folderInfo, IconCache iconCache) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800113
Adam Cohend0445262011-07-04 23:53:22 -0700114 if (INITIAL_ITEM_ANIMATION_DURATION >= DROP_IN_ANIMATION_DURATION) {
115 throw new IllegalStateException("DROP_IN_ANIMATION_DURATION must be greater than " +
116 "INITIAL_ITEM_ANIMATION_DURATION, as sequencing of adding first two items " +
117 "is dependent on this");
118 }
119
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800120 FolderIcon icon = (FolderIcon) LayoutInflater.from(launcher).inflate(resId, group, false);
121
Adam Cohend2eca6b2011-07-26 22:49:13 -0700122 icon.mFolderName = (BubbleTextView) icon.findViewById(R.id.folder_icon_name);
Adam Cohen76fc0852011-06-17 13:26:23 -0700123 icon.mFolderName.setText(folderInfo.title);
124 icon.mPreviewBackground = (ImageView) icon.findViewById(R.id.preview_background);
Winson Chung6a0f57d2011-06-29 20:10:49 -0700125 icon.mPreviewBackground.setContentDescription(folderInfo.title);
Adam Cohen76fc0852011-06-17 13:26:23 -0700126
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800127 icon.setTag(folderInfo);
128 icon.setOnClickListener(launcher);
129 icon.mInfo = folderInfo;
130 icon.mLauncher = launcher;
Adam Cohena9cf38f2011-05-02 15:36:58 -0700131
132 Folder folder = Folder.fromXml(launcher);
133 folder.setDragController(launcher.getDragController());
Adam Cohen2801caf2011-05-13 20:57:39 -0700134 folder.setFolderIcon(icon);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700135 folder.bind(folderInfo);
136 icon.mFolder = folder;
Adam Cohenc51934b2011-07-26 21:07:43 -0700137 Resources res = launcher.getResources();
Adam Cohen099f60d2011-08-23 21:07:26 -0700138
139 // We need to reload the static values when configuration changes in case they are
140 // different in another configuration
141 if (sStaticValuesDirty) {
Adam Cohenc51934b2011-07-26 21:07:43 -0700142 sSharedFolderLeaveBehind = res.getDrawable(R.drawable.portal_ring_rest);
143 }
144
Adam Cohen099f60d2011-08-23 21:07:26 -0700145 icon.mFolderRingAnimator = new FolderRingAnimator(launcher, icon);
146 folderInfo.addListener(icon);
147
Adam Cohen19072da2011-05-31 14:30:45 -0700148 return icon;
149 }
150
Adam Cohen099f60d2011-08-23 21:07:26 -0700151 @Override
152 protected Parcelable onSaveInstanceState() {
153 sStaticValuesDirty = true;
154 return super.onSaveInstanceState();
155 }
156
Adam Cohen19072da2011-05-31 14:30:45 -0700157 public static class FolderRingAnimator {
Adam Cohen69ce2e52011-07-03 19:25:21 -0700158 public int mCellX;
159 public int mCellY;
160 private CellLayout mCellLayout;
Adam Cohen76fc0852011-06-17 13:26:23 -0700161 public float mOuterRingSize;
162 public float mInnerRingSize;
Adam Cohen19072da2011-05-31 14:30:45 -0700163 public FolderIcon mFolderIcon = null;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700164 public Drawable mOuterRingDrawable = null;
165 public Drawable mInnerRingDrawable = null;
166 public static Drawable sSharedOuterRingDrawable = null;
167 public static Drawable sSharedInnerRingDrawable = null;
Adam Cohen76fc0852011-06-17 13:26:23 -0700168 public static int sPreviewSize = -1;
169 public static int sPreviewPadding = -1;
170
Adam Cohenc0dcf592011-06-01 15:30:43 -0700171 private ValueAnimator mAcceptAnimator;
172 private ValueAnimator mNeutralAnimator;
Adam Cohen19072da2011-05-31 14:30:45 -0700173
174 public FolderRingAnimator(Launcher launcher, FolderIcon folderIcon) {
Adam Cohen19072da2011-05-31 14:30:45 -0700175 mFolderIcon = folderIcon;
Adam Cohen76fc0852011-06-17 13:26:23 -0700176 Resources res = launcher.getResources();
177 mOuterRingDrawable = res.getDrawable(R.drawable.portal_ring_outer_holo);
178 mInnerRingDrawable = res.getDrawable(R.drawable.portal_ring_inner_holo);
179
Adam Cohen099f60d2011-08-23 21:07:26 -0700180 // We need to reload the static values when configuration changes in case they are
181 // different in another configuration
182 if (sStaticValuesDirty) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700183 sPreviewSize = res.getDimensionPixelSize(R.dimen.folder_preview_size);
184 sPreviewPadding = res.getDimensionPixelSize(R.dimen.folder_preview_padding);
Adam Cohen76fc0852011-06-17 13:26:23 -0700185 sSharedOuterRingDrawable = res.getDrawable(R.drawable.portal_ring_outer_holo);
Adam Cohen76fc0852011-06-17 13:26:23 -0700186 sSharedInnerRingDrawable = res.getDrawable(R.drawable.portal_ring_inner_holo);
Adam Cohen099f60d2011-08-23 21:07:26 -0700187 sStaticValuesDirty = false;
Adam Cohen19072da2011-05-31 14:30:45 -0700188 }
Adam Cohen073a46f2011-05-17 16:28:09 -0700189 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700190
Adam Cohen19072da2011-05-31 14:30:45 -0700191 public void animateToAcceptState() {
Adam Cohenc0dcf592011-06-01 15:30:43 -0700192 if (mNeutralAnimator != null) {
193 mNeutralAnimator.cancel();
194 }
195 mAcceptAnimator = ValueAnimator.ofFloat(0f, 1f);
196 mAcceptAnimator.setDuration(CONSUMPTION_ANIMATION_DURATION);
197 mAcceptAnimator.addUpdateListener(new AnimatorUpdateListener() {
Adam Cohen19072da2011-05-31 14:30:45 -0700198 public void onAnimationUpdate(ValueAnimator animation) {
199 final float percent = (Float) animation.getAnimatedValue();
Adam Cohen76fc0852011-06-17 13:26:23 -0700200 mOuterRingSize = (1 + percent * OUTER_RING_GROWTH_FACTOR) * sPreviewSize;
201 mInnerRingSize = (1 + percent * INNER_RING_GROWTH_FACTOR) * sPreviewSize;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700202 if (mCellLayout != null) {
203 mCellLayout.invalidate();
Adam Cohen19072da2011-05-31 14:30:45 -0700204 }
205 }
206 });
Adam Cohenc0dcf592011-06-01 15:30:43 -0700207 mAcceptAnimator.addListener(new AnimatorListenerAdapter() {
Adam Cohen19072da2011-05-31 14:30:45 -0700208 @Override
Adam Cohenc0dcf592011-06-01 15:30:43 -0700209 public void onAnimationStart(Animator animation) {
Adam Cohen19072da2011-05-31 14:30:45 -0700210 if (mFolderIcon != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700211 mFolderIcon.mPreviewBackground.setVisibility(INVISIBLE);
Adam Cohen19072da2011-05-31 14:30:45 -0700212 }
213 }
214 });
Adam Cohenc0dcf592011-06-01 15:30:43 -0700215 mAcceptAnimator.start();
Adam Cohen19072da2011-05-31 14:30:45 -0700216 }
217
218 public void animateToNaturalState() {
Adam Cohenc0dcf592011-06-01 15:30:43 -0700219 if (mAcceptAnimator != null) {
220 mAcceptAnimator.cancel();
221 }
222 mNeutralAnimator = ValueAnimator.ofFloat(0f, 1f);
223 mNeutralAnimator.setDuration(CONSUMPTION_ANIMATION_DURATION);
224 mNeutralAnimator.addUpdateListener(new AnimatorUpdateListener() {
Adam Cohen19072da2011-05-31 14:30:45 -0700225 public void onAnimationUpdate(ValueAnimator animation) {
226 final float percent = (Float) animation.getAnimatedValue();
Adam Cohen76fc0852011-06-17 13:26:23 -0700227 mOuterRingSize = (1 + (1 - percent) * OUTER_RING_GROWTH_FACTOR) * sPreviewSize;
228 mInnerRingSize = (1 + (1 - percent) * INNER_RING_GROWTH_FACTOR) * sPreviewSize;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700229 if (mCellLayout != null) {
230 mCellLayout.invalidate();
Adam Cohen19072da2011-05-31 14:30:45 -0700231 }
232 }
233 });
Adam Cohenc0dcf592011-06-01 15:30:43 -0700234 mNeutralAnimator.addListener(new AnimatorListenerAdapter() {
Adam Cohen19072da2011-05-31 14:30:45 -0700235 @Override
236 public void onAnimationEnd(Animator animation) {
Adam Cohen69ce2e52011-07-03 19:25:21 -0700237 if (mCellLayout != null) {
238 mCellLayout.hideFolderAccept(FolderRingAnimator.this);
239 }
Adam Cohen19072da2011-05-31 14:30:45 -0700240 if (mFolderIcon != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700241 mFolderIcon.mPreviewBackground.setVisibility(VISIBLE);
Adam Cohen19072da2011-05-31 14:30:45 -0700242 }
Adam Cohen19072da2011-05-31 14:30:45 -0700243 }
244 });
Adam Cohenc0dcf592011-06-01 15:30:43 -0700245 mNeutralAnimator.start();
Adam Cohen19072da2011-05-31 14:30:45 -0700246 }
247
Adam Cohenc0dcf592011-06-01 15:30:43 -0700248 // Location is expressed in window coordinates
Adam Cohen69ce2e52011-07-03 19:25:21 -0700249 public void getCell(int[] loc) {
250 loc[0] = mCellX;
251 loc[1] = mCellY;
252 }
253
254 // Location is expressed in window coordinates
255 public void setCell(int x, int y) {
256 mCellX = x;
257 mCellY = y;
258 }
259
260 public void setCellLayout(CellLayout layout) {
261 mCellLayout = layout;
Adam Cohen19072da2011-05-31 14:30:45 -0700262 }
263
Adam Cohen76fc0852011-06-17 13:26:23 -0700264 public float getOuterRingSize() {
265 return mOuterRingSize;
Adam Cohen19072da2011-05-31 14:30:45 -0700266 }
267
Adam Cohen76fc0852011-06-17 13:26:23 -0700268 public float getInnerRingSize() {
269 return mInnerRingSize;
Adam Cohen19072da2011-05-31 14:30:45 -0700270 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800271 }
272
Adam Cohen073a46f2011-05-17 16:28:09 -0700273 private boolean willAcceptItem(ItemInfo item) {
274 final int itemType = item.itemType;
275 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
276 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
Adam Cohenc51934b2011-07-26 21:07:43 -0700277 !mFolder.isFull() && item != mInfo && !mInfo.opened);
Adam Cohen073a46f2011-05-17 16:28:09 -0700278 }
279
Adam Cohenc0dcf592011-06-01 15:30:43 -0700280 public boolean acceptDrop(Object dragInfo) {
281 final ItemInfo item = (ItemInfo) dragInfo;
Adam Cohen073a46f2011-05-17 16:28:09 -0700282 return willAcceptItem(item);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800283 }
284
Adam Cohendf035382011-04-11 17:22:04 -0700285 public void addItem(ShortcutInfo item) {
286 mInfo.add(item);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700287 LauncherModel.addOrMoveItemInDatabase(mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
Adam Cohendf035382011-04-11 17:22:04 -0700288 }
289
Adam Cohenc0dcf592011-06-01 15:30:43 -0700290 public void onDragEnter(Object dragInfo) {
291 if (!willAcceptItem((ItemInfo) dragInfo)) return;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700292 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) getLayoutParams();
293 CellLayout layout = (CellLayout) getParent().getParent();
294 mFolderRingAnimator.setCell(lp.cellX, lp.cellY);
295 mFolderRingAnimator.setCellLayout(layout);
Adam Cohen19072da2011-05-31 14:30:45 -0700296 mFolderRingAnimator.animateToAcceptState();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700297 layout.showFolderAccept(mFolderRingAnimator);
Adam Cohen073a46f2011-05-17 16:28:09 -0700298 }
299
Adam Cohenc0dcf592011-06-01 15:30:43 -0700300 public void onDragOver(Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800301 }
302
Adam Cohend0445262011-07-04 23:53:22 -0700303 public void performCreateAnimation(final ShortcutInfo destInfo, final View destView,
Winson Chung557d6ed2011-07-08 15:34:52 -0700304 final ShortcutInfo srcInfo, final View srcView, Rect dstRect,
Adam Cohenac8c8762011-07-13 11:15:27 -0700305 float scaleRelativeToDragLayer, Runnable postAnimationRunnable) {
Adam Cohend0445262011-07-04 23:53:22 -0700306
307 Drawable animateDrawable = ((TextView) destView).getCompoundDrawables()[1];
308 computePreviewDrawingParams(animateDrawable.getIntrinsicWidth(), destView.getMeasuredWidth());
Adam Cohenac8c8762011-07-13 11:15:27 -0700309
Adam Cohend0445262011-07-04 23:53:22 -0700310 // This will animate the dragView (srcView) into the new folder
Adam Cohenac8c8762011-07-13 11:15:27 -0700311 onDrop(srcInfo, srcView, dstRect, scaleRelativeToDragLayer, 1, postAnimationRunnable);
Adam Cohend0445262011-07-04 23:53:22 -0700312
313 // This will animate the first item from it's position as an icon into its
314 // position as the first item in the preview
Adam Cohen8dfcba42011-07-07 16:38:18 -0700315 animateFirstItem(animateDrawable, INITIAL_ITEM_ANIMATION_DURATION);
Adam Cohend0445262011-07-04 23:53:22 -0700316
317 postDelayed(new Runnable() {
318 public void run() {
319 addItem(destInfo);
320 }
321 }, INITIAL_ITEM_ANIMATION_DURATION);
322 }
323
Adam Cohenc0dcf592011-06-01 15:30:43 -0700324 public void onDragExit(Object dragInfo) {
325 if (!willAcceptItem((ItemInfo) dragInfo)) return;
Adam Cohen19072da2011-05-31 14:30:45 -0700326 mFolderRingAnimator.animateToNaturalState();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800327 }
Patrick Dubroy440c3602010-07-13 17:50:32 -0700328
Adam Cohenac8c8762011-07-13 11:15:27 -0700329 private void onDrop(final ShortcutInfo item, View animateView, Rect finalRect,
330 float scaleRelativeToDragLayer, int index, Runnable postAnimationRunnable) {
Adam Cohend0445262011-07-04 23:53:22 -0700331 item.cellX = -1;
332 item.cellY = -1;
Adam Cohend0445262011-07-04 23:53:22 -0700333
Adam Cohen558baaf2011-08-15 15:22:57 -0700334 // Typically, the animateView corresponds to the DragView; however, if this is being done
335 // after a configuration activity (ie. for a Shortcut being dragged from AllApps) we
336 // will not have a view to animate
337 if (animateView != null) {
338 DragLayer dragLayer = mLauncher.getDragLayer();
339 Rect from = new Rect();
340 dragLayer.getViewRectRelativeToSelf(animateView, from);
341 Rect to = finalRect;
342 if (to == null) {
343 to = new Rect();
344 Workspace workspace = mLauncher.getWorkspace();
345 // Set cellLayout and this to it's final state to compute final animation locations
346 workspace.setFinalTransitionTransform((CellLayout) getParent().getParent());
347 float scaleX = getScaleX();
348 float scaleY = getScaleY();
349 setScaleX(1.0f);
350 setScaleY(1.0f);
351 scaleRelativeToDragLayer = dragLayer.getDescendantRectRelativeToSelf(this, to);
352 // Finished computing final animation locations, restore current state
353 setScaleX(scaleX);
354 setScaleY(scaleY);
355 workspace.resetTransitionTransform((CellLayout) getParent().getParent());
Adam Cohend0445262011-07-04 23:53:22 -0700356 }
Adam Cohen558baaf2011-08-15 15:22:57 -0700357
358 int[] center = new int[2];
359 float scale = getLocalCenterForIndex(index, center);
360 center[0] = (int) Math.round(scaleRelativeToDragLayer * center[0]);
361 center[1] = (int) Math.round(scaleRelativeToDragLayer * center[1]);
362
363 to.offset(center[0] - animateView.getMeasuredWidth() / 2,
364 center[1] - animateView.getMeasuredHeight() / 2);
365
366 float finalAlpha = index < NUM_ITEMS_IN_PREVIEW ? 0.5f : 0f;
367
368 dragLayer.animateView(animateView, from, to, finalAlpha,
369 scale * scaleRelativeToDragLayer, DROP_IN_ANIMATION_DURATION,
370 new DecelerateInterpolator(2), new AccelerateInterpolator(2),
371 postAnimationRunnable, false);
372 postDelayed(new Runnable() {
373 public void run() {
374 addItem(item);
375 }
376 }, DROP_IN_ANIMATION_DURATION);
377 } else {
378 addItem(item);
379 }
Adam Cohend0445262011-07-04 23:53:22 -0700380 }
381
Adam Cohen3e8f8112011-07-02 18:03:00 -0700382 public void onDrop(DragObject d) {
Adam Cohenc0dcf592011-06-01 15:30:43 -0700383 ShortcutInfo item;
Adam Cohen3e8f8112011-07-02 18:03:00 -0700384 if (d.dragInfo instanceof ApplicationInfo) {
Adam Cohenc0dcf592011-06-01 15:30:43 -0700385 // Came from all apps -- make a copy
Adam Cohen3e8f8112011-07-02 18:03:00 -0700386 item = ((ApplicationInfo) d.dragInfo).makeShortcut();
Adam Cohenc0dcf592011-06-01 15:30:43 -0700387 } else {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700388 item = (ShortcutInfo) d.dragInfo;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700389 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700390 mFolder.notifyDrop();
Adam Cohenac8c8762011-07-13 11:15:27 -0700391 onDrop(item, d.dragView, null, 1.0f, mInfo.contents.size(), d.postAnimationRunnable);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700392 }
393
Adam Cohencb3382b2011-05-24 14:07:08 -0700394 public DropTarget getDropTargetDelegate(DragObject d) {
Patrick Dubroy440c3602010-07-13 17:50:32 -0700395 return null;
396 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700397
Adam Cohend0445262011-07-04 23:53:22 -0700398 private void computePreviewDrawingParams(int drawableSize, int totalSize) {
399 if (mIntrinsicIconSize != drawableSize || mTotalWidth != totalSize) {
400 mIntrinsicIconSize = drawableSize;
401 mTotalWidth = totalSize;
402
403 final int previewSize = FolderRingAnimator.sPreviewSize;
404 final int previewPadding = FolderRingAnimator.sPreviewPadding;
405
406 mAvailableSpaceInPreview = (previewSize - 2 * previewPadding);
407 // cos(45) = 0.707 + ~= 0.1) = 0.8f
408 int adjustedAvailableSpace = (int) ((mAvailableSpaceInPreview / 2) * (1 + 0.8f));
409
410 int unscaledHeight = (int) (mIntrinsicIconSize * (1 + PERSPECTIVE_SHIFT_FACTOR));
411 mBaselineIconScale = (1.0f * adjustedAvailableSpace / unscaledHeight);
412
413 mBaselineIconSize = (int) (mIntrinsicIconSize * mBaselineIconScale);
414 mMaxPerspectiveShift = mBaselineIconSize * PERSPECTIVE_SHIFT_FACTOR;
415
416 mPreviewOffsetX = (mTotalWidth - mAvailableSpaceInPreview) / 2;
417 mPreviewOffsetY = previewPadding;
418 }
419 }
420
421 private void computePreviewDrawingParams(Drawable d) {
422 computePreviewDrawingParams(d.getIntrinsicWidth(), getMeasuredWidth());
423 }
424
425 class PreviewItemDrawingParams {
426 PreviewItemDrawingParams(float transX, float transY, float scale, int overlayAlpha) {
427 this.transX = transX;
428 this.transY = transY;
429 this.scale = scale;
430 this.overlayAlpha = overlayAlpha;
431 }
432 float transX;
433 float transY;
434 float scale;
435 int overlayAlpha;
436 Drawable drawable;
437 }
438
Adam Cohenac8c8762011-07-13 11:15:27 -0700439 private float getLocalCenterForIndex(int index, int[] center) {
Adam Cohend0445262011-07-04 23:53:22 -0700440 mParams = computePreviewItemDrawingParams(Math.min(NUM_ITEMS_IN_PREVIEW, index), mParams);
441
442 mParams.transX += mPreviewOffsetX;
443 mParams.transY += mPreviewOffsetY;
Adam Cohenac8c8762011-07-13 11:15:27 -0700444 float offsetX = mParams.transX + (mParams.scale * mIntrinsicIconSize) / 2;
445 float offsetY = mParams.transY + (mParams.scale * mIntrinsicIconSize) / 2;
Adam Cohend0445262011-07-04 23:53:22 -0700446
Adam Cohenac8c8762011-07-13 11:15:27 -0700447 center[0] = (int) Math.round(offsetX);
448 center[1] = (int) Math.round(offsetY);
Adam Cohend0445262011-07-04 23:53:22 -0700449 return mParams.scale;
450 }
451
452 private PreviewItemDrawingParams computePreviewItemDrawingParams(int index,
453 PreviewItemDrawingParams params) {
454 index = NUM_ITEMS_IN_PREVIEW - index - 1;
455 float r = (index * 1.0f) / (NUM_ITEMS_IN_PREVIEW - 1);
456 float scale = (1 - PERSPECTIVE_SCALE_FACTOR * (1 - r));
457
458 float offset = (1 - r) * mMaxPerspectiveShift;
459 float scaledSize = scale * mBaselineIconSize;
460 float scaleOffsetCorrection = (1 - scale) * mBaselineIconSize;
461
462 // We want to imagine our coordinates from the bottom left, growing up and to the
463 // right. This is natural for the x-axis, but for the y-axis, we have to invert things.
464 float transY = mAvailableSpaceInPreview - (offset + scaledSize + scaleOffsetCorrection);
465 float transX = offset + scaleOffsetCorrection;
466 float totalScale = mBaselineIconScale * scale;
467 final int overlayAlpha = (int) (80 * (1 - r));
468
469 if (params == null) {
470 params = new PreviewItemDrawingParams(transX, transY, totalScale, overlayAlpha);
471 } else {
472 params.transX = transX;
473 params.transY = transY;
474 params.scale = totalScale;
475 params.overlayAlpha = overlayAlpha;
476 }
477 return params;
478 }
479
480 private void drawPreviewItem(Canvas canvas, PreviewItemDrawingParams params) {
481 canvas.save();
482 canvas.translate(params.transX + mPreviewOffsetX, params.transY + mPreviewOffsetY);
483 canvas.scale(params.scale, params.scale);
484 Drawable d = params.drawable;
485
486 if (d != null) {
487 d.setBounds(0, 0, mIntrinsicIconSize, mIntrinsicIconSize);
488 d.setFilterBitmap(true);
489 d.setColorFilter(Color.argb(params.overlayAlpha, 0, 0, 0), PorterDuff.Mode.SRC_ATOP);
490 d.draw(canvas);
491 d.clearColorFilter();
492 d.setFilterBitmap(false);
493 }
494 canvas.restore();
495 }
496
Adam Cohena9cf38f2011-05-02 15:36:58 -0700497 @Override
Adam Cohenc0dcf592011-06-01 15:30:43 -0700498 protected void dispatchDraw(Canvas canvas) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700499 super.dispatchDraw(canvas);
500
Adam Cohena9cf38f2011-05-02 15:36:58 -0700501 if (mFolder == null) return;
Adam Cohend0445262011-07-04 23:53:22 -0700502 if (mFolder.getItemCount() == 0 && !mAnimating) return;
Adam Cohena9cf38f2011-05-02 15:36:58 -0700503
Adam Cohen76078c42011-06-09 15:06:52 -0700504 ArrayList<View> items = mFolder.getItemsInReadingOrder(false);
Adam Cohend0445262011-07-04 23:53:22 -0700505 Drawable d;
506 TextView v;
Adam Cohenbadf71e2011-05-26 19:08:29 -0700507
Adam Cohend0445262011-07-04 23:53:22 -0700508 // Update our drawing parameters if necessary
509 if (mAnimating) {
510 computePreviewDrawingParams(mAnimParams.drawable);
511 } else {
512 v = (TextView) items.get(0);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700513 d = v.getCompoundDrawables()[1];
Adam Cohend0445262011-07-04 23:53:22 -0700514 computePreviewDrawingParams(d);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700515 }
Adam Cohend0445262011-07-04 23:53:22 -0700516
517 int nItemsInPreview = Math.min(items.size(), NUM_ITEMS_IN_PREVIEW);
518 if (!mAnimating) {
519 for (int i = nItemsInPreview - 1; i >= 0; i--) {
520 v = (TextView) items.get(i);
521 d = v.getCompoundDrawables()[1];
522
523 mParams = computePreviewItemDrawingParams(i, mParams);
524 mParams.drawable = d;
525 drawPreviewItem(canvas, mParams);
526 }
527 } else {
528 drawPreviewItem(canvas, mAnimParams);
529 }
530 }
531
532 private void animateFirstItem(final Drawable d, int duration) {
533 computePreviewDrawingParams(d);
534 final PreviewItemDrawingParams finalParams = computePreviewItemDrawingParams(0, null);
535
536 final float scale0 = 1.0f;
Adam Cohen1d4ee4e2011-08-12 15:51:59 -0700537 final float transX0 = (mAvailableSpaceInPreview - d.getIntrinsicWidth()) / 2;
538 final float transY0 = (mAvailableSpaceInPreview - d.getIntrinsicHeight()) / 2;
Adam Cohend0445262011-07-04 23:53:22 -0700539 mAnimParams.drawable = d;
540
541 ValueAnimator va = ValueAnimator.ofFloat(0f, 1.0f);
542 va.addUpdateListener(new AnimatorUpdateListener(){
543 public void onAnimationUpdate(ValueAnimator animation) {
544 float progress = (Float) animation.getAnimatedValue();
545
546 mAnimParams.transX = transX0 + progress * (finalParams.transX - transX0);
547 mAnimParams.transY = transY0 + progress * (finalParams.transY - transY0);
548 mAnimParams.scale = scale0 + progress * (finalParams.scale - scale0);
549 invalidate();
550 }
551 });
552 va.addListener(new AnimatorListenerAdapter() {
553 @Override
554 public void onAnimationStart(Animator animation) {
555 mAnimating = true;
556 }
557 @Override
558 public void onAnimationEnd(Animator animation) {
559 mAnimating = false;
560 }
561 });
562 va.setDuration(duration);
563 va.start();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700564 }
565
Adam Cohen099f60d2011-08-23 21:07:26 -0700566 public void setTextVisible(boolean visible) {
567 if (visible) {
568 mFolderName.setVisibility(VISIBLE);
569 } else {
570 mFolderName.setVisibility(INVISIBLE);
571 }
572 }
573
574 public boolean getTextVisible() {
575 return mFolderName.getVisibility() == VISIBLE;
576 }
577
Adam Cohen76078c42011-06-09 15:06:52 -0700578 public void onItemsChanged() {
579 invalidate();
580 requestLayout();
581 }
582
Adam Cohena9cf38f2011-05-02 15:36:58 -0700583 public void onAdd(ShortcutInfo item) {
584 invalidate();
585 requestLayout();
586 }
587
588 public void onRemove(ShortcutInfo item) {
589 invalidate();
590 requestLayout();
591 }
Adam Cohen76fc0852011-06-17 13:26:23 -0700592
593 public void onTitleChanged(CharSequence title) {
Adam Cohend63cfa92011-06-24 14:17:17 -0700594 mFolderName.setText(title.toString());
Winson Chung6a0f57d2011-06-29 20:10:49 -0700595 mPreviewBackground.setContentDescription(title.toString());
Adam Cohen76fc0852011-06-17 13:26:23 -0700596 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800597}