blob: 2a711f88b4fb67f76b8d82f7c995e2a6df6c64ea [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);
125
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126 icon.setTag(folderInfo);
127 icon.setOnClickListener(launcher);
128 icon.mInfo = folderInfo;
129 icon.mLauncher = launcher;
Adam Cohen3371da02011-10-25 21:38:29 -0700130 icon.setContentDescription(String.format(launcher.getString(R.string.folder_name_format),
131 folderInfo.title));
Adam Cohena9cf38f2011-05-02 15:36:58 -0700132 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 Cohen099f60d2011-08-23 21:07:26 -0700137
Adam Cohen099f60d2011-08-23 21:07:26 -0700138 icon.mFolderRingAnimator = new FolderRingAnimator(launcher, icon);
139 folderInfo.addListener(icon);
140
Adam Cohen19072da2011-05-31 14:30:45 -0700141 return icon;
142 }
143
Adam Cohen099f60d2011-08-23 21:07:26 -0700144 @Override
145 protected Parcelable onSaveInstanceState() {
146 sStaticValuesDirty = true;
147 return super.onSaveInstanceState();
148 }
149
Adam Cohen19072da2011-05-31 14:30:45 -0700150 public static class FolderRingAnimator {
Adam Cohen69ce2e52011-07-03 19:25:21 -0700151 public int mCellX;
152 public int mCellY;
153 private CellLayout mCellLayout;
Adam Cohen76fc0852011-06-17 13:26:23 -0700154 public float mOuterRingSize;
155 public float mInnerRingSize;
Adam Cohen19072da2011-05-31 14:30:45 -0700156 public FolderIcon mFolderIcon = null;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700157 public Drawable mOuterRingDrawable = null;
158 public Drawable mInnerRingDrawable = null;
159 public static Drawable sSharedOuterRingDrawable = null;
160 public static Drawable sSharedInnerRingDrawable = null;
Adam Cohen76fc0852011-06-17 13:26:23 -0700161 public static int sPreviewSize = -1;
162 public static int sPreviewPadding = -1;
163
Adam Cohenc0dcf592011-06-01 15:30:43 -0700164 private ValueAnimator mAcceptAnimator;
165 private ValueAnimator mNeutralAnimator;
Adam Cohen19072da2011-05-31 14:30:45 -0700166
167 public FolderRingAnimator(Launcher launcher, FolderIcon folderIcon) {
Adam Cohen19072da2011-05-31 14:30:45 -0700168 mFolderIcon = folderIcon;
Adam Cohen76fc0852011-06-17 13:26:23 -0700169 Resources res = launcher.getResources();
170 mOuterRingDrawable = res.getDrawable(R.drawable.portal_ring_outer_holo);
171 mInnerRingDrawable = res.getDrawable(R.drawable.portal_ring_inner_holo);
172
Adam Cohen099f60d2011-08-23 21:07:26 -0700173 // We need to reload the static values when configuration changes in case they are
174 // different in another configuration
175 if (sStaticValuesDirty) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700176 sPreviewSize = res.getDimensionPixelSize(R.dimen.folder_preview_size);
177 sPreviewPadding = res.getDimensionPixelSize(R.dimen.folder_preview_padding);
Adam Cohen76fc0852011-06-17 13:26:23 -0700178 sSharedOuterRingDrawable = res.getDrawable(R.drawable.portal_ring_outer_holo);
Adam Cohen76fc0852011-06-17 13:26:23 -0700179 sSharedInnerRingDrawable = res.getDrawable(R.drawable.portal_ring_inner_holo);
Adam Cohendf6af572011-10-19 14:38:16 -0700180 sSharedFolderLeaveBehind = res.getDrawable(R.drawable.portal_ring_rest);
Adam Cohen099f60d2011-08-23 21:07:26 -0700181 sStaticValuesDirty = false;
Adam Cohen19072da2011-05-31 14:30:45 -0700182 }
Adam Cohen073a46f2011-05-17 16:28:09 -0700183 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700184
Adam Cohen19072da2011-05-31 14:30:45 -0700185 public void animateToAcceptState() {
Adam Cohenc0dcf592011-06-01 15:30:43 -0700186 if (mNeutralAnimator != null) {
187 mNeutralAnimator.cancel();
188 }
189 mAcceptAnimator = ValueAnimator.ofFloat(0f, 1f);
190 mAcceptAnimator.setDuration(CONSUMPTION_ANIMATION_DURATION);
191 mAcceptAnimator.addUpdateListener(new AnimatorUpdateListener() {
Adam Cohen19072da2011-05-31 14:30:45 -0700192 public void onAnimationUpdate(ValueAnimator animation) {
193 final float percent = (Float) animation.getAnimatedValue();
Adam Cohen76fc0852011-06-17 13:26:23 -0700194 mOuterRingSize = (1 + percent * OUTER_RING_GROWTH_FACTOR) * sPreviewSize;
195 mInnerRingSize = (1 + percent * INNER_RING_GROWTH_FACTOR) * sPreviewSize;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700196 if (mCellLayout != null) {
197 mCellLayout.invalidate();
Adam Cohen19072da2011-05-31 14:30:45 -0700198 }
199 }
200 });
Adam Cohenc0dcf592011-06-01 15:30:43 -0700201 mAcceptAnimator.addListener(new AnimatorListenerAdapter() {
Adam Cohen19072da2011-05-31 14:30:45 -0700202 @Override
Adam Cohenc0dcf592011-06-01 15:30:43 -0700203 public void onAnimationStart(Animator animation) {
Adam Cohen19072da2011-05-31 14:30:45 -0700204 if (mFolderIcon != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700205 mFolderIcon.mPreviewBackground.setVisibility(INVISIBLE);
Adam Cohen19072da2011-05-31 14:30:45 -0700206 }
207 }
208 });
Adam Cohenc0dcf592011-06-01 15:30:43 -0700209 mAcceptAnimator.start();
Adam Cohen19072da2011-05-31 14:30:45 -0700210 }
211
212 public void animateToNaturalState() {
Adam Cohenc0dcf592011-06-01 15:30:43 -0700213 if (mAcceptAnimator != null) {
214 mAcceptAnimator.cancel();
215 }
216 mNeutralAnimator = ValueAnimator.ofFloat(0f, 1f);
217 mNeutralAnimator.setDuration(CONSUMPTION_ANIMATION_DURATION);
218 mNeutralAnimator.addUpdateListener(new AnimatorUpdateListener() {
Adam Cohen19072da2011-05-31 14:30:45 -0700219 public void onAnimationUpdate(ValueAnimator animation) {
220 final float percent = (Float) animation.getAnimatedValue();
Adam Cohen76fc0852011-06-17 13:26:23 -0700221 mOuterRingSize = (1 + (1 - percent) * OUTER_RING_GROWTH_FACTOR) * sPreviewSize;
222 mInnerRingSize = (1 + (1 - percent) * INNER_RING_GROWTH_FACTOR) * sPreviewSize;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700223 if (mCellLayout != null) {
224 mCellLayout.invalidate();
Adam Cohen19072da2011-05-31 14:30:45 -0700225 }
226 }
227 });
Adam Cohenc0dcf592011-06-01 15:30:43 -0700228 mNeutralAnimator.addListener(new AnimatorListenerAdapter() {
Adam Cohen19072da2011-05-31 14:30:45 -0700229 @Override
230 public void onAnimationEnd(Animator animation) {
Adam Cohen69ce2e52011-07-03 19:25:21 -0700231 if (mCellLayout != null) {
232 mCellLayout.hideFolderAccept(FolderRingAnimator.this);
233 }
Adam Cohen19072da2011-05-31 14:30:45 -0700234 if (mFolderIcon != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700235 mFolderIcon.mPreviewBackground.setVisibility(VISIBLE);
Adam Cohen19072da2011-05-31 14:30:45 -0700236 }
Adam Cohen19072da2011-05-31 14:30:45 -0700237 }
238 });
Adam Cohenc0dcf592011-06-01 15:30:43 -0700239 mNeutralAnimator.start();
Adam Cohen19072da2011-05-31 14:30:45 -0700240 }
241
Adam Cohenc0dcf592011-06-01 15:30:43 -0700242 // Location is expressed in window coordinates
Adam Cohen69ce2e52011-07-03 19:25:21 -0700243 public void getCell(int[] loc) {
244 loc[0] = mCellX;
245 loc[1] = mCellY;
246 }
247
248 // Location is expressed in window coordinates
249 public void setCell(int x, int y) {
250 mCellX = x;
251 mCellY = y;
252 }
253
254 public void setCellLayout(CellLayout layout) {
255 mCellLayout = layout;
Adam Cohen19072da2011-05-31 14:30:45 -0700256 }
257
Adam Cohen76fc0852011-06-17 13:26:23 -0700258 public float getOuterRingSize() {
259 return mOuterRingSize;
Adam Cohen19072da2011-05-31 14:30:45 -0700260 }
261
Adam Cohen76fc0852011-06-17 13:26:23 -0700262 public float getInnerRingSize() {
263 return mInnerRingSize;
Adam Cohen19072da2011-05-31 14:30:45 -0700264 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800265 }
266
Adam Cohen073a46f2011-05-17 16:28:09 -0700267 private boolean willAcceptItem(ItemInfo item) {
268 final int itemType = item.itemType;
269 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
270 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
Adam Cohenc51934b2011-07-26 21:07:43 -0700271 !mFolder.isFull() && item != mInfo && !mInfo.opened);
Adam Cohen073a46f2011-05-17 16:28:09 -0700272 }
273
Adam Cohenc0dcf592011-06-01 15:30:43 -0700274 public boolean acceptDrop(Object dragInfo) {
275 final ItemInfo item = (ItemInfo) dragInfo;
Adam Cohen073a46f2011-05-17 16:28:09 -0700276 return willAcceptItem(item);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800277 }
278
Adam Cohendf035382011-04-11 17:22:04 -0700279 public void addItem(ShortcutInfo item) {
280 mInfo.add(item);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700281 LauncherModel.addOrMoveItemInDatabase(mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
Adam Cohendf035382011-04-11 17:22:04 -0700282 }
283
Adam Cohenc0dcf592011-06-01 15:30:43 -0700284 public void onDragEnter(Object dragInfo) {
285 if (!willAcceptItem((ItemInfo) dragInfo)) return;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700286 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) getLayoutParams();
287 CellLayout layout = (CellLayout) getParent().getParent();
288 mFolderRingAnimator.setCell(lp.cellX, lp.cellY);
289 mFolderRingAnimator.setCellLayout(layout);
Adam Cohen19072da2011-05-31 14:30:45 -0700290 mFolderRingAnimator.animateToAcceptState();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700291 layout.showFolderAccept(mFolderRingAnimator);
Adam Cohen073a46f2011-05-17 16:28:09 -0700292 }
293
Adam Cohenc0dcf592011-06-01 15:30:43 -0700294 public void onDragOver(Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800295 }
296
Adam Cohend0445262011-07-04 23:53:22 -0700297 public void performCreateAnimation(final ShortcutInfo destInfo, final View destView,
Winson Chunga1cdab02012-02-13 13:03:52 -0800298 final ShortcutInfo srcInfo, final View srcView, Rect dstRect,
Adam Cohenac8c8762011-07-13 11:15:27 -0700299 float scaleRelativeToDragLayer, Runnable postAnimationRunnable) {
Adam Cohend0445262011-07-04 23:53:22 -0700300
301 Drawable animateDrawable = ((TextView) destView).getCompoundDrawables()[1];
302 computePreviewDrawingParams(animateDrawable.getIntrinsicWidth(), destView.getMeasuredWidth());
Adam Cohenac8c8762011-07-13 11:15:27 -0700303
Adam Cohend0445262011-07-04 23:53:22 -0700304 // This will animate the dragView (srcView) into the new folder
Winson Chunga1cdab02012-02-13 13:03:52 -0800305 onDrop(srcInfo, srcView, dstRect, scaleRelativeToDragLayer, 1, postAnimationRunnable);
Adam Cohend0445262011-07-04 23:53:22 -0700306
307 // This will animate the first item from it's position as an icon into its
308 // position as the first item in the preview
Adam Cohen8dfcba42011-07-07 16:38:18 -0700309 animateFirstItem(animateDrawable, INITIAL_ITEM_ANIMATION_DURATION);
Adam Cohend0445262011-07-04 23:53:22 -0700310
311 postDelayed(new Runnable() {
312 public void run() {
313 addItem(destInfo);
314 }
315 }, INITIAL_ITEM_ANIMATION_DURATION);
316 }
317
Adam Cohenc0dcf592011-06-01 15:30:43 -0700318 public void onDragExit(Object dragInfo) {
319 if (!willAcceptItem((ItemInfo) dragInfo)) return;
Adam Cohen19072da2011-05-31 14:30:45 -0700320 mFolderRingAnimator.animateToNaturalState();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800321 }
Patrick Dubroy440c3602010-07-13 17:50:32 -0700322
Winson Chunga1cdab02012-02-13 13:03:52 -0800323 private void onDrop(final ShortcutInfo item, View animateView, Rect finalRect,
324 float scaleRelativeToDragLayer, int index, Runnable postAnimationRunnable) {
Adam Cohend0445262011-07-04 23:53:22 -0700325 item.cellX = -1;
326 item.cellY = -1;
Adam Cohend0445262011-07-04 23:53:22 -0700327
Adam Cohen558baaf2011-08-15 15:22:57 -0700328 // Typically, the animateView corresponds to the DragView; however, if this is being done
329 // after a configuration activity (ie. for a Shortcut being dragged from AllApps) we
330 // will not have a view to animate
331 if (animateView != null) {
332 DragLayer dragLayer = mLauncher.getDragLayer();
333 Rect from = new Rect();
334 dragLayer.getViewRectRelativeToSelf(animateView, from);
335 Rect to = finalRect;
336 if (to == null) {
337 to = new Rect();
338 Workspace workspace = mLauncher.getWorkspace();
339 // Set cellLayout and this to it's final state to compute final animation locations
340 workspace.setFinalTransitionTransform((CellLayout) getParent().getParent());
341 float scaleX = getScaleX();
342 float scaleY = getScaleY();
343 setScaleX(1.0f);
344 setScaleY(1.0f);
345 scaleRelativeToDragLayer = dragLayer.getDescendantRectRelativeToSelf(this, to);
346 // Finished computing final animation locations, restore current state
347 setScaleX(scaleX);
348 setScaleY(scaleY);
349 workspace.resetTransitionTransform((CellLayout) getParent().getParent());
Adam Cohend0445262011-07-04 23:53:22 -0700350 }
Adam Cohen558baaf2011-08-15 15:22:57 -0700351
352 int[] center = new int[2];
353 float scale = getLocalCenterForIndex(index, center);
354 center[0] = (int) Math.round(scaleRelativeToDragLayer * center[0]);
355 center[1] = (int) Math.round(scaleRelativeToDragLayer * center[1]);
356
357 to.offset(center[0] - animateView.getMeasuredWidth() / 2,
358 center[1] - animateView.getMeasuredHeight() / 2);
359
360 float finalAlpha = index < NUM_ITEMS_IN_PREVIEW ? 0.5f : 0f;
361
362 dragLayer.animateView(animateView, from, to, finalAlpha,
363 scale * scaleRelativeToDragLayer, DROP_IN_ANIMATION_DURATION,
364 new DecelerateInterpolator(2), new AccelerateInterpolator(2),
Adam Cohen6441de02011-12-14 14:25:32 -0800365 postAnimationRunnable, false, null);
Adam Cohen558baaf2011-08-15 15:22:57 -0700366 postDelayed(new Runnable() {
367 public void run() {
368 addItem(item);
369 }
370 }, DROP_IN_ANIMATION_DURATION);
371 } else {
372 addItem(item);
373 }
Adam Cohend0445262011-07-04 23:53:22 -0700374 }
375
Adam Cohen3e8f8112011-07-02 18:03:00 -0700376 public void onDrop(DragObject d) {
Adam Cohenc0dcf592011-06-01 15:30:43 -0700377 ShortcutInfo item;
Adam Cohen3e8f8112011-07-02 18:03:00 -0700378 if (d.dragInfo instanceof ApplicationInfo) {
Adam Cohenc0dcf592011-06-01 15:30:43 -0700379 // Came from all apps -- make a copy
Adam Cohen3e8f8112011-07-02 18:03:00 -0700380 item = ((ApplicationInfo) d.dragInfo).makeShortcut();
Adam Cohenc0dcf592011-06-01 15:30:43 -0700381 } else {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700382 item = (ShortcutInfo) d.dragInfo;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700383 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700384 mFolder.notifyDrop();
Winson Chunga1cdab02012-02-13 13:03:52 -0800385 onDrop(item, d.dragView, null, 1.0f, mInfo.contents.size(), d.postAnimationRunnable);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700386 }
387
Adam Cohencb3382b2011-05-24 14:07:08 -0700388 public DropTarget getDropTargetDelegate(DragObject d) {
Patrick Dubroy440c3602010-07-13 17:50:32 -0700389 return null;
390 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700391
Adam Cohend0445262011-07-04 23:53:22 -0700392 private void computePreviewDrawingParams(int drawableSize, int totalSize) {
393 if (mIntrinsicIconSize != drawableSize || mTotalWidth != totalSize) {
394 mIntrinsicIconSize = drawableSize;
395 mTotalWidth = totalSize;
396
397 final int previewSize = FolderRingAnimator.sPreviewSize;
398 final int previewPadding = FolderRingAnimator.sPreviewPadding;
399
400 mAvailableSpaceInPreview = (previewSize - 2 * previewPadding);
401 // cos(45) = 0.707 + ~= 0.1) = 0.8f
402 int adjustedAvailableSpace = (int) ((mAvailableSpaceInPreview / 2) * (1 + 0.8f));
403
404 int unscaledHeight = (int) (mIntrinsicIconSize * (1 + PERSPECTIVE_SHIFT_FACTOR));
405 mBaselineIconScale = (1.0f * adjustedAvailableSpace / unscaledHeight);
406
407 mBaselineIconSize = (int) (mIntrinsicIconSize * mBaselineIconScale);
408 mMaxPerspectiveShift = mBaselineIconSize * PERSPECTIVE_SHIFT_FACTOR;
409
410 mPreviewOffsetX = (mTotalWidth - mAvailableSpaceInPreview) / 2;
411 mPreviewOffsetY = previewPadding;
412 }
413 }
414
415 private void computePreviewDrawingParams(Drawable d) {
416 computePreviewDrawingParams(d.getIntrinsicWidth(), getMeasuredWidth());
417 }
418
419 class PreviewItemDrawingParams {
420 PreviewItemDrawingParams(float transX, float transY, float scale, int overlayAlpha) {
421 this.transX = transX;
422 this.transY = transY;
423 this.scale = scale;
424 this.overlayAlpha = overlayAlpha;
425 }
426 float transX;
427 float transY;
428 float scale;
429 int overlayAlpha;
430 Drawable drawable;
431 }
432
Adam Cohenac8c8762011-07-13 11:15:27 -0700433 private float getLocalCenterForIndex(int index, int[] center) {
Adam Cohend0445262011-07-04 23:53:22 -0700434 mParams = computePreviewItemDrawingParams(Math.min(NUM_ITEMS_IN_PREVIEW, index), mParams);
435
436 mParams.transX += mPreviewOffsetX;
437 mParams.transY += mPreviewOffsetY;
Adam Cohenac8c8762011-07-13 11:15:27 -0700438 float offsetX = mParams.transX + (mParams.scale * mIntrinsicIconSize) / 2;
439 float offsetY = mParams.transY + (mParams.scale * mIntrinsicIconSize) / 2;
Adam Cohend0445262011-07-04 23:53:22 -0700440
Adam Cohenac8c8762011-07-13 11:15:27 -0700441 center[0] = (int) Math.round(offsetX);
442 center[1] = (int) Math.round(offsetY);
Adam Cohend0445262011-07-04 23:53:22 -0700443 return mParams.scale;
444 }
445
446 private PreviewItemDrawingParams computePreviewItemDrawingParams(int index,
447 PreviewItemDrawingParams params) {
448 index = NUM_ITEMS_IN_PREVIEW - index - 1;
449 float r = (index * 1.0f) / (NUM_ITEMS_IN_PREVIEW - 1);
450 float scale = (1 - PERSPECTIVE_SCALE_FACTOR * (1 - r));
451
452 float offset = (1 - r) * mMaxPerspectiveShift;
453 float scaledSize = scale * mBaselineIconSize;
454 float scaleOffsetCorrection = (1 - scale) * mBaselineIconSize;
455
456 // We want to imagine our coordinates from the bottom left, growing up and to the
457 // right. This is natural for the x-axis, but for the y-axis, we have to invert things.
458 float transY = mAvailableSpaceInPreview - (offset + scaledSize + scaleOffsetCorrection);
459 float transX = offset + scaleOffsetCorrection;
460 float totalScale = mBaselineIconScale * scale;
461 final int overlayAlpha = (int) (80 * (1 - r));
462
463 if (params == null) {
464 params = new PreviewItemDrawingParams(transX, transY, totalScale, overlayAlpha);
465 } else {
466 params.transX = transX;
467 params.transY = transY;
468 params.scale = totalScale;
469 params.overlayAlpha = overlayAlpha;
470 }
471 return params;
472 }
473
474 private void drawPreviewItem(Canvas canvas, PreviewItemDrawingParams params) {
475 canvas.save();
476 canvas.translate(params.transX + mPreviewOffsetX, params.transY + mPreviewOffsetY);
477 canvas.scale(params.scale, params.scale);
478 Drawable d = params.drawable;
479
480 if (d != null) {
481 d.setBounds(0, 0, mIntrinsicIconSize, mIntrinsicIconSize);
482 d.setFilterBitmap(true);
483 d.setColorFilter(Color.argb(params.overlayAlpha, 0, 0, 0), PorterDuff.Mode.SRC_ATOP);
484 d.draw(canvas);
485 d.clearColorFilter();
486 d.setFilterBitmap(false);
487 }
488 canvas.restore();
489 }
490
Adam Cohena9cf38f2011-05-02 15:36:58 -0700491 @Override
Adam Cohenc0dcf592011-06-01 15:30:43 -0700492 protected void dispatchDraw(Canvas canvas) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700493 super.dispatchDraw(canvas);
494
Adam Cohena9cf38f2011-05-02 15:36:58 -0700495 if (mFolder == null) return;
Adam Cohend0445262011-07-04 23:53:22 -0700496 if (mFolder.getItemCount() == 0 && !mAnimating) return;
Adam Cohena9cf38f2011-05-02 15:36:58 -0700497
Adam Cohen76078c42011-06-09 15:06:52 -0700498 ArrayList<View> items = mFolder.getItemsInReadingOrder(false);
Adam Cohend0445262011-07-04 23:53:22 -0700499 Drawable d;
500 TextView v;
Adam Cohenbadf71e2011-05-26 19:08:29 -0700501
Adam Cohend0445262011-07-04 23:53:22 -0700502 // Update our drawing parameters if necessary
503 if (mAnimating) {
504 computePreviewDrawingParams(mAnimParams.drawable);
505 } else {
506 v = (TextView) items.get(0);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700507 d = v.getCompoundDrawables()[1];
Adam Cohend0445262011-07-04 23:53:22 -0700508 computePreviewDrawingParams(d);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700509 }
Adam Cohend0445262011-07-04 23:53:22 -0700510
511 int nItemsInPreview = Math.min(items.size(), NUM_ITEMS_IN_PREVIEW);
512 if (!mAnimating) {
513 for (int i = nItemsInPreview - 1; i >= 0; i--) {
514 v = (TextView) items.get(i);
515 d = v.getCompoundDrawables()[1];
516
517 mParams = computePreviewItemDrawingParams(i, mParams);
518 mParams.drawable = d;
519 drawPreviewItem(canvas, mParams);
520 }
521 } else {
522 drawPreviewItem(canvas, mAnimParams);
523 }
524 }
525
526 private void animateFirstItem(final Drawable d, int duration) {
527 computePreviewDrawingParams(d);
528 final PreviewItemDrawingParams finalParams = computePreviewItemDrawingParams(0, null);
529
530 final float scale0 = 1.0f;
Adam Cohen1d4ee4e2011-08-12 15:51:59 -0700531 final float transX0 = (mAvailableSpaceInPreview - d.getIntrinsicWidth()) / 2;
532 final float transY0 = (mAvailableSpaceInPreview - d.getIntrinsicHeight()) / 2;
Adam Cohend0445262011-07-04 23:53:22 -0700533 mAnimParams.drawable = d;
534
535 ValueAnimator va = ValueAnimator.ofFloat(0f, 1.0f);
536 va.addUpdateListener(new AnimatorUpdateListener(){
537 public void onAnimationUpdate(ValueAnimator animation) {
538 float progress = (Float) animation.getAnimatedValue();
539
540 mAnimParams.transX = transX0 + progress * (finalParams.transX - transX0);
541 mAnimParams.transY = transY0 + progress * (finalParams.transY - transY0);
542 mAnimParams.scale = scale0 + progress * (finalParams.scale - scale0);
543 invalidate();
544 }
545 });
546 va.addListener(new AnimatorListenerAdapter() {
547 @Override
548 public void onAnimationStart(Animator animation) {
549 mAnimating = true;
550 }
551 @Override
552 public void onAnimationEnd(Animator animation) {
553 mAnimating = false;
554 }
555 });
556 va.setDuration(duration);
557 va.start();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700558 }
559
Adam Cohen099f60d2011-08-23 21:07:26 -0700560 public void setTextVisible(boolean visible) {
561 if (visible) {
562 mFolderName.setVisibility(VISIBLE);
563 } else {
564 mFolderName.setVisibility(INVISIBLE);
565 }
566 }
567
568 public boolean getTextVisible() {
569 return mFolderName.getVisibility() == VISIBLE;
570 }
571
Adam Cohen76078c42011-06-09 15:06:52 -0700572 public void onItemsChanged() {
573 invalidate();
574 requestLayout();
575 }
576
Adam Cohena9cf38f2011-05-02 15:36:58 -0700577 public void onAdd(ShortcutInfo item) {
578 invalidate();
579 requestLayout();
580 }
581
582 public void onRemove(ShortcutInfo item) {
583 invalidate();
584 requestLayout();
585 }
Adam Cohen76fc0852011-06-17 13:26:23 -0700586
587 public void onTitleChanged(CharSequence title) {
Adam Cohend63cfa92011-06-24 14:17:17 -0700588 mFolderName.setText(title.toString());
Adam Cohen3371da02011-10-25 21:38:29 -0700589 setContentDescription(String.format(mContext.getString(R.string.folder_name_format),
590 title));
Adam Cohen76fc0852011-06-17 13:26:23 -0700591 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800592}