blob: 1b863de1416677717ae77f09793a68563c00b01d [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 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 Cohen67bd9cc2011-07-29 14:07:04 -070099 private boolean mDragInProgress = false;
100 private boolean mDeleteFolderOnDropCompleted = false;
101 private boolean mSuppressFolderDeletion = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700102 private boolean mItemAddedBackToSelfViaIcon = false;
Adam Cohenac56cff2011-09-28 20:45:37 -0700103 FolderEditText mFolderName;
Adam Cohen228da5a2011-07-27 22:23:47 -0700104
Adam Cohen76fc0852011-06-17 13:26:23 -0700105 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 }
Adam Cohen4eac29a2011-07-11 17:53:37 -0700137 mLauncher = (Launcher) context;
Adam Cohenac56cff2011-09-28 20:45:37 -0700138 // We need this view to be focusable in touch mode so that when text editing of the folder
139 // name is complete, we have something to focus on, thus hiding the cursor and giving
140 // reliable behvior when clicking the text field (since it will always gain focus on click).
141 setFocusableInTouchMode(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800142 }
143
144 @Override
145 protected void onFinishInflate() {
146 super.onFinishInflate();
Adam Cohendf2cc412011-04-27 16:56:57 -0700147 mContent = (CellLayout) findViewById(R.id.folder_content);
Adam Cohen2801caf2011-05-13 20:57:39 -0700148 mContent.setGridSize(0, 0);
Adam Cohenac56cff2011-09-28 20:45:37 -0700149 mFolderName = (FolderEditText) findViewById(R.id.folder_name);
150 mFolderName.setFolder(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700151
152 // We find out how tall the text view wants to be (it is set to wrap_content), so that
153 // we can allocate the appropriate amount of space for it.
154 int measureSpec = MeasureSpec.UNSPECIFIED;
155 mFolderName.measure(measureSpec, measureSpec);
156 mFolderNameHeight = mFolderName.getMeasuredHeight();
157
158 // We disable action mode for now since it messes up the view on phones
159 mFolderName.setCustomSelectionActionModeCallback(mActionModeCallback);
Adam Cohen76fc0852011-06-17 13:26:23 -0700160 mFolderName.setOnEditorActionListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700161 mFolderName.setSelectAllOnFocus(true);
Adam Cohen7a14d0b2011-06-24 11:36:35 -0700162 mFolderName.setInputType(mFolderName.getInputType() |
163 InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700165
Adam Cohen76fc0852011-06-17 13:26:23 -0700166 private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
167 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
168 return false;
169 }
170
171 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
172 return false;
173 }
174
175 public void onDestroyActionMode(ActionMode mode) {
176 }
177
178 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
179 return false;
180 }
181 };
182
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 public void onClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700184 Object tag = v.getTag();
185 if (tag instanceof ShortcutInfo) {
186 // refactor this code from Folder
187 ShortcutInfo item = (ShortcutInfo) tag;
188 int[] pos = new int[2];
189 v.getLocationOnScreen(pos);
190 item.intent.setSourceBounds(new Rect(pos[0], pos[1],
191 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
192 mLauncher.startActivitySafely(item.intent, item);
Adam Cohendf2cc412011-04-27 16:56:57 -0700193 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194 }
195
Adam Cohen76fc0852011-06-17 13:26:23 -0700196 public boolean onInterceptTouchEvent(MotionEvent ev) {
197 if (ev.getAction() == MotionEvent.ACTION_DOWN) {
198 mFolderName.getHitRect(mHitRect);
199 if (mHitRect.contains((int) ev.getX(), (int) ev.getY()) && !mIsEditingName) {
200 startEditingFolderName();
201 }
202 }
203 return false;
204 }
205
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800206 public boolean onLongClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700207 Object tag = v.getTag();
208 if (tag instanceof ShortcutInfo) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700209 ShortcutInfo item = (ShortcutInfo) tag;
210 if (!v.isInTouchMode()) {
211 return false;
212 }
213
Winson Chung7d7541e2011-09-16 20:14:36 -0700214 mLauncher.dismissFolderCling(null);
215
Adam Cohendf2cc412011-04-27 16:56:57 -0700216 mLauncher.getWorkspace().onDragStartedWithItem(v);
Adam Cohenac8c8762011-07-13 11:15:27 -0700217 mLauncher.getWorkspace().beginDragShared(v, this);
Adam Cohenbadf71e2011-05-26 19:08:29 -0700218 mIconDrawable = ((TextView) v).getCompoundDrawables()[1];
Adam Cohen76078c42011-06-09 15:06:52 -0700219
220 mCurrentDragInfo = item;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700221 mEmptyCell[0] = item.cellX;
222 mEmptyCell[1] = item.cellY;
223 mCurrentDragView = v;
Adam Cohenfc53cd22011-07-20 15:45:11 -0700224
225 mContent.removeView(mCurrentDragView);
226 mInfo.remove(mCurrentDragInfo);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700227 mDragInProgress = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700228 mItemAddedBackToSelfViaIcon = false;
Adam Cohendf2cc412011-04-27 16:56:57 -0700229 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800230 return true;
231 }
232
Adam Cohen76fc0852011-06-17 13:26:23 -0700233 public boolean isEditingName() {
234 return mIsEditingName;
235 }
236
237 public void startEditingFolderName() {
Adam Cohena65beee2011-06-27 21:32:23 -0700238 mFolderName.setHint("");
Adam Cohen76fc0852011-06-17 13:26:23 -0700239 mIsEditingName = true;
240 }
241
242 public void dismissEditingName() {
243 mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
244 doneEditingFolderName(true);
245 }
246
247 public void doneEditingFolderName(boolean commit) {
Adam Cohena65beee2011-06-27 21:32:23 -0700248 mFolderName.setHint(sHintText);
Adam Cohen1df26a32011-08-12 16:15:23 -0700249 // Convert to a string here to ensure that no other state associated with the text field
250 // gets saved.
251 mInfo.setTitle(mFolderName.getText().toString());
Adam Cohen76fc0852011-06-17 13:26:23 -0700252 LauncherModel.updateItemInDatabase(mLauncher, mInfo);
Adam Cohenac56cff2011-09-28 20:45:37 -0700253
254 // In order to clear the focus from the text field, we set the focus on ourself. This
255 // ensures that every time the field is clicked, focus is gained, giving reliable behavior.
256 requestFocus();
257
Adam Cohene601a432011-07-26 21:51:30 -0700258 Selection.setSelection((Spannable) mFolderName.getText(), 0, 0);
Adam Cohen76fc0852011-06-17 13:26:23 -0700259 mIsEditingName = false;
260 }
261
262 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
263 if (actionId == EditorInfo.IME_ACTION_DONE) {
264 dismissEditingName();
265 return true;
266 }
267 return false;
268 }
269
270 public View getEditTextRegion() {
271 return mFolderName;
272 }
273
Adam Cohenbadf71e2011-05-26 19:08:29 -0700274 public Drawable getDragDrawable() {
275 return mIconDrawable;
276 }
277
Adam Cohen0c872ba2011-05-05 10:34:16 -0700278 /**
279 * We need to handle touch events to prevent them from falling through to the workspace below.
280 */
281 @Override
282 public boolean onTouchEvent(MotionEvent ev) {
283 return true;
284 }
285
Joe Onorato00acb122009-08-04 16:04:30 -0400286 public void setDragController(DragController dragController) {
287 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800288 }
289
Adam Cohen2801caf2011-05-13 20:57:39 -0700290 void setFolderIcon(FolderIcon icon) {
291 mFolderIcon = icon;
292 }
293
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800294 /**
295 * @return the FolderInfo object associated with this folder
296 */
297 FolderInfo getInfo() {
298 return mInfo;
299 }
300
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800301 void bind(FolderInfo info) {
302 mInfo = info;
Adam Cohendf2cc412011-04-27 16:56:57 -0700303 ArrayList<ShortcutInfo> children = info.contents;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700304 ArrayList<ShortcutInfo> overflow = new ArrayList<ShortcutInfo>();
Adam Cohen7c693212011-05-18 15:26:57 -0700305 setupContentForNumItems(children.size());
Adam Cohen0057bbc2011-08-12 18:30:51 -0700306 int count = 0;
Adam Cohendf2cc412011-04-27 16:56:57 -0700307 for (int i = 0; i < children.size(); i++) {
308 ShortcutInfo child = (ShortcutInfo) children.get(i);
Adam Cohenc508b2d2011-06-28 14:41:44 -0700309 if (!createAndAddShortcut(child)) {
310 overflow.add(child);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700311 } else {
312 count++;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700313 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700314 }
Adam Cohenc508b2d2011-06-28 14:41:44 -0700315
Adam Cohen0057bbc2011-08-12 18:30:51 -0700316 // We rearrange the items in case there are any empty gaps
317 setupContentForNumItems(count);
318
Adam Cohenc508b2d2011-06-28 14:41:44 -0700319 // If our folder has too many items we prune them from the list. This is an issue
320 // when upgrading from the old Folders implementation which could contain an unlimited
321 // number of items.
322 for (ShortcutInfo item: overflow) {
323 mInfo.remove(item);
324 LauncherModel.deleteItemFromDatabase(mLauncher, item);
325 }
326
Adam Cohen4dbe6d92011-05-18 17:14:15 -0700327 mItemsInvalidated = true;
Adam Cohenac56cff2011-09-28 20:45:37 -0700328 updateTextViewFocus();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700329 mInfo.addListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700330
Adam Cohenafb01ee2011-06-23 15:38:03 -0700331 if (!sDefaultFolderName.contentEquals(mInfo.title)) {
Adam Cohen4ef610f2011-06-21 22:37:36 -0700332 mFolderName.setText(mInfo.title);
333 } else {
334 mFolderName.setText("");
335 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700336 }
337
338 /**
339 * Creates a new UserFolder, inflated from R.layout.user_folder.
340 *
341 * @param context The application's context.
342 *
343 * @return A new UserFolder.
344 */
345 static Folder fromXml(Context context) {
346 return (Folder) LayoutInflater.from(context).inflate(R.layout.user_folder, null);
347 }
348
349 /**
350 * This method is intended to make the UserFolder to be visually identical in size and position
351 * to its associated FolderIcon. This allows for a seamless transition into the expanded state.
352 */
353 private void positionAndSizeAsIcon() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700354 if (!(getParent() instanceof DragLayer)) return;
Adam Cohendf2cc412011-04-27 16:56:57 -0700355
Adam Cohen8e776a62011-06-28 18:10:06 -0700356 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohendf2cc412011-04-27 16:56:57 -0700357
Adam Cohen2801caf2011-05-13 20:57:39 -0700358 if (mMode == PARTIAL_GROW) {
359 setScaleX(0.8f);
360 setScaleY(0.8f);
361 setAlpha(0f);
362 } else {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700363 mLauncher.getDragLayer().getDescendantRectRelativeToSelf(mFolderIcon, mIconRect);
364 lp.width = mIconRect.width();
365 lp.height = mIconRect.height();
366 lp.x = mIconRect.left;
367 lp.y = mIconRect.top;
Adam Cohen2801caf2011-05-13 20:57:39 -0700368 mContent.setAlpha(0);
369 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700370 mState = STATE_SMALL;
371 }
372
373 public void animateOpen() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700374 positionAndSizeAsIcon();
375
Adam Cohen8e776a62011-06-28 18:10:06 -0700376 if (!(getParent() instanceof DragLayer)) return;
Adam Cohendf2cc412011-04-27 16:56:57 -0700377
Adam Cohen2801caf2011-05-13 20:57:39 -0700378 ObjectAnimator oa;
Adam Cohen8e776a62011-06-28 18:10:06 -0700379 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohendf2cc412011-04-27 16:56:57 -0700380
Adam Cohen2801caf2011-05-13 20:57:39 -0700381 centerAboutIcon();
382 if (mMode == PARTIAL_GROW) {
383 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1);
384 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
385 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
386 oa = ObjectAnimator.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
387 } else {
388 PropertyValuesHolder width = PropertyValuesHolder.ofInt("width", mNewSize.width());
389 PropertyValuesHolder height = PropertyValuesHolder.ofInt("height", mNewSize.height());
390 PropertyValuesHolder x = PropertyValuesHolder.ofInt("x", mNewSize.left);
391 PropertyValuesHolder y = PropertyValuesHolder.ofInt("y", mNewSize.top);
392 oa = ObjectAnimator.ofPropertyValuesHolder(lp, width, height, x, y);
393 oa.addUpdateListener(new AnimatorUpdateListener() {
394 public void onAnimationUpdate(ValueAnimator animation) {
395 requestLayout();
396 }
397 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700398
Adam Cohen2801caf2011-05-13 20:57:39 -0700399 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
400 ObjectAnimator alphaOa = ObjectAnimator.ofPropertyValuesHolder(mContent, alpha);
401 alphaOa.setDuration(mExpandDuration);
402 alphaOa.setInterpolator(new AccelerateInterpolator(2.0f));
403 alphaOa.start();
404 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700405
Adam Cohen2801caf2011-05-13 20:57:39 -0700406 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700407 @Override
408 public void onAnimationStart(Animator animation) {
409 mState = STATE_ANIMATING;
410 }
411 @Override
412 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700413 mState = STATE_OPEN;
Winson Chung7a74ac92011-09-20 17:43:51 -0700414
415 Cling cling = mLauncher.showFirstRunFoldersCling();
416 if (cling != null) {
417 cling.bringToFront();
418 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700419 setFocusOnFirstChild();
Adam Cohendf2cc412011-04-27 16:56:57 -0700420 }
421 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700422 oa.setDuration(mExpandDuration);
423 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700424 }
425
Adam Cohenac56cff2011-09-28 20:45:37 -0700426 private void setFocusOnFirstChild() {
427 View firstChild = mContent.getChildAt(0, 0);
428 if (firstChild != null) {
429 firstChild.requestFocus();
430 }
431 }
432
Adam Cohendf2cc412011-04-27 16:56:57 -0700433 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700434 if (!(getParent() instanceof DragLayer)) return;
Adam Cohendf2cc412011-04-27 16:56:57 -0700435
Adam Cohen2801caf2011-05-13 20:57:39 -0700436 ObjectAnimator oa;
Adam Cohen2801caf2011-05-13 20:57:39 -0700437 if (mMode == PARTIAL_GROW) {
438 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
439 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
440 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
441 oa = ObjectAnimator.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
442 } else {
Adam Cohen8e776a62011-06-28 18:10:06 -0700443 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohendf2cc412011-04-27 16:56:57 -0700444
Adam Cohen3e8f8112011-07-02 18:03:00 -0700445 PropertyValuesHolder width = PropertyValuesHolder.ofInt("width", mIconRect.width());
446 PropertyValuesHolder height = PropertyValuesHolder.ofInt("height", mIconRect.height());
447 PropertyValuesHolder x = PropertyValuesHolder.ofInt("x", mIconRect.left);
448 PropertyValuesHolder y = PropertyValuesHolder.ofInt("y", mIconRect.top);
Adam Cohen2801caf2011-05-13 20:57:39 -0700449 oa = ObjectAnimator.ofPropertyValuesHolder(lp, width, height, x, y);
450 oa.addUpdateListener(new AnimatorUpdateListener() {
451 public void onAnimationUpdate(ValueAnimator animation) {
452 requestLayout();
453 }
454 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700455
Adam Cohen2801caf2011-05-13 20:57:39 -0700456 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0f);
457 ObjectAnimator alphaOa = ObjectAnimator.ofPropertyValuesHolder(mContent, alpha);
458 alphaOa.setDuration(mExpandDuration);
459 alphaOa.setInterpolator(new DecelerateInterpolator(2.0f));
460 alphaOa.start();
461 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700462
Adam Cohen2801caf2011-05-13 20:57:39 -0700463 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700464 @Override
465 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700466 onCloseComplete();
Adam Cohen2801caf2011-05-13 20:57:39 -0700467 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700468 }
469 @Override
470 public void onAnimationStart(Animator animation) {
471 mState = STATE_ANIMATING;
472 }
473 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700474 oa.setDuration(mExpandDuration);
475 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700476 }
477
478 void notifyDataSetChanged() {
479 // recreate all the children if the data set changes under us. We may want to do this more
480 // intelligently (ie just removing the views that should no longer exist)
481 mContent.removeAllViewsInLayout();
482 bind(mInfo);
483 }
484
Adam Cohencb3382b2011-05-24 14:07:08 -0700485 public boolean acceptDrop(DragObject d) {
486 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700487 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700488 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
489 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
490 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700491 }
492
Adam Cohendf2cc412011-04-27 16:56:57 -0700493 protected boolean findAndSetEmptyCells(ShortcutInfo item) {
494 int[] emptyCell = new int[2];
495 if (mContent.findCellForSpan(emptyCell, item.spanX, item.spanY)) {
496 item.cellX = emptyCell[0];
497 item.cellY = emptyCell[1];
Adam Cohendf2cc412011-04-27 16:56:57 -0700498 return true;
499 } else {
500 return false;
501 }
502 }
503
Adam Cohenc508b2d2011-06-28 14:41:44 -0700504 protected boolean createAndAddShortcut(ShortcutInfo item) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700505 final TextView textView =
Adam Cohene87b9242011-06-29 14:01:26 -0700506 (TextView) mInflater.inflate(R.layout.application, this, false);
Adam Cohendf2cc412011-04-27 16:56:57 -0700507 textView.setCompoundDrawablesWithIntrinsicBounds(null,
508 new FastBitmapDrawable(item.getIcon(mIconCache)), null, null);
509 textView.setText(item.title);
510 textView.setTag(item);
511
512 textView.setOnClickListener(this);
513 textView.setOnLongClickListener(this);
514
Adam Cohenc508b2d2011-06-28 14:41:44 -0700515 // We need to check here to verify that the given item's location isn't already occupied
516 // by another item. If it is, we need to find the next available slot and assign
517 // it that position. This is an issue when upgrading from the old Folders implementation
518 // which could contain an unlimited number of items.
Adam Cohen0057bbc2011-08-12 18:30:51 -0700519 if (mContent.getChildAt(item.cellX, item.cellY) != null || item.cellX < 0 || item.cellY < 0
520 || item.cellX >= mContent.getCountX() || item.cellY >= mContent.getCountY()) {
Adam Cohenc508b2d2011-06-28 14:41:44 -0700521 if (!findAndSetEmptyCells(item)) {
522 return false;
523 }
524 }
525
Adam Cohendf2cc412011-04-27 16:56:57 -0700526 CellLayout.LayoutParams lp =
527 new CellLayout.LayoutParams(item.cellX, item.cellY, item.spanX, item.spanY);
528 boolean insert = false;
Adam Cohenac56cff2011-09-28 20:45:37 -0700529 textView.setOnKeyListener(new FolderKeyEventListener());
Adam Cohendf2cc412011-04-27 16:56:57 -0700530 mContent.addViewToCellLayout(textView, insert ? 0 : -1, (int)item.id, lp, true);
Adam Cohenc508b2d2011-06-28 14:41:44 -0700531 return true;
Adam Cohendf2cc412011-04-27 16:56:57 -0700532 }
533
Adam Cohencb3382b2011-05-24 14:07:08 -0700534 public void onDragEnter(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700535 mPreviousTargetCell[0] = -1;
536 mPreviousTargetCell[1] = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700537 mOnExitAlarm.cancelAlarm();
538 }
539
540 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
541 public void onAlarm(Alarm alarm) {
542 realTimeReorder(mEmptyCell, mTargetCell);
543 }
544 };
545
546 boolean readingOrderGreaterThan(int[] v1, int[] v2) {
547 if (v1[1] > v2[1] || (v1[1] == v2[1] && v1[0] > v2[0])) {
548 return true;
549 } else {
550 return false;
551 }
552 }
553
554 private void realTimeReorder(int[] empty, int[] target) {
555 boolean wrap;
556 int startX;
557 int endX;
558 int startY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700559 int delay = 0;
560 float delayAmount = 30;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700561 if (readingOrderGreaterThan(target, empty)) {
562 wrap = empty[0] >= mContent.getCountX() - 1;
563 startY = wrap ? empty[1] + 1 : empty[1];
564 for (int y = startY; y <= target[1]; y++) {
565 startX = y == empty[1] ? empty[0] + 1 : 0;
566 endX = y < target[1] ? mContent.getCountX() - 1 : target[0];
567 for (int x = startX; x <= endX; x++) {
568 View v = mContent.getChildAt(x,y);
569 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen76fc0852011-06-17 13:26:23 -0700570 REORDER_ANIMATION_DURATION, delay)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700571 empty[0] = x;
572 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700573 delay += delayAmount;
574 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700575 }
576 }
577 }
578 } else {
579 wrap = empty[0] == 0;
580 startY = wrap ? empty[1] - 1 : empty[1];
581 for (int y = startY; y >= target[1]; y--) {
582 startX = y == empty[1] ? empty[0] - 1 : mContent.getCountX() - 1;
583 endX = y > target[1] ? 0 : target[0];
584 for (int x = startX; x >= endX; x--) {
585 View v = mContent.getChildAt(x,y);
586 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen76fc0852011-06-17 13:26:23 -0700587 REORDER_ANIMATION_DURATION, delay)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700588 empty[0] = x;
589 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700590 delay += delayAmount;
591 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700592 }
593 }
594 }
595 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700596 }
597
Adam Cohencb3382b2011-05-24 14:07:08 -0700598 public void onDragOver(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700599 float[] r = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, d.dragView, null);
600 mTargetCell = mContent.findNearestArea((int) r[0], (int) r[1], 1, 1, mTargetCell);
601
602 if (mTargetCell[0] != mPreviousTargetCell[0] || mTargetCell[1] != mPreviousTargetCell[1]) {
603 mReorderAlarm.cancelAlarm();
604 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
605 mReorderAlarm.setAlarm(150);
606 mPreviousTargetCell[0] = mTargetCell[0];
607 mPreviousTargetCell[1] = mTargetCell[1];
608 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700609 }
610
Adam Cohenbfbfd262011-06-13 16:55:12 -0700611 // This is used to compute the visual center of the dragView. The idea is that
612 // the visual center represents the user's interpretation of where the item is, and hence
613 // is the appropriate point to use when determining drop location.
614 private float[] getDragViewVisualCenter(int x, int y, int xOffset, int yOffset,
615 DragView dragView, float[] recycle) {
616 float res[];
617 if (recycle == null) {
618 res = new float[2];
619 } else {
620 res = recycle;
621 }
622
623 // These represent the visual top and left of drag view if a dragRect was provided.
624 // If a dragRect was not provided, then they correspond to the actual view left and
625 // top, as the dragRect is in that case taken to be the entire dragView.
626 // R.dimen.dragViewOffsetY.
627 int left = x - xOffset;
628 int top = y - yOffset;
629
630 // In order to find the visual center, we shift by half the dragRect
631 res[0] = left + dragView.getDragRegion().width() / 2;
632 res[1] = top + dragView.getDragRegion().height() / 2;
633
634 return res;
635 }
636
637 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
638 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700639 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700640 }
641 };
642
Adam Cohen95bb8002011-07-03 23:40:28 -0700643 public void completeDragExit() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700644 mLauncher.closeFolder();
645 mCurrentDragInfo = null;
646 mCurrentDragView = null;
647 mSuppressOnAdd = false;
648 mRearrangeOnClose = true;
649 }
650
Adam Cohencb3382b2011-05-24 14:07:08 -0700651 public void onDragExit(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700652 // We only close the folder if this is a true drag exit, ie. not because a drop
653 // has occurred above the folder.
654 if (!d.dragComplete) {
655 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
656 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
657 }
658 mReorderAlarm.cancelAlarm();
Adam Cohen2801caf2011-05-13 20:57:39 -0700659 }
660
Adam Cohenc0dcf592011-06-01 15:30:43 -0700661 public void onDropCompleted(View target, DragObject d, boolean success) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700662 if (success) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700663 if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700664 replaceFolderWithFinalItem();
665 }
666 } else {
667 // The drag failed, we need to return the item to the folder
668 mFolderIcon.onDrop(d);
669
670 // We're going to trigger a "closeFolder" which may occur before this item has
671 // been added back to the folder -- this could cause the folder to be deleted
672 if (mOnExitAlarm.alarmPending()) {
673 mSuppressFolderDeletion = true;
674 }
675 }
676
677 if (target != this) {
678 if (mOnExitAlarm.alarmPending()) {
679 mOnExitAlarm.cancelAlarm();
680 completeDragExit();
681 }
682 }
683 mDeleteFolderOnDropCompleted = false;
684 mDragInProgress = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700685 mItemAddedBackToSelfViaIcon = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700686 mCurrentDragInfo = null;
687 mCurrentDragView = null;
688 mSuppressOnAdd = false;
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700689 }
Adam Cohen228da5a2011-07-27 22:23:47 -0700690
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700691 public void notifyDrop() {
692 if (mDragInProgress) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700693 mItemAddedBackToSelfViaIcon = true;
Adam Cohen76078c42011-06-09 15:06:52 -0700694 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700695 }
696
697 public boolean isDropEnabled() {
698 return true;
699 }
700
Adam Cohencb3382b2011-05-24 14:07:08 -0700701 public DropTarget getDropTargetDelegate(DragObject d) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700702 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800703 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700704
Adam Cohen2801caf2011-05-13 20:57:39 -0700705 private void setupContentDimension(int count) {
706 ArrayList<View> list = getItemsInReadingOrder();
707
708 int countX = mContent.getCountX();
709 int countY = mContent.getCountY();
Adam Cohen7c693212011-05-18 15:26:57 -0700710 boolean done = false;
Adam Cohen2801caf2011-05-13 20:57:39 -0700711
Adam Cohen7c693212011-05-18 15:26:57 -0700712 while (!done) {
713 int oldCountX = countX;
714 int oldCountY = countY;
715 if (countX * countY < count) {
716 // Current grid is too small, expand it
717 if (countX <= countY && countX < mMaxCountX) {
718 countX++;
719 } else if (countY < mMaxCountY) {
720 countY++;
721 }
722 if (countY == 0) countY++;
723 } else if ((countY - 1) * countX >= count && countY >= countX) {
724 countY = Math.max(0, countY - 1);
725 } else if ((countX - 1) * countY >= count) {
726 countX = Math.max(0, countX - 1);
Adam Cohen2801caf2011-05-13 20:57:39 -0700727 }
Adam Cohen7c693212011-05-18 15:26:57 -0700728 done = countX == oldCountX && countY == oldCountY;
Adam Cohen2801caf2011-05-13 20:57:39 -0700729 }
Adam Cohen7c693212011-05-18 15:26:57 -0700730 mContent.setGridSize(countX, countY);
Adam Cohen2801caf2011-05-13 20:57:39 -0700731 arrangeChildren(list);
732 }
733
734 public boolean isFull() {
735 return getItemCount() >= mMaxCountX * mMaxCountY;
736 }
737
738 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700739 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700740
741 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf4bb1cd2011-07-22 14:36:03 -0700742 int height = getPaddingTop() + getPaddingBottom() + mContent.getDesiredHeight()
743 + mFolderNameHeight;
Adam Cohen8e776a62011-06-28 18:10:06 -0700744 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700745
Adam Cohen8e776a62011-06-28 18:10:06 -0700746 parent.getDescendantRectRelativeToSelf(mFolderIcon, mTempRect);
747
748 int centerX = mTempRect.centerX();
749 int centerY = mTempRect.centerY();
Adam Cohen2801caf2011-05-13 20:57:39 -0700750 int centeredLeft = centerX - width / 2;
751 int centeredTop = centerY - height / 2;
752
Adam Cohen35e7e642011-07-17 14:47:18 -0700753 // We first fetch the currently visible CellLayoutChildren
Winson Chung3d503fb2011-07-13 17:25:49 -0700754 CellLayout currentPage = mLauncher.getWorkspace().getCurrentDropLayout();
Adam Cohen35e7e642011-07-17 14:47:18 -0700755 CellLayoutChildren boundingLayout = currentPage.getChildrenLayout();
756 Rect bounds = new Rect();
757 parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
Adam Cohen2801caf2011-05-13 20:57:39 -0700758
Adam Cohen35e7e642011-07-17 14:47:18 -0700759 // We need to bound the folder to the currently visible CellLayoutChildren
760 int left = Math.min(Math.max(bounds.left, centeredLeft),
761 bounds.left + bounds.width() - width);
762 int top = Math.min(Math.max(bounds.top, centeredTop),
763 bounds.top + bounds.height() - height);
764 // If the folder doesn't fit within the bounds, center it about the desired bounds
765 if (width >= bounds.width()) {
766 left = bounds.left + (bounds.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700767 }
Adam Cohen35e7e642011-07-17 14:47:18 -0700768 if (height >= bounds.height()) {
769 top = bounds.top + (bounds.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700770 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700771
772 int folderPivotX = width / 2 + (centeredLeft - left);
773 int folderPivotY = height / 2 + (centeredTop - top);
774 setPivotX(folderPivotX);
775 setPivotY(folderPivotY);
776 int folderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
777 (1.0f * folderPivotX / width));
778 int folderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
779 (1.0f * folderPivotY / height));
780 mFolderIcon.setPivotX(folderIconPivotX);
781 mFolderIcon.setPivotY(folderIconPivotY);
782
783 if (mMode == PARTIAL_GROW) {
784 lp.width = width;
785 lp.height = height;
786 lp.x = left;
787 lp.y = top;
788 } else {
789 mNewSize.set(left, top, left + width, top + height);
790 }
791 }
792
793 private void setupContentForNumItems(int count) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700794 setupContentDimension(count);
795
Adam Cohen8e776a62011-06-28 18:10:06 -0700796 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700797 if (lp == null) {
Adam Cohen8e776a62011-06-28 18:10:06 -0700798 lp = new DragLayer.LayoutParams(0, 0);
799 lp.customPosition = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700800 setLayoutParams(lp);
801 }
802 centerAboutIcon();
803 }
804
Adam Cohen234c4cd2011-07-17 21:03:04 -0700805 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
806 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf4bb1cd2011-07-22 14:36:03 -0700807 int height = getPaddingTop() + getPaddingBottom() + mContent.getDesiredHeight()
808 + mFolderNameHeight;
Adam Cohen234c4cd2011-07-17 21:03:04 -0700809
810 int contentWidthSpec = MeasureSpec.makeMeasureSpec(mContent.getDesiredWidth(),
811 MeasureSpec.EXACTLY);
812 int contentHeightSpec = MeasureSpec.makeMeasureSpec(mContent.getDesiredHeight(),
813 MeasureSpec.EXACTLY);
814 mContent.measure(contentWidthSpec, contentHeightSpec);
815
816 mFolderName.measure(contentWidthSpec,
817 MeasureSpec.makeMeasureSpec(mFolderNameHeight, MeasureSpec.EXACTLY));
818 setMeasuredDimension(width, height);
819 }
820
Adam Cohen2801caf2011-05-13 20:57:39 -0700821 private void arrangeChildren(ArrayList<View> list) {
822 int[] vacant = new int[2];
823 if (list == null) {
824 list = getItemsInReadingOrder();
825 }
826 mContent.removeAllViews();
827
828 for (int i = 0; i < list.size(); i++) {
829 View v = list.get(i);
830 mContent.getVacantCell(vacant, 1, 1);
831 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) v.getLayoutParams();
832 lp.cellX = vacant[0];
833 lp.cellY = vacant[1];
834 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohen2792a332011-09-26 21:09:47 -0700835 if (info.cellX != vacant[0] || info.cellY != vacant[1]) {
836 info.cellX = vacant[0];
837 info.cellY = vacant[1];
838 LauncherModel.addOrMoveItemInDatabase(mLauncher, info, mInfo.id, 0,
839 info.cellX, info.cellY);
840 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700841 boolean insert = false;
842 mContent.addViewToCellLayout(v, insert ? 0 : -1, (int)info.id, lp, true);
843 }
Adam Cohen7c693212011-05-18 15:26:57 -0700844 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700845 }
846
Adam Cohena9cf38f2011-05-02 15:36:58 -0700847 public int getItemCount() {
848 return mContent.getChildrenLayout().getChildCount();
849 }
850
851 public View getItemAt(int index) {
852 return mContent.getChildrenLayout().getChildAt(index);
853 }
854
Adam Cohen2801caf2011-05-13 20:57:39 -0700855 private void onCloseComplete() {
Adam Cohen05e0f402011-08-01 12:12:49 -0700856 DragLayer parent = (DragLayer) getParent();
Adam Cohen4554ee12011-08-03 16:13:21 -0700857 parent.removeView(this);
858 mDragController.removeDropTarget((DropTarget) this);
Adam Cohen05e0f402011-08-01 12:12:49 -0700859 clearFocus();
Adam Cohenac56cff2011-09-28 20:45:37 -0700860 mFolderIcon.requestFocus();
Adam Cohen05e0f402011-08-01 12:12:49 -0700861
Adam Cohen2801caf2011-05-13 20:57:39 -0700862 if (mRearrangeOnClose) {
863 setupContentForNumItems(getItemCount());
864 mRearrangeOnClose = false;
865 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700866 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700867 if (!mDragInProgress && !mSuppressFolderDeletion) {
868 replaceFolderWithFinalItem();
869 } else if (mDragInProgress) {
870 mDeleteFolderOnDropCompleted = true;
871 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700872 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700873 mSuppressFolderDeletion = false;
Adam Cohenafb01ee2011-06-23 15:38:03 -0700874 }
875
876 private void replaceFolderWithFinalItem() {
877 ItemInfo finalItem = null;
878
879 if (getItemCount() == 1) {
880 finalItem = mInfo.contents.get(0);
881 }
882
883 // Remove the folder completely
Winson Chung3d503fb2011-07-13 17:25:49 -0700884 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screen);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700885 cellLayout.removeView(mFolderIcon);
886 if (mFolderIcon instanceof DropTarget) {
887 mDragController.removeDropTarget((DropTarget) mFolderIcon);
888 }
889 mLauncher.removeFolder(mInfo);
890
891 if (finalItem != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700892 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
893 mInfo.screen, mInfo.cellX, mInfo.cellY);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700894 }
Adam Cohen716b51e2011-06-30 12:09:54 -0700895 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700896
897 // Add the last remaining child to the workspace in place of the folder
898 if (finalItem != null) {
899 View child = mLauncher.createShortcut(R.layout.application, cellLayout,
900 (ShortcutInfo) finalItem);
901
Winson Chung3d503fb2011-07-13 17:25:49 -0700902 mLauncher.getWorkspace().addInScreen(child, mInfo.container, mInfo.screen, mInfo.cellX,
903 mInfo.cellY, mInfo.spanX, mInfo.spanY);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700904 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700905 }
906
Adam Cohenac56cff2011-09-28 20:45:37 -0700907 // This method keeps track of the last item in the folder for the purposes
908 // of keyboard focus
909 private void updateTextViewFocus() {
910 View lastChild = getItemAt(getItemCount() - 1);
911 getItemAt(getItemCount() - 1);
912 if (lastChild != null) {
913 mFolderName.setNextFocusDownId(lastChild.getId());
914 mFolderName.setNextFocusRightId(lastChild.getId());
915 mFolderName.setNextFocusLeftId(lastChild.getId());
916 mFolderName.setNextFocusUpId(lastChild.getId());
917 }
918 }
919
Adam Cohenbfbfd262011-06-13 16:55:12 -0700920 public void onDrop(DragObject d) {
921 ShortcutInfo item;
922 if (d.dragInfo instanceof ApplicationInfo) {
923 // Came from all apps -- make a copy
924 item = ((ApplicationInfo) d.dragInfo).makeShortcut();
925 item.spanX = 1;
926 item.spanY = 1;
927 } else {
928 item = (ShortcutInfo) d.dragInfo;
929 }
Adam Cohen05e0f402011-08-01 12:12:49 -0700930 // Dragged from self onto self, currently this is the only path possible, however
931 // we keep this as a distinct code path.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700932 if (item == mCurrentDragInfo) {
933 ShortcutInfo si = (ShortcutInfo) mCurrentDragView.getTag();
934 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) mCurrentDragView.getLayoutParams();
935 si.cellX = lp.cellX = mEmptyCell[0];
936 si.cellX = lp.cellY = mEmptyCell[1];
937 mContent.addViewToCellLayout(mCurrentDragView, -1, (int)item.id, lp, true);
Adam Cohenfc53cd22011-07-20 15:45:11 -0700938 if (d.dragView.hasDrawn()) {
939 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, mCurrentDragView);
940 } else {
941 mCurrentDragView.setVisibility(VISIBLE);
942 }
Adam Cohene9166b22011-07-08 17:11:11 -0700943 mItemsInvalidated = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700944 setupContentDimension(getItemCount());
Adam Cohen716b51e2011-06-30 12:09:54 -0700945 mSuppressOnAdd = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700946 }
947 mInfo.add(item);
948 }
949
950 public void onAdd(ShortcutInfo item) {
951 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700952 // If the item was dropped onto this open folder, we have done the work associated
953 // with adding the item to the folder, as indicated by mSuppressOnAdd being set
Adam Cohenbfbfd262011-06-13 16:55:12 -0700954 if (mSuppressOnAdd) return;
955 if (!findAndSetEmptyCells(item)) {
956 // The current layout is full, can we expand it?
957 setupContentForNumItems(getItemCount() + 1);
958 findAndSetEmptyCells(item);
959 }
960 createAndAddShortcut(item);
961 LauncherModel.addOrMoveItemInDatabase(
962 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
963 }
964
Adam Cohena9cf38f2011-05-02 15:36:58 -0700965 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -0700966 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700967 // If this item is being dragged from this open folder, we have already handled
968 // the work associated with removing the item, so we don't have to do anything here.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700969 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -0700970 View v = getViewForInfo(item);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700971 mContent.removeView(v);
Adam Cohen2801caf2011-05-13 20:57:39 -0700972 if (mState == STATE_ANIMATING) {
973 mRearrangeOnClose = true;
974 } else {
975 setupContentForNumItems(getItemCount());
976 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700977 if (getItemCount() <= 1) {
978 replaceFolderWithFinalItem();
979 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700980 }
Adam Cohen7c693212011-05-18 15:26:57 -0700981
Adam Cohendf1e4e82011-06-24 15:57:39 -0700982 private View getViewForInfo(ShortcutInfo item) {
983 for (int j = 0; j < mContent.getCountY(); j++) {
984 for (int i = 0; i < mContent.getCountX(); i++) {
985 View v = mContent.getChildAt(i, j);
986 if (v.getTag() == item) {
987 return v;
988 }
989 }
990 }
991 return null;
992 }
993
Adam Cohen76078c42011-06-09 15:06:52 -0700994 public void onItemsChanged() {
Adam Cohenac56cff2011-09-28 20:45:37 -0700995 updateTextViewFocus();
Adam Cohen76078c42011-06-09 15:06:52 -0700996 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700997
Adam Cohen76fc0852011-06-17 13:26:23 -0700998 public void onTitleChanged(CharSequence title) {
999 }
Adam Cohen76078c42011-06-09 15:06:52 -07001000
Adam Cohen7c693212011-05-18 15:26:57 -07001001 public ArrayList<View> getItemsInReadingOrder() {
Adam Cohen76078c42011-06-09 15:06:52 -07001002 return getItemsInReadingOrder(true);
1003 }
1004
1005 public ArrayList<View> getItemsInReadingOrder(boolean includeCurrentDragItem) {
Adam Cohen7c693212011-05-18 15:26:57 -07001006 if (mItemsInvalidated) {
1007 mItemsInReadingOrder.clear();
1008 for (int j = 0; j < mContent.getCountY(); j++) {
1009 for (int i = 0; i < mContent.getCountX(); i++) {
1010 View v = mContent.getChildAt(i, j);
1011 if (v != null) {
Adam Cohen76078c42011-06-09 15:06:52 -07001012 ShortcutInfo info = (ShortcutInfo) v.getTag();
1013 if (info != mCurrentDragInfo || includeCurrentDragItem) {
1014 mItemsInReadingOrder.add(v);
1015 }
Adam Cohen7c693212011-05-18 15:26:57 -07001016 }
1017 }
1018 }
1019 mItemsInvalidated = false;
1020 }
1021 return mItemsInReadingOrder;
1022 }
Adam Cohen8dfcba42011-07-07 16:38:18 -07001023
1024 public void getLocationInDragLayer(int[] loc) {
1025 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
1026 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001027}