blob: a0342cf0648844f8b18343f12f08ab73abf07df2 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Adam Cohendf2cc412011-04-27 16:56:57 -070019import android.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
Adam Cohendf2cc412011-04-27 16:56:57 -070021import android.animation.ObjectAnimator;
22import android.animation.PropertyValuesHolder;
23import android.animation.ValueAnimator;
24import android.animation.ValueAnimator.AnimatorUpdateListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080025import android.content.Context;
Adam Cohen76fc0852011-06-17 13:26:23 -070026import android.content.res.Resources;
Romain Guyfb5411e2010-02-24 10:04:17 -080027import android.graphics.Rect;
Adam Cohenbadf71e2011-05-26 19:08:29 -070028import android.graphics.drawable.Drawable;
Adam Cohen7a14d0b2011-06-24 11:36:35 -070029import android.text.InputType;
Adam Cohene601a432011-07-26 21:51:30 -070030import android.text.Selection;
31import android.text.Spannable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.util.AttributeSet;
Adam Cohen76fc0852011-06-17 13:26:23 -070033import android.view.ActionMode;
34import android.view.KeyEvent;
Adam Cohendf2cc412011-04-27 16:56:57 -070035import android.view.LayoutInflater;
Adam Cohen76fc0852011-06-17 13:26:23 -070036import android.view.Menu;
37import android.view.MenuItem;
Adam Cohen0c872ba2011-05-05 10:34:16 -070038import android.view.MotionEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.view.View;
Adam Cohen2801caf2011-05-13 20:57:39 -070040import android.view.animation.AccelerateInterpolator;
41import android.view.animation.DecelerateInterpolator;
Adam Cohen76fc0852011-06-17 13:26:23 -070042import android.view.inputmethod.EditorInfo;
43import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080044import android.widget.LinearLayout;
Adam Cohendf2cc412011-04-27 16:56:57 -070045import android.widget.TextView;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046
Romain Guyedcce092010-03-04 13:03:17 -080047import com.android.launcher.R;
Adam Cohena9cf38f2011-05-02 15:36:58 -070048import com.android.launcher2.FolderInfo.FolderListener;
Romain Guyedcce092010-03-04 13:03:17 -080049
Adam Cohenc0dcf592011-06-01 15:30:43 -070050import java.util.ArrayList;
51
The Android Open Source Project31dd5032009-03-03 19:32:27 -080052/**
53 * Represents a set of icons chosen by the user or generated by the system.
54 */
Adam Cohen8dfcba42011-07-07 16:38:18 -070055public class Folder extends LinearLayout implements DragSource, View.OnClickListener,
56 View.OnLongClickListener, DropTarget, FolderListener, TextView.OnEditorActionListener {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057
Adam Cohendf2cc412011-04-27 16:56:57 -070058 private static final String TAG = "Launcher.Folder";
59
Adam Cohen4eac29a2011-07-11 17:53:37 -070060 protected DragController mDragController;
61 protected Launcher mLauncher;
62 protected FolderInfo mInfo;
63
Adam Cohendf2cc412011-04-27 16:56:57 -070064 static final int STATE_NONE = -1;
65 static final int STATE_SMALL = 0;
66 static final int STATE_ANIMATING = 1;
67 static final int STATE_OPEN = 2;
68
69 private int mExpandDuration;
70 protected CellLayout mContent;
71 private final LayoutInflater mInflater;
72 private final IconCache mIconCache;
73 private int mState = STATE_NONE;
Adam Cohen2801caf2011-05-13 20:57:39 -070074 private static final int FULL_GROW = 0;
75 private static final int PARTIAL_GROW = 1;
Adam Cohenbfbfd262011-06-13 16:55:12 -070076 private static final int REORDER_ANIMATION_DURATION = 230;
77 private static final int ON_EXIT_CLOSE_DELAY = 800;
Adam Cohen2801caf2011-05-13 20:57:39 -070078 private int mMode = PARTIAL_GROW;
79 private boolean mRearrangeOnClose = false;
80 private FolderIcon mFolderIcon;
81 private int mMaxCountX;
82 private int mMaxCountY;
83 private Rect mNewSize = new Rect();
Adam Cohen3e8f8112011-07-02 18:03:00 -070084 private Rect mIconRect = new Rect();
Adam Cohen7c693212011-05-18 15:26:57 -070085 private ArrayList<View> mItemsInReadingOrder = new ArrayList<View>();
Adam Cohenbadf71e2011-05-26 19:08:29 -070086 private Drawable mIconDrawable;
Adam Cohen7c693212011-05-18 15:26:57 -070087 boolean mItemsInvalidated = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -070088 private ShortcutInfo mCurrentDragInfo;
89 private View mCurrentDragView;
90 boolean mSuppressOnAdd = false;
91 private int[] mTargetCell = new int[2];
92 private int[] mPreviousTargetCell = new int[2];
93 private int[] mEmptyCell = new int[2];
94 private Alarm mReorderAlarm = new Alarm();
95 private Alarm mOnExitAlarm = new Alarm();
Adam Cohen76fc0852011-06-17 13:26:23 -070096 private TextView mFolderName;
97 private int mFolderNameHeight;
Adam Cohen4ef610f2011-06-21 22:37:36 -070098 private Rect mHitRect = new Rect();
Adam Cohen8e776a62011-06-28 18:10:06 -070099 private Rect mTempRect = new Rect();
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700100 private boolean mDragInProgress = false;
101 private boolean mDeleteFolderOnDropCompleted = false;
102 private boolean mSuppressFolderDeletion = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700103 private boolean mItemAddedBackToSelfViaIcon = false;
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
138 mLauncher = (Launcher) context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139 }
140
141 @Override
142 protected void onFinishInflate() {
143 super.onFinishInflate();
Adam Cohendf2cc412011-04-27 16:56:57 -0700144 mContent = (CellLayout) findViewById(R.id.folder_content);
Adam Cohen2801caf2011-05-13 20:57:39 -0700145 mContent.setGridSize(0, 0);
Adam Cohen76fc0852011-06-17 13:26:23 -0700146 mFolderName = (TextView) findViewById(R.id.folder_name);
147
148 // We find out how tall the text view wants to be (it is set to wrap_content), so that
149 // we can allocate the appropriate amount of space for it.
150 int measureSpec = MeasureSpec.UNSPECIFIED;
151 mFolderName.measure(measureSpec, measureSpec);
152 mFolderNameHeight = mFolderName.getMeasuredHeight();
153
154 // We disable action mode for now since it messes up the view on phones
155 mFolderName.setCustomSelectionActionModeCallback(mActionModeCallback);
156 mFolderName.setCursorVisible(false);
157 mFolderName.setOnEditorActionListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700158 mFolderName.setSelectAllOnFocus(true);
Adam Cohen7a14d0b2011-06-24 11:36:35 -0700159 mFolderName.setInputType(mFolderName.getInputType() |
160 InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700162
Adam Cohen76fc0852011-06-17 13:26:23 -0700163 private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
164 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
165 return false;
166 }
167
168 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
169 return false;
170 }
171
172 public void onDestroyActionMode(ActionMode mode) {
173 }
174
175 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
176 return false;
177 }
178 };
179
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180 public void onClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700181 Object tag = v.getTag();
182 if (tag instanceof ShortcutInfo) {
183 // refactor this code from Folder
184 ShortcutInfo item = (ShortcutInfo) tag;
185 int[] pos = new int[2];
186 v.getLocationOnScreen(pos);
187 item.intent.setSourceBounds(new Rect(pos[0], pos[1],
188 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
189 mLauncher.startActivitySafely(item.intent, item);
Adam Cohendf2cc412011-04-27 16:56:57 -0700190 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191 }
192
Adam Cohen76fc0852011-06-17 13:26:23 -0700193 public boolean onInterceptTouchEvent(MotionEvent ev) {
194 if (ev.getAction() == MotionEvent.ACTION_DOWN) {
195 mFolderName.getHitRect(mHitRect);
196 if (mHitRect.contains((int) ev.getX(), (int) ev.getY()) && !mIsEditingName) {
197 startEditingFolderName();
198 }
199 }
200 return false;
201 }
202
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800203 public boolean onLongClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700204 Object tag = v.getTag();
205 if (tag instanceof ShortcutInfo) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700206 ShortcutInfo item = (ShortcutInfo) tag;
207 if (!v.isInTouchMode()) {
208 return false;
209 }
210
211 mLauncher.getWorkspace().onDragStartedWithItem(v);
Adam Cohenac8c8762011-07-13 11:15:27 -0700212 mLauncher.getWorkspace().beginDragShared(v, this);
Adam Cohenbadf71e2011-05-26 19:08:29 -0700213 mIconDrawable = ((TextView) v).getCompoundDrawables()[1];
Adam Cohen76078c42011-06-09 15:06:52 -0700214
215 mCurrentDragInfo = item;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700216 mEmptyCell[0] = item.cellX;
217 mEmptyCell[1] = item.cellY;
218 mCurrentDragView = v;
Adam Cohenfc53cd22011-07-20 15:45:11 -0700219
220 mContent.removeView(mCurrentDragView);
221 mInfo.remove(mCurrentDragInfo);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700222 mDragInProgress = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700223 mItemAddedBackToSelfViaIcon = false;
Adam Cohendf2cc412011-04-27 16:56:57 -0700224 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800225 return true;
226 }
227
Adam Cohen76fc0852011-06-17 13:26:23 -0700228 public boolean isEditingName() {
229 return mIsEditingName;
230 }
231
232 public void startEditingFolderName() {
Adam Cohena65beee2011-06-27 21:32:23 -0700233 mFolderName.setHint("");
Adam Cohen76fc0852011-06-17 13:26:23 -0700234 mFolderName.setCursorVisible(true);
235 mIsEditingName = true;
236 }
237
238 public void dismissEditingName() {
239 mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
240 doneEditingFolderName(true);
241 }
242
243 public void doneEditingFolderName(boolean commit) {
Adam Cohena65beee2011-06-27 21:32:23 -0700244 mFolderName.setHint(sHintText);
Adam Cohen1df26a32011-08-12 16:15:23 -0700245 // Convert to a string here to ensure that no other state associated with the text field
246 // gets saved.
247 mInfo.setTitle(mFolderName.getText().toString());
Adam Cohen76fc0852011-06-17 13:26:23 -0700248 LauncherModel.updateItemInDatabase(mLauncher, mInfo);
249 mFolderName.setCursorVisible(false);
250 mFolderName.clearFocus();
Adam Cohene601a432011-07-26 21:51:30 -0700251 Selection.setSelection((Spannable) mFolderName.getText(), 0, 0);
Adam Cohen76fc0852011-06-17 13:26:23 -0700252 mIsEditingName = false;
253 }
254
255 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
256 if (actionId == EditorInfo.IME_ACTION_DONE) {
257 dismissEditingName();
258 return true;
259 }
260 return false;
261 }
262
263 public View getEditTextRegion() {
264 return mFolderName;
265 }
266
Adam Cohenbadf71e2011-05-26 19:08:29 -0700267 public Drawable getDragDrawable() {
268 return mIconDrawable;
269 }
270
Adam Cohen0c872ba2011-05-05 10:34:16 -0700271 /**
272 * We need to handle touch events to prevent them from falling through to the workspace below.
273 */
274 @Override
275 public boolean onTouchEvent(MotionEvent ev) {
276 return true;
277 }
278
Joe Onorato00acb122009-08-04 16:04:30 -0400279 public void setDragController(DragController dragController) {
280 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800281 }
282
Adam Cohen2801caf2011-05-13 20:57:39 -0700283 void setFolderIcon(FolderIcon icon) {
284 mFolderIcon = icon;
285 }
286
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800287 /**
288 * @return the FolderInfo object associated with this folder
289 */
290 FolderInfo getInfo() {
291 return mInfo;
292 }
293
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800294 void bind(FolderInfo info) {
295 mInfo = info;
Adam Cohendf2cc412011-04-27 16:56:57 -0700296 ArrayList<ShortcutInfo> children = info.contents;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700297 ArrayList<ShortcutInfo> overflow = new ArrayList<ShortcutInfo>();
Adam Cohen7c693212011-05-18 15:26:57 -0700298 setupContentForNumItems(children.size());
Adam Cohen0057bbc2011-08-12 18:30:51 -0700299 int count = 0;
Adam Cohendf2cc412011-04-27 16:56:57 -0700300 for (int i = 0; i < children.size(); i++) {
301 ShortcutInfo child = (ShortcutInfo) children.get(i);
Adam Cohenc508b2d2011-06-28 14:41:44 -0700302 if (!createAndAddShortcut(child)) {
303 overflow.add(child);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700304 } else {
305 count++;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700306 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700307 }
Adam Cohenc508b2d2011-06-28 14:41:44 -0700308
Adam Cohen0057bbc2011-08-12 18:30:51 -0700309 // We rearrange the items in case there are any empty gaps
310 setupContentForNumItems(count);
311
Adam Cohenc508b2d2011-06-28 14:41:44 -0700312 // If our folder has too many items we prune them from the list. This is an issue
313 // when upgrading from the old Folders implementation which could contain an unlimited
314 // number of items.
315 for (ShortcutInfo item: overflow) {
316 mInfo.remove(item);
317 LauncherModel.deleteItemFromDatabase(mLauncher, item);
318 }
319
Adam Cohen4dbe6d92011-05-18 17:14:15 -0700320 mItemsInvalidated = true;
Adam Cohena9cf38f2011-05-02 15:36:58 -0700321 mInfo.addListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700322
Adam Cohenafb01ee2011-06-23 15:38:03 -0700323 if (!sDefaultFolderName.contentEquals(mInfo.title)) {
Adam Cohen4ef610f2011-06-21 22:37:36 -0700324 mFolderName.setText(mInfo.title);
325 } else {
326 mFolderName.setText("");
327 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700328 }
329
330 /**
331 * Creates a new UserFolder, inflated from R.layout.user_folder.
332 *
333 * @param context The application's context.
334 *
335 * @return A new UserFolder.
336 */
337 static Folder fromXml(Context context) {
338 return (Folder) LayoutInflater.from(context).inflate(R.layout.user_folder, null);
339 }
340
341 /**
342 * This method is intended to make the UserFolder to be visually identical in size and position
343 * to its associated FolderIcon. This allows for a seamless transition into the expanded state.
344 */
345 private void positionAndSizeAsIcon() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700346 if (!(getParent() instanceof DragLayer)) return;
Adam Cohendf2cc412011-04-27 16:56:57 -0700347
Adam Cohen8e776a62011-06-28 18:10:06 -0700348 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohendf2cc412011-04-27 16:56:57 -0700349
Adam Cohen2801caf2011-05-13 20:57:39 -0700350 if (mMode == PARTIAL_GROW) {
351 setScaleX(0.8f);
352 setScaleY(0.8f);
353 setAlpha(0f);
354 } else {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700355 mLauncher.getDragLayer().getDescendantRectRelativeToSelf(mFolderIcon, mIconRect);
356 lp.width = mIconRect.width();
357 lp.height = mIconRect.height();
358 lp.x = mIconRect.left;
359 lp.y = mIconRect.top;
Adam Cohen2801caf2011-05-13 20:57:39 -0700360 mContent.setAlpha(0);
361 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700362 mState = STATE_SMALL;
363 }
364
365 public void animateOpen() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700366 positionAndSizeAsIcon();
367
Adam Cohen8e776a62011-06-28 18:10:06 -0700368 if (!(getParent() instanceof DragLayer)) return;
Adam Cohendf2cc412011-04-27 16:56:57 -0700369
Adam Cohen2801caf2011-05-13 20:57:39 -0700370 ObjectAnimator oa;
Adam Cohen8e776a62011-06-28 18:10:06 -0700371 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohendf2cc412011-04-27 16:56:57 -0700372
Adam Cohen2801caf2011-05-13 20:57:39 -0700373 centerAboutIcon();
374 if (mMode == PARTIAL_GROW) {
375 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1);
376 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
377 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
378 oa = ObjectAnimator.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
379 } else {
380 PropertyValuesHolder width = PropertyValuesHolder.ofInt("width", mNewSize.width());
381 PropertyValuesHolder height = PropertyValuesHolder.ofInt("height", mNewSize.height());
382 PropertyValuesHolder x = PropertyValuesHolder.ofInt("x", mNewSize.left);
383 PropertyValuesHolder y = PropertyValuesHolder.ofInt("y", mNewSize.top);
384 oa = ObjectAnimator.ofPropertyValuesHolder(lp, width, height, x, y);
385 oa.addUpdateListener(new AnimatorUpdateListener() {
386 public void onAnimationUpdate(ValueAnimator animation) {
387 requestLayout();
388 }
389 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700390
Adam Cohen2801caf2011-05-13 20:57:39 -0700391 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
392 ObjectAnimator alphaOa = ObjectAnimator.ofPropertyValuesHolder(mContent, alpha);
393 alphaOa.setDuration(mExpandDuration);
394 alphaOa.setInterpolator(new AccelerateInterpolator(2.0f));
395 alphaOa.start();
396 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700397
Adam Cohen2801caf2011-05-13 20:57:39 -0700398 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700399 @Override
400 public void onAnimationStart(Animator animation) {
401 mState = STATE_ANIMATING;
402 }
403 @Override
404 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700405 mState = STATE_OPEN;
Adam Cohendf2cc412011-04-27 16:56:57 -0700406 }
407 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700408 oa.setDuration(mExpandDuration);
409 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700410 }
411
412 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700413 if (!(getParent() instanceof DragLayer)) return;
Adam Cohendf2cc412011-04-27 16:56:57 -0700414
Adam Cohen2801caf2011-05-13 20:57:39 -0700415 ObjectAnimator oa;
Adam Cohen2801caf2011-05-13 20:57:39 -0700416 if (mMode == PARTIAL_GROW) {
417 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
418 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
419 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
420 oa = ObjectAnimator.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
421 } else {
Adam Cohen8e776a62011-06-28 18:10:06 -0700422 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohendf2cc412011-04-27 16:56:57 -0700423
Adam Cohen3e8f8112011-07-02 18:03:00 -0700424 PropertyValuesHolder width = PropertyValuesHolder.ofInt("width", mIconRect.width());
425 PropertyValuesHolder height = PropertyValuesHolder.ofInt("height", mIconRect.height());
426 PropertyValuesHolder x = PropertyValuesHolder.ofInt("x", mIconRect.left);
427 PropertyValuesHolder y = PropertyValuesHolder.ofInt("y", mIconRect.top);
Adam Cohen2801caf2011-05-13 20:57:39 -0700428 oa = ObjectAnimator.ofPropertyValuesHolder(lp, width, height, x, y);
429 oa.addUpdateListener(new AnimatorUpdateListener() {
430 public void onAnimationUpdate(ValueAnimator animation) {
431 requestLayout();
432 }
433 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700434
Adam Cohen2801caf2011-05-13 20:57:39 -0700435 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0f);
436 ObjectAnimator alphaOa = ObjectAnimator.ofPropertyValuesHolder(mContent, alpha);
437 alphaOa.setDuration(mExpandDuration);
438 alphaOa.setInterpolator(new DecelerateInterpolator(2.0f));
439 alphaOa.start();
440 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700441
Adam Cohen2801caf2011-05-13 20:57:39 -0700442 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700443 @Override
444 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700445 onCloseComplete();
Adam Cohen2801caf2011-05-13 20:57:39 -0700446 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700447 }
448 @Override
449 public void onAnimationStart(Animator animation) {
450 mState = STATE_ANIMATING;
451 }
452 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700453 oa.setDuration(mExpandDuration);
454 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700455 }
456
457 void notifyDataSetChanged() {
458 // recreate all the children if the data set changes under us. We may want to do this more
459 // intelligently (ie just removing the views that should no longer exist)
460 mContent.removeAllViewsInLayout();
461 bind(mInfo);
462 }
463
Adam Cohencb3382b2011-05-24 14:07:08 -0700464 public boolean acceptDrop(DragObject d) {
465 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700466 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700467 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
468 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
469 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700470 }
471
Adam Cohendf2cc412011-04-27 16:56:57 -0700472 protected boolean findAndSetEmptyCells(ShortcutInfo item) {
473 int[] emptyCell = new int[2];
474 if (mContent.findCellForSpan(emptyCell, item.spanX, item.spanY)) {
475 item.cellX = emptyCell[0];
476 item.cellY = emptyCell[1];
Adam Cohendf2cc412011-04-27 16:56:57 -0700477 return true;
478 } else {
479 return false;
480 }
481 }
482
Adam Cohenc508b2d2011-06-28 14:41:44 -0700483 protected boolean createAndAddShortcut(ShortcutInfo item) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700484 final TextView textView =
Adam Cohene87b9242011-06-29 14:01:26 -0700485 (TextView) mInflater.inflate(R.layout.application, this, false);
Adam Cohendf2cc412011-04-27 16:56:57 -0700486 textView.setCompoundDrawablesWithIntrinsicBounds(null,
487 new FastBitmapDrawable(item.getIcon(mIconCache)), null, null);
488 textView.setText(item.title);
489 textView.setTag(item);
490
491 textView.setOnClickListener(this);
492 textView.setOnLongClickListener(this);
493
Adam Cohenc508b2d2011-06-28 14:41:44 -0700494 // We need to check here to verify that the given item's location isn't already occupied
495 // by another item. If it is, we need to find the next available slot and assign
496 // it that position. This is an issue when upgrading from the old Folders implementation
497 // which could contain an unlimited number of items.
Adam Cohen0057bbc2011-08-12 18:30:51 -0700498 if (mContent.getChildAt(item.cellX, item.cellY) != null || item.cellX < 0 || item.cellY < 0
499 || item.cellX >= mContent.getCountX() || item.cellY >= mContent.getCountY()) {
Adam Cohenc508b2d2011-06-28 14:41:44 -0700500 if (!findAndSetEmptyCells(item)) {
501 return false;
502 }
503 }
504
Adam Cohendf2cc412011-04-27 16:56:57 -0700505 CellLayout.LayoutParams lp =
506 new CellLayout.LayoutParams(item.cellX, item.cellY, item.spanX, item.spanY);
507 boolean insert = false;
508 mContent.addViewToCellLayout(textView, insert ? 0 : -1, (int)item.id, lp, true);
Adam Cohenc508b2d2011-06-28 14:41:44 -0700509 return true;
Adam Cohendf2cc412011-04-27 16:56:57 -0700510 }
511
Adam Cohencb3382b2011-05-24 14:07:08 -0700512 public void onDragEnter(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700513 mPreviousTargetCell[0] = -1;
514 mPreviousTargetCell[1] = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700515 mOnExitAlarm.cancelAlarm();
516 }
517
518 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
519 public void onAlarm(Alarm alarm) {
520 realTimeReorder(mEmptyCell, mTargetCell);
521 }
522 };
523
524 boolean readingOrderGreaterThan(int[] v1, int[] v2) {
525 if (v1[1] > v2[1] || (v1[1] == v2[1] && v1[0] > v2[0])) {
526 return true;
527 } else {
528 return false;
529 }
530 }
531
532 private void realTimeReorder(int[] empty, int[] target) {
533 boolean wrap;
534 int startX;
535 int endX;
536 int startY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700537 int delay = 0;
538 float delayAmount = 30;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700539 if (readingOrderGreaterThan(target, empty)) {
540 wrap = empty[0] >= mContent.getCountX() - 1;
541 startY = wrap ? empty[1] + 1 : empty[1];
542 for (int y = startY; y <= target[1]; y++) {
543 startX = y == empty[1] ? empty[0] + 1 : 0;
544 endX = y < target[1] ? mContent.getCountX() - 1 : target[0];
545 for (int x = startX; x <= endX; x++) {
546 View v = mContent.getChildAt(x,y);
547 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen76fc0852011-06-17 13:26:23 -0700548 REORDER_ANIMATION_DURATION, delay)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700549 empty[0] = x;
550 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700551 delay += delayAmount;
552 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700553 }
554 }
555 }
556 } else {
557 wrap = empty[0] == 0;
558 startY = wrap ? empty[1] - 1 : empty[1];
559 for (int y = startY; y >= target[1]; y--) {
560 startX = y == empty[1] ? empty[0] - 1 : mContent.getCountX() - 1;
561 endX = y > target[1] ? 0 : target[0];
562 for (int x = startX; x >= endX; x--) {
563 View v = mContent.getChildAt(x,y);
564 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen76fc0852011-06-17 13:26:23 -0700565 REORDER_ANIMATION_DURATION, delay)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700566 empty[0] = x;
567 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700568 delay += delayAmount;
569 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700570 }
571 }
572 }
573 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700574 }
575
Adam Cohencb3382b2011-05-24 14:07:08 -0700576 public void onDragOver(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700577 float[] r = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, d.dragView, null);
578 mTargetCell = mContent.findNearestArea((int) r[0], (int) r[1], 1, 1, mTargetCell);
579
580 if (mTargetCell[0] != mPreviousTargetCell[0] || mTargetCell[1] != mPreviousTargetCell[1]) {
581 mReorderAlarm.cancelAlarm();
582 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
583 mReorderAlarm.setAlarm(150);
584 mPreviousTargetCell[0] = mTargetCell[0];
585 mPreviousTargetCell[1] = mTargetCell[1];
586 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700587 }
588
Adam Cohenbfbfd262011-06-13 16:55:12 -0700589 // This is used to compute the visual center of the dragView. The idea is that
590 // the visual center represents the user's interpretation of where the item is, and hence
591 // is the appropriate point to use when determining drop location.
592 private float[] getDragViewVisualCenter(int x, int y, int xOffset, int yOffset,
593 DragView dragView, float[] recycle) {
594 float res[];
595 if (recycle == null) {
596 res = new float[2];
597 } else {
598 res = recycle;
599 }
600
601 // These represent the visual top and left of drag view if a dragRect was provided.
602 // If a dragRect was not provided, then they correspond to the actual view left and
603 // top, as the dragRect is in that case taken to be the entire dragView.
604 // R.dimen.dragViewOffsetY.
605 int left = x - xOffset;
606 int top = y - yOffset;
607
608 // In order to find the visual center, we shift by half the dragRect
609 res[0] = left + dragView.getDragRegion().width() / 2;
610 res[1] = top + dragView.getDragRegion().height() / 2;
611
612 return res;
613 }
614
615 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
616 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700617 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700618 }
619 };
620
Adam Cohen95bb8002011-07-03 23:40:28 -0700621 public void completeDragExit() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700622 mLauncher.closeFolder();
623 mCurrentDragInfo = null;
624 mCurrentDragView = null;
625 mSuppressOnAdd = false;
626 mRearrangeOnClose = true;
627 }
628
Adam Cohencb3382b2011-05-24 14:07:08 -0700629 public void onDragExit(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700630 // We only close the folder if this is a true drag exit, ie. not because a drop
631 // has occurred above the folder.
632 if (!d.dragComplete) {
633 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
634 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
635 }
636 mReorderAlarm.cancelAlarm();
Adam Cohen2801caf2011-05-13 20:57:39 -0700637 }
638
Adam Cohenc0dcf592011-06-01 15:30:43 -0700639 public void onDropCompleted(View target, DragObject d, boolean success) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700640 if (success) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700641 if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700642 replaceFolderWithFinalItem();
643 }
644 } else {
645 // The drag failed, we need to return the item to the folder
646 mFolderIcon.onDrop(d);
647
648 // We're going to trigger a "closeFolder" which may occur before this item has
649 // been added back to the folder -- this could cause the folder to be deleted
650 if (mOnExitAlarm.alarmPending()) {
651 mSuppressFolderDeletion = true;
652 }
653 }
654
655 if (target != this) {
656 if (mOnExitAlarm.alarmPending()) {
657 mOnExitAlarm.cancelAlarm();
658 completeDragExit();
659 }
660 }
661 mDeleteFolderOnDropCompleted = false;
662 mDragInProgress = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700663 mItemAddedBackToSelfViaIcon = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700664 mCurrentDragInfo = null;
665 mCurrentDragView = null;
666 mSuppressOnAdd = false;
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700667 }
Adam Cohen228da5a2011-07-27 22:23:47 -0700668
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700669 public void notifyDrop() {
670 if (mDragInProgress) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700671 mItemAddedBackToSelfViaIcon = true;
Adam Cohen76078c42011-06-09 15:06:52 -0700672 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700673 }
674
675 public boolean isDropEnabled() {
676 return true;
677 }
678
Adam Cohencb3382b2011-05-24 14:07:08 -0700679 public DropTarget getDropTargetDelegate(DragObject d) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700680 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800681 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700682
Adam Cohen2801caf2011-05-13 20:57:39 -0700683 private void setupContentDimension(int count) {
684 ArrayList<View> list = getItemsInReadingOrder();
685
686 int countX = mContent.getCountX();
687 int countY = mContent.getCountY();
Adam Cohen7c693212011-05-18 15:26:57 -0700688 boolean done = false;
Adam Cohen2801caf2011-05-13 20:57:39 -0700689
Adam Cohen7c693212011-05-18 15:26:57 -0700690 while (!done) {
691 int oldCountX = countX;
692 int oldCountY = countY;
693 if (countX * countY < count) {
694 // Current grid is too small, expand it
695 if (countX <= countY && countX < mMaxCountX) {
696 countX++;
697 } else if (countY < mMaxCountY) {
698 countY++;
699 }
700 if (countY == 0) countY++;
701 } else if ((countY - 1) * countX >= count && countY >= countX) {
702 countY = Math.max(0, countY - 1);
703 } else if ((countX - 1) * countY >= count) {
704 countX = Math.max(0, countX - 1);
Adam Cohen2801caf2011-05-13 20:57:39 -0700705 }
Adam Cohen7c693212011-05-18 15:26:57 -0700706 done = countX == oldCountX && countY == oldCountY;
Adam Cohen2801caf2011-05-13 20:57:39 -0700707 }
Adam Cohen7c693212011-05-18 15:26:57 -0700708 mContent.setGridSize(countX, countY);
Adam Cohen2801caf2011-05-13 20:57:39 -0700709 arrangeChildren(list);
710 }
711
712 public boolean isFull() {
713 return getItemCount() >= mMaxCountX * mMaxCountY;
714 }
715
716 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700717 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700718
719 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf4bb1cd2011-07-22 14:36:03 -0700720 int height = getPaddingTop() + getPaddingBottom() + mContent.getDesiredHeight()
721 + mFolderNameHeight;
Adam Cohen8e776a62011-06-28 18:10:06 -0700722 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700723
Adam Cohen8e776a62011-06-28 18:10:06 -0700724 parent.getDescendantRectRelativeToSelf(mFolderIcon, mTempRect);
725
726 int centerX = mTempRect.centerX();
727 int centerY = mTempRect.centerY();
Adam Cohen2801caf2011-05-13 20:57:39 -0700728 int centeredLeft = centerX - width / 2;
729 int centeredTop = centerY - height / 2;
730
Adam Cohen35e7e642011-07-17 14:47:18 -0700731 // We first fetch the currently visible CellLayoutChildren
Winson Chung3d503fb2011-07-13 17:25:49 -0700732 CellLayout currentPage = mLauncher.getWorkspace().getCurrentDropLayout();
Adam Cohen35e7e642011-07-17 14:47:18 -0700733 CellLayoutChildren boundingLayout = currentPage.getChildrenLayout();
734 Rect bounds = new Rect();
735 parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
Adam Cohen2801caf2011-05-13 20:57:39 -0700736
Adam Cohen35e7e642011-07-17 14:47:18 -0700737 // We need to bound the folder to the currently visible CellLayoutChildren
738 int left = Math.min(Math.max(bounds.left, centeredLeft),
739 bounds.left + bounds.width() - width);
740 int top = Math.min(Math.max(bounds.top, centeredTop),
741 bounds.top + bounds.height() - height);
742 // If the folder doesn't fit within the bounds, center it about the desired bounds
743 if (width >= bounds.width()) {
744 left = bounds.left + (bounds.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700745 }
Adam Cohen35e7e642011-07-17 14:47:18 -0700746 if (height >= bounds.height()) {
747 top = bounds.top + (bounds.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700748 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700749
750 int folderPivotX = width / 2 + (centeredLeft - left);
751 int folderPivotY = height / 2 + (centeredTop - top);
752 setPivotX(folderPivotX);
753 setPivotY(folderPivotY);
754 int folderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
755 (1.0f * folderPivotX / width));
756 int folderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
757 (1.0f * folderPivotY / height));
758 mFolderIcon.setPivotX(folderIconPivotX);
759 mFolderIcon.setPivotY(folderIconPivotY);
760
761 if (mMode == PARTIAL_GROW) {
762 lp.width = width;
763 lp.height = height;
764 lp.x = left;
765 lp.y = top;
766 } else {
767 mNewSize.set(left, top, left + width, top + height);
768 }
769 }
770
771 private void setupContentForNumItems(int count) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700772 setupContentDimension(count);
773
Adam Cohen8e776a62011-06-28 18:10:06 -0700774 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700775 if (lp == null) {
Adam Cohen8e776a62011-06-28 18:10:06 -0700776 lp = new DragLayer.LayoutParams(0, 0);
777 lp.customPosition = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700778 setLayoutParams(lp);
779 }
780 centerAboutIcon();
781 }
782
Adam Cohen234c4cd2011-07-17 21:03:04 -0700783 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
784 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf4bb1cd2011-07-22 14:36:03 -0700785 int height = getPaddingTop() + getPaddingBottom() + mContent.getDesiredHeight()
786 + mFolderNameHeight;
Adam Cohen234c4cd2011-07-17 21:03:04 -0700787
788 int contentWidthSpec = MeasureSpec.makeMeasureSpec(mContent.getDesiredWidth(),
789 MeasureSpec.EXACTLY);
790 int contentHeightSpec = MeasureSpec.makeMeasureSpec(mContent.getDesiredHeight(),
791 MeasureSpec.EXACTLY);
792 mContent.measure(contentWidthSpec, contentHeightSpec);
793
794 mFolderName.measure(contentWidthSpec,
795 MeasureSpec.makeMeasureSpec(mFolderNameHeight, MeasureSpec.EXACTLY));
796 setMeasuredDimension(width, height);
797 }
798
Adam Cohen2801caf2011-05-13 20:57:39 -0700799 private void arrangeChildren(ArrayList<View> list) {
800 int[] vacant = new int[2];
801 if (list == null) {
802 list = getItemsInReadingOrder();
803 }
804 mContent.removeAllViews();
805
806 for (int i = 0; i < list.size(); i++) {
807 View v = list.get(i);
808 mContent.getVacantCell(vacant, 1, 1);
809 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) v.getLayoutParams();
810 lp.cellX = vacant[0];
811 lp.cellY = vacant[1];
812 ItemInfo info = (ItemInfo) v.getTag();
813 info.cellX = vacant[0];
814 info.cellY = vacant[1];
815 boolean insert = false;
816 mContent.addViewToCellLayout(v, insert ? 0 : -1, (int)info.id, lp, true);
Adam Cohen7c693212011-05-18 15:26:57 -0700817 LauncherModel.addOrMoveItemInDatabase(mLauncher, info, mInfo.id, 0,
818 info.cellX, info.cellY);
Adam Cohen2801caf2011-05-13 20:57:39 -0700819 }
Adam Cohen7c693212011-05-18 15:26:57 -0700820 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700821 }
822
Adam Cohena9cf38f2011-05-02 15:36:58 -0700823 public int getItemCount() {
824 return mContent.getChildrenLayout().getChildCount();
825 }
826
827 public View getItemAt(int index) {
828 return mContent.getChildrenLayout().getChildAt(index);
829 }
830
Adam Cohen2801caf2011-05-13 20:57:39 -0700831 private void onCloseComplete() {
Adam Cohen05e0f402011-08-01 12:12:49 -0700832 DragLayer parent = (DragLayer) getParent();
Adam Cohen4554ee12011-08-03 16:13:21 -0700833 parent.removeView(this);
834 mDragController.removeDropTarget((DropTarget) this);
Adam Cohen05e0f402011-08-01 12:12:49 -0700835 clearFocus();
836
Adam Cohen2801caf2011-05-13 20:57:39 -0700837 if (mRearrangeOnClose) {
838 setupContentForNumItems(getItemCount());
839 mRearrangeOnClose = false;
840 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700841 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700842 if (!mDragInProgress && !mSuppressFolderDeletion) {
843 replaceFolderWithFinalItem();
844 } else if (mDragInProgress) {
845 mDeleteFolderOnDropCompleted = true;
846 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700847 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700848 mSuppressFolderDeletion = false;
Adam Cohenafb01ee2011-06-23 15:38:03 -0700849 }
850
851 private void replaceFolderWithFinalItem() {
852 ItemInfo finalItem = null;
853
854 if (getItemCount() == 1) {
855 finalItem = mInfo.contents.get(0);
856 }
857
858 // Remove the folder completely
Winson Chung3d503fb2011-07-13 17:25:49 -0700859 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screen);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700860 cellLayout.removeView(mFolderIcon);
861 if (mFolderIcon instanceof DropTarget) {
862 mDragController.removeDropTarget((DropTarget) mFolderIcon);
863 }
864 mLauncher.removeFolder(mInfo);
865
866 if (finalItem != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700867 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
868 mInfo.screen, mInfo.cellX, mInfo.cellY);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700869 }
Adam Cohen716b51e2011-06-30 12:09:54 -0700870 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700871
872 // Add the last remaining child to the workspace in place of the folder
873 if (finalItem != null) {
874 View child = mLauncher.createShortcut(R.layout.application, cellLayout,
875 (ShortcutInfo) finalItem);
876
Winson Chung3d503fb2011-07-13 17:25:49 -0700877 mLauncher.getWorkspace().addInScreen(child, mInfo.container, mInfo.screen, mInfo.cellX,
878 mInfo.cellY, mInfo.spanX, mInfo.spanY);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700879 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700880 }
881
Adam Cohenbfbfd262011-06-13 16:55:12 -0700882 public void onDrop(DragObject d) {
883 ShortcutInfo item;
884 if (d.dragInfo instanceof ApplicationInfo) {
885 // Came from all apps -- make a copy
886 item = ((ApplicationInfo) d.dragInfo).makeShortcut();
887 item.spanX = 1;
888 item.spanY = 1;
889 } else {
890 item = (ShortcutInfo) d.dragInfo;
891 }
Adam Cohen05e0f402011-08-01 12:12:49 -0700892 // Dragged from self onto self, currently this is the only path possible, however
893 // we keep this as a distinct code path.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700894 if (item == mCurrentDragInfo) {
895 ShortcutInfo si = (ShortcutInfo) mCurrentDragView.getTag();
896 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) mCurrentDragView.getLayoutParams();
897 si.cellX = lp.cellX = mEmptyCell[0];
898 si.cellX = lp.cellY = mEmptyCell[1];
899 mContent.addViewToCellLayout(mCurrentDragView, -1, (int)item.id, lp, true);
Adam Cohenfc53cd22011-07-20 15:45:11 -0700900 if (d.dragView.hasDrawn()) {
901 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, mCurrentDragView);
902 } else {
903 mCurrentDragView.setVisibility(VISIBLE);
904 }
Adam Cohene9166b22011-07-08 17:11:11 -0700905 mItemsInvalidated = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700906 setupContentDimension(getItemCount());
Adam Cohen716b51e2011-06-30 12:09:54 -0700907 mSuppressOnAdd = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700908 }
909 mInfo.add(item);
910 }
911
912 public void onAdd(ShortcutInfo item) {
913 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700914 // If the item was dropped onto this open folder, we have done the work associated
915 // with adding the item to the folder, as indicated by mSuppressOnAdd being set
Adam Cohenbfbfd262011-06-13 16:55:12 -0700916 if (mSuppressOnAdd) return;
917 if (!findAndSetEmptyCells(item)) {
918 // The current layout is full, can we expand it?
919 setupContentForNumItems(getItemCount() + 1);
920 findAndSetEmptyCells(item);
921 }
922 createAndAddShortcut(item);
923 LauncherModel.addOrMoveItemInDatabase(
924 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
925 }
926
Adam Cohena9cf38f2011-05-02 15:36:58 -0700927 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -0700928 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700929 // If this item is being dragged from this open folder, we have already handled
930 // the work associated with removing the item, so we don't have to do anything here.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700931 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -0700932 View v = getViewForInfo(item);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700933 mContent.removeView(v);
Adam Cohen2801caf2011-05-13 20:57:39 -0700934 if (mState == STATE_ANIMATING) {
935 mRearrangeOnClose = true;
936 } else {
937 setupContentForNumItems(getItemCount());
938 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700939 if (getItemCount() <= 1) {
940 replaceFolderWithFinalItem();
941 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700942 }
Adam Cohen7c693212011-05-18 15:26:57 -0700943
Adam Cohendf1e4e82011-06-24 15:57:39 -0700944 private View getViewForInfo(ShortcutInfo item) {
945 for (int j = 0; j < mContent.getCountY(); j++) {
946 for (int i = 0; i < mContent.getCountX(); i++) {
947 View v = mContent.getChildAt(i, j);
948 if (v.getTag() == item) {
949 return v;
950 }
951 }
952 }
953 return null;
954 }
955
Adam Cohen76078c42011-06-09 15:06:52 -0700956 public void onItemsChanged() {
957 }
Adam Cohen76fc0852011-06-17 13:26:23 -0700958 public void onTitleChanged(CharSequence title) {
959 }
Adam Cohen76078c42011-06-09 15:06:52 -0700960
Adam Cohen7c693212011-05-18 15:26:57 -0700961 public ArrayList<View> getItemsInReadingOrder() {
Adam Cohen76078c42011-06-09 15:06:52 -0700962 return getItemsInReadingOrder(true);
963 }
964
965 public ArrayList<View> getItemsInReadingOrder(boolean includeCurrentDragItem) {
Adam Cohen7c693212011-05-18 15:26:57 -0700966 if (mItemsInvalidated) {
967 mItemsInReadingOrder.clear();
968 for (int j = 0; j < mContent.getCountY(); j++) {
969 for (int i = 0; i < mContent.getCountX(); i++) {
970 View v = mContent.getChildAt(i, j);
971 if (v != null) {
Adam Cohen76078c42011-06-09 15:06:52 -0700972 ShortcutInfo info = (ShortcutInfo) v.getTag();
973 if (info != mCurrentDragInfo || includeCurrentDragItem) {
974 mItemsInReadingOrder.add(v);
975 }
Adam Cohen7c693212011-05-18 15:26:57 -0700976 }
977 }
978 }
979 mItemsInvalidated = false;
980 }
981 return mItemsInReadingOrder;
982 }
Adam Cohen8dfcba42011-07-07 16:38:18 -0700983
984 public void getLocationInDragLayer(int[] loc) {
985 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
986 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800987}