blob: c005edfcd4208eea182baa2d57887206e97d0565 [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) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800122
Adam Cohend0445262011-07-04 23:53:22 -0700123 if (INITIAL_ITEM_ANIMATION_DURATION >= DROP_IN_ANIMATION_DURATION) {
124 throw new IllegalStateException("DROP_IN_ANIMATION_DURATION must be greater than " +
125 "INITIAL_ITEM_ANIMATION_DURATION, as sequencing of adding first two items " +
126 "is dependent on this");
127 }
128
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129 FolderIcon icon = (FolderIcon) LayoutInflater.from(launcher).inflate(resId, group, false);
130
Adam Cohend2eca6b2011-07-26 22:49:13 -0700131 icon.mFolderName = (BubbleTextView) icon.findViewById(R.id.folder_icon_name);
Adam Cohen76fc0852011-06-17 13:26:23 -0700132 icon.mFolderName.setText(folderInfo.title);
133 icon.mPreviewBackground = (ImageView) icon.findViewById(R.id.preview_background);
134
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135 icon.setTag(folderInfo);
136 icon.setOnClickListener(launcher);
137 icon.mInfo = folderInfo;
138 icon.mLauncher = launcher;
Adam Cohen3371da02011-10-25 21:38:29 -0700139 icon.setContentDescription(String.format(launcher.getString(R.string.folder_name_format),
140 folderInfo.title));
Adam Cohena9cf38f2011-05-02 15:36:58 -0700141 Folder folder = Folder.fromXml(launcher);
142 folder.setDragController(launcher.getDragController());
Adam Cohen2801caf2011-05-13 20:57:39 -0700143 folder.setFolderIcon(icon);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700144 folder.bind(folderInfo);
145 icon.mFolder = folder;
Adam Cohen099f60d2011-08-23 21:07:26 -0700146
Adam Cohen099f60d2011-08-23 21:07:26 -0700147 icon.mFolderRingAnimator = new FolderRingAnimator(launcher, icon);
148 folderInfo.addListener(icon);
149
Adam Cohen19072da2011-05-31 14:30:45 -0700150 return icon;
151 }
152
Adam Cohen099f60d2011-08-23 21:07:26 -0700153 @Override
154 protected Parcelable onSaveInstanceState() {
155 sStaticValuesDirty = true;
156 return super.onSaveInstanceState();
157 }
158
Adam Cohen19072da2011-05-31 14:30:45 -0700159 public static class FolderRingAnimator {
Adam Cohen69ce2e52011-07-03 19:25:21 -0700160 public int mCellX;
161 public int mCellY;
162 private CellLayout mCellLayout;
Adam Cohen76fc0852011-06-17 13:26:23 -0700163 public float mOuterRingSize;
164 public float mInnerRingSize;
Adam Cohen19072da2011-05-31 14:30:45 -0700165 public FolderIcon mFolderIcon = null;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700166 public Drawable mOuterRingDrawable = null;
167 public Drawable mInnerRingDrawable = null;
168 public static Drawable sSharedOuterRingDrawable = null;
169 public static Drawable sSharedInnerRingDrawable = null;
Adam Cohen76fc0852011-06-17 13:26:23 -0700170 public static int sPreviewSize = -1;
171 public static int sPreviewPadding = -1;
172
Adam Cohenc0dcf592011-06-01 15:30:43 -0700173 private ValueAnimator mAcceptAnimator;
174 private ValueAnimator mNeutralAnimator;
Adam Cohen19072da2011-05-31 14:30:45 -0700175
176 public FolderRingAnimator(Launcher launcher, FolderIcon folderIcon) {
Adam Cohen19072da2011-05-31 14:30:45 -0700177 mFolderIcon = folderIcon;
Adam Cohen76fc0852011-06-17 13:26:23 -0700178 Resources res = launcher.getResources();
179 mOuterRingDrawable = res.getDrawable(R.drawable.portal_ring_outer_holo);
180 mInnerRingDrawable = res.getDrawable(R.drawable.portal_ring_inner_holo);
181
Adam Cohen099f60d2011-08-23 21:07:26 -0700182 // We need to reload the static values when configuration changes in case they are
183 // different in another configuration
184 if (sStaticValuesDirty) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700185 sPreviewSize = res.getDimensionPixelSize(R.dimen.folder_preview_size);
186 sPreviewPadding = res.getDimensionPixelSize(R.dimen.folder_preview_padding);
Adam Cohen76fc0852011-06-17 13:26:23 -0700187 sSharedOuterRingDrawable = res.getDrawable(R.drawable.portal_ring_outer_holo);
Adam Cohen76fc0852011-06-17 13:26:23 -0700188 sSharedInnerRingDrawable = res.getDrawable(R.drawable.portal_ring_inner_holo);
Adam Cohendf6af572011-10-19 14:38:16 -0700189 sSharedFolderLeaveBehind = res.getDrawable(R.drawable.portal_ring_rest);
Adam Cohen099f60d2011-08-23 21:07:26 -0700190 sStaticValuesDirty = false;
Adam Cohen19072da2011-05-31 14:30:45 -0700191 }
Adam Cohen073a46f2011-05-17 16:28:09 -0700192 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700193
Adam Cohen19072da2011-05-31 14:30:45 -0700194 public void animateToAcceptState() {
Adam Cohenc0dcf592011-06-01 15:30:43 -0700195 if (mNeutralAnimator != null) {
196 mNeutralAnimator.cancel();
197 }
198 mAcceptAnimator = ValueAnimator.ofFloat(0f, 1f);
199 mAcceptAnimator.setDuration(CONSUMPTION_ANIMATION_DURATION);
200 mAcceptAnimator.addUpdateListener(new AnimatorUpdateListener() {
Adam Cohen19072da2011-05-31 14:30:45 -0700201 public void onAnimationUpdate(ValueAnimator animation) {
202 final float percent = (Float) animation.getAnimatedValue();
Adam Cohen76fc0852011-06-17 13:26:23 -0700203 mOuterRingSize = (1 + percent * OUTER_RING_GROWTH_FACTOR) * sPreviewSize;
204 mInnerRingSize = (1 + percent * INNER_RING_GROWTH_FACTOR) * sPreviewSize;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700205 if (mCellLayout != null) {
206 mCellLayout.invalidate();
Adam Cohen19072da2011-05-31 14:30:45 -0700207 }
208 }
209 });
Adam Cohenc0dcf592011-06-01 15:30:43 -0700210 mAcceptAnimator.addListener(new AnimatorListenerAdapter() {
Adam Cohen19072da2011-05-31 14:30:45 -0700211 @Override
Adam Cohenc0dcf592011-06-01 15:30:43 -0700212 public void onAnimationStart(Animator animation) {
Adam Cohen19072da2011-05-31 14:30:45 -0700213 if (mFolderIcon != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700214 mFolderIcon.mPreviewBackground.setVisibility(INVISIBLE);
Adam Cohen19072da2011-05-31 14:30:45 -0700215 }
216 }
217 });
Adam Cohenc0dcf592011-06-01 15:30:43 -0700218 mAcceptAnimator.start();
Adam Cohen19072da2011-05-31 14:30:45 -0700219 }
220
221 public void animateToNaturalState() {
Adam Cohenc0dcf592011-06-01 15:30:43 -0700222 if (mAcceptAnimator != null) {
223 mAcceptAnimator.cancel();
224 }
225 mNeutralAnimator = ValueAnimator.ofFloat(0f, 1f);
226 mNeutralAnimator.setDuration(CONSUMPTION_ANIMATION_DURATION);
227 mNeutralAnimator.addUpdateListener(new AnimatorUpdateListener() {
Adam Cohen19072da2011-05-31 14:30:45 -0700228 public void onAnimationUpdate(ValueAnimator animation) {
229 final float percent = (Float) animation.getAnimatedValue();
Adam Cohen76fc0852011-06-17 13:26:23 -0700230 mOuterRingSize = (1 + (1 - percent) * OUTER_RING_GROWTH_FACTOR) * sPreviewSize;
231 mInnerRingSize = (1 + (1 - percent) * INNER_RING_GROWTH_FACTOR) * sPreviewSize;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700232 if (mCellLayout != null) {
233 mCellLayout.invalidate();
Adam Cohen19072da2011-05-31 14:30:45 -0700234 }
235 }
236 });
Adam Cohenc0dcf592011-06-01 15:30:43 -0700237 mNeutralAnimator.addListener(new AnimatorListenerAdapter() {
Adam Cohen19072da2011-05-31 14:30:45 -0700238 @Override
239 public void onAnimationEnd(Animator animation) {
Adam Cohen69ce2e52011-07-03 19:25:21 -0700240 if (mCellLayout != null) {
241 mCellLayout.hideFolderAccept(FolderRingAnimator.this);
242 }
Adam Cohen19072da2011-05-31 14:30:45 -0700243 if (mFolderIcon != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700244 mFolderIcon.mPreviewBackground.setVisibility(VISIBLE);
Adam Cohen19072da2011-05-31 14:30:45 -0700245 }
Adam Cohen19072da2011-05-31 14:30:45 -0700246 }
247 });
Adam Cohenc0dcf592011-06-01 15:30:43 -0700248 mNeutralAnimator.start();
Adam Cohen19072da2011-05-31 14:30:45 -0700249 }
250
Adam Cohenc0dcf592011-06-01 15:30:43 -0700251 // Location is expressed in window coordinates
Adam Cohen69ce2e52011-07-03 19:25:21 -0700252 public void getCell(int[] loc) {
253 loc[0] = mCellX;
254 loc[1] = mCellY;
255 }
256
257 // Location is expressed in window coordinates
258 public void setCell(int x, int y) {
259 mCellX = x;
260 mCellY = y;
261 }
262
263 public void setCellLayout(CellLayout layout) {
264 mCellLayout = layout;
Adam Cohen19072da2011-05-31 14:30:45 -0700265 }
266
Adam Cohen76fc0852011-06-17 13:26:23 -0700267 public float getOuterRingSize() {
268 return mOuterRingSize;
Adam Cohen19072da2011-05-31 14:30:45 -0700269 }
270
Adam Cohen76fc0852011-06-17 13:26:23 -0700271 public float getInnerRingSize() {
272 return mInnerRingSize;
Adam Cohen19072da2011-05-31 14:30:45 -0700273 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800274 }
275
Adam Cohen073a46f2011-05-17 16:28:09 -0700276 private boolean willAcceptItem(ItemInfo item) {
277 final int itemType = item.itemType;
278 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
279 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
Adam Cohenc51934b2011-07-26 21:07:43 -0700280 !mFolder.isFull() && item != mInfo && !mInfo.opened);
Adam Cohen073a46f2011-05-17 16:28:09 -0700281 }
282
Adam Cohenc0dcf592011-06-01 15:30:43 -0700283 public boolean acceptDrop(Object dragInfo) {
284 final ItemInfo item = (ItemInfo) dragInfo;
Adam Cohen073a46f2011-05-17 16:28:09 -0700285 return willAcceptItem(item);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800286 }
287
Adam Cohendf035382011-04-11 17:22:04 -0700288 public void addItem(ShortcutInfo item) {
289 mInfo.add(item);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700290 LauncherModel.addOrMoveItemInDatabase(mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
Adam Cohendf035382011-04-11 17:22:04 -0700291 }
292
Adam Cohenc0dcf592011-06-01 15:30:43 -0700293 public void onDragEnter(Object dragInfo) {
294 if (!willAcceptItem((ItemInfo) dragInfo)) return;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700295 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) getLayoutParams();
296 CellLayout layout = (CellLayout) getParent().getParent();
297 mFolderRingAnimator.setCell(lp.cellX, lp.cellY);
298 mFolderRingAnimator.setCellLayout(layout);
Adam Cohen19072da2011-05-31 14:30:45 -0700299 mFolderRingAnimator.animateToAcceptState();
Adam Cohen69ce2e52011-07-03 19:25:21 -0700300 layout.showFolderAccept(mFolderRingAnimator);
Adam Cohen073a46f2011-05-17 16:28:09 -0700301 }
302
Adam Cohenc0dcf592011-06-01 15:30:43 -0700303 public void onDragOver(Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800304 }
305
Adam Cohend0445262011-07-04 23:53:22 -0700306 public void performCreateAnimation(final ShortcutInfo destInfo, final View destView,
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800307 final ShortcutInfo srcInfo, final DragView srcView, Rect dstRect,
Adam Cohenac8c8762011-07-13 11:15:27 -0700308 float scaleRelativeToDragLayer, Runnable postAnimationRunnable) {
Adam Cohend0445262011-07-04 23:53:22 -0700309
310 Drawable animateDrawable = ((TextView) destView).getCompoundDrawables()[1];
311 computePreviewDrawingParams(animateDrawable.getIntrinsicWidth(), destView.getMeasuredWidth());
Adam Cohenac8c8762011-07-13 11:15:27 -0700312
Adam Cohend0445262011-07-04 23:53:22 -0700313 // This will animate the dragView (srcView) into the new folder
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800314 onDrop(srcInfo, srcView, dstRect, scaleRelativeToDragLayer, 1, postAnimationRunnable, null);
Adam Cohend0445262011-07-04 23:53:22 -0700315
316 // This will animate the first item from it's position as an icon into its
317 // position as the first item in the preview
Adam Cohen8dfcba42011-07-07 16:38:18 -0700318 animateFirstItem(animateDrawable, INITIAL_ITEM_ANIMATION_DURATION);
Adam Cohend0445262011-07-04 23:53:22 -0700319
320 postDelayed(new Runnable() {
321 public void run() {
322 addItem(destInfo);
323 }
324 }, INITIAL_ITEM_ANIMATION_DURATION);
325 }
326
Adam Cohenc0dcf592011-06-01 15:30:43 -0700327 public void onDragExit(Object dragInfo) {
328 if (!willAcceptItem((ItemInfo) dragInfo)) return;
Adam Cohen19072da2011-05-31 14:30:45 -0700329 mFolderRingAnimator.animateToNaturalState();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800330 }
Patrick Dubroy440c3602010-07-13 17:50:32 -0700331
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800332 private void onDrop(final ShortcutInfo item, DragView animateView, Rect finalRect,
333 float scaleRelativeToDragLayer, int index, Runnable postAnimationRunnable,
334 DragObject d) {
Adam Cohend0445262011-07-04 23:53:22 -0700335 item.cellX = -1;
336 item.cellY = -1;
Adam Cohend0445262011-07-04 23:53:22 -0700337
Adam Cohen558baaf2011-08-15 15:22:57 -0700338 // Typically, the animateView corresponds to the DragView; however, if this is being done
339 // after a configuration activity (ie. for a Shortcut being dragged from AllApps) we
340 // will not have a view to animate
341 if (animateView != null) {
342 DragLayer dragLayer = mLauncher.getDragLayer();
343 Rect from = new Rect();
344 dragLayer.getViewRectRelativeToSelf(animateView, from);
345 Rect to = finalRect;
346 if (to == null) {
347 to = new Rect();
348 Workspace workspace = mLauncher.getWorkspace();
349 // Set cellLayout and this to it's final state to compute final animation locations
350 workspace.setFinalTransitionTransform((CellLayout) getParent().getParent());
351 float scaleX = getScaleX();
352 float scaleY = getScaleY();
353 setScaleX(1.0f);
354 setScaleY(1.0f);
355 scaleRelativeToDragLayer = dragLayer.getDescendantRectRelativeToSelf(this, to);
356 // Finished computing final animation locations, restore current state
357 setScaleX(scaleX);
358 setScaleY(scaleY);
359 workspace.resetTransitionTransform((CellLayout) getParent().getParent());
Adam Cohend0445262011-07-04 23:53:22 -0700360 }
Adam Cohen558baaf2011-08-15 15:22:57 -0700361
362 int[] center = new int[2];
363 float scale = getLocalCenterForIndex(index, center);
364 center[0] = (int) Math.round(scaleRelativeToDragLayer * center[0]);
365 center[1] = (int) Math.round(scaleRelativeToDragLayer * center[1]);
366
367 to.offset(center[0] - animateView.getMeasuredWidth() / 2,
368 center[1] - animateView.getMeasuredHeight() / 2);
369
370 float finalAlpha = index < NUM_ITEMS_IN_PREVIEW ? 0.5f : 0f;
371
Adam Cohened66b2b2012-01-23 17:28:51 -0800372 float finalScale = scale * scaleRelativeToDragLayer;
Adam Cohen558baaf2011-08-15 15:22:57 -0700373 dragLayer.animateView(animateView, from, to, finalAlpha,
Adam Cohened66b2b2012-01-23 17:28:51 -0800374 1, 1, finalScale, finalScale, DROP_IN_ANIMATION_DURATION,
Adam Cohen558baaf2011-08-15 15:22:57 -0700375 new DecelerateInterpolator(2), new AccelerateInterpolator(2),
Adam Cohened66b2b2012-01-23 17:28:51 -0800376 postAnimationRunnable, DragLayer.ANIMATION_END_DISAPPEAR, null);
Adam Cohen558baaf2011-08-15 15:22:57 -0700377 postDelayed(new Runnable() {
378 public void run() {
379 addItem(item);
380 }
381 }, DROP_IN_ANIMATION_DURATION);
382 } else {
383 addItem(item);
384 }
Adam Cohend0445262011-07-04 23:53:22 -0700385 }
386
Adam Cohen3e8f8112011-07-02 18:03:00 -0700387 public void onDrop(DragObject d) {
Adam Cohenc0dcf592011-06-01 15:30:43 -0700388 ShortcutInfo item;
Adam Cohen3e8f8112011-07-02 18:03:00 -0700389 if (d.dragInfo instanceof ApplicationInfo) {
Adam Cohenc0dcf592011-06-01 15:30:43 -0700390 // Came from all apps -- make a copy
Adam Cohen3e8f8112011-07-02 18:03:00 -0700391 item = ((ApplicationInfo) d.dragInfo).makeShortcut();
Adam Cohenc0dcf592011-06-01 15:30:43 -0700392 } else {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700393 item = (ShortcutInfo) d.dragInfo;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700394 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700395 mFolder.notifyDrop();
Winson Chung7bd1bbb2012-02-13 18:29:29 -0800396 onDrop(item, d.dragView, null, 1.0f, mInfo.contents.size(), d.postAnimationRunnable, d);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700397 }
398
Adam Cohencb3382b2011-05-24 14:07:08 -0700399 public DropTarget getDropTargetDelegate(DragObject d) {
Patrick Dubroy440c3602010-07-13 17:50:32 -0700400 return null;
401 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700402
Adam Cohend0445262011-07-04 23:53:22 -0700403 private void computePreviewDrawingParams(int drawableSize, int totalSize) {
404 if (mIntrinsicIconSize != drawableSize || mTotalWidth != totalSize) {
405 mIntrinsicIconSize = drawableSize;
406 mTotalWidth = totalSize;
407
408 final int previewSize = FolderRingAnimator.sPreviewSize;
409 final int previewPadding = FolderRingAnimator.sPreviewPadding;
410
411 mAvailableSpaceInPreview = (previewSize - 2 * previewPadding);
412 // cos(45) = 0.707 + ~= 0.1) = 0.8f
413 int adjustedAvailableSpace = (int) ((mAvailableSpaceInPreview / 2) * (1 + 0.8f));
414
415 int unscaledHeight = (int) (mIntrinsicIconSize * (1 + PERSPECTIVE_SHIFT_FACTOR));
416 mBaselineIconScale = (1.0f * adjustedAvailableSpace / unscaledHeight);
417
418 mBaselineIconSize = (int) (mIntrinsicIconSize * mBaselineIconScale);
419 mMaxPerspectiveShift = mBaselineIconSize * PERSPECTIVE_SHIFT_FACTOR;
420
421 mPreviewOffsetX = (mTotalWidth - mAvailableSpaceInPreview) / 2;
422 mPreviewOffsetY = previewPadding;
423 }
424 }
425
426 private void computePreviewDrawingParams(Drawable d) {
427 computePreviewDrawingParams(d.getIntrinsicWidth(), getMeasuredWidth());
428 }
429
430 class PreviewItemDrawingParams {
431 PreviewItemDrawingParams(float transX, float transY, float scale, int overlayAlpha) {
432 this.transX = transX;
433 this.transY = transY;
434 this.scale = scale;
435 this.overlayAlpha = overlayAlpha;
436 }
437 float transX;
438 float transY;
439 float scale;
440 int overlayAlpha;
441 Drawable drawable;
442 }
443
Adam Cohenac8c8762011-07-13 11:15:27 -0700444 private float getLocalCenterForIndex(int index, int[] center) {
Adam Cohend0445262011-07-04 23:53:22 -0700445 mParams = computePreviewItemDrawingParams(Math.min(NUM_ITEMS_IN_PREVIEW, index), mParams);
446
447 mParams.transX += mPreviewOffsetX;
448 mParams.transY += mPreviewOffsetY;
Adam Cohenac8c8762011-07-13 11:15:27 -0700449 float offsetX = mParams.transX + (mParams.scale * mIntrinsicIconSize) / 2;
450 float offsetY = mParams.transY + (mParams.scale * mIntrinsicIconSize) / 2;
Adam Cohend0445262011-07-04 23:53:22 -0700451
Adam Cohenac8c8762011-07-13 11:15:27 -0700452 center[0] = (int) Math.round(offsetX);
453 center[1] = (int) Math.round(offsetY);
Adam Cohend0445262011-07-04 23:53:22 -0700454 return mParams.scale;
455 }
456
457 private PreviewItemDrawingParams computePreviewItemDrawingParams(int index,
458 PreviewItemDrawingParams params) {
459 index = NUM_ITEMS_IN_PREVIEW - index - 1;
460 float r = (index * 1.0f) / (NUM_ITEMS_IN_PREVIEW - 1);
461 float scale = (1 - PERSPECTIVE_SCALE_FACTOR * (1 - r));
462
463 float offset = (1 - r) * mMaxPerspectiveShift;
464 float scaledSize = scale * mBaselineIconSize;
465 float scaleOffsetCorrection = (1 - scale) * mBaselineIconSize;
466
467 // We want to imagine our coordinates from the bottom left, growing up and to the
468 // right. This is natural for the x-axis, but for the y-axis, we have to invert things.
469 float transY = mAvailableSpaceInPreview - (offset + scaledSize + scaleOffsetCorrection);
470 float transX = offset + scaleOffsetCorrection;
471 float totalScale = mBaselineIconScale * scale;
472 final int overlayAlpha = (int) (80 * (1 - r));
473
474 if (params == null) {
475 params = new PreviewItemDrawingParams(transX, transY, totalScale, overlayAlpha);
476 } else {
477 params.transX = transX;
478 params.transY = transY;
479 params.scale = totalScale;
480 params.overlayAlpha = overlayAlpha;
481 }
482 return params;
483 }
484
485 private void drawPreviewItem(Canvas canvas, PreviewItemDrawingParams params) {
486 canvas.save();
487 canvas.translate(params.transX + mPreviewOffsetX, params.transY + mPreviewOffsetY);
488 canvas.scale(params.scale, params.scale);
489 Drawable d = params.drawable;
490
491 if (d != null) {
492 d.setBounds(0, 0, mIntrinsicIconSize, mIntrinsicIconSize);
493 d.setFilterBitmap(true);
494 d.setColorFilter(Color.argb(params.overlayAlpha, 0, 0, 0), PorterDuff.Mode.SRC_ATOP);
495 d.draw(canvas);
496 d.clearColorFilter();
497 d.setFilterBitmap(false);
498 }
499 canvas.restore();
500 }
501
Adam Cohena9cf38f2011-05-02 15:36:58 -0700502 @Override
Adam Cohenc0dcf592011-06-01 15:30:43 -0700503 protected void dispatchDraw(Canvas canvas) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700504 super.dispatchDraw(canvas);
505
Adam Cohena9cf38f2011-05-02 15:36:58 -0700506 if (mFolder == null) return;
Adam Cohend0445262011-07-04 23:53:22 -0700507 if (mFolder.getItemCount() == 0 && !mAnimating) return;
Adam Cohena9cf38f2011-05-02 15:36:58 -0700508
Adam Cohen76078c42011-06-09 15:06:52 -0700509 ArrayList<View> items = mFolder.getItemsInReadingOrder(false);
Adam Cohend0445262011-07-04 23:53:22 -0700510 Drawable d;
511 TextView v;
Adam Cohenbadf71e2011-05-26 19:08:29 -0700512
Adam Cohend0445262011-07-04 23:53:22 -0700513 // Update our drawing parameters if necessary
514 if (mAnimating) {
515 computePreviewDrawingParams(mAnimParams.drawable);
516 } else {
517 v = (TextView) items.get(0);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700518 d = v.getCompoundDrawables()[1];
Adam Cohend0445262011-07-04 23:53:22 -0700519 computePreviewDrawingParams(d);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700520 }
Adam Cohend0445262011-07-04 23:53:22 -0700521
522 int nItemsInPreview = Math.min(items.size(), NUM_ITEMS_IN_PREVIEW);
523 if (!mAnimating) {
524 for (int i = nItemsInPreview - 1; i >= 0; i--) {
525 v = (TextView) items.get(i);
526 d = v.getCompoundDrawables()[1];
527
528 mParams = computePreviewItemDrawingParams(i, mParams);
529 mParams.drawable = d;
530 drawPreviewItem(canvas, mParams);
531 }
532 } else {
533 drawPreviewItem(canvas, mAnimParams);
534 }
535 }
536
537 private void animateFirstItem(final Drawable d, int duration) {
538 computePreviewDrawingParams(d);
539 final PreviewItemDrawingParams finalParams = computePreviewItemDrawingParams(0, null);
540
541 final float scale0 = 1.0f;
Adam Cohen1d4ee4e2011-08-12 15:51:59 -0700542 final float transX0 = (mAvailableSpaceInPreview - d.getIntrinsicWidth()) / 2;
543 final float transY0 = (mAvailableSpaceInPreview - d.getIntrinsicHeight()) / 2;
Adam Cohend0445262011-07-04 23:53:22 -0700544 mAnimParams.drawable = d;
545
546 ValueAnimator va = ValueAnimator.ofFloat(0f, 1.0f);
547 va.addUpdateListener(new AnimatorUpdateListener(){
548 public void onAnimationUpdate(ValueAnimator animation) {
549 float progress = (Float) animation.getAnimatedValue();
550
551 mAnimParams.transX = transX0 + progress * (finalParams.transX - transX0);
552 mAnimParams.transY = transY0 + progress * (finalParams.transY - transY0);
553 mAnimParams.scale = scale0 + progress * (finalParams.scale - scale0);
554 invalidate();
555 }
556 });
557 va.addListener(new AnimatorListenerAdapter() {
558 @Override
559 public void onAnimationStart(Animator animation) {
560 mAnimating = true;
561 }
562 @Override
563 public void onAnimationEnd(Animator animation) {
564 mAnimating = false;
565 }
566 });
567 va.setDuration(duration);
568 va.start();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700569 }
570
Adam Cohen099f60d2011-08-23 21:07:26 -0700571 public void setTextVisible(boolean visible) {
572 if (visible) {
573 mFolderName.setVisibility(VISIBLE);
574 } else {
575 mFolderName.setVisibility(INVISIBLE);
576 }
577 }
578
579 public boolean getTextVisible() {
580 return mFolderName.getVisibility() == VISIBLE;
581 }
582
Adam Cohen76078c42011-06-09 15:06:52 -0700583 public void onItemsChanged() {
584 invalidate();
585 requestLayout();
586 }
587
Adam Cohena9cf38f2011-05-02 15:36:58 -0700588 public void onAdd(ShortcutInfo item) {
589 invalidate();
590 requestLayout();
591 }
592
593 public void onRemove(ShortcutInfo item) {
594 invalidate();
595 requestLayout();
596 }
Adam Cohen76fc0852011-06-17 13:26:23 -0700597
598 public void onTitleChanged(CharSequence title) {
Adam Cohend63cfa92011-06-24 14:17:17 -0700599 mFolderName.setText(title.toString());
Adam Cohen3371da02011-10-25 21:38:29 -0700600 setContentDescription(String.format(mContext.getString(R.string.folder_name_format),
601 title));
Adam Cohen76fc0852011-06-17 13:26:23 -0700602 }
Winson Chung88f33452012-02-23 15:23:44 -0800603
604 @Override
605 public boolean onTouchEvent(MotionEvent event) {
606 // Call the superclass onTouchEvent first, because sometimes it changes the state to
607 // isPressed() on an ACTION_UP
608 boolean result = super.onTouchEvent(event);
609
610 switch (event.getAction()) {
611 case MotionEvent.ACTION_DOWN:
612 mLongPressHelper.postCheckForLongPress();
613 break;
614 case MotionEvent.ACTION_CANCEL:
615 case MotionEvent.ACTION_UP:
616 mLongPressHelper.cancelLongPress();
617 break;
618 }
619 return result;
620 }
621
622 @Override
623 public void cancelLongPress() {
624 super.cancelLongPress();
625
626 mLongPressHelper.cancelLongPress();
627 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800628}