blob: aa7d079c04bcfb0b8092d7dca7d85290c943b016 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
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 Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Adam Cohen2801caf2011-05-13 20:57:39 -070019import android.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
21import android.animation.ObjectAnimator;
22import android.animation.PropertyValuesHolder;
23import android.animation.ValueAnimator;
24import android.animation.ValueAnimator.AnimatorUpdateListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080025import android.content.Context;
26import android.content.res.Resources;
Adam Cohena9cf38f2011-05-02 15:36:58 -070027import android.graphics.Canvas;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.graphics.drawable.Drawable;
29import android.util.AttributeSet;
30import android.view.LayoutInflater;
31import android.view.ViewGroup;
Adam Cohena9cf38f2011-05-02 15:36:58 -070032import android.widget.FrameLayout;
33import android.widget.TextView;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034
Romain Guyedcce092010-03-04 13:03:17 -080035import com.android.launcher.R;
Adam Cohena9cf38f2011-05-02 15:36:58 -070036import com.android.launcher2.FolderInfo.FolderListener;
Romain Guyedcce092010-03-04 13:03:17 -080037
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038/**
39 * An icon that can appear on in the workspace representing an {@link UserFolder}.
40 */
Adam Cohena9cf38f2011-05-02 15:36:58 -070041public class FolderIcon extends FrameLayout implements DropTarget, FolderListener {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042 private Launcher mLauncher;
Adam Cohena9cf38f2011-05-02 15:36:58 -070043 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 Project31dd5032009-03-03 19:32:27 -080048
Adam Cohen2801caf2011-05-13 20:57:39 -070049 int mOriginalWidth;
50 int mOriginalHeight;
51 int mOriginalX;
52 int mOriginalY;
53
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054 public FolderIcon(Context context, AttributeSet attrs) {
55 super(context, attrs);
56 }
57
58 public FolderIcon(Context context) {
59 super(context);
60 }
61
Michael Jurka0280c3b2010-09-17 15:00:07 -070062 public boolean isDropEnabled() {
Winson Chung7a1d1652011-03-18 15:56:01 -070063 final ViewGroup cellLayoutChildren = (ViewGroup) getParent();
64 final ViewGroup cellLayout = (ViewGroup) cellLayoutChildren.getParent();
65 final Workspace workspace = (Workspace) cellLayout.getParent();
66 return !workspace.isSmall();
Michael Jurka0280c3b2010-09-17 15:00:07 -070067 }
68
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069 static FolderIcon fromXml(int resId, Launcher launcher, ViewGroup group,
Adam Cohendf2cc412011-04-27 16:56:57 -070070 FolderInfo folderInfo, IconCache iconCache) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071
72 FolderIcon icon = (FolderIcon) LayoutInflater.from(launcher).inflate(resId, group, false);
73
74 final Resources resources = launcher.getResources();
Adam Cohen2801caf2011-05-13 20:57:39 -070075 Drawable d = iconCache.getFullResIcon(resources, R.drawable.portal_ring_inner_holo);
Adam Cohena9cf38f2011-05-02 15:36:58 -070076 icon.setBackgroundDrawable(d);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080077 icon.setTag(folderInfo);
78 icon.setOnClickListener(launcher);
79 icon.mInfo = folderInfo;
80 icon.mLauncher = launcher;
Adam Cohena9cf38f2011-05-02 15:36:58 -070081
82 Folder folder = Folder.fromXml(launcher);
83 folder.setDragController(launcher.getDragController());
84 folder.setLauncher(launcher);
Adam Cohen2801caf2011-05-13 20:57:39 -070085 folder.setFolderIcon(icon);
Adam Cohena9cf38f2011-05-02 15:36:58 -070086 folder.bind(folderInfo);
87 icon.mFolder = folder;
88
89 folderInfo.addListener(icon);
90
The Android Open Source Project31dd5032009-03-03 19:32:27 -080091 return icon;
92 }
93
94 public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -040095 DragView dragView, Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080096 final ItemInfo item = (ItemInfo) dragInfo;
97 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -070098 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
99 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
100 !mFolder.isFull());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800101 }
102
Adam Cohendf035382011-04-11 17:22:04 -0700103 public void addItem(ShortcutInfo item) {
104 mInfo.add(item);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700105 LauncherModel.addOrMoveItemInDatabase(mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
Adam Cohendf035382011-04-11 17:22:04 -0700106 }
107
Joe Onorato00acb122009-08-04 16:04:30 -0400108 public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset,
109 DragView dragView, Object dragInfo) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800110 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 Jurka66d72172011-04-12 16:29:25 -0700117 item.cellX = -1;
118 item.cellY = -1;
Adam Cohendf035382011-04-11 17:22:04 -0700119 addItem(item);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800120 }
121
Adam Cohen2801caf2011-05-13 20:57:39 -0700122 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 Project31dd5032009-03-03 19:32:27 -0800129 public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -0400130 DragView dragView, Object dragInfo) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700131 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 Project31dd5032009-03-03 19:32:27 -0800148 }
149
150 public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -0400151 DragView dragView, Object dragInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152 }
153
154 public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset,
Joe Onorato00acb122009-08-04 16:04:30 -0400155 DragView dragView, Object dragInfo) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700156 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 Project31dd5032009-03-03 19:32:27 -0800178 }
Patrick Dubroy440c3602010-07-13 17:50:32 -0700179
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 Cohena9cf38f2011-05-02 15:36:58 -0700185
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 Project31dd5032009-03-03 19:32:27 -0800230}