The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
Daniel Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 17 | package com.android.launcher3; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 18 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 19 | import android.animation.Animator; |
| 20 | import android.animation.AnimatorListenerAdapter; |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 21 | import android.animation.ValueAnimator; |
| 22 | import android.animation.ValueAnimator.AnimatorUpdateListener; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 23 | import android.content.Context; |
| 24 | import android.content.res.Resources; |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 25 | import android.graphics.Canvas; |
Adam Cohen | f4b0891 | 2011-05-17 18:45:47 -0700 | [diff] [blame] | 26 | import android.graphics.Color; |
Adam Cohen | badf71e | 2011-05-26 19:08:29 -0700 | [diff] [blame] | 27 | import android.graphics.PorterDuff; |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 28 | import android.graphics.Rect; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 29 | import android.graphics.drawable.Drawable; |
Daniel Sandler | 24ad0f0 | 2013-07-31 00:14:58 -0400 | [diff] [blame] | 30 | import android.os.Looper; |
Adam Cohen | 099f60d | 2011-08-23 21:07:26 -0700 | [diff] [blame] | 31 | import android.os.Parcelable; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 32 | import android.util.AttributeSet; |
| 33 | import android.view.LayoutInflater; |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 34 | import android.view.MotionEvent; |
Adam Cohen | 7c69321 | 2011-05-18 15:26:57 -0700 | [diff] [blame] | 35 | import android.view.View; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 36 | import android.view.ViewGroup; |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 37 | import android.view.animation.AccelerateInterpolator; |
| 38 | import android.view.animation.DecelerateInterpolator; |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 39 | import android.widget.ImageView; |
| 40 | import android.widget.LinearLayout; |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 41 | import android.widget.TextView; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 42 | |
Daniel Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 43 | import com.android.launcher3.R; |
| 44 | import com.android.launcher3.DropTarget.DragObject; |
| 45 | import com.android.launcher3.FolderInfo.FolderListener; |
Romain Guy | edcce09 | 2010-03-04 13:03:17 -0800 | [diff] [blame] | 46 | |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 47 | import java.util.ArrayList; |
| 48 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 49 | /** |
| 50 | * An icon that can appear on in the workspace representing an {@link UserFolder}. |
| 51 | */ |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 52 | public class FolderIcon extends LinearLayout implements FolderListener { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 53 | private Launcher mLauncher; |
Adam Cohen | fb91f30 | 2012-06-11 15:45:18 -0700 | [diff] [blame] | 54 | private Folder mFolder; |
| 55 | private FolderInfo mInfo; |
Adam Cohen | 099f60d | 2011-08-23 21:07:26 -0700 | [diff] [blame] | 56 | private static boolean sStaticValuesDirty = true; |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 57 | |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 58 | private CheckLongPressHelper mLongPressHelper; |
| 59 | |
Adam Cohen | badf71e | 2011-05-26 19:08:29 -0700 | [diff] [blame] | 60 | // The number of icons to display in the |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 61 | private static final int NUM_ITEMS_IN_PREVIEW = 3; |
Adam Cohen | f4b0891 | 2011-05-17 18:45:47 -0700 | [diff] [blame] | 62 | private static final int CONSUMPTION_ANIMATION_DURATION = 100; |
Adam Cohen | d044526 | 2011-07-04 23:53:22 -0700 | [diff] [blame] | 63 | private static final int DROP_IN_ANIMATION_DURATION = 400; |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 64 | private static final int INITIAL_ITEM_ANIMATION_DURATION = 350; |
Adam Cohen | fb91f30 | 2012-06-11 15:45:18 -0700 | [diff] [blame] | 65 | private static final int FINAL_ITEM_ANIMATION_DURATION = 200; |
Adam Cohen | badf71e | 2011-05-26 19:08:29 -0700 | [diff] [blame] | 66 | |
| 67 | // The degree to which the inner ring grows when accepting drop |
Adam Cohen | 69ce2e5 | 2011-07-03 19:25:21 -0700 | [diff] [blame] | 68 | private static final float INNER_RING_GROWTH_FACTOR = 0.15f; |
Adam Cohen | badf71e | 2011-05-26 19:08:29 -0700 | [diff] [blame] | 69 | |
Adam Cohen | badf71e | 2011-05-26 19:08:29 -0700 | [diff] [blame] | 70 | // The degree to which the outer ring is scaled in its natural state |
Adam Cohen | 69ce2e5 | 2011-07-03 19:25:21 -0700 | [diff] [blame] | 71 | private static final float OUTER_RING_GROWTH_FACTOR = 0.3f; |
Adam Cohen | badf71e | 2011-05-26 19:08:29 -0700 | [diff] [blame] | 72 | |
| 73 | // The amount of vertical spread between items in the stack [0...1] |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 74 | private static final float PERSPECTIVE_SHIFT_FACTOR = 0.24f; |
Adam Cohen | badf71e | 2011-05-26 19:08:29 -0700 | [diff] [blame] | 75 | |
| 76 | // The degree to which the item in the back of the stack is scaled [0...1] |
| 77 | // (0 means it's not scaled at all, 1 means it's scaled to nothing) |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 78 | private static final float PERSPECTIVE_SCALE_FACTOR = 0.35f; |
Adam Cohen | badf71e | 2011-05-26 19:08:29 -0700 | [diff] [blame] | 79 | |
Adam Cohen | c51934b | 2011-07-26 21:07:43 -0700 | [diff] [blame] | 80 | public static Drawable sSharedFolderLeaveBehind = null; |
| 81 | |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 82 | private ImageView mPreviewBackground; |
| 83 | private BubbleTextView mFolderName; |
Adam Cohen | 073a46f | 2011-05-17 16:28:09 -0700 | [diff] [blame] | 84 | |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 85 | FolderRingAnimator mFolderRingAnimator = null; |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 86 | |
Adam Cohen | d044526 | 2011-07-04 23:53:22 -0700 | [diff] [blame] | 87 | // These variables are all associated with the drawing of the preview; they are stored |
| 88 | // as member variables for shared usage and to avoid computation on each frame |
| 89 | private int mIntrinsicIconSize; |
| 90 | private float mBaselineIconScale; |
| 91 | private int mBaselineIconSize; |
| 92 | private int mAvailableSpaceInPreview; |
| 93 | private int mTotalWidth = -1; |
| 94 | private int mPreviewOffsetX; |
| 95 | private int mPreviewOffsetY; |
| 96 | private float mMaxPerspectiveShift; |
| 97 | boolean mAnimating = false; |
Adam Cohen | fb91f30 | 2012-06-11 15:45:18 -0700 | [diff] [blame] | 98 | |
Adam Cohen | d044526 | 2011-07-04 23:53:22 -0700 | [diff] [blame] | 99 | private PreviewItemDrawingParams mParams = new PreviewItemDrawingParams(0, 0, 0, 0); |
| 100 | private PreviewItemDrawingParams mAnimParams = new PreviewItemDrawingParams(0, 0, 0, 0); |
Adam Cohen | fb91f30 | 2012-06-11 15:45:18 -0700 | [diff] [blame] | 101 | private ArrayList<ShortcutInfo> mHiddenItems = new ArrayList<ShortcutInfo>(); |
Adam Cohen | d044526 | 2011-07-04 23:53:22 -0700 | [diff] [blame] | 102 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 103 | public FolderIcon(Context context, AttributeSet attrs) { |
| 104 | super(context, attrs); |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 105 | init(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | public FolderIcon(Context context) { |
| 109 | super(context); |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 110 | init(); |
| 111 | } |
| 112 | |
| 113 | private void init() { |
| 114 | mLongPressHelper = new CheckLongPressHelper(this); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 115 | } |
| 116 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 117 | public boolean isDropEnabled() { |
Winson Chung | 7a1d165 | 2011-03-18 15:56:01 -0700 | [diff] [blame] | 118 | final ViewGroup cellLayoutChildren = (ViewGroup) getParent(); |
| 119 | final ViewGroup cellLayout = (ViewGroup) cellLayoutChildren.getParent(); |
| 120 | final Workspace workspace = (Workspace) cellLayout.getParent(); |
| 121 | return !workspace.isSmall(); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 122 | } |
| 123 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 124 | static FolderIcon fromXml(int resId, Launcher launcher, ViewGroup group, |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 125 | FolderInfo folderInfo, IconCache iconCache) { |
Michael Jurka | 3a9fced | 2012-04-13 14:44:29 -0700 | [diff] [blame] | 126 | @SuppressWarnings("all") // suppress dead code warning |
| 127 | final boolean error = INITIAL_ITEM_ANIMATION_DURATION >= DROP_IN_ANIMATION_DURATION; |
| 128 | if (error) { |
Adam Cohen | d044526 | 2011-07-04 23:53:22 -0700 | [diff] [blame] | 129 | throw new IllegalStateException("DROP_IN_ANIMATION_DURATION must be greater than " + |
| 130 | "INITIAL_ITEM_ANIMATION_DURATION, as sequencing of adding first two items " + |
| 131 | "is dependent on this"); |
| 132 | } |
| 133 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 134 | FolderIcon icon = (FolderIcon) LayoutInflater.from(launcher).inflate(resId, group, false); |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 135 | icon.setClipToPadding(false); |
Adam Cohen | d2eca6b | 2011-07-26 22:49:13 -0700 | [diff] [blame] | 136 | icon.mFolderName = (BubbleTextView) icon.findViewById(R.id.folder_icon_name); |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 137 | icon.mFolderName.setText(folderInfo.title); |
| 138 | icon.mPreviewBackground = (ImageView) icon.findViewById(R.id.preview_background); |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 139 | LauncherAppState app = LauncherAppState.getInstance(); |
| 140 | DeviceProfile grid = app.getDynamicGrid().getDeviceProfile(); |
| 141 | // Offset the preview background to center this view accordingly |
| 142 | LinearLayout.LayoutParams lp = |
| 143 | (LinearLayout.LayoutParams) icon.mPreviewBackground.getLayoutParams(); |
| 144 | lp.topMargin = grid.folderBackgroundOffset; |
| 145 | lp.width = grid.folderIconSizePx; |
| 146 | lp.height = grid.folderIconSizePx; |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 147 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 148 | icon.setTag(folderInfo); |
| 149 | icon.setOnClickListener(launcher); |
| 150 | icon.mInfo = folderInfo; |
| 151 | icon.mLauncher = launcher; |
Adam Cohen | 3371da0 | 2011-10-25 21:38:29 -0700 | [diff] [blame] | 152 | icon.setContentDescription(String.format(launcher.getString(R.string.folder_name_format), |
| 153 | folderInfo.title)); |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 154 | Folder folder = Folder.fromXml(launcher); |
| 155 | folder.setDragController(launcher.getDragController()); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 156 | folder.setFolderIcon(icon); |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 157 | folder.bind(folderInfo); |
| 158 | icon.mFolder = folder; |
Adam Cohen | 099f60d | 2011-08-23 21:07:26 -0700 | [diff] [blame] | 159 | |
Adam Cohen | 099f60d | 2011-08-23 21:07:26 -0700 | [diff] [blame] | 160 | icon.mFolderRingAnimator = new FolderRingAnimator(launcher, icon); |
| 161 | folderInfo.addListener(icon); |
| 162 | |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 163 | return icon; |
| 164 | } |
| 165 | |
Adam Cohen | 099f60d | 2011-08-23 21:07:26 -0700 | [diff] [blame] | 166 | @Override |
| 167 | protected Parcelable onSaveInstanceState() { |
| 168 | sStaticValuesDirty = true; |
| 169 | return super.onSaveInstanceState(); |
| 170 | } |
| 171 | |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 172 | public static class FolderRingAnimator { |
Adam Cohen | 69ce2e5 | 2011-07-03 19:25:21 -0700 | [diff] [blame] | 173 | public int mCellX; |
| 174 | public int mCellY; |
| 175 | private CellLayout mCellLayout; |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 176 | public float mOuterRingSize; |
| 177 | public float mInnerRingSize; |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 178 | public FolderIcon mFolderIcon = null; |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 179 | public static Drawable sSharedOuterRingDrawable = null; |
| 180 | public static Drawable sSharedInnerRingDrawable = null; |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 181 | public static int sPreviewSize = -1; |
| 182 | public static int sPreviewPadding = -1; |
| 183 | |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 184 | private ValueAnimator mAcceptAnimator; |
| 185 | private ValueAnimator mNeutralAnimator; |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 186 | |
| 187 | public FolderRingAnimator(Launcher launcher, FolderIcon folderIcon) { |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 188 | mFolderIcon = folderIcon; |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 189 | Resources res = launcher.getResources(); |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 190 | |
Adam Cohen | 099f60d | 2011-08-23 21:07:26 -0700 | [diff] [blame] | 191 | // We need to reload the static values when configuration changes in case they are |
| 192 | // different in another configuration |
| 193 | if (sStaticValuesDirty) { |
Daniel Sandler | 24ad0f0 | 2013-07-31 00:14:58 -0400 | [diff] [blame] | 194 | if (Looper.myLooper() != Looper.getMainLooper()) { |
| 195 | throw new RuntimeException("FolderRingAnimator loading drawables on non-UI thread " |
| 196 | + Thread.currentThread()); |
| 197 | } |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 198 | |
| 199 | LauncherAppState app = LauncherAppState.getInstance(); |
| 200 | DeviceProfile grid = app.getDynamicGrid().getDeviceProfile(); |
| 201 | sPreviewSize = grid.folderIconSizePx; |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 202 | sPreviewPadding = res.getDimensionPixelSize(R.dimen.folder_preview_padding); |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 203 | sSharedOuterRingDrawable = res.getDrawable(R.drawable.portal_ring_outer_holo); |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 204 | sSharedInnerRingDrawable = res.getDrawable(R.drawable.portal_ring_inner_holo); |
Adam Cohen | df6af57 | 2011-10-19 14:38:16 -0700 | [diff] [blame] | 205 | sSharedFolderLeaveBehind = res.getDrawable(R.drawable.portal_ring_rest); |
Adam Cohen | 099f60d | 2011-08-23 21:07:26 -0700 | [diff] [blame] | 206 | sStaticValuesDirty = false; |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 207 | } |
Adam Cohen | 073a46f | 2011-05-17 16:28:09 -0700 | [diff] [blame] | 208 | } |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 209 | |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 210 | public void animateToAcceptState() { |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 211 | if (mNeutralAnimator != null) { |
| 212 | mNeutralAnimator.cancel(); |
| 213 | } |
Michael Jurka | f1ad608 | 2013-03-13 12:55:46 +0100 | [diff] [blame] | 214 | mAcceptAnimator = LauncherAnimUtils.ofFloat(mCellLayout, 0f, 1f); |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 215 | mAcceptAnimator.setDuration(CONSUMPTION_ANIMATION_DURATION); |
Andrew Flynn | 850d2e7 | 2012-04-26 16:51:20 -0700 | [diff] [blame] | 216 | |
| 217 | final int previewSize = sPreviewSize; |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 218 | mAcceptAnimator.addUpdateListener(new AnimatorUpdateListener() { |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 219 | public void onAnimationUpdate(ValueAnimator animation) { |
| 220 | final float percent = (Float) animation.getAnimatedValue(); |
Andrew Flynn | 850d2e7 | 2012-04-26 16:51:20 -0700 | [diff] [blame] | 221 | mOuterRingSize = (1 + percent * OUTER_RING_GROWTH_FACTOR) * previewSize; |
| 222 | mInnerRingSize = (1 + percent * INNER_RING_GROWTH_FACTOR) * previewSize; |
Adam Cohen | 69ce2e5 | 2011-07-03 19:25:21 -0700 | [diff] [blame] | 223 | if (mCellLayout != null) { |
| 224 | mCellLayout.invalidate(); |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | }); |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 228 | mAcceptAnimator.addListener(new AnimatorListenerAdapter() { |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 229 | @Override |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 230 | public void onAnimationStart(Animator animation) { |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 231 | if (mFolderIcon != null) { |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 232 | mFolderIcon.mPreviewBackground.setVisibility(INVISIBLE); |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 233 | } |
| 234 | } |
| 235 | }); |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 236 | mAcceptAnimator.start(); |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | public void animateToNaturalState() { |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 240 | if (mAcceptAnimator != null) { |
| 241 | mAcceptAnimator.cancel(); |
| 242 | } |
Michael Jurka | f1ad608 | 2013-03-13 12:55:46 +0100 | [diff] [blame] | 243 | mNeutralAnimator = LauncherAnimUtils.ofFloat(mCellLayout, 0f, 1f); |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 244 | mNeutralAnimator.setDuration(CONSUMPTION_ANIMATION_DURATION); |
Andrew Flynn | 850d2e7 | 2012-04-26 16:51:20 -0700 | [diff] [blame] | 245 | |
| 246 | final int previewSize = sPreviewSize; |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 247 | mNeutralAnimator.addUpdateListener(new AnimatorUpdateListener() { |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 248 | public void onAnimationUpdate(ValueAnimator animation) { |
| 249 | final float percent = (Float) animation.getAnimatedValue(); |
Andrew Flynn | 850d2e7 | 2012-04-26 16:51:20 -0700 | [diff] [blame] | 250 | mOuterRingSize = (1 + (1 - percent) * OUTER_RING_GROWTH_FACTOR) * previewSize; |
| 251 | mInnerRingSize = (1 + (1 - percent) * INNER_RING_GROWTH_FACTOR) * previewSize; |
Adam Cohen | 69ce2e5 | 2011-07-03 19:25:21 -0700 | [diff] [blame] | 252 | if (mCellLayout != null) { |
| 253 | mCellLayout.invalidate(); |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | }); |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 257 | mNeutralAnimator.addListener(new AnimatorListenerAdapter() { |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 258 | @Override |
| 259 | public void onAnimationEnd(Animator animation) { |
Adam Cohen | 69ce2e5 | 2011-07-03 19:25:21 -0700 | [diff] [blame] | 260 | if (mCellLayout != null) { |
| 261 | mCellLayout.hideFolderAccept(FolderRingAnimator.this); |
| 262 | } |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 263 | if (mFolderIcon != null) { |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 264 | mFolderIcon.mPreviewBackground.setVisibility(VISIBLE); |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 265 | } |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 266 | } |
| 267 | }); |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 268 | mNeutralAnimator.start(); |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 269 | } |
| 270 | |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 271 | // Location is expressed in window coordinates |
Adam Cohen | 69ce2e5 | 2011-07-03 19:25:21 -0700 | [diff] [blame] | 272 | public void getCell(int[] loc) { |
| 273 | loc[0] = mCellX; |
| 274 | loc[1] = mCellY; |
| 275 | } |
| 276 | |
| 277 | // Location is expressed in window coordinates |
| 278 | public void setCell(int x, int y) { |
| 279 | mCellX = x; |
| 280 | mCellY = y; |
| 281 | } |
| 282 | |
| 283 | public void setCellLayout(CellLayout layout) { |
| 284 | mCellLayout = layout; |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 285 | } |
| 286 | |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 287 | public float getOuterRingSize() { |
| 288 | return mOuterRingSize; |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 289 | } |
| 290 | |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 291 | public float getInnerRingSize() { |
| 292 | return mInnerRingSize; |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 293 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 294 | } |
| 295 | |
Adam Cohen | fb91f30 | 2012-06-11 15:45:18 -0700 | [diff] [blame] | 296 | Folder getFolder() { |
| 297 | return mFolder; |
| 298 | } |
| 299 | |
| 300 | FolderInfo getFolderInfo() { |
| 301 | return mInfo; |
| 302 | } |
| 303 | |
Adam Cohen | 073a46f | 2011-05-17 16:28:09 -0700 | [diff] [blame] | 304 | private boolean willAcceptItem(ItemInfo item) { |
| 305 | final int itemType = item.itemType; |
| 306 | return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION || |
| 307 | itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) && |
Adam Cohen | c51934b | 2011-07-26 21:07:43 -0700 | [diff] [blame] | 308 | !mFolder.isFull() && item != mInfo && !mInfo.opened); |
Adam Cohen | 073a46f | 2011-05-17 16:28:09 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 311 | public boolean acceptDrop(Object dragInfo) { |
| 312 | final ItemInfo item = (ItemInfo) dragInfo; |
Adam Cohen | fb91f30 | 2012-06-11 15:45:18 -0700 | [diff] [blame] | 313 | return !mFolder.isDestroyed() && willAcceptItem(item); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 314 | } |
| 315 | |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 316 | public void addItem(ShortcutInfo item) { |
| 317 | mInfo.add(item); |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 318 | } |
| 319 | |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 320 | public void onDragEnter(Object dragInfo) { |
Adam Cohen | fb91f30 | 2012-06-11 15:45:18 -0700 | [diff] [blame] | 321 | if (mFolder.isDestroyed() || !willAcceptItem((ItemInfo) dragInfo)) return; |
Adam Cohen | 69ce2e5 | 2011-07-03 19:25:21 -0700 | [diff] [blame] | 322 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) getLayoutParams(); |
| 323 | CellLayout layout = (CellLayout) getParent().getParent(); |
| 324 | mFolderRingAnimator.setCell(lp.cellX, lp.cellY); |
| 325 | mFolderRingAnimator.setCellLayout(layout); |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 326 | mFolderRingAnimator.animateToAcceptState(); |
Adam Cohen | 69ce2e5 | 2011-07-03 19:25:21 -0700 | [diff] [blame] | 327 | layout.showFolderAccept(mFolderRingAnimator); |
Adam Cohen | 073a46f | 2011-05-17 16:28:09 -0700 | [diff] [blame] | 328 | } |
| 329 | |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 330 | public void onDragOver(Object dragInfo) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 331 | } |
| 332 | |
Adam Cohen | d044526 | 2011-07-04 23:53:22 -0700 | [diff] [blame] | 333 | public void performCreateAnimation(final ShortcutInfo destInfo, final View destView, |
Winson Chung | 7bd1bbb | 2012-02-13 18:29:29 -0800 | [diff] [blame] | 334 | final ShortcutInfo srcInfo, final DragView srcView, Rect dstRect, |
Adam Cohen | ac8c876 | 2011-07-13 11:15:27 -0700 | [diff] [blame] | 335 | float scaleRelativeToDragLayer, Runnable postAnimationRunnable) { |
Adam Cohen | d044526 | 2011-07-04 23:53:22 -0700 | [diff] [blame] | 336 | |
Adam Cohen | fb91f30 | 2012-06-11 15:45:18 -0700 | [diff] [blame] | 337 | // These correspond two the drawable and view that the icon was dropped _onto_ |
Adam Cohen | d044526 | 2011-07-04 23:53:22 -0700 | [diff] [blame] | 338 | Drawable animateDrawable = ((TextView) destView).getCompoundDrawables()[1]; |
Adam Cohen | fb91f30 | 2012-06-11 15:45:18 -0700 | [diff] [blame] | 339 | computePreviewDrawingParams(animateDrawable.getIntrinsicWidth(), |
| 340 | destView.getMeasuredWidth()); |
Adam Cohen | d044526 | 2011-07-04 23:53:22 -0700 | [diff] [blame] | 341 | |
| 342 | // This will animate the first item from it's position as an icon into its |
| 343 | // position as the first item in the preview |
Adam Cohen | fb91f30 | 2012-06-11 15:45:18 -0700 | [diff] [blame] | 344 | animateFirstItem(animateDrawable, INITIAL_ITEM_ANIMATION_DURATION, false, null); |
Adam Cohen | 268c475 | 2012-06-06 17:47:33 -0700 | [diff] [blame] | 345 | addItem(destInfo); |
Adam Cohen | fb91f30 | 2012-06-11 15:45:18 -0700 | [diff] [blame] | 346 | |
| 347 | // This will animate the dragView (srcView) into the new folder |
| 348 | onDrop(srcInfo, srcView, dstRect, scaleRelativeToDragLayer, 1, postAnimationRunnable, null); |
| 349 | } |
| 350 | |
| 351 | public void performDestroyAnimation(final View finalView, Runnable onCompleteRunnable) { |
| 352 | Drawable animateDrawable = ((TextView) finalView).getCompoundDrawables()[1]; |
| 353 | computePreviewDrawingParams(animateDrawable.getIntrinsicWidth(), |
| 354 | finalView.getMeasuredWidth()); |
| 355 | |
| 356 | // This will animate the first item from it's position as an icon into its |
| 357 | // position as the first item in the preview |
| 358 | animateFirstItem(animateDrawable, FINAL_ITEM_ANIMATION_DURATION, true, |
| 359 | onCompleteRunnable); |
Adam Cohen | d044526 | 2011-07-04 23:53:22 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 362 | public void onDragExit(Object dragInfo) { |
Adam Cohen | c6cc61d | 2012-04-04 12:47:08 -0700 | [diff] [blame] | 363 | onDragExit(); |
| 364 | } |
| 365 | |
| 366 | public void onDragExit() { |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 367 | mFolderRingAnimator.animateToNaturalState(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 368 | } |
Patrick Dubroy | 440c360 | 2010-07-13 17:50:32 -0700 | [diff] [blame] | 369 | |
Winson Chung | 7bd1bbb | 2012-02-13 18:29:29 -0800 | [diff] [blame] | 370 | private void onDrop(final ShortcutInfo item, DragView animateView, Rect finalRect, |
| 371 | float scaleRelativeToDragLayer, int index, Runnable postAnimationRunnable, |
| 372 | DragObject d) { |
Adam Cohen | d044526 | 2011-07-04 23:53:22 -0700 | [diff] [blame] | 373 | item.cellX = -1; |
| 374 | item.cellY = -1; |
Adam Cohen | d044526 | 2011-07-04 23:53:22 -0700 | [diff] [blame] | 375 | |
Adam Cohen | 558baaf | 2011-08-15 15:22:57 -0700 | [diff] [blame] | 376 | // Typically, the animateView corresponds to the DragView; however, if this is being done |
| 377 | // after a configuration activity (ie. for a Shortcut being dragged from AllApps) we |
| 378 | // will not have a view to animate |
| 379 | if (animateView != null) { |
| 380 | DragLayer dragLayer = mLauncher.getDragLayer(); |
| 381 | Rect from = new Rect(); |
| 382 | dragLayer.getViewRectRelativeToSelf(animateView, from); |
| 383 | Rect to = finalRect; |
| 384 | if (to == null) { |
| 385 | to = new Rect(); |
| 386 | Workspace workspace = mLauncher.getWorkspace(); |
| 387 | // Set cellLayout and this to it's final state to compute final animation locations |
| 388 | workspace.setFinalTransitionTransform((CellLayout) getParent().getParent()); |
| 389 | float scaleX = getScaleX(); |
| 390 | float scaleY = getScaleY(); |
| 391 | setScaleX(1.0f); |
| 392 | setScaleY(1.0f); |
| 393 | scaleRelativeToDragLayer = dragLayer.getDescendantRectRelativeToSelf(this, to); |
| 394 | // Finished computing final animation locations, restore current state |
| 395 | setScaleX(scaleX); |
| 396 | setScaleY(scaleY); |
| 397 | workspace.resetTransitionTransform((CellLayout) getParent().getParent()); |
Adam Cohen | d044526 | 2011-07-04 23:53:22 -0700 | [diff] [blame] | 398 | } |
Adam Cohen | 558baaf | 2011-08-15 15:22:57 -0700 | [diff] [blame] | 399 | |
| 400 | int[] center = new int[2]; |
| 401 | float scale = getLocalCenterForIndex(index, center); |
| 402 | center[0] = (int) Math.round(scaleRelativeToDragLayer * center[0]); |
| 403 | center[1] = (int) Math.round(scaleRelativeToDragLayer * center[1]); |
| 404 | |
| 405 | to.offset(center[0] - animateView.getMeasuredWidth() / 2, |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 406 | center[1] - animateView.getMeasuredHeight() / 2); |
Adam Cohen | 558baaf | 2011-08-15 15:22:57 -0700 | [diff] [blame] | 407 | |
| 408 | float finalAlpha = index < NUM_ITEMS_IN_PREVIEW ? 0.5f : 0f; |
| 409 | |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 410 | float finalScale = scale * scaleRelativeToDragLayer; |
Adam Cohen | 558baaf | 2011-08-15 15:22:57 -0700 | [diff] [blame] | 411 | dragLayer.animateView(animateView, from, to, finalAlpha, |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 412 | 1, 1, finalScale, finalScale, DROP_IN_ANIMATION_DURATION, |
Adam Cohen | 558baaf | 2011-08-15 15:22:57 -0700 | [diff] [blame] | 413 | new DecelerateInterpolator(2), new AccelerateInterpolator(2), |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 414 | postAnimationRunnable, DragLayer.ANIMATION_END_DISAPPEAR, null); |
Adam Cohen | fb91f30 | 2012-06-11 15:45:18 -0700 | [diff] [blame] | 415 | addItem(item); |
| 416 | mHiddenItems.add(item); |
Adam Cohen | 7a8b82b | 2013-05-29 18:41:50 -0700 | [diff] [blame] | 417 | mFolder.hideItem(item); |
Adam Cohen | 558baaf | 2011-08-15 15:22:57 -0700 | [diff] [blame] | 418 | postDelayed(new Runnable() { |
| 419 | public void run() { |
Adam Cohen | fb91f30 | 2012-06-11 15:45:18 -0700 | [diff] [blame] | 420 | mHiddenItems.remove(item); |
Adam Cohen | 7a8b82b | 2013-05-29 18:41:50 -0700 | [diff] [blame] | 421 | mFolder.showItem(item); |
Adam Cohen | fb91f30 | 2012-06-11 15:45:18 -0700 | [diff] [blame] | 422 | invalidate(); |
Adam Cohen | 558baaf | 2011-08-15 15:22:57 -0700 | [diff] [blame] | 423 | } |
| 424 | }, DROP_IN_ANIMATION_DURATION); |
| 425 | } else { |
| 426 | addItem(item); |
| 427 | } |
Adam Cohen | d044526 | 2011-07-04 23:53:22 -0700 | [diff] [blame] | 428 | } |
| 429 | |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 430 | public void onDrop(DragObject d) { |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 431 | ShortcutInfo item; |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 432 | if (d.dragInfo instanceof AppInfo) { |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 433 | // Came from all apps -- make a copy |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 434 | item = ((AppInfo) d.dragInfo).makeShortcut(); |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 435 | } else { |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 436 | item = (ShortcutInfo) d.dragInfo; |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 437 | } |
Adam Cohen | 67bd9cc | 2011-07-29 14:07:04 -0700 | [diff] [blame] | 438 | mFolder.notifyDrop(); |
Winson Chung | 7bd1bbb | 2012-02-13 18:29:29 -0800 | [diff] [blame] | 439 | onDrop(item, d.dragView, null, 1.0f, mInfo.contents.size(), d.postAnimationRunnable, d); |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 440 | } |
| 441 | |
Adam Cohen | d044526 | 2011-07-04 23:53:22 -0700 | [diff] [blame] | 442 | private void computePreviewDrawingParams(int drawableSize, int totalSize) { |
| 443 | if (mIntrinsicIconSize != drawableSize || mTotalWidth != totalSize) { |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 444 | LauncherAppState app = LauncherAppState.getInstance(); |
| 445 | DeviceProfile grid = app.getDynamicGrid().getDeviceProfile(); |
| 446 | |
Adam Cohen | d044526 | 2011-07-04 23:53:22 -0700 | [diff] [blame] | 447 | mIntrinsicIconSize = drawableSize; |
| 448 | mTotalWidth = totalSize; |
| 449 | |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 450 | final int previewSize = mPreviewBackground.getLayoutParams().height; |
Adam Cohen | d044526 | 2011-07-04 23:53:22 -0700 | [diff] [blame] | 451 | final int previewPadding = FolderRingAnimator.sPreviewPadding; |
| 452 | |
| 453 | mAvailableSpaceInPreview = (previewSize - 2 * previewPadding); |
| 454 | // cos(45) = 0.707 + ~= 0.1) = 0.8f |
| 455 | int adjustedAvailableSpace = (int) ((mAvailableSpaceInPreview / 2) * (1 + 0.8f)); |
| 456 | |
| 457 | int unscaledHeight = (int) (mIntrinsicIconSize * (1 + PERSPECTIVE_SHIFT_FACTOR)); |
| 458 | mBaselineIconScale = (1.0f * adjustedAvailableSpace / unscaledHeight); |
| 459 | |
| 460 | mBaselineIconSize = (int) (mIntrinsicIconSize * mBaselineIconScale); |
| 461 | mMaxPerspectiveShift = mBaselineIconSize * PERSPECTIVE_SHIFT_FACTOR; |
| 462 | |
| 463 | mPreviewOffsetX = (mTotalWidth - mAvailableSpaceInPreview) / 2; |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 464 | mPreviewOffsetY = previewPadding + grid.folderBackgroundOffset; |
Adam Cohen | d044526 | 2011-07-04 23:53:22 -0700 | [diff] [blame] | 465 | } |
| 466 | } |
| 467 | |
| 468 | private void computePreviewDrawingParams(Drawable d) { |
| 469 | computePreviewDrawingParams(d.getIntrinsicWidth(), getMeasuredWidth()); |
| 470 | } |
| 471 | |
| 472 | class PreviewItemDrawingParams { |
| 473 | PreviewItemDrawingParams(float transX, float transY, float scale, int overlayAlpha) { |
| 474 | this.transX = transX; |
| 475 | this.transY = transY; |
| 476 | this.scale = scale; |
| 477 | this.overlayAlpha = overlayAlpha; |
| 478 | } |
| 479 | float transX; |
| 480 | float transY; |
| 481 | float scale; |
| 482 | int overlayAlpha; |
| 483 | Drawable drawable; |
| 484 | } |
| 485 | |
Adam Cohen | ac8c876 | 2011-07-13 11:15:27 -0700 | [diff] [blame] | 486 | private float getLocalCenterForIndex(int index, int[] center) { |
Adam Cohen | d044526 | 2011-07-04 23:53:22 -0700 | [diff] [blame] | 487 | mParams = computePreviewItemDrawingParams(Math.min(NUM_ITEMS_IN_PREVIEW, index), mParams); |
| 488 | |
| 489 | mParams.transX += mPreviewOffsetX; |
| 490 | mParams.transY += mPreviewOffsetY; |
Adam Cohen | ac8c876 | 2011-07-13 11:15:27 -0700 | [diff] [blame] | 491 | float offsetX = mParams.transX + (mParams.scale * mIntrinsicIconSize) / 2; |
| 492 | float offsetY = mParams.transY + (mParams.scale * mIntrinsicIconSize) / 2; |
Adam Cohen | d044526 | 2011-07-04 23:53:22 -0700 | [diff] [blame] | 493 | |
Adam Cohen | ac8c876 | 2011-07-13 11:15:27 -0700 | [diff] [blame] | 494 | center[0] = (int) Math.round(offsetX); |
| 495 | center[1] = (int) Math.round(offsetY); |
Adam Cohen | d044526 | 2011-07-04 23:53:22 -0700 | [diff] [blame] | 496 | return mParams.scale; |
| 497 | } |
| 498 | |
| 499 | private PreviewItemDrawingParams computePreviewItemDrawingParams(int index, |
| 500 | PreviewItemDrawingParams params) { |
| 501 | index = NUM_ITEMS_IN_PREVIEW - index - 1; |
| 502 | float r = (index * 1.0f) / (NUM_ITEMS_IN_PREVIEW - 1); |
| 503 | float scale = (1 - PERSPECTIVE_SCALE_FACTOR * (1 - r)); |
| 504 | |
| 505 | float offset = (1 - r) * mMaxPerspectiveShift; |
| 506 | float scaledSize = scale * mBaselineIconSize; |
| 507 | float scaleOffsetCorrection = (1 - scale) * mBaselineIconSize; |
| 508 | |
| 509 | // We want to imagine our coordinates from the bottom left, growing up and to the |
| 510 | // right. This is natural for the x-axis, but for the y-axis, we have to invert things. |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 511 | float transY = mAvailableSpaceInPreview - (offset + scaledSize + scaleOffsetCorrection) + getPaddingTop(); |
Adam Cohen | d044526 | 2011-07-04 23:53:22 -0700 | [diff] [blame] | 512 | float transX = offset + scaleOffsetCorrection; |
| 513 | float totalScale = mBaselineIconScale * scale; |
| 514 | final int overlayAlpha = (int) (80 * (1 - r)); |
| 515 | |
| 516 | if (params == null) { |
| 517 | params = new PreviewItemDrawingParams(transX, transY, totalScale, overlayAlpha); |
| 518 | } else { |
| 519 | params.transX = transX; |
| 520 | params.transY = transY; |
| 521 | params.scale = totalScale; |
| 522 | params.overlayAlpha = overlayAlpha; |
| 523 | } |
| 524 | return params; |
| 525 | } |
| 526 | |
| 527 | private void drawPreviewItem(Canvas canvas, PreviewItemDrawingParams params) { |
| 528 | canvas.save(); |
| 529 | canvas.translate(params.transX + mPreviewOffsetX, params.transY + mPreviewOffsetY); |
| 530 | canvas.scale(params.scale, params.scale); |
| 531 | Drawable d = params.drawable; |
| 532 | |
| 533 | if (d != null) { |
| 534 | d.setBounds(0, 0, mIntrinsicIconSize, mIntrinsicIconSize); |
| 535 | d.setFilterBitmap(true); |
| 536 | d.setColorFilter(Color.argb(params.overlayAlpha, 0, 0, 0), PorterDuff.Mode.SRC_ATOP); |
| 537 | d.draw(canvas); |
| 538 | d.clearColorFilter(); |
| 539 | d.setFilterBitmap(false); |
| 540 | } |
| 541 | canvas.restore(); |
| 542 | } |
| 543 | |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 544 | @Override |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 545 | protected void dispatchDraw(Canvas canvas) { |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 546 | super.dispatchDraw(canvas); |
| 547 | |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 548 | if (mFolder == null) return; |
Adam Cohen | d044526 | 2011-07-04 23:53:22 -0700 | [diff] [blame] | 549 | if (mFolder.getItemCount() == 0 && !mAnimating) return; |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 550 | |
Adam Cohen | 7a8b82b | 2013-05-29 18:41:50 -0700 | [diff] [blame] | 551 | ArrayList<View> items = mFolder.getItemsInReadingOrder(); |
Adam Cohen | d044526 | 2011-07-04 23:53:22 -0700 | [diff] [blame] | 552 | Drawable d; |
| 553 | TextView v; |
Adam Cohen | badf71e | 2011-05-26 19:08:29 -0700 | [diff] [blame] | 554 | |
Adam Cohen | d044526 | 2011-07-04 23:53:22 -0700 | [diff] [blame] | 555 | // Update our drawing parameters if necessary |
| 556 | if (mAnimating) { |
| 557 | computePreviewDrawingParams(mAnimParams.drawable); |
| 558 | } else { |
| 559 | v = (TextView) items.get(0); |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 560 | d = v.getCompoundDrawables()[1]; |
Adam Cohen | d044526 | 2011-07-04 23:53:22 -0700 | [diff] [blame] | 561 | computePreviewDrawingParams(d); |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 562 | } |
Adam Cohen | d044526 | 2011-07-04 23:53:22 -0700 | [diff] [blame] | 563 | |
| 564 | int nItemsInPreview = Math.min(items.size(), NUM_ITEMS_IN_PREVIEW); |
| 565 | if (!mAnimating) { |
| 566 | for (int i = nItemsInPreview - 1; i >= 0; i--) { |
| 567 | v = (TextView) items.get(i); |
Adam Cohen | fb91f30 | 2012-06-11 15:45:18 -0700 | [diff] [blame] | 568 | if (!mHiddenItems.contains(v.getTag())) { |
| 569 | d = v.getCompoundDrawables()[1]; |
| 570 | mParams = computePreviewItemDrawingParams(i, mParams); |
| 571 | mParams.drawable = d; |
| 572 | drawPreviewItem(canvas, mParams); |
| 573 | } |
Adam Cohen | d044526 | 2011-07-04 23:53:22 -0700 | [diff] [blame] | 574 | } |
| 575 | } else { |
| 576 | drawPreviewItem(canvas, mAnimParams); |
| 577 | } |
| 578 | } |
| 579 | |
Adam Cohen | fb91f30 | 2012-06-11 15:45:18 -0700 | [diff] [blame] | 580 | private void animateFirstItem(final Drawable d, int duration, final boolean reverse, |
| 581 | final Runnable onCompleteRunnable) { |
Adam Cohen | d044526 | 2011-07-04 23:53:22 -0700 | [diff] [blame] | 582 | final PreviewItemDrawingParams finalParams = computePreviewItemDrawingParams(0, null); |
| 583 | |
| 584 | final float scale0 = 1.0f; |
Adam Cohen | 1d4ee4e | 2011-08-12 15:51:59 -0700 | [diff] [blame] | 585 | final float transX0 = (mAvailableSpaceInPreview - d.getIntrinsicWidth()) / 2; |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 586 | final float transY0 = (mAvailableSpaceInPreview - d.getIntrinsicHeight()) / 2 + getPaddingTop(); |
Adam Cohen | d044526 | 2011-07-04 23:53:22 -0700 | [diff] [blame] | 587 | mAnimParams.drawable = d; |
| 588 | |
Michael Jurka | f1ad608 | 2013-03-13 12:55:46 +0100 | [diff] [blame] | 589 | ValueAnimator va = LauncherAnimUtils.ofFloat(this, 0f, 1.0f); |
Adam Cohen | d044526 | 2011-07-04 23:53:22 -0700 | [diff] [blame] | 590 | va.addUpdateListener(new AnimatorUpdateListener(){ |
| 591 | public void onAnimationUpdate(ValueAnimator animation) { |
| 592 | float progress = (Float) animation.getAnimatedValue(); |
Adam Cohen | fb91f30 | 2012-06-11 15:45:18 -0700 | [diff] [blame] | 593 | if (reverse) { |
| 594 | progress = 1 - progress; |
| 595 | mPreviewBackground.setAlpha(progress); |
| 596 | } |
Adam Cohen | d044526 | 2011-07-04 23:53:22 -0700 | [diff] [blame] | 597 | |
| 598 | mAnimParams.transX = transX0 + progress * (finalParams.transX - transX0); |
| 599 | mAnimParams.transY = transY0 + progress * (finalParams.transY - transY0); |
| 600 | mAnimParams.scale = scale0 + progress * (finalParams.scale - scale0); |
| 601 | invalidate(); |
| 602 | } |
| 603 | }); |
| 604 | va.addListener(new AnimatorListenerAdapter() { |
| 605 | @Override |
| 606 | public void onAnimationStart(Animator animation) { |
| 607 | mAnimating = true; |
| 608 | } |
| 609 | @Override |
| 610 | public void onAnimationEnd(Animator animation) { |
| 611 | mAnimating = false; |
Adam Cohen | fb91f30 | 2012-06-11 15:45:18 -0700 | [diff] [blame] | 612 | if (onCompleteRunnable != null) { |
| 613 | onCompleteRunnable.run(); |
| 614 | } |
Adam Cohen | d044526 | 2011-07-04 23:53:22 -0700 | [diff] [blame] | 615 | } |
| 616 | }); |
| 617 | va.setDuration(duration); |
| 618 | va.start(); |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 619 | } |
| 620 | |
Adam Cohen | 099f60d | 2011-08-23 21:07:26 -0700 | [diff] [blame] | 621 | public void setTextVisible(boolean visible) { |
| 622 | if (visible) { |
| 623 | mFolderName.setVisibility(VISIBLE); |
| 624 | } else { |
| 625 | mFolderName.setVisibility(INVISIBLE); |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | public boolean getTextVisible() { |
| 630 | return mFolderName.getVisibility() == VISIBLE; |
| 631 | } |
| 632 | |
Adam Cohen | 76078c4 | 2011-06-09 15:06:52 -0700 | [diff] [blame] | 633 | public void onItemsChanged() { |
| 634 | invalidate(); |
| 635 | requestLayout(); |
| 636 | } |
| 637 | |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 638 | public void onAdd(ShortcutInfo item) { |
| 639 | invalidate(); |
| 640 | requestLayout(); |
| 641 | } |
| 642 | |
| 643 | public void onRemove(ShortcutInfo item) { |
| 644 | invalidate(); |
| 645 | requestLayout(); |
| 646 | } |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 647 | |
| 648 | public void onTitleChanged(CharSequence title) { |
Adam Cohen | d63cfa9 | 2011-06-24 14:17:17 -0700 | [diff] [blame] | 649 | mFolderName.setText(title.toString()); |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 650 | setContentDescription(String.format(getContext().getString(R.string.folder_name_format), |
Adam Cohen | 3371da0 | 2011-10-25 21:38:29 -0700 | [diff] [blame] | 651 | title)); |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 652 | } |
Winson Chung | 88f3345 | 2012-02-23 15:23:44 -0800 | [diff] [blame] | 653 | |
| 654 | @Override |
| 655 | public boolean onTouchEvent(MotionEvent event) { |
| 656 | // Call the superclass onTouchEvent first, because sometimes it changes the state to |
| 657 | // isPressed() on an ACTION_UP |
| 658 | boolean result = super.onTouchEvent(event); |
| 659 | |
| 660 | switch (event.getAction()) { |
| 661 | case MotionEvent.ACTION_DOWN: |
| 662 | mLongPressHelper.postCheckForLongPress(); |
| 663 | break; |
| 664 | case MotionEvent.ACTION_CANCEL: |
| 665 | case MotionEvent.ACTION_UP: |
| 666 | mLongPressHelper.cancelLongPress(); |
| 667 | break; |
| 668 | } |
| 669 | return result; |
| 670 | } |
| 671 | |
| 672 | @Override |
| 673 | public void cancelLongPress() { |
| 674 | super.cancelLongPress(); |
| 675 | |
| 676 | mLongPressHelper.cancelLongPress(); |
| 677 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 678 | } |