blob: 2377f3008ba385c86320eb517abfafb3bf704509 [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);
201 mAcceptAnimator.addUpdateListener(new AnimatorUpdateListener() {
Adam Cohen19072da2011-05-31 14:30:45 -0700202 public void onAnimationUpdate(ValueAnimator animation) {
203 final float percent = (Float) animation.getAnimatedValue();
Adam Cohen76fc0852011-06-17 13:26:23 -0700204 mOuterRingSize = (1 + percent * OUTER_RING_GROWTH_FACTOR) * sPreviewSize;
205 mInnerRingSize = (1 + percent * INNER_RING_GROWTH_FACTOR) * sPreviewSize;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700206 if (mCellLayout != null) {
207 mCellLayout.invalidate();
Adam Cohen19072da2011-05-31 14:30:45 -0700208 }
209 }
210 });
Adam Cohenc0dcf592011-06-01 15:30:43 -0700211 mAcceptAnimator.addListener(new AnimatorListenerAdapter() {
Adam Cohen19072da2011-05-31 14:30:45 -0700212 @Override
Adam Cohenc0dcf592011-06-01 15:30:43 -0700213 public void onAnimationStart(Animator animation) {
Adam Cohen19072da2011-05-31 14:30:45 -0700214 if (mFolderIcon != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700215 mFolderIcon.mPreviewBackground.setVisibility(INVISIBLE);
Adam Cohen19072da2011-05-31 14:30:45 -0700216 }
217 }
218 });
Adam Cohenc0dcf592011-06-01 15:30:43 -0700219 mAcceptAnimator.start();
Adam Cohen19072da2011-05-31 14:30:45 -0700220 }
221
222 public void animateToNaturalState() {
Adam Cohenc0dcf592011-06-01 15:30:43 -0700223 if (mAcceptAnimator != null) {
224 mAcceptAnimator.cancel();
225 }
226 mNeutralAnimator = ValueAnimator.ofFloat(0f, 1f);
227 mNeutralAnimator.setDuration(CONSUMPTION_ANIMATION_DURATION);
228 mNeutralAnimator.addUpdateListener(new AnimatorUpdateListener() {
Adam Cohen19072da2011-05-31 14:30:45 -0700229 public void onAnimationUpdate(ValueAnimator animation) {
230 final float percent = (Float) animation.getAnimatedValue();
Adam Cohen76fc0852011-06-17 13:26:23 -0700231 mOuterRingSize = (1 + (1 - percent) * OUTER_RING_GROWTH_FACTOR) * sPreviewSize;
232 mInnerRingSize = (1 + (1 - percent) * INNER_RING_GROWTH_FACTOR) * sPreviewSize;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700233 if (mCellLayout != null) {
234 mCellLayout.invalidate();
Adam Cohen19072da2011-05-31 14:30:45 -0700235 }
236 }
237 });
Adam Cohenc0dcf592011-06-01 15:30:43 -0700238 mNeutralAnimator.addListener(new AnimatorListenerAdapter() {
Adam Cohen19072da2011-05-31 14:30:45 -0700239 @Override
240 public void onAnimationEnd(Animator animation) {
Adam Cohen69ce2e52011-07-03 19:25:21 -0700241 if (mCellLayout != null) {
242 mCellLayout.hideFolderAccept(FolderRingAnimator.this);
243 }
Adam Cohen19072da2011-05-31 14:30:45 -0700244 if (mFolderIcon != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700245 mFolderIcon.mPreviewBackground.setVisibility(VISIBLE);
Adam Cohen19072da2011-05-31 14:30:45 -0700246 }
Adam Cohen19072da2011-05-31 14:30:45 -0700247 }
248 });
Adam Cohenc0dcf592011-06-01 15:30:43 -0700249 mNeutralAnimator.start();
Adam Cohen19072da2011-05-31 14:30:45 -0700250 }
251
Adam Cohenc0dcf592011-06-01 15:30:43 -0700252 // Location is expressed in window coordinates
Adam Cohen69ce2e52011-07-03 19:25:21 -0700253 public void getCell(int[] loc) {
254 loc[0] = mCellX;
255 loc[1] = mCellY;
256 }
257
258 // Location is expressed in window coordinates
259 public void setCell(int x, int y) {
260 mCellX = x;
261 mCellY = y;
262 }
263
264 public void setCellLayout(CellLayout layout) {
265 mCellLayout = layout;
Adam Cohen19072da2011-05-31 14:30:45 -0700266 }
267
Adam Cohen76fc0852011-06-17 13:26:23 -0700268 public float getOuterRingSize() {
269 return mOuterRingSize;
Adam Cohen19072da2011-05-31 14:30:45 -0700270 }
271
Adam Cohen76fc0852011-06-17 13:26:23 -0700272 public float getInnerRingSize() {
273 return mInnerRingSize;
Adam Cohen19072da2011-05-31 14:30:45 -0700274 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800275 }
276
Adam Cohen073a46f2011-05-17 16:28:09 -0700277 private boolean willAcceptItem(ItemInfo item) {
278 final int itemType = item.itemType;
279 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
280 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
Adam Cohenc51934b2011-07-26 21:07:43 -0700281 !mFolder.isFull() && item != mInfo && !mInfo.opened);
Adam Cohen073a46f2011-05-17 16:28:09 -0700282 }
283
Adam Cohenc0dcf592011-06-01 15:30:43 -0700284 public boolean acceptDrop(Object dragInfo) {
285 final ItemInfo item = (ItemInfo) dragInfo;
Adam Cohen073a46f2011-05-17 16:28:09 -0700286 return willAcceptItem(item);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800287 }
288
Adam Cohendf035382011-04-11 17:22:04 -0700289 public void addItem(ShortcutInfo item) {
290 mInfo.add(item);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700291 LauncherModel.addOrMoveItemInDatabase(mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
Adam Cohendf035382011-04-11 17:22:04 -0700292 }
293
Adam Cohenc0dcf592011-06-01 15:30:43 -0700294 public void onDragEnter(Object dragInfo) {
295 if (!willAcceptItem((ItemInfo) dragInfo)) return;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700296 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) getLayoutParams();
297 CellLayout layout = (CellLayout) getParent().getParent();
298 mFolderRingAnimator.setCell(lp.cellX, lp.cellY);
299 mFolderRingAnimator.setCellLayout(layout);
Adam Cohen19072da2011-05-31 14:30:45 -0700300 mFolderRingAnimator.animateToAcceptState();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700301 layout.showFolderAccept(mFolderRingAnimator);
Adam Cohen073a46f2011-05-17 16:28:09 -0700302 }
303
Adam Cohenc0dcf592011-06-01 15:30:43 -0700304 public void onDragOver(Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800305 }
306
Adam Cohend0445262011-07-04 23:53:22 -0700307 public void performCreateAnimation(final ShortcutInfo destInfo, final View destView,
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800308 final ShortcutInfo srcInfo, final DragView srcView, Rect dstRect,
Adam Cohenac8c8762011-07-13 11:15:27 -0700309 float scaleRelativeToDragLayer, Runnable postAnimationRunnable) {
Adam Cohend0445262011-07-04 23:53:22 -0700310
311 Drawable animateDrawable = ((TextView) destView).getCompoundDrawables()[1];
312 computePreviewDrawingParams(animateDrawable.getIntrinsicWidth(), destView.getMeasuredWidth());
Adam Cohenac8c8762011-07-13 11:15:27 -0700313
Adam Cohend0445262011-07-04 23:53:22 -0700314 // This will animate the dragView (srcView) into the new folder
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800315 onDrop(srcInfo, srcView, dstRect, scaleRelativeToDragLayer, 1, postAnimationRunnable, null);
Adam Cohend0445262011-07-04 23:53:22 -0700316
317 // This will animate the first item from it's position as an icon into its
318 // position as the first item in the preview
Adam Cohen8dfcba42011-07-07 16:38:18 -0700319 animateFirstItem(animateDrawable, INITIAL_ITEM_ANIMATION_DURATION);
Adam Cohend0445262011-07-04 23:53:22 -0700320
321 postDelayed(new Runnable() {
322 public void run() {
323 addItem(destInfo);
324 }
325 }, INITIAL_ITEM_ANIMATION_DURATION);
326 }
327
Adam Cohenc0dcf592011-06-01 15:30:43 -0700328 public void onDragExit(Object dragInfo) {
Adam Cohenc6cc61d2012-04-04 12:47:08 -0700329 onDragExit();
330 }
331
332 public void onDragExit() {
Adam Cohen19072da2011-05-31 14:30:45 -0700333 mFolderRingAnimator.animateToNaturalState();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800334 }
Patrick Dubroy440c3602010-07-13 17:50:32 -0700335
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800336 private void onDrop(final ShortcutInfo item, DragView animateView, Rect finalRect,
337 float scaleRelativeToDragLayer, int index, Runnable postAnimationRunnable,
338 DragObject d) {
Adam Cohend0445262011-07-04 23:53:22 -0700339 item.cellX = -1;
340 item.cellY = -1;
Adam Cohend0445262011-07-04 23:53:22 -0700341
Adam Cohen558baaf2011-08-15 15:22:57 -0700342 // Typically, the animateView corresponds to the DragView; however, if this is being done
343 // after a configuration activity (ie. for a Shortcut being dragged from AllApps) we
344 // will not have a view to animate
345 if (animateView != null) {
346 DragLayer dragLayer = mLauncher.getDragLayer();
347 Rect from = new Rect();
348 dragLayer.getViewRectRelativeToSelf(animateView, from);
349 Rect to = finalRect;
350 if (to == null) {
351 to = new Rect();
352 Workspace workspace = mLauncher.getWorkspace();
353 // Set cellLayout and this to it's final state to compute final animation locations
354 workspace.setFinalTransitionTransform((CellLayout) getParent().getParent());
355 float scaleX = getScaleX();
356 float scaleY = getScaleY();
357 setScaleX(1.0f);
358 setScaleY(1.0f);
359 scaleRelativeToDragLayer = dragLayer.getDescendantRectRelativeToSelf(this, to);
360 // Finished computing final animation locations, restore current state
361 setScaleX(scaleX);
362 setScaleY(scaleY);
363 workspace.resetTransitionTransform((CellLayout) getParent().getParent());
Adam Cohend0445262011-07-04 23:53:22 -0700364 }
Adam Cohen558baaf2011-08-15 15:22:57 -0700365
366 int[] center = new int[2];
367 float scale = getLocalCenterForIndex(index, center);
368 center[0] = (int) Math.round(scaleRelativeToDragLayer * center[0]);
369 center[1] = (int) Math.round(scaleRelativeToDragLayer * center[1]);
370
371 to.offset(center[0] - animateView.getMeasuredWidth() / 2,
372 center[1] - animateView.getMeasuredHeight() / 2);
373
374 float finalAlpha = index < NUM_ITEMS_IN_PREVIEW ? 0.5f : 0f;
375
Adam Cohened66b2b2012-01-23 17:28:51 -0800376 float finalScale = scale * scaleRelativeToDragLayer;
Adam Cohen558baaf2011-08-15 15:22:57 -0700377 dragLayer.animateView(animateView, from, to, finalAlpha,
Adam Cohened66b2b2012-01-23 17:28:51 -0800378 1, 1, finalScale, finalScale, DROP_IN_ANIMATION_DURATION,
Adam Cohen558baaf2011-08-15 15:22:57 -0700379 new DecelerateInterpolator(2), new AccelerateInterpolator(2),
Adam Cohened66b2b2012-01-23 17:28:51 -0800380 postAnimationRunnable, DragLayer.ANIMATION_END_DISAPPEAR, null);
Adam Cohen558baaf2011-08-15 15:22:57 -0700381 postDelayed(new Runnable() {
382 public void run() {
383 addItem(item);
384 }
385 }, DROP_IN_ANIMATION_DURATION);
386 } else {
387 addItem(item);
388 }
Adam Cohend0445262011-07-04 23:53:22 -0700389 }
390
Adam Cohen3e8f8112011-07-02 18:03:00 -0700391 public void onDrop(DragObject d) {
Adam Cohenc0dcf592011-06-01 15:30:43 -0700392 ShortcutInfo item;
Adam Cohen3e8f8112011-07-02 18:03:00 -0700393 if (d.dragInfo instanceof ApplicationInfo) {
Adam Cohenc0dcf592011-06-01 15:30:43 -0700394 // Came from all apps -- make a copy
Adam Cohen3e8f8112011-07-02 18:03:00 -0700395 item = ((ApplicationInfo) d.dragInfo).makeShortcut();
Adam Cohenc0dcf592011-06-01 15:30:43 -0700396 } else {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700397 item = (ShortcutInfo) d.dragInfo;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700398 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700399 mFolder.notifyDrop();
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800400 onDrop(item, d.dragView, null, 1.0f, mInfo.contents.size(), d.postAnimationRunnable, d);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700401 }
402
Adam Cohencb3382b2011-05-24 14:07:08 -0700403 public DropTarget getDropTargetDelegate(DragObject d) {
Patrick Dubroy440c3602010-07-13 17:50:32 -0700404 return null;
405 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700406
Adam Cohend0445262011-07-04 23:53:22 -0700407 private void computePreviewDrawingParams(int drawableSize, int totalSize) {
408 if (mIntrinsicIconSize != drawableSize || mTotalWidth != totalSize) {
409 mIntrinsicIconSize = drawableSize;
410 mTotalWidth = totalSize;
411
412 final int previewSize = FolderRingAnimator.sPreviewSize;
413 final int previewPadding = FolderRingAnimator.sPreviewPadding;
414
415 mAvailableSpaceInPreview = (previewSize - 2 * previewPadding);
416 // cos(45) = 0.707 + ~= 0.1) = 0.8f
417 int adjustedAvailableSpace = (int) ((mAvailableSpaceInPreview / 2) * (1 + 0.8f));
418
419 int unscaledHeight = (int) (mIntrinsicIconSize * (1 + PERSPECTIVE_SHIFT_FACTOR));
420 mBaselineIconScale = (1.0f * adjustedAvailableSpace / unscaledHeight);
421
422 mBaselineIconSize = (int) (mIntrinsicIconSize * mBaselineIconScale);
423 mMaxPerspectiveShift = mBaselineIconSize * PERSPECTIVE_SHIFT_FACTOR;
424
425 mPreviewOffsetX = (mTotalWidth - mAvailableSpaceInPreview) / 2;
426 mPreviewOffsetY = previewPadding;
427 }
428 }
429
430 private void computePreviewDrawingParams(Drawable d) {
431 computePreviewDrawingParams(d.getIntrinsicWidth(), getMeasuredWidth());
432 }
433
434 class PreviewItemDrawingParams {
435 PreviewItemDrawingParams(float transX, float transY, float scale, int overlayAlpha) {
436 this.transX = transX;
437 this.transY = transY;
438 this.scale = scale;
439 this.overlayAlpha = overlayAlpha;
440 }
441 float transX;
442 float transY;
443 float scale;
444 int overlayAlpha;
445 Drawable drawable;
446 }
447
Adam Cohenac8c8762011-07-13 11:15:27 -0700448 private float getLocalCenterForIndex(int index, int[] center) {
Adam Cohend0445262011-07-04 23:53:22 -0700449 mParams = computePreviewItemDrawingParams(Math.min(NUM_ITEMS_IN_PREVIEW, index), mParams);
450
451 mParams.transX += mPreviewOffsetX;
452 mParams.transY += mPreviewOffsetY;
Adam Cohenac8c8762011-07-13 11:15:27 -0700453 float offsetX = mParams.transX + (mParams.scale * mIntrinsicIconSize) / 2;
454 float offsetY = mParams.transY + (mParams.scale * mIntrinsicIconSize) / 2;
Adam Cohend0445262011-07-04 23:53:22 -0700455
Adam Cohenac8c8762011-07-13 11:15:27 -0700456 center[0] = (int) Math.round(offsetX);
457 center[1] = (int) Math.round(offsetY);
Adam Cohend0445262011-07-04 23:53:22 -0700458 return mParams.scale;
459 }
460
461 private PreviewItemDrawingParams computePreviewItemDrawingParams(int index,
462 PreviewItemDrawingParams params) {
463 index = NUM_ITEMS_IN_PREVIEW - index - 1;
464 float r = (index * 1.0f) / (NUM_ITEMS_IN_PREVIEW - 1);
465 float scale = (1 - PERSPECTIVE_SCALE_FACTOR * (1 - r));
466
467 float offset = (1 - r) * mMaxPerspectiveShift;
468 float scaledSize = scale * mBaselineIconSize;
469 float scaleOffsetCorrection = (1 - scale) * mBaselineIconSize;
470
471 // We want to imagine our coordinates from the bottom left, growing up and to the
472 // right. This is natural for the x-axis, but for the y-axis, we have to invert things.
473 float transY = mAvailableSpaceInPreview - (offset + scaledSize + scaleOffsetCorrection);
474 float transX = offset + scaleOffsetCorrection;
475 float totalScale = mBaselineIconScale * scale;
476 final int overlayAlpha = (int) (80 * (1 - r));
477
478 if (params == null) {
479 params = new PreviewItemDrawingParams(transX, transY, totalScale, overlayAlpha);
480 } else {
481 params.transX = transX;
482 params.transY = transY;
483 params.scale = totalScale;
484 params.overlayAlpha = overlayAlpha;
485 }
486 return params;
487 }
488
489 private void drawPreviewItem(Canvas canvas, PreviewItemDrawingParams params) {
490 canvas.save();
491 canvas.translate(params.transX + mPreviewOffsetX, params.transY + mPreviewOffsetY);
492 canvas.scale(params.scale, params.scale);
493 Drawable d = params.drawable;
494
495 if (d != null) {
496 d.setBounds(0, 0, mIntrinsicIconSize, mIntrinsicIconSize);
497 d.setFilterBitmap(true);
498 d.setColorFilter(Color.argb(params.overlayAlpha, 0, 0, 0), PorterDuff.Mode.SRC_ATOP);
499 d.draw(canvas);
500 d.clearColorFilter();
501 d.setFilterBitmap(false);
502 }
503 canvas.restore();
504 }
505
Adam Cohena9cf38f2011-05-02 15:36:58 -0700506 @Override
Adam Cohenc0dcf592011-06-01 15:30:43 -0700507 protected void dispatchDraw(Canvas canvas) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700508 super.dispatchDraw(canvas);
509
Adam Cohena9cf38f2011-05-02 15:36:58 -0700510 if (mFolder == null) return;
Adam Cohend0445262011-07-04 23:53:22 -0700511 if (mFolder.getItemCount() == 0 && !mAnimating) return;
Adam Cohena9cf38f2011-05-02 15:36:58 -0700512
Adam Cohen76078c42011-06-09 15:06:52 -0700513 ArrayList<View> items = mFolder.getItemsInReadingOrder(false);
Adam Cohend0445262011-07-04 23:53:22 -0700514 Drawable d;
515 TextView v;
Adam Cohenbadf71e2011-05-26 19:08:29 -0700516
Adam Cohend0445262011-07-04 23:53:22 -0700517 // Update our drawing parameters if necessary
518 if (mAnimating) {
519 computePreviewDrawingParams(mAnimParams.drawable);
520 } else {
521 v = (TextView) items.get(0);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700522 d = v.getCompoundDrawables()[1];
Adam Cohend0445262011-07-04 23:53:22 -0700523 computePreviewDrawingParams(d);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700524 }
Adam Cohend0445262011-07-04 23:53:22 -0700525
526 int nItemsInPreview = Math.min(items.size(), NUM_ITEMS_IN_PREVIEW);
527 if (!mAnimating) {
528 for (int i = nItemsInPreview - 1; i >= 0; i--) {
529 v = (TextView) items.get(i);
530 d = v.getCompoundDrawables()[1];
531
532 mParams = computePreviewItemDrawingParams(i, mParams);
533 mParams.drawable = d;
534 drawPreviewItem(canvas, mParams);
535 }
536 } else {
537 drawPreviewItem(canvas, mAnimParams);
538 }
539 }
540
541 private void animateFirstItem(final Drawable d, int duration) {
542 computePreviewDrawingParams(d);
543 final PreviewItemDrawingParams finalParams = computePreviewItemDrawingParams(0, null);
544
545 final float scale0 = 1.0f;
Adam Cohen1d4ee4e2011-08-12 15:51:59 -0700546 final float transX0 = (mAvailableSpaceInPreview - d.getIntrinsicWidth()) / 2;
547 final float transY0 = (mAvailableSpaceInPreview - d.getIntrinsicHeight()) / 2;
Adam Cohend0445262011-07-04 23:53:22 -0700548 mAnimParams.drawable = d;
549
550 ValueAnimator va = ValueAnimator.ofFloat(0f, 1.0f);
551 va.addUpdateListener(new AnimatorUpdateListener(){
552 public void onAnimationUpdate(ValueAnimator animation) {
553 float progress = (Float) animation.getAnimatedValue();
554
555 mAnimParams.transX = transX0 + progress * (finalParams.transX - transX0);
556 mAnimParams.transY = transY0 + progress * (finalParams.transY - transY0);
557 mAnimParams.scale = scale0 + progress * (finalParams.scale - scale0);
558 invalidate();
559 }
560 });
561 va.addListener(new AnimatorListenerAdapter() {
562 @Override
563 public void onAnimationStart(Animator animation) {
564 mAnimating = true;
565 }
566 @Override
567 public void onAnimationEnd(Animator animation) {
568 mAnimating = false;
569 }
570 });
571 va.setDuration(duration);
572 va.start();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700573 }
574
Adam Cohen099f60d2011-08-23 21:07:26 -0700575 public void setTextVisible(boolean visible) {
576 if (visible) {
577 mFolderName.setVisibility(VISIBLE);
578 } else {
579 mFolderName.setVisibility(INVISIBLE);
580 }
581 }
582
583 public boolean getTextVisible() {
584 return mFolderName.getVisibility() == VISIBLE;
585 }
586
Adam Cohen76078c42011-06-09 15:06:52 -0700587 public void onItemsChanged() {
588 invalidate();
589 requestLayout();
590 }
591
Adam Cohena9cf38f2011-05-02 15:36:58 -0700592 public void onAdd(ShortcutInfo item) {
593 invalidate();
594 requestLayout();
595 }
596
597 public void onRemove(ShortcutInfo item) {
598 invalidate();
599 requestLayout();
600 }
Adam Cohen76fc0852011-06-17 13:26:23 -0700601
602 public void onTitleChanged(CharSequence title) {
Adam Cohend63cfa92011-06-24 14:17:17 -0700603 mFolderName.setText(title.toString());
Adam Cohen3371da02011-10-25 21:38:29 -0700604 setContentDescription(String.format(mContext.getString(R.string.folder_name_format),
605 title));
Adam Cohen76fc0852011-06-17 13:26:23 -0700606 }
Winson Chung88f33452012-02-23 15:23:44 -0800607
608 @Override
609 public boolean onTouchEvent(MotionEvent event) {
610 // Call the superclass onTouchEvent first, because sometimes it changes the state to
611 // isPressed() on an ACTION_UP
612 boolean result = super.onTouchEvent(event);
613
614 switch (event.getAction()) {
615 case MotionEvent.ACTION_DOWN:
616 mLongPressHelper.postCheckForLongPress();
617 break;
618 case MotionEvent.ACTION_CANCEL:
619 case MotionEvent.ACTION_UP:
620 mLongPressHelper.cancelLongPress();
621 break;
622 }
623 return result;
624 }
625
626 @Override
627 public void cancelLongPress() {
628 super.cancelLongPress();
629
630 mLongPressHelper.cancelLongPress();
631 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800632}