blob: 71b708128f38e7784a5ffba8ff29f19973117a48 [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;
Winson Chung7a74ac92011-09-20 17:43:51 -0700408
409 Cling cling = mLauncher.showFirstRunFoldersCling();
410 if (cling != null) {
411 cling.bringToFront();
412 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700413 }
414 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700415 oa.setDuration(mExpandDuration);
416 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700417 }
418
419 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700420 if (!(getParent() instanceof DragLayer)) return;
Adam Cohendf2cc412011-04-27 16:56:57 -0700421
Adam Cohen2801caf2011-05-13 20:57:39 -0700422 ObjectAnimator oa;
Adam Cohen2801caf2011-05-13 20:57:39 -0700423 if (mMode == PARTIAL_GROW) {
424 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
425 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
426 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
427 oa = ObjectAnimator.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
428 } else {
Adam Cohen8e776a62011-06-28 18:10:06 -0700429 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohendf2cc412011-04-27 16:56:57 -0700430
Adam Cohen3e8f8112011-07-02 18:03:00 -0700431 PropertyValuesHolder width = PropertyValuesHolder.ofInt("width", mIconRect.width());
432 PropertyValuesHolder height = PropertyValuesHolder.ofInt("height", mIconRect.height());
433 PropertyValuesHolder x = PropertyValuesHolder.ofInt("x", mIconRect.left);
434 PropertyValuesHolder y = PropertyValuesHolder.ofInt("y", mIconRect.top);
Adam Cohen2801caf2011-05-13 20:57:39 -0700435 oa = ObjectAnimator.ofPropertyValuesHolder(lp, width, height, x, y);
436 oa.addUpdateListener(new AnimatorUpdateListener() {
437 public void onAnimationUpdate(ValueAnimator animation) {
438 requestLayout();
439 }
440 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700441
Adam Cohen2801caf2011-05-13 20:57:39 -0700442 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0f);
443 ObjectAnimator alphaOa = ObjectAnimator.ofPropertyValuesHolder(mContent, alpha);
444 alphaOa.setDuration(mExpandDuration);
445 alphaOa.setInterpolator(new DecelerateInterpolator(2.0f));
446 alphaOa.start();
447 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700448
Adam Cohen2801caf2011-05-13 20:57:39 -0700449 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700450 @Override
451 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700452 onCloseComplete();
Adam Cohen2801caf2011-05-13 20:57:39 -0700453 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700454 }
455 @Override
456 public void onAnimationStart(Animator animation) {
457 mState = STATE_ANIMATING;
458 }
459 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700460 oa.setDuration(mExpandDuration);
461 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700462 }
463
464 void notifyDataSetChanged() {
465 // recreate all the children if the data set changes under us. We may want to do this more
466 // intelligently (ie just removing the views that should no longer exist)
467 mContent.removeAllViewsInLayout();
468 bind(mInfo);
469 }
470
Adam Cohencb3382b2011-05-24 14:07:08 -0700471 public boolean acceptDrop(DragObject d) {
472 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700473 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700474 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
475 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
476 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700477 }
478
Adam Cohendf2cc412011-04-27 16:56:57 -0700479 protected boolean findAndSetEmptyCells(ShortcutInfo item) {
480 int[] emptyCell = new int[2];
481 if (mContent.findCellForSpan(emptyCell, item.spanX, item.spanY)) {
482 item.cellX = emptyCell[0];
483 item.cellY = emptyCell[1];
Adam Cohendf2cc412011-04-27 16:56:57 -0700484 return true;
485 } else {
486 return false;
487 }
488 }
489
Adam Cohenc508b2d2011-06-28 14:41:44 -0700490 protected boolean createAndAddShortcut(ShortcutInfo item) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700491 final TextView textView =
Adam Cohene87b9242011-06-29 14:01:26 -0700492 (TextView) mInflater.inflate(R.layout.application, this, false);
Adam Cohendf2cc412011-04-27 16:56:57 -0700493 textView.setCompoundDrawablesWithIntrinsicBounds(null,
494 new FastBitmapDrawable(item.getIcon(mIconCache)), null, null);
495 textView.setText(item.title);
496 textView.setTag(item);
497
498 textView.setOnClickListener(this);
499 textView.setOnLongClickListener(this);
500
Adam Cohenc508b2d2011-06-28 14:41:44 -0700501 // We need to check here to verify that the given item's location isn't already occupied
502 // by another item. If it is, we need to find the next available slot and assign
503 // it that position. This is an issue when upgrading from the old Folders implementation
504 // which could contain an unlimited number of items.
Adam Cohen0057bbc2011-08-12 18:30:51 -0700505 if (mContent.getChildAt(item.cellX, item.cellY) != null || item.cellX < 0 || item.cellY < 0
506 || item.cellX >= mContent.getCountX() || item.cellY >= mContent.getCountY()) {
Adam Cohenc508b2d2011-06-28 14:41:44 -0700507 if (!findAndSetEmptyCells(item)) {
508 return false;
509 }
510 }
511
Adam Cohendf2cc412011-04-27 16:56:57 -0700512 CellLayout.LayoutParams lp =
513 new CellLayout.LayoutParams(item.cellX, item.cellY, item.spanX, item.spanY);
514 boolean insert = false;
515 mContent.addViewToCellLayout(textView, insert ? 0 : -1, (int)item.id, lp, true);
Adam Cohenc508b2d2011-06-28 14:41:44 -0700516 return true;
Adam Cohendf2cc412011-04-27 16:56:57 -0700517 }
518
Adam Cohencb3382b2011-05-24 14:07:08 -0700519 public void onDragEnter(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700520 mPreviousTargetCell[0] = -1;
521 mPreviousTargetCell[1] = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700522 mOnExitAlarm.cancelAlarm();
523 }
524
525 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
526 public void onAlarm(Alarm alarm) {
527 realTimeReorder(mEmptyCell, mTargetCell);
528 }
529 };
530
531 boolean readingOrderGreaterThan(int[] v1, int[] v2) {
532 if (v1[1] > v2[1] || (v1[1] == v2[1] && v1[0] > v2[0])) {
533 return true;
534 } else {
535 return false;
536 }
537 }
538
539 private void realTimeReorder(int[] empty, int[] target) {
540 boolean wrap;
541 int startX;
542 int endX;
543 int startY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700544 int delay = 0;
545 float delayAmount = 30;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700546 if (readingOrderGreaterThan(target, empty)) {
547 wrap = empty[0] >= mContent.getCountX() - 1;
548 startY = wrap ? empty[1] + 1 : empty[1];
549 for (int y = startY; y <= target[1]; y++) {
550 startX = y == empty[1] ? empty[0] + 1 : 0;
551 endX = y < target[1] ? mContent.getCountX() - 1 : target[0];
552 for (int x = startX; x <= endX; x++) {
553 View v = mContent.getChildAt(x,y);
554 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen76fc0852011-06-17 13:26:23 -0700555 REORDER_ANIMATION_DURATION, delay)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700556 empty[0] = x;
557 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700558 delay += delayAmount;
559 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700560 }
561 }
562 }
563 } else {
564 wrap = empty[0] == 0;
565 startY = wrap ? empty[1] - 1 : empty[1];
566 for (int y = startY; y >= target[1]; y--) {
567 startX = y == empty[1] ? empty[0] - 1 : mContent.getCountX() - 1;
568 endX = y > target[1] ? 0 : target[0];
569 for (int x = startX; x >= endX; x--) {
570 View v = mContent.getChildAt(x,y);
571 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen76fc0852011-06-17 13:26:23 -0700572 REORDER_ANIMATION_DURATION, delay)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700573 empty[0] = x;
574 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700575 delay += delayAmount;
576 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700577 }
578 }
579 }
580 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700581 }
582
Adam Cohencb3382b2011-05-24 14:07:08 -0700583 public void onDragOver(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700584 float[] r = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, d.dragView, null);
585 mTargetCell = mContent.findNearestArea((int) r[0], (int) r[1], 1, 1, mTargetCell);
586
587 if (mTargetCell[0] != mPreviousTargetCell[0] || mTargetCell[1] != mPreviousTargetCell[1]) {
588 mReorderAlarm.cancelAlarm();
589 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
590 mReorderAlarm.setAlarm(150);
591 mPreviousTargetCell[0] = mTargetCell[0];
592 mPreviousTargetCell[1] = mTargetCell[1];
593 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700594 }
595
Adam Cohenbfbfd262011-06-13 16:55:12 -0700596 // This is used to compute the visual center of the dragView. The idea is that
597 // the visual center represents the user's interpretation of where the item is, and hence
598 // is the appropriate point to use when determining drop location.
599 private float[] getDragViewVisualCenter(int x, int y, int xOffset, int yOffset,
600 DragView dragView, float[] recycle) {
601 float res[];
602 if (recycle == null) {
603 res = new float[2];
604 } else {
605 res = recycle;
606 }
607
608 // These represent the visual top and left of drag view if a dragRect was provided.
609 // If a dragRect was not provided, then they correspond to the actual view left and
610 // top, as the dragRect is in that case taken to be the entire dragView.
611 // R.dimen.dragViewOffsetY.
612 int left = x - xOffset;
613 int top = y - yOffset;
614
615 // In order to find the visual center, we shift by half the dragRect
616 res[0] = left + dragView.getDragRegion().width() / 2;
617 res[1] = top + dragView.getDragRegion().height() / 2;
618
619 return res;
620 }
621
622 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
623 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700624 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700625 }
626 };
627
Adam Cohen95bb8002011-07-03 23:40:28 -0700628 public void completeDragExit() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700629 mLauncher.closeFolder();
630 mCurrentDragInfo = null;
631 mCurrentDragView = null;
632 mSuppressOnAdd = false;
633 mRearrangeOnClose = true;
634 }
635
Adam Cohencb3382b2011-05-24 14:07:08 -0700636 public void onDragExit(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700637 // We only close the folder if this is a true drag exit, ie. not because a drop
638 // has occurred above the folder.
639 if (!d.dragComplete) {
640 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
641 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
642 }
643 mReorderAlarm.cancelAlarm();
Adam Cohen2801caf2011-05-13 20:57:39 -0700644 }
645
Adam Cohenc0dcf592011-06-01 15:30:43 -0700646 public void onDropCompleted(View target, DragObject d, boolean success) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700647 if (success) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700648 if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700649 replaceFolderWithFinalItem();
650 }
651 } else {
652 // The drag failed, we need to return the item to the folder
653 mFolderIcon.onDrop(d);
654
655 // We're going to trigger a "closeFolder" which may occur before this item has
656 // been added back to the folder -- this could cause the folder to be deleted
657 if (mOnExitAlarm.alarmPending()) {
658 mSuppressFolderDeletion = true;
659 }
660 }
661
662 if (target != this) {
663 if (mOnExitAlarm.alarmPending()) {
664 mOnExitAlarm.cancelAlarm();
665 completeDragExit();
666 }
667 }
668 mDeleteFolderOnDropCompleted = false;
669 mDragInProgress = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700670 mItemAddedBackToSelfViaIcon = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700671 mCurrentDragInfo = null;
672 mCurrentDragView = null;
673 mSuppressOnAdd = false;
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700674 }
Adam Cohen228da5a2011-07-27 22:23:47 -0700675
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700676 public void notifyDrop() {
677 if (mDragInProgress) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700678 mItemAddedBackToSelfViaIcon = true;
Adam Cohen76078c42011-06-09 15:06:52 -0700679 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700680 }
681
682 public boolean isDropEnabled() {
683 return true;
684 }
685
Adam Cohencb3382b2011-05-24 14:07:08 -0700686 public DropTarget getDropTargetDelegate(DragObject d) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700687 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800688 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700689
Adam Cohen2801caf2011-05-13 20:57:39 -0700690 private void setupContentDimension(int count) {
691 ArrayList<View> list = getItemsInReadingOrder();
692
693 int countX = mContent.getCountX();
694 int countY = mContent.getCountY();
Adam Cohen7c693212011-05-18 15:26:57 -0700695 boolean done = false;
Adam Cohen2801caf2011-05-13 20:57:39 -0700696
Adam Cohen7c693212011-05-18 15:26:57 -0700697 while (!done) {
698 int oldCountX = countX;
699 int oldCountY = countY;
700 if (countX * countY < count) {
701 // Current grid is too small, expand it
702 if (countX <= countY && countX < mMaxCountX) {
703 countX++;
704 } else if (countY < mMaxCountY) {
705 countY++;
706 }
707 if (countY == 0) countY++;
708 } else if ((countY - 1) * countX >= count && countY >= countX) {
709 countY = Math.max(0, countY - 1);
710 } else if ((countX - 1) * countY >= count) {
711 countX = Math.max(0, countX - 1);
Adam Cohen2801caf2011-05-13 20:57:39 -0700712 }
Adam Cohen7c693212011-05-18 15:26:57 -0700713 done = countX == oldCountX && countY == oldCountY;
Adam Cohen2801caf2011-05-13 20:57:39 -0700714 }
Adam Cohen7c693212011-05-18 15:26:57 -0700715 mContent.setGridSize(countX, countY);
Adam Cohen2801caf2011-05-13 20:57:39 -0700716 arrangeChildren(list);
717 }
718
719 public boolean isFull() {
720 return getItemCount() >= mMaxCountX * mMaxCountY;
721 }
722
723 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700724 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700725
726 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf4bb1cd2011-07-22 14:36:03 -0700727 int height = getPaddingTop() + getPaddingBottom() + mContent.getDesiredHeight()
728 + mFolderNameHeight;
Adam Cohen8e776a62011-06-28 18:10:06 -0700729 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700730
Adam Cohen8e776a62011-06-28 18:10:06 -0700731 parent.getDescendantRectRelativeToSelf(mFolderIcon, mTempRect);
732
733 int centerX = mTempRect.centerX();
734 int centerY = mTempRect.centerY();
Adam Cohen2801caf2011-05-13 20:57:39 -0700735 int centeredLeft = centerX - width / 2;
736 int centeredTop = centerY - height / 2;
737
Adam Cohen35e7e642011-07-17 14:47:18 -0700738 // We first fetch the currently visible CellLayoutChildren
Winson Chung3d503fb2011-07-13 17:25:49 -0700739 CellLayout currentPage = mLauncher.getWorkspace().getCurrentDropLayout();
Adam Cohen35e7e642011-07-17 14:47:18 -0700740 CellLayoutChildren boundingLayout = currentPage.getChildrenLayout();
741 Rect bounds = new Rect();
742 parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
Adam Cohen2801caf2011-05-13 20:57:39 -0700743
Adam Cohen35e7e642011-07-17 14:47:18 -0700744 // We need to bound the folder to the currently visible CellLayoutChildren
745 int left = Math.min(Math.max(bounds.left, centeredLeft),
746 bounds.left + bounds.width() - width);
747 int top = Math.min(Math.max(bounds.top, centeredTop),
748 bounds.top + bounds.height() - height);
749 // If the folder doesn't fit within the bounds, center it about the desired bounds
750 if (width >= bounds.width()) {
751 left = bounds.left + (bounds.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700752 }
Adam Cohen35e7e642011-07-17 14:47:18 -0700753 if (height >= bounds.height()) {
754 top = bounds.top + (bounds.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700755 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700756
757 int folderPivotX = width / 2 + (centeredLeft - left);
758 int folderPivotY = height / 2 + (centeredTop - top);
759 setPivotX(folderPivotX);
760 setPivotY(folderPivotY);
761 int folderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
762 (1.0f * folderPivotX / width));
763 int folderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
764 (1.0f * folderPivotY / height));
765 mFolderIcon.setPivotX(folderIconPivotX);
766 mFolderIcon.setPivotY(folderIconPivotY);
767
768 if (mMode == PARTIAL_GROW) {
769 lp.width = width;
770 lp.height = height;
771 lp.x = left;
772 lp.y = top;
773 } else {
774 mNewSize.set(left, top, left + width, top + height);
775 }
776 }
777
778 private void setupContentForNumItems(int count) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700779 setupContentDimension(count);
780
Adam Cohen8e776a62011-06-28 18:10:06 -0700781 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700782 if (lp == null) {
Adam Cohen8e776a62011-06-28 18:10:06 -0700783 lp = new DragLayer.LayoutParams(0, 0);
784 lp.customPosition = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700785 setLayoutParams(lp);
786 }
787 centerAboutIcon();
788 }
789
Adam Cohen234c4cd2011-07-17 21:03:04 -0700790 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
791 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf4bb1cd2011-07-22 14:36:03 -0700792 int height = getPaddingTop() + getPaddingBottom() + mContent.getDesiredHeight()
793 + mFolderNameHeight;
Adam Cohen234c4cd2011-07-17 21:03:04 -0700794
795 int contentWidthSpec = MeasureSpec.makeMeasureSpec(mContent.getDesiredWidth(),
796 MeasureSpec.EXACTLY);
797 int contentHeightSpec = MeasureSpec.makeMeasureSpec(mContent.getDesiredHeight(),
798 MeasureSpec.EXACTLY);
799 mContent.measure(contentWidthSpec, contentHeightSpec);
800
801 mFolderName.measure(contentWidthSpec,
802 MeasureSpec.makeMeasureSpec(mFolderNameHeight, MeasureSpec.EXACTLY));
803 setMeasuredDimension(width, height);
804 }
805
Adam Cohen2801caf2011-05-13 20:57:39 -0700806 private void arrangeChildren(ArrayList<View> list) {
807 int[] vacant = new int[2];
808 if (list == null) {
809 list = getItemsInReadingOrder();
810 }
811 mContent.removeAllViews();
812
813 for (int i = 0; i < list.size(); i++) {
814 View v = list.get(i);
815 mContent.getVacantCell(vacant, 1, 1);
816 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) v.getLayoutParams();
817 lp.cellX = vacant[0];
818 lp.cellY = vacant[1];
819 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohen2792a332011-09-26 21:09:47 -0700820 if (info.cellX != vacant[0] || info.cellY != vacant[1]) {
821 info.cellX = vacant[0];
822 info.cellY = vacant[1];
823 LauncherModel.addOrMoveItemInDatabase(mLauncher, info, mInfo.id, 0,
824 info.cellX, info.cellY);
825 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700826 boolean insert = false;
827 mContent.addViewToCellLayout(v, insert ? 0 : -1, (int)info.id, lp, true);
828 }
Adam Cohen7c693212011-05-18 15:26:57 -0700829 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700830 }
831
Adam Cohena9cf38f2011-05-02 15:36:58 -0700832 public int getItemCount() {
833 return mContent.getChildrenLayout().getChildCount();
834 }
835
836 public View getItemAt(int index) {
837 return mContent.getChildrenLayout().getChildAt(index);
838 }
839
Adam Cohen2801caf2011-05-13 20:57:39 -0700840 private void onCloseComplete() {
Adam Cohen05e0f402011-08-01 12:12:49 -0700841 DragLayer parent = (DragLayer) getParent();
Adam Cohen4554ee12011-08-03 16:13:21 -0700842 parent.removeView(this);
843 mDragController.removeDropTarget((DropTarget) this);
Adam Cohen05e0f402011-08-01 12:12:49 -0700844 clearFocus();
845
Adam Cohen2801caf2011-05-13 20:57:39 -0700846 if (mRearrangeOnClose) {
847 setupContentForNumItems(getItemCount());
848 mRearrangeOnClose = false;
849 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700850 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700851 if (!mDragInProgress && !mSuppressFolderDeletion) {
852 replaceFolderWithFinalItem();
853 } else if (mDragInProgress) {
854 mDeleteFolderOnDropCompleted = true;
855 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700856 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700857 mSuppressFolderDeletion = false;
Adam Cohenafb01ee2011-06-23 15:38:03 -0700858 }
859
860 private void replaceFolderWithFinalItem() {
861 ItemInfo finalItem = null;
862
863 if (getItemCount() == 1) {
864 finalItem = mInfo.contents.get(0);
865 }
866
867 // Remove the folder completely
Winson Chung3d503fb2011-07-13 17:25:49 -0700868 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screen);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700869 cellLayout.removeView(mFolderIcon);
870 if (mFolderIcon instanceof DropTarget) {
871 mDragController.removeDropTarget((DropTarget) mFolderIcon);
872 }
873 mLauncher.removeFolder(mInfo);
874
875 if (finalItem != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700876 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
877 mInfo.screen, mInfo.cellX, mInfo.cellY);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700878 }
Adam Cohen716b51e2011-06-30 12:09:54 -0700879 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700880
881 // Add the last remaining child to the workspace in place of the folder
882 if (finalItem != null) {
883 View child = mLauncher.createShortcut(R.layout.application, cellLayout,
884 (ShortcutInfo) finalItem);
885
Winson Chung3d503fb2011-07-13 17:25:49 -0700886 mLauncher.getWorkspace().addInScreen(child, mInfo.container, mInfo.screen, mInfo.cellX,
887 mInfo.cellY, mInfo.spanX, mInfo.spanY);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700888 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700889 }
890
Adam Cohenbfbfd262011-06-13 16:55:12 -0700891 public void onDrop(DragObject d) {
892 ShortcutInfo item;
893 if (d.dragInfo instanceof ApplicationInfo) {
894 // Came from all apps -- make a copy
895 item = ((ApplicationInfo) d.dragInfo).makeShortcut();
896 item.spanX = 1;
897 item.spanY = 1;
898 } else {
899 item = (ShortcutInfo) d.dragInfo;
900 }
Adam Cohen05e0f402011-08-01 12:12:49 -0700901 // Dragged from self onto self, currently this is the only path possible, however
902 // we keep this as a distinct code path.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700903 if (item == mCurrentDragInfo) {
904 ShortcutInfo si = (ShortcutInfo) mCurrentDragView.getTag();
905 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) mCurrentDragView.getLayoutParams();
906 si.cellX = lp.cellX = mEmptyCell[0];
907 si.cellX = lp.cellY = mEmptyCell[1];
908 mContent.addViewToCellLayout(mCurrentDragView, -1, (int)item.id, lp, true);
Adam Cohenfc53cd22011-07-20 15:45:11 -0700909 if (d.dragView.hasDrawn()) {
910 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, mCurrentDragView);
911 } else {
912 mCurrentDragView.setVisibility(VISIBLE);
913 }
Adam Cohene9166b22011-07-08 17:11:11 -0700914 mItemsInvalidated = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700915 setupContentDimension(getItemCount());
Adam Cohen716b51e2011-06-30 12:09:54 -0700916 mSuppressOnAdd = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700917 }
918 mInfo.add(item);
919 }
920
921 public void onAdd(ShortcutInfo item) {
922 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700923 // If the item was dropped onto this open folder, we have done the work associated
924 // with adding the item to the folder, as indicated by mSuppressOnAdd being set
Adam Cohenbfbfd262011-06-13 16:55:12 -0700925 if (mSuppressOnAdd) return;
926 if (!findAndSetEmptyCells(item)) {
927 // The current layout is full, can we expand it?
928 setupContentForNumItems(getItemCount() + 1);
929 findAndSetEmptyCells(item);
930 }
931 createAndAddShortcut(item);
932 LauncherModel.addOrMoveItemInDatabase(
933 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
934 }
935
Adam Cohena9cf38f2011-05-02 15:36:58 -0700936 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -0700937 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700938 // If this item is being dragged from this open folder, we have already handled
939 // the work associated with removing the item, so we don't have to do anything here.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700940 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -0700941 View v = getViewForInfo(item);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700942 mContent.removeView(v);
Adam Cohen2801caf2011-05-13 20:57:39 -0700943 if (mState == STATE_ANIMATING) {
944 mRearrangeOnClose = true;
945 } else {
946 setupContentForNumItems(getItemCount());
947 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700948 if (getItemCount() <= 1) {
949 replaceFolderWithFinalItem();
950 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700951 }
Adam Cohen7c693212011-05-18 15:26:57 -0700952
Adam Cohendf1e4e82011-06-24 15:57:39 -0700953 private View getViewForInfo(ShortcutInfo item) {
954 for (int j = 0; j < mContent.getCountY(); j++) {
955 for (int i = 0; i < mContent.getCountX(); i++) {
956 View v = mContent.getChildAt(i, j);
957 if (v.getTag() == item) {
958 return v;
959 }
960 }
961 }
962 return null;
963 }
964
Adam Cohen76078c42011-06-09 15:06:52 -0700965 public void onItemsChanged() {
966 }
Adam Cohen76fc0852011-06-17 13:26:23 -0700967 public void onTitleChanged(CharSequence title) {
968 }
Adam Cohen76078c42011-06-09 15:06:52 -0700969
Adam Cohen7c693212011-05-18 15:26:57 -0700970 public ArrayList<View> getItemsInReadingOrder() {
Adam Cohen76078c42011-06-09 15:06:52 -0700971 return getItemsInReadingOrder(true);
972 }
973
974 public ArrayList<View> getItemsInReadingOrder(boolean includeCurrentDragItem) {
Adam Cohen7c693212011-05-18 15:26:57 -0700975 if (mItemsInvalidated) {
976 mItemsInReadingOrder.clear();
977 for (int j = 0; j < mContent.getCountY(); j++) {
978 for (int i = 0; i < mContent.getCountX(); i++) {
979 View v = mContent.getChildAt(i, j);
980 if (v != null) {
Adam Cohen76078c42011-06-09 15:06:52 -0700981 ShortcutInfo info = (ShortcutInfo) v.getTag();
982 if (info != mCurrentDragInfo || includeCurrentDragItem) {
983 mItemsInReadingOrder.add(v);
984 }
Adam Cohen7c693212011-05-18 15:26:57 -0700985 }
986 }
987 }
988 mItemsInvalidated = false;
989 }
990 return mItemsInReadingOrder;
991 }
Adam Cohen8dfcba42011-07-07 16:38:18 -0700992
993 public void getLocationInDragLayer(int[] loc) {
994 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
995 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800996}