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; |
| 21 | import android.graphics.drawable.Drawable; |
| 22 | import android.util.AttributeSet; |
| 23 | import android.view.LayoutInflater; |
| 24 | import android.view.ViewGroup; |
| 25 | |
Romain Guy | edcce09 | 2010-03-04 13:03:17 -0800 | [diff] [blame] | 26 | import com.android.launcher.R; |
| 27 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 28 | /** |
| 29 | * An icon that can appear on in the workspace representing an {@link UserFolder}. |
| 30 | */ |
Michael Jurka | c66a7c2 | 2010-12-04 11:22:18 -0800 | [diff] [blame] | 31 | public class FolderIcon extends BubbleTextView implements DropTarget { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 32 | private UserFolderInfo mInfo; |
| 33 | private Launcher mLauncher; |
| 34 | private Drawable mCloseIcon; |
| 35 | private Drawable mOpenIcon; |
| 36 | |
| 37 | public FolderIcon(Context context, AttributeSet attrs) { |
| 38 | super(context, attrs); |
| 39 | } |
| 40 | |
| 41 | public FolderIcon(Context context) { |
| 42 | super(context); |
| 43 | } |
| 44 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 45 | public boolean isDropEnabled() { |
Winson Chung | 7a1d165 | 2011-03-18 15:56:01 -0700 | [diff] [blame] | 46 | final ViewGroup cellLayoutChildren = (ViewGroup) getParent(); |
| 47 | final ViewGroup cellLayout = (ViewGroup) cellLayoutChildren.getParent(); |
| 48 | final Workspace workspace = (Workspace) cellLayout.getParent(); |
| 49 | return !workspace.isSmall(); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 50 | } |
| 51 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 52 | static FolderIcon fromXml(int resId, Launcher launcher, ViewGroup group, |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 53 | UserFolderInfo folderInfo, IconCache iconCache) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 54 | |
| 55 | FolderIcon icon = (FolderIcon) LayoutInflater.from(launcher).inflate(resId, group, false); |
| 56 | |
| 57 | final Resources resources = launcher.getResources(); |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 58 | Drawable d = iconCache.getFullResIcon(resources, R.drawable.ic_launcher_folder); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 59 | icon.mCloseIcon = d; |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 60 | icon.mOpenIcon = iconCache.getFullResIcon(resources, R.drawable.ic_launcher_folder_open); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 61 | icon.setCompoundDrawablesWithIntrinsicBounds(null, d, null, null); |
| 62 | icon.setText(folderInfo.title); |
| 63 | icon.setTag(folderInfo); |
| 64 | icon.setOnClickListener(launcher); |
| 65 | icon.mInfo = folderInfo; |
| 66 | icon.mLauncher = launcher; |
| 67 | |
| 68 | return icon; |
| 69 | } |
| 70 | |
| 71 | 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] | 72 | DragView dragView, Object dragInfo) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 73 | final ItemInfo item = (ItemInfo) dragInfo; |
| 74 | final int itemType = item.itemType; |
| 75 | return (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION || |
| 76 | itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) |
| 77 | && item.container != mInfo.id; |
| 78 | } |
| 79 | |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 80 | public void addItem(ShortcutInfo item) { |
| 81 | mInfo.add(item); |
| 82 | LauncherModel.addOrMoveItemInDatabase(mLauncher, item, mInfo.id, 0, 0, 0); |
| 83 | } |
| 84 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 85 | public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset, |
| 86 | DragView dragView, Object dragInfo) { |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 87 | ShortcutInfo item; |
| 88 | if (dragInfo instanceof ApplicationInfo) { |
| 89 | // Came from all apps -- make a copy |
| 90 | item = ((ApplicationInfo)dragInfo).makeShortcut(); |
| 91 | } else { |
| 92 | item = (ShortcutInfo)dragInfo; |
| 93 | } |
Michael Jurka | 66d7217 | 2011-04-12 16:29:25 -0700 | [diff] [blame] | 94 | item.cellX = -1; |
| 95 | item.cellY = -1; |
Adam Cohen | df03538 | 2011-04-11 17:22:04 -0700 | [diff] [blame] | 96 | addItem(item); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | 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] | 100 | DragView dragView, Object dragInfo) { |
Patrick Dubroy | 379f160 | 2010-07-14 11:29:09 -0700 | [diff] [blame] | 101 | if (acceptDrop(source, x, y, xOffset, yOffset, dragView, dragInfo)) { |
| 102 | setCompoundDrawablesWithIntrinsicBounds(null, mOpenIcon, null, null); |
| 103 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | 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] | 107 | DragView dragView, Object dragInfo) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | 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] | 111 | DragView dragView, Object dragInfo) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 112 | setCompoundDrawablesWithIntrinsicBounds(null, mCloseIcon, null, null); |
| 113 | } |
Patrick Dubroy | 440c360 | 2010-07-13 17:50:32 -0700 | [diff] [blame] | 114 | |
| 115 | @Override |
| 116 | public DropTarget getDropTargetDelegate(DragSource source, int x, int y, int xOffset, int yOffset, |
| 117 | DragView dragView, Object dragInfo) { |
| 118 | return null; |
| 119 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 120 | } |