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