blob: aebfcb5334cfb0752412ae95a0f7b92cb70a977b [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;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.util.AttributeSet;
Adam Cohen76fc0852011-06-17 13:26:23 -070031import android.view.ActionMode;
32import android.view.KeyEvent;
Adam Cohendf2cc412011-04-27 16:56:57 -070033import android.view.LayoutInflater;
Adam Cohen76fc0852011-06-17 13:26:23 -070034import android.view.Menu;
35import android.view.MenuItem;
Adam Cohen0c872ba2011-05-05 10:34:16 -070036import android.view.MotionEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080037import android.view.View;
38import android.view.View.OnClickListener;
Adam Cohen2801caf2011-05-13 20:57:39 -070039import android.view.animation.AccelerateInterpolator;
40import android.view.animation.DecelerateInterpolator;
Adam Cohen76fc0852011-06-17 13:26:23 -070041import android.view.inputmethod.EditorInfo;
42import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.widget.AdapterView;
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 -080046import android.widget.AdapterView.OnItemClickListener;
47import android.widget.AdapterView.OnItemLongClickListener;
48
Romain Guyedcce092010-03-04 13:03:17 -080049import com.android.launcher.R;
Adam Cohena9cf38f2011-05-02 15:36:58 -070050import com.android.launcher2.FolderInfo.FolderListener;
Romain Guyedcce092010-03-04 13:03:17 -080051
Adam Cohenc0dcf592011-06-01 15:30:43 -070052import java.util.ArrayList;
53
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054/**
55 * Represents a set of icons chosen by the user or generated by the system.
56 */
57public class Folder extends LinearLayout implements DragSource, OnItemLongClickListener,
Adam Cohen76fc0852011-06-17 13:26:23 -070058 OnItemClickListener, OnClickListener, View.OnLongClickListener, DropTarget, FolderListener,
59 TextView.OnEditorActionListener {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060
Joe Onorato00acb122009-08-04 16:04:30 -040061 protected DragController mDragController;
Adam Cohendf2cc412011-04-27 16:56:57 -070062
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063 protected Launcher mLauncher;
64
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065 protected FolderInfo mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066
Adam Cohendf2cc412011-04-27 16:56:57 -070067 private static final String TAG = "Launcher.Folder";
68
69 static final int STATE_NONE = -1;
70 static final int STATE_SMALL = 0;
71 static final int STATE_ANIMATING = 1;
72 static final int STATE_OPEN = 2;
73
74 private int mExpandDuration;
75 protected CellLayout mContent;
76 private final LayoutInflater mInflater;
77 private final IconCache mIconCache;
78 private int mState = STATE_NONE;
Adam Cohen2801caf2011-05-13 20:57:39 -070079 private static final int FULL_GROW = 0;
80 private static final int PARTIAL_GROW = 1;
Adam Cohenbfbfd262011-06-13 16:55:12 -070081 private static final int REORDER_ANIMATION_DURATION = 230;
82 private static final int ON_EXIT_CLOSE_DELAY = 800;
Adam Cohen2801caf2011-05-13 20:57:39 -070083 private int mMode = PARTIAL_GROW;
84 private boolean mRearrangeOnClose = false;
85 private FolderIcon mFolderIcon;
86 private int mMaxCountX;
87 private int mMaxCountY;
88 private Rect mNewSize = new Rect();
Adam Cohen7c693212011-05-18 15:26:57 -070089 private ArrayList<View> mItemsInReadingOrder = new ArrayList<View>();
Adam Cohenbadf71e2011-05-26 19:08:29 -070090 private Drawable mIconDrawable;
Adam Cohen7c693212011-05-18 15:26:57 -070091 boolean mItemsInvalidated = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -070092 private ShortcutInfo mCurrentDragInfo;
93 private View mCurrentDragView;
94 boolean mSuppressOnAdd = false;
95 private int[] mTargetCell = new int[2];
96 private int[] mPreviousTargetCell = new int[2];
97 private int[] mEmptyCell = new int[2];
98 private Alarm mReorderAlarm = new Alarm();
99 private Alarm mOnExitAlarm = new Alarm();
Adam Cohen76fc0852011-06-17 13:26:23 -0700100 private TextView mFolderName;
101 private int mFolderNameHeight;
Adam Cohen4ef610f2011-06-21 22:37:36 -0700102 private Rect mHitRect = new Rect();
Adam Cohen8e776a62011-06-28 18:10:06 -0700103 private Rect mTempRect = new Rect();
Adam Cohen76fc0852011-06-17 13:26:23 -0700104
105 private boolean mIsEditingName = false;
106 private InputMethodManager mInputMethodManager;
Adam Cohendf2cc412011-04-27 16:56:57 -0700107
Adam Cohena65beee2011-06-27 21:32:23 -0700108 private static String sDefaultFolderName;
109 private static String sHintText;
110
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800111 /**
112 * Used to inflate the Workspace from XML.
113 *
114 * @param context The application's context.
115 * @param attrs The attribtues set containing the Workspace's customization values.
116 */
117 public Folder(Context context, AttributeSet attrs) {
118 super(context, attrs);
119 setAlwaysDrawnWithCacheEnabled(false);
Adam Cohendf2cc412011-04-27 16:56:57 -0700120 mInflater = LayoutInflater.from(context);
121 mIconCache = ((LauncherApplication)context.getApplicationContext()).getIconCache();
Adam Cohen8e776a62011-06-28 18:10:06 -0700122 mMaxCountX = LauncherModel.getCellCountX();
123 mMaxCountY = LauncherModel.getCellCountY();
Adam Cohen76fc0852011-06-17 13:26:23 -0700124
125 mInputMethodManager = (InputMethodManager)
126 mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
127
128 Resources res = getResources();
129 mExpandDuration = res.getInteger(R.integer.config_folderAnimDuration);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700130
131 if (sDefaultFolderName == null) {
132 sDefaultFolderName = res.getString(R.string.folder_name);
133 }
Adam Cohena65beee2011-06-27 21:32:23 -0700134 if (sHintText == null) {
135 sHintText = res.getString(R.string.folder_hint_text);
136 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137 }
138
139 @Override
140 protected void onFinishInflate() {
141 super.onFinishInflate();
Adam Cohendf2cc412011-04-27 16:56:57 -0700142 mContent = (CellLayout) findViewById(R.id.folder_content);
Adam Cohen2801caf2011-05-13 20:57:39 -0700143 mContent.setGridSize(0, 0);
144 mContent.enableHardwareLayers();
Adam Cohen76fc0852011-06-17 13:26:23 -0700145 mFolderName = (TextView) findViewById(R.id.folder_name);
146
147 // We find out how tall the text view wants to be (it is set to wrap_content), so that
148 // we can allocate the appropriate amount of space for it.
149 int measureSpec = MeasureSpec.UNSPECIFIED;
150 mFolderName.measure(measureSpec, measureSpec);
151 mFolderNameHeight = mFolderName.getMeasuredHeight();
152
153 // We disable action mode for now since it messes up the view on phones
154 mFolderName.setCustomSelectionActionModeCallback(mActionModeCallback);
155 mFolderName.setCursorVisible(false);
156 mFolderName.setOnEditorActionListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700157 mFolderName.setSelectAllOnFocus(true);
Adam Cohen7a14d0b2011-06-24 11:36:35 -0700158 mFolderName.setInputType(mFolderName.getInputType() |
159 InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700161
Adam Cohen76fc0852011-06-17 13:26:23 -0700162 private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
163 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
164 return false;
165 }
166
167 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
168 return false;
169 }
170
171 public void onDestroyActionMode(ActionMode mode) {
172 }
173
174 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
175 return false;
176 }
177 };
178
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179 public void onItemClick(AdapterView parent, View v, int position, long id) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800180 ShortcutInfo app = (ShortcutInfo) parent.getItemAtPosition(position);
Romain Guyfb5411e2010-02-24 10:04:17 -0800181 int[] pos = new int[2];
182 v.getLocationOnScreen(pos);
183 app.intent.setSourceBounds(new Rect(pos[0], pos[1],
184 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -0700185 mLauncher.startActivitySafely(app.intent, app);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186 }
187
188 public void onClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700189 Object tag = v.getTag();
190 if (tag instanceof ShortcutInfo) {
191 // refactor this code from Folder
192 ShortcutInfo item = (ShortcutInfo) tag;
193 int[] pos = new int[2];
194 v.getLocationOnScreen(pos);
195 item.intent.setSourceBounds(new Rect(pos[0], pos[1],
196 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
197 mLauncher.startActivitySafely(item.intent, item);
Adam Cohendf2cc412011-04-27 16:56:57 -0700198 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800199 }
200
Adam Cohen76fc0852011-06-17 13:26:23 -0700201 public boolean onInterceptTouchEvent(MotionEvent ev) {
202 if (ev.getAction() == MotionEvent.ACTION_DOWN) {
203 mFolderName.getHitRect(mHitRect);
204 if (mHitRect.contains((int) ev.getX(), (int) ev.getY()) && !mIsEditingName) {
205 startEditingFolderName();
206 }
207 }
208 return false;
209 }
210
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800211 public boolean onLongClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700212 Object tag = v.getTag();
213 if (tag instanceof ShortcutInfo) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700214 ShortcutInfo item = (ShortcutInfo) tag;
215 if (!v.isInTouchMode()) {
216 return false;
217 }
218
219 mLauncher.getWorkspace().onDragStartedWithItem(v);
220 mDragController.startDrag(v, this, item, DragController.DRAG_ACTION_COPY);
Adam Cohenbadf71e2011-05-26 19:08:29 -0700221 mIconDrawable = ((TextView) v).getCompoundDrawables()[1];
Adam Cohen76078c42011-06-09 15:06:52 -0700222
223 mCurrentDragInfo = item;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700224 mEmptyCell[0] = item.cellX;
225 mEmptyCell[1] = item.cellY;
226 mCurrentDragView = v;
Adam Cohen716b51e2011-06-30 12:09:54 -0700227
Adam Cohendf2cc412011-04-27 16:56:57 -0700228 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800229 return true;
230 }
231
Adam Cohen716b51e2011-06-30 12:09:54 -0700232 public void onDragViewVisible() {
233 mContent.removeView(mCurrentDragView);
234 mInfo.remove(mCurrentDragInfo);
235 }
236
Adam Cohen76fc0852011-06-17 13:26:23 -0700237 public boolean isEditingName() {
238 return mIsEditingName;
239 }
240
241 public void startEditingFolderName() {
Adam Cohena65beee2011-06-27 21:32:23 -0700242 mFolderName.setHint("");
Adam Cohen76fc0852011-06-17 13:26:23 -0700243 mFolderName.setCursorVisible(true);
244 mIsEditingName = true;
245 }
246
247 public void dismissEditingName() {
248 mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
249 doneEditingFolderName(true);
250 }
251
252 public void doneEditingFolderName(boolean commit) {
Adam Cohena65beee2011-06-27 21:32:23 -0700253 mFolderName.setHint(sHintText);
Adam Cohen76fc0852011-06-17 13:26:23 -0700254 mInfo.setTitle(mFolderName.getText());
255 LauncherModel.updateItemInDatabase(mLauncher, mInfo);
256 mFolderName.setCursorVisible(false);
257 mFolderName.clearFocus();
258 mIsEditingName = false;
259 }
260
261 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
262 if (actionId == EditorInfo.IME_ACTION_DONE) {
263 dismissEditingName();
264 return true;
265 }
266 return false;
267 }
268
269 public View getEditTextRegion() {
270 return mFolderName;
271 }
272
Adam Cohenbadf71e2011-05-26 19:08:29 -0700273 public Drawable getDragDrawable() {
274 return mIconDrawable;
275 }
276
Adam Cohen0c872ba2011-05-05 10:34:16 -0700277 /**
278 * We need to handle touch events to prevent them from falling through to the workspace below.
279 */
280 @Override
281 public boolean onTouchEvent(MotionEvent ev) {
282 return true;
283 }
284
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800285 public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
286 if (!view.isInTouchMode()) {
287 return false;
288 }
289
Joe Onorato0589f0f2010-02-08 13:44:00 -0800290 ShortcutInfo app = (ShortcutInfo) parent.getItemAtPosition(position);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800291
Joe Onorato00acb122009-08-04 16:04:30 -0400292 mDragController.startDrag(view, this, app, DragController.DRAG_ACTION_COPY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800293 mLauncher.closeFolder(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800294 return true;
295 }
296
Joe Onorato00acb122009-08-04 16:04:30 -0400297 public void setDragController(DragController dragController) {
298 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800299 }
300
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800301 void setLauncher(Launcher launcher) {
302 mLauncher = launcher;
303 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700304
305 void setFolderIcon(FolderIcon icon) {
306 mFolderIcon = icon;
307 }
308
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800309 /**
310 * @return the FolderInfo object associated with this folder
311 */
312 FolderInfo getInfo() {
313 return mInfo;
314 }
315
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800316 void onOpen() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700317 // When the folder opens, we need to refresh the GridView's selection by
318 // forcing a layout
319 // TODO: find out if this is still necessary
320 mContent.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800321 }
322
323 void onClose() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700324 DragLayer parent = (DragLayer) getParent();
325 parent.removeView(Folder.this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700326 clearFocus();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800327 }
328
329 void bind(FolderInfo info) {
330 mInfo = info;
Adam Cohendf2cc412011-04-27 16:56:57 -0700331 ArrayList<ShortcutInfo> children = info.contents;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700332 ArrayList<ShortcutInfo> overflow = new ArrayList<ShortcutInfo>();
Adam Cohen7c693212011-05-18 15:26:57 -0700333 setupContentForNumItems(children.size());
Adam Cohendf2cc412011-04-27 16:56:57 -0700334 for (int i = 0; i < children.size(); i++) {
335 ShortcutInfo child = (ShortcutInfo) children.get(i);
Adam Cohenc508b2d2011-06-28 14:41:44 -0700336 if (!createAndAddShortcut(child)) {
337 overflow.add(child);
338 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700339 }
Adam Cohenc508b2d2011-06-28 14:41:44 -0700340
341 // If our folder has too many items we prune them from the list. This is an issue
342 // when upgrading from the old Folders implementation which could contain an unlimited
343 // number of items.
344 for (ShortcutInfo item: overflow) {
345 mInfo.remove(item);
346 LauncherModel.deleteItemFromDatabase(mLauncher, item);
347 }
348
Adam Cohen4dbe6d92011-05-18 17:14:15 -0700349 mItemsInvalidated = true;
Adam Cohena9cf38f2011-05-02 15:36:58 -0700350 mInfo.addListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700351
Adam Cohenafb01ee2011-06-23 15:38:03 -0700352 if (!sDefaultFolderName.contentEquals(mInfo.title)) {
Adam Cohen4ef610f2011-06-21 22:37:36 -0700353 mFolderName.setText(mInfo.title);
354 } else {
355 mFolderName.setText("");
356 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700357 }
358
359 /**
360 * Creates a new UserFolder, inflated from R.layout.user_folder.
361 *
362 * @param context The application's context.
363 *
364 * @return A new UserFolder.
365 */
366 static Folder fromXml(Context context) {
367 return (Folder) LayoutInflater.from(context).inflate(R.layout.user_folder, null);
368 }
369
370 /**
371 * This method is intended to make the UserFolder to be visually identical in size and position
372 * to its associated FolderIcon. This allows for a seamless transition into the expanded state.
373 */
374 private void positionAndSizeAsIcon() {
375 if (!(getParent() instanceof CellLayoutChildren)) return;
376
Adam Cohen8e776a62011-06-28 18:10:06 -0700377 DragLayer.LayoutParams iconLp = (DragLayer.LayoutParams) mFolderIcon.getLayoutParams();
378 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohendf2cc412011-04-27 16:56:57 -0700379
Adam Cohen2801caf2011-05-13 20:57:39 -0700380 if (mMode == PARTIAL_GROW) {
381 setScaleX(0.8f);
382 setScaleY(0.8f);
383 setAlpha(0f);
384 } else {
385 lp.width = iconLp.width;
386 lp.height = iconLp.height;
387 lp.x = iconLp.x;
388 lp.y = iconLp.y;
389 mContent.setAlpha(0);
390 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700391 mState = STATE_SMALL;
392 }
393
394 public void animateOpen() {
395 if (mState != STATE_SMALL) {
396 positionAndSizeAsIcon();
397 }
Adam Cohen8e776a62011-06-28 18:10:06 -0700398 if (!(getParent() instanceof DragLayer)) return;
Adam Cohendf2cc412011-04-27 16:56:57 -0700399
Adam Cohen2801caf2011-05-13 20:57:39 -0700400 ObjectAnimator oa;
Adam Cohen8e776a62011-06-28 18:10:06 -0700401 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohendf2cc412011-04-27 16:56:57 -0700402
Adam Cohen2801caf2011-05-13 20:57:39 -0700403 centerAboutIcon();
404 if (mMode == PARTIAL_GROW) {
405 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1);
406 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
407 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
408 oa = ObjectAnimator.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
409 } else {
410 PropertyValuesHolder width = PropertyValuesHolder.ofInt("width", mNewSize.width());
411 PropertyValuesHolder height = PropertyValuesHolder.ofInt("height", mNewSize.height());
412 PropertyValuesHolder x = PropertyValuesHolder.ofInt("x", mNewSize.left);
413 PropertyValuesHolder y = PropertyValuesHolder.ofInt("y", mNewSize.top);
414 oa = ObjectAnimator.ofPropertyValuesHolder(lp, width, height, x, y);
415 oa.addUpdateListener(new AnimatorUpdateListener() {
416 public void onAnimationUpdate(ValueAnimator animation) {
417 requestLayout();
418 }
419 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700420
Adam Cohen2801caf2011-05-13 20:57:39 -0700421 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
422 ObjectAnimator alphaOa = ObjectAnimator.ofPropertyValuesHolder(mContent, alpha);
423 alphaOa.setDuration(mExpandDuration);
424 alphaOa.setInterpolator(new AccelerateInterpolator(2.0f));
425 alphaOa.start();
426 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700427
Adam Cohen2801caf2011-05-13 20:57:39 -0700428 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700429 @Override
430 public void onAnimationStart(Animator animation) {
431 mState = STATE_ANIMATING;
432 }
433 @Override
434 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700435 mState = STATE_OPEN;
Adam Cohendf2cc412011-04-27 16:56:57 -0700436 }
437 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700438 oa.setDuration(mExpandDuration);
439 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700440 }
441
442 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700443 if (!(getParent() instanceof DragLayer)) return;
Adam Cohendf2cc412011-04-27 16:56:57 -0700444
Adam Cohen2801caf2011-05-13 20:57:39 -0700445 ObjectAnimator oa;
Adam Cohen2801caf2011-05-13 20:57:39 -0700446 if (mMode == PARTIAL_GROW) {
447 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
448 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
449 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
450 oa = ObjectAnimator.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
451 } else {
Adam Cohen8e776a62011-06-28 18:10:06 -0700452 DragLayer.LayoutParams iconLp = (DragLayer.LayoutParams) mFolderIcon.getLayoutParams();
453 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohendf2cc412011-04-27 16:56:57 -0700454
Adam Cohen2801caf2011-05-13 20:57:39 -0700455 PropertyValuesHolder width = PropertyValuesHolder.ofInt("width", iconLp.width);
456 PropertyValuesHolder height = PropertyValuesHolder.ofInt("height", iconLp.height);
457 PropertyValuesHolder x = PropertyValuesHolder.ofInt("x",iconLp.x);
458 PropertyValuesHolder y = PropertyValuesHolder.ofInt("y", iconLp.y);
459 oa = ObjectAnimator.ofPropertyValuesHolder(lp, width, height, x, y);
460 oa.addUpdateListener(new AnimatorUpdateListener() {
461 public void onAnimationUpdate(ValueAnimator animation) {
462 requestLayout();
463 }
464 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700465
Adam Cohen2801caf2011-05-13 20:57:39 -0700466 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0f);
467 ObjectAnimator alphaOa = ObjectAnimator.ofPropertyValuesHolder(mContent, alpha);
468 alphaOa.setDuration(mExpandDuration);
469 alphaOa.setInterpolator(new DecelerateInterpolator(2.0f));
470 alphaOa.start();
471 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700472
Adam Cohen2801caf2011-05-13 20:57:39 -0700473 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700474 @Override
475 public void onAnimationEnd(Animator animation) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700476 onClose();
Adam Cohen2801caf2011-05-13 20:57:39 -0700477 onCloseComplete();
Adam Cohen2801caf2011-05-13 20:57:39 -0700478 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700479 }
480 @Override
481 public void onAnimationStart(Animator animation) {
482 mState = STATE_ANIMATING;
483 }
484 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700485 oa.setDuration(mExpandDuration);
486 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700487 }
488
489 void notifyDataSetChanged() {
490 // recreate all the children if the data set changes under us. We may want to do this more
491 // intelligently (ie just removing the views that should no longer exist)
492 mContent.removeAllViewsInLayout();
493 bind(mInfo);
494 }
495
Adam Cohencb3382b2011-05-24 14:07:08 -0700496 public boolean acceptDrop(DragObject d) {
497 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700498 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700499 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
500 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
501 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700502 }
503
Adam Cohendf2cc412011-04-27 16:56:57 -0700504 protected boolean findAndSetEmptyCells(ShortcutInfo item) {
505 int[] emptyCell = new int[2];
506 if (mContent.findCellForSpan(emptyCell, item.spanX, item.spanY)) {
507 item.cellX = emptyCell[0];
508 item.cellY = emptyCell[1];
Adam Cohendf2cc412011-04-27 16:56:57 -0700509 return true;
510 } else {
511 return false;
512 }
513 }
514
Adam Cohenc508b2d2011-06-28 14:41:44 -0700515 protected boolean createAndAddShortcut(ShortcutInfo item) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700516 final TextView textView =
Adam Cohene87b9242011-06-29 14:01:26 -0700517 (TextView) mInflater.inflate(R.layout.application, this, false);
Adam Cohendf2cc412011-04-27 16:56:57 -0700518 textView.setCompoundDrawablesWithIntrinsicBounds(null,
519 new FastBitmapDrawable(item.getIcon(mIconCache)), null, null);
520 textView.setText(item.title);
521 textView.setTag(item);
522
523 textView.setOnClickListener(this);
524 textView.setOnLongClickListener(this);
525
Adam Cohenc508b2d2011-06-28 14:41:44 -0700526 // We need to check here to verify that the given item's location isn't already occupied
527 // by another item. If it is, we need to find the next available slot and assign
528 // it that position. This is an issue when upgrading from the old Folders implementation
529 // which could contain an unlimited number of items.
530 if (mContent.getChildAt(item.cellX, item.cellY) != null) {
531 if (!findAndSetEmptyCells(item)) {
532 return false;
533 }
534 }
535
Adam Cohendf2cc412011-04-27 16:56:57 -0700536 CellLayout.LayoutParams lp =
537 new CellLayout.LayoutParams(item.cellX, item.cellY, item.spanX, item.spanY);
538 boolean insert = false;
539 mContent.addViewToCellLayout(textView, insert ? 0 : -1, (int)item.id, lp, true);
Adam Cohenc508b2d2011-06-28 14:41:44 -0700540 return true;
Adam Cohendf2cc412011-04-27 16:56:57 -0700541 }
542
Adam Cohencb3382b2011-05-24 14:07:08 -0700543 public void onDragEnter(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700544 mPreviousTargetCell[0] = -1;
545 mPreviousTargetCell[1] = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700546 mOnExitAlarm.cancelAlarm();
547 }
548
549 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
550 public void onAlarm(Alarm alarm) {
551 realTimeReorder(mEmptyCell, mTargetCell);
552 }
553 };
554
555 boolean readingOrderGreaterThan(int[] v1, int[] v2) {
556 if (v1[1] > v2[1] || (v1[1] == v2[1] && v1[0] > v2[0])) {
557 return true;
558 } else {
559 return false;
560 }
561 }
562
563 private void realTimeReorder(int[] empty, int[] target) {
564 boolean wrap;
565 int startX;
566 int endX;
567 int startY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700568 int delay = 0;
569 float delayAmount = 30;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700570 if (readingOrderGreaterThan(target, empty)) {
571 wrap = empty[0] >= mContent.getCountX() - 1;
572 startY = wrap ? empty[1] + 1 : empty[1];
573 for (int y = startY; y <= target[1]; y++) {
574 startX = y == empty[1] ? empty[0] + 1 : 0;
575 endX = y < target[1] ? mContent.getCountX() - 1 : target[0];
576 for (int x = startX; x <= endX; x++) {
577 View v = mContent.getChildAt(x,y);
578 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen76fc0852011-06-17 13:26:23 -0700579 REORDER_ANIMATION_DURATION, delay)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700580 empty[0] = x;
581 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700582 delay += delayAmount;
583 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700584 }
585 }
586 }
587 } else {
588 wrap = empty[0] == 0;
589 startY = wrap ? empty[1] - 1 : empty[1];
590 for (int y = startY; y >= target[1]; y--) {
591 startX = y == empty[1] ? empty[0] - 1 : mContent.getCountX() - 1;
592 endX = y > target[1] ? 0 : target[0];
593 for (int x = startX; x >= endX; x--) {
594 View v = mContent.getChildAt(x,y);
595 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen76fc0852011-06-17 13:26:23 -0700596 REORDER_ANIMATION_DURATION, delay)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700597 empty[0] = x;
598 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700599 delay += delayAmount;
600 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700601 }
602 }
603 }
604 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700605 }
606
Adam Cohencb3382b2011-05-24 14:07:08 -0700607 public void onDragOver(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700608 float[] r = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, d.dragView, null);
609 mTargetCell = mContent.findNearestArea((int) r[0], (int) r[1], 1, 1, mTargetCell);
610
611 if (mTargetCell[0] != mPreviousTargetCell[0] || mTargetCell[1] != mPreviousTargetCell[1]) {
612 mReorderAlarm.cancelAlarm();
613 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
614 mReorderAlarm.setAlarm(150);
615 mPreviousTargetCell[0] = mTargetCell[0];
616 mPreviousTargetCell[1] = mTargetCell[1];
617 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700618 }
619
Adam Cohenbfbfd262011-06-13 16:55:12 -0700620 // This is used to compute the visual center of the dragView. The idea is that
621 // the visual center represents the user's interpretation of where the item is, and hence
622 // is the appropriate point to use when determining drop location.
623 private float[] getDragViewVisualCenter(int x, int y, int xOffset, int yOffset,
624 DragView dragView, float[] recycle) {
625 float res[];
626 if (recycle == null) {
627 res = new float[2];
628 } else {
629 res = recycle;
630 }
631
632 // These represent the visual top and left of drag view if a dragRect was provided.
633 // If a dragRect was not provided, then they correspond to the actual view left and
634 // top, as the dragRect is in that case taken to be the entire dragView.
635 // R.dimen.dragViewOffsetY.
636 int left = x - xOffset;
637 int top = y - yOffset;
638
639 // In order to find the visual center, we shift by half the dragRect
640 res[0] = left + dragView.getDragRegion().width() / 2;
641 res[1] = top + dragView.getDragRegion().height() / 2;
642
643 return res;
644 }
645
646 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
647 public void onAlarm(Alarm alarm) {
Adam Cohen1d9af7d2011-06-22 15:26:58 -0700648 mLauncher.closeFolder();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700649 mCurrentDragInfo = null;
650 mCurrentDragView = null;
651 mSuppressOnAdd = false;
652 mRearrangeOnClose = true;
653 }
654 };
655
Adam Cohencb3382b2011-05-24 14:07:08 -0700656 public void onDragExit(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700657 // We only close the folder if this is a true drag exit, ie. not because a drop
658 // has occurred above the folder.
659 if (!d.dragComplete) {
660 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
661 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
662 }
663 mReorderAlarm.cancelAlarm();
Adam Cohen2801caf2011-05-13 20:57:39 -0700664 }
665
Adam Cohenc0dcf592011-06-01 15:30:43 -0700666 public void onDropCompleted(View target, DragObject d, boolean success) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700667 mCurrentDragInfo = null;
668 mCurrentDragView = null;
669 mSuppressOnAdd = false;
Adam Cohen76078c42011-06-09 15:06:52 -0700670 if (!success) {
671 if (d.dragView != null) {
672 if (target instanceof CellLayout) {
673 // TODO: we should animate the item back to the folder in this case
674 }
675 }
Adam Cohenbfbfd262011-06-13 16:55:12 -0700676 // TODO: if the drag fails, we need to re-add the item
Adam Cohen76078c42011-06-09 15:06:52 -0700677 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700678 }
679
680 public boolean isDropEnabled() {
681 return true;
682 }
683
Adam Cohencb3382b2011-05-24 14:07:08 -0700684 public DropTarget getDropTargetDelegate(DragObject d) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700685 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800686 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700687
Adam Cohen2801caf2011-05-13 20:57:39 -0700688 private void setupContentDimension(int count) {
689 ArrayList<View> list = getItemsInReadingOrder();
690
691 int countX = mContent.getCountX();
692 int countY = mContent.getCountY();
Adam Cohen7c693212011-05-18 15:26:57 -0700693 boolean done = false;
Adam Cohen2801caf2011-05-13 20:57:39 -0700694
Adam Cohen7c693212011-05-18 15:26:57 -0700695 while (!done) {
696 int oldCountX = countX;
697 int oldCountY = countY;
698 if (countX * countY < count) {
699 // Current grid is too small, expand it
700 if (countX <= countY && countX < mMaxCountX) {
701 countX++;
702 } else if (countY < mMaxCountY) {
703 countY++;
704 }
705 if (countY == 0) countY++;
706 } else if ((countY - 1) * countX >= count && countY >= countX) {
707 countY = Math.max(0, countY - 1);
708 } else if ((countX - 1) * countY >= count) {
709 countX = Math.max(0, countX - 1);
Adam Cohen2801caf2011-05-13 20:57:39 -0700710 }
Adam Cohen7c693212011-05-18 15:26:57 -0700711 done = countX == oldCountX && countY == oldCountY;
Adam Cohen2801caf2011-05-13 20:57:39 -0700712 }
Adam Cohen7c693212011-05-18 15:26:57 -0700713 mContent.setGridSize(countX, countY);
Adam Cohen2801caf2011-05-13 20:57:39 -0700714 arrangeChildren(list);
715 }
716
717 public boolean isFull() {
718 return getItemCount() >= mMaxCountX * mMaxCountY;
719 }
720
721 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700722 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700723
724 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohen76fc0852011-06-17 13:26:23 -0700725 // Technically there is no padding at the bottom, but we add space equal to the padding
726 // and have to account for that here.
727 int height = getPaddingTop() + mContent.getDesiredHeight() + mFolderNameHeight;
Adam Cohen8e776a62011-06-28 18:10:06 -0700728 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700729
Adam Cohen8e776a62011-06-28 18:10:06 -0700730 parent.getDescendantRectRelativeToSelf(mFolderIcon, mTempRect);
731
732 int centerX = mTempRect.centerX();
733 int centerY = mTempRect.centerY();
Adam Cohen2801caf2011-05-13 20:57:39 -0700734 int centeredLeft = centerX - width / 2;
735 int centeredTop = centerY - height / 2;
736
Adam Cohen2801caf2011-05-13 20:57:39 -0700737 int parentWidth = 0;
738 int parentHeight = 0;
Adam Cohen8e776a62011-06-28 18:10:06 -0700739 if (parent != null) {
740 parentWidth = parent.getMeasuredWidth();
741 parentHeight = parent.getMeasuredHeight();
Adam Cohen2801caf2011-05-13 20:57:39 -0700742 }
743
744 int left = Math.min(Math.max(0, centeredLeft), parentWidth - width);
745 int top = Math.min(Math.max(0, centeredTop), parentHeight - height);
746
747 int folderPivotX = width / 2 + (centeredLeft - left);
748 int folderPivotY = height / 2 + (centeredTop - top);
749 setPivotX(folderPivotX);
750 setPivotY(folderPivotY);
751 int folderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
752 (1.0f * folderPivotX / width));
753 int folderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
754 (1.0f * folderPivotY / height));
755 mFolderIcon.setPivotX(folderIconPivotX);
756 mFolderIcon.setPivotY(folderIconPivotY);
757
758 if (mMode == PARTIAL_GROW) {
759 lp.width = width;
760 lp.height = height;
761 lp.x = left;
762 lp.y = top;
763 } else {
764 mNewSize.set(left, top, left + width, top + height);
765 }
766 }
767
768 private void setupContentForNumItems(int count) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700769 setupContentDimension(count);
770
Adam Cohen8e776a62011-06-28 18:10:06 -0700771 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700772 if (lp == null) {
Adam Cohen8e776a62011-06-28 18:10:06 -0700773 lp = new DragLayer.LayoutParams(0, 0);
774 lp.customPosition = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700775 setLayoutParams(lp);
776 }
777 centerAboutIcon();
778 }
779
780 private void arrangeChildren(ArrayList<View> list) {
781 int[] vacant = new int[2];
782 if (list == null) {
783 list = getItemsInReadingOrder();
784 }
785 mContent.removeAllViews();
786
787 for (int i = 0; i < list.size(); i++) {
788 View v = list.get(i);
789 mContent.getVacantCell(vacant, 1, 1);
790 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) v.getLayoutParams();
791 lp.cellX = vacant[0];
792 lp.cellY = vacant[1];
793 ItemInfo info = (ItemInfo) v.getTag();
794 info.cellX = vacant[0];
795 info.cellY = vacant[1];
796 boolean insert = false;
797 mContent.addViewToCellLayout(v, insert ? 0 : -1, (int)info.id, lp, true);
Adam Cohen7c693212011-05-18 15:26:57 -0700798 LauncherModel.addOrMoveItemInDatabase(mLauncher, info, mInfo.id, 0,
799 info.cellX, info.cellY);
Adam Cohen2801caf2011-05-13 20:57:39 -0700800 }
Adam Cohen7c693212011-05-18 15:26:57 -0700801 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700802 }
803
Adam Cohena9cf38f2011-05-02 15:36:58 -0700804 public int getItemCount() {
805 return mContent.getChildrenLayout().getChildCount();
806 }
807
808 public View getItemAt(int index) {
809 return mContent.getChildrenLayout().getChildAt(index);
810 }
811
Adam Cohen2801caf2011-05-13 20:57:39 -0700812 private void onCloseComplete() {
813 if (mRearrangeOnClose) {
814 setupContentForNumItems(getItemCount());
815 mRearrangeOnClose = false;
816 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700817 if (getItemCount() <= 1) {
818 replaceFolderWithFinalItem();
819 }
820 }
821
822 private void replaceFolderWithFinalItem() {
823 ItemInfo finalItem = null;
824
825 if (getItemCount() == 1) {
826 finalItem = mInfo.contents.get(0);
827 }
828
829 // Remove the folder completely
830 final CellLayout cellLayout = (CellLayout)
831 mLauncher.getWorkspace().getChildAt(mInfo.screen);
832 cellLayout.removeView(mFolderIcon);
833 if (mFolderIcon instanceof DropTarget) {
834 mDragController.removeDropTarget((DropTarget) mFolderIcon);
835 }
836 mLauncher.removeFolder(mInfo);
837
838 if (finalItem != null) {
839 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem,
840 LauncherSettings.Favorites.CONTAINER_DESKTOP, mInfo.screen,
841 mInfo.cellX, mInfo.cellY);
842 }
Adam Cohen716b51e2011-06-30 12:09:54 -0700843 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700844
845 // Add the last remaining child to the workspace in place of the folder
846 if (finalItem != null) {
847 View child = mLauncher.createShortcut(R.layout.application, cellLayout,
848 (ShortcutInfo) finalItem);
849
850 mLauncher.getWorkspace().addInScreen(child, mInfo.screen, mInfo.cellX, mInfo.cellY,
851 mInfo.spanX, mInfo.spanY);
852 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700853 }
854
Adam Cohenbfbfd262011-06-13 16:55:12 -0700855 public void onDrop(DragObject d) {
856 ShortcutInfo item;
857 if (d.dragInfo instanceof ApplicationInfo) {
858 // Came from all apps -- make a copy
859 item = ((ApplicationInfo) d.dragInfo).makeShortcut();
860 item.spanX = 1;
861 item.spanY = 1;
862 } else {
863 item = (ShortcutInfo) d.dragInfo;
864 }
865 // Dragged from self onto self
866 if (item == mCurrentDragInfo) {
867 ShortcutInfo si = (ShortcutInfo) mCurrentDragView.getTag();
868 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) mCurrentDragView.getLayoutParams();
869 si.cellX = lp.cellX = mEmptyCell[0];
870 si.cellX = lp.cellY = mEmptyCell[1];
871 mContent.addViewToCellLayout(mCurrentDragView, -1, (int)item.id, lp, true);
Adam Cohen716b51e2011-06-30 12:09:54 -0700872 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, mCurrentDragView);
Adam Cohenbfbfd262011-06-13 16:55:12 -0700873 setupContentDimension(getItemCount());
Adam Cohen716b51e2011-06-30 12:09:54 -0700874 mSuppressOnAdd = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700875 }
876 mInfo.add(item);
877 }
878
879 public void onAdd(ShortcutInfo item) {
880 mItemsInvalidated = true;
881 if (mSuppressOnAdd) return;
882 if (!findAndSetEmptyCells(item)) {
883 // The current layout is full, can we expand it?
884 setupContentForNumItems(getItemCount() + 1);
885 findAndSetEmptyCells(item);
886 }
887 createAndAddShortcut(item);
888 LauncherModel.addOrMoveItemInDatabase(
889 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
890 }
891
Adam Cohena9cf38f2011-05-02 15:36:58 -0700892 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -0700893 mItemsInvalidated = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700894 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -0700895 View v = getViewForInfo(item);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700896 mContent.removeView(v);
Adam Cohen2801caf2011-05-13 20:57:39 -0700897 if (mState == STATE_ANIMATING) {
898 mRearrangeOnClose = true;
899 } else {
900 setupContentForNumItems(getItemCount());
901 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700902 if (getItemCount() <= 1) {
903 replaceFolderWithFinalItem();
904 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700905 }
Adam Cohen7c693212011-05-18 15:26:57 -0700906
Adam Cohendf1e4e82011-06-24 15:57:39 -0700907 private View getViewForInfo(ShortcutInfo item) {
908 for (int j = 0; j < mContent.getCountY(); j++) {
909 for (int i = 0; i < mContent.getCountX(); i++) {
910 View v = mContent.getChildAt(i, j);
911 if (v.getTag() == item) {
912 return v;
913 }
914 }
915 }
916 return null;
917 }
918
Adam Cohen76078c42011-06-09 15:06:52 -0700919 public void onItemsChanged() {
920 }
Adam Cohen76fc0852011-06-17 13:26:23 -0700921 public void onTitleChanged(CharSequence title) {
922 }
Adam Cohen76078c42011-06-09 15:06:52 -0700923
Adam Cohen7c693212011-05-18 15:26:57 -0700924 public ArrayList<View> getItemsInReadingOrder() {
Adam Cohen76078c42011-06-09 15:06:52 -0700925 return getItemsInReadingOrder(true);
926 }
927
928 public ArrayList<View> getItemsInReadingOrder(boolean includeCurrentDragItem) {
Adam Cohen7c693212011-05-18 15:26:57 -0700929 if (mItemsInvalidated) {
930 mItemsInReadingOrder.clear();
931 for (int j = 0; j < mContent.getCountY(); j++) {
932 for (int i = 0; i < mContent.getCountX(); i++) {
933 View v = mContent.getChildAt(i, j);
934 if (v != null) {
Adam Cohen76078c42011-06-09 15:06:52 -0700935 ShortcutInfo info = (ShortcutInfo) v.getTag();
936 if (info != mCurrentDragInfo || includeCurrentDragItem) {
937 mItemsInReadingOrder.add(v);
938 }
Adam Cohen7c693212011-05-18 15:26:57 -0700939 }
940 }
941 }
942 mItemsInvalidated = false;
943 }
944 return mItemsInReadingOrder;
945 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800946}