blob: 07ae5b8e2e4b91b20b80fc5e875642810cd4bdac [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 Cohenac8c8762011-07-13 11:15:27 -070027import android.graphics.Paint;
Adam Cohenbadf71e2011-05-26 19:08:29 -070028import android.graphics.PorterDuff;
Adam Cohen3e8f8112011-07-02 18:03:00 -070029import android.graphics.Rect;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.graphics.drawable.Drawable;
31import 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;
54
Adam Cohenbadf71e2011-05-26 19:08:29 -070055 // The number of icons to display in the
Adam Cohen76fc0852011-06-17 13:26:23 -070056 private static final int NUM_ITEMS_IN_PREVIEW = 3;
Adam Cohenf4b08912011-05-17 18:45:47 -070057 private static final int CONSUMPTION_ANIMATION_DURATION = 100;
Adam Cohend0445262011-07-04 23:53:22 -070058 private static final int DROP_IN_ANIMATION_DURATION = 400;
Adam Cohen8dfcba42011-07-07 16:38:18 -070059 private static final int INITIAL_ITEM_ANIMATION_DURATION = 350;
Adam Cohenbadf71e2011-05-26 19:08:29 -070060
61 // The degree to which the inner ring grows when accepting drop
Adam Cohen69ce2e52011-07-03 19:25:21 -070062 private static final float INNER_RING_GROWTH_FACTOR = 0.15f;
Adam Cohenbadf71e2011-05-26 19:08:29 -070063
Adam Cohenbadf71e2011-05-26 19:08:29 -070064 // The degree to which the outer ring is scaled in its natural state
Adam Cohen69ce2e52011-07-03 19:25:21 -070065 private static final float OUTER_RING_GROWTH_FACTOR = 0.3f;
Adam Cohenbadf71e2011-05-26 19:08:29 -070066
67 // The amount of vertical spread between items in the stack [0...1]
Adam Cohen76fc0852011-06-17 13:26:23 -070068 private static final float PERSPECTIVE_SHIFT_FACTOR = 0.24f;
Adam Cohenbadf71e2011-05-26 19:08:29 -070069
70 // The degree to which the item in the back of the stack is scaled [0...1]
71 // (0 means it's not scaled at all, 1 means it's scaled to nothing)
Adam Cohen76fc0852011-06-17 13:26:23 -070072 private static final float PERSPECTIVE_SCALE_FACTOR = 0.35f;
Adam Cohenbadf71e2011-05-26 19:08:29 -070073
Adam Cohen76fc0852011-06-17 13:26:23 -070074 private ImageView mPreviewBackground;
75 private BubbleTextView mFolderName;
Adam Cohen073a46f2011-05-17 16:28:09 -070076
Adam Cohen19072da2011-05-31 14:30:45 -070077 FolderRingAnimator mFolderRingAnimator = null;
Adam Cohen2801caf2011-05-13 20:57:39 -070078
Adam Cohend0445262011-07-04 23:53:22 -070079 // These variables are all associated with the drawing of the preview; they are stored
80 // as member variables for shared usage and to avoid computation on each frame
81 private int mIntrinsicIconSize;
82 private float mBaselineIconScale;
83 private int mBaselineIconSize;
84 private int mAvailableSpaceInPreview;
85 private int mTotalWidth = -1;
86 private int mPreviewOffsetX;
87 private int mPreviewOffsetY;
88 private float mMaxPerspectiveShift;
89 boolean mAnimating = false;
90 private PreviewItemDrawingParams mParams = new PreviewItemDrawingParams(0, 0, 0, 0);
91 private PreviewItemDrawingParams mAnimParams = new PreviewItemDrawingParams(0, 0, 0, 0);
92
The Android Open Source Project31dd5032009-03-03 19:32:27 -080093 public FolderIcon(Context context, AttributeSet attrs) {
94 super(context, attrs);
95 }
96
97 public FolderIcon(Context context) {
98 super(context);
99 }
100
Michael Jurka0280c3b2010-09-17 15:00:07 -0700101 public boolean isDropEnabled() {
Winson Chung7a1d1652011-03-18 15:56:01 -0700102 final ViewGroup cellLayoutChildren = (ViewGroup) getParent();
103 final ViewGroup cellLayout = (ViewGroup) cellLayoutChildren.getParent();
104 final Workspace workspace = (Workspace) cellLayout.getParent();
105 return !workspace.isSmall();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700106 }
107
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800108 static FolderIcon fromXml(int resId, Launcher launcher, ViewGroup group,
Adam Cohendf2cc412011-04-27 16:56:57 -0700109 FolderInfo folderInfo, IconCache iconCache) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110
Adam Cohend0445262011-07-04 23:53:22 -0700111 if (INITIAL_ITEM_ANIMATION_DURATION >= DROP_IN_ANIMATION_DURATION) {
112 throw new IllegalStateException("DROP_IN_ANIMATION_DURATION must be greater than " +
113 "INITIAL_ITEM_ANIMATION_DURATION, as sequencing of adding first two items " +
114 "is dependent on this");
115 }
116
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800117 FolderIcon icon = (FolderIcon) LayoutInflater.from(launcher).inflate(resId, group, false);
118
Adam Cohend2eca6b2011-07-26 22:49:13 -0700119 icon.mFolderName = (BubbleTextView) icon.findViewById(R.id.folder_icon_name);
Adam Cohen76fc0852011-06-17 13:26:23 -0700120 icon.mFolderName.setText(folderInfo.title);
121 icon.mPreviewBackground = (ImageView) icon.findViewById(R.id.preview_background);
Winson Chung6a0f57d2011-06-29 20:10:49 -0700122 icon.mPreviewBackground.setContentDescription(folderInfo.title);
Adam Cohen76fc0852011-06-17 13:26:23 -0700123
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800124 icon.setTag(folderInfo);
125 icon.setOnClickListener(launcher);
126 icon.mInfo = folderInfo;
127 icon.mLauncher = launcher;
Adam Cohena9cf38f2011-05-02 15:36:58 -0700128
129 Folder folder = Folder.fromXml(launcher);
130 folder.setDragController(launcher.getDragController());
Adam Cohen2801caf2011-05-13 20:57:39 -0700131 folder.setFolderIcon(icon);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700132 folder.bind(folderInfo);
133 icon.mFolder = folder;
Adam Cohen19072da2011-05-31 14:30:45 -0700134 icon.mFolderRingAnimator = new FolderRingAnimator(launcher, icon);
Adam Cohen69ce2e52011-07-03 19:25:21 -0700135
Adam Cohena9cf38f2011-05-02 15:36:58 -0700136 folderInfo.addListener(icon);
Adam Cohen19072da2011-05-31 14:30:45 -0700137
138 return icon;
139 }
140
141 public static class FolderRingAnimator {
Adam Cohen69ce2e52011-07-03 19:25:21 -0700142 public int mCellX;
143 public int mCellY;
144 private CellLayout mCellLayout;
Adam Cohen76fc0852011-06-17 13:26:23 -0700145 public float mOuterRingSize;
146 public float mInnerRingSize;
Adam Cohen19072da2011-05-31 14:30:45 -0700147 public FolderIcon mFolderIcon = null;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700148 public Drawable mOuterRingDrawable = null;
149 public Drawable mInnerRingDrawable = null;
150 public static Drawable sSharedOuterRingDrawable = null;
151 public static Drawable sSharedInnerRingDrawable = null;
Adam Cohen76fc0852011-06-17 13:26:23 -0700152 public static int sPreviewSize = -1;
153 public static int sPreviewPadding = -1;
154
Adam Cohenc0dcf592011-06-01 15:30:43 -0700155 private ValueAnimator mAcceptAnimator;
156 private ValueAnimator mNeutralAnimator;
Adam Cohen19072da2011-05-31 14:30:45 -0700157
158 public FolderRingAnimator(Launcher launcher, FolderIcon folderIcon) {
Adam Cohen19072da2011-05-31 14:30:45 -0700159 mFolderIcon = folderIcon;
Adam Cohen76fc0852011-06-17 13:26:23 -0700160 Resources res = launcher.getResources();
161 mOuterRingDrawable = res.getDrawable(R.drawable.portal_ring_outer_holo);
162 mInnerRingDrawable = res.getDrawable(R.drawable.portal_ring_inner_holo);
163
164 if (sPreviewSize < 0 || sPreviewPadding < 0) {
165 sPreviewSize = res.getDimensionPixelSize(R.dimen.folder_preview_size);
166 sPreviewPadding = res.getDimensionPixelSize(R.dimen.folder_preview_padding);
167 }
Adam Cohenc0dcf592011-06-01 15:30:43 -0700168 if (sSharedOuterRingDrawable == null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700169 sSharedOuterRingDrawable = res.getDrawable(R.drawable.portal_ring_outer_holo);
Adam Cohen19072da2011-05-31 14:30:45 -0700170 }
Adam Cohenc0dcf592011-06-01 15:30:43 -0700171 if (sSharedInnerRingDrawable == null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700172 sSharedInnerRingDrawable = res.getDrawable(R.drawable.portal_ring_inner_holo);
Adam Cohen19072da2011-05-31 14:30:45 -0700173 }
Adam Cohen073a46f2011-05-17 16:28:09 -0700174 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700175
Adam Cohen19072da2011-05-31 14:30:45 -0700176 public void animateToAcceptState() {
Adam Cohenc0dcf592011-06-01 15:30:43 -0700177 if (mNeutralAnimator != null) {
178 mNeutralAnimator.cancel();
179 }
180 mAcceptAnimator = ValueAnimator.ofFloat(0f, 1f);
181 mAcceptAnimator.setDuration(CONSUMPTION_ANIMATION_DURATION);
182 mAcceptAnimator.addUpdateListener(new AnimatorUpdateListener() {
Adam Cohen19072da2011-05-31 14:30:45 -0700183 public void onAnimationUpdate(ValueAnimator animation) {
184 final float percent = (Float) animation.getAnimatedValue();
Adam Cohen76fc0852011-06-17 13:26:23 -0700185 mOuterRingSize = (1 + percent * OUTER_RING_GROWTH_FACTOR) * sPreviewSize;
186 mInnerRingSize = (1 + percent * INNER_RING_GROWTH_FACTOR) * sPreviewSize;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700187 if (mCellLayout != null) {
188 mCellLayout.invalidate();
Adam Cohen19072da2011-05-31 14:30:45 -0700189 }
190 }
191 });
Adam Cohenc0dcf592011-06-01 15:30:43 -0700192 mAcceptAnimator.addListener(new AnimatorListenerAdapter() {
Adam Cohen19072da2011-05-31 14:30:45 -0700193 @Override
Adam Cohenc0dcf592011-06-01 15:30:43 -0700194 public void onAnimationStart(Animator animation) {
Adam Cohen19072da2011-05-31 14:30:45 -0700195 if (mFolderIcon != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700196 mFolderIcon.mPreviewBackground.setVisibility(INVISIBLE);
Adam Cohen19072da2011-05-31 14:30:45 -0700197 }
198 }
199 });
Adam Cohenc0dcf592011-06-01 15:30:43 -0700200 mAcceptAnimator.start();
Adam Cohen19072da2011-05-31 14:30:45 -0700201 }
202
203 public void animateToNaturalState() {
Adam Cohenc0dcf592011-06-01 15:30:43 -0700204 if (mAcceptAnimator != null) {
205 mAcceptAnimator.cancel();
206 }
207 mNeutralAnimator = ValueAnimator.ofFloat(0f, 1f);
208 mNeutralAnimator.setDuration(CONSUMPTION_ANIMATION_DURATION);
209 mNeutralAnimator.addUpdateListener(new AnimatorUpdateListener() {
Adam Cohen19072da2011-05-31 14:30:45 -0700210 public void onAnimationUpdate(ValueAnimator animation) {
211 final float percent = (Float) animation.getAnimatedValue();
Adam Cohen76fc0852011-06-17 13:26:23 -0700212 mOuterRingSize = (1 + (1 - percent) * OUTER_RING_GROWTH_FACTOR) * sPreviewSize;
213 mInnerRingSize = (1 + (1 - percent) * INNER_RING_GROWTH_FACTOR) * sPreviewSize;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700214 if (mCellLayout != null) {
215 mCellLayout.invalidate();
Adam Cohen19072da2011-05-31 14:30:45 -0700216 }
217 }
218 });
Adam Cohenc0dcf592011-06-01 15:30:43 -0700219 mNeutralAnimator.addListener(new AnimatorListenerAdapter() {
Adam Cohen19072da2011-05-31 14:30:45 -0700220 @Override
221 public void onAnimationEnd(Animator animation) {
Adam Cohen69ce2e52011-07-03 19:25:21 -0700222 if (mCellLayout != null) {
223 mCellLayout.hideFolderAccept(FolderRingAnimator.this);
224 }
Adam Cohen19072da2011-05-31 14:30:45 -0700225 if (mFolderIcon != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700226 mFolderIcon.mPreviewBackground.setVisibility(VISIBLE);
Adam Cohen19072da2011-05-31 14:30:45 -0700227 }
Adam Cohen19072da2011-05-31 14:30:45 -0700228 }
229 });
Adam Cohenc0dcf592011-06-01 15:30:43 -0700230 mNeutralAnimator.start();
Adam Cohen19072da2011-05-31 14:30:45 -0700231 }
232
Adam Cohenc0dcf592011-06-01 15:30:43 -0700233 // Location is expressed in window coordinates
Adam Cohen69ce2e52011-07-03 19:25:21 -0700234 public void getCell(int[] loc) {
235 loc[0] = mCellX;
236 loc[1] = mCellY;
237 }
238
239 // Location is expressed in window coordinates
240 public void setCell(int x, int y) {
241 mCellX = x;
242 mCellY = y;
243 }
244
245 public void setCellLayout(CellLayout layout) {
246 mCellLayout = layout;
Adam Cohen19072da2011-05-31 14:30:45 -0700247 }
248
Adam Cohen76fc0852011-06-17 13:26:23 -0700249 public float getOuterRingSize() {
250 return mOuterRingSize;
Adam Cohen19072da2011-05-31 14:30:45 -0700251 }
252
Adam Cohen76fc0852011-06-17 13:26:23 -0700253 public float getInnerRingSize() {
254 return mInnerRingSize;
Adam Cohen19072da2011-05-31 14:30:45 -0700255 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800256 }
257
Adam Cohen073a46f2011-05-17 16:28:09 -0700258 private boolean willAcceptItem(ItemInfo item) {
259 final int itemType = item.itemType;
260 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
261 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
262 !mFolder.isFull() && item != mInfo);
263 }
264
Adam Cohenc0dcf592011-06-01 15:30:43 -0700265 public boolean acceptDrop(Object dragInfo) {
266 final ItemInfo item = (ItemInfo) dragInfo;
Adam Cohen073a46f2011-05-17 16:28:09 -0700267 return willAcceptItem(item);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800268 }
269
Adam Cohendf035382011-04-11 17:22:04 -0700270 public void addItem(ShortcutInfo item) {
271 mInfo.add(item);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700272 LauncherModel.addOrMoveItemInDatabase(mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
Adam Cohendf035382011-04-11 17:22:04 -0700273 }
274
Adam Cohenc0dcf592011-06-01 15:30:43 -0700275 public void onDragEnter(Object dragInfo) {
276 if (!willAcceptItem((ItemInfo) dragInfo)) return;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700277 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) getLayoutParams();
278 CellLayout layout = (CellLayout) getParent().getParent();
279 mFolderRingAnimator.setCell(lp.cellX, lp.cellY);
280 mFolderRingAnimator.setCellLayout(layout);
Adam Cohen19072da2011-05-31 14:30:45 -0700281 mFolderRingAnimator.animateToAcceptState();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700282 layout.showFolderAccept(mFolderRingAnimator);
Adam Cohen073a46f2011-05-17 16:28:09 -0700283 }
284
Adam Cohenc0dcf592011-06-01 15:30:43 -0700285 public void onDragOver(Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800286 }
287
Adam Cohend0445262011-07-04 23:53:22 -0700288 public void performCreateAnimation(final ShortcutInfo destInfo, final View destView,
Winson Chung557d6ed2011-07-08 15:34:52 -0700289 final ShortcutInfo srcInfo, final View srcView, Rect dstRect,
Adam Cohenac8c8762011-07-13 11:15:27 -0700290 float scaleRelativeToDragLayer, Runnable postAnimationRunnable) {
Adam Cohend0445262011-07-04 23:53:22 -0700291
292 Drawable animateDrawable = ((TextView) destView).getCompoundDrawables()[1];
293 computePreviewDrawingParams(animateDrawable.getIntrinsicWidth(), destView.getMeasuredWidth());
Adam Cohenac8c8762011-07-13 11:15:27 -0700294
Adam Cohend0445262011-07-04 23:53:22 -0700295 // This will animate the dragView (srcView) into the new folder
Adam Cohenac8c8762011-07-13 11:15:27 -0700296 onDrop(srcInfo, srcView, dstRect, scaleRelativeToDragLayer, 1, postAnimationRunnable);
Adam Cohend0445262011-07-04 23:53:22 -0700297
298 // This will animate the first item from it's position as an icon into its
299 // position as the first item in the preview
Adam Cohen8dfcba42011-07-07 16:38:18 -0700300 animateFirstItem(animateDrawable, INITIAL_ITEM_ANIMATION_DURATION);
Adam Cohend0445262011-07-04 23:53:22 -0700301
302 postDelayed(new Runnable() {
303 public void run() {
304 addItem(destInfo);
305 }
306 }, INITIAL_ITEM_ANIMATION_DURATION);
307 }
308
Adam Cohenc0dcf592011-06-01 15:30:43 -0700309 public void onDragExit(Object dragInfo) {
310 if (!willAcceptItem((ItemInfo) dragInfo)) return;
Adam Cohen19072da2011-05-31 14:30:45 -0700311 mFolderRingAnimator.animateToNaturalState();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800312 }
Patrick Dubroy440c3602010-07-13 17:50:32 -0700313
Adam Cohenac8c8762011-07-13 11:15:27 -0700314 private void onDrop(final ShortcutInfo item, View animateView, Rect finalRect,
315 float scaleRelativeToDragLayer, int index, Runnable postAnimationRunnable) {
Adam Cohend0445262011-07-04 23:53:22 -0700316 item.cellX = -1;
317 item.cellY = -1;
318 DragLayer dragLayer = mLauncher.getDragLayer();
319 Rect from = new Rect();
320 dragLayer.getViewRectRelativeToSelf(animateView, from);
321 Rect to = finalRect;
322 if (to == null) {
323 to = new Rect();
Adam Cohen4b285c52011-07-21 14:24:06 -0700324 Workspace workspace = mLauncher.getWorkspace();
325 workspace.setFinalTransitionTransform((CellLayout) getParent().getParent());
Adam Cohenac8c8762011-07-13 11:15:27 -0700326 scaleRelativeToDragLayer = dragLayer.getDescendantRectRelativeToSelf(this, to);
Adam Cohen4b285c52011-07-21 14:24:06 -0700327 workspace.resetTransitionTransform((CellLayout) getParent().getParent());
Adam Cohend0445262011-07-04 23:53:22 -0700328 }
329
Adam Cohenac8c8762011-07-13 11:15:27 -0700330 int[] center = new int[2];
331 float scale = getLocalCenterForIndex(index, center);
332 center[0] = (int) Math.round(scaleRelativeToDragLayer * center[0]);
333 center[1] = (int) Math.round(scaleRelativeToDragLayer * center[1]);
334
335 to.offset(center[0] - animateView.getMeasuredWidth() / 2,
336 center[1] - animateView.getMeasuredHeight() / 2);
Adam Cohend0445262011-07-04 23:53:22 -0700337
338 float finalAlpha = index < NUM_ITEMS_IN_PREVIEW ? 0.5f : 0f;
339
Adam Cohenac8c8762011-07-13 11:15:27 -0700340 dragLayer.animateView(animateView, from, to, finalAlpha, scale * scaleRelativeToDragLayer,
341 DROP_IN_ANIMATION_DURATION, new DecelerateInterpolator(2),
342 new AccelerateInterpolator(2), postAnimationRunnable, false);
Adam Cohend0445262011-07-04 23:53:22 -0700343 postDelayed(new Runnable() {
344 public void run() {
345 addItem(item);
346 }
347 }, DROP_IN_ANIMATION_DURATION);
348 }
349
Adam Cohen3e8f8112011-07-02 18:03:00 -0700350 public void onDrop(DragObject d) {
Adam Cohenc0dcf592011-06-01 15:30:43 -0700351 ShortcutInfo item;
Adam Cohen3e8f8112011-07-02 18:03:00 -0700352 if (d.dragInfo instanceof ApplicationInfo) {
Adam Cohenc0dcf592011-06-01 15:30:43 -0700353 // Came from all apps -- make a copy
Adam Cohen3e8f8112011-07-02 18:03:00 -0700354 item = ((ApplicationInfo) d.dragInfo).makeShortcut();
Adam Cohenc0dcf592011-06-01 15:30:43 -0700355 } else {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700356 item = (ShortcutInfo) d.dragInfo;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700357 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700358 onDrop(item, d.dragView, null, 1.0f, mInfo.contents.size(), d.postAnimationRunnable);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700359 }
360
Adam Cohencb3382b2011-05-24 14:07:08 -0700361 public DropTarget getDropTargetDelegate(DragObject d) {
Patrick Dubroy440c3602010-07-13 17:50:32 -0700362 return null;
363 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700364
Adam Cohend0445262011-07-04 23:53:22 -0700365 private void computePreviewDrawingParams(int drawableSize, int totalSize) {
366 if (mIntrinsicIconSize != drawableSize || mTotalWidth != totalSize) {
367 mIntrinsicIconSize = drawableSize;
368 mTotalWidth = totalSize;
369
370 final int previewSize = FolderRingAnimator.sPreviewSize;
371 final int previewPadding = FolderRingAnimator.sPreviewPadding;
372
373 mAvailableSpaceInPreview = (previewSize - 2 * previewPadding);
374 // cos(45) = 0.707 + ~= 0.1) = 0.8f
375 int adjustedAvailableSpace = (int) ((mAvailableSpaceInPreview / 2) * (1 + 0.8f));
376
377 int unscaledHeight = (int) (mIntrinsicIconSize * (1 + PERSPECTIVE_SHIFT_FACTOR));
378 mBaselineIconScale = (1.0f * adjustedAvailableSpace / unscaledHeight);
379
380 mBaselineIconSize = (int) (mIntrinsicIconSize * mBaselineIconScale);
381 mMaxPerspectiveShift = mBaselineIconSize * PERSPECTIVE_SHIFT_FACTOR;
382
383 mPreviewOffsetX = (mTotalWidth - mAvailableSpaceInPreview) / 2;
384 mPreviewOffsetY = previewPadding;
385 }
386 }
387
388 private void computePreviewDrawingParams(Drawable d) {
389 computePreviewDrawingParams(d.getIntrinsicWidth(), getMeasuredWidth());
390 }
391
392 class PreviewItemDrawingParams {
393 PreviewItemDrawingParams(float transX, float transY, float scale, int overlayAlpha) {
394 this.transX = transX;
395 this.transY = transY;
396 this.scale = scale;
397 this.overlayAlpha = overlayAlpha;
398 }
399 float transX;
400 float transY;
401 float scale;
402 int overlayAlpha;
403 Drawable drawable;
404 }
405
Adam Cohenac8c8762011-07-13 11:15:27 -0700406 private float getLocalCenterForIndex(int index, int[] center) {
Adam Cohend0445262011-07-04 23:53:22 -0700407 mParams = computePreviewItemDrawingParams(Math.min(NUM_ITEMS_IN_PREVIEW, index), mParams);
408
409 mParams.transX += mPreviewOffsetX;
410 mParams.transY += mPreviewOffsetY;
Adam Cohenac8c8762011-07-13 11:15:27 -0700411 float offsetX = mParams.transX + (mParams.scale * mIntrinsicIconSize) / 2;
412 float offsetY = mParams.transY + (mParams.scale * mIntrinsicIconSize) / 2;
Adam Cohend0445262011-07-04 23:53:22 -0700413
Adam Cohenac8c8762011-07-13 11:15:27 -0700414 center[0] = (int) Math.round(offsetX);
415 center[1] = (int) Math.round(offsetY);
Adam Cohend0445262011-07-04 23:53:22 -0700416 return mParams.scale;
417 }
418
419 private PreviewItemDrawingParams computePreviewItemDrawingParams(int index,
420 PreviewItemDrawingParams params) {
421 index = NUM_ITEMS_IN_PREVIEW - index - 1;
422 float r = (index * 1.0f) / (NUM_ITEMS_IN_PREVIEW - 1);
423 float scale = (1 - PERSPECTIVE_SCALE_FACTOR * (1 - r));
424
425 float offset = (1 - r) * mMaxPerspectiveShift;
426 float scaledSize = scale * mBaselineIconSize;
427 float scaleOffsetCorrection = (1 - scale) * mBaselineIconSize;
428
429 // We want to imagine our coordinates from the bottom left, growing up and to the
430 // right. This is natural for the x-axis, but for the y-axis, we have to invert things.
431 float transY = mAvailableSpaceInPreview - (offset + scaledSize + scaleOffsetCorrection);
432 float transX = offset + scaleOffsetCorrection;
433 float totalScale = mBaselineIconScale * scale;
434 final int overlayAlpha = (int) (80 * (1 - r));
435
436 if (params == null) {
437 params = new PreviewItemDrawingParams(transX, transY, totalScale, overlayAlpha);
438 } else {
439 params.transX = transX;
440 params.transY = transY;
441 params.scale = totalScale;
442 params.overlayAlpha = overlayAlpha;
443 }
444 return params;
445 }
446
447 private void drawPreviewItem(Canvas canvas, PreviewItemDrawingParams params) {
448 canvas.save();
449 canvas.translate(params.transX + mPreviewOffsetX, params.transY + mPreviewOffsetY);
450 canvas.scale(params.scale, params.scale);
451 Drawable d = params.drawable;
452
453 if (d != null) {
454 d.setBounds(0, 0, mIntrinsicIconSize, mIntrinsicIconSize);
455 d.setFilterBitmap(true);
456 d.setColorFilter(Color.argb(params.overlayAlpha, 0, 0, 0), PorterDuff.Mode.SRC_ATOP);
457 d.draw(canvas);
458 d.clearColorFilter();
459 d.setFilterBitmap(false);
460 }
461 canvas.restore();
462 }
463
Adam Cohena9cf38f2011-05-02 15:36:58 -0700464 @Override
Adam Cohenc0dcf592011-06-01 15:30:43 -0700465 protected void dispatchDraw(Canvas canvas) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700466 super.dispatchDraw(canvas);
467
Adam Cohena9cf38f2011-05-02 15:36:58 -0700468 if (mFolder == null) return;
Adam Cohend0445262011-07-04 23:53:22 -0700469 if (mFolder.getItemCount() == 0 && !mAnimating) return;
Adam Cohena9cf38f2011-05-02 15:36:58 -0700470
Adam Cohen76078c42011-06-09 15:06:52 -0700471 ArrayList<View> items = mFolder.getItemsInReadingOrder(false);
Adam Cohend0445262011-07-04 23:53:22 -0700472 Drawable d;
473 TextView v;
Adam Cohenbadf71e2011-05-26 19:08:29 -0700474
Adam Cohend0445262011-07-04 23:53:22 -0700475 // Update our drawing parameters if necessary
476 if (mAnimating) {
477 computePreviewDrawingParams(mAnimParams.drawable);
478 } else {
479 v = (TextView) items.get(0);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700480 d = v.getCompoundDrawables()[1];
Adam Cohend0445262011-07-04 23:53:22 -0700481 computePreviewDrawingParams(d);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700482 }
Adam Cohend0445262011-07-04 23:53:22 -0700483
484 int nItemsInPreview = Math.min(items.size(), NUM_ITEMS_IN_PREVIEW);
485 if (!mAnimating) {
486 for (int i = nItemsInPreview - 1; i >= 0; i--) {
487 v = (TextView) items.get(i);
488 d = v.getCompoundDrawables()[1];
489
490 mParams = computePreviewItemDrawingParams(i, mParams);
491 mParams.drawable = d;
492 drawPreviewItem(canvas, mParams);
493 }
494 } else {
495 drawPreviewItem(canvas, mAnimParams);
496 }
497 }
498
499 private void animateFirstItem(final Drawable d, int duration) {
500 computePreviewDrawingParams(d);
501 final PreviewItemDrawingParams finalParams = computePreviewItemDrawingParams(0, null);
502
503 final float scale0 = 1.0f;
504 final float transX0 = 0;
505 final float transY0 = 0;
506 mAnimParams.drawable = d;
507
508 ValueAnimator va = ValueAnimator.ofFloat(0f, 1.0f);
509 va.addUpdateListener(new AnimatorUpdateListener(){
510 public void onAnimationUpdate(ValueAnimator animation) {
511 float progress = (Float) animation.getAnimatedValue();
512
513 mAnimParams.transX = transX0 + progress * (finalParams.transX - transX0);
514 mAnimParams.transY = transY0 + progress * (finalParams.transY - transY0);
515 mAnimParams.scale = scale0 + progress * (finalParams.scale - scale0);
516 invalidate();
517 }
518 });
519 va.addListener(new AnimatorListenerAdapter() {
520 @Override
521 public void onAnimationStart(Animator animation) {
522 mAnimating = true;
523 }
524 @Override
525 public void onAnimationEnd(Animator animation) {
526 mAnimating = false;
527 }
528 });
529 va.setDuration(duration);
530 va.start();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700531 }
532
Adam Cohen76078c42011-06-09 15:06:52 -0700533 public void onItemsChanged() {
534 invalidate();
535 requestLayout();
536 }
537
Adam Cohena9cf38f2011-05-02 15:36:58 -0700538 public void onAdd(ShortcutInfo item) {
539 invalidate();
540 requestLayout();
541 }
542
543 public void onRemove(ShortcutInfo item) {
544 invalidate();
545 requestLayout();
546 }
Adam Cohen76fc0852011-06-17 13:26:23 -0700547
548 public void onTitleChanged(CharSequence title) {
Adam Cohend63cfa92011-06-24 14:17:17 -0700549 mFolderName.setText(title.toString());
Winson Chung6a0f57d2011-06-29 20:10:49 -0700550 mPreviewBackground.setContentDescription(title.toString());
Adam Cohen76fc0852011-06-17 13:26:23 -0700551 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800552}