blob: d6430840680303cd5d6c72f09ecc359624740d84 [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;
Winson Chung88f33452012-02-23 15:23:44 -080033import android.view.MotionEvent;
Adam Cohen7c693212011-05-18 15:26:57 -070034import android.view.View;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.view.ViewGroup;
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
Winson Chung88f33452012-02-23 15:23:44 -080057 private CheckLongPressHelper mLongPressHelper;
58
Adam Cohenbadf71e2011-05-26 19:08:29 -070059 // The number of icons to display in the
Adam Cohen76fc0852011-06-17 13:26:23 -070060 private static final int NUM_ITEMS_IN_PREVIEW = 3;
Adam Cohenf4b08912011-05-17 18:45:47 -070061 private static final int CONSUMPTION_ANIMATION_DURATION = 100;
Adam Cohend0445262011-07-04 23:53:22 -070062 private static final int DROP_IN_ANIMATION_DURATION = 400;
Adam Cohen8dfcba42011-07-07 16:38:18 -070063 private static final int INITIAL_ITEM_ANIMATION_DURATION = 350;
Adam Cohenbadf71e2011-05-26 19:08:29 -070064
65 // The degree to which the inner ring grows when accepting drop
Adam Cohen69ce2e52011-07-03 19:25:21 -070066 private static final float INNER_RING_GROWTH_FACTOR = 0.15f;
Adam Cohenbadf71e2011-05-26 19:08:29 -070067
Adam Cohenbadf71e2011-05-26 19:08:29 -070068 // The degree to which the outer ring is scaled in its natural state
Adam Cohen69ce2e52011-07-03 19:25:21 -070069 private static final float OUTER_RING_GROWTH_FACTOR = 0.3f;
Adam Cohenbadf71e2011-05-26 19:08:29 -070070
71 // The amount of vertical spread between items in the stack [0...1]
Adam Cohen76fc0852011-06-17 13:26:23 -070072 private static final float PERSPECTIVE_SHIFT_FACTOR = 0.24f;
Adam Cohenbadf71e2011-05-26 19:08:29 -070073
74 // The degree to which the item in the back of the stack is scaled [0...1]
75 // (0 means it's not scaled at all, 1 means it's scaled to nothing)
Adam Cohen76fc0852011-06-17 13:26:23 -070076 private static final float PERSPECTIVE_SCALE_FACTOR = 0.35f;
Adam Cohenbadf71e2011-05-26 19:08:29 -070077
Adam Cohenc51934b2011-07-26 21:07:43 -070078 public static Drawable sSharedFolderLeaveBehind = null;
79
Adam Cohen76fc0852011-06-17 13:26:23 -070080 private ImageView mPreviewBackground;
81 private BubbleTextView mFolderName;
Adam Cohen073a46f2011-05-17 16:28:09 -070082
Adam Cohen19072da2011-05-31 14:30:45 -070083 FolderRingAnimator mFolderRingAnimator = null;
Adam Cohen2801caf2011-05-13 20:57:39 -070084
Adam Cohend0445262011-07-04 23:53:22 -070085 // These variables are all associated with the drawing of the preview; they are stored
86 // as member variables for shared usage and to avoid computation on each frame
87 private int mIntrinsicIconSize;
88 private float mBaselineIconScale;
89 private int mBaselineIconSize;
90 private int mAvailableSpaceInPreview;
91 private int mTotalWidth = -1;
92 private int mPreviewOffsetX;
93 private int mPreviewOffsetY;
94 private float mMaxPerspectiveShift;
95 boolean mAnimating = false;
96 private PreviewItemDrawingParams mParams = new PreviewItemDrawingParams(0, 0, 0, 0);
97 private PreviewItemDrawingParams mAnimParams = new PreviewItemDrawingParams(0, 0, 0, 0);
98
The Android Open Source Project31dd5032009-03-03 19:32:27 -080099 public FolderIcon(Context context, AttributeSet attrs) {
100 super(context, attrs);
Winson Chung88f33452012-02-23 15:23:44 -0800101 init();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800102 }
103
104 public FolderIcon(Context context) {
105 super(context);
Winson Chung88f33452012-02-23 15:23:44 -0800106 init();
107 }
108
109 private void init() {
110 mLongPressHelper = new CheckLongPressHelper(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800111 }
112
Michael Jurka0280c3b2010-09-17 15:00:07 -0700113 public boolean isDropEnabled() {
Winson Chung7a1d1652011-03-18 15:56:01 -0700114 final ViewGroup cellLayoutChildren = (ViewGroup) getParent();
115 final ViewGroup cellLayout = (ViewGroup) cellLayoutChildren.getParent();
116 final Workspace workspace = (Workspace) cellLayout.getParent();
117 return !workspace.isSmall();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700118 }
119
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800120 static FolderIcon fromXml(int resId, Launcher launcher, ViewGroup group,
Adam Cohendf2cc412011-04-27 16:56:57 -0700121 FolderInfo folderInfo, IconCache iconCache) {
Michael Jurka3a9fced2012-04-13 14:44:29 -0700122 @SuppressWarnings("all") // suppress dead code warning
123 final boolean error = INITIAL_ITEM_ANIMATION_DURATION >= DROP_IN_ANIMATION_DURATION;
124 if (error) {
Adam Cohend0445262011-07-04 23:53:22 -0700125 throw new IllegalStateException("DROP_IN_ANIMATION_DURATION must be greater than " +
126 "INITIAL_ITEM_ANIMATION_DURATION, as sequencing of adding first two items " +
127 "is dependent on this");
128 }
129
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130 FolderIcon icon = (FolderIcon) LayoutInflater.from(launcher).inflate(resId, group, false);
131
Adam Cohend2eca6b2011-07-26 22:49:13 -0700132 icon.mFolderName = (BubbleTextView) icon.findViewById(R.id.folder_icon_name);
Adam Cohen76fc0852011-06-17 13:26:23 -0700133 icon.mFolderName.setText(folderInfo.title);
134 icon.mPreviewBackground = (ImageView) icon.findViewById(R.id.preview_background);
135
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136 icon.setTag(folderInfo);
137 icon.setOnClickListener(launcher);
138 icon.mInfo = folderInfo;
139 icon.mLauncher = launcher;
Adam Cohen3371da02011-10-25 21:38:29 -0700140 icon.setContentDescription(String.format(launcher.getString(R.string.folder_name_format),
141 folderInfo.title));
Adam Cohena9cf38f2011-05-02 15:36:58 -0700142 Folder folder = Folder.fromXml(launcher);
143 folder.setDragController(launcher.getDragController());
Adam Cohen2801caf2011-05-13 20:57:39 -0700144 folder.setFolderIcon(icon);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700145 folder.bind(folderInfo);
146 icon.mFolder = folder;
Adam Cohen099f60d2011-08-23 21:07:26 -0700147
Adam Cohen099f60d2011-08-23 21:07:26 -0700148 icon.mFolderRingAnimator = new FolderRingAnimator(launcher, icon);
149 folderInfo.addListener(icon);
150
Adam Cohen19072da2011-05-31 14:30:45 -0700151 return icon;
152 }
153
Adam Cohen099f60d2011-08-23 21:07:26 -0700154 @Override
155 protected Parcelable onSaveInstanceState() {
156 sStaticValuesDirty = true;
157 return super.onSaveInstanceState();
158 }
159
Adam Cohen19072da2011-05-31 14:30:45 -0700160 public static class FolderRingAnimator {
Adam Cohen69ce2e52011-07-03 19:25:21 -0700161 public int mCellX;
162 public int mCellY;
163 private CellLayout mCellLayout;
Adam Cohen76fc0852011-06-17 13:26:23 -0700164 public float mOuterRingSize;
165 public float mInnerRingSize;
Adam Cohen19072da2011-05-31 14:30:45 -0700166 public FolderIcon mFolderIcon = null;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700167 public Drawable mOuterRingDrawable = null;
168 public Drawable mInnerRingDrawable = null;
169 public static Drawable sSharedOuterRingDrawable = null;
170 public static Drawable sSharedInnerRingDrawable = null;
Adam Cohen76fc0852011-06-17 13:26:23 -0700171 public static int sPreviewSize = -1;
172 public static int sPreviewPadding = -1;
173
Adam Cohenc0dcf592011-06-01 15:30:43 -0700174 private ValueAnimator mAcceptAnimator;
175 private ValueAnimator mNeutralAnimator;
Adam Cohen19072da2011-05-31 14:30:45 -0700176
177 public FolderRingAnimator(Launcher launcher, FolderIcon folderIcon) {
Adam Cohen19072da2011-05-31 14:30:45 -0700178 mFolderIcon = folderIcon;
Adam Cohen76fc0852011-06-17 13:26:23 -0700179 Resources res = launcher.getResources();
180 mOuterRingDrawable = res.getDrawable(R.drawable.portal_ring_outer_holo);
181 mInnerRingDrawable = res.getDrawable(R.drawable.portal_ring_inner_holo);
182
Adam Cohen099f60d2011-08-23 21:07:26 -0700183 // We need to reload the static values when configuration changes in case they are
184 // different in another configuration
185 if (sStaticValuesDirty) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700186 sPreviewSize = res.getDimensionPixelSize(R.dimen.folder_preview_size);
187 sPreviewPadding = res.getDimensionPixelSize(R.dimen.folder_preview_padding);
Adam Cohen76fc0852011-06-17 13:26:23 -0700188 sSharedOuterRingDrawable = res.getDrawable(R.drawable.portal_ring_outer_holo);
Adam Cohen76fc0852011-06-17 13:26:23 -0700189 sSharedInnerRingDrawable = res.getDrawable(R.drawable.portal_ring_inner_holo);
Adam Cohendf6af572011-10-19 14:38:16 -0700190 sSharedFolderLeaveBehind = res.getDrawable(R.drawable.portal_ring_rest);
Adam Cohen099f60d2011-08-23 21:07:26 -0700191 sStaticValuesDirty = false;
Adam Cohen19072da2011-05-31 14:30:45 -0700192 }
Adam Cohen073a46f2011-05-17 16:28:09 -0700193 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700194
Adam Cohen19072da2011-05-31 14:30:45 -0700195 public void animateToAcceptState() {
Adam Cohenc0dcf592011-06-01 15:30:43 -0700196 if (mNeutralAnimator != null) {
197 mNeutralAnimator.cancel();
198 }
199 mAcceptAnimator = ValueAnimator.ofFloat(0f, 1f);
200 mAcceptAnimator.setDuration(CONSUMPTION_ANIMATION_DURATION);
Andrew Flynn850d2e72012-04-26 16:51:20 -0700201
202 final int previewSize = sPreviewSize;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700203 mAcceptAnimator.addUpdateListener(new AnimatorUpdateListener() {
Adam Cohen19072da2011-05-31 14:30:45 -0700204 public void onAnimationUpdate(ValueAnimator animation) {
205 final float percent = (Float) animation.getAnimatedValue();
Andrew Flynn850d2e72012-04-26 16:51:20 -0700206 mOuterRingSize = (1 + percent * OUTER_RING_GROWTH_FACTOR) * previewSize;
207 mInnerRingSize = (1 + percent * INNER_RING_GROWTH_FACTOR) * previewSize;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700208 if (mCellLayout != null) {
209 mCellLayout.invalidate();
Adam Cohen19072da2011-05-31 14:30:45 -0700210 }
211 }
212 });
Adam Cohenc0dcf592011-06-01 15:30:43 -0700213 mAcceptAnimator.addListener(new AnimatorListenerAdapter() {
Adam Cohen19072da2011-05-31 14:30:45 -0700214 @Override
Adam Cohenc0dcf592011-06-01 15:30:43 -0700215 public void onAnimationStart(Animator animation) {
Adam Cohen19072da2011-05-31 14:30:45 -0700216 if (mFolderIcon != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700217 mFolderIcon.mPreviewBackground.setVisibility(INVISIBLE);
Adam Cohen19072da2011-05-31 14:30:45 -0700218 }
219 }
220 });
Adam Cohenc0dcf592011-06-01 15:30:43 -0700221 mAcceptAnimator.start();
Adam Cohen19072da2011-05-31 14:30:45 -0700222 }
223
224 public void animateToNaturalState() {
Adam Cohenc0dcf592011-06-01 15:30:43 -0700225 if (mAcceptAnimator != null) {
226 mAcceptAnimator.cancel();
227 }
228 mNeutralAnimator = ValueAnimator.ofFloat(0f, 1f);
229 mNeutralAnimator.setDuration(CONSUMPTION_ANIMATION_DURATION);
Andrew Flynn850d2e72012-04-26 16:51:20 -0700230
231 final int previewSize = sPreviewSize;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700232 mNeutralAnimator.addUpdateListener(new AnimatorUpdateListener() {
Adam Cohen19072da2011-05-31 14:30:45 -0700233 public void onAnimationUpdate(ValueAnimator animation) {
234 final float percent = (Float) animation.getAnimatedValue();
Andrew Flynn850d2e72012-04-26 16:51:20 -0700235 mOuterRingSize = (1 + (1 - percent) * OUTER_RING_GROWTH_FACTOR) * previewSize;
236 mInnerRingSize = (1 + (1 - percent) * INNER_RING_GROWTH_FACTOR) * previewSize;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700237 if (mCellLayout != null) {
238 mCellLayout.invalidate();
Adam Cohen19072da2011-05-31 14:30:45 -0700239 }
240 }
241 });
Adam Cohenc0dcf592011-06-01 15:30:43 -0700242 mNeutralAnimator.addListener(new AnimatorListenerAdapter() {
Adam Cohen19072da2011-05-31 14:30:45 -0700243 @Override
244 public void onAnimationEnd(Animator animation) {
Adam Cohen69ce2e52011-07-03 19:25:21 -0700245 if (mCellLayout != null) {
246 mCellLayout.hideFolderAccept(FolderRingAnimator.this);
247 }
Adam Cohen19072da2011-05-31 14:30:45 -0700248 if (mFolderIcon != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700249 mFolderIcon.mPreviewBackground.setVisibility(VISIBLE);
Adam Cohen19072da2011-05-31 14:30:45 -0700250 }
Adam Cohen19072da2011-05-31 14:30:45 -0700251 }
252 });
Adam Cohenc0dcf592011-06-01 15:30:43 -0700253 mNeutralAnimator.start();
Adam Cohen19072da2011-05-31 14:30:45 -0700254 }
255
Adam Cohenc0dcf592011-06-01 15:30:43 -0700256 // Location is expressed in window coordinates
Adam Cohen69ce2e52011-07-03 19:25:21 -0700257 public void getCell(int[] loc) {
258 loc[0] = mCellX;
259 loc[1] = mCellY;
260 }
261
262 // Location is expressed in window coordinates
263 public void setCell(int x, int y) {
264 mCellX = x;
265 mCellY = y;
266 }
267
268 public void setCellLayout(CellLayout layout) {
269 mCellLayout = layout;
Adam Cohen19072da2011-05-31 14:30:45 -0700270 }
271
Adam Cohen76fc0852011-06-17 13:26:23 -0700272 public float getOuterRingSize() {
273 return mOuterRingSize;
Adam Cohen19072da2011-05-31 14:30:45 -0700274 }
275
Adam Cohen76fc0852011-06-17 13:26:23 -0700276 public float getInnerRingSize() {
277 return mInnerRingSize;
Adam Cohen19072da2011-05-31 14:30:45 -0700278 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800279 }
280
Adam Cohen073a46f2011-05-17 16:28:09 -0700281 private boolean willAcceptItem(ItemInfo item) {
282 final int itemType = item.itemType;
283 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
284 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
Adam Cohenc51934b2011-07-26 21:07:43 -0700285 !mFolder.isFull() && item != mInfo && !mInfo.opened);
Adam Cohen073a46f2011-05-17 16:28:09 -0700286 }
287
Adam Cohenc0dcf592011-06-01 15:30:43 -0700288 public boolean acceptDrop(Object dragInfo) {
289 final ItemInfo item = (ItemInfo) dragInfo;
Adam Cohen073a46f2011-05-17 16:28:09 -0700290 return willAcceptItem(item);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800291 }
292
Adam Cohendf035382011-04-11 17:22:04 -0700293 public void addItem(ShortcutInfo item) {
294 mInfo.add(item);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700295 LauncherModel.addOrMoveItemInDatabase(mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
Adam Cohendf035382011-04-11 17:22:04 -0700296 }
297
Adam Cohenc0dcf592011-06-01 15:30:43 -0700298 public void onDragEnter(Object dragInfo) {
299 if (!willAcceptItem((ItemInfo) dragInfo)) return;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700300 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) getLayoutParams();
301 CellLayout layout = (CellLayout) getParent().getParent();
302 mFolderRingAnimator.setCell(lp.cellX, lp.cellY);
303 mFolderRingAnimator.setCellLayout(layout);
Adam Cohen19072da2011-05-31 14:30:45 -0700304 mFolderRingAnimator.animateToAcceptState();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700305 layout.showFolderAccept(mFolderRingAnimator);
Adam Cohen073a46f2011-05-17 16:28:09 -0700306 }
307
Adam Cohenc0dcf592011-06-01 15:30:43 -0700308 public void onDragOver(Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800309 }
310
Adam Cohend0445262011-07-04 23:53:22 -0700311 public void performCreateAnimation(final ShortcutInfo destInfo, final View destView,
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800312 final ShortcutInfo srcInfo, final DragView srcView, Rect dstRect,
Adam Cohenac8c8762011-07-13 11:15:27 -0700313 float scaleRelativeToDragLayer, Runnable postAnimationRunnable) {
Adam Cohend0445262011-07-04 23:53:22 -0700314
315 Drawable animateDrawable = ((TextView) destView).getCompoundDrawables()[1];
316 computePreviewDrawingParams(animateDrawable.getIntrinsicWidth(), destView.getMeasuredWidth());
Adam Cohenac8c8762011-07-13 11:15:27 -0700317
Adam Cohend0445262011-07-04 23:53:22 -0700318 // This will animate the dragView (srcView) into the new folder
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800319 onDrop(srcInfo, srcView, dstRect, scaleRelativeToDragLayer, 1, postAnimationRunnable, null);
Adam Cohend0445262011-07-04 23:53:22 -0700320
321 // This will animate the first item from it's position as an icon into its
322 // position as the first item in the preview
Adam Cohen8dfcba42011-07-07 16:38:18 -0700323 animateFirstItem(animateDrawable, INITIAL_ITEM_ANIMATION_DURATION);
Adam Cohend0445262011-07-04 23:53:22 -0700324
325 postDelayed(new Runnable() {
326 public void run() {
327 addItem(destInfo);
328 }
329 }, INITIAL_ITEM_ANIMATION_DURATION);
330 }
331
Adam Cohenc0dcf592011-06-01 15:30:43 -0700332 public void onDragExit(Object dragInfo) {
Adam Cohenc6cc61d2012-04-04 12:47:08 -0700333 onDragExit();
334 }
335
336 public void onDragExit() {
Adam Cohen19072da2011-05-31 14:30:45 -0700337 mFolderRingAnimator.animateToNaturalState();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800338 }
Patrick Dubroy440c3602010-07-13 17:50:32 -0700339
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800340 private void onDrop(final ShortcutInfo item, DragView animateView, Rect finalRect,
341 float scaleRelativeToDragLayer, int index, Runnable postAnimationRunnable,
342 DragObject d) {
Adam Cohend0445262011-07-04 23:53:22 -0700343 item.cellX = -1;
344 item.cellY = -1;
Adam Cohend0445262011-07-04 23:53:22 -0700345
Adam Cohen558baaf2011-08-15 15:22:57 -0700346 // Typically, the animateView corresponds to the DragView; however, if this is being done
347 // after a configuration activity (ie. for a Shortcut being dragged from AllApps) we
348 // will not have a view to animate
349 if (animateView != null) {
350 DragLayer dragLayer = mLauncher.getDragLayer();
351 Rect from = new Rect();
352 dragLayer.getViewRectRelativeToSelf(animateView, from);
353 Rect to = finalRect;
354 if (to == null) {
355 to = new Rect();
356 Workspace workspace = mLauncher.getWorkspace();
357 // Set cellLayout and this to it's final state to compute final animation locations
358 workspace.setFinalTransitionTransform((CellLayout) getParent().getParent());
359 float scaleX = getScaleX();
360 float scaleY = getScaleY();
361 setScaleX(1.0f);
362 setScaleY(1.0f);
363 scaleRelativeToDragLayer = dragLayer.getDescendantRectRelativeToSelf(this, to);
364 // Finished computing final animation locations, restore current state
365 setScaleX(scaleX);
366 setScaleY(scaleY);
367 workspace.resetTransitionTransform((CellLayout) getParent().getParent());
Adam Cohend0445262011-07-04 23:53:22 -0700368 }
Adam Cohen558baaf2011-08-15 15:22:57 -0700369
370 int[] center = new int[2];
371 float scale = getLocalCenterForIndex(index, center);
372 center[0] = (int) Math.round(scaleRelativeToDragLayer * center[0]);
373 center[1] = (int) Math.round(scaleRelativeToDragLayer * center[1]);
374
375 to.offset(center[0] - animateView.getMeasuredWidth() / 2,
376 center[1] - animateView.getMeasuredHeight() / 2);
377
378 float finalAlpha = index < NUM_ITEMS_IN_PREVIEW ? 0.5f : 0f;
379
Adam Cohened66b2b2012-01-23 17:28:51 -0800380 float finalScale = scale * scaleRelativeToDragLayer;
Adam Cohen558baaf2011-08-15 15:22:57 -0700381 dragLayer.animateView(animateView, from, to, finalAlpha,
Adam Cohened66b2b2012-01-23 17:28:51 -0800382 1, 1, finalScale, finalScale, DROP_IN_ANIMATION_DURATION,
Adam Cohen558baaf2011-08-15 15:22:57 -0700383 new DecelerateInterpolator(2), new AccelerateInterpolator(2),
Adam Cohened66b2b2012-01-23 17:28:51 -0800384 postAnimationRunnable, DragLayer.ANIMATION_END_DISAPPEAR, null);
Adam Cohen558baaf2011-08-15 15:22:57 -0700385 postDelayed(new Runnable() {
386 public void run() {
387 addItem(item);
388 }
389 }, DROP_IN_ANIMATION_DURATION);
390 } else {
391 addItem(item);
392 }
Adam Cohend0445262011-07-04 23:53:22 -0700393 }
394
Adam Cohen3e8f8112011-07-02 18:03:00 -0700395 public void onDrop(DragObject d) {
Adam Cohenc0dcf592011-06-01 15:30:43 -0700396 ShortcutInfo item;
Adam Cohen3e8f8112011-07-02 18:03:00 -0700397 if (d.dragInfo instanceof ApplicationInfo) {
Adam Cohenc0dcf592011-06-01 15:30:43 -0700398 // Came from all apps -- make a copy
Adam Cohen3e8f8112011-07-02 18:03:00 -0700399 item = ((ApplicationInfo) d.dragInfo).makeShortcut();
Adam Cohenc0dcf592011-06-01 15:30:43 -0700400 } else {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700401 item = (ShortcutInfo) d.dragInfo;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700402 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700403 mFolder.notifyDrop();
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800404 onDrop(item, d.dragView, null, 1.0f, mInfo.contents.size(), d.postAnimationRunnable, d);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700405 }
406
Adam Cohencb3382b2011-05-24 14:07:08 -0700407 public DropTarget getDropTargetDelegate(DragObject d) {
Patrick Dubroy440c3602010-07-13 17:50:32 -0700408 return null;
409 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700410
Adam Cohend0445262011-07-04 23:53:22 -0700411 private void computePreviewDrawingParams(int drawableSize, int totalSize) {
412 if (mIntrinsicIconSize != drawableSize || mTotalWidth != totalSize) {
413 mIntrinsicIconSize = drawableSize;
414 mTotalWidth = totalSize;
415
416 final int previewSize = FolderRingAnimator.sPreviewSize;
417 final int previewPadding = FolderRingAnimator.sPreviewPadding;
418
419 mAvailableSpaceInPreview = (previewSize - 2 * previewPadding);
420 // cos(45) = 0.707 + ~= 0.1) = 0.8f
421 int adjustedAvailableSpace = (int) ((mAvailableSpaceInPreview / 2) * (1 + 0.8f));
422
423 int unscaledHeight = (int) (mIntrinsicIconSize * (1 + PERSPECTIVE_SHIFT_FACTOR));
424 mBaselineIconScale = (1.0f * adjustedAvailableSpace / unscaledHeight);
425
426 mBaselineIconSize = (int) (mIntrinsicIconSize * mBaselineIconScale);
427 mMaxPerspectiveShift = mBaselineIconSize * PERSPECTIVE_SHIFT_FACTOR;
428
429 mPreviewOffsetX = (mTotalWidth - mAvailableSpaceInPreview) / 2;
430 mPreviewOffsetY = previewPadding;
431 }
432 }
433
434 private void computePreviewDrawingParams(Drawable d) {
435 computePreviewDrawingParams(d.getIntrinsicWidth(), getMeasuredWidth());
436 }
437
438 class PreviewItemDrawingParams {
439 PreviewItemDrawingParams(float transX, float transY, float scale, int overlayAlpha) {
440 this.transX = transX;
441 this.transY = transY;
442 this.scale = scale;
443 this.overlayAlpha = overlayAlpha;
444 }
445 float transX;
446 float transY;
447 float scale;
448 int overlayAlpha;
449 Drawable drawable;
450 }
451
Adam Cohenac8c8762011-07-13 11:15:27 -0700452 private float getLocalCenterForIndex(int index, int[] center) {
Adam Cohend0445262011-07-04 23:53:22 -0700453 mParams = computePreviewItemDrawingParams(Math.min(NUM_ITEMS_IN_PREVIEW, index), mParams);
454
455 mParams.transX += mPreviewOffsetX;
456 mParams.transY += mPreviewOffsetY;
Adam Cohenac8c8762011-07-13 11:15:27 -0700457 float offsetX = mParams.transX + (mParams.scale * mIntrinsicIconSize) / 2;
458 float offsetY = mParams.transY + (mParams.scale * mIntrinsicIconSize) / 2;
Adam Cohend0445262011-07-04 23:53:22 -0700459
Adam Cohenac8c8762011-07-13 11:15:27 -0700460 center[0] = (int) Math.round(offsetX);
461 center[1] = (int) Math.round(offsetY);
Adam Cohend0445262011-07-04 23:53:22 -0700462 return mParams.scale;
463 }
464
465 private PreviewItemDrawingParams computePreviewItemDrawingParams(int index,
466 PreviewItemDrawingParams params) {
467 index = NUM_ITEMS_IN_PREVIEW - index - 1;
468 float r = (index * 1.0f) / (NUM_ITEMS_IN_PREVIEW - 1);
469 float scale = (1 - PERSPECTIVE_SCALE_FACTOR * (1 - r));
470
471 float offset = (1 - r) * mMaxPerspectiveShift;
472 float scaledSize = scale * mBaselineIconSize;
473 float scaleOffsetCorrection = (1 - scale) * mBaselineIconSize;
474
475 // We want to imagine our coordinates from the bottom left, growing up and to the
476 // right. This is natural for the x-axis, but for the y-axis, we have to invert things.
477 float transY = mAvailableSpaceInPreview - (offset + scaledSize + scaleOffsetCorrection);
478 float transX = offset + scaleOffsetCorrection;
479 float totalScale = mBaselineIconScale * scale;
480 final int overlayAlpha = (int) (80 * (1 - r));
481
482 if (params == null) {
483 params = new PreviewItemDrawingParams(transX, transY, totalScale, overlayAlpha);
484 } else {
485 params.transX = transX;
486 params.transY = transY;
487 params.scale = totalScale;
488 params.overlayAlpha = overlayAlpha;
489 }
490 return params;
491 }
492
493 private void drawPreviewItem(Canvas canvas, PreviewItemDrawingParams params) {
494 canvas.save();
495 canvas.translate(params.transX + mPreviewOffsetX, params.transY + mPreviewOffsetY);
496 canvas.scale(params.scale, params.scale);
497 Drawable d = params.drawable;
498
499 if (d != null) {
500 d.setBounds(0, 0, mIntrinsicIconSize, mIntrinsicIconSize);
501 d.setFilterBitmap(true);
502 d.setColorFilter(Color.argb(params.overlayAlpha, 0, 0, 0), PorterDuff.Mode.SRC_ATOP);
503 d.draw(canvas);
504 d.clearColorFilter();
505 d.setFilterBitmap(false);
506 }
507 canvas.restore();
508 }
509
Adam Cohena9cf38f2011-05-02 15:36:58 -0700510 @Override
Adam Cohenc0dcf592011-06-01 15:30:43 -0700511 protected void dispatchDraw(Canvas canvas) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700512 super.dispatchDraw(canvas);
513
Adam Cohena9cf38f2011-05-02 15:36:58 -0700514 if (mFolder == null) return;
Adam Cohend0445262011-07-04 23:53:22 -0700515 if (mFolder.getItemCount() == 0 && !mAnimating) return;
Adam Cohena9cf38f2011-05-02 15:36:58 -0700516
Adam Cohen76078c42011-06-09 15:06:52 -0700517 ArrayList<View> items = mFolder.getItemsInReadingOrder(false);
Adam Cohend0445262011-07-04 23:53:22 -0700518 Drawable d;
519 TextView v;
Adam Cohenbadf71e2011-05-26 19:08:29 -0700520
Adam Cohend0445262011-07-04 23:53:22 -0700521 // Update our drawing parameters if necessary
522 if (mAnimating) {
523 computePreviewDrawingParams(mAnimParams.drawable);
524 } else {
525 v = (TextView) items.get(0);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700526 d = v.getCompoundDrawables()[1];
Adam Cohend0445262011-07-04 23:53:22 -0700527 computePreviewDrawingParams(d);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700528 }
Adam Cohend0445262011-07-04 23:53:22 -0700529
530 int nItemsInPreview = Math.min(items.size(), NUM_ITEMS_IN_PREVIEW);
531 if (!mAnimating) {
532 for (int i = nItemsInPreview - 1; i >= 0; i--) {
533 v = (TextView) items.get(i);
534 d = v.getCompoundDrawables()[1];
535
536 mParams = computePreviewItemDrawingParams(i, mParams);
537 mParams.drawable = d;
538 drawPreviewItem(canvas, mParams);
539 }
540 } else {
541 drawPreviewItem(canvas, mAnimParams);
542 }
543 }
544
545 private void animateFirstItem(final Drawable d, int duration) {
546 computePreviewDrawingParams(d);
547 final PreviewItemDrawingParams finalParams = computePreviewItemDrawingParams(0, null);
548
549 final float scale0 = 1.0f;
Adam Cohen1d4ee4e2011-08-12 15:51:59 -0700550 final float transX0 = (mAvailableSpaceInPreview - d.getIntrinsicWidth()) / 2;
551 final float transY0 = (mAvailableSpaceInPreview - d.getIntrinsicHeight()) / 2;
Adam Cohend0445262011-07-04 23:53:22 -0700552 mAnimParams.drawable = d;
553
554 ValueAnimator va = ValueAnimator.ofFloat(0f, 1.0f);
555 va.addUpdateListener(new AnimatorUpdateListener(){
556 public void onAnimationUpdate(ValueAnimator animation) {
557 float progress = (Float) animation.getAnimatedValue();
558
559 mAnimParams.transX = transX0 + progress * (finalParams.transX - transX0);
560 mAnimParams.transY = transY0 + progress * (finalParams.transY - transY0);
561 mAnimParams.scale = scale0 + progress * (finalParams.scale - scale0);
562 invalidate();
563 }
564 });
565 va.addListener(new AnimatorListenerAdapter() {
566 @Override
567 public void onAnimationStart(Animator animation) {
568 mAnimating = true;
569 }
570 @Override
571 public void onAnimationEnd(Animator animation) {
572 mAnimating = false;
573 }
574 });
575 va.setDuration(duration);
576 va.start();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700577 }
578
Adam Cohen099f60d2011-08-23 21:07:26 -0700579 public void setTextVisible(boolean visible) {
580 if (visible) {
581 mFolderName.setVisibility(VISIBLE);
582 } else {
583 mFolderName.setVisibility(INVISIBLE);
584 }
585 }
586
587 public boolean getTextVisible() {
588 return mFolderName.getVisibility() == VISIBLE;
589 }
590
Adam Cohen76078c42011-06-09 15:06:52 -0700591 public void onItemsChanged() {
592 invalidate();
593 requestLayout();
594 }
595
Adam Cohena9cf38f2011-05-02 15:36:58 -0700596 public void onAdd(ShortcutInfo item) {
597 invalidate();
598 requestLayout();
599 }
600
601 public void onRemove(ShortcutInfo item) {
602 invalidate();
603 requestLayout();
604 }
Adam Cohen76fc0852011-06-17 13:26:23 -0700605
606 public void onTitleChanged(CharSequence title) {
Adam Cohend63cfa92011-06-24 14:17:17 -0700607 mFolderName.setText(title.toString());
Michael Jurka8b805b12012-04-18 14:23:14 -0700608 setContentDescription(String.format(getContext().getString(R.string.folder_name_format),
Adam Cohen3371da02011-10-25 21:38:29 -0700609 title));
Adam Cohen76fc0852011-06-17 13:26:23 -0700610 }
Winson Chung88f33452012-02-23 15:23:44 -0800611
612 @Override
613 public boolean onTouchEvent(MotionEvent event) {
614 // Call the superclass onTouchEvent first, because sometimes it changes the state to
615 // isPressed() on an ACTION_UP
616 boolean result = super.onTouchEvent(event);
617
618 switch (event.getAction()) {
619 case MotionEvent.ACTION_DOWN:
620 mLongPressHelper.postCheckForLongPress();
621 break;
622 case MotionEvent.ACTION_CANCEL:
623 case MotionEvent.ACTION_UP:
624 mLongPressHelper.cancelLongPress();
625 break;
626 }
627 return result;
628 }
629
630 @Override
631 public void cancelLongPress() {
632 super.cancelLongPress();
633
634 mLongPressHelper.cancelLongPress();
635 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800636}