blob: 45730e01822bc93aaf5e60e6eccc1791a23224b2 [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 Cohendf2cc412011-04-27 16:56:57 -070019import android.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
Adam Cohendf2cc412011-04-27 16:56:57 -070021import 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;
Adam Cohen76fc0852011-06-17 13:26:23 -070026import android.content.res.Resources;
Romain Guyfb5411e2010-02-24 10:04:17 -080027import android.graphics.Rect;
Adam Cohenbadf71e2011-05-26 19:08:29 -070028import android.graphics.drawable.Drawable;
Adam Cohen7a14d0b2011-06-24 11:36:35 -070029import android.text.InputType;
Adam Cohene601a432011-07-26 21:51:30 -070030import android.text.Selection;
31import android.text.Spannable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.util.AttributeSet;
Adam Cohen76fc0852011-06-17 13:26:23 -070033import android.view.ActionMode;
34import android.view.KeyEvent;
Adam Cohendf2cc412011-04-27 16:56:57 -070035import android.view.LayoutInflater;
Adam Cohen76fc0852011-06-17 13:26:23 -070036import android.view.Menu;
37import android.view.MenuItem;
Adam Cohen0c872ba2011-05-05 10:34:16 -070038import android.view.MotionEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.view.View;
Adam Cohen2801caf2011-05-13 20:57:39 -070040import android.view.animation.AccelerateInterpolator;
41import android.view.animation.DecelerateInterpolator;
Adam Cohen76fc0852011-06-17 13:26:23 -070042import android.view.inputmethod.EditorInfo;
43import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080044import android.widget.LinearLayout;
Adam Cohendf2cc412011-04-27 16:56:57 -070045import android.widget.TextView;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046
Romain Guyedcce092010-03-04 13:03:17 -080047import com.android.launcher.R;
Adam Cohena9cf38f2011-05-02 15:36:58 -070048import com.android.launcher2.FolderInfo.FolderListener;
Romain Guyedcce092010-03-04 13:03:17 -080049
Adam Cohenc0dcf592011-06-01 15:30:43 -070050import java.util.ArrayList;
51
The Android Open Source Project31dd5032009-03-03 19:32:27 -080052/**
53 * Represents a set of icons chosen by the user or generated by the system.
54 */
Adam Cohen8dfcba42011-07-07 16:38:18 -070055public class Folder extends LinearLayout implements DragSource, View.OnClickListener,
56 View.OnLongClickListener, DropTarget, FolderListener, TextView.OnEditorActionListener {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057
Adam Cohendf2cc412011-04-27 16:56:57 -070058 private static final String TAG = "Launcher.Folder";
59
Adam Cohen4eac29a2011-07-11 17:53:37 -070060 protected DragController mDragController;
61 protected Launcher mLauncher;
62 protected FolderInfo mInfo;
63
Adam Cohendf2cc412011-04-27 16:56:57 -070064 static final int STATE_NONE = -1;
65 static final int STATE_SMALL = 0;
66 static final int STATE_ANIMATING = 1;
67 static final int STATE_OPEN = 2;
68
69 private int mExpandDuration;
70 protected CellLayout mContent;
71 private final LayoutInflater mInflater;
72 private final IconCache mIconCache;
73 private int mState = STATE_NONE;
Adam Cohen2801caf2011-05-13 20:57:39 -070074 private static final int FULL_GROW = 0;
75 private static final int PARTIAL_GROW = 1;
Adam Cohenbfbfd262011-06-13 16:55:12 -070076 private static final int REORDER_ANIMATION_DURATION = 230;
77 private static final int ON_EXIT_CLOSE_DELAY = 800;
Adam Cohen2801caf2011-05-13 20:57:39 -070078 private int mMode = PARTIAL_GROW;
79 private boolean mRearrangeOnClose = false;
80 private FolderIcon mFolderIcon;
81 private int mMaxCountX;
82 private int mMaxCountY;
83 private Rect mNewSize = new Rect();
Adam Cohen3e8f8112011-07-02 18:03:00 -070084 private Rect mIconRect = new Rect();
Adam Cohen7c693212011-05-18 15:26:57 -070085 private ArrayList<View> mItemsInReadingOrder = new ArrayList<View>();
Adam Cohenbadf71e2011-05-26 19:08:29 -070086 private Drawable mIconDrawable;
Adam Cohen7c693212011-05-18 15:26:57 -070087 boolean mItemsInvalidated = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -070088 private ShortcutInfo mCurrentDragInfo;
89 private View mCurrentDragView;
90 boolean mSuppressOnAdd = false;
91 private int[] mTargetCell = new int[2];
92 private int[] mPreviousTargetCell = new int[2];
93 private int[] mEmptyCell = new int[2];
94 private Alarm mReorderAlarm = new Alarm();
95 private Alarm mOnExitAlarm = new Alarm();
Adam Cohen76fc0852011-06-17 13:26:23 -070096 private TextView mFolderName;
97 private int mFolderNameHeight;
Adam Cohen4ef610f2011-06-21 22:37:36 -070098 private Rect mHitRect = new Rect();
Adam Cohen8e776a62011-06-28 18:10:06 -070099 private Rect mTempRect = new Rect();
Adam Cohen8dfcba42011-07-07 16:38:18 -0700100 private boolean mFirstOpen = true;
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700101 private boolean mDragInProgress = false;
102 private boolean mDeleteFolderOnDropCompleted = false;
103 private boolean mSuppressFolderDeletion = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700104 private boolean mItemAddedBackToSelfViaIcon = false;
Adam Cohen228da5a2011-07-27 22:23:47 -0700105
Adam Cohen76fc0852011-06-17 13:26:23 -0700106 private boolean mIsEditingName = false;
107 private InputMethodManager mInputMethodManager;
Adam Cohendf2cc412011-04-27 16:56:57 -0700108
Adam Cohena65beee2011-06-27 21:32:23 -0700109 private static String sDefaultFolderName;
110 private static String sHintText;
111
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800112 /**
113 * Used to inflate the Workspace from XML.
114 *
115 * @param context The application's context.
116 * @param attrs The attribtues set containing the Workspace's customization values.
117 */
118 public Folder(Context context, AttributeSet attrs) {
119 super(context, attrs);
120 setAlwaysDrawnWithCacheEnabled(false);
Adam Cohendf2cc412011-04-27 16:56:57 -0700121 mInflater = LayoutInflater.from(context);
122 mIconCache = ((LauncherApplication)context.getApplicationContext()).getIconCache();
Adam Cohen8e776a62011-06-28 18:10:06 -0700123 mMaxCountX = LauncherModel.getCellCountX();
124 mMaxCountY = LauncherModel.getCellCountY();
Adam Cohen76fc0852011-06-17 13:26:23 -0700125
126 mInputMethodManager = (InputMethodManager)
127 mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
128
129 Resources res = getResources();
130 mExpandDuration = res.getInteger(R.integer.config_folderAnimDuration);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700131
132 if (sDefaultFolderName == null) {
133 sDefaultFolderName = res.getString(R.string.folder_name);
134 }
Adam Cohena65beee2011-06-27 21:32:23 -0700135 if (sHintText == null) {
136 sHintText = res.getString(R.string.folder_hint_text);
137 }
Adam Cohen4eac29a2011-07-11 17:53:37 -0700138
139 mLauncher = (Launcher) context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140 }
141
142 @Override
143 protected void onFinishInflate() {
144 super.onFinishInflate();
Adam Cohendf2cc412011-04-27 16:56:57 -0700145 mContent = (CellLayout) findViewById(R.id.folder_content);
Adam Cohen2801caf2011-05-13 20:57:39 -0700146 mContent.setGridSize(0, 0);
Adam Cohen76fc0852011-06-17 13:26:23 -0700147 mFolderName = (TextView) findViewById(R.id.folder_name);
148
149 // We find out how tall the text view wants to be (it is set to wrap_content), so that
150 // we can allocate the appropriate amount of space for it.
151 int measureSpec = MeasureSpec.UNSPECIFIED;
152 mFolderName.measure(measureSpec, measureSpec);
153 mFolderNameHeight = mFolderName.getMeasuredHeight();
154
155 // We disable action mode for now since it messes up the view on phones
156 mFolderName.setCustomSelectionActionModeCallback(mActionModeCallback);
157 mFolderName.setCursorVisible(false);
158 mFolderName.setOnEditorActionListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700159 mFolderName.setSelectAllOnFocus(true);
Adam Cohen7a14d0b2011-06-24 11:36:35 -0700160 mFolderName.setInputType(mFolderName.getInputType() |
161 InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700163
Adam Cohen76fc0852011-06-17 13:26:23 -0700164 private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
165 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
166 return false;
167 }
168
169 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
170 return false;
171 }
172
173 public void onDestroyActionMode(ActionMode mode) {
174 }
175
176 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
177 return false;
178 }
179 };
180
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181 public void onClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700182 Object tag = v.getTag();
183 if (tag instanceof ShortcutInfo) {
184 // refactor this code from Folder
185 ShortcutInfo item = (ShortcutInfo) tag;
186 int[] pos = new int[2];
187 v.getLocationOnScreen(pos);
188 item.intent.setSourceBounds(new Rect(pos[0], pos[1],
189 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
190 mLauncher.startActivitySafely(item.intent, item);
Adam Cohendf2cc412011-04-27 16:56:57 -0700191 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192 }
193
Adam Cohen76fc0852011-06-17 13:26:23 -0700194 public boolean onInterceptTouchEvent(MotionEvent ev) {
195 if (ev.getAction() == MotionEvent.ACTION_DOWN) {
196 mFolderName.getHitRect(mHitRect);
197 if (mHitRect.contains((int) ev.getX(), (int) ev.getY()) && !mIsEditingName) {
198 startEditingFolderName();
199 }
200 }
201 return false;
202 }
203
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800204 public boolean onLongClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700205 Object tag = v.getTag();
206 if (tag instanceof ShortcutInfo) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700207 ShortcutInfo item = (ShortcutInfo) tag;
208 if (!v.isInTouchMode()) {
209 return false;
210 }
211
212 mLauncher.getWorkspace().onDragStartedWithItem(v);
Adam Cohenac8c8762011-07-13 11:15:27 -0700213 mLauncher.getWorkspace().beginDragShared(v, this);
Adam Cohenbadf71e2011-05-26 19:08:29 -0700214 mIconDrawable = ((TextView) v).getCompoundDrawables()[1];
Adam Cohen76078c42011-06-09 15:06:52 -0700215
216 mCurrentDragInfo = item;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700217 mEmptyCell[0] = item.cellX;
218 mEmptyCell[1] = item.cellY;
219 mCurrentDragView = v;
Adam Cohenfc53cd22011-07-20 15:45:11 -0700220
221 mContent.removeView(mCurrentDragView);
222 mInfo.remove(mCurrentDragInfo);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700223 mDragInProgress = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700224 mItemAddedBackToSelfViaIcon = false;
Adam Cohendf2cc412011-04-27 16:56:57 -0700225 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800226 return true;
227 }
228
Adam Cohen76fc0852011-06-17 13:26:23 -0700229 public boolean isEditingName() {
230 return mIsEditingName;
231 }
232
233 public void startEditingFolderName() {
Adam Cohena65beee2011-06-27 21:32:23 -0700234 mFolderName.setHint("");
Adam Cohen76fc0852011-06-17 13:26:23 -0700235 mFolderName.setCursorVisible(true);
236 mIsEditingName = true;
237 }
238
239 public void dismissEditingName() {
240 mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
241 doneEditingFolderName(true);
242 }
243
244 public void doneEditingFolderName(boolean commit) {
Adam Cohena65beee2011-06-27 21:32:23 -0700245 mFolderName.setHint(sHintText);
Adam Cohen76fc0852011-06-17 13:26:23 -0700246 mInfo.setTitle(mFolderName.getText());
247 LauncherModel.updateItemInDatabase(mLauncher, mInfo);
248 mFolderName.setCursorVisible(false);
249 mFolderName.clearFocus();
Adam Cohene601a432011-07-26 21:51:30 -0700250 Selection.setSelection((Spannable) mFolderName.getText(), 0, 0);
Adam Cohen76fc0852011-06-17 13:26:23 -0700251 mIsEditingName = false;
252 }
253
254 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
255 if (actionId == EditorInfo.IME_ACTION_DONE) {
256 dismissEditingName();
257 return true;
258 }
259 return false;
260 }
261
262 public View getEditTextRegion() {
263 return mFolderName;
264 }
265
Adam Cohenbadf71e2011-05-26 19:08:29 -0700266 public Drawable getDragDrawable() {
267 return mIconDrawable;
268 }
269
Adam Cohen0c872ba2011-05-05 10:34:16 -0700270 /**
271 * We need to handle touch events to prevent them from falling through to the workspace below.
272 */
273 @Override
274 public boolean onTouchEvent(MotionEvent ev) {
275 return true;
276 }
277
Joe Onorato00acb122009-08-04 16:04:30 -0400278 public void setDragController(DragController dragController) {
279 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800280 }
281
Adam Cohen2801caf2011-05-13 20:57:39 -0700282 void setFolderIcon(FolderIcon icon) {
283 mFolderIcon = icon;
284 }
285
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800286 /**
287 * @return the FolderInfo object associated with this folder
288 */
289 FolderInfo getInfo() {
290 return mInfo;
291 }
292
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800293 void onOpen() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700294 // When the folder opens, we need to refresh the GridView's selection by
295 // forcing a layout
296 // TODO: find out if this is still necessary
297 mContent.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800298 }
299
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800300 void bind(FolderInfo info) {
301 mInfo = info;
Adam Cohendf2cc412011-04-27 16:56:57 -0700302 ArrayList<ShortcutInfo> children = info.contents;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700303 ArrayList<ShortcutInfo> overflow = new ArrayList<ShortcutInfo>();
Adam Cohen7c693212011-05-18 15:26:57 -0700304 setupContentForNumItems(children.size());
Adam Cohendf2cc412011-04-27 16:56:57 -0700305 for (int i = 0; i < children.size(); i++) {
306 ShortcutInfo child = (ShortcutInfo) children.get(i);
Adam Cohenc508b2d2011-06-28 14:41:44 -0700307 if (!createAndAddShortcut(child)) {
308 overflow.add(child);
309 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700310 }
Adam Cohenc508b2d2011-06-28 14:41:44 -0700311
312 // If our folder has too many items we prune them from the list. This is an issue
313 // when upgrading from the old Folders implementation which could contain an unlimited
314 // number of items.
315 for (ShortcutInfo item: overflow) {
316 mInfo.remove(item);
317 LauncherModel.deleteItemFromDatabase(mLauncher, item);
318 }
319
Adam Cohen4dbe6d92011-05-18 17:14:15 -0700320 mItemsInvalidated = true;
Adam Cohena9cf38f2011-05-02 15:36:58 -0700321 mInfo.addListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700322
Adam Cohenafb01ee2011-06-23 15:38:03 -0700323 if (!sDefaultFolderName.contentEquals(mInfo.title)) {
Adam Cohen4ef610f2011-06-21 22:37:36 -0700324 mFolderName.setText(mInfo.title);
325 } else {
326 mFolderName.setText("");
327 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700328 }
329
330 /**
331 * Creates a new UserFolder, inflated from R.layout.user_folder.
332 *
333 * @param context The application's context.
334 *
335 * @return A new UserFolder.
336 */
337 static Folder fromXml(Context context) {
338 return (Folder) LayoutInflater.from(context).inflate(R.layout.user_folder, null);
339 }
340
341 /**
342 * This method is intended to make the UserFolder to be visually identical in size and position
343 * to its associated FolderIcon. This allows for a seamless transition into the expanded state.
344 */
345 private void positionAndSizeAsIcon() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700346 if (!(getParent() instanceof DragLayer)) return;
Adam Cohendf2cc412011-04-27 16:56:57 -0700347
Adam Cohen8e776a62011-06-28 18:10:06 -0700348 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohendf2cc412011-04-27 16:56:57 -0700349
Adam Cohen2801caf2011-05-13 20:57:39 -0700350 if (mMode == PARTIAL_GROW) {
351 setScaleX(0.8f);
352 setScaleY(0.8f);
353 setAlpha(0f);
354 } else {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700355 mLauncher.getDragLayer().getDescendantRectRelativeToSelf(mFolderIcon, mIconRect);
356 lp.width = mIconRect.width();
357 lp.height = mIconRect.height();
358 lp.x = mIconRect.left;
359 lp.y = mIconRect.top;
Adam Cohen2801caf2011-05-13 20:57:39 -0700360 mContent.setAlpha(0);
361 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700362 mState = STATE_SMALL;
363 }
364
365 public void animateOpen() {
Adam Cohen8dfcba42011-07-07 16:38:18 -0700366 if (mFirstOpen) {
367 setLayerType(LAYER_TYPE_HARDWARE, null);
368 buildLayer();
369 mFirstOpen = false;
370 }
371
Adam Cohen3e8f8112011-07-02 18:03:00 -0700372 positionAndSizeAsIcon();
373
Adam Cohen8e776a62011-06-28 18:10:06 -0700374 if (!(getParent() instanceof DragLayer)) return;
Adam Cohendf2cc412011-04-27 16:56:57 -0700375
Adam Cohen2801caf2011-05-13 20:57:39 -0700376 ObjectAnimator oa;
Adam Cohen8e776a62011-06-28 18:10:06 -0700377 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohendf2cc412011-04-27 16:56:57 -0700378
Adam Cohen2801caf2011-05-13 20:57:39 -0700379 centerAboutIcon();
380 if (mMode == PARTIAL_GROW) {
381 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1);
382 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
383 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
384 oa = ObjectAnimator.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
385 } else {
386 PropertyValuesHolder width = PropertyValuesHolder.ofInt("width", mNewSize.width());
387 PropertyValuesHolder height = PropertyValuesHolder.ofInt("height", mNewSize.height());
388 PropertyValuesHolder x = PropertyValuesHolder.ofInt("x", mNewSize.left);
389 PropertyValuesHolder y = PropertyValuesHolder.ofInt("y", mNewSize.top);
390 oa = ObjectAnimator.ofPropertyValuesHolder(lp, width, height, x, y);
391 oa.addUpdateListener(new AnimatorUpdateListener() {
392 public void onAnimationUpdate(ValueAnimator animation) {
393 requestLayout();
394 }
395 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700396
Adam Cohen2801caf2011-05-13 20:57:39 -0700397 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
398 ObjectAnimator alphaOa = ObjectAnimator.ofPropertyValuesHolder(mContent, alpha);
399 alphaOa.setDuration(mExpandDuration);
400 alphaOa.setInterpolator(new AccelerateInterpolator(2.0f));
401 alphaOa.start();
402 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700403
Adam Cohen2801caf2011-05-13 20:57:39 -0700404 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700405 @Override
406 public void onAnimationStart(Animator animation) {
407 mState = STATE_ANIMATING;
408 }
409 @Override
410 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700411 mState = STATE_OPEN;
Adam Cohen8dfcba42011-07-07 16:38:18 -0700412 setLayerType(LAYER_TYPE_NONE, null);
413 enableHardwareLayersForChildren();
Adam Cohendf2cc412011-04-27 16:56:57 -0700414 }
415 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700416 oa.setDuration(mExpandDuration);
417 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700418 }
419
Adam Cohen8dfcba42011-07-07 16:38:18 -0700420 void enableHardwareLayersForChildren() {
421 ArrayList<View> children = getItemsInReadingOrder();
422 for (View child: children) {
423 child.setLayerType(LAYER_TYPE_HARDWARE, null);
424 }
425 }
426
Adam Cohendf2cc412011-04-27 16:56:57 -0700427 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700428 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen8dfcba42011-07-07 16:38:18 -0700429 setLayerType(LAYER_TYPE_HARDWARE, null);
430 buildLayer();
Adam Cohendf2cc412011-04-27 16:56:57 -0700431
Adam Cohen2801caf2011-05-13 20:57:39 -0700432 ObjectAnimator oa;
Adam Cohen2801caf2011-05-13 20:57:39 -0700433 if (mMode == PARTIAL_GROW) {
434 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
435 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
436 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
437 oa = ObjectAnimator.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
438 } else {
Adam Cohen8e776a62011-06-28 18:10:06 -0700439 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohendf2cc412011-04-27 16:56:57 -0700440
Adam Cohen3e8f8112011-07-02 18:03:00 -0700441 PropertyValuesHolder width = PropertyValuesHolder.ofInt("width", mIconRect.width());
442 PropertyValuesHolder height = PropertyValuesHolder.ofInt("height", mIconRect.height());
443 PropertyValuesHolder x = PropertyValuesHolder.ofInt("x", mIconRect.left);
444 PropertyValuesHolder y = PropertyValuesHolder.ofInt("y", mIconRect.top);
Adam Cohen2801caf2011-05-13 20:57:39 -0700445 oa = ObjectAnimator.ofPropertyValuesHolder(lp, width, height, x, y);
446 oa.addUpdateListener(new AnimatorUpdateListener() {
447 public void onAnimationUpdate(ValueAnimator animation) {
448 requestLayout();
449 }
450 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700451
Adam Cohen2801caf2011-05-13 20:57:39 -0700452 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0f);
453 ObjectAnimator alphaOa = ObjectAnimator.ofPropertyValuesHolder(mContent, alpha);
454 alphaOa.setDuration(mExpandDuration);
455 alphaOa.setInterpolator(new DecelerateInterpolator(2.0f));
456 alphaOa.start();
457 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700458
Adam Cohen2801caf2011-05-13 20:57:39 -0700459 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700460 @Override
461 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700462 onCloseComplete();
Adam Cohen2801caf2011-05-13 20:57:39 -0700463 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700464 }
465 @Override
466 public void onAnimationStart(Animator animation) {
467 mState = STATE_ANIMATING;
468 }
469 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700470 oa.setDuration(mExpandDuration);
471 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700472 }
473
474 void notifyDataSetChanged() {
475 // recreate all the children if the data set changes under us. We may want to do this more
476 // intelligently (ie just removing the views that should no longer exist)
477 mContent.removeAllViewsInLayout();
478 bind(mInfo);
479 }
480
Adam Cohencb3382b2011-05-24 14:07:08 -0700481 public boolean acceptDrop(DragObject d) {
482 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700483 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700484 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
485 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
486 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700487 }
488
Adam Cohendf2cc412011-04-27 16:56:57 -0700489 protected boolean findAndSetEmptyCells(ShortcutInfo item) {
490 int[] emptyCell = new int[2];
491 if (mContent.findCellForSpan(emptyCell, item.spanX, item.spanY)) {
492 item.cellX = emptyCell[0];
493 item.cellY = emptyCell[1];
Adam Cohendf2cc412011-04-27 16:56:57 -0700494 return true;
495 } else {
496 return false;
497 }
498 }
499
Adam Cohenc508b2d2011-06-28 14:41:44 -0700500 protected boolean createAndAddShortcut(ShortcutInfo item) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700501 final TextView textView =
Adam Cohene87b9242011-06-29 14:01:26 -0700502 (TextView) mInflater.inflate(R.layout.application, this, false);
Adam Cohendf2cc412011-04-27 16:56:57 -0700503 textView.setCompoundDrawablesWithIntrinsicBounds(null,
504 new FastBitmapDrawable(item.getIcon(mIconCache)), null, null);
505 textView.setText(item.title);
506 textView.setTag(item);
507
508 textView.setOnClickListener(this);
509 textView.setOnLongClickListener(this);
510
Adam Cohenc508b2d2011-06-28 14:41:44 -0700511 // We need to check here to verify that the given item's location isn't already occupied
512 // by another item. If it is, we need to find the next available slot and assign
513 // it that position. This is an issue when upgrading from the old Folders implementation
514 // which could contain an unlimited number of items.
Adam Cohen228da5a2011-07-27 22:23:47 -0700515 if (mContent.getChildAt(item.cellX, item.cellY) != null ||
516 item.cellX < 0 || item.cellY < 0) {
Adam Cohenc508b2d2011-06-28 14:41:44 -0700517 if (!findAndSetEmptyCells(item)) {
518 return false;
519 }
520 }
521
Adam Cohendf2cc412011-04-27 16:56:57 -0700522 CellLayout.LayoutParams lp =
523 new CellLayout.LayoutParams(item.cellX, item.cellY, item.spanX, item.spanY);
524 boolean insert = false;
525 mContent.addViewToCellLayout(textView, insert ? 0 : -1, (int)item.id, lp, true);
Adam Cohenc508b2d2011-06-28 14:41:44 -0700526 return true;
Adam Cohendf2cc412011-04-27 16:56:57 -0700527 }
528
Adam Cohencb3382b2011-05-24 14:07:08 -0700529 public void onDragEnter(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700530 mPreviousTargetCell[0] = -1;
531 mPreviousTargetCell[1] = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700532 mOnExitAlarm.cancelAlarm();
533 }
534
535 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
536 public void onAlarm(Alarm alarm) {
537 realTimeReorder(mEmptyCell, mTargetCell);
538 }
539 };
540
541 boolean readingOrderGreaterThan(int[] v1, int[] v2) {
542 if (v1[1] > v2[1] || (v1[1] == v2[1] && v1[0] > v2[0])) {
543 return true;
544 } else {
545 return false;
546 }
547 }
548
549 private void realTimeReorder(int[] empty, int[] target) {
550 boolean wrap;
551 int startX;
552 int endX;
553 int startY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700554 int delay = 0;
555 float delayAmount = 30;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700556 if (readingOrderGreaterThan(target, empty)) {
557 wrap = empty[0] >= mContent.getCountX() - 1;
558 startY = wrap ? empty[1] + 1 : empty[1];
559 for (int y = startY; y <= target[1]; y++) {
560 startX = y == empty[1] ? empty[0] + 1 : 0;
561 endX = y < target[1] ? mContent.getCountX() - 1 : target[0];
562 for (int x = startX; x <= endX; x++) {
563 View v = mContent.getChildAt(x,y);
564 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen76fc0852011-06-17 13:26:23 -0700565 REORDER_ANIMATION_DURATION, delay)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700566 empty[0] = x;
567 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700568 delay += delayAmount;
569 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700570 }
571 }
572 }
573 } else {
574 wrap = empty[0] == 0;
575 startY = wrap ? empty[1] - 1 : empty[1];
576 for (int y = startY; y >= target[1]; y--) {
577 startX = y == empty[1] ? empty[0] - 1 : mContent.getCountX() - 1;
578 endX = y > target[1] ? 0 : target[0];
579 for (int x = startX; x >= endX; x--) {
580 View v = mContent.getChildAt(x,y);
581 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen76fc0852011-06-17 13:26:23 -0700582 REORDER_ANIMATION_DURATION, delay)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700583 empty[0] = x;
584 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700585 delay += delayAmount;
586 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700587 }
588 }
589 }
590 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700591 }
592
Adam Cohencb3382b2011-05-24 14:07:08 -0700593 public void onDragOver(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700594 float[] r = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, d.dragView, null);
595 mTargetCell = mContent.findNearestArea((int) r[0], (int) r[1], 1, 1, mTargetCell);
596
597 if (mTargetCell[0] != mPreviousTargetCell[0] || mTargetCell[1] != mPreviousTargetCell[1]) {
598 mReorderAlarm.cancelAlarm();
599 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
600 mReorderAlarm.setAlarm(150);
601 mPreviousTargetCell[0] = mTargetCell[0];
602 mPreviousTargetCell[1] = mTargetCell[1];
603 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700604 }
605
Adam Cohenbfbfd262011-06-13 16:55:12 -0700606 // This is used to compute the visual center of the dragView. The idea is that
607 // the visual center represents the user's interpretation of where the item is, and hence
608 // is the appropriate point to use when determining drop location.
609 private float[] getDragViewVisualCenter(int x, int y, int xOffset, int yOffset,
610 DragView dragView, float[] recycle) {
611 float res[];
612 if (recycle == null) {
613 res = new float[2];
614 } else {
615 res = recycle;
616 }
617
618 // These represent the visual top and left of drag view if a dragRect was provided.
619 // If a dragRect was not provided, then they correspond to the actual view left and
620 // top, as the dragRect is in that case taken to be the entire dragView.
621 // R.dimen.dragViewOffsetY.
622 int left = x - xOffset;
623 int top = y - yOffset;
624
625 // In order to find the visual center, we shift by half the dragRect
626 res[0] = left + dragView.getDragRegion().width() / 2;
627 res[1] = top + dragView.getDragRegion().height() / 2;
628
629 return res;
630 }
631
632 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
633 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700634 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700635 }
636 };
637
Adam Cohen95bb8002011-07-03 23:40:28 -0700638 public void completeDragExit() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700639 mLauncher.closeFolder();
640 mCurrentDragInfo = null;
641 mCurrentDragView = null;
642 mSuppressOnAdd = false;
643 mRearrangeOnClose = true;
644 }
645
Adam Cohencb3382b2011-05-24 14:07:08 -0700646 public void onDragExit(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700647 // We only close the folder if this is a true drag exit, ie. not because a drop
648 // has occurred above the folder.
649 if (!d.dragComplete) {
650 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
651 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
652 }
653 mReorderAlarm.cancelAlarm();
Adam Cohen2801caf2011-05-13 20:57:39 -0700654 }
655
Adam Cohenc0dcf592011-06-01 15:30:43 -0700656 public void onDropCompleted(View target, DragObject d, boolean success) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700657 if (success) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700658 if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700659 replaceFolderWithFinalItem();
660 }
661 } else {
662 // The drag failed, we need to return the item to the folder
663 mFolderIcon.onDrop(d);
664
665 // We're going to trigger a "closeFolder" which may occur before this item has
666 // been added back to the folder -- this could cause the folder to be deleted
667 if (mOnExitAlarm.alarmPending()) {
668 mSuppressFolderDeletion = true;
669 }
670 }
671
672 if (target != this) {
673 if (mOnExitAlarm.alarmPending()) {
674 mOnExitAlarm.cancelAlarm();
675 completeDragExit();
676 }
677 }
678 mDeleteFolderOnDropCompleted = false;
679 mDragInProgress = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700680 mItemAddedBackToSelfViaIcon = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700681 mCurrentDragInfo = null;
682 mCurrentDragView = null;
683 mSuppressOnAdd = false;
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700684 }
Adam Cohen228da5a2011-07-27 22:23:47 -0700685
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700686 public void notifyDrop() {
687 if (mDragInProgress) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700688 mItemAddedBackToSelfViaIcon = true;
Adam Cohen76078c42011-06-09 15:06:52 -0700689 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700690 }
691
692 public boolean isDropEnabled() {
693 return true;
694 }
695
Adam Cohencb3382b2011-05-24 14:07:08 -0700696 public DropTarget getDropTargetDelegate(DragObject d) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700697 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800698 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700699
Adam Cohen2801caf2011-05-13 20:57:39 -0700700 private void setupContentDimension(int count) {
701 ArrayList<View> list = getItemsInReadingOrder();
702
703 int countX = mContent.getCountX();
704 int countY = mContent.getCountY();
Adam Cohen7c693212011-05-18 15:26:57 -0700705 boolean done = false;
Adam Cohen2801caf2011-05-13 20:57:39 -0700706
Adam Cohen7c693212011-05-18 15:26:57 -0700707 while (!done) {
708 int oldCountX = countX;
709 int oldCountY = countY;
710 if (countX * countY < count) {
711 // Current grid is too small, expand it
712 if (countX <= countY && countX < mMaxCountX) {
713 countX++;
714 } else if (countY < mMaxCountY) {
715 countY++;
716 }
717 if (countY == 0) countY++;
718 } else if ((countY - 1) * countX >= count && countY >= countX) {
719 countY = Math.max(0, countY - 1);
720 } else if ((countX - 1) * countY >= count) {
721 countX = Math.max(0, countX - 1);
Adam Cohen2801caf2011-05-13 20:57:39 -0700722 }
Adam Cohen7c693212011-05-18 15:26:57 -0700723 done = countX == oldCountX && countY == oldCountY;
Adam Cohen2801caf2011-05-13 20:57:39 -0700724 }
Adam Cohen7c693212011-05-18 15:26:57 -0700725 mContent.setGridSize(countX, countY);
Adam Cohen2801caf2011-05-13 20:57:39 -0700726 arrangeChildren(list);
727 }
728
729 public boolean isFull() {
730 return getItemCount() >= mMaxCountX * mMaxCountY;
731 }
732
733 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700734 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700735
736 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf4bb1cd2011-07-22 14:36:03 -0700737 int height = getPaddingTop() + getPaddingBottom() + mContent.getDesiredHeight()
738 + mFolderNameHeight;
Adam Cohen8e776a62011-06-28 18:10:06 -0700739 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700740
Adam Cohen8e776a62011-06-28 18:10:06 -0700741 parent.getDescendantRectRelativeToSelf(mFolderIcon, mTempRect);
742
743 int centerX = mTempRect.centerX();
744 int centerY = mTempRect.centerY();
Adam Cohen2801caf2011-05-13 20:57:39 -0700745 int centeredLeft = centerX - width / 2;
746 int centeredTop = centerY - height / 2;
747
Adam Cohen35e7e642011-07-17 14:47:18 -0700748 // We first fetch the currently visible CellLayoutChildren
Winson Chung3d503fb2011-07-13 17:25:49 -0700749 CellLayout currentPage = mLauncher.getWorkspace().getCurrentDropLayout();
Adam Cohen35e7e642011-07-17 14:47:18 -0700750 CellLayoutChildren boundingLayout = currentPage.getChildrenLayout();
751 Rect bounds = new Rect();
752 parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
Adam Cohen2801caf2011-05-13 20:57:39 -0700753
Adam Cohen35e7e642011-07-17 14:47:18 -0700754 // We need to bound the folder to the currently visible CellLayoutChildren
755 int left = Math.min(Math.max(bounds.left, centeredLeft),
756 bounds.left + bounds.width() - width);
757 int top = Math.min(Math.max(bounds.top, centeredTop),
758 bounds.top + bounds.height() - height);
759 // If the folder doesn't fit within the bounds, center it about the desired bounds
760 if (width >= bounds.width()) {
761 left = bounds.left + (bounds.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700762 }
Adam Cohen35e7e642011-07-17 14:47:18 -0700763 if (height >= bounds.height()) {
764 top = bounds.top + (bounds.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700765 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700766
767 int folderPivotX = width / 2 + (centeredLeft - left);
768 int folderPivotY = height / 2 + (centeredTop - top);
769 setPivotX(folderPivotX);
770 setPivotY(folderPivotY);
771 int folderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
772 (1.0f * folderPivotX / width));
773 int folderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
774 (1.0f * folderPivotY / height));
775 mFolderIcon.setPivotX(folderIconPivotX);
776 mFolderIcon.setPivotY(folderIconPivotY);
777
778 if (mMode == PARTIAL_GROW) {
779 lp.width = width;
780 lp.height = height;
781 lp.x = left;
782 lp.y = top;
783 } else {
784 mNewSize.set(left, top, left + width, top + height);
785 }
786 }
787
788 private void setupContentForNumItems(int count) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700789 setupContentDimension(count);
790
Adam Cohen8e776a62011-06-28 18:10:06 -0700791 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700792 if (lp == null) {
Adam Cohen8e776a62011-06-28 18:10:06 -0700793 lp = new DragLayer.LayoutParams(0, 0);
794 lp.customPosition = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700795 setLayoutParams(lp);
796 }
797 centerAboutIcon();
798 }
799
Adam Cohen234c4cd2011-07-17 21:03:04 -0700800 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
801 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf4bb1cd2011-07-22 14:36:03 -0700802 int height = getPaddingTop() + getPaddingBottom() + mContent.getDesiredHeight()
803 + mFolderNameHeight;
Adam Cohen234c4cd2011-07-17 21:03:04 -0700804
805 int contentWidthSpec = MeasureSpec.makeMeasureSpec(mContent.getDesiredWidth(),
806 MeasureSpec.EXACTLY);
807 int contentHeightSpec = MeasureSpec.makeMeasureSpec(mContent.getDesiredHeight(),
808 MeasureSpec.EXACTLY);
809 mContent.measure(contentWidthSpec, contentHeightSpec);
810
811 mFolderName.measure(contentWidthSpec,
812 MeasureSpec.makeMeasureSpec(mFolderNameHeight, MeasureSpec.EXACTLY));
813 setMeasuredDimension(width, height);
814 }
815
Adam Cohen2801caf2011-05-13 20:57:39 -0700816 private void arrangeChildren(ArrayList<View> list) {
817 int[] vacant = new int[2];
818 if (list == null) {
819 list = getItemsInReadingOrder();
820 }
821 mContent.removeAllViews();
822
823 for (int i = 0; i < list.size(); i++) {
824 View v = list.get(i);
825 mContent.getVacantCell(vacant, 1, 1);
826 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) v.getLayoutParams();
827 lp.cellX = vacant[0];
828 lp.cellY = vacant[1];
829 ItemInfo info = (ItemInfo) v.getTag();
830 info.cellX = vacant[0];
831 info.cellY = vacant[1];
832 boolean insert = false;
833 mContent.addViewToCellLayout(v, insert ? 0 : -1, (int)info.id, lp, true);
Adam Cohen7c693212011-05-18 15:26:57 -0700834 LauncherModel.addOrMoveItemInDatabase(mLauncher, info, mInfo.id, 0,
835 info.cellX, info.cellY);
Adam Cohen2801caf2011-05-13 20:57:39 -0700836 }
Adam Cohen7c693212011-05-18 15:26:57 -0700837 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700838 }
839
Adam Cohena9cf38f2011-05-02 15:36:58 -0700840 public int getItemCount() {
841 return mContent.getChildrenLayout().getChildCount();
842 }
843
844 public View getItemAt(int index) {
845 return mContent.getChildrenLayout().getChildAt(index);
846 }
847
Adam Cohen2801caf2011-05-13 20:57:39 -0700848 private void onCloseComplete() {
Adam Cohen05e0f402011-08-01 12:12:49 -0700849 DragLayer parent = (DragLayer) getParent();
850 parent.removeView(Folder.this);
851 clearFocus();
852
Adam Cohen2801caf2011-05-13 20:57:39 -0700853 if (mRearrangeOnClose) {
854 setupContentForNumItems(getItemCount());
855 mRearrangeOnClose = false;
856 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700857 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700858 if (!mDragInProgress && !mSuppressFolderDeletion) {
859 replaceFolderWithFinalItem();
860 } else if (mDragInProgress) {
861 mDeleteFolderOnDropCompleted = true;
862 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700863 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700864 mSuppressFolderDeletion = false;
Adam Cohenafb01ee2011-06-23 15:38:03 -0700865 }
866
867 private void replaceFolderWithFinalItem() {
868 ItemInfo finalItem = null;
869
870 if (getItemCount() == 1) {
871 finalItem = mInfo.contents.get(0);
872 }
873
874 // Remove the folder completely
Winson Chung3d503fb2011-07-13 17:25:49 -0700875 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screen);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700876 cellLayout.removeView(mFolderIcon);
877 if (mFolderIcon instanceof DropTarget) {
878 mDragController.removeDropTarget((DropTarget) mFolderIcon);
879 }
880 mLauncher.removeFolder(mInfo);
881
882 if (finalItem != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700883 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
884 mInfo.screen, mInfo.cellX, mInfo.cellY);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700885 }
Adam Cohen716b51e2011-06-30 12:09:54 -0700886 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700887
888 // Add the last remaining child to the workspace in place of the folder
889 if (finalItem != null) {
890 View child = mLauncher.createShortcut(R.layout.application, cellLayout,
891 (ShortcutInfo) finalItem);
892
Winson Chung3d503fb2011-07-13 17:25:49 -0700893 mLauncher.getWorkspace().addInScreen(child, mInfo.container, mInfo.screen, mInfo.cellX,
894 mInfo.cellY, mInfo.spanX, mInfo.spanY);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700895 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700896 }
897
Adam Cohenbfbfd262011-06-13 16:55:12 -0700898 public void onDrop(DragObject d) {
899 ShortcutInfo item;
900 if (d.dragInfo instanceof ApplicationInfo) {
901 // Came from all apps -- make a copy
902 item = ((ApplicationInfo) d.dragInfo).makeShortcut();
903 item.spanX = 1;
904 item.spanY = 1;
905 } else {
906 item = (ShortcutInfo) d.dragInfo;
907 }
Adam Cohen05e0f402011-08-01 12:12:49 -0700908 // Dragged from self onto self, currently this is the only path possible, however
909 // we keep this as a distinct code path.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700910 if (item == mCurrentDragInfo) {
911 ShortcutInfo si = (ShortcutInfo) mCurrentDragView.getTag();
912 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) mCurrentDragView.getLayoutParams();
913 si.cellX = lp.cellX = mEmptyCell[0];
914 si.cellX = lp.cellY = mEmptyCell[1];
915 mContent.addViewToCellLayout(mCurrentDragView, -1, (int)item.id, lp, true);
Adam Cohenfc53cd22011-07-20 15:45:11 -0700916 if (d.dragView.hasDrawn()) {
917 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, mCurrentDragView);
918 } else {
919 mCurrentDragView.setVisibility(VISIBLE);
920 }
Adam Cohene9166b22011-07-08 17:11:11 -0700921 mItemsInvalidated = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700922 setupContentDimension(getItemCount());
Adam Cohen716b51e2011-06-30 12:09:54 -0700923 mSuppressOnAdd = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700924 }
925 mInfo.add(item);
926 }
927
928 public void onAdd(ShortcutInfo item) {
929 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700930 // If the item was dropped onto this open folder, we have done the work associated
931 // with adding the item to the folder, as indicated by mSuppressOnAdd being set
Adam Cohenbfbfd262011-06-13 16:55:12 -0700932 if (mSuppressOnAdd) return;
933 if (!findAndSetEmptyCells(item)) {
934 // The current layout is full, can we expand it?
935 setupContentForNumItems(getItemCount() + 1);
936 findAndSetEmptyCells(item);
937 }
938 createAndAddShortcut(item);
939 LauncherModel.addOrMoveItemInDatabase(
940 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
941 }
942
Adam Cohena9cf38f2011-05-02 15:36:58 -0700943 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -0700944 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700945 // If this item is being dragged from this open folder, we have already handled
946 // the work associated with removing the item, so we don't have to do anything here.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700947 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -0700948 View v = getViewForInfo(item);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700949 mContent.removeView(v);
Adam Cohen2801caf2011-05-13 20:57:39 -0700950 if (mState == STATE_ANIMATING) {
951 mRearrangeOnClose = true;
952 } else {
953 setupContentForNumItems(getItemCount());
954 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700955 if (getItemCount() <= 1) {
956 replaceFolderWithFinalItem();
957 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700958 }
Adam Cohen7c693212011-05-18 15:26:57 -0700959
Adam Cohendf1e4e82011-06-24 15:57:39 -0700960 private View getViewForInfo(ShortcutInfo item) {
961 for (int j = 0; j < mContent.getCountY(); j++) {
962 for (int i = 0; i < mContent.getCountX(); i++) {
963 View v = mContent.getChildAt(i, j);
964 if (v.getTag() == item) {
965 return v;
966 }
967 }
968 }
969 return null;
970 }
971
Adam Cohen76078c42011-06-09 15:06:52 -0700972 public void onItemsChanged() {
973 }
Adam Cohen76fc0852011-06-17 13:26:23 -0700974 public void onTitleChanged(CharSequence title) {
975 }
Adam Cohen76078c42011-06-09 15:06:52 -0700976
Adam Cohen7c693212011-05-18 15:26:57 -0700977 public ArrayList<View> getItemsInReadingOrder() {
Adam Cohen76078c42011-06-09 15:06:52 -0700978 return getItemsInReadingOrder(true);
979 }
980
981 public ArrayList<View> getItemsInReadingOrder(boolean includeCurrentDragItem) {
Adam Cohen7c693212011-05-18 15:26:57 -0700982 if (mItemsInvalidated) {
983 mItemsInReadingOrder.clear();
984 for (int j = 0; j < mContent.getCountY(); j++) {
985 for (int i = 0; i < mContent.getCountX(); i++) {
986 View v = mContent.getChildAt(i, j);
987 if (v != null) {
Adam Cohen76078c42011-06-09 15:06:52 -0700988 ShortcutInfo info = (ShortcutInfo) v.getTag();
989 if (info != mCurrentDragInfo || includeCurrentDragItem) {
990 mItemsInReadingOrder.add(v);
991 }
Adam Cohen7c693212011-05-18 15:26:57 -0700992 }
993 }
994 }
995 mItemsInvalidated = false;
996 }
997 return mItemsInReadingOrder;
998 }
Adam Cohen8dfcba42011-07-07 16:38:18 -0700999
1000 public void getLocationInDragLayer(int[] loc) {
1001 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
1002 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001003}