blob: a1aa670c5113464567b8bab76b2c813efef5f59f [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
Winson Chung7d7541e2011-09-16 20:14:36 -0700211 mLauncher.dismissFolderCling(null);
212
Adam Cohendf2cc412011-04-27 16:56:57 -0700213 mLauncher.getWorkspace().onDragStartedWithItem(v);
Adam Cohenac8c8762011-07-13 11:15:27 -0700214 mLauncher.getWorkspace().beginDragShared(v, this);
Adam Cohenbadf71e2011-05-26 19:08:29 -0700215 mIconDrawable = ((TextView) v).getCompoundDrawables()[1];
Adam Cohen76078c42011-06-09 15:06:52 -0700216
217 mCurrentDragInfo = item;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700218 mEmptyCell[0] = item.cellX;
219 mEmptyCell[1] = item.cellY;
220 mCurrentDragView = v;
Adam Cohenfc53cd22011-07-20 15:45:11 -0700221
222 mContent.removeView(mCurrentDragView);
223 mInfo.remove(mCurrentDragInfo);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700224 mDragInProgress = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700225 mItemAddedBackToSelfViaIcon = false;
Adam Cohendf2cc412011-04-27 16:56:57 -0700226 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800227 return true;
228 }
229
Adam Cohen76fc0852011-06-17 13:26:23 -0700230 public boolean isEditingName() {
231 return mIsEditingName;
232 }
233
234 public void startEditingFolderName() {
Adam Cohena65beee2011-06-27 21:32:23 -0700235 mFolderName.setHint("");
Adam Cohen76fc0852011-06-17 13:26:23 -0700236 mFolderName.setCursorVisible(true);
237 mIsEditingName = true;
238 }
239
240 public void dismissEditingName() {
241 mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
242 doneEditingFolderName(true);
243 }
244
245 public void doneEditingFolderName(boolean commit) {
Adam Cohena65beee2011-06-27 21:32:23 -0700246 mFolderName.setHint(sHintText);
Adam Cohen1df26a32011-08-12 16:15:23 -0700247 // Convert to a string here to ensure that no other state associated with the text field
248 // gets saved.
249 mInfo.setTitle(mFolderName.getText().toString());
Adam Cohen76fc0852011-06-17 13:26:23 -0700250 LauncherModel.updateItemInDatabase(mLauncher, mInfo);
251 mFolderName.setCursorVisible(false);
252 mFolderName.clearFocus();
Adam Cohene601a432011-07-26 21:51:30 -0700253 Selection.setSelection((Spannable) mFolderName.getText(), 0, 0);
Adam Cohen76fc0852011-06-17 13:26:23 -0700254 mIsEditingName = false;
255 }
256
257 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
258 if (actionId == EditorInfo.IME_ACTION_DONE) {
259 dismissEditingName();
260 return true;
261 }
262 return false;
263 }
264
265 public View getEditTextRegion() {
266 return mFolderName;
267 }
268
Adam Cohenbadf71e2011-05-26 19:08:29 -0700269 public Drawable getDragDrawable() {
270 return mIconDrawable;
271 }
272
Adam Cohen0c872ba2011-05-05 10:34:16 -0700273 /**
274 * We need to handle touch events to prevent them from falling through to the workspace below.
275 */
276 @Override
277 public boolean onTouchEvent(MotionEvent ev) {
278 return true;
279 }
280
Joe Onorato00acb122009-08-04 16:04:30 -0400281 public void setDragController(DragController dragController) {
282 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800283 }
284
Adam Cohen2801caf2011-05-13 20:57:39 -0700285 void setFolderIcon(FolderIcon icon) {
286 mFolderIcon = icon;
287 }
288
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800289 /**
290 * @return the FolderInfo object associated with this folder
291 */
292 FolderInfo getInfo() {
293 return mInfo;
294 }
295
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800296 void bind(FolderInfo info) {
297 mInfo = info;
Adam Cohendf2cc412011-04-27 16:56:57 -0700298 ArrayList<ShortcutInfo> children = info.contents;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700299 ArrayList<ShortcutInfo> overflow = new ArrayList<ShortcutInfo>();
Adam Cohen7c693212011-05-18 15:26:57 -0700300 setupContentForNumItems(children.size());
Adam Cohen0057bbc2011-08-12 18:30:51 -0700301 int count = 0;
Adam Cohendf2cc412011-04-27 16:56:57 -0700302 for (int i = 0; i < children.size(); i++) {
303 ShortcutInfo child = (ShortcutInfo) children.get(i);
Adam Cohenc508b2d2011-06-28 14:41:44 -0700304 if (!createAndAddShortcut(child)) {
305 overflow.add(child);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700306 } else {
307 count++;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700308 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700309 }
Adam Cohenc508b2d2011-06-28 14:41:44 -0700310
Adam Cohen0057bbc2011-08-12 18:30:51 -0700311 // We rearrange the items in case there are any empty gaps
312 setupContentForNumItems(count);
313
Adam Cohenc508b2d2011-06-28 14:41:44 -0700314 // If our folder has too many items we prune them from the list. This is an issue
315 // when upgrading from the old Folders implementation which could contain an unlimited
316 // number of items.
317 for (ShortcutInfo item: overflow) {
318 mInfo.remove(item);
319 LauncherModel.deleteItemFromDatabase(mLauncher, item);
320 }
321
Adam Cohen4dbe6d92011-05-18 17:14:15 -0700322 mItemsInvalidated = true;
Adam Cohena9cf38f2011-05-02 15:36:58 -0700323 mInfo.addListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700324
Adam Cohenafb01ee2011-06-23 15:38:03 -0700325 if (!sDefaultFolderName.contentEquals(mInfo.title)) {
Adam Cohen4ef610f2011-06-21 22:37:36 -0700326 mFolderName.setText(mInfo.title);
327 } else {
328 mFolderName.setText("");
329 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700330 }
331
332 /**
333 * Creates a new UserFolder, inflated from R.layout.user_folder.
334 *
335 * @param context The application's context.
336 *
337 * @return A new UserFolder.
338 */
339 static Folder fromXml(Context context) {
340 return (Folder) LayoutInflater.from(context).inflate(R.layout.user_folder, null);
341 }
342
343 /**
344 * This method is intended to make the UserFolder to be visually identical in size and position
345 * to its associated FolderIcon. This allows for a seamless transition into the expanded state.
346 */
347 private void positionAndSizeAsIcon() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700348 if (!(getParent() instanceof DragLayer)) return;
Adam Cohendf2cc412011-04-27 16:56:57 -0700349
Adam Cohen8e776a62011-06-28 18:10:06 -0700350 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohendf2cc412011-04-27 16:56:57 -0700351
Adam Cohen2801caf2011-05-13 20:57:39 -0700352 if (mMode == PARTIAL_GROW) {
353 setScaleX(0.8f);
354 setScaleY(0.8f);
355 setAlpha(0f);
356 } else {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700357 mLauncher.getDragLayer().getDescendantRectRelativeToSelf(mFolderIcon, mIconRect);
358 lp.width = mIconRect.width();
359 lp.height = mIconRect.height();
360 lp.x = mIconRect.left;
361 lp.y = mIconRect.top;
Adam Cohen2801caf2011-05-13 20:57:39 -0700362 mContent.setAlpha(0);
363 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700364 mState = STATE_SMALL;
365 }
366
367 public void animateOpen() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700368 positionAndSizeAsIcon();
369
Adam Cohen8e776a62011-06-28 18:10:06 -0700370 if (!(getParent() instanceof DragLayer)) return;
Adam Cohendf2cc412011-04-27 16:56:57 -0700371
Adam Cohen2801caf2011-05-13 20:57:39 -0700372 ObjectAnimator oa;
Adam Cohen8e776a62011-06-28 18:10:06 -0700373 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohendf2cc412011-04-27 16:56:57 -0700374
Adam Cohen2801caf2011-05-13 20:57:39 -0700375 centerAboutIcon();
376 if (mMode == PARTIAL_GROW) {
377 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1);
378 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
379 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
380 oa = ObjectAnimator.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
381 } else {
382 PropertyValuesHolder width = PropertyValuesHolder.ofInt("width", mNewSize.width());
383 PropertyValuesHolder height = PropertyValuesHolder.ofInt("height", mNewSize.height());
384 PropertyValuesHolder x = PropertyValuesHolder.ofInt("x", mNewSize.left);
385 PropertyValuesHolder y = PropertyValuesHolder.ofInt("y", mNewSize.top);
386 oa = ObjectAnimator.ofPropertyValuesHolder(lp, width, height, x, y);
387 oa.addUpdateListener(new AnimatorUpdateListener() {
388 public void onAnimationUpdate(ValueAnimator animation) {
389 requestLayout();
390 }
391 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700392
Adam Cohen2801caf2011-05-13 20:57:39 -0700393 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
394 ObjectAnimator alphaOa = ObjectAnimator.ofPropertyValuesHolder(mContent, alpha);
395 alphaOa.setDuration(mExpandDuration);
396 alphaOa.setInterpolator(new AccelerateInterpolator(2.0f));
397 alphaOa.start();
398 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700399
Adam Cohen2801caf2011-05-13 20:57:39 -0700400 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700401 @Override
402 public void onAnimationStart(Animator animation) {
403 mState = STATE_ANIMATING;
404 }
405 @Override
406 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700407 mState = STATE_OPEN;
Adam Cohendf2cc412011-04-27 16:56:57 -0700408 }
409 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700410 oa.setDuration(mExpandDuration);
411 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700412 }
413
414 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700415 if (!(getParent() instanceof DragLayer)) return;
Adam Cohendf2cc412011-04-27 16:56:57 -0700416
Adam Cohen2801caf2011-05-13 20:57:39 -0700417 ObjectAnimator oa;
Adam Cohen2801caf2011-05-13 20:57:39 -0700418 if (mMode == PARTIAL_GROW) {
419 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
420 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
421 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
422 oa = ObjectAnimator.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
423 } else {
Adam Cohen8e776a62011-06-28 18:10:06 -0700424 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohendf2cc412011-04-27 16:56:57 -0700425
Adam Cohen3e8f8112011-07-02 18:03:00 -0700426 PropertyValuesHolder width = PropertyValuesHolder.ofInt("width", mIconRect.width());
427 PropertyValuesHolder height = PropertyValuesHolder.ofInt("height", mIconRect.height());
428 PropertyValuesHolder x = PropertyValuesHolder.ofInt("x", mIconRect.left);
429 PropertyValuesHolder y = PropertyValuesHolder.ofInt("y", mIconRect.top);
Adam Cohen2801caf2011-05-13 20:57:39 -0700430 oa = ObjectAnimator.ofPropertyValuesHolder(lp, width, height, x, y);
431 oa.addUpdateListener(new AnimatorUpdateListener() {
432 public void onAnimationUpdate(ValueAnimator animation) {
433 requestLayout();
434 }
435 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700436
Adam Cohen2801caf2011-05-13 20:57:39 -0700437 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0f);
438 ObjectAnimator alphaOa = ObjectAnimator.ofPropertyValuesHolder(mContent, alpha);
439 alphaOa.setDuration(mExpandDuration);
440 alphaOa.setInterpolator(new DecelerateInterpolator(2.0f));
441 alphaOa.start();
442 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700443
Adam Cohen2801caf2011-05-13 20:57:39 -0700444 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700445 @Override
446 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700447 onCloseComplete();
Adam Cohen2801caf2011-05-13 20:57:39 -0700448 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700449 }
450 @Override
451 public void onAnimationStart(Animator animation) {
452 mState = STATE_ANIMATING;
453 }
454 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700455 oa.setDuration(mExpandDuration);
456 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700457 }
458
459 void notifyDataSetChanged() {
460 // recreate all the children if the data set changes under us. We may want to do this more
461 // intelligently (ie just removing the views that should no longer exist)
462 mContent.removeAllViewsInLayout();
463 bind(mInfo);
464 }
465
Adam Cohencb3382b2011-05-24 14:07:08 -0700466 public boolean acceptDrop(DragObject d) {
467 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700468 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700469 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
470 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
471 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700472 }
473
Adam Cohendf2cc412011-04-27 16:56:57 -0700474 protected boolean findAndSetEmptyCells(ShortcutInfo item) {
475 int[] emptyCell = new int[2];
476 if (mContent.findCellForSpan(emptyCell, item.spanX, item.spanY)) {
477 item.cellX = emptyCell[0];
478 item.cellY = emptyCell[1];
Adam Cohendf2cc412011-04-27 16:56:57 -0700479 return true;
480 } else {
481 return false;
482 }
483 }
484
Adam Cohenc508b2d2011-06-28 14:41:44 -0700485 protected boolean createAndAddShortcut(ShortcutInfo item) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700486 final TextView textView =
Adam Cohene87b9242011-06-29 14:01:26 -0700487 (TextView) mInflater.inflate(R.layout.application, this, false);
Adam Cohendf2cc412011-04-27 16:56:57 -0700488 textView.setCompoundDrawablesWithIntrinsicBounds(null,
489 new FastBitmapDrawable(item.getIcon(mIconCache)), null, null);
490 textView.setText(item.title);
491 textView.setTag(item);
492
493 textView.setOnClickListener(this);
494 textView.setOnLongClickListener(this);
495
Adam Cohenc508b2d2011-06-28 14:41:44 -0700496 // We need to check here to verify that the given item's location isn't already occupied
497 // by another item. If it is, we need to find the next available slot and assign
498 // it that position. This is an issue when upgrading from the old Folders implementation
499 // which could contain an unlimited number of items.
Adam Cohen0057bbc2011-08-12 18:30:51 -0700500 if (mContent.getChildAt(item.cellX, item.cellY) != null || item.cellX < 0 || item.cellY < 0
501 || item.cellX >= mContent.getCountX() || item.cellY >= mContent.getCountY()) {
Adam Cohenc508b2d2011-06-28 14:41:44 -0700502 if (!findAndSetEmptyCells(item)) {
503 return false;
504 }
505 }
506
Adam Cohendf2cc412011-04-27 16:56:57 -0700507 CellLayout.LayoutParams lp =
508 new CellLayout.LayoutParams(item.cellX, item.cellY, item.spanX, item.spanY);
509 boolean insert = false;
510 mContent.addViewToCellLayout(textView, insert ? 0 : -1, (int)item.id, lp, true);
Adam Cohenc508b2d2011-06-28 14:41:44 -0700511 return true;
Adam Cohendf2cc412011-04-27 16:56:57 -0700512 }
513
Adam Cohencb3382b2011-05-24 14:07:08 -0700514 public void onDragEnter(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700515 mPreviousTargetCell[0] = -1;
516 mPreviousTargetCell[1] = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700517 mOnExitAlarm.cancelAlarm();
518 }
519
520 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
521 public void onAlarm(Alarm alarm) {
522 realTimeReorder(mEmptyCell, mTargetCell);
523 }
524 };
525
526 boolean readingOrderGreaterThan(int[] v1, int[] v2) {
527 if (v1[1] > v2[1] || (v1[1] == v2[1] && v1[0] > v2[0])) {
528 return true;
529 } else {
530 return false;
531 }
532 }
533
534 private void realTimeReorder(int[] empty, int[] target) {
535 boolean wrap;
536 int startX;
537 int endX;
538 int startY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700539 int delay = 0;
540 float delayAmount = 30;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700541 if (readingOrderGreaterThan(target, empty)) {
542 wrap = empty[0] >= mContent.getCountX() - 1;
543 startY = wrap ? empty[1] + 1 : empty[1];
544 for (int y = startY; y <= target[1]; y++) {
545 startX = y == empty[1] ? empty[0] + 1 : 0;
546 endX = y < target[1] ? mContent.getCountX() - 1 : target[0];
547 for (int x = startX; x <= endX; x++) {
548 View v = mContent.getChildAt(x,y);
549 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen76fc0852011-06-17 13:26:23 -0700550 REORDER_ANIMATION_DURATION, delay)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700551 empty[0] = x;
552 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700553 delay += delayAmount;
554 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700555 }
556 }
557 }
558 } else {
559 wrap = empty[0] == 0;
560 startY = wrap ? empty[1] - 1 : empty[1];
561 for (int y = startY; y >= target[1]; y--) {
562 startX = y == empty[1] ? empty[0] - 1 : mContent.getCountX() - 1;
563 endX = y > target[1] ? 0 : target[0];
564 for (int x = startX; x >= endX; x--) {
565 View v = mContent.getChildAt(x,y);
566 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen76fc0852011-06-17 13:26:23 -0700567 REORDER_ANIMATION_DURATION, delay)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700568 empty[0] = x;
569 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700570 delay += delayAmount;
571 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700572 }
573 }
574 }
575 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700576 }
577
Adam Cohencb3382b2011-05-24 14:07:08 -0700578 public void onDragOver(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700579 float[] r = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, d.dragView, null);
580 mTargetCell = mContent.findNearestArea((int) r[0], (int) r[1], 1, 1, mTargetCell);
581
582 if (mTargetCell[0] != mPreviousTargetCell[0] || mTargetCell[1] != mPreviousTargetCell[1]) {
583 mReorderAlarm.cancelAlarm();
584 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
585 mReorderAlarm.setAlarm(150);
586 mPreviousTargetCell[0] = mTargetCell[0];
587 mPreviousTargetCell[1] = mTargetCell[1];
588 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700589 }
590
Adam Cohenbfbfd262011-06-13 16:55:12 -0700591 // This is used to compute the visual center of the dragView. The idea is that
592 // the visual center represents the user's interpretation of where the item is, and hence
593 // is the appropriate point to use when determining drop location.
594 private float[] getDragViewVisualCenter(int x, int y, int xOffset, int yOffset,
595 DragView dragView, float[] recycle) {
596 float res[];
597 if (recycle == null) {
598 res = new float[2];
599 } else {
600 res = recycle;
601 }
602
603 // These represent the visual top and left of drag view if a dragRect was provided.
604 // If a dragRect was not provided, then they correspond to the actual view left and
605 // top, as the dragRect is in that case taken to be the entire dragView.
606 // R.dimen.dragViewOffsetY.
607 int left = x - xOffset;
608 int top = y - yOffset;
609
610 // In order to find the visual center, we shift by half the dragRect
611 res[0] = left + dragView.getDragRegion().width() / 2;
612 res[1] = top + dragView.getDragRegion().height() / 2;
613
614 return res;
615 }
616
617 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
618 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700619 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700620 }
621 };
622
Adam Cohen95bb8002011-07-03 23:40:28 -0700623 public void completeDragExit() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700624 mLauncher.closeFolder();
625 mCurrentDragInfo = null;
626 mCurrentDragView = null;
627 mSuppressOnAdd = false;
628 mRearrangeOnClose = true;
629 }
630
Adam Cohencb3382b2011-05-24 14:07:08 -0700631 public void onDragExit(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700632 // We only close the folder if this is a true drag exit, ie. not because a drop
633 // has occurred above the folder.
634 if (!d.dragComplete) {
635 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
636 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
637 }
638 mReorderAlarm.cancelAlarm();
Adam Cohen2801caf2011-05-13 20:57:39 -0700639 }
640
Adam Cohenc0dcf592011-06-01 15:30:43 -0700641 public void onDropCompleted(View target, DragObject d, boolean success) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700642 if (success) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700643 if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700644 replaceFolderWithFinalItem();
645 }
646 } else {
647 // The drag failed, we need to return the item to the folder
648 mFolderIcon.onDrop(d);
649
650 // We're going to trigger a "closeFolder" which may occur before this item has
651 // been added back to the folder -- this could cause the folder to be deleted
652 if (mOnExitAlarm.alarmPending()) {
653 mSuppressFolderDeletion = true;
654 }
655 }
656
657 if (target != this) {
658 if (mOnExitAlarm.alarmPending()) {
659 mOnExitAlarm.cancelAlarm();
660 completeDragExit();
661 }
662 }
663 mDeleteFolderOnDropCompleted = false;
664 mDragInProgress = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700665 mItemAddedBackToSelfViaIcon = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700666 mCurrentDragInfo = null;
667 mCurrentDragView = null;
668 mSuppressOnAdd = false;
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700669 }
Adam Cohen228da5a2011-07-27 22:23:47 -0700670
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700671 public void notifyDrop() {
672 if (mDragInProgress) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700673 mItemAddedBackToSelfViaIcon = true;
Adam Cohen76078c42011-06-09 15:06:52 -0700674 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700675 }
676
677 public boolean isDropEnabled() {
678 return true;
679 }
680
Adam Cohencb3382b2011-05-24 14:07:08 -0700681 public DropTarget getDropTargetDelegate(DragObject d) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700682 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800683 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700684
Adam Cohen2801caf2011-05-13 20:57:39 -0700685 private void setupContentDimension(int count) {
686 ArrayList<View> list = getItemsInReadingOrder();
687
688 int countX = mContent.getCountX();
689 int countY = mContent.getCountY();
Adam Cohen7c693212011-05-18 15:26:57 -0700690 boolean done = false;
Adam Cohen2801caf2011-05-13 20:57:39 -0700691
Adam Cohen7c693212011-05-18 15:26:57 -0700692 while (!done) {
693 int oldCountX = countX;
694 int oldCountY = countY;
695 if (countX * countY < count) {
696 // Current grid is too small, expand it
697 if (countX <= countY && countX < mMaxCountX) {
698 countX++;
699 } else if (countY < mMaxCountY) {
700 countY++;
701 }
702 if (countY == 0) countY++;
703 } else if ((countY - 1) * countX >= count && countY >= countX) {
704 countY = Math.max(0, countY - 1);
705 } else if ((countX - 1) * countY >= count) {
706 countX = Math.max(0, countX - 1);
Adam Cohen2801caf2011-05-13 20:57:39 -0700707 }
Adam Cohen7c693212011-05-18 15:26:57 -0700708 done = countX == oldCountX && countY == oldCountY;
Adam Cohen2801caf2011-05-13 20:57:39 -0700709 }
Adam Cohen7c693212011-05-18 15:26:57 -0700710 mContent.setGridSize(countX, countY);
Adam Cohen2801caf2011-05-13 20:57:39 -0700711 arrangeChildren(list);
712 }
713
714 public boolean isFull() {
715 return getItemCount() >= mMaxCountX * mMaxCountY;
716 }
717
718 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700719 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700720
721 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf4bb1cd2011-07-22 14:36:03 -0700722 int height = getPaddingTop() + getPaddingBottom() + mContent.getDesiredHeight()
723 + mFolderNameHeight;
Adam Cohen8e776a62011-06-28 18:10:06 -0700724 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700725
Adam Cohen8e776a62011-06-28 18:10:06 -0700726 parent.getDescendantRectRelativeToSelf(mFolderIcon, mTempRect);
727
728 int centerX = mTempRect.centerX();
729 int centerY = mTempRect.centerY();
Adam Cohen2801caf2011-05-13 20:57:39 -0700730 int centeredLeft = centerX - width / 2;
731 int centeredTop = centerY - height / 2;
732
Adam Cohen35e7e642011-07-17 14:47:18 -0700733 // We first fetch the currently visible CellLayoutChildren
Winson Chung3d503fb2011-07-13 17:25:49 -0700734 CellLayout currentPage = mLauncher.getWorkspace().getCurrentDropLayout();
Adam Cohen35e7e642011-07-17 14:47:18 -0700735 CellLayoutChildren boundingLayout = currentPage.getChildrenLayout();
736 Rect bounds = new Rect();
737 parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
Adam Cohen2801caf2011-05-13 20:57:39 -0700738
Adam Cohen35e7e642011-07-17 14:47:18 -0700739 // We need to bound the folder to the currently visible CellLayoutChildren
740 int left = Math.min(Math.max(bounds.left, centeredLeft),
741 bounds.left + bounds.width() - width);
742 int top = Math.min(Math.max(bounds.top, centeredTop),
743 bounds.top + bounds.height() - height);
744 // If the folder doesn't fit within the bounds, center it about the desired bounds
745 if (width >= bounds.width()) {
746 left = bounds.left + (bounds.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700747 }
Adam Cohen35e7e642011-07-17 14:47:18 -0700748 if (height >= bounds.height()) {
749 top = bounds.top + (bounds.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700750 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700751
752 int folderPivotX = width / 2 + (centeredLeft - left);
753 int folderPivotY = height / 2 + (centeredTop - top);
754 setPivotX(folderPivotX);
755 setPivotY(folderPivotY);
756 int folderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
757 (1.0f * folderPivotX / width));
758 int folderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
759 (1.0f * folderPivotY / height));
760 mFolderIcon.setPivotX(folderIconPivotX);
761 mFolderIcon.setPivotY(folderIconPivotY);
762
763 if (mMode == PARTIAL_GROW) {
764 lp.width = width;
765 lp.height = height;
766 lp.x = left;
767 lp.y = top;
768 } else {
769 mNewSize.set(left, top, left + width, top + height);
770 }
771 }
772
773 private void setupContentForNumItems(int count) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700774 setupContentDimension(count);
775
Adam Cohen8e776a62011-06-28 18:10:06 -0700776 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700777 if (lp == null) {
Adam Cohen8e776a62011-06-28 18:10:06 -0700778 lp = new DragLayer.LayoutParams(0, 0);
779 lp.customPosition = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700780 setLayoutParams(lp);
781 }
782 centerAboutIcon();
783 }
784
Adam Cohen234c4cd2011-07-17 21:03:04 -0700785 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
786 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf4bb1cd2011-07-22 14:36:03 -0700787 int height = getPaddingTop() + getPaddingBottom() + mContent.getDesiredHeight()
788 + mFolderNameHeight;
Adam Cohen234c4cd2011-07-17 21:03:04 -0700789
790 int contentWidthSpec = MeasureSpec.makeMeasureSpec(mContent.getDesiredWidth(),
791 MeasureSpec.EXACTLY);
792 int contentHeightSpec = MeasureSpec.makeMeasureSpec(mContent.getDesiredHeight(),
793 MeasureSpec.EXACTLY);
794 mContent.measure(contentWidthSpec, contentHeightSpec);
795
796 mFolderName.measure(contentWidthSpec,
797 MeasureSpec.makeMeasureSpec(mFolderNameHeight, MeasureSpec.EXACTLY));
798 setMeasuredDimension(width, height);
799 }
800
Adam Cohen2801caf2011-05-13 20:57:39 -0700801 private void arrangeChildren(ArrayList<View> list) {
802 int[] vacant = new int[2];
803 if (list == null) {
804 list = getItemsInReadingOrder();
805 }
806 mContent.removeAllViews();
807
808 for (int i = 0; i < list.size(); i++) {
809 View v = list.get(i);
810 mContent.getVacantCell(vacant, 1, 1);
811 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) v.getLayoutParams();
812 lp.cellX = vacant[0];
813 lp.cellY = vacant[1];
814 ItemInfo info = (ItemInfo) v.getTag();
815 info.cellX = vacant[0];
816 info.cellY = vacant[1];
817 boolean insert = false;
818 mContent.addViewToCellLayout(v, insert ? 0 : -1, (int)info.id, lp, true);
Adam Cohen7c693212011-05-18 15:26:57 -0700819 LauncherModel.addOrMoveItemInDatabase(mLauncher, info, mInfo.id, 0,
820 info.cellX, info.cellY);
Adam Cohen2801caf2011-05-13 20:57:39 -0700821 }
Adam Cohen7c693212011-05-18 15:26:57 -0700822 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700823 }
824
Adam Cohena9cf38f2011-05-02 15:36:58 -0700825 public int getItemCount() {
826 return mContent.getChildrenLayout().getChildCount();
827 }
828
829 public View getItemAt(int index) {
830 return mContent.getChildrenLayout().getChildAt(index);
831 }
832
Adam Cohen2801caf2011-05-13 20:57:39 -0700833 private void onCloseComplete() {
Adam Cohen05e0f402011-08-01 12:12:49 -0700834 DragLayer parent = (DragLayer) getParent();
Adam Cohen4554ee12011-08-03 16:13:21 -0700835 parent.removeView(this);
836 mDragController.removeDropTarget((DropTarget) this);
Adam Cohen05e0f402011-08-01 12:12:49 -0700837 clearFocus();
838
Adam Cohen2801caf2011-05-13 20:57:39 -0700839 if (mRearrangeOnClose) {
840 setupContentForNumItems(getItemCount());
841 mRearrangeOnClose = false;
842 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700843 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700844 if (!mDragInProgress && !mSuppressFolderDeletion) {
845 replaceFolderWithFinalItem();
846 } else if (mDragInProgress) {
847 mDeleteFolderOnDropCompleted = true;
848 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700849 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700850 mSuppressFolderDeletion = false;
Adam Cohenafb01ee2011-06-23 15:38:03 -0700851 }
852
853 private void replaceFolderWithFinalItem() {
854 ItemInfo finalItem = null;
855
856 if (getItemCount() == 1) {
857 finalItem = mInfo.contents.get(0);
858 }
859
860 // Remove the folder completely
Winson Chung3d503fb2011-07-13 17:25:49 -0700861 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screen);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700862 cellLayout.removeView(mFolderIcon);
863 if (mFolderIcon instanceof DropTarget) {
864 mDragController.removeDropTarget((DropTarget) mFolderIcon);
865 }
866 mLauncher.removeFolder(mInfo);
867
868 if (finalItem != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700869 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
870 mInfo.screen, mInfo.cellX, mInfo.cellY);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700871 }
Adam Cohen716b51e2011-06-30 12:09:54 -0700872 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700873
874 // Add the last remaining child to the workspace in place of the folder
875 if (finalItem != null) {
876 View child = mLauncher.createShortcut(R.layout.application, cellLayout,
877 (ShortcutInfo) finalItem);
878
Winson Chung3d503fb2011-07-13 17:25:49 -0700879 mLauncher.getWorkspace().addInScreen(child, mInfo.container, mInfo.screen, mInfo.cellX,
880 mInfo.cellY, mInfo.spanX, mInfo.spanY);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700881 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700882 }
883
Adam Cohenbfbfd262011-06-13 16:55:12 -0700884 public void onDrop(DragObject d) {
885 ShortcutInfo item;
886 if (d.dragInfo instanceof ApplicationInfo) {
887 // Came from all apps -- make a copy
888 item = ((ApplicationInfo) d.dragInfo).makeShortcut();
889 item.spanX = 1;
890 item.spanY = 1;
891 } else {
892 item = (ShortcutInfo) d.dragInfo;
893 }
Adam Cohen05e0f402011-08-01 12:12:49 -0700894 // Dragged from self onto self, currently this is the only path possible, however
895 // we keep this as a distinct code path.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700896 if (item == mCurrentDragInfo) {
897 ShortcutInfo si = (ShortcutInfo) mCurrentDragView.getTag();
898 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) mCurrentDragView.getLayoutParams();
899 si.cellX = lp.cellX = mEmptyCell[0];
900 si.cellX = lp.cellY = mEmptyCell[1];
901 mContent.addViewToCellLayout(mCurrentDragView, -1, (int)item.id, lp, true);
Adam Cohenfc53cd22011-07-20 15:45:11 -0700902 if (d.dragView.hasDrawn()) {
903 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, mCurrentDragView);
904 } else {
905 mCurrentDragView.setVisibility(VISIBLE);
906 }
Adam Cohene9166b22011-07-08 17:11:11 -0700907 mItemsInvalidated = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700908 setupContentDimension(getItemCount());
Adam Cohen716b51e2011-06-30 12:09:54 -0700909 mSuppressOnAdd = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700910 }
911 mInfo.add(item);
912 }
913
914 public void onAdd(ShortcutInfo item) {
915 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700916 // If the item was dropped onto this open folder, we have done the work associated
917 // with adding the item to the folder, as indicated by mSuppressOnAdd being set
Adam Cohenbfbfd262011-06-13 16:55:12 -0700918 if (mSuppressOnAdd) return;
919 if (!findAndSetEmptyCells(item)) {
920 // The current layout is full, can we expand it?
921 setupContentForNumItems(getItemCount() + 1);
922 findAndSetEmptyCells(item);
923 }
924 createAndAddShortcut(item);
925 LauncherModel.addOrMoveItemInDatabase(
926 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
927 }
928
Adam Cohena9cf38f2011-05-02 15:36:58 -0700929 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -0700930 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700931 // If this item is being dragged from this open folder, we have already handled
932 // the work associated with removing the item, so we don't have to do anything here.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700933 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -0700934 View v = getViewForInfo(item);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700935 mContent.removeView(v);
Adam Cohen2801caf2011-05-13 20:57:39 -0700936 if (mState == STATE_ANIMATING) {
937 mRearrangeOnClose = true;
938 } else {
939 setupContentForNumItems(getItemCount());
940 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700941 if (getItemCount() <= 1) {
942 replaceFolderWithFinalItem();
943 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700944 }
Adam Cohen7c693212011-05-18 15:26:57 -0700945
Adam Cohendf1e4e82011-06-24 15:57:39 -0700946 private View getViewForInfo(ShortcutInfo item) {
947 for (int j = 0; j < mContent.getCountY(); j++) {
948 for (int i = 0; i < mContent.getCountX(); i++) {
949 View v = mContent.getChildAt(i, j);
950 if (v.getTag() == item) {
951 return v;
952 }
953 }
954 }
955 return null;
956 }
957
Adam Cohen76078c42011-06-09 15:06:52 -0700958 public void onItemsChanged() {
959 }
Adam Cohen76fc0852011-06-17 13:26:23 -0700960 public void onTitleChanged(CharSequence title) {
961 }
Adam Cohen76078c42011-06-09 15:06:52 -0700962
Adam Cohen7c693212011-05-18 15:26:57 -0700963 public ArrayList<View> getItemsInReadingOrder() {
Adam Cohen76078c42011-06-09 15:06:52 -0700964 return getItemsInReadingOrder(true);
965 }
966
967 public ArrayList<View> getItemsInReadingOrder(boolean includeCurrentDragItem) {
Adam Cohen7c693212011-05-18 15:26:57 -0700968 if (mItemsInvalidated) {
969 mItemsInReadingOrder.clear();
970 for (int j = 0; j < mContent.getCountY(); j++) {
971 for (int i = 0; i < mContent.getCountX(); i++) {
972 View v = mContent.getChildAt(i, j);
973 if (v != null) {
Adam Cohen76078c42011-06-09 15:06:52 -0700974 ShortcutInfo info = (ShortcutInfo) v.getTag();
975 if (info != mCurrentDragInfo || includeCurrentDragItem) {
976 mItemsInReadingOrder.add(v);
977 }
Adam Cohen7c693212011-05-18 15:26:57 -0700978 }
979 }
980 }
981 mItemsInvalidated = false;
982 }
983 return mItemsInReadingOrder;
984 }
Adam Cohen8dfcba42011-07-07 16:38:18 -0700985
986 public void getLocationInDragLayer(int[] loc) {
987 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
988 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800989}