blob: 4071433439d99e49a98c42f3ee13cde93444b46a [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;
58 private static final int INITIAL_ITEM_ANIMATION_DURATION = 150;
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,
289 final ShortcutInfo srcInfo, final View srcView, Rect dstRect) {
290
291 Drawable animateDrawable = ((TextView) destView).getCompoundDrawables()[1];
292 computePreviewDrawingParams(animateDrawable.getIntrinsicWidth(), destView.getMeasuredWidth());
293 // This will animate the dragView (srcView) into the new folder
294 onDrop(srcInfo, srcView, dstRect, 1);
295
296 // This will animate the first item from it's position as an icon into its
297 // position as the first item in the preview
298 animateFirstItem(animateDrawable, DROP_IN_ANIMATION_DURATION);
299
300 postDelayed(new Runnable() {
301 public void run() {
302 addItem(destInfo);
303 }
304 }, INITIAL_ITEM_ANIMATION_DURATION);
305 }
306
Adam Cohenc0dcf592011-06-01 15:30:43 -0700307 public void onDragExit(Object dragInfo) {
308 if (!willAcceptItem((ItemInfo) dragInfo)) return;
Adam Cohen19072da2011-05-31 14:30:45 -0700309 mFolderRingAnimator.animateToNaturalState();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800310 }
Patrick Dubroy440c3602010-07-13 17:50:32 -0700311
Adam Cohend0445262011-07-04 23:53:22 -0700312 private void onDrop(final ShortcutInfo item, View animateView, Rect finalRect, int index) {
313 item.cellX = -1;
314 item.cellY = -1;
315 DragLayer dragLayer = mLauncher.getDragLayer();
316 Rect from = new Rect();
317 dragLayer.getViewRectRelativeToSelf(animateView, from);
318 Rect to = finalRect;
319 if (to == null) {
320 to = new Rect();
321 dragLayer.getDescendantRectRelativeToSelf(this, to);
322 }
323
324 if (animateView.getMeasuredWidth() != to.width() ||
325 animateView.getMeasuredHeight() != to.height()) {
326 int offsetX = (animateView.getMeasuredWidth() - to.width()) / 2;
327 int offsetY = (animateView.getMeasuredHeight() - to.height()) / 2;
328 to.offset(-offsetX, -offsetY);
329 }
330 float scale = adjustFinalScreenRectForIndex(to, index);
331
332 float finalAlpha = index < NUM_ITEMS_IN_PREVIEW ? 0.5f : 0f;
333
334 dragLayer.animateView(animateView, from, to, finalAlpha, scale, DROP_IN_ANIMATION_DURATION,
335 new DecelerateInterpolator(2), new AccelerateInterpolator(2));
336 postDelayed(new Runnable() {
337 public void run() {
338 addItem(item);
339 }
340 }, DROP_IN_ANIMATION_DURATION);
341 }
342
Adam Cohen3e8f8112011-07-02 18:03:00 -0700343 public void onDrop(DragObject d) {
Adam Cohenc0dcf592011-06-01 15:30:43 -0700344 ShortcutInfo item;
Adam Cohen3e8f8112011-07-02 18:03:00 -0700345 if (d.dragInfo instanceof ApplicationInfo) {
Adam Cohenc0dcf592011-06-01 15:30:43 -0700346 // Came from all apps -- make a copy
Adam Cohen3e8f8112011-07-02 18:03:00 -0700347 item = ((ApplicationInfo) d.dragInfo).makeShortcut();
Adam Cohenc0dcf592011-06-01 15:30:43 -0700348 } else {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700349 item = (ShortcutInfo) d.dragInfo;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700350 }
Adam Cohend0445262011-07-04 23:53:22 -0700351 onDrop(item, d.dragView, null, mInfo.contents.size());
Adam Cohenc0dcf592011-06-01 15:30:43 -0700352 }
353
Adam Cohencb3382b2011-05-24 14:07:08 -0700354 public DropTarget getDropTargetDelegate(DragObject d) {
Patrick Dubroy440c3602010-07-13 17:50:32 -0700355 return null;
356 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700357
Adam Cohend0445262011-07-04 23:53:22 -0700358 private void computePreviewDrawingParams(int drawableSize, int totalSize) {
359 if (mIntrinsicIconSize != drawableSize || mTotalWidth != totalSize) {
360 mIntrinsicIconSize = drawableSize;
361 mTotalWidth = totalSize;
362
363 final int previewSize = FolderRingAnimator.sPreviewSize;
364 final int previewPadding = FolderRingAnimator.sPreviewPadding;
365
366 mAvailableSpaceInPreview = (previewSize - 2 * previewPadding);
367 // cos(45) = 0.707 + ~= 0.1) = 0.8f
368 int adjustedAvailableSpace = (int) ((mAvailableSpaceInPreview / 2) * (1 + 0.8f));
369
370 int unscaledHeight = (int) (mIntrinsicIconSize * (1 + PERSPECTIVE_SHIFT_FACTOR));
371 mBaselineIconScale = (1.0f * adjustedAvailableSpace / unscaledHeight);
372
373 mBaselineIconSize = (int) (mIntrinsicIconSize * mBaselineIconScale);
374 mMaxPerspectiveShift = mBaselineIconSize * PERSPECTIVE_SHIFT_FACTOR;
375
376 mPreviewOffsetX = (mTotalWidth - mAvailableSpaceInPreview) / 2;
377 mPreviewOffsetY = previewPadding;
378 }
379 }
380
381 private void computePreviewDrawingParams(Drawable d) {
382 computePreviewDrawingParams(d.getIntrinsicWidth(), getMeasuredWidth());
383 }
384
385 class PreviewItemDrawingParams {
386 PreviewItemDrawingParams(float transX, float transY, float scale, int overlayAlpha) {
387 this.transX = transX;
388 this.transY = transY;
389 this.scale = scale;
390 this.overlayAlpha = overlayAlpha;
391 }
392 float transX;
393 float transY;
394 float scale;
395 int overlayAlpha;
396 Drawable drawable;
397 }
398
399 private float adjustFinalScreenRectForIndex(Rect r, int index) {
400 mParams = computePreviewItemDrawingParams(Math.min(NUM_ITEMS_IN_PREVIEW, index), mParams);
401
402 mParams.transX += mPreviewOffsetX;
403 mParams.transY += mPreviewOffsetY;
404 float offsetX = mParams.transX + (mParams.scale * mIntrinsicIconSize) / 2 - mTotalWidth / 2;
405 float offsetY = mParams.transY + (mParams.scale * mIntrinsicIconSize) / 2 - mTotalWidth / 2;
406
407 r.offset((int) offsetX, (int) offsetY);
408 return mParams.scale;
409 }
410
411 private PreviewItemDrawingParams computePreviewItemDrawingParams(int index,
412 PreviewItemDrawingParams params) {
413 index = NUM_ITEMS_IN_PREVIEW - index - 1;
414 float r = (index * 1.0f) / (NUM_ITEMS_IN_PREVIEW - 1);
415 float scale = (1 - PERSPECTIVE_SCALE_FACTOR * (1 - r));
416
417 float offset = (1 - r) * mMaxPerspectiveShift;
418 float scaledSize = scale * mBaselineIconSize;
419 float scaleOffsetCorrection = (1 - scale) * mBaselineIconSize;
420
421 // We want to imagine our coordinates from the bottom left, growing up and to the
422 // right. This is natural for the x-axis, but for the y-axis, we have to invert things.
423 float transY = mAvailableSpaceInPreview - (offset + scaledSize + scaleOffsetCorrection);
424 float transX = offset + scaleOffsetCorrection;
425 float totalScale = mBaselineIconScale * scale;
426 final int overlayAlpha = (int) (80 * (1 - r));
427
428 if (params == null) {
429 params = new PreviewItemDrawingParams(transX, transY, totalScale, overlayAlpha);
430 } else {
431 params.transX = transX;
432 params.transY = transY;
433 params.scale = totalScale;
434 params.overlayAlpha = overlayAlpha;
435 }
436 return params;
437 }
438
439 private void drawPreviewItem(Canvas canvas, PreviewItemDrawingParams params) {
440 canvas.save();
441 canvas.translate(params.transX + mPreviewOffsetX, params.transY + mPreviewOffsetY);
442 canvas.scale(params.scale, params.scale);
443 Drawable d = params.drawable;
444
445 if (d != null) {
446 d.setBounds(0, 0, mIntrinsicIconSize, mIntrinsicIconSize);
447 d.setFilterBitmap(true);
448 d.setColorFilter(Color.argb(params.overlayAlpha, 0, 0, 0), PorterDuff.Mode.SRC_ATOP);
449 d.draw(canvas);
450 d.clearColorFilter();
451 d.setFilterBitmap(false);
452 }
453 canvas.restore();
454 }
455
Adam Cohena9cf38f2011-05-02 15:36:58 -0700456 @Override
Adam Cohenc0dcf592011-06-01 15:30:43 -0700457 protected void dispatchDraw(Canvas canvas) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700458 super.dispatchDraw(canvas);
459
Adam Cohena9cf38f2011-05-02 15:36:58 -0700460 if (mFolder == null) return;
Adam Cohend0445262011-07-04 23:53:22 -0700461 if (mFolder.getItemCount() == 0 && !mAnimating) return;
Adam Cohena9cf38f2011-05-02 15:36:58 -0700462
Adam Cohen76078c42011-06-09 15:06:52 -0700463 ArrayList<View> items = mFolder.getItemsInReadingOrder(false);
Adam Cohend0445262011-07-04 23:53:22 -0700464 Drawable d;
465 TextView v;
Adam Cohenbadf71e2011-05-26 19:08:29 -0700466
Adam Cohend0445262011-07-04 23:53:22 -0700467 // Update our drawing parameters if necessary
468 if (mAnimating) {
469 computePreviewDrawingParams(mAnimParams.drawable);
470 } else {
471 v = (TextView) items.get(0);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700472 d = v.getCompoundDrawables()[1];
Adam Cohend0445262011-07-04 23:53:22 -0700473 computePreviewDrawingParams(d);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700474 }
Adam Cohend0445262011-07-04 23:53:22 -0700475
476 int nItemsInPreview = Math.min(items.size(), NUM_ITEMS_IN_PREVIEW);
477 if (!mAnimating) {
478 for (int i = nItemsInPreview - 1; i >= 0; i--) {
479 v = (TextView) items.get(i);
480 d = v.getCompoundDrawables()[1];
481
482 mParams = computePreviewItemDrawingParams(i, mParams);
483 mParams.drawable = d;
484 drawPreviewItem(canvas, mParams);
485 }
486 } else {
487 drawPreviewItem(canvas, mAnimParams);
488 }
489 }
490
491 private void animateFirstItem(final Drawable d, int duration) {
492 computePreviewDrawingParams(d);
493 final PreviewItemDrawingParams finalParams = computePreviewItemDrawingParams(0, null);
494
495 final float scale0 = 1.0f;
496 final float transX0 = 0;
497 final float transY0 = 0;
498 mAnimParams.drawable = d;
499
500 ValueAnimator va = ValueAnimator.ofFloat(0f, 1.0f);
501 va.addUpdateListener(new AnimatorUpdateListener(){
502 public void onAnimationUpdate(ValueAnimator animation) {
503 float progress = (Float) animation.getAnimatedValue();
504
505 mAnimParams.transX = transX0 + progress * (finalParams.transX - transX0);
506 mAnimParams.transY = transY0 + progress * (finalParams.transY - transY0);
507 mAnimParams.scale = scale0 + progress * (finalParams.scale - scale0);
508 invalidate();
509 }
510 });
511 va.addListener(new AnimatorListenerAdapter() {
512 @Override
513 public void onAnimationStart(Animator animation) {
514 mAnimating = true;
515 }
516 @Override
517 public void onAnimationEnd(Animator animation) {
518 mAnimating = false;
519 }
520 });
521 va.setDuration(duration);
522 va.start();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700523 }
524
Adam Cohen76078c42011-06-09 15:06:52 -0700525 public void onItemsChanged() {
526 invalidate();
527 requestLayout();
528 }
529
Adam Cohena9cf38f2011-05-02 15:36:58 -0700530 public void onAdd(ShortcutInfo item) {
531 invalidate();
532 requestLayout();
533 }
534
535 public void onRemove(ShortcutInfo item) {
536 invalidate();
537 requestLayout();
538 }
Adam Cohen76fc0852011-06-17 13:26:23 -0700539
540 public void onTitleChanged(CharSequence title) {
Adam Cohend63cfa92011-06-24 14:17:17 -0700541 mFolderName.setText(title.toString());
Winson Chung6a0f57d2011-06-29 20:10:49 -0700542 mPreviewBackground.setContentDescription(title.toString());
Adam Cohen76fc0852011-06-17 13:26:23 -0700543 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800544}