blob: 78df80fb322fbf82fa567557a50c823730f6fa76 [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;
Adam Cohen2801caf2011-05-13 20:57:39 -070038import android.view.animation.AccelerateInterpolator;
39import android.view.animation.DecelerateInterpolator;
Adam Cohen76fc0852011-06-17 13:26:23 -070040import android.view.inputmethod.EditorInfo;
41import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.widget.AdapterView;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.widget.LinearLayout;
Adam Cohendf2cc412011-04-27 16:56:57 -070044import android.widget.TextView;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045
Romain Guyedcce092010-03-04 13:03:17 -080046import com.android.launcher.R;
Adam Cohena9cf38f2011-05-02 15:36:58 -070047import com.android.launcher2.FolderInfo.FolderListener;
Romain Guyedcce092010-03-04 13:03:17 -080048
Adam Cohenc0dcf592011-06-01 15:30:43 -070049import java.util.ArrayList;
50
The Android Open Source Project31dd5032009-03-03 19:32:27 -080051/**
52 * Represents a set of icons chosen by the user or generated by the system.
53 */
Adam Cohen8dfcba42011-07-07 16:38:18 -070054public class Folder extends LinearLayout implements DragSource, View.OnClickListener,
55 View.OnLongClickListener, DropTarget, FolderListener, TextView.OnEditorActionListener {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080056
Adam Cohendf2cc412011-04-27 16:56:57 -070057 private static final String TAG = "Launcher.Folder";
58
Adam Cohen4eac29a2011-07-11 17:53:37 -070059 protected DragController mDragController;
60 protected Launcher mLauncher;
61 protected FolderInfo mInfo;
62
Adam Cohendf2cc412011-04-27 16:56:57 -070063 static final int STATE_NONE = -1;
64 static final int STATE_SMALL = 0;
65 static final int STATE_ANIMATING = 1;
66 static final int STATE_OPEN = 2;
67
68 private int mExpandDuration;
69 protected CellLayout mContent;
70 private final LayoutInflater mInflater;
71 private final IconCache mIconCache;
72 private int mState = STATE_NONE;
Adam Cohen2801caf2011-05-13 20:57:39 -070073 private static final int FULL_GROW = 0;
74 private static final int PARTIAL_GROW = 1;
Adam Cohenbfbfd262011-06-13 16:55:12 -070075 private static final int REORDER_ANIMATION_DURATION = 230;
76 private static final int ON_EXIT_CLOSE_DELAY = 800;
Adam Cohen2801caf2011-05-13 20:57:39 -070077 private int mMode = PARTIAL_GROW;
78 private boolean mRearrangeOnClose = false;
79 private FolderIcon mFolderIcon;
80 private int mMaxCountX;
81 private int mMaxCountY;
82 private Rect mNewSize = new Rect();
Adam Cohen3e8f8112011-07-02 18:03:00 -070083 private Rect mIconRect = new Rect();
Adam Cohen7c693212011-05-18 15:26:57 -070084 private ArrayList<View> mItemsInReadingOrder = new ArrayList<View>();
Adam Cohenbadf71e2011-05-26 19:08:29 -070085 private Drawable mIconDrawable;
Adam Cohen7c693212011-05-18 15:26:57 -070086 boolean mItemsInvalidated = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -070087 private ShortcutInfo mCurrentDragInfo;
88 private View mCurrentDragView;
89 boolean mSuppressOnAdd = false;
90 private int[] mTargetCell = new int[2];
91 private int[] mPreviousTargetCell = new int[2];
92 private int[] mEmptyCell = new int[2];
93 private Alarm mReorderAlarm = new Alarm();
94 private Alarm mOnExitAlarm = new Alarm();
Adam Cohen76fc0852011-06-17 13:26:23 -070095 private TextView mFolderName;
96 private int mFolderNameHeight;
Adam Cohen4ef610f2011-06-21 22:37:36 -070097 private Rect mHitRect = new Rect();
Adam Cohen8e776a62011-06-28 18:10:06 -070098 private Rect mTempRect = new Rect();
Adam Cohen8dfcba42011-07-07 16:38:18 -070099 private boolean mFirstOpen = true;
Adam Cohen76fc0852011-06-17 13:26:23 -0700100
101 private boolean mIsEditingName = false;
102 private InputMethodManager mInputMethodManager;
Adam Cohendf2cc412011-04-27 16:56:57 -0700103
Adam Cohena65beee2011-06-27 21:32:23 -0700104 private static String sDefaultFolderName;
105 private static String sHintText;
106
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800107 /**
108 * Used to inflate the Workspace from XML.
109 *
110 * @param context The application's context.
111 * @param attrs The attribtues set containing the Workspace's customization values.
112 */
113 public Folder(Context context, AttributeSet attrs) {
114 super(context, attrs);
115 setAlwaysDrawnWithCacheEnabled(false);
Adam Cohendf2cc412011-04-27 16:56:57 -0700116 mInflater = LayoutInflater.from(context);
117 mIconCache = ((LauncherApplication)context.getApplicationContext()).getIconCache();
Adam Cohen8e776a62011-06-28 18:10:06 -0700118 mMaxCountX = LauncherModel.getCellCountX();
119 mMaxCountY = LauncherModel.getCellCountY();
Adam Cohen76fc0852011-06-17 13:26:23 -0700120
121 mInputMethodManager = (InputMethodManager)
122 mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
123
124 Resources res = getResources();
125 mExpandDuration = res.getInteger(R.integer.config_folderAnimDuration);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700126
127 if (sDefaultFolderName == null) {
128 sDefaultFolderName = res.getString(R.string.folder_name);
129 }
Adam Cohena65beee2011-06-27 21:32:23 -0700130 if (sHintText == null) {
131 sHintText = res.getString(R.string.folder_hint_text);
132 }
Adam Cohen4eac29a2011-07-11 17:53:37 -0700133
134 mLauncher = (Launcher) context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135 }
136
137 @Override
138 protected void onFinishInflate() {
139 super.onFinishInflate();
Adam Cohendf2cc412011-04-27 16:56:57 -0700140 mContent = (CellLayout) findViewById(R.id.folder_content);
Adam Cohen2801caf2011-05-13 20:57:39 -0700141 mContent.setGridSize(0, 0);
Adam Cohen76fc0852011-06-17 13:26:23 -0700142 mFolderName = (TextView) findViewById(R.id.folder_name);
143
144 // We find out how tall the text view wants to be (it is set to wrap_content), so that
145 // we can allocate the appropriate amount of space for it.
146 int measureSpec = MeasureSpec.UNSPECIFIED;
147 mFolderName.measure(measureSpec, measureSpec);
148 mFolderNameHeight = mFolderName.getMeasuredHeight();
149
150 // We disable action mode for now since it messes up the view on phones
151 mFolderName.setCustomSelectionActionModeCallback(mActionModeCallback);
152 mFolderName.setCursorVisible(false);
153 mFolderName.setOnEditorActionListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700154 mFolderName.setSelectAllOnFocus(true);
Adam Cohen7a14d0b2011-06-24 11:36:35 -0700155 mFolderName.setInputType(mFolderName.getInputType() |
156 InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700158
Adam Cohen76fc0852011-06-17 13:26:23 -0700159 private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
160 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
161 return false;
162 }
163
164 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
165 return false;
166 }
167
168 public void onDestroyActionMode(ActionMode mode) {
169 }
170
171 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
172 return false;
173 }
174 };
175
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176 public void onClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700177 Object tag = v.getTag();
178 if (tag instanceof ShortcutInfo) {
179 // refactor this code from Folder
180 ShortcutInfo item = (ShortcutInfo) tag;
181 int[] pos = new int[2];
182 v.getLocationOnScreen(pos);
183 item.intent.setSourceBounds(new Rect(pos[0], pos[1],
184 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
185 mLauncher.startActivitySafely(item.intent, item);
Adam Cohendf2cc412011-04-27 16:56:57 -0700186 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187 }
188
Adam Cohen76fc0852011-06-17 13:26:23 -0700189 public boolean onInterceptTouchEvent(MotionEvent ev) {
190 if (ev.getAction() == MotionEvent.ACTION_DOWN) {
191 mFolderName.getHitRect(mHitRect);
192 if (mHitRect.contains((int) ev.getX(), (int) ev.getY()) && !mIsEditingName) {
193 startEditingFolderName();
194 }
195 }
196 return false;
197 }
198
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800199 public boolean onLongClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700200 Object tag = v.getTag();
201 if (tag instanceof ShortcutInfo) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700202 ShortcutInfo item = (ShortcutInfo) tag;
203 if (!v.isInTouchMode()) {
204 return false;
205 }
206
207 mLauncher.getWorkspace().onDragStartedWithItem(v);
208 mDragController.startDrag(v, this, item, DragController.DRAG_ACTION_COPY);
Adam Cohenbadf71e2011-05-26 19:08:29 -0700209 mIconDrawable = ((TextView) v).getCompoundDrawables()[1];
Adam Cohen76078c42011-06-09 15:06:52 -0700210
211 mCurrentDragInfo = item;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700212 mEmptyCell[0] = item.cellX;
213 mEmptyCell[1] = item.cellY;
214 mCurrentDragView = v;
Adam Cohen716b51e2011-06-30 12:09:54 -0700215
Adam Cohendf2cc412011-04-27 16:56:57 -0700216 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800217 return true;
218 }
219
Adam Cohen716b51e2011-06-30 12:09:54 -0700220 public void onDragViewVisible() {
221 mContent.removeView(mCurrentDragView);
222 mInfo.remove(mCurrentDragInfo);
223 }
224
Adam Cohen76fc0852011-06-17 13:26:23 -0700225 public boolean isEditingName() {
226 return mIsEditingName;
227 }
228
229 public void startEditingFolderName() {
Adam Cohena65beee2011-06-27 21:32:23 -0700230 mFolderName.setHint("");
Adam Cohen76fc0852011-06-17 13:26:23 -0700231 mFolderName.setCursorVisible(true);
232 mIsEditingName = true;
233 }
234
235 public void dismissEditingName() {
236 mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
237 doneEditingFolderName(true);
238 }
239
240 public void doneEditingFolderName(boolean commit) {
Adam Cohena65beee2011-06-27 21:32:23 -0700241 mFolderName.setHint(sHintText);
Adam Cohen76fc0852011-06-17 13:26:23 -0700242 mInfo.setTitle(mFolderName.getText());
243 LauncherModel.updateItemInDatabase(mLauncher, mInfo);
244 mFolderName.setCursorVisible(false);
245 mFolderName.clearFocus();
246 mIsEditingName = false;
247 }
248
249 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
250 if (actionId == EditorInfo.IME_ACTION_DONE) {
251 dismissEditingName();
252 return true;
253 }
254 return false;
255 }
256
257 public View getEditTextRegion() {
258 return mFolderName;
259 }
260
Adam Cohenbadf71e2011-05-26 19:08:29 -0700261 public Drawable getDragDrawable() {
262 return mIconDrawable;
263 }
264
Adam Cohen0c872ba2011-05-05 10:34:16 -0700265 /**
266 * We need to handle touch events to prevent them from falling through to the workspace below.
267 */
268 @Override
269 public boolean onTouchEvent(MotionEvent ev) {
270 return true;
271 }
272
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800273 public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
274 if (!view.isInTouchMode()) {
275 return false;
276 }
277
Joe Onorato0589f0f2010-02-08 13:44:00 -0800278 ShortcutInfo app = (ShortcutInfo) parent.getItemAtPosition(position);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800279
Joe Onorato00acb122009-08-04 16:04:30 -0400280 mDragController.startDrag(view, this, app, DragController.DRAG_ACTION_COPY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800281 mLauncher.closeFolder(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800282 return true;
283 }
284
Joe Onorato00acb122009-08-04 16:04:30 -0400285 public void setDragController(DragController dragController) {
286 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800287 }
288
Adam Cohen2801caf2011-05-13 20:57:39 -0700289 void setFolderIcon(FolderIcon icon) {
290 mFolderIcon = icon;
291 }
292
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800293 /**
294 * @return the FolderInfo object associated with this folder
295 */
296 FolderInfo getInfo() {
297 return mInfo;
298 }
299
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800300 void onOpen() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700301 // When the folder opens, we need to refresh the GridView's selection by
302 // forcing a layout
303 // TODO: find out if this is still necessary
304 mContent.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800305 }
306
307 void onClose() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700308 DragLayer parent = (DragLayer) getParent();
309 parent.removeView(Folder.this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700310 clearFocus();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800311 }
312
313 void bind(FolderInfo info) {
314 mInfo = info;
Adam Cohendf2cc412011-04-27 16:56:57 -0700315 ArrayList<ShortcutInfo> children = info.contents;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700316 ArrayList<ShortcutInfo> overflow = new ArrayList<ShortcutInfo>();
Adam Cohen7c693212011-05-18 15:26:57 -0700317 setupContentForNumItems(children.size());
Adam Cohendf2cc412011-04-27 16:56:57 -0700318 for (int i = 0; i < children.size(); i++) {
319 ShortcutInfo child = (ShortcutInfo) children.get(i);
Adam Cohenc508b2d2011-06-28 14:41:44 -0700320 if (!createAndAddShortcut(child)) {
321 overflow.add(child);
322 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700323 }
Adam Cohenc508b2d2011-06-28 14:41:44 -0700324
325 // If our folder has too many items we prune them from the list. This is an issue
326 // when upgrading from the old Folders implementation which could contain an unlimited
327 // number of items.
328 for (ShortcutInfo item: overflow) {
329 mInfo.remove(item);
330 LauncherModel.deleteItemFromDatabase(mLauncher, item);
331 }
332
Adam Cohen4dbe6d92011-05-18 17:14:15 -0700333 mItemsInvalidated = true;
Adam Cohena9cf38f2011-05-02 15:36:58 -0700334 mInfo.addListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700335
Adam Cohenafb01ee2011-06-23 15:38:03 -0700336 if (!sDefaultFolderName.contentEquals(mInfo.title)) {
Adam Cohen4ef610f2011-06-21 22:37:36 -0700337 mFolderName.setText(mInfo.title);
338 } else {
339 mFolderName.setText("");
340 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700341 }
342
343 /**
344 * Creates a new UserFolder, inflated from R.layout.user_folder.
345 *
346 * @param context The application's context.
347 *
348 * @return A new UserFolder.
349 */
350 static Folder fromXml(Context context) {
351 return (Folder) LayoutInflater.from(context).inflate(R.layout.user_folder, null);
352 }
353
354 /**
355 * This method is intended to make the UserFolder to be visually identical in size and position
356 * to its associated FolderIcon. This allows for a seamless transition into the expanded state.
357 */
358 private void positionAndSizeAsIcon() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700359 if (!(getParent() instanceof DragLayer)) return;
Adam Cohendf2cc412011-04-27 16:56:57 -0700360
Adam Cohen8e776a62011-06-28 18:10:06 -0700361 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohendf2cc412011-04-27 16:56:57 -0700362
Adam Cohen2801caf2011-05-13 20:57:39 -0700363 if (mMode == PARTIAL_GROW) {
364 setScaleX(0.8f);
365 setScaleY(0.8f);
366 setAlpha(0f);
367 } else {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700368 mLauncher.getDragLayer().getDescendantRectRelativeToSelf(mFolderIcon, mIconRect);
369 lp.width = mIconRect.width();
370 lp.height = mIconRect.height();
371 lp.x = mIconRect.left;
372 lp.y = mIconRect.top;
Adam Cohen2801caf2011-05-13 20:57:39 -0700373 mContent.setAlpha(0);
374 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700375 mState = STATE_SMALL;
376 }
377
378 public void animateOpen() {
Adam Cohen8dfcba42011-07-07 16:38:18 -0700379 if (mFirstOpen) {
380 setLayerType(LAYER_TYPE_HARDWARE, null);
381 buildLayer();
382 mFirstOpen = false;
383 }
384
Adam Cohen3e8f8112011-07-02 18:03:00 -0700385 positionAndSizeAsIcon();
386
Adam Cohen8e776a62011-06-28 18:10:06 -0700387 if (!(getParent() instanceof DragLayer)) return;
Adam Cohendf2cc412011-04-27 16:56:57 -0700388
Adam Cohen2801caf2011-05-13 20:57:39 -0700389 ObjectAnimator oa;
Adam Cohen8e776a62011-06-28 18:10:06 -0700390 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohendf2cc412011-04-27 16:56:57 -0700391
Adam Cohen2801caf2011-05-13 20:57:39 -0700392 centerAboutIcon();
393 if (mMode == PARTIAL_GROW) {
394 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1);
395 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
396 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
397 oa = ObjectAnimator.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
398 } else {
399 PropertyValuesHolder width = PropertyValuesHolder.ofInt("width", mNewSize.width());
400 PropertyValuesHolder height = PropertyValuesHolder.ofInt("height", mNewSize.height());
401 PropertyValuesHolder x = PropertyValuesHolder.ofInt("x", mNewSize.left);
402 PropertyValuesHolder y = PropertyValuesHolder.ofInt("y", mNewSize.top);
403 oa = ObjectAnimator.ofPropertyValuesHolder(lp, width, height, x, y);
404 oa.addUpdateListener(new AnimatorUpdateListener() {
405 public void onAnimationUpdate(ValueAnimator animation) {
406 requestLayout();
407 }
408 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700409
Adam Cohen2801caf2011-05-13 20:57:39 -0700410 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
411 ObjectAnimator alphaOa = ObjectAnimator.ofPropertyValuesHolder(mContent, alpha);
412 alphaOa.setDuration(mExpandDuration);
413 alphaOa.setInterpolator(new AccelerateInterpolator(2.0f));
414 alphaOa.start();
415 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700416
Adam Cohen2801caf2011-05-13 20:57:39 -0700417 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700418 @Override
419 public void onAnimationStart(Animator animation) {
420 mState = STATE_ANIMATING;
421 }
422 @Override
423 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700424 mState = STATE_OPEN;
Adam Cohen8dfcba42011-07-07 16:38:18 -0700425 setLayerType(LAYER_TYPE_NONE, null);
426 enableHardwareLayersForChildren();
Adam Cohendf2cc412011-04-27 16:56:57 -0700427 }
428 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700429 oa.setDuration(mExpandDuration);
430 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700431 }
432
Adam Cohen8dfcba42011-07-07 16:38:18 -0700433 void enableHardwareLayersForChildren() {
434 ArrayList<View> children = getItemsInReadingOrder();
435 for (View child: children) {
436 child.setLayerType(LAYER_TYPE_HARDWARE, null);
437 }
438 }
439
Adam Cohendf2cc412011-04-27 16:56:57 -0700440 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700441 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen8dfcba42011-07-07 16:38:18 -0700442 setLayerType(LAYER_TYPE_HARDWARE, null);
443 buildLayer();
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 lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohendf2cc412011-04-27 16:56:57 -0700453
Adam Cohen3e8f8112011-07-02 18:03:00 -0700454 PropertyValuesHolder width = PropertyValuesHolder.ofInt("width", mIconRect.width());
455 PropertyValuesHolder height = PropertyValuesHolder.ofInt("height", mIconRect.height());
456 PropertyValuesHolder x = PropertyValuesHolder.ofInt("x", mIconRect.left);
457 PropertyValuesHolder y = PropertyValuesHolder.ofInt("y", mIconRect.top);
Adam Cohen2801caf2011-05-13 20:57:39 -0700458 oa = ObjectAnimator.ofPropertyValuesHolder(lp, width, height, x, y);
459 oa.addUpdateListener(new AnimatorUpdateListener() {
460 public void onAnimationUpdate(ValueAnimator animation) {
461 requestLayout();
462 }
463 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700464
Adam Cohen2801caf2011-05-13 20:57:39 -0700465 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0f);
466 ObjectAnimator alphaOa = ObjectAnimator.ofPropertyValuesHolder(mContent, alpha);
467 alphaOa.setDuration(mExpandDuration);
468 alphaOa.setInterpolator(new DecelerateInterpolator(2.0f));
469 alphaOa.start();
470 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700471
Adam Cohen2801caf2011-05-13 20:57:39 -0700472 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700473 @Override
474 public void onAnimationEnd(Animator animation) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700475 onClose();
Adam Cohen2801caf2011-05-13 20:57:39 -0700476 onCloseComplete();
Adam Cohen2801caf2011-05-13 20:57:39 -0700477 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700478 }
479 @Override
480 public void onAnimationStart(Animator animation) {
481 mState = STATE_ANIMATING;
482 }
483 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700484 oa.setDuration(mExpandDuration);
485 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700486 }
487
488 void notifyDataSetChanged() {
489 // recreate all the children if the data set changes under us. We may want to do this more
490 // intelligently (ie just removing the views that should no longer exist)
491 mContent.removeAllViewsInLayout();
492 bind(mInfo);
493 }
494
Adam Cohencb3382b2011-05-24 14:07:08 -0700495 public boolean acceptDrop(DragObject d) {
496 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700497 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700498 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
499 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
500 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700501 }
502
Adam Cohendf2cc412011-04-27 16:56:57 -0700503 protected boolean findAndSetEmptyCells(ShortcutInfo item) {
504 int[] emptyCell = new int[2];
505 if (mContent.findCellForSpan(emptyCell, item.spanX, item.spanY)) {
506 item.cellX = emptyCell[0];
507 item.cellY = emptyCell[1];
Adam Cohendf2cc412011-04-27 16:56:57 -0700508 return true;
509 } else {
510 return false;
511 }
512 }
513
Adam Cohenc508b2d2011-06-28 14:41:44 -0700514 protected boolean createAndAddShortcut(ShortcutInfo item) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700515 final TextView textView =
Adam Cohene87b9242011-06-29 14:01:26 -0700516 (TextView) mInflater.inflate(R.layout.application, this, false);
Adam Cohendf2cc412011-04-27 16:56:57 -0700517 textView.setCompoundDrawablesWithIntrinsicBounds(null,
518 new FastBitmapDrawable(item.getIcon(mIconCache)), null, null);
519 textView.setText(item.title);
520 textView.setTag(item);
521
522 textView.setOnClickListener(this);
523 textView.setOnLongClickListener(this);
524
Adam Cohenc508b2d2011-06-28 14:41:44 -0700525 // We need to check here to verify that the given item's location isn't already occupied
526 // by another item. If it is, we need to find the next available slot and assign
527 // it that position. This is an issue when upgrading from the old Folders implementation
528 // which could contain an unlimited number of items.
529 if (mContent.getChildAt(item.cellX, item.cellY) != null) {
530 if (!findAndSetEmptyCells(item)) {
531 return false;
532 }
533 }
534
Adam Cohendf2cc412011-04-27 16:56:57 -0700535 CellLayout.LayoutParams lp =
536 new CellLayout.LayoutParams(item.cellX, item.cellY, item.spanX, item.spanY);
537 boolean insert = false;
538 mContent.addViewToCellLayout(textView, insert ? 0 : -1, (int)item.id, lp, true);
Adam Cohenc508b2d2011-06-28 14:41:44 -0700539 return true;
Adam Cohendf2cc412011-04-27 16:56:57 -0700540 }
541
Adam Cohencb3382b2011-05-24 14:07:08 -0700542 public void onDragEnter(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700543 mPreviousTargetCell[0] = -1;
544 mPreviousTargetCell[1] = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700545 mOnExitAlarm.cancelAlarm();
546 }
547
548 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
549 public void onAlarm(Alarm alarm) {
550 realTimeReorder(mEmptyCell, mTargetCell);
551 }
552 };
553
554 boolean readingOrderGreaterThan(int[] v1, int[] v2) {
555 if (v1[1] > v2[1] || (v1[1] == v2[1] && v1[0] > v2[0])) {
556 return true;
557 } else {
558 return false;
559 }
560 }
561
562 private void realTimeReorder(int[] empty, int[] target) {
563 boolean wrap;
564 int startX;
565 int endX;
566 int startY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700567 int delay = 0;
568 float delayAmount = 30;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700569 if (readingOrderGreaterThan(target, empty)) {
570 wrap = empty[0] >= mContent.getCountX() - 1;
571 startY = wrap ? empty[1] + 1 : empty[1];
572 for (int y = startY; y <= target[1]; y++) {
573 startX = y == empty[1] ? empty[0] + 1 : 0;
574 endX = y < target[1] ? mContent.getCountX() - 1 : target[0];
575 for (int x = startX; x <= endX; x++) {
576 View v = mContent.getChildAt(x,y);
577 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen76fc0852011-06-17 13:26:23 -0700578 REORDER_ANIMATION_DURATION, delay)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700579 empty[0] = x;
580 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700581 delay += delayAmount;
582 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700583 }
584 }
585 }
586 } else {
587 wrap = empty[0] == 0;
588 startY = wrap ? empty[1] - 1 : empty[1];
589 for (int y = startY; y >= target[1]; y--) {
590 startX = y == empty[1] ? empty[0] - 1 : mContent.getCountX() - 1;
591 endX = y > target[1] ? 0 : target[0];
592 for (int x = startX; x >= endX; x--) {
593 View v = mContent.getChildAt(x,y);
594 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen76fc0852011-06-17 13:26:23 -0700595 REORDER_ANIMATION_DURATION, delay)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700596 empty[0] = x;
597 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700598 delay += delayAmount;
599 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700600 }
601 }
602 }
603 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700604 }
605
Adam Cohencb3382b2011-05-24 14:07:08 -0700606 public void onDragOver(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700607 float[] r = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, d.dragView, null);
608 mTargetCell = mContent.findNearestArea((int) r[0], (int) r[1], 1, 1, mTargetCell);
609
610 if (mTargetCell[0] != mPreviousTargetCell[0] || mTargetCell[1] != mPreviousTargetCell[1]) {
611 mReorderAlarm.cancelAlarm();
612 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
613 mReorderAlarm.setAlarm(150);
614 mPreviousTargetCell[0] = mTargetCell[0];
615 mPreviousTargetCell[1] = mTargetCell[1];
616 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700617 }
618
Adam Cohenbfbfd262011-06-13 16:55:12 -0700619 // This is used to compute the visual center of the dragView. The idea is that
620 // the visual center represents the user's interpretation of where the item is, and hence
621 // is the appropriate point to use when determining drop location.
622 private float[] getDragViewVisualCenter(int x, int y, int xOffset, int yOffset,
623 DragView dragView, float[] recycle) {
624 float res[];
625 if (recycle == null) {
626 res = new float[2];
627 } else {
628 res = recycle;
629 }
630
631 // These represent the visual top and left of drag view if a dragRect was provided.
632 // If a dragRect was not provided, then they correspond to the actual view left and
633 // top, as the dragRect is in that case taken to be the entire dragView.
634 // R.dimen.dragViewOffsetY.
635 int left = x - xOffset;
636 int top = y - yOffset;
637
638 // In order to find the visual center, we shift by half the dragRect
639 res[0] = left + dragView.getDragRegion().width() / 2;
640 res[1] = top + dragView.getDragRegion().height() / 2;
641
642 return res;
643 }
644
645 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
646 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700647 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700648 }
649 };
650
Adam Cohen95bb8002011-07-03 23:40:28 -0700651 public void completeDragExit() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700652 mLauncher.closeFolder();
653 mCurrentDragInfo = null;
654 mCurrentDragView = null;
655 mSuppressOnAdd = false;
656 mRearrangeOnClose = true;
657 }
658
Adam Cohencb3382b2011-05-24 14:07:08 -0700659 public void onDragExit(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700660 // We only close the folder if this is a true drag exit, ie. not because a drop
661 // has occurred above the folder.
662 if (!d.dragComplete) {
663 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
664 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
665 }
666 mReorderAlarm.cancelAlarm();
Adam Cohen2801caf2011-05-13 20:57:39 -0700667 }
668
Adam Cohenc0dcf592011-06-01 15:30:43 -0700669 public void onDropCompleted(View target, DragObject d, boolean success) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700670 mCurrentDragInfo = null;
671 mCurrentDragView = null;
672 mSuppressOnAdd = false;
Adam Cohen76078c42011-06-09 15:06:52 -0700673 if (!success) {
674 if (d.dragView != null) {
675 if (target instanceof CellLayout) {
676 // TODO: we should animate the item back to the folder in this case
677 }
678 }
Adam Cohenbfbfd262011-06-13 16:55:12 -0700679 // TODO: if the drag fails, we need to re-add the item
Adam Cohen3e8f8112011-07-02 18:03:00 -0700680 } else {
681 if (target != this) {
682 mOnExitAlarm.cancelAlarm();
683 completeDragExit();
684 }
Adam Cohen76078c42011-06-09 15:06:52 -0700685 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700686 }
687
688 public boolean isDropEnabled() {
689 return true;
690 }
691
Adam Cohencb3382b2011-05-24 14:07:08 -0700692 public DropTarget getDropTargetDelegate(DragObject d) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700693 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800694 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700695
Adam Cohen2801caf2011-05-13 20:57:39 -0700696 private void setupContentDimension(int count) {
697 ArrayList<View> list = getItemsInReadingOrder();
698
699 int countX = mContent.getCountX();
700 int countY = mContent.getCountY();
Adam Cohen7c693212011-05-18 15:26:57 -0700701 boolean done = false;
Adam Cohen2801caf2011-05-13 20:57:39 -0700702
Adam Cohen7c693212011-05-18 15:26:57 -0700703 while (!done) {
704 int oldCountX = countX;
705 int oldCountY = countY;
706 if (countX * countY < count) {
707 // Current grid is too small, expand it
708 if (countX <= countY && countX < mMaxCountX) {
709 countX++;
710 } else if (countY < mMaxCountY) {
711 countY++;
712 }
713 if (countY == 0) countY++;
714 } else if ((countY - 1) * countX >= count && countY >= countX) {
715 countY = Math.max(0, countY - 1);
716 } else if ((countX - 1) * countY >= count) {
717 countX = Math.max(0, countX - 1);
Adam Cohen2801caf2011-05-13 20:57:39 -0700718 }
Adam Cohen7c693212011-05-18 15:26:57 -0700719 done = countX == oldCountX && countY == oldCountY;
Adam Cohen2801caf2011-05-13 20:57:39 -0700720 }
Adam Cohen7c693212011-05-18 15:26:57 -0700721 mContent.setGridSize(countX, countY);
Adam Cohen2801caf2011-05-13 20:57:39 -0700722 arrangeChildren(list);
723 }
724
725 public boolean isFull() {
726 return getItemCount() >= mMaxCountX * mMaxCountY;
727 }
728
729 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700730 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700731
732 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohen76fc0852011-06-17 13:26:23 -0700733 // Technically there is no padding at the bottom, but we add space equal to the padding
734 // and have to account for that here.
735 int height = getPaddingTop() + mContent.getDesiredHeight() + mFolderNameHeight;
Adam Cohen8e776a62011-06-28 18:10:06 -0700736 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700737
Adam Cohen8e776a62011-06-28 18:10:06 -0700738 parent.getDescendantRectRelativeToSelf(mFolderIcon, mTempRect);
739
740 int centerX = mTempRect.centerX();
741 int centerY = mTempRect.centerY();
Adam Cohen2801caf2011-05-13 20:57:39 -0700742 int centeredLeft = centerX - width / 2;
743 int centeredTop = centerY - height / 2;
744
Adam Cohen2801caf2011-05-13 20:57:39 -0700745 int parentWidth = 0;
746 int parentHeight = 0;
Adam Cohen8e776a62011-06-28 18:10:06 -0700747 if (parent != null) {
748 parentWidth = parent.getMeasuredWidth();
749 parentHeight = parent.getMeasuredHeight();
Adam Cohen2801caf2011-05-13 20:57:39 -0700750 }
751
752 int left = Math.min(Math.max(0, centeredLeft), parentWidth - width);
753 int top = Math.min(Math.max(0, centeredTop), parentHeight - height);
Adam Cohen0e4857c2011-06-30 17:05:14 -0700754 if (width >= parentWidth) {
755 left = (parentWidth - width) / 2;
756 }
757 if (height >= parentHeight) {
758 top = (parentHeight - height) / 2;
759 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700760
761 int folderPivotX = width / 2 + (centeredLeft - left);
762 int folderPivotY = height / 2 + (centeredTop - top);
763 setPivotX(folderPivotX);
764 setPivotY(folderPivotY);
765 int folderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
766 (1.0f * folderPivotX / width));
767 int folderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
768 (1.0f * folderPivotY / height));
769 mFolderIcon.setPivotX(folderIconPivotX);
770 mFolderIcon.setPivotY(folderIconPivotY);
771
772 if (mMode == PARTIAL_GROW) {
773 lp.width = width;
774 lp.height = height;
775 lp.x = left;
776 lp.y = top;
777 } else {
778 mNewSize.set(left, top, left + width, top + height);
779 }
780 }
781
782 private void setupContentForNumItems(int count) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700783 setupContentDimension(count);
784
Adam Cohen8e776a62011-06-28 18:10:06 -0700785 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700786 if (lp == null) {
Adam Cohen8e776a62011-06-28 18:10:06 -0700787 lp = new DragLayer.LayoutParams(0, 0);
788 lp.customPosition = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700789 setLayoutParams(lp);
790 }
791 centerAboutIcon();
792 }
793
794 private void arrangeChildren(ArrayList<View> list) {
795 int[] vacant = new int[2];
796 if (list == null) {
797 list = getItemsInReadingOrder();
798 }
799 mContent.removeAllViews();
800
801 for (int i = 0; i < list.size(); i++) {
802 View v = list.get(i);
803 mContent.getVacantCell(vacant, 1, 1);
804 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) v.getLayoutParams();
805 lp.cellX = vacant[0];
806 lp.cellY = vacant[1];
807 ItemInfo info = (ItemInfo) v.getTag();
808 info.cellX = vacant[0];
809 info.cellY = vacant[1];
810 boolean insert = false;
811 mContent.addViewToCellLayout(v, insert ? 0 : -1, (int)info.id, lp, true);
Adam Cohen7c693212011-05-18 15:26:57 -0700812 LauncherModel.addOrMoveItemInDatabase(mLauncher, info, mInfo.id, 0,
813 info.cellX, info.cellY);
Adam Cohen2801caf2011-05-13 20:57:39 -0700814 }
Adam Cohen7c693212011-05-18 15:26:57 -0700815 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700816 }
817
Adam Cohena9cf38f2011-05-02 15:36:58 -0700818 public int getItemCount() {
819 return mContent.getChildrenLayout().getChildCount();
820 }
821
822 public View getItemAt(int index) {
823 return mContent.getChildrenLayout().getChildAt(index);
824 }
825
Adam Cohen2801caf2011-05-13 20:57:39 -0700826 private void onCloseComplete() {
827 if (mRearrangeOnClose) {
828 setupContentForNumItems(getItemCount());
829 mRearrangeOnClose = false;
830 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700831 if (getItemCount() <= 1) {
832 replaceFolderWithFinalItem();
833 }
834 }
835
836 private void replaceFolderWithFinalItem() {
837 ItemInfo finalItem = null;
838
839 if (getItemCount() == 1) {
840 finalItem = mInfo.contents.get(0);
841 }
842
843 // Remove the folder completely
844 final CellLayout cellLayout = (CellLayout)
845 mLauncher.getWorkspace().getChildAt(mInfo.screen);
846 cellLayout.removeView(mFolderIcon);
847 if (mFolderIcon instanceof DropTarget) {
848 mDragController.removeDropTarget((DropTarget) mFolderIcon);
849 }
850 mLauncher.removeFolder(mInfo);
851
852 if (finalItem != null) {
853 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem,
854 LauncherSettings.Favorites.CONTAINER_DESKTOP, mInfo.screen,
855 mInfo.cellX, mInfo.cellY);
856 }
Adam Cohen716b51e2011-06-30 12:09:54 -0700857 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700858
859 // Add the last remaining child to the workspace in place of the folder
860 if (finalItem != null) {
861 View child = mLauncher.createShortcut(R.layout.application, cellLayout,
862 (ShortcutInfo) finalItem);
863
864 mLauncher.getWorkspace().addInScreen(child, mInfo.screen, mInfo.cellX, mInfo.cellY,
865 mInfo.spanX, mInfo.spanY);
866 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700867 }
868
Adam Cohenbfbfd262011-06-13 16:55:12 -0700869 public void onDrop(DragObject d) {
870 ShortcutInfo item;
871 if (d.dragInfo instanceof ApplicationInfo) {
872 // Came from all apps -- make a copy
873 item = ((ApplicationInfo) d.dragInfo).makeShortcut();
874 item.spanX = 1;
875 item.spanY = 1;
876 } else {
877 item = (ShortcutInfo) d.dragInfo;
878 }
879 // Dragged from self onto self
880 if (item == mCurrentDragInfo) {
881 ShortcutInfo si = (ShortcutInfo) mCurrentDragView.getTag();
882 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) mCurrentDragView.getLayoutParams();
883 si.cellX = lp.cellX = mEmptyCell[0];
884 si.cellX = lp.cellY = mEmptyCell[1];
885 mContent.addViewToCellLayout(mCurrentDragView, -1, (int)item.id, lp, true);
Adam Cohen716b51e2011-06-30 12:09:54 -0700886 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, mCurrentDragView);
Adam Cohene9166b22011-07-08 17:11:11 -0700887 mItemsInvalidated = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700888 setupContentDimension(getItemCount());
Adam Cohen716b51e2011-06-30 12:09:54 -0700889 mSuppressOnAdd = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700890 }
891 mInfo.add(item);
892 }
893
894 public void onAdd(ShortcutInfo item) {
895 mItemsInvalidated = true;
896 if (mSuppressOnAdd) return;
897 if (!findAndSetEmptyCells(item)) {
898 // The current layout is full, can we expand it?
899 setupContentForNumItems(getItemCount() + 1);
900 findAndSetEmptyCells(item);
901 }
902 createAndAddShortcut(item);
903 LauncherModel.addOrMoveItemInDatabase(
904 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
905 }
906
Adam Cohena9cf38f2011-05-02 15:36:58 -0700907 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -0700908 mItemsInvalidated = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700909 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -0700910 View v = getViewForInfo(item);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700911 mContent.removeView(v);
Adam Cohen2801caf2011-05-13 20:57:39 -0700912 if (mState == STATE_ANIMATING) {
913 mRearrangeOnClose = true;
914 } else {
915 setupContentForNumItems(getItemCount());
916 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700917 if (getItemCount() <= 1) {
918 replaceFolderWithFinalItem();
919 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700920 }
Adam Cohen7c693212011-05-18 15:26:57 -0700921
Adam Cohendf1e4e82011-06-24 15:57:39 -0700922 private View getViewForInfo(ShortcutInfo item) {
923 for (int j = 0; j < mContent.getCountY(); j++) {
924 for (int i = 0; i < mContent.getCountX(); i++) {
925 View v = mContent.getChildAt(i, j);
926 if (v.getTag() == item) {
927 return v;
928 }
929 }
930 }
931 return null;
932 }
933
Adam Cohen76078c42011-06-09 15:06:52 -0700934 public void onItemsChanged() {
935 }
Adam Cohen76fc0852011-06-17 13:26:23 -0700936 public void onTitleChanged(CharSequence title) {
937 }
Adam Cohen76078c42011-06-09 15:06:52 -0700938
Adam Cohen7c693212011-05-18 15:26:57 -0700939 public ArrayList<View> getItemsInReadingOrder() {
Adam Cohen76078c42011-06-09 15:06:52 -0700940 return getItemsInReadingOrder(true);
941 }
942
943 public ArrayList<View> getItemsInReadingOrder(boolean includeCurrentDragItem) {
Adam Cohen7c693212011-05-18 15:26:57 -0700944 if (mItemsInvalidated) {
945 mItemsInReadingOrder.clear();
946 for (int j = 0; j < mContent.getCountY(); j++) {
947 for (int i = 0; i < mContent.getCountX(); i++) {
948 View v = mContent.getChildAt(i, j);
949 if (v != null) {
Adam Cohen76078c42011-06-09 15:06:52 -0700950 ShortcutInfo info = (ShortcutInfo) v.getTag();
951 if (info != mCurrentDragInfo || includeCurrentDragItem) {
952 mItemsInReadingOrder.add(v);
953 }
Adam Cohen7c693212011-05-18 15:26:57 -0700954 }
955 }
956 }
957 mItemsInvalidated = false;
958 }
959 return mItemsInReadingOrder;
960 }
Adam Cohen8dfcba42011-07-07 16:38:18 -0700961
962 public void getLocationInDragLayer(int[] loc) {
963 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
964 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800965}