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 | |
Joe Onorato | a590252 | 2009-07-30 13:37:37 -0700 | [diff] [blame] | 17 | package com.android.launcher2; |
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; |
| 30 | import android.util.AttributeSet; |
| 31 | import android.view.LayoutInflater; |
Adam Cohen | 7c69321 | 2011-05-18 15:26:57 -0700 | [diff] [blame] | 32 | import android.view.View; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 33 | import android.view.ViewGroup; |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 34 | import android.view.animation.AccelerateInterpolator; |
| 35 | import android.view.animation.DecelerateInterpolator; |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 36 | import android.widget.ImageView; |
| 37 | import android.widget.LinearLayout; |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 38 | import android.widget.TextView; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 39 | |
Romain Guy | edcce09 | 2010-03-04 13:03:17 -0800 | [diff] [blame] | 40 | import com.android.launcher.R; |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 41 | import com.android.launcher2.DropTarget.DragObject; |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 42 | import com.android.launcher2.FolderInfo.FolderListener; |
Romain Guy | edcce09 | 2010-03-04 13:03:17 -0800 | [diff] [blame] | 43 | |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 44 | import java.util.ArrayList; |
| 45 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 46 | /** |
| 47 | * An icon that can appear on in the workspace representing an {@link UserFolder}. |
| 48 | */ |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 49 | public class FolderIcon extends LinearLayout implements FolderListener { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 50 | private Launcher mLauncher; |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 51 | Folder mFolder; |
| 52 | FolderInfo mInfo; |
| 53 | |
Adam Cohen | badf71e | 2011-05-26 19:08:29 -0700 | [diff] [blame] | 54 | // The number of icons to display in the |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 55 | private static final int NUM_ITEMS_IN_PREVIEW = 3; |
Adam Cohen | f4b0891 | 2011-05-17 18:45:47 -0700 | [diff] [blame] | 56 | private static final int CONSUMPTION_ANIMATION_DURATION = 100; |
Adam Cohen | d044526 | 2011-07-04 23:53:22 -0700 | [diff] [blame] | 57 | private static final int DROP_IN_ANIMATION_DURATION = 400; |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 58 | private static final int INITIAL_ITEM_ANIMATION_DURATION = 350; |
Adam Cohen | badf71e | 2011-05-26 19:08:29 -0700 | [diff] [blame] | 59 | |
| 60 | // The degree to which the inner ring grows when accepting drop |
Adam Cohen | 69ce2e5 | 2011-07-03 19:25:21 -0700 | [diff] [blame] | 61 | private static final float INNER_RING_GROWTH_FACTOR = 0.15f; |
Adam Cohen | badf71e | 2011-05-26 19:08:29 -0700 | [diff] [blame] | 62 | |
Adam Cohen | badf71e | 2011-05-26 19:08:29 -0700 | [diff] [blame] | 63 | // 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] | 64 | private static final float OUTER_RING_GROWTH_FACTOR = 0.3f; |
Adam Cohen | badf71e | 2011-05-26 19:08:29 -0700 | [diff] [blame] | 65 | |
| 66 | // The amount of vertical spread between items in the stack [0...1] |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 67 | private static final float PERSPECTIVE_SHIFT_FACTOR = 0.24f; |
Adam Cohen | badf71e | 2011-05-26 19:08:29 -0700 | [diff] [blame] | 68 | |
| 69 | // The degree to which the item in the back of the stack is scaled [0...1] |
| 70 | // (0 means it's not scaled at all, 1 means it's scaled to nothing) |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 71 | private static final float PERSPECTIVE_SCALE_FACTOR = 0.35f; |
Adam Cohen | badf71e | 2011-05-26 19:08:29 -0700 | [diff] [blame] | 72 | |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 73 | private ImageView mPreviewBackground; |
| 74 | private BubbleTextView mFolderName; |
Adam Cohen | 073a46f | 2011-05-17 16:28:09 -0700 | [diff] [blame] | 75 | |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 76 | FolderRingAnimator mFolderRingAnimator = null; |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 77 | |
Adam Cohen | d044526 | 2011-07-04 23:53:22 -0700 | [diff] [blame] | 78 | // These variables are all associated with the drawing of the preview; they are stored |
| 79 | // as member variables for shared usage and to avoid computation on each frame |
| 80 | private int mIntrinsicIconSize; |
| 81 | private float mBaselineIconScale; |
| 82 | private int mBaselineIconSize; |
| 83 | private int mAvailableSpaceInPreview; |
| 84 | private int mTotalWidth = -1; |
| 85 | private int mPreviewOffsetX; |
| 86 | private int mPreviewOffsetY; |
| 87 | private float mMaxPerspectiveShift; |
| 88 | boolean mAnimating = false; |
| 89 | private PreviewItemDrawingParams mParams = new PreviewItemDrawingParams(0, 0, 0, 0); |
| 90 | private PreviewItemDrawingParams mAnimParams = new PreviewItemDrawingParams(0, 0, 0, 0); |
| 91 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 92 | public FolderIcon(Context context, AttributeSet attrs) { |
| 93 | super(context, attrs); |
| 94 | } |
| 95 | |
| 96 | public FolderIcon(Context context) { |
| 97 | super(context); |
| 98 | } |
| 99 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 100 | public boolean isDropEnabled() { |
Winson Chung | 7a1d165 | 2011-03-18 15:56:01 -0700 | [diff] [blame] | 101 | final ViewGroup cellLayoutChildren = (ViewGroup) getParent(); |
| 102 | final ViewGroup cellLayout = (ViewGroup) cellLayoutChildren.getParent(); |
| 103 | final Workspace workspace = (Workspace) cellLayout.getParent(); |
| 104 | return !workspace.isSmall(); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 105 | } |
| 106 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 107 | static FolderIcon fromXml(int resId, Launcher launcher, ViewGroup group, |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 108 | FolderInfo folderInfo, IconCache iconCache) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 109 | |
Adam Cohen | d044526 | 2011-07-04 23:53:22 -0700 | [diff] [blame] | 110 | if (INITIAL_ITEM_ANIMATION_DURATION >= DROP_IN_ANIMATION_DURATION) { |
| 111 | throw new IllegalStateException("DROP_IN_ANIMATION_DURATION must be greater than " + |
| 112 | "INITIAL_ITEM_ANIMATION_DURATION, as sequencing of adding first two items " + |
| 113 | "is dependent on this"); |
| 114 | } |
| 115 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 116 | FolderIcon icon = (FolderIcon) LayoutInflater.from(launcher).inflate(resId, group, false); |
| 117 | |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 118 | icon.mFolderName = (BubbleTextView) icon.findViewById(R.id.folder_name); |
| 119 | icon.mFolderName.setText(folderInfo.title); |
| 120 | icon.mPreviewBackground = (ImageView) icon.findViewById(R.id.preview_background); |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 121 | icon.mPreviewBackground.setContentDescription(folderInfo.title); |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 122 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 123 | icon.setTag(folderInfo); |
| 124 | icon.setOnClickListener(launcher); |
| 125 | icon.mInfo = folderInfo; |
| 126 | icon.mLauncher = launcher; |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 127 | |
| 128 | Folder folder = Folder.fromXml(launcher); |
| 129 | folder.setDragController(launcher.getDragController()); |
| 130 | folder.setLauncher(launcher); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 131 | folder.setFolderIcon(icon); |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 132 | folder.bind(folderInfo); |
| 133 | icon.mFolder = folder; |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 134 | icon.mFolderRingAnimator = new FolderRingAnimator(launcher, icon); |
Adam Cohen | 69ce2e5 | 2011-07-03 19:25:21 -0700 | [diff] [blame] | 135 | |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 136 | folderInfo.addListener(icon); |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 137 | |
| 138 | return icon; |
| 139 | } |
| 140 | |
| 141 | public static class FolderRingAnimator { |
Adam Cohen | 69ce2e5 | 2011-07-03 19:25:21 -0700 | [diff] [blame] | 142 | public int mCellX; |
| 143 | public int mCellY; |
| 144 | private CellLayout mCellLayout; |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 145 | public float mOuterRingSize; |
| 146 | public float mInnerRingSize; |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 147 | public FolderIcon mFolderIcon = null; |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 148 | public Drawable mOuterRingDrawable = null; |
| 149 | public Drawable mInnerRingDrawable = null; |
| 150 | public static Drawable sSharedOuterRingDrawable = null; |
| 151 | public static Drawable sSharedInnerRingDrawable = null; |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 152 | public static int sPreviewSize = -1; |
| 153 | public static int sPreviewPadding = -1; |
| 154 | |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 155 | private ValueAnimator mAcceptAnimator; |
| 156 | private ValueAnimator mNeutralAnimator; |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 157 | |
| 158 | public FolderRingAnimator(Launcher launcher, FolderIcon folderIcon) { |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 159 | mFolderIcon = folderIcon; |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 160 | Resources res = launcher.getResources(); |
| 161 | mOuterRingDrawable = res.getDrawable(R.drawable.portal_ring_outer_holo); |
| 162 | mInnerRingDrawable = res.getDrawable(R.drawable.portal_ring_inner_holo); |
| 163 | |
| 164 | if (sPreviewSize < 0 || sPreviewPadding < 0) { |
| 165 | sPreviewSize = res.getDimensionPixelSize(R.dimen.folder_preview_size); |
| 166 | sPreviewPadding = res.getDimensionPixelSize(R.dimen.folder_preview_padding); |
| 167 | } |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 168 | if (sSharedOuterRingDrawable == null) { |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 169 | sSharedOuterRingDrawable = res.getDrawable(R.drawable.portal_ring_outer_holo); |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 170 | } |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 171 | if (sSharedInnerRingDrawable == null) { |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 172 | sSharedInnerRingDrawable = res.getDrawable(R.drawable.portal_ring_inner_holo); |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 173 | } |
Adam Cohen | 073a46f | 2011-05-17 16:28:09 -0700 | [diff] [blame] | 174 | } |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 175 | |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 176 | public void animateToAcceptState() { |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 177 | if (mNeutralAnimator != null) { |
| 178 | mNeutralAnimator.cancel(); |
| 179 | } |
| 180 | mAcceptAnimator = ValueAnimator.ofFloat(0f, 1f); |
| 181 | mAcceptAnimator.setDuration(CONSUMPTION_ANIMATION_DURATION); |
| 182 | mAcceptAnimator.addUpdateListener(new AnimatorUpdateListener() { |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 183 | public void onAnimationUpdate(ValueAnimator animation) { |
| 184 | final float percent = (Float) animation.getAnimatedValue(); |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 185 | mOuterRingSize = (1 + percent * OUTER_RING_GROWTH_FACTOR) * sPreviewSize; |
| 186 | mInnerRingSize = (1 + percent * INNER_RING_GROWTH_FACTOR) * sPreviewSize; |
Adam Cohen | 69ce2e5 | 2011-07-03 19:25:21 -0700 | [diff] [blame] | 187 | if (mCellLayout != null) { |
| 188 | mCellLayout.invalidate(); |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | }); |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 192 | mAcceptAnimator.addListener(new AnimatorListenerAdapter() { |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 193 | @Override |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 194 | public void onAnimationStart(Animator animation) { |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 195 | if (mFolderIcon != null) { |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 196 | mFolderIcon.mPreviewBackground.setVisibility(INVISIBLE); |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | }); |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 200 | mAcceptAnimator.start(); |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | public void animateToNaturalState() { |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 204 | if (mAcceptAnimator != null) { |
| 205 | mAcceptAnimator.cancel(); |
| 206 | } |
| 207 | mNeutralAnimator = ValueAnimator.ofFloat(0f, 1f); |
| 208 | mNeutralAnimator.setDuration(CONSUMPTION_ANIMATION_DURATION); |
| 209 | mNeutralAnimator.addUpdateListener(new AnimatorUpdateListener() { |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 210 | public void onAnimationUpdate(ValueAnimator animation) { |
| 211 | final float percent = (Float) animation.getAnimatedValue(); |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 212 | mOuterRingSize = (1 + (1 - percent) * OUTER_RING_GROWTH_FACTOR) * sPreviewSize; |
| 213 | mInnerRingSize = (1 + (1 - percent) * INNER_RING_GROWTH_FACTOR) * sPreviewSize; |
Adam Cohen | 69ce2e5 | 2011-07-03 19:25:21 -0700 | [diff] [blame] | 214 | if (mCellLayout != null) { |
| 215 | mCellLayout.invalidate(); |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | }); |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 219 | mNeutralAnimator.addListener(new AnimatorListenerAdapter() { |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 220 | @Override |
| 221 | public void onAnimationEnd(Animator animation) { |
Adam Cohen | 69ce2e5 | 2011-07-03 19:25:21 -0700 | [diff] [blame] | 222 | if (mCellLayout != null) { |
| 223 | mCellLayout.hideFolderAccept(FolderRingAnimator.this); |
| 224 | } |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 225 | if (mFolderIcon != null) { |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 226 | mFolderIcon.mPreviewBackground.setVisibility(VISIBLE); |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 227 | } |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 228 | } |
| 229 | }); |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 230 | mNeutralAnimator.start(); |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 233 | // Location is expressed in window coordinates |
Adam Cohen | 69ce2e5 | 2011-07-03 19:25:21 -0700 | [diff] [blame] | 234 | public void getCell(int[] loc) { |
| 235 | loc[0] = mCellX; |
| 236 | loc[1] = mCellY; |
| 237 | } |
| 238 | |
| 239 | // Location is expressed in window coordinates |
| 240 | public void setCell(int x, int y) { |
| 241 | mCellX = x; |
| 242 | mCellY = y; |
| 243 | } |
| 244 | |
| 245 | public void setCellLayout(CellLayout layout) { |
| 246 | mCellLayout = layout; |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 249 | public float getOuterRingSize() { |
| 250 | return mOuterRingSize; |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 253 | public float getInnerRingSize() { |
| 254 | return mInnerRingSize; |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 255 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 256 | } |
| 257 | |
Adam Cohen | 073a46f | 2011-05-17 16:28:09 -0700 | [diff] [blame] | 258 | private boolean willAcceptItem(ItemInfo item) { |
| 259 | final int itemType = item.itemType; |
| 260 | return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION || |
| 261 | itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) && |
| 262 | !mFolder.isFull() && item != mInfo); |
| 263 | } |
| 264 | |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 265 | public boolean acceptDrop(Object dragInfo) { |
| 266 | final ItemInfo item = (ItemInfo) dragInfo; |
Adam Cohen | 073a46f | 2011-05-17 16:28:09 -0700 | [diff] [blame] | 267 | return willAcceptItem(item); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 268 | } |
| 269 | |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 270 | public void addItem(ShortcutInfo item) { |
| 271 | mInfo.add(item); |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 272 | LauncherModel.addOrMoveItemInDatabase(mLauncher, item, mInfo.id, 0, item.cellX, item.cellY); |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 273 | } |
| 274 | |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 275 | public void onDragEnter(Object dragInfo) { |
| 276 | if (!willAcceptItem((ItemInfo) dragInfo)) return; |
Adam Cohen | 69ce2e5 | 2011-07-03 19:25:21 -0700 | [diff] [blame] | 277 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) getLayoutParams(); |
| 278 | CellLayout layout = (CellLayout) getParent().getParent(); |
| 279 | mFolderRingAnimator.setCell(lp.cellX, lp.cellY); |
| 280 | mFolderRingAnimator.setCellLayout(layout); |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 281 | mFolderRingAnimator.animateToAcceptState(); |
Adam Cohen | 69ce2e5 | 2011-07-03 19:25:21 -0700 | [diff] [blame] | 282 | layout.showFolderAccept(mFolderRingAnimator); |
Adam Cohen | 073a46f | 2011-05-17 16:28:09 -0700 | [diff] [blame] | 283 | } |
| 284 | |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 285 | public void onDragOver(Object dragInfo) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 286 | } |
| 287 | |
Adam Cohen | d044526 | 2011-07-04 23:53:22 -0700 | [diff] [blame] | 288 | public void performCreateAnimation(final ShortcutInfo destInfo, final View destView, |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame^] | 289 | final ShortcutInfo srcInfo, final View srcView, Rect dstRect, |
| 290 | Runnable postAnimationRunnable) { |
Adam Cohen | d044526 | 2011-07-04 23:53:22 -0700 | [diff] [blame] | 291 | |
| 292 | Drawable animateDrawable = ((TextView) destView).getCompoundDrawables()[1]; |
| 293 | computePreviewDrawingParams(animateDrawable.getIntrinsicWidth(), destView.getMeasuredWidth()); |
| 294 | // This will animate the dragView (srcView) into the new folder |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame^] | 295 | onDrop(srcInfo, srcView, dstRect, 1, postAnimationRunnable); |
Adam Cohen | d044526 | 2011-07-04 23:53:22 -0700 | [diff] [blame] | 296 | |
| 297 | // This will animate the first item from it's position as an icon into its |
| 298 | // position as the first item in the preview |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 299 | animateFirstItem(animateDrawable, INITIAL_ITEM_ANIMATION_DURATION); |
Adam Cohen | d044526 | 2011-07-04 23:53:22 -0700 | [diff] [blame] | 300 | |
| 301 | postDelayed(new Runnable() { |
| 302 | public void run() { |
| 303 | addItem(destInfo); |
| 304 | } |
| 305 | }, INITIAL_ITEM_ANIMATION_DURATION); |
| 306 | } |
| 307 | |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 308 | public void onDragExit(Object dragInfo) { |
| 309 | if (!willAcceptItem((ItemInfo) dragInfo)) return; |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 310 | mFolderRingAnimator.animateToNaturalState(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 311 | } |
Patrick Dubroy | 440c360 | 2010-07-13 17:50:32 -0700 | [diff] [blame] | 312 | |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame^] | 313 | private void onDrop(final ShortcutInfo item, View animateView, Rect finalRect, int index, |
| 314 | Runnable postAnimationRunnable) { |
Adam Cohen | d044526 | 2011-07-04 23:53:22 -0700 | [diff] [blame] | 315 | item.cellX = -1; |
| 316 | item.cellY = -1; |
| 317 | DragLayer dragLayer = mLauncher.getDragLayer(); |
| 318 | Rect from = new Rect(); |
| 319 | dragLayer.getViewRectRelativeToSelf(animateView, from); |
| 320 | Rect to = finalRect; |
| 321 | if (to == null) { |
| 322 | to = new Rect(); |
| 323 | dragLayer.getDescendantRectRelativeToSelf(this, to); |
| 324 | } |
| 325 | |
| 326 | if (animateView.getMeasuredWidth() != to.width() || |
| 327 | animateView.getMeasuredHeight() != to.height()) { |
| 328 | int offsetX = (animateView.getMeasuredWidth() - to.width()) / 2; |
| 329 | int offsetY = (animateView.getMeasuredHeight() - to.height()) / 2; |
| 330 | to.offset(-offsetX, -offsetY); |
| 331 | } |
| 332 | float scale = adjustFinalScreenRectForIndex(to, index); |
| 333 | |
| 334 | float finalAlpha = index < NUM_ITEMS_IN_PREVIEW ? 0.5f : 0f; |
| 335 | |
| 336 | dragLayer.animateView(animateView, from, to, finalAlpha, scale, DROP_IN_ANIMATION_DURATION, |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame^] | 337 | new DecelerateInterpolator(2), new AccelerateInterpolator(2), postAnimationRunnable, |
| 338 | false); |
Adam Cohen | d044526 | 2011-07-04 23:53:22 -0700 | [diff] [blame] | 339 | postDelayed(new Runnable() { |
| 340 | public void run() { |
| 341 | addItem(item); |
| 342 | } |
| 343 | }, DROP_IN_ANIMATION_DURATION); |
| 344 | } |
| 345 | |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 346 | public void onDrop(DragObject d) { |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 347 | ShortcutInfo item; |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 348 | if (d.dragInfo instanceof ApplicationInfo) { |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 349 | // Came from all apps -- make a copy |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 350 | item = ((ApplicationInfo) d.dragInfo).makeShortcut(); |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 351 | } else { |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 352 | item = (ShortcutInfo) d.dragInfo; |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 353 | } |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame^] | 354 | onDrop(item, d.dragView, null, mInfo.contents.size(), d.postAnimationRunnable); |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 355 | } |
| 356 | |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 357 | public DropTarget getDropTargetDelegate(DragObject d) { |
Patrick Dubroy | 440c360 | 2010-07-13 17:50:32 -0700 | [diff] [blame] | 358 | return null; |
| 359 | } |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 360 | |
Adam Cohen | d044526 | 2011-07-04 23:53:22 -0700 | [diff] [blame] | 361 | private void computePreviewDrawingParams(int drawableSize, int totalSize) { |
| 362 | if (mIntrinsicIconSize != drawableSize || mTotalWidth != totalSize) { |
| 363 | mIntrinsicIconSize = drawableSize; |
| 364 | mTotalWidth = totalSize; |
| 365 | |
| 366 | final int previewSize = FolderRingAnimator.sPreviewSize; |
| 367 | final int previewPadding = FolderRingAnimator.sPreviewPadding; |
| 368 | |
| 369 | mAvailableSpaceInPreview = (previewSize - 2 * previewPadding); |
| 370 | // cos(45) = 0.707 + ~= 0.1) = 0.8f |
| 371 | int adjustedAvailableSpace = (int) ((mAvailableSpaceInPreview / 2) * (1 + 0.8f)); |
| 372 | |
| 373 | int unscaledHeight = (int) (mIntrinsicIconSize * (1 + PERSPECTIVE_SHIFT_FACTOR)); |
| 374 | mBaselineIconScale = (1.0f * adjustedAvailableSpace / unscaledHeight); |
| 375 | |
| 376 | mBaselineIconSize = (int) (mIntrinsicIconSize * mBaselineIconScale); |
| 377 | mMaxPerspectiveShift = mBaselineIconSize * PERSPECTIVE_SHIFT_FACTOR; |
| 378 | |
| 379 | mPreviewOffsetX = (mTotalWidth - mAvailableSpaceInPreview) / 2; |
| 380 | mPreviewOffsetY = previewPadding; |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | private void computePreviewDrawingParams(Drawable d) { |
| 385 | computePreviewDrawingParams(d.getIntrinsicWidth(), getMeasuredWidth()); |
| 386 | } |
| 387 | |
| 388 | class PreviewItemDrawingParams { |
| 389 | PreviewItemDrawingParams(float transX, float transY, float scale, int overlayAlpha) { |
| 390 | this.transX = transX; |
| 391 | this.transY = transY; |
| 392 | this.scale = scale; |
| 393 | this.overlayAlpha = overlayAlpha; |
| 394 | } |
| 395 | float transX; |
| 396 | float transY; |
| 397 | float scale; |
| 398 | int overlayAlpha; |
| 399 | Drawable drawable; |
| 400 | } |
| 401 | |
| 402 | private float adjustFinalScreenRectForIndex(Rect r, int index) { |
| 403 | mParams = computePreviewItemDrawingParams(Math.min(NUM_ITEMS_IN_PREVIEW, index), mParams); |
| 404 | |
| 405 | mParams.transX += mPreviewOffsetX; |
| 406 | mParams.transY += mPreviewOffsetY; |
| 407 | float offsetX = mParams.transX + (mParams.scale * mIntrinsicIconSize) / 2 - mTotalWidth / 2; |
| 408 | float offsetY = mParams.transY + (mParams.scale * mIntrinsicIconSize) / 2 - mTotalWidth / 2; |
| 409 | |
| 410 | r.offset((int) offsetX, (int) offsetY); |
| 411 | return mParams.scale; |
| 412 | } |
| 413 | |
| 414 | private PreviewItemDrawingParams computePreviewItemDrawingParams(int index, |
| 415 | PreviewItemDrawingParams params) { |
| 416 | index = NUM_ITEMS_IN_PREVIEW - index - 1; |
| 417 | float r = (index * 1.0f) / (NUM_ITEMS_IN_PREVIEW - 1); |
| 418 | float scale = (1 - PERSPECTIVE_SCALE_FACTOR * (1 - r)); |
| 419 | |
| 420 | float offset = (1 - r) * mMaxPerspectiveShift; |
| 421 | float scaledSize = scale * mBaselineIconSize; |
| 422 | float scaleOffsetCorrection = (1 - scale) * mBaselineIconSize; |
| 423 | |
| 424 | // We want to imagine our coordinates from the bottom left, growing up and to the |
| 425 | // right. This is natural for the x-axis, but for the y-axis, we have to invert things. |
| 426 | float transY = mAvailableSpaceInPreview - (offset + scaledSize + scaleOffsetCorrection); |
| 427 | float transX = offset + scaleOffsetCorrection; |
| 428 | float totalScale = mBaselineIconScale * scale; |
| 429 | final int overlayAlpha = (int) (80 * (1 - r)); |
| 430 | |
| 431 | if (params == null) { |
| 432 | params = new PreviewItemDrawingParams(transX, transY, totalScale, overlayAlpha); |
| 433 | } else { |
| 434 | params.transX = transX; |
| 435 | params.transY = transY; |
| 436 | params.scale = totalScale; |
| 437 | params.overlayAlpha = overlayAlpha; |
| 438 | } |
| 439 | return params; |
| 440 | } |
| 441 | |
| 442 | private void drawPreviewItem(Canvas canvas, PreviewItemDrawingParams params) { |
| 443 | canvas.save(); |
| 444 | canvas.translate(params.transX + mPreviewOffsetX, params.transY + mPreviewOffsetY); |
| 445 | canvas.scale(params.scale, params.scale); |
| 446 | Drawable d = params.drawable; |
| 447 | |
| 448 | if (d != null) { |
| 449 | d.setBounds(0, 0, mIntrinsicIconSize, mIntrinsicIconSize); |
| 450 | d.setFilterBitmap(true); |
| 451 | d.setColorFilter(Color.argb(params.overlayAlpha, 0, 0, 0), PorterDuff.Mode.SRC_ATOP); |
| 452 | d.draw(canvas); |
| 453 | d.clearColorFilter(); |
| 454 | d.setFilterBitmap(false); |
| 455 | } |
| 456 | canvas.restore(); |
| 457 | } |
| 458 | |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 459 | @Override |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 460 | protected void dispatchDraw(Canvas canvas) { |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 461 | super.dispatchDraw(canvas); |
| 462 | |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 463 | if (mFolder == null) return; |
Adam Cohen | d044526 | 2011-07-04 23:53:22 -0700 | [diff] [blame] | 464 | if (mFolder.getItemCount() == 0 && !mAnimating) return; |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 465 | |
Adam Cohen | 76078c4 | 2011-06-09 15:06:52 -0700 | [diff] [blame] | 466 | ArrayList<View> items = mFolder.getItemsInReadingOrder(false); |
Adam Cohen | d044526 | 2011-07-04 23:53:22 -0700 | [diff] [blame] | 467 | Drawable d; |
| 468 | TextView v; |
Adam Cohen | badf71e | 2011-05-26 19:08:29 -0700 | [diff] [blame] | 469 | |
Adam Cohen | d044526 | 2011-07-04 23:53:22 -0700 | [diff] [blame] | 470 | // Update our drawing parameters if necessary |
| 471 | if (mAnimating) { |
| 472 | computePreviewDrawingParams(mAnimParams.drawable); |
| 473 | } else { |
| 474 | v = (TextView) items.get(0); |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 475 | d = v.getCompoundDrawables()[1]; |
Adam Cohen | d044526 | 2011-07-04 23:53:22 -0700 | [diff] [blame] | 476 | computePreviewDrawingParams(d); |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 477 | } |
Adam Cohen | d044526 | 2011-07-04 23:53:22 -0700 | [diff] [blame] | 478 | |
| 479 | int nItemsInPreview = Math.min(items.size(), NUM_ITEMS_IN_PREVIEW); |
| 480 | if (!mAnimating) { |
| 481 | for (int i = nItemsInPreview - 1; i >= 0; i--) { |
| 482 | v = (TextView) items.get(i); |
| 483 | d = v.getCompoundDrawables()[1]; |
| 484 | |
| 485 | mParams = computePreviewItemDrawingParams(i, mParams); |
| 486 | mParams.drawable = d; |
| 487 | drawPreviewItem(canvas, mParams); |
| 488 | } |
| 489 | } else { |
| 490 | drawPreviewItem(canvas, mAnimParams); |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | private void animateFirstItem(final Drawable d, int duration) { |
| 495 | computePreviewDrawingParams(d); |
| 496 | final PreviewItemDrawingParams finalParams = computePreviewItemDrawingParams(0, null); |
| 497 | |
| 498 | final float scale0 = 1.0f; |
| 499 | final float transX0 = 0; |
| 500 | final float transY0 = 0; |
| 501 | mAnimParams.drawable = d; |
| 502 | |
| 503 | ValueAnimator va = ValueAnimator.ofFloat(0f, 1.0f); |
| 504 | va.addUpdateListener(new AnimatorUpdateListener(){ |
| 505 | public void onAnimationUpdate(ValueAnimator animation) { |
| 506 | float progress = (Float) animation.getAnimatedValue(); |
| 507 | |
| 508 | mAnimParams.transX = transX0 + progress * (finalParams.transX - transX0); |
| 509 | mAnimParams.transY = transY0 + progress * (finalParams.transY - transY0); |
| 510 | mAnimParams.scale = scale0 + progress * (finalParams.scale - scale0); |
| 511 | invalidate(); |
| 512 | } |
| 513 | }); |
| 514 | va.addListener(new AnimatorListenerAdapter() { |
| 515 | @Override |
| 516 | public void onAnimationStart(Animator animation) { |
| 517 | mAnimating = true; |
| 518 | } |
| 519 | @Override |
| 520 | public void onAnimationEnd(Animator animation) { |
| 521 | mAnimating = false; |
| 522 | } |
| 523 | }); |
| 524 | va.setDuration(duration); |
| 525 | va.start(); |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 526 | } |
| 527 | |
Adam Cohen | 76078c4 | 2011-06-09 15:06:52 -0700 | [diff] [blame] | 528 | public void onItemsChanged() { |
| 529 | invalidate(); |
| 530 | requestLayout(); |
| 531 | } |
| 532 | |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 533 | public void onAdd(ShortcutInfo item) { |
| 534 | invalidate(); |
| 535 | requestLayout(); |
| 536 | } |
| 537 | |
| 538 | public void onRemove(ShortcutInfo item) { |
| 539 | invalidate(); |
| 540 | requestLayout(); |
| 541 | } |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 542 | |
| 543 | public void onTitleChanged(CharSequence title) { |
Adam Cohen | d63cfa9 | 2011-06-24 14:17:17 -0700 | [diff] [blame] | 544 | mFolderName.setText(title.toString()); |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 545 | mPreviewBackground.setContentDescription(title.toString()); |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 546 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 547 | } |