FolderIcon.java revision a9cf38f533d1e86269868f1e6a806ccffd4a78fc
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 17package com.android.launcher2; 18 19import android.content.Context; 20import android.content.res.Resources; 21import android.graphics.Canvas; 22import android.graphics.drawable.Drawable; 23import android.util.AttributeSet; 24import android.view.LayoutInflater; 25import android.view.ViewGroup; 26import android.widget.FrameLayout; 27import android.widget.TextView; 28 29import com.android.launcher.R; 30import com.android.launcher2.FolderInfo.FolderListener; 31 32/** 33 * An icon that can appear on in the workspace representing an {@link UserFolder}. 34 */ 35public class FolderIcon extends FrameLayout implements DropTarget, FolderListener { 36 private Launcher mLauncher; 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; 42 43 public FolderIcon(Context context, AttributeSet attrs) { 44 super(context, attrs); 45 } 46 47 public FolderIcon(Context context) { 48 super(context); 49 } 50 51 public boolean isDropEnabled() { 52 final ViewGroup cellLayoutChildren = (ViewGroup) getParent(); 53 final ViewGroup cellLayout = (ViewGroup) cellLayoutChildren.getParent(); 54 final Workspace workspace = (Workspace) cellLayout.getParent(); 55 return !workspace.isSmall(); 56 } 57 58 static FolderIcon fromXml(int resId, Launcher launcher, ViewGroup group, 59 FolderInfo folderInfo, IconCache iconCache) { 60 61 FolderIcon icon = (FolderIcon) LayoutInflater.from(launcher).inflate(resId, group, false); 62 63 final Resources resources = launcher.getResources(); 64 Drawable d = iconCache.getFullResIcon(resources, R.drawable.folder_bg); 65 icon.setBackgroundDrawable(d); 66 icon.setTag(folderInfo); 67 icon.setOnClickListener(launcher); 68 icon.mInfo = folderInfo; 69 icon.mLauncher = launcher; 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 79 return icon; 80 } 81 82 public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset, 83 DragView dragView, Object dragInfo) { 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 91 public void addItem(ShortcutInfo item) { 92 mInfo.add(item); 93 LauncherModel.addOrMoveItemInDatabase(mLauncher, item, mInfo.id, 0, item.cellX, item.cellY); 94 } 95 96 public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset, 97 DragView dragView, Object dragInfo) { 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 } 105 item.cellX = -1; 106 item.cellY = -1; 107 addItem(item); 108 } 109 110 public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset, 111 DragView dragView, Object dragInfo) { 112 } 113 114 public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset, 115 DragView dragView, Object dragInfo) { 116 } 117 118 public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset, 119 DragView dragView, Object dragInfo) { 120 } 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 } 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 } 172} 173