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; |
| 21 | import android.animation.ObjectAnimator; |
| 22 | import android.animation.PropertyValuesHolder; |
| 23 | import android.animation.ValueAnimator; |
| 24 | import android.animation.ValueAnimator.AnimatorUpdateListener; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 25 | import android.content.Context; |
| 26 | import android.content.res.Resources; |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 27 | import android.graphics.Canvas; |
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; |
| 31 | import android.view.ViewGroup; |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 32 | import android.widget.FrameLayout; |
| 33 | import android.widget.TextView; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 34 | |
Romain Guy | edcce09 | 2010-03-04 13:03:17 -0800 | [diff] [blame] | 35 | import com.android.launcher.R; |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 36 | import com.android.launcher2.FolderInfo.FolderListener; |
Romain Guy | edcce09 | 2010-03-04 13:03:17 -0800 | [diff] [blame] | 37 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 38 | /** |
| 39 | * An icon that can appear on in the workspace representing an {@link UserFolder}. |
| 40 | */ |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 41 | public class FolderIcon extends FrameLayout implements DropTarget, FolderListener { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 42 | private Launcher mLauncher; |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 43 | Folder mFolder; |
| 44 | FolderInfo mInfo; |
| 45 | |
| 46 | private static final int NUM_ITEMS_IN_PREVIEW = 4; |
| 47 | private static final float ICON_ANGLE = 15f; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 48 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame^] | 49 | int mOriginalWidth; |
| 50 | int mOriginalHeight; |
| 51 | int mOriginalX; |
| 52 | int mOriginalY; |
| 53 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 54 | public FolderIcon(Context context, AttributeSet attrs) { |
| 55 | super(context, attrs); |
| 56 | } |
| 57 | |
| 58 | public FolderIcon(Context context) { |
| 59 | super(context); |
| 60 | } |
| 61 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 62 | public boolean isDropEnabled() { |
Winson Chung | 7a1d165 | 2011-03-18 15:56:01 -0700 | [diff] [blame] | 63 | final ViewGroup cellLayoutChildren = (ViewGroup) getParent(); |
| 64 | final ViewGroup cellLayout = (ViewGroup) cellLayoutChildren.getParent(); |
| 65 | final Workspace workspace = (Workspace) cellLayout.getParent(); |
| 66 | return !workspace.isSmall(); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 67 | } |
| 68 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 69 | static FolderIcon fromXml(int resId, Launcher launcher, ViewGroup group, |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 70 | FolderInfo folderInfo, IconCache iconCache) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 71 | |
| 72 | FolderIcon icon = (FolderIcon) LayoutInflater.from(launcher).inflate(resId, group, false); |
| 73 | |
| 74 | final Resources resources = launcher.getResources(); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame^] | 75 | Drawable d = iconCache.getFullResIcon(resources, R.drawable.portal_ring_inner_holo); |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 76 | icon.setBackgroundDrawable(d); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 77 | icon.setTag(folderInfo); |
| 78 | icon.setOnClickListener(launcher); |
| 79 | icon.mInfo = folderInfo; |
| 80 | icon.mLauncher = launcher; |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 81 | |
| 82 | Folder folder = Folder.fromXml(launcher); |
| 83 | folder.setDragController(launcher.getDragController()); |
| 84 | folder.setLauncher(launcher); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame^] | 85 | folder.setFolderIcon(icon); |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 86 | folder.bind(folderInfo); |
| 87 | icon.mFolder = folder; |
| 88 | |
| 89 | folderInfo.addListener(icon); |
| 90 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 91 | return icon; |
| 92 | } |
| 93 | |
| 94 | public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset, |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 95 | DragView dragView, Object dragInfo) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 96 | final ItemInfo item = (ItemInfo) dragInfo; |
| 97 | final int itemType = item.itemType; |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame^] | 98 | return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION || |
| 99 | itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) && |
| 100 | !mFolder.isFull()); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 101 | } |
| 102 | |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 103 | public void addItem(ShortcutInfo item) { |
| 104 | mInfo.add(item); |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 105 | LauncherModel.addOrMoveItemInDatabase(mLauncher, item, mInfo.id, 0, item.cellX, item.cellY); |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 108 | public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset, |
| 109 | DragView dragView, Object dragInfo) { |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 110 | ShortcutInfo item; |
| 111 | if (dragInfo instanceof ApplicationInfo) { |
| 112 | // Came from all apps -- make a copy |
| 113 | item = ((ApplicationInfo)dragInfo).makeShortcut(); |
| 114 | } else { |
| 115 | item = (ShortcutInfo)dragInfo; |
| 116 | } |
Michael Jurka | 66d7217 | 2011-04-12 16:29:25 -0700 | [diff] [blame] | 117 | item.cellX = -1; |
| 118 | item.cellY = -1; |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 119 | addItem(item); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame^] | 122 | void saveState(CellLayout.LayoutParams lp) { |
| 123 | mOriginalWidth = lp.width; |
| 124 | mOriginalHeight = lp.height; |
| 125 | mOriginalX = lp.x; |
| 126 | mOriginalY = lp.y; |
| 127 | } |
| 128 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 129 | public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset, |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 130 | DragView dragView, Object dragInfo) { |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame^] | 131 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) getLayoutParams(); |
| 132 | lp.isLockedToGrid = false; |
| 133 | saveState(lp); |
| 134 | |
| 135 | PropertyValuesHolder width = PropertyValuesHolder.ofInt("width", (int) (1.1 * lp.width)); |
| 136 | PropertyValuesHolder height = PropertyValuesHolder.ofInt("height", (int) (1.1 * lp.height)); |
| 137 | PropertyValuesHolder newX = PropertyValuesHolder.ofInt("x", lp.x - (int) (0.05 * lp.width)); |
| 138 | PropertyValuesHolder newY = PropertyValuesHolder.ofInt("y", lp.y - (int) (0.05 * lp.height)); |
| 139 | ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(lp, width, height, newX, newY); |
| 140 | oa.addUpdateListener(new AnimatorUpdateListener() { |
| 141 | public void onAnimationUpdate(ValueAnimator animation) { |
| 142 | invalidate(); |
| 143 | requestLayout(); |
| 144 | } |
| 145 | }); |
| 146 | oa.setDuration(50); |
| 147 | oa.start(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset, |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 151 | DragView dragView, Object dragInfo) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset, |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 155 | DragView dragView, Object dragInfo) { |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame^] | 156 | final CellLayout.LayoutParams lp = (CellLayout.LayoutParams) getLayoutParams(); |
| 157 | lp.isLockedToGrid = false; |
| 158 | |
| 159 | PropertyValuesHolder width = PropertyValuesHolder.ofInt("width", mOriginalWidth); |
| 160 | PropertyValuesHolder height = PropertyValuesHolder.ofInt("height", mOriginalHeight); |
| 161 | PropertyValuesHolder newX = PropertyValuesHolder.ofInt("x", mOriginalX); |
| 162 | PropertyValuesHolder newY = PropertyValuesHolder.ofInt("y", mOriginalY); |
| 163 | ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(lp, width, height, newX, newY); |
| 164 | oa.addUpdateListener(new AnimatorUpdateListener() { |
| 165 | public void onAnimationUpdate(ValueAnimator animation) { |
| 166 | invalidate(); |
| 167 | requestLayout(); |
| 168 | } |
| 169 | }); |
| 170 | oa.addListener(new AnimatorListenerAdapter() { |
| 171 | @Override |
| 172 | public void onAnimationEnd(Animator animation) { |
| 173 | lp.isLockedToGrid = true; |
| 174 | } |
| 175 | }); |
| 176 | oa.setDuration(50); |
| 177 | oa.start(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 178 | } |
Patrick Dubroy | 440c360 | 2010-07-13 17:50:32 -0700 | [diff] [blame] | 179 | |
| 180 | @Override |
| 181 | public DropTarget getDropTargetDelegate(DragSource source, int x, int y, int xOffset, int yOffset, |
| 182 | DragView dragView, Object dragInfo) { |
| 183 | return null; |
| 184 | } |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 185 | |
| 186 | @Override |
| 187 | protected void onDraw(Canvas canvas) { |
| 188 | if (mFolder == null) return; |
| 189 | if (mFolder.getItemCount() == 0) return; |
| 190 | |
| 191 | canvas.save(); |
| 192 | TextView v = (TextView) mFolder.getItemAt(0); |
| 193 | Drawable d = v.getCompoundDrawables()[1]; |
| 194 | |
| 195 | canvas.translate( (getMeasuredWidth() - d.getIntrinsicWidth()) / 2, |
| 196 | (getMeasuredHeight() - d.getIntrinsicHeight()) / 2); |
| 197 | |
| 198 | canvas.translate(d.getIntrinsicWidth() / 2, d.getIntrinsicHeight() / 2); |
| 199 | canvas.rotate(ICON_ANGLE); |
| 200 | |
| 201 | canvas.translate(-d.getIntrinsicWidth() / 2, -d.getIntrinsicHeight() / 2); |
| 202 | |
| 203 | for (int i = Math.max(0, mFolder.getItemCount() - NUM_ITEMS_IN_PREVIEW); |
| 204 | i < mFolder.getItemCount(); i++) { |
| 205 | v = (TextView) mFolder.getItemAt(i); |
| 206 | d = v.getCompoundDrawables()[1]; |
| 207 | |
| 208 | if (d != null) { |
| 209 | d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight()); |
| 210 | d.draw(canvas); |
| 211 | } |
| 212 | |
| 213 | canvas.translate(d.getIntrinsicWidth() / 2, d.getIntrinsicHeight() / 2); |
| 214 | canvas.rotate(-ICON_ANGLE); |
| 215 | canvas.translate(-d.getIntrinsicWidth() / 2, -d.getIntrinsicHeight() / 2); |
| 216 | } |
| 217 | |
| 218 | canvas.restore(); |
| 219 | } |
| 220 | |
| 221 | public void onAdd(ShortcutInfo item) { |
| 222 | invalidate(); |
| 223 | requestLayout(); |
| 224 | } |
| 225 | |
| 226 | public void onRemove(ShortcutInfo item) { |
| 227 | invalidate(); |
| 228 | requestLayout(); |
| 229 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 230 | } |