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 | badf71e | 2011-05-26 19:08:29 -0700 | [diff] [blame] | 57 | |
| 58 | // The degree to which the inner ring grows when accepting drop |
Adam Cohen | 073a46f | 2011-05-17 16:28:09 -0700 | [diff] [blame] | 59 | private static final float INNER_RING_GROWTH_FACTOR = 0.1f; |
Adam Cohen | badf71e | 2011-05-26 19:08:29 -0700 | [diff] [blame] | 60 | |
Adam Cohen | badf71e | 2011-05-26 19:08:29 -0700 | [diff] [blame] | 61 | // The degree to which the outer ring is scaled in its natural state |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 62 | private static final float OUTER_RING_GROWTH_FACTOR = 0.4f; |
Adam Cohen | badf71e | 2011-05-26 19:08:29 -0700 | [diff] [blame] | 63 | |
| 64 | // The amount of vertical spread between items in the stack [0...1] |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 65 | private static final float PERSPECTIVE_SHIFT_FACTOR = 0.24f; |
Adam Cohen | badf71e | 2011-05-26 19:08:29 -0700 | [diff] [blame] | 66 | |
| 67 | // The degree to which the item in the back of the stack is scaled [0...1] |
| 68 | // (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] | 69 | private static final float PERSPECTIVE_SCALE_FACTOR = 0.35f; |
Adam Cohen | badf71e | 2011-05-26 19:08:29 -0700 | [diff] [blame] | 70 | |
Adam Cohen | 073a46f | 2011-05-17 16:28:09 -0700 | [diff] [blame] | 71 | private int mOriginalWidth = -1; |
| 72 | private int mOriginalHeight = -1; |
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 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 78 | public FolderIcon(Context context, AttributeSet attrs) { |
| 79 | super(context, attrs); |
| 80 | } |
| 81 | |
| 82 | public FolderIcon(Context context) { |
| 83 | super(context); |
| 84 | } |
| 85 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 86 | public boolean isDropEnabled() { |
Winson Chung | 7a1d165 | 2011-03-18 15:56:01 -0700 | [diff] [blame] | 87 | final ViewGroup cellLayoutChildren = (ViewGroup) getParent(); |
| 88 | final ViewGroup cellLayout = (ViewGroup) cellLayoutChildren.getParent(); |
| 89 | final Workspace workspace = (Workspace) cellLayout.getParent(); |
| 90 | return !workspace.isSmall(); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 91 | } |
| 92 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 93 | static FolderIcon fromXml(int resId, Launcher launcher, ViewGroup group, |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 94 | FolderInfo folderInfo, IconCache iconCache) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 95 | |
| 96 | FolderIcon icon = (FolderIcon) LayoutInflater.from(launcher).inflate(resId, group, false); |
| 97 | |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 98 | icon.mFolderName = (BubbleTextView) icon.findViewById(R.id.folder_name); |
| 99 | icon.mFolderName.setText(folderInfo.title); |
| 100 | icon.mPreviewBackground = (ImageView) icon.findViewById(R.id.preview_background); |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 101 | icon.mPreviewBackground.setContentDescription(folderInfo.title); |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 102 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 103 | icon.setTag(folderInfo); |
| 104 | icon.setOnClickListener(launcher); |
| 105 | icon.mInfo = folderInfo; |
| 106 | icon.mLauncher = launcher; |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 107 | |
| 108 | Folder folder = Folder.fromXml(launcher); |
| 109 | folder.setDragController(launcher.getDragController()); |
| 110 | folder.setLauncher(launcher); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 111 | folder.setFolderIcon(icon); |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 112 | folder.bind(folderInfo); |
| 113 | icon.mFolder = folder; |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 114 | icon.mFolderRingAnimator = new FolderRingAnimator(launcher, icon); |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 115 | folderInfo.addListener(icon); |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 116 | |
| 117 | return icon; |
| 118 | } |
| 119 | |
| 120 | public static class FolderRingAnimator { |
| 121 | public int mFolderLocX; |
| 122 | public int mFolderLocY; |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 123 | public float mOuterRingSize; |
| 124 | public float mInnerRingSize; |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 125 | public FolderIcon mFolderIcon = null; |
| 126 | private Launcher mLauncher; |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 127 | public Drawable mOuterRingDrawable = null; |
| 128 | public Drawable mInnerRingDrawable = null; |
| 129 | public static Drawable sSharedOuterRingDrawable = null; |
| 130 | public static Drawable sSharedInnerRingDrawable = null; |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 131 | public static int sPreviewSize = -1; |
| 132 | public static int sPreviewPadding = -1; |
| 133 | |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 134 | private ValueAnimator mAcceptAnimator; |
| 135 | private ValueAnimator mNeutralAnimator; |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 136 | |
| 137 | public FolderRingAnimator(Launcher launcher, FolderIcon folderIcon) { |
| 138 | mLauncher = launcher; |
| 139 | mFolderIcon = folderIcon; |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 140 | Resources res = launcher.getResources(); |
| 141 | mOuterRingDrawable = res.getDrawable(R.drawable.portal_ring_outer_holo); |
| 142 | mInnerRingDrawable = res.getDrawable(R.drawable.portal_ring_inner_holo); |
| 143 | |
| 144 | if (sPreviewSize < 0 || sPreviewPadding < 0) { |
| 145 | sPreviewSize = res.getDimensionPixelSize(R.dimen.folder_preview_size); |
| 146 | sPreviewPadding = res.getDimensionPixelSize(R.dimen.folder_preview_padding); |
| 147 | } |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 148 | if (sSharedOuterRingDrawable == null) { |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 149 | sSharedOuterRingDrawable = res.getDrawable(R.drawable.portal_ring_outer_holo); |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 150 | } |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 151 | if (sSharedInnerRingDrawable == null) { |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 152 | sSharedInnerRingDrawable = res.getDrawable(R.drawable.portal_ring_inner_holo); |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 153 | } |
Adam Cohen | 073a46f | 2011-05-17 16:28:09 -0700 | [diff] [blame] | 154 | } |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 155 | |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 156 | // Location is expressed in window coordinates |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 157 | public void setLocation(int x, int y) { |
| 158 | mFolderLocX = x; |
| 159 | mFolderLocY = y; |
Adam Cohen | f4b0891 | 2011-05-17 18:45:47 -0700 | [diff] [blame] | 160 | } |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 161 | |
| 162 | public void animateToAcceptState() { |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 163 | if (mNeutralAnimator != null) { |
| 164 | mNeutralAnimator.cancel(); |
| 165 | } |
| 166 | mAcceptAnimator = ValueAnimator.ofFloat(0f, 1f); |
| 167 | mAcceptAnimator.setDuration(CONSUMPTION_ANIMATION_DURATION); |
| 168 | mAcceptAnimator.addUpdateListener(new AnimatorUpdateListener() { |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 169 | public void onAnimationUpdate(ValueAnimator animation) { |
| 170 | final float percent = (Float) animation.getAnimatedValue(); |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 171 | mOuterRingSize = (1 + percent * OUTER_RING_GROWTH_FACTOR) * sPreviewSize; |
| 172 | mInnerRingSize = (1 + percent * INNER_RING_GROWTH_FACTOR) * sPreviewSize; |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 173 | mLauncher.getWorkspace().invalidate(); |
| 174 | if (mFolderIcon != null) { |
| 175 | mFolderIcon.invalidate(); |
| 176 | } |
| 177 | } |
| 178 | }); |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 179 | mAcceptAnimator.addListener(new AnimatorListenerAdapter() { |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 180 | @Override |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 181 | public void onAnimationStart(Animator animation) { |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 182 | if (mFolderIcon != null) { |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 183 | mFolderIcon.mPreviewBackground.setVisibility(INVISIBLE); |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | }); |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 187 | mAcceptAnimator.start(); |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | public void animateToNaturalState() { |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 191 | if (mAcceptAnimator != null) { |
| 192 | mAcceptAnimator.cancel(); |
| 193 | } |
| 194 | mNeutralAnimator = ValueAnimator.ofFloat(0f, 1f); |
| 195 | mNeutralAnimator.setDuration(CONSUMPTION_ANIMATION_DURATION); |
| 196 | mNeutralAnimator.addUpdateListener(new AnimatorUpdateListener() { |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 197 | public void onAnimationUpdate(ValueAnimator animation) { |
| 198 | final float percent = (Float) animation.getAnimatedValue(); |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 199 | mOuterRingSize = (1 + (1 - percent) * OUTER_RING_GROWTH_FACTOR) * sPreviewSize; |
| 200 | mInnerRingSize = (1 + (1 - percent) * INNER_RING_GROWTH_FACTOR) * sPreviewSize; |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 201 | mLauncher.getWorkspace().invalidate(); |
| 202 | if (mFolderIcon != null) { |
| 203 | mFolderIcon.invalidate(); |
| 204 | } |
| 205 | } |
| 206 | }); |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 207 | mNeutralAnimator.addListener(new AnimatorListenerAdapter() { |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 208 | @Override |
| 209 | public void onAnimationEnd(Animator animation) { |
| 210 | if (mFolderIcon != null) { |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 211 | mFolderIcon.mPreviewBackground.setVisibility(VISIBLE); |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 212 | } |
| 213 | mLauncher.getWorkspace().hideFolderAccept(FolderRingAnimator.this); |
| 214 | } |
| 215 | }); |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 216 | mNeutralAnimator.start(); |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 219 | // Location is expressed in window coordinates |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 220 | public void getLocation(int[] loc) { |
| 221 | loc[0] = mFolderLocX; |
| 222 | loc[1] = mFolderLocY; |
| 223 | } |
| 224 | |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 225 | public float getOuterRingSize() { |
| 226 | return mOuterRingSize; |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 229 | public float getInnerRingSize() { |
| 230 | return mInnerRingSize; |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 231 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 232 | } |
| 233 | |
Adam Cohen | 073a46f | 2011-05-17 16:28:09 -0700 | [diff] [blame] | 234 | private boolean willAcceptItem(ItemInfo item) { |
| 235 | final int itemType = item.itemType; |
| 236 | return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION || |
| 237 | itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) && |
| 238 | !mFolder.isFull() && item != mInfo); |
| 239 | } |
| 240 | |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 241 | public boolean acceptDrop(Object dragInfo) { |
| 242 | final ItemInfo item = (ItemInfo) dragInfo; |
Adam Cohen | 073a46f | 2011-05-17 16:28:09 -0700 | [diff] [blame] | 243 | return willAcceptItem(item); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 244 | } |
| 245 | |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 246 | public void addItem(ShortcutInfo item) { |
| 247 | mInfo.add(item); |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 248 | LauncherModel.addOrMoveItemInDatabase(mLauncher, item, mInfo.id, 0, item.cellX, item.cellY); |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 251 | void saveState(CellLayout.LayoutParams lp) { |
| 252 | mOriginalWidth = lp.width; |
| 253 | mOriginalHeight = lp.height; |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Adam Cohen | 073a46f | 2011-05-17 16:28:09 -0700 | [diff] [blame] | 256 | private void determineFolderLocationInWorkspace() { |
| 257 | int tvLocation[] = new int[2]; |
| 258 | int wsLocation[] = new int[2]; |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 259 | getLocationInWindow(tvLocation); |
| 260 | mLauncher.getWorkspace().getLocationInWindow(wsLocation); |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 261 | |
| 262 | int x = tvLocation[0] - wsLocation[0] + getMeasuredWidth() / 2; |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 263 | int y = tvLocation[1] - wsLocation[1] + FolderRingAnimator.sPreviewSize / 2; |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 264 | mFolderRingAnimator.setLocation(x, y); |
Adam Cohen | 073a46f | 2011-05-17 16:28:09 -0700 | [diff] [blame] | 265 | } |
| 266 | |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 267 | public void onDragEnter(Object dragInfo) { |
| 268 | if (!willAcceptItem((ItemInfo) dragInfo)) return; |
Adam Cohen | 073a46f | 2011-05-17 16:28:09 -0700 | [diff] [blame] | 269 | determineFolderLocationInWorkspace(); |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 270 | mLauncher.getWorkspace().showFolderAccept(mFolderRingAnimator); |
| 271 | mFolderRingAnimator.animateToAcceptState(); |
Adam Cohen | 073a46f | 2011-05-17 16:28:09 -0700 | [diff] [blame] | 272 | } |
| 273 | |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 274 | public void onDragOver(Object dragInfo) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 275 | } |
| 276 | |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 277 | public void onDragExit(Object dragInfo) { |
| 278 | if (!willAcceptItem((ItemInfo) dragInfo)) return; |
Adam Cohen | 19072da | 2011-05-31 14:30:45 -0700 | [diff] [blame] | 279 | mFolderRingAnimator.animateToNaturalState(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 280 | } |
Patrick Dubroy | 440c360 | 2010-07-13 17:50:32 -0700 | [diff] [blame] | 281 | |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 282 | public void onDrop(DragObject d) { |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 283 | ShortcutInfo item; |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 284 | if (d.dragInfo instanceof ApplicationInfo) { |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 285 | // Came from all apps -- make a copy |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 286 | item = ((ApplicationInfo) d.dragInfo).makeShortcut(); |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 287 | } else { |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 288 | item = (ShortcutInfo) d.dragInfo; |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 289 | } |
| 290 | item.cellX = -1; |
| 291 | item.cellY = -1; |
| 292 | addItem(item); |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 293 | DragLayer dragLayer = mLauncher.getDragLayer(); |
| 294 | Rect from = new Rect(); |
| 295 | dragLayer.getViewRectRelativeToSelf(d.dragView, from); |
| 296 | Rect to = new Rect(); |
| 297 | dragLayer.getDescendantRectRelativeToSelf(this, to); |
| 298 | |
| 299 | int previewSize = FolderRingAnimator.sPreviewSize; |
| 300 | int vanishingPointX = (int) (previewSize * 0.7); |
| 301 | int vanishingPointY = (int) (previewSize * (1 - 0.88f)); |
| 302 | to.offset(vanishingPointX - previewSize / 2 , vanishingPointY - previewSize / 2); |
| 303 | |
| 304 | dragLayer.animateView(d.dragView, from, to, 0f, 0.2f, 400, new DecelerateInterpolator(2), |
| 305 | new AccelerateInterpolator(2)); |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 308 | public DropTarget getDropTargetDelegate(DragObject d) { |
Patrick Dubroy | 440c360 | 2010-07-13 17:50:32 -0700 | [diff] [blame] | 309 | return null; |
| 310 | } |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 311 | |
| 312 | @Override |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 313 | protected void dispatchDraw(Canvas canvas) { |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 314 | super.dispatchDraw(canvas); |
| 315 | |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 316 | if (mFolder == null) return; |
| 317 | if (mFolder.getItemCount() == 0) return; |
| 318 | |
| 319 | canvas.save(); |
| 320 | TextView v = (TextView) mFolder.getItemAt(0); |
| 321 | Drawable d = v.getCompoundDrawables()[1]; |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 322 | int intrinsicIconSize = d.getIntrinsicHeight(); |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 323 | |
Adam Cohen | 073a46f | 2011-05-17 16:28:09 -0700 | [diff] [blame] | 324 | if (mOriginalWidth < 0 || mOriginalHeight < 0) { |
| 325 | mOriginalWidth = getMeasuredWidth(); |
| 326 | mOriginalHeight = getMeasuredHeight(); |
| 327 | } |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 328 | final int previewSize = FolderRingAnimator.sPreviewSize; |
| 329 | final int previewPadding = FolderRingAnimator.sPreviewPadding; |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 330 | |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 331 | int halfAvailableSpace = (previewSize - 2 * previewPadding) / 2; |
| 332 | // cos(45) = 0.707 + ~= 0.1) |
| 333 | int availableSpace = (int) (halfAvailableSpace * (1 + 0.8f)); |
Adam Cohen | badf71e | 2011-05-26 19:08:29 -0700 | [diff] [blame] | 334 | |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 335 | int unscaledHeight = (int) (intrinsicIconSize * (1 + PERSPECTIVE_SHIFT_FACTOR)); |
| 336 | float baselineIconScale = (1.0f * availableSpace / unscaledHeight); |
| 337 | |
| 338 | int baselineSize = (int) (intrinsicIconSize * baselineIconScale); |
| 339 | float maxPerspectiveShift = baselineSize * PERSPECTIVE_SHIFT_FACTOR; |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 340 | |
Adam Cohen | 76078c4 | 2011-06-09 15:06:52 -0700 | [diff] [blame] | 341 | ArrayList<View> items = mFolder.getItemsInReadingOrder(false); |
Adam Cohen | badf71e | 2011-05-26 19:08:29 -0700 | [diff] [blame] | 342 | |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 343 | int xShift = (mOriginalWidth - 2 * halfAvailableSpace) / 2; |
| 344 | int yShift = previewPadding; |
Adam Cohen | badf71e | 2011-05-26 19:08:29 -0700 | [diff] [blame] | 345 | canvas.translate(xShift, yShift); |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 346 | int nItemsInPreview = Math.min(items.size(), NUM_ITEMS_IN_PREVIEW); |
| 347 | for (int i = nItemsInPreview - 1; i >= 0; i--) { |
| 348 | int index = NUM_ITEMS_IN_PREVIEW - i - 1; |
Adam Cohen | badf71e | 2011-05-26 19:08:29 -0700 | [diff] [blame] | 349 | |
| 350 | float r = (index * 1.0f) / (NUM_ITEMS_IN_PREVIEW - 1); |
| 351 | float scale = (1 - PERSPECTIVE_SCALE_FACTOR * (1 - r)); |
Adam Cohen | badf71e | 2011-05-26 19:08:29 -0700 | [diff] [blame] | 352 | |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 353 | float offset = (1 - r) * maxPerspectiveShift; |
| 354 | float scaledSize = scale * baselineSize; |
| 355 | float scaleOffsetCorrection = (1 - scale) * baselineSize; |
| 356 | |
| 357 | // We want to imagine our coordinates from the bottom left, growing up and to the |
| 358 | // right. This is natural for the x-axis, but for the y-axis, we have to invert things. |
| 359 | float transY = 2 * halfAvailableSpace - (offset + scaledSize + scaleOffsetCorrection); |
| 360 | float transX = offset + scaleOffsetCorrection; |
Adam Cohen | badf71e | 2011-05-26 19:08:29 -0700 | [diff] [blame] | 361 | |
Adam Cohen | 7c69321 | 2011-05-18 15:26:57 -0700 | [diff] [blame] | 362 | v = (TextView) items.get(i); |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 363 | d = v.getCompoundDrawables()[1]; |
| 364 | |
Adam Cohen | badf71e | 2011-05-26 19:08:29 -0700 | [diff] [blame] | 365 | canvas.save(); |
| 366 | canvas.translate(transX, transY); |
| 367 | canvas.scale(baselineIconScale * scale, baselineIconScale * scale); |
Adam Cohen | f4b0891 | 2011-05-17 18:45:47 -0700 | [diff] [blame] | 368 | |
Adam Cohen | badf71e | 2011-05-26 19:08:29 -0700 | [diff] [blame] | 369 | int overlayAlpha = (int) (80 * (1 - r)); |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 370 | if (d != null) { |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 371 | d.setBounds(0, 0, intrinsicIconSize, intrinsicIconSize); |
| 372 | d.setFilterBitmap(true); |
Adam Cohen | badf71e | 2011-05-26 19:08:29 -0700 | [diff] [blame] | 373 | d.setColorFilter(Color.argb(overlayAlpha, 0, 0, 0), PorterDuff.Mode.SRC_ATOP); |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 374 | d.draw(canvas); |
Adam Cohen | badf71e | 2011-05-26 19:08:29 -0700 | [diff] [blame] | 375 | d.clearColorFilter(); |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 376 | d.setFilterBitmap(false); |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 377 | } |
Adam Cohen | badf71e | 2011-05-26 19:08:29 -0700 | [diff] [blame] | 378 | canvas.restore(); |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 379 | } |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 380 | canvas.restore(); |
| 381 | } |
| 382 | |
Adam Cohen | 76078c4 | 2011-06-09 15:06:52 -0700 | [diff] [blame] | 383 | public void onItemsChanged() { |
| 384 | invalidate(); |
| 385 | requestLayout(); |
| 386 | } |
| 387 | |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 388 | public void onAdd(ShortcutInfo item) { |
| 389 | invalidate(); |
| 390 | requestLayout(); |
| 391 | } |
| 392 | |
| 393 | public void onRemove(ShortcutInfo item) { |
| 394 | invalidate(); |
| 395 | requestLayout(); |
| 396 | } |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 397 | |
| 398 | public void onTitleChanged(CharSequence title) { |
Adam Cohen | d63cfa9 | 2011-06-24 14:17:17 -0700 | [diff] [blame] | 399 | mFolderName.setText(title.toString()); |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 400 | mPreviewBackground.setContentDescription(title.toString()); |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 401 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 402 | } |