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 | |
| 19 | import android.content.Context; |
| 20 | import android.content.res.Resources; |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 21 | import android.graphics.Canvas; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 22 | import android.graphics.drawable.Drawable; |
| 23 | import android.util.AttributeSet; |
| 24 | import android.view.LayoutInflater; |
| 25 | import android.view.ViewGroup; |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 26 | import android.widget.FrameLayout; |
| 27 | import android.widget.TextView; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 28 | |
Romain Guy | edcce09 | 2010-03-04 13:03:17 -0800 | [diff] [blame] | 29 | import com.android.launcher.R; |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 30 | import com.android.launcher2.FolderInfo.FolderListener; |
Romain Guy | edcce09 | 2010-03-04 13:03:17 -0800 | [diff] [blame] | 31 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 32 | /** |
| 33 | * An icon that can appear on in the workspace representing an {@link UserFolder}. |
| 34 | */ |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 35 | public class FolderIcon extends FrameLayout implements DropTarget, FolderListener { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 36 | private Launcher mLauncher; |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 37 | Folder mFolder; |
| 38 | FolderInfo mInfo; |
| 39 | |
| 40 | private static final int NUM_ITEMS_IN_PREVIEW = 4; |
| 41 | private static final float ICON_ANGLE = 15f; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 42 | |
| 43 | public FolderIcon(Context context, AttributeSet attrs) { |
| 44 | super(context, attrs); |
| 45 | } |
| 46 | |
| 47 | public FolderIcon(Context context) { |
| 48 | super(context); |
| 49 | } |
| 50 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 51 | public boolean isDropEnabled() { |
Winson Chung | 7a1d165 | 2011-03-18 15:56:01 -0700 | [diff] [blame] | 52 | final ViewGroup cellLayoutChildren = (ViewGroup) getParent(); |
| 53 | final ViewGroup cellLayout = (ViewGroup) cellLayoutChildren.getParent(); |
| 54 | final Workspace workspace = (Workspace) cellLayout.getParent(); |
| 55 | return !workspace.isSmall(); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 56 | } |
| 57 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 58 | static FolderIcon fromXml(int resId, Launcher launcher, ViewGroup group, |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 59 | FolderInfo folderInfo, IconCache iconCache) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 60 | |
| 61 | FolderIcon icon = (FolderIcon) LayoutInflater.from(launcher).inflate(resId, group, false); |
| 62 | |
| 63 | final Resources resources = launcher.getResources(); |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 64 | Drawable d = iconCache.getFullResIcon(resources, R.drawable.folder_bg); |
| 65 | icon.setBackgroundDrawable(d); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 66 | icon.setTag(folderInfo); |
| 67 | icon.setOnClickListener(launcher); |
| 68 | icon.mInfo = folderInfo; |
| 69 | icon.mLauncher = launcher; |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 70 | |
| 71 | Folder folder = Folder.fromXml(launcher); |
| 72 | folder.setDragController(launcher.getDragController()); |
| 73 | folder.setLauncher(launcher); |
| 74 | folder.bind(folderInfo); |
| 75 | icon.mFolder = folder; |
| 76 | |
| 77 | folderInfo.addListener(icon); |
| 78 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 79 | return icon; |
| 80 | } |
| 81 | |
| 82 | 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] | 83 | DragView dragView, Object dragInfo) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 84 | final ItemInfo item = (ItemInfo) dragInfo; |
| 85 | final int itemType = item.itemType; |
| 86 | return (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION || |
| 87 | itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) |
| 88 | && item.container != mInfo.id; |
| 89 | } |
| 90 | |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 91 | public void addItem(ShortcutInfo item) { |
| 92 | mInfo.add(item); |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 93 | LauncherModel.addOrMoveItemInDatabase(mLauncher, item, mInfo.id, 0, item.cellX, item.cellY); |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 96 | public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset, |
| 97 | DragView dragView, Object dragInfo) { |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 98 | ShortcutInfo item; |
| 99 | if (dragInfo instanceof ApplicationInfo) { |
| 100 | // Came from all apps -- make a copy |
| 101 | item = ((ApplicationInfo)dragInfo).makeShortcut(); |
| 102 | } else { |
| 103 | item = (ShortcutInfo)dragInfo; |
| 104 | } |
Michael Jurka | 66d7217 | 2011-04-12 16:29:25 -0700 | [diff] [blame] | 105 | item.cellX = -1; |
| 106 | item.cellY = -1; |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 107 | addItem(item); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | 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] | 111 | DragView dragView, Object dragInfo) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | 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] | 115 | DragView dragView, Object dragInfo) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | 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] | 119 | DragView dragView, Object dragInfo) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 120 | } |
Patrick Dubroy | 440c360 | 2010-07-13 17:50:32 -0700 | [diff] [blame] | 121 | |
| 122 | @Override |
| 123 | public DropTarget getDropTargetDelegate(DragSource source, int x, int y, int xOffset, int yOffset, |
| 124 | DragView dragView, Object dragInfo) { |
| 125 | return null; |
| 126 | } |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 127 | |
| 128 | @Override |
| 129 | protected void onDraw(Canvas canvas) { |
| 130 | if (mFolder == null) return; |
| 131 | if (mFolder.getItemCount() == 0) return; |
| 132 | |
| 133 | canvas.save(); |
| 134 | TextView v = (TextView) mFolder.getItemAt(0); |
| 135 | Drawable d = v.getCompoundDrawables()[1]; |
| 136 | |
| 137 | canvas.translate( (getMeasuredWidth() - d.getIntrinsicWidth()) / 2, |
| 138 | (getMeasuredHeight() - d.getIntrinsicHeight()) / 2); |
| 139 | |
| 140 | canvas.translate(d.getIntrinsicWidth() / 2, d.getIntrinsicHeight() / 2); |
| 141 | canvas.rotate(ICON_ANGLE); |
| 142 | |
| 143 | canvas.translate(-d.getIntrinsicWidth() / 2, -d.getIntrinsicHeight() / 2); |
| 144 | |
| 145 | for (int i = Math.max(0, mFolder.getItemCount() - NUM_ITEMS_IN_PREVIEW); |
| 146 | i < mFolder.getItemCount(); i++) { |
| 147 | v = (TextView) mFolder.getItemAt(i); |
| 148 | d = v.getCompoundDrawables()[1]; |
| 149 | |
| 150 | if (d != null) { |
| 151 | d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight()); |
| 152 | d.draw(canvas); |
| 153 | } |
| 154 | |
| 155 | canvas.translate(d.getIntrinsicWidth() / 2, d.getIntrinsicHeight() / 2); |
| 156 | canvas.rotate(-ICON_ANGLE); |
| 157 | canvas.translate(-d.getIntrinsicWidth() / 2, -d.getIntrinsicHeight() / 2); |
| 158 | } |
| 159 | |
| 160 | canvas.restore(); |
| 161 | } |
| 162 | |
| 163 | public void onAdd(ShortcutInfo item) { |
| 164 | invalidate(); |
| 165 | requestLayout(); |
| 166 | } |
| 167 | |
| 168 | public void onRemove(ShortcutInfo item) { |
| 169 | invalidate(); |
| 170 | requestLayout(); |
| 171 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 172 | } |