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 17 package com.android.launcher2; 18 19 import android.content.Context; 20 import android.content.res.Resources; 21 import android.graphics.Rect; 22 import android.graphics.drawable.Drawable; 23 import android.util.AttributeSet; 24 import android.view.LayoutInflater; 25 import android.view.ViewGroup; 26 27 import com.android.launcher.R; 28 29 /** 30 * An icon that can appear on in the workspace representing an {@link UserFolder}. 31 */ 32 public class FolderIcon extends BubbleTextView implements DropTarget { 33 private UserFolderInfo mInfo; 34 private Launcher mLauncher; 35 private Drawable mCloseIcon; 36 private Drawable mOpenIcon; 37 38 public FolderIcon(Context context, AttributeSet attrs) { 39 super(context, attrs); 40 } 41 42 public FolderIcon(Context context) { 43 super(context); 44 } 45 46 static FolderIcon fromXml(int resId, Launcher launcher, ViewGroup group, 47 UserFolderInfo folderInfo) { 48 49 FolderIcon icon = (FolderIcon) LayoutInflater.from(launcher).inflate(resId, group, false); 50 51 final Resources resources = launcher.getResources(); 52 Drawable d = resources.getDrawable(R.drawable.ic_launcher_folder); 53 icon.mCloseIcon = d; 54 icon.mOpenIcon = resources.getDrawable(R.drawable.ic_launcher_folder_open); 55 icon.setCompoundDrawablesWithIntrinsicBounds(null, d, null, null); 56 icon.setText(folderInfo.title); 57 icon.setTag(folderInfo); 58 icon.setOnClickListener(launcher); 59 icon.mInfo = folderInfo; 60 icon.mLauncher = launcher; 61 62 return icon; 63 } 64 65 public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset, 66 DragView dragView, Object dragInfo) { 67 final ItemInfo item = (ItemInfo) dragInfo; 68 final int itemType = item.itemType; 69 return (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION || 70 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) 71 && item.container != mInfo.id; 72 } 73 74 public Rect estimateDropLocation(DragSource source, int x, int y, int xOffset, int yOffset, 75 DragView dragView, Object dragInfo, Rect recycle) { 76 return null; 77 } 78 79 public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset, 80 DragView dragView, Object dragInfo) { 81 ShortcutInfo item; 82 if (dragInfo instanceof ApplicationInfo) { 83 // Came from all apps -- make a copy 84 item = ((ApplicationInfo)dragInfo).makeShortcut(); 85 } else { 86 item = (ShortcutInfo)dragInfo; 87 } 88 mInfo.add(item); 89 LauncherModel.addOrMoveItemInDatabase(mLauncher, item, mInfo.id, 0, 0, 0); 90 } 91 92 public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset, 93 DragView dragView, Object dragInfo) { 94 setCompoundDrawablesWithIntrinsicBounds(null, mOpenIcon, null, null); 95 } 96 97 public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset, 98 DragView dragView, Object dragInfo) { 99 } 100 101 public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset, 102 DragView dragView, Object dragInfo) { 103 setCompoundDrawablesWithIntrinsicBounds(null, mCloseIcon, null, null); 104 } 105 } 106