blob: 546f4b53cbe72da23e26fcbaa1640dee84cb13c7 [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 Cohen3371da02011-10-25 21:38:29 -070035import android.view.accessibility.AccessibilityEvent;
Adam Cohen3e8f8112011-07-02 18:03:00 -070036import android.view.animation.AccelerateInterpolator;
37import android.view.animation.DecelerateInterpolator;
Adam Cohen76fc0852011-06-17 13:26:23 -070038import android.widget.ImageView;
39import android.widget.LinearLayout;
Adam Cohena9cf38f2011-05-02 15:36:58 -070040import android.widget.TextView;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041
Romain Guyedcce092010-03-04 13:03:17 -080042import com.android.launcher.R;
Adam Cohenc0dcf592011-06-01 15:30:43 -070043import com.android.launcher2.DropTarget.DragObject;
Adam Cohena9cf38f2011-05-02 15:36:58 -070044import com.android.launcher2.FolderInfo.FolderListener;
Romain Guyedcce092010-03-04 13:03:17 -080045
Adam Cohenc0dcf592011-06-01 15:30:43 -070046import java.util.ArrayList;
47
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048/**
49 * An icon that can appear on in the workspace representing an {@link UserFolder}.
50 */
Adam Cohen76fc0852011-06-17 13:26:23 -070051public class FolderIcon extends LinearLayout implements FolderListener {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080052 private Launcher mLauncher;
Adam Cohena9cf38f2011-05-02 15:36:58 -070053 Folder mFolder;
54 FolderInfo mInfo;
Adam Cohen099f60d2011-08-23 21:07:26 -070055 private static boolean sStaticValuesDirty = true;
Adam Cohena9cf38f2011-05-02 15:36:58 -070056
Adam Cohenbadf71e2011-05-26 19:08:29 -070057 // The number of icons to display in the
Adam Cohen76fc0852011-06-17 13:26:23 -070058 private static final int NUM_ITEMS_IN_PREVIEW = 3;
Adam Cohenf4b08912011-05-17 18:45:47 -070059 private static final int CONSUMPTION_ANIMATION_DURATION = 100;
Adam Cohend0445262011-07-04 23:53:22 -070060 private static final int DROP_IN_ANIMATION_DURATION = 400;
Adam Cohen8dfcba42011-07-07 16:38:18 -070061 private static final int INITIAL_ITEM_ANIMATION_DURATION = 350;
Adam Cohenbadf71e2011-05-26 19:08:29 -070062
63 // The degree to which the inner ring grows when accepting drop
Adam Cohen69ce2e52011-07-03 19:25:21 -070064 private static final float INNER_RING_GROWTH_FACTOR = 0.15f;
Adam Cohenbadf71e2011-05-26 19:08:29 -070065
Adam Cohenbadf71e2011-05-26 19:08:29 -070066 // The degree to which the outer ring is scaled in its natural state
Adam Cohen69ce2e52011-07-03 19:25:21 -070067 private static final float OUTER_RING_GROWTH_FACTOR = 0.3f;
Adam Cohenbadf71e2011-05-26 19:08:29 -070068
69 // The amount of vertical spread between items in the stack [0...1]
Adam Cohen76fc0852011-06-17 13:26:23 -070070 private static final float PERSPECTIVE_SHIFT_FACTOR = 0.24f;
Adam Cohenbadf71e2011-05-26 19:08:29 -070071
72 // The degree to which the item in the back of the stack is scaled [0...1]
73 // (0 means it's not scaled at all, 1 means it's scaled to nothing)
Adam Cohen76fc0852011-06-17 13:26:23 -070074 private static final float PERSPECTIVE_SCALE_FACTOR = 0.35f;
Adam Cohenbadf71e2011-05-26 19:08:29 -070075
Adam Cohenc51934b2011-07-26 21:07:43 -070076 public static Drawable sSharedFolderLeaveBehind = null;
77
Adam Cohen76fc0852011-06-17 13:26:23 -070078 private ImageView mPreviewBackground;
79 private BubbleTextView mFolderName;
Adam Cohen073a46f2011-05-17 16:28:09 -070080
Adam Cohen19072da2011-05-31 14:30:45 -070081 FolderRingAnimator mFolderRingAnimator = null;
Adam Cohen2801caf2011-05-13 20:57:39 -070082
Adam Cohend0445262011-07-04 23:53:22 -070083 // These variables are all associated with the drawing of the preview; they are stored
84 // as member variables for shared usage and to avoid computation on each frame
85 private int mIntrinsicIconSize;
86 private float mBaselineIconScale;
87 private int mBaselineIconSize;
88 private int mAvailableSpaceInPreview;
89 private int mTotalWidth = -1;
90 private int mPreviewOffsetX;
91 private int mPreviewOffsetY;
92 private float mMaxPerspectiveShift;
93 boolean mAnimating = false;
94 private PreviewItemDrawingParams mParams = new PreviewItemDrawingParams(0, 0, 0, 0);
95 private PreviewItemDrawingParams mAnimParams = new PreviewItemDrawingParams(0, 0, 0, 0);
96
The Android Open Source Project31dd5032009-03-03 19:32:27 -080097 public FolderIcon(Context context, AttributeSet attrs) {
98 super(context, attrs);
99 }
100
101 public FolderIcon(Context context) {
102 super(context);
103 }
104
Michael Jurka0280c3b2010-09-17 15:00:07 -0700105 public boolean isDropEnabled() {
Winson Chung7a1d1652011-03-18 15:56:01 -0700106 final ViewGroup cellLayoutChildren = (ViewGroup) getParent();
107 final ViewGroup cellLayout = (ViewGroup) cellLayoutChildren.getParent();
108 final Workspace workspace = (Workspace) cellLayout.getParent();
109 return !workspace.isSmall();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700110 }
111
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800112 static FolderIcon fromXml(int resId, Launcher launcher, ViewGroup group,
Adam Cohendf2cc412011-04-27 16:56:57 -0700113 FolderInfo folderInfo, IconCache iconCache) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800114
Adam Cohend0445262011-07-04 23:53:22 -0700115 if (INITIAL_ITEM_ANIMATION_DURATION >= DROP_IN_ANIMATION_DURATION) {
116 throw new IllegalStateException("DROP_IN_ANIMATION_DURATION must be greater than " +
117 "INITIAL_ITEM_ANIMATION_DURATION, as sequencing of adding first two items " +
118 "is dependent on this");
119 }
120
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121 FolderIcon icon = (FolderIcon) LayoutInflater.from(launcher).inflate(resId, group, false);
122
Adam Cohend2eca6b2011-07-26 22:49:13 -0700123 icon.mFolderName = (BubbleTextView) icon.findViewById(R.id.folder_icon_name);
Adam Cohen76fc0852011-06-17 13:26:23 -0700124 icon.mFolderName.setText(folderInfo.title);
125 icon.mPreviewBackground = (ImageView) icon.findViewById(R.id.preview_background);
126
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 Cohen3371da02011-10-25 21:38:29 -0700131 icon.setContentDescription(String.format(launcher.getString(R.string.folder_name_format),
132 folderInfo.title));
Adam Cohena9cf38f2011-05-02 15:36:58 -0700133 Folder folder = Folder.fromXml(launcher);
134 folder.setDragController(launcher.getDragController());
Adam Cohen2801caf2011-05-13 20:57:39 -0700135 folder.setFolderIcon(icon);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700136 folder.bind(folderInfo);
137 icon.mFolder = folder;
Adam Cohen099f60d2011-08-23 21:07:26 -0700138
Adam Cohen099f60d2011-08-23 21:07:26 -0700139 icon.mFolderRingAnimator = new FolderRingAnimator(launcher, icon);
140 folderInfo.addListener(icon);
141
Adam Cohen19072da2011-05-31 14:30:45 -0700142 return icon;
143 }
144
Adam Cohen099f60d2011-08-23 21:07:26 -0700145 @Override
146 protected Parcelable onSaveInstanceState() {
147 sStaticValuesDirty = true;
148 return super.onSaveInstanceState();
149 }
150
Adam Cohen19072da2011-05-31 14:30:45 -0700151 public static class FolderRingAnimator {
Adam Cohen69ce2e52011-07-03 19:25:21 -0700152 public int mCellX;
153 public int mCellY;
154 private CellLayout mCellLayout;
Adam Cohen76fc0852011-06-17 13:26:23 -0700155 public float mOuterRingSize;
156 public float mInnerRingSize;
Adam Cohen19072da2011-05-31 14:30:45 -0700157 public FolderIcon mFolderIcon = null;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700158 public Drawable mOuterRingDrawable = null;
159 public Drawable mInnerRingDrawable = null;
160 public static Drawable sSharedOuterRingDrawable = null;
161 public static Drawable sSharedInnerRingDrawable = null;
Adam Cohen76fc0852011-06-17 13:26:23 -0700162 public static int sPreviewSize = -1;
163 public static int sPreviewPadding = -1;
164
Adam Cohenc0dcf592011-06-01 15:30:43 -0700165 private ValueAnimator mAcceptAnimator;
166 private ValueAnimator mNeutralAnimator;
Adam Cohen19072da2011-05-31 14:30:45 -0700167
168 public FolderRingAnimator(Launcher launcher, FolderIcon folderIcon) {
Adam Cohen19072da2011-05-31 14:30:45 -0700169 mFolderIcon = folderIcon;
Adam Cohen76fc0852011-06-17 13:26:23 -0700170 Resources res = launcher.getResources();
171 mOuterRingDrawable = res.getDrawable(R.drawable.portal_ring_outer_holo);
172 mInnerRingDrawable = res.getDrawable(R.drawable.portal_ring_inner_holo);
173
Adam Cohen099f60d2011-08-23 21:07:26 -0700174 // We need to reload the static values when configuration changes in case they are
175 // different in another configuration
176 if (sStaticValuesDirty) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700177 sPreviewSize = res.getDimensionPixelSize(R.dimen.folder_preview_size);
178 sPreviewPadding = res.getDimensionPixelSize(R.dimen.folder_preview_padding);
Adam Cohen76fc0852011-06-17 13:26:23 -0700179 sSharedOuterRingDrawable = res.getDrawable(R.drawable.portal_ring_outer_holo);
Adam Cohen76fc0852011-06-17 13:26:23 -0700180 sSharedInnerRingDrawable = res.getDrawable(R.drawable.portal_ring_inner_holo);
Adam Cohendf6af572011-10-19 14:38:16 -0700181 sSharedFolderLeaveBehind = res.getDrawable(R.drawable.portal_ring_rest);
Adam Cohen099f60d2011-08-23 21:07:26 -0700182 sStaticValuesDirty = false;
Adam Cohen19072da2011-05-31 14:30:45 -0700183 }
Adam Cohen073a46f2011-05-17 16:28:09 -0700184 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700185
Adam Cohen19072da2011-05-31 14:30:45 -0700186 public void animateToAcceptState() {
Adam Cohenc0dcf592011-06-01 15:30:43 -0700187 if (mNeutralAnimator != null) {
188 mNeutralAnimator.cancel();
189 }
190 mAcceptAnimator = ValueAnimator.ofFloat(0f, 1f);
191 mAcceptAnimator.setDuration(CONSUMPTION_ANIMATION_DURATION);
192 mAcceptAnimator.addUpdateListener(new AnimatorUpdateListener() {
Adam Cohen19072da2011-05-31 14:30:45 -0700193 public void onAnimationUpdate(ValueAnimator animation) {
194 final float percent = (Float) animation.getAnimatedValue();
Adam Cohen76fc0852011-06-17 13:26:23 -0700195 mOuterRingSize = (1 + percent * OUTER_RING_GROWTH_FACTOR) * sPreviewSize;
196 mInnerRingSize = (1 + percent * INNER_RING_GROWTH_FACTOR) * sPreviewSize;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700197 if (mCellLayout != null) {
198 mCellLayout.invalidate();
Adam Cohen19072da2011-05-31 14:30:45 -0700199 }
200 }
201 });
Adam Cohenc0dcf592011-06-01 15:30:43 -0700202 mAcceptAnimator.addListener(new AnimatorListenerAdapter() {
Adam Cohen19072da2011-05-31 14:30:45 -0700203 @Override
Adam Cohenc0dcf592011-06-01 15:30:43 -0700204 public void onAnimationStart(Animator animation) {
Adam Cohen19072da2011-05-31 14:30:45 -0700205 if (mFolderIcon != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700206 mFolderIcon.mPreviewBackground.setVisibility(INVISIBLE);
Adam Cohen19072da2011-05-31 14:30:45 -0700207 }
208 }
209 });
Adam Cohenc0dcf592011-06-01 15:30:43 -0700210 mAcceptAnimator.start();
Adam Cohen19072da2011-05-31 14:30:45 -0700211 }
212
213 public void animateToNaturalState() {
Adam Cohenc0dcf592011-06-01 15:30:43 -0700214 if (mAcceptAnimator != null) {
215 mAcceptAnimator.cancel();
216 }
217 mNeutralAnimator = ValueAnimator.ofFloat(0f, 1f);
218 mNeutralAnimator.setDuration(CONSUMPTION_ANIMATION_DURATION);
219 mNeutralAnimator.addUpdateListener(new AnimatorUpdateListener() {
Adam Cohen19072da2011-05-31 14:30:45 -0700220 public void onAnimationUpdate(ValueAnimator animation) {
221 final float percent = (Float) animation.getAnimatedValue();
Adam Cohen76fc0852011-06-17 13:26:23 -0700222 mOuterRingSize = (1 + (1 - percent) * OUTER_RING_GROWTH_FACTOR) * sPreviewSize;
223 mInnerRingSize = (1 + (1 - percent) * INNER_RING_GROWTH_FACTOR) * sPreviewSize;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700224 if (mCellLayout != null) {
225 mCellLayout.invalidate();
Adam Cohen19072da2011-05-31 14:30:45 -0700226 }
227 }
228 });
Adam Cohenc0dcf592011-06-01 15:30:43 -0700229 mNeutralAnimator.addListener(new AnimatorListenerAdapter() {
Adam Cohen19072da2011-05-31 14:30:45 -0700230 @Override
231 public void onAnimationEnd(Animator animation) {
Adam Cohen69ce2e52011-07-03 19:25:21 -0700232 if (mCellLayout != null) {
233 mCellLayout.hideFolderAccept(FolderRingAnimator.this);
234 }
Adam Cohen19072da2011-05-31 14:30:45 -0700235 if (mFolderIcon != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700236 mFolderIcon.mPreviewBackground.setVisibility(VISIBLE);
Adam Cohen19072da2011-05-31 14:30:45 -0700237 }
Adam Cohen19072da2011-05-31 14:30:45 -0700238 }
239 });
Adam Cohenc0dcf592011-06-01 15:30:43 -0700240 mNeutralAnimator.start();
Adam Cohen19072da2011-05-31 14:30:45 -0700241 }
242
Adam Cohenc0dcf592011-06-01 15:30:43 -0700243 // Location is expressed in window coordinates
Adam Cohen69ce2e52011-07-03 19:25:21 -0700244 public void getCell(int[] loc) {
245 loc[0] = mCellX;
246 loc[1] = mCellY;
247 }
248
249 // Location is expressed in window coordinates
250 public void setCell(int x, int y) {
251 mCellX = x;
252 mCellY = y;
253 }
254
255 public void setCellLayout(CellLayout layout) {
256 mCellLayout = layout;
Adam Cohen19072da2011-05-31 14:30:45 -0700257 }
258
Adam Cohen76fc0852011-06-17 13:26:23 -0700259 public float getOuterRingSize() {
260 return mOuterRingSize;
Adam Cohen19072da2011-05-31 14:30:45 -0700261 }
262
Adam Cohen76fc0852011-06-17 13:26:23 -0700263 public float getInnerRingSize() {
264 return mInnerRingSize;
Adam Cohen19072da2011-05-31 14:30:45 -0700265 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800266 }
267
Adam Cohen073a46f2011-05-17 16:28:09 -0700268 private boolean willAcceptItem(ItemInfo item) {
269 final int itemType = item.itemType;
270 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
271 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
Adam Cohenc51934b2011-07-26 21:07:43 -0700272 !mFolder.isFull() && item != mInfo && !mInfo.opened);
Adam Cohen073a46f2011-05-17 16:28:09 -0700273 }
274
Adam Cohenc0dcf592011-06-01 15:30:43 -0700275 public boolean acceptDrop(Object dragInfo) {
276 final ItemInfo item = (ItemInfo) dragInfo;
Adam Cohen073a46f2011-05-17 16:28:09 -0700277 return willAcceptItem(item);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800278 }
279
Adam Cohendf035382011-04-11 17:22:04 -0700280 public void addItem(ShortcutInfo item) {
281 mInfo.add(item);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700282 LauncherModel.addOrMoveItemInDatabase(mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
Adam Cohendf035382011-04-11 17:22:04 -0700283 }
284
Adam Cohenc0dcf592011-06-01 15:30:43 -0700285 public void onDragEnter(Object dragInfo) {
286 if (!willAcceptItem((ItemInfo) dragInfo)) return;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700287 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) getLayoutParams();
288 CellLayout layout = (CellLayout) getParent().getParent();
289 mFolderRingAnimator.setCell(lp.cellX, lp.cellY);
290 mFolderRingAnimator.setCellLayout(layout);
Adam Cohen19072da2011-05-31 14:30:45 -0700291 mFolderRingAnimator.animateToAcceptState();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700292 layout.showFolderAccept(mFolderRingAnimator);
Adam Cohen073a46f2011-05-17 16:28:09 -0700293 }
294
Adam Cohenc0dcf592011-06-01 15:30:43 -0700295 public void onDragOver(Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800296 }
297
Adam Cohend0445262011-07-04 23:53:22 -0700298 public void performCreateAnimation(final ShortcutInfo destInfo, final View destView,
Winson Chung557d6ed2011-07-08 15:34:52 -0700299 final ShortcutInfo srcInfo, final View srcView, Rect dstRect,
Adam Cohenac8c8762011-07-13 11:15:27 -0700300 float scaleRelativeToDragLayer, Runnable postAnimationRunnable) {
Adam Cohend0445262011-07-04 23:53:22 -0700301
302 Drawable animateDrawable = ((TextView) destView).getCompoundDrawables()[1];
303 computePreviewDrawingParams(animateDrawable.getIntrinsicWidth(), destView.getMeasuredWidth());
Adam Cohenac8c8762011-07-13 11:15:27 -0700304
Adam Cohend0445262011-07-04 23:53:22 -0700305 // This will animate the dragView (srcView) into the new folder
Adam Cohenac8c8762011-07-13 11:15:27 -0700306 onDrop(srcInfo, srcView, dstRect, scaleRelativeToDragLayer, 1, postAnimationRunnable);
Adam Cohend0445262011-07-04 23:53:22 -0700307
308 // This will animate the first item from it's position as an icon into its
309 // position as the first item in the preview
Adam Cohen8dfcba42011-07-07 16:38:18 -0700310 animateFirstItem(animateDrawable, INITIAL_ITEM_ANIMATION_DURATION);
Adam Cohend0445262011-07-04 23:53:22 -0700311
312 postDelayed(new Runnable() {
313 public void run() {
314 addItem(destInfo);
315 }
316 }, INITIAL_ITEM_ANIMATION_DURATION);
317 }
318
Adam Cohenc0dcf592011-06-01 15:30:43 -0700319 public void onDragExit(Object dragInfo) {
320 if (!willAcceptItem((ItemInfo) dragInfo)) return;
Adam Cohen19072da2011-05-31 14:30:45 -0700321 mFolderRingAnimator.animateToNaturalState();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800322 }
Patrick Dubroy440c3602010-07-13 17:50:32 -0700323
Adam Cohenac8c8762011-07-13 11:15:27 -0700324 private void onDrop(final ShortcutInfo item, View animateView, Rect finalRect,
325 float scaleRelativeToDragLayer, int index, Runnable postAnimationRunnable) {
Adam Cohend0445262011-07-04 23:53:22 -0700326 item.cellX = -1;
327 item.cellY = -1;
Adam Cohend0445262011-07-04 23:53:22 -0700328
Adam Cohen558baaf2011-08-15 15:22:57 -0700329 // Typically, the animateView corresponds to the DragView; however, if this is being done
330 // after a configuration activity (ie. for a Shortcut being dragged from AllApps) we
331 // will not have a view to animate
332 if (animateView != null) {
333 DragLayer dragLayer = mLauncher.getDragLayer();
334 Rect from = new Rect();
335 dragLayer.getViewRectRelativeToSelf(animateView, from);
336 Rect to = finalRect;
337 if (to == null) {
338 to = new Rect();
339 Workspace workspace = mLauncher.getWorkspace();
340 // Set cellLayout and this to it's final state to compute final animation locations
341 workspace.setFinalTransitionTransform((CellLayout) getParent().getParent());
342 float scaleX = getScaleX();
343 float scaleY = getScaleY();
344 setScaleX(1.0f);
345 setScaleY(1.0f);
346 scaleRelativeToDragLayer = dragLayer.getDescendantRectRelativeToSelf(this, to);
347 // Finished computing final animation locations, restore current state
348 setScaleX(scaleX);
349 setScaleY(scaleY);
350 workspace.resetTransitionTransform((CellLayout) getParent().getParent());
Adam Cohend0445262011-07-04 23:53:22 -0700351 }
Adam Cohen558baaf2011-08-15 15:22:57 -0700352
353 int[] center = new int[2];
354 float scale = getLocalCenterForIndex(index, center);
355 center[0] = (int) Math.round(scaleRelativeToDragLayer * center[0]);
356 center[1] = (int) Math.round(scaleRelativeToDragLayer * center[1]);
357
358 to.offset(center[0] - animateView.getMeasuredWidth() / 2,
359 center[1] - animateView.getMeasuredHeight() / 2);
360
361 float finalAlpha = index < NUM_ITEMS_IN_PREVIEW ? 0.5f : 0f;
362
363 dragLayer.animateView(animateView, from, to, finalAlpha,
364 scale * scaleRelativeToDragLayer, DROP_IN_ANIMATION_DURATION,
365 new DecelerateInterpolator(2), new AccelerateInterpolator(2),
366 postAnimationRunnable, false);
367 postDelayed(new Runnable() {
368 public void run() {
369 addItem(item);
370 }
371 }, DROP_IN_ANIMATION_DURATION);
372 } else {
373 addItem(item);
374 }
Adam Cohend0445262011-07-04 23:53:22 -0700375 }
376
Adam Cohen3e8f8112011-07-02 18:03:00 -0700377 public void onDrop(DragObject d) {
Adam Cohenc0dcf592011-06-01 15:30:43 -0700378 ShortcutInfo item;
Adam Cohen3e8f8112011-07-02 18:03:00 -0700379 if (d.dragInfo instanceof ApplicationInfo) {
Adam Cohenc0dcf592011-06-01 15:30:43 -0700380 // Came from all apps -- make a copy
Adam Cohen3e8f8112011-07-02 18:03:00 -0700381 item = ((ApplicationInfo) d.dragInfo).makeShortcut();
Adam Cohenc0dcf592011-06-01 15:30:43 -0700382 } else {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700383 item = (ShortcutInfo) d.dragInfo;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700384 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700385 mFolder.notifyDrop();
Adam Cohenac8c8762011-07-13 11:15:27 -0700386 onDrop(item, d.dragView, null, 1.0f, mInfo.contents.size(), d.postAnimationRunnable);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700387 }
388
Adam Cohencb3382b2011-05-24 14:07:08 -0700389 public DropTarget getDropTargetDelegate(DragObject d) {
Patrick Dubroy440c3602010-07-13 17:50:32 -0700390 return null;
391 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700392
Adam Cohend0445262011-07-04 23:53:22 -0700393 private void computePreviewDrawingParams(int drawableSize, int totalSize) {
394 if (mIntrinsicIconSize != drawableSize || mTotalWidth != totalSize) {
395 mIntrinsicIconSize = drawableSize;
396 mTotalWidth = totalSize;
397
398 final int previewSize = FolderRingAnimator.sPreviewSize;
399 final int previewPadding = FolderRingAnimator.sPreviewPadding;
400
401 mAvailableSpaceInPreview = (previewSize - 2 * previewPadding);
402 // cos(45) = 0.707 + ~= 0.1) = 0.8f
403 int adjustedAvailableSpace = (int) ((mAvailableSpaceInPreview / 2) * (1 + 0.8f));
404
405 int unscaledHeight = (int) (mIntrinsicIconSize * (1 + PERSPECTIVE_SHIFT_FACTOR));
406 mBaselineIconScale = (1.0f * adjustedAvailableSpace / unscaledHeight);
407
408 mBaselineIconSize = (int) (mIntrinsicIconSize * mBaselineIconScale);
409 mMaxPerspectiveShift = mBaselineIconSize * PERSPECTIVE_SHIFT_FACTOR;
410
411 mPreviewOffsetX = (mTotalWidth - mAvailableSpaceInPreview) / 2;
412 mPreviewOffsetY = previewPadding;
413 }
414 }
415
416 private void computePreviewDrawingParams(Drawable d) {
417 computePreviewDrawingParams(d.getIntrinsicWidth(), getMeasuredWidth());
418 }
419
420 class PreviewItemDrawingParams {
421 PreviewItemDrawingParams(float transX, float transY, float scale, int overlayAlpha) {
422 this.transX = transX;
423 this.transY = transY;
424 this.scale = scale;
425 this.overlayAlpha = overlayAlpha;
426 }
427 float transX;
428 float transY;
429 float scale;
430 int overlayAlpha;
431 Drawable drawable;
432 }
433
Adam Cohenac8c8762011-07-13 11:15:27 -0700434 private float getLocalCenterForIndex(int index, int[] center) {
Adam Cohend0445262011-07-04 23:53:22 -0700435 mParams = computePreviewItemDrawingParams(Math.min(NUM_ITEMS_IN_PREVIEW, index), mParams);
436
437 mParams.transX += mPreviewOffsetX;
438 mParams.transY += mPreviewOffsetY;
Adam Cohenac8c8762011-07-13 11:15:27 -0700439 float offsetX = mParams.transX + (mParams.scale * mIntrinsicIconSize) / 2;
440 float offsetY = mParams.transY + (mParams.scale * mIntrinsicIconSize) / 2;
Adam Cohend0445262011-07-04 23:53:22 -0700441
Adam Cohenac8c8762011-07-13 11:15:27 -0700442 center[0] = (int) Math.round(offsetX);
443 center[1] = (int) Math.round(offsetY);
Adam Cohend0445262011-07-04 23:53:22 -0700444 return mParams.scale;
445 }
446
447 private PreviewItemDrawingParams computePreviewItemDrawingParams(int index,
448 PreviewItemDrawingParams params) {
449 index = NUM_ITEMS_IN_PREVIEW - index - 1;
450 float r = (index * 1.0f) / (NUM_ITEMS_IN_PREVIEW - 1);
451 float scale = (1 - PERSPECTIVE_SCALE_FACTOR * (1 - r));
452
453 float offset = (1 - r) * mMaxPerspectiveShift;
454 float scaledSize = scale * mBaselineIconSize;
455 float scaleOffsetCorrection = (1 - scale) * mBaselineIconSize;
456
457 // We want to imagine our coordinates from the bottom left, growing up and to the
458 // right. This is natural for the x-axis, but for the y-axis, we have to invert things.
459 float transY = mAvailableSpaceInPreview - (offset + scaledSize + scaleOffsetCorrection);
460 float transX = offset + scaleOffsetCorrection;
461 float totalScale = mBaselineIconScale * scale;
462 final int overlayAlpha = (int) (80 * (1 - r));
463
464 if (params == null) {
465 params = new PreviewItemDrawingParams(transX, transY, totalScale, overlayAlpha);
466 } else {
467 params.transX = transX;
468 params.transY = transY;
469 params.scale = totalScale;
470 params.overlayAlpha = overlayAlpha;
471 }
472 return params;
473 }
474
475 private void drawPreviewItem(Canvas canvas, PreviewItemDrawingParams params) {
476 canvas.save();
477 canvas.translate(params.transX + mPreviewOffsetX, params.transY + mPreviewOffsetY);
478 canvas.scale(params.scale, params.scale);
479 Drawable d = params.drawable;
480
481 if (d != null) {
482 d.setBounds(0, 0, mIntrinsicIconSize, mIntrinsicIconSize);
483 d.setFilterBitmap(true);
484 d.setColorFilter(Color.argb(params.overlayAlpha, 0, 0, 0), PorterDuff.Mode.SRC_ATOP);
485 d.draw(canvas);
486 d.clearColorFilter();
487 d.setFilterBitmap(false);
488 }
489 canvas.restore();
490 }
491
Adam Cohena9cf38f2011-05-02 15:36:58 -0700492 @Override
Adam Cohenc0dcf592011-06-01 15:30:43 -0700493 protected void dispatchDraw(Canvas canvas) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700494 super.dispatchDraw(canvas);
495
Adam Cohena9cf38f2011-05-02 15:36:58 -0700496 if (mFolder == null) return;
Adam Cohend0445262011-07-04 23:53:22 -0700497 if (mFolder.getItemCount() == 0 && !mAnimating) return;
Adam Cohena9cf38f2011-05-02 15:36:58 -0700498
Adam Cohen76078c42011-06-09 15:06:52 -0700499 ArrayList<View> items = mFolder.getItemsInReadingOrder(false);
Adam Cohend0445262011-07-04 23:53:22 -0700500 Drawable d;
501 TextView v;
Adam Cohenbadf71e2011-05-26 19:08:29 -0700502
Adam Cohend0445262011-07-04 23:53:22 -0700503 // Update our drawing parameters if necessary
504 if (mAnimating) {
505 computePreviewDrawingParams(mAnimParams.drawable);
506 } else {
507 v = (TextView) items.get(0);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700508 d = v.getCompoundDrawables()[1];
Adam Cohend0445262011-07-04 23:53:22 -0700509 computePreviewDrawingParams(d);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700510 }
Adam Cohend0445262011-07-04 23:53:22 -0700511
512 int nItemsInPreview = Math.min(items.size(), NUM_ITEMS_IN_PREVIEW);
513 if (!mAnimating) {
514 for (int i = nItemsInPreview - 1; i >= 0; i--) {
515 v = (TextView) items.get(i);
516 d = v.getCompoundDrawables()[1];
517
518 mParams = computePreviewItemDrawingParams(i, mParams);
519 mParams.drawable = d;
520 drawPreviewItem(canvas, mParams);
521 }
522 } else {
523 drawPreviewItem(canvas, mAnimParams);
524 }
525 }
526
527 private void animateFirstItem(final Drawable d, int duration) {
528 computePreviewDrawingParams(d);
529 final PreviewItemDrawingParams finalParams = computePreviewItemDrawingParams(0, null);
530
531 final float scale0 = 1.0f;
Adam Cohen1d4ee4e2011-08-12 15:51:59 -0700532 final float transX0 = (mAvailableSpaceInPreview - d.getIntrinsicWidth()) / 2;
533 final float transY0 = (mAvailableSpaceInPreview - d.getIntrinsicHeight()) / 2;
Adam Cohend0445262011-07-04 23:53:22 -0700534 mAnimParams.drawable = d;
535
536 ValueAnimator va = ValueAnimator.ofFloat(0f, 1.0f);
537 va.addUpdateListener(new AnimatorUpdateListener(){
538 public void onAnimationUpdate(ValueAnimator animation) {
539 float progress = (Float) animation.getAnimatedValue();
540
541 mAnimParams.transX = transX0 + progress * (finalParams.transX - transX0);
542 mAnimParams.transY = transY0 + progress * (finalParams.transY - transY0);
543 mAnimParams.scale = scale0 + progress * (finalParams.scale - scale0);
544 invalidate();
545 }
546 });
547 va.addListener(new AnimatorListenerAdapter() {
548 @Override
549 public void onAnimationStart(Animator animation) {
550 mAnimating = true;
551 }
552 @Override
553 public void onAnimationEnd(Animator animation) {
554 mAnimating = false;
555 }
556 });
557 va.setDuration(duration);
558 va.start();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700559 }
560
Adam Cohen099f60d2011-08-23 21:07:26 -0700561 public void setTextVisible(boolean visible) {
562 if (visible) {
563 mFolderName.setVisibility(VISIBLE);
564 } else {
565 mFolderName.setVisibility(INVISIBLE);
566 }
567 }
568
569 public boolean getTextVisible() {
570 return mFolderName.getVisibility() == VISIBLE;
571 }
572
Adam Cohen76078c42011-06-09 15:06:52 -0700573 public void onItemsChanged() {
574 invalidate();
575 requestLayout();
576 }
577
Adam Cohena9cf38f2011-05-02 15:36:58 -0700578 public void onAdd(ShortcutInfo item) {
579 invalidate();
580 requestLayout();
581 }
582
583 public void onRemove(ShortcutInfo item) {
584 invalidate();
585 requestLayout();
586 }
Adam Cohen76fc0852011-06-17 13:26:23 -0700587
588 public void onTitleChanged(CharSequence title) {
Adam Cohend63cfa92011-06-24 14:17:17 -0700589 mFolderName.setText(title.toString());
Adam Cohen3371da02011-10-25 21:38:29 -0700590 setContentDescription(String.format(mContext.getString(R.string.folder_name_format),
591 title));
Adam Cohen76fc0852011-06-17 13:26:23 -0700592 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800593}