blob: 283c2954f66ae724bc3275b6e8b23f868273c728 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Adam Cohen2801caf2011-05-13 20:57:39 -070019import android.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
Adam Cohen2801caf2011-05-13 20:57:39 -070021import android.animation.ValueAnimator;
22import android.animation.ValueAnimator.AnimatorUpdateListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080023import android.content.Context;
24import android.content.res.Resources;
Adam Cohena9cf38f2011-05-02 15:36:58 -070025import android.graphics.Canvas;
Adam Cohenf4b08912011-05-17 18:45:47 -070026import android.graphics.Color;
Adam Cohenbadf71e2011-05-26 19:08:29 -070027import android.graphics.PorterDuff;
Adam Cohen3e8f8112011-07-02 18:03:00 -070028import android.graphics.Rect;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.graphics.drawable.Drawable;
30import android.util.AttributeSet;
31import android.view.LayoutInflater;
Adam Cohen7c693212011-05-18 15:26:57 -070032import android.view.View;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.view.ViewGroup;
Adam Cohen3e8f8112011-07-02 18:03:00 -070034import android.view.animation.AccelerateInterpolator;
35import android.view.animation.DecelerateInterpolator;
Adam Cohen76fc0852011-06-17 13:26:23 -070036import android.widget.ImageView;
37import android.widget.LinearLayout;
Adam Cohena9cf38f2011-05-02 15:36:58 -070038import android.widget.TextView;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039
Romain Guyedcce092010-03-04 13:03:17 -080040import com.android.launcher.R;
Adam Cohenc0dcf592011-06-01 15:30:43 -070041import com.android.launcher2.DropTarget.DragObject;
Adam Cohena9cf38f2011-05-02 15:36:58 -070042import com.android.launcher2.FolderInfo.FolderListener;
Romain Guyedcce092010-03-04 13:03:17 -080043
Adam Cohenc0dcf592011-06-01 15:30:43 -070044import java.util.ArrayList;
45
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046/**
47 * An icon that can appear on in the workspace representing an {@link UserFolder}.
48 */
Adam Cohen76fc0852011-06-17 13:26:23 -070049public class FolderIcon extends LinearLayout implements FolderListener {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050 private Launcher mLauncher;
Adam Cohena9cf38f2011-05-02 15:36:58 -070051 Folder mFolder;
52 FolderInfo mInfo;
53
Adam Cohenbadf71e2011-05-26 19:08:29 -070054 // The number of icons to display in the
Adam Cohen76fc0852011-06-17 13:26:23 -070055 private static final int NUM_ITEMS_IN_PREVIEW = 3;
Adam Cohenf4b08912011-05-17 18:45:47 -070056 private static final int CONSUMPTION_ANIMATION_DURATION = 100;
Adam Cohend0445262011-07-04 23:53:22 -070057 private static final int DROP_IN_ANIMATION_DURATION = 400;
Adam Cohen8dfcba42011-07-07 16:38:18 -070058 private static final int INITIAL_ITEM_ANIMATION_DURATION = 350;
Adam Cohenbadf71e2011-05-26 19:08:29 -070059
60 // The degree to which the inner ring grows when accepting drop
Adam Cohen69ce2e52011-07-03 19:25:21 -070061 private static final float INNER_RING_GROWTH_FACTOR = 0.15f;
Adam Cohenbadf71e2011-05-26 19:08:29 -070062
Adam Cohenbadf71e2011-05-26 19:08:29 -070063 // The degree to which the outer ring is scaled in its natural state
Adam Cohen69ce2e52011-07-03 19:25:21 -070064 private static final float OUTER_RING_GROWTH_FACTOR = 0.3f;
Adam Cohenbadf71e2011-05-26 19:08:29 -070065
66 // The amount of vertical spread between items in the stack [0...1]
Adam Cohen76fc0852011-06-17 13:26:23 -070067 private static final float PERSPECTIVE_SHIFT_FACTOR = 0.24f;
Adam Cohenbadf71e2011-05-26 19:08:29 -070068
69 // The degree to which the item in the back of the stack is scaled [0...1]
70 // (0 means it's not scaled at all, 1 means it's scaled to nothing)
Adam Cohen76fc0852011-06-17 13:26:23 -070071 private static final float PERSPECTIVE_SCALE_FACTOR = 0.35f;
Adam Cohenbadf71e2011-05-26 19:08:29 -070072
Adam Cohen76fc0852011-06-17 13:26:23 -070073 private ImageView mPreviewBackground;
74 private BubbleTextView mFolderName;
Adam Cohen073a46f2011-05-17 16:28:09 -070075
Adam Cohen19072da2011-05-31 14:30:45 -070076 FolderRingAnimator mFolderRingAnimator = null;
Adam Cohen2801caf2011-05-13 20:57:39 -070077
Adam Cohend0445262011-07-04 23:53:22 -070078 // These variables are all associated with the drawing of the preview; they are stored
79 // as member variables for shared usage and to avoid computation on each frame
80 private int mIntrinsicIconSize;
81 private float mBaselineIconScale;
82 private int mBaselineIconSize;
83 private int mAvailableSpaceInPreview;
84 private int mTotalWidth = -1;
85 private int mPreviewOffsetX;
86 private int mPreviewOffsetY;
87 private float mMaxPerspectiveShift;
88 boolean mAnimating = false;
89 private PreviewItemDrawingParams mParams = new PreviewItemDrawingParams(0, 0, 0, 0);
90 private PreviewItemDrawingParams mAnimParams = new PreviewItemDrawingParams(0, 0, 0, 0);
91
The Android Open Source Project31dd5032009-03-03 19:32:27 -080092 public FolderIcon(Context context, AttributeSet attrs) {
93 super(context, attrs);
94 }
95
96 public FolderIcon(Context context) {
97 super(context);
98 }
99
Michael Jurka0280c3b2010-09-17 15:00:07 -0700100 public boolean isDropEnabled() {
Winson Chung7a1d1652011-03-18 15:56:01 -0700101 final ViewGroup cellLayoutChildren = (ViewGroup) getParent();
102 final ViewGroup cellLayout = (ViewGroup) cellLayoutChildren.getParent();
103 final Workspace workspace = (Workspace) cellLayout.getParent();
104 return !workspace.isSmall();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700105 }
106
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800107 static FolderIcon fromXml(int resId, Launcher launcher, ViewGroup group,
Adam Cohendf2cc412011-04-27 16:56:57 -0700108 FolderInfo folderInfo, IconCache iconCache) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800109
Adam Cohend0445262011-07-04 23:53:22 -0700110 if (INITIAL_ITEM_ANIMATION_DURATION >= DROP_IN_ANIMATION_DURATION) {
111 throw new IllegalStateException("DROP_IN_ANIMATION_DURATION must be greater than " +
112 "INITIAL_ITEM_ANIMATION_DURATION, as sequencing of adding first two items " +
113 "is dependent on this");
114 }
115
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800116 FolderIcon icon = (FolderIcon) LayoutInflater.from(launcher).inflate(resId, group, false);
117
Adam Cohen76fc0852011-06-17 13:26:23 -0700118 icon.mFolderName = (BubbleTextView) icon.findViewById(R.id.folder_name);
119 icon.mFolderName.setText(folderInfo.title);
120 icon.mPreviewBackground = (ImageView) icon.findViewById(R.id.preview_background);
Winson Chung6a0f57d2011-06-29 20:10:49 -0700121 icon.mPreviewBackground.setContentDescription(folderInfo.title);
Adam Cohen76fc0852011-06-17 13:26:23 -0700122
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123 icon.setTag(folderInfo);
124 icon.setOnClickListener(launcher);
125 icon.mInfo = folderInfo;
126 icon.mLauncher = launcher;
Adam Cohena9cf38f2011-05-02 15:36:58 -0700127
128 Folder folder = Folder.fromXml(launcher);
129 folder.setDragController(launcher.getDragController());
130 folder.setLauncher(launcher);
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,
290 Runnable postAnimationRunnable) {
Adam Cohend0445262011-07-04 23:53:22 -0700291
292 Drawable animateDrawable = ((TextView) destView).getCompoundDrawables()[1];
293 computePreviewDrawingParams(animateDrawable.getIntrinsicWidth(), destView.getMeasuredWidth());
294 // This will animate the dragView (srcView) into the new folder
Winson Chung557d6ed2011-07-08 15:34:52 -0700295 onDrop(srcInfo, srcView, dstRect, 1, postAnimationRunnable);
Adam Cohend0445262011-07-04 23:53:22 -0700296
297 // This will animate the first item from it's position as an icon into its
298 // position as the first item in the preview
Adam Cohen8dfcba42011-07-07 16:38:18 -0700299 animateFirstItem(animateDrawable, INITIAL_ITEM_ANIMATION_DURATION);
Adam Cohend0445262011-07-04 23:53:22 -0700300
301 postDelayed(new Runnable() {
302 public void run() {
303 addItem(destInfo);
304 }
305 }, INITIAL_ITEM_ANIMATION_DURATION);
306 }
307
Adam Cohenc0dcf592011-06-01 15:30:43 -0700308 public void onDragExit(Object dragInfo) {
309 if (!willAcceptItem((ItemInfo) dragInfo)) return;
Adam Cohen19072da2011-05-31 14:30:45 -0700310 mFolderRingAnimator.animateToNaturalState();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800311 }
Patrick Dubroy440c3602010-07-13 17:50:32 -0700312
Winson Chung557d6ed2011-07-08 15:34:52 -0700313 private void onDrop(final ShortcutInfo item, View animateView, Rect finalRect, int index,
314 Runnable postAnimationRunnable) {
Adam Cohend0445262011-07-04 23:53:22 -0700315 item.cellX = -1;
316 item.cellY = -1;
317 DragLayer dragLayer = mLauncher.getDragLayer();
318 Rect from = new Rect();
319 dragLayer.getViewRectRelativeToSelf(animateView, from);
320 Rect to = finalRect;
321 if (to == null) {
322 to = new Rect();
323 dragLayer.getDescendantRectRelativeToSelf(this, to);
324 }
325
326 if (animateView.getMeasuredWidth() != to.width() ||
327 animateView.getMeasuredHeight() != to.height()) {
328 int offsetX = (animateView.getMeasuredWidth() - to.width()) / 2;
329 int offsetY = (animateView.getMeasuredHeight() - to.height()) / 2;
330 to.offset(-offsetX, -offsetY);
331 }
332 float scale = adjustFinalScreenRectForIndex(to, index);
333
334 float finalAlpha = index < NUM_ITEMS_IN_PREVIEW ? 0.5f : 0f;
335
336 dragLayer.animateView(animateView, from, to, finalAlpha, scale, DROP_IN_ANIMATION_DURATION,
Winson Chung557d6ed2011-07-08 15:34:52 -0700337 new DecelerateInterpolator(2), new AccelerateInterpolator(2), postAnimationRunnable,
338 false);
Adam Cohend0445262011-07-04 23:53:22 -0700339 postDelayed(new Runnable() {
340 public void run() {
341 addItem(item);
342 }
343 }, DROP_IN_ANIMATION_DURATION);
344 }
345
Adam Cohen3e8f8112011-07-02 18:03:00 -0700346 public void onDrop(DragObject d) {
Adam Cohenc0dcf592011-06-01 15:30:43 -0700347 ShortcutInfo item;
Adam Cohen3e8f8112011-07-02 18:03:00 -0700348 if (d.dragInfo instanceof ApplicationInfo) {
Adam Cohenc0dcf592011-06-01 15:30:43 -0700349 // Came from all apps -- make a copy
Adam Cohen3e8f8112011-07-02 18:03:00 -0700350 item = ((ApplicationInfo) d.dragInfo).makeShortcut();
Adam Cohenc0dcf592011-06-01 15:30:43 -0700351 } else {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700352 item = (ShortcutInfo) d.dragInfo;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700353 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700354 onDrop(item, d.dragView, null, mInfo.contents.size(), d.postAnimationRunnable);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700355 }
356
Adam Cohencb3382b2011-05-24 14:07:08 -0700357 public DropTarget getDropTargetDelegate(DragObject d) {
Patrick Dubroy440c3602010-07-13 17:50:32 -0700358 return null;
359 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700360
Adam Cohend0445262011-07-04 23:53:22 -0700361 private void computePreviewDrawingParams(int drawableSize, int totalSize) {
362 if (mIntrinsicIconSize != drawableSize || mTotalWidth != totalSize) {
363 mIntrinsicIconSize = drawableSize;
364 mTotalWidth = totalSize;
365
366 final int previewSize = FolderRingAnimator.sPreviewSize;
367 final int previewPadding = FolderRingAnimator.sPreviewPadding;
368
369 mAvailableSpaceInPreview = (previewSize - 2 * previewPadding);
370 // cos(45) = 0.707 + ~= 0.1) = 0.8f
371 int adjustedAvailableSpace = (int) ((mAvailableSpaceInPreview / 2) * (1 + 0.8f));
372
373 int unscaledHeight = (int) (mIntrinsicIconSize * (1 + PERSPECTIVE_SHIFT_FACTOR));
374 mBaselineIconScale = (1.0f * adjustedAvailableSpace / unscaledHeight);
375
376 mBaselineIconSize = (int) (mIntrinsicIconSize * mBaselineIconScale);
377 mMaxPerspectiveShift = mBaselineIconSize * PERSPECTIVE_SHIFT_FACTOR;
378
379 mPreviewOffsetX = (mTotalWidth - mAvailableSpaceInPreview) / 2;
380 mPreviewOffsetY = previewPadding;
381 }
382 }
383
384 private void computePreviewDrawingParams(Drawable d) {
385 computePreviewDrawingParams(d.getIntrinsicWidth(), getMeasuredWidth());
386 }
387
388 class PreviewItemDrawingParams {
389 PreviewItemDrawingParams(float transX, float transY, float scale, int overlayAlpha) {
390 this.transX = transX;
391 this.transY = transY;
392 this.scale = scale;
393 this.overlayAlpha = overlayAlpha;
394 }
395 float transX;
396 float transY;
397 float scale;
398 int overlayAlpha;
399 Drawable drawable;
400 }
401
402 private float adjustFinalScreenRectForIndex(Rect r, int index) {
403 mParams = computePreviewItemDrawingParams(Math.min(NUM_ITEMS_IN_PREVIEW, index), mParams);
404
405 mParams.transX += mPreviewOffsetX;
406 mParams.transY += mPreviewOffsetY;
407 float offsetX = mParams.transX + (mParams.scale * mIntrinsicIconSize) / 2 - mTotalWidth / 2;
408 float offsetY = mParams.transY + (mParams.scale * mIntrinsicIconSize) / 2 - mTotalWidth / 2;
409
410 r.offset((int) offsetX, (int) offsetY);
411 return mParams.scale;
412 }
413
414 private PreviewItemDrawingParams computePreviewItemDrawingParams(int index,
415 PreviewItemDrawingParams params) {
416 index = NUM_ITEMS_IN_PREVIEW - index - 1;
417 float r = (index * 1.0f) / (NUM_ITEMS_IN_PREVIEW - 1);
418 float scale = (1 - PERSPECTIVE_SCALE_FACTOR * (1 - r));
419
420 float offset = (1 - r) * mMaxPerspectiveShift;
421 float scaledSize = scale * mBaselineIconSize;
422 float scaleOffsetCorrection = (1 - scale) * mBaselineIconSize;
423
424 // We want to imagine our coordinates from the bottom left, growing up and to the
425 // right. This is natural for the x-axis, but for the y-axis, we have to invert things.
426 float transY = mAvailableSpaceInPreview - (offset + scaledSize + scaleOffsetCorrection);
427 float transX = offset + scaleOffsetCorrection;
428 float totalScale = mBaselineIconScale * scale;
429 final int overlayAlpha = (int) (80 * (1 - r));
430
431 if (params == null) {
432 params = new PreviewItemDrawingParams(transX, transY, totalScale, overlayAlpha);
433 } else {
434 params.transX = transX;
435 params.transY = transY;
436 params.scale = totalScale;
437 params.overlayAlpha = overlayAlpha;
438 }
439 return params;
440 }
441
442 private void drawPreviewItem(Canvas canvas, PreviewItemDrawingParams params) {
443 canvas.save();
444 canvas.translate(params.transX + mPreviewOffsetX, params.transY + mPreviewOffsetY);
445 canvas.scale(params.scale, params.scale);
446 Drawable d = params.drawable;
447
448 if (d != null) {
449 d.setBounds(0, 0, mIntrinsicIconSize, mIntrinsicIconSize);
450 d.setFilterBitmap(true);
451 d.setColorFilter(Color.argb(params.overlayAlpha, 0, 0, 0), PorterDuff.Mode.SRC_ATOP);
452 d.draw(canvas);
453 d.clearColorFilter();
454 d.setFilterBitmap(false);
455 }
456 canvas.restore();
457 }
458
Adam Cohena9cf38f2011-05-02 15:36:58 -0700459 @Override
Adam Cohenc0dcf592011-06-01 15:30:43 -0700460 protected void dispatchDraw(Canvas canvas) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700461 super.dispatchDraw(canvas);
462
Adam Cohena9cf38f2011-05-02 15:36:58 -0700463 if (mFolder == null) return;
Adam Cohend0445262011-07-04 23:53:22 -0700464 if (mFolder.getItemCount() == 0 && !mAnimating) return;
Adam Cohena9cf38f2011-05-02 15:36:58 -0700465
Adam Cohen76078c42011-06-09 15:06:52 -0700466 ArrayList<View> items = mFolder.getItemsInReadingOrder(false);
Adam Cohend0445262011-07-04 23:53:22 -0700467 Drawable d;
468 TextView v;
Adam Cohenbadf71e2011-05-26 19:08:29 -0700469
Adam Cohend0445262011-07-04 23:53:22 -0700470 // Update our drawing parameters if necessary
471 if (mAnimating) {
472 computePreviewDrawingParams(mAnimParams.drawable);
473 } else {
474 v = (TextView) items.get(0);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700475 d = v.getCompoundDrawables()[1];
Adam Cohend0445262011-07-04 23:53:22 -0700476 computePreviewDrawingParams(d);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700477 }
Adam Cohend0445262011-07-04 23:53:22 -0700478
479 int nItemsInPreview = Math.min(items.size(), NUM_ITEMS_IN_PREVIEW);
480 if (!mAnimating) {
481 for (int i = nItemsInPreview - 1; i >= 0; i--) {
482 v = (TextView) items.get(i);
483 d = v.getCompoundDrawables()[1];
484
485 mParams = computePreviewItemDrawingParams(i, mParams);
486 mParams.drawable = d;
487 drawPreviewItem(canvas, mParams);
488 }
489 } else {
490 drawPreviewItem(canvas, mAnimParams);
491 }
492 }
493
494 private void animateFirstItem(final Drawable d, int duration) {
495 computePreviewDrawingParams(d);
496 final PreviewItemDrawingParams finalParams = computePreviewItemDrawingParams(0, null);
497
498 final float scale0 = 1.0f;
499 final float transX0 = 0;
500 final float transY0 = 0;
501 mAnimParams.drawable = d;
502
503 ValueAnimator va = ValueAnimator.ofFloat(0f, 1.0f);
504 va.addUpdateListener(new AnimatorUpdateListener(){
505 public void onAnimationUpdate(ValueAnimator animation) {
506 float progress = (Float) animation.getAnimatedValue();
507
508 mAnimParams.transX = transX0 + progress * (finalParams.transX - transX0);
509 mAnimParams.transY = transY0 + progress * (finalParams.transY - transY0);
510 mAnimParams.scale = scale0 + progress * (finalParams.scale - scale0);
511 invalidate();
512 }
513 });
514 va.addListener(new AnimatorListenerAdapter() {
515 @Override
516 public void onAnimationStart(Animator animation) {
517 mAnimating = true;
518 }
519 @Override
520 public void onAnimationEnd(Animator animation) {
521 mAnimating = false;
522 }
523 });
524 va.setDuration(duration);
525 va.start();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700526 }
527
Adam Cohen76078c42011-06-09 15:06:52 -0700528 public void onItemsChanged() {
529 invalidate();
530 requestLayout();
531 }
532
Adam Cohena9cf38f2011-05-02 15:36:58 -0700533 public void onAdd(ShortcutInfo item) {
534 invalidate();
535 requestLayout();
536 }
537
538 public void onRemove(ShortcutInfo item) {
539 invalidate();
540 requestLayout();
541 }
Adam Cohen76fc0852011-06-17 13:26:23 -0700542
543 public void onTitleChanged(CharSequence title) {
Adam Cohend63cfa92011-06-24 14:17:17 -0700544 mFolderName.setText(title.toString());
Winson Chung6a0f57d2011-06-29 20:10:49 -0700545 mPreviewBackground.setContentDescription(title.toString());
Adam Cohen76fc0852011-06-17 13:26:23 -0700546 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800547}