blob: 652c8b03d4ffbbace947744343be6efcc35d9c07 [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 Cohen8dfcba42011-07-07 16:38:18 -0700100 private boolean mFirstOpen = true;
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700101 private boolean mDragInProgress = false;
102 private boolean mDeleteFolderOnDropCompleted = false;
103 private boolean mSuppressFolderDeletion = false;
104 private boolean mItemAddedBackToSelf = false;
Adam Cohen228da5a2011-07-27 22:23:47 -0700105
Adam Cohen76fc0852011-06-17 13:26:23 -0700106 private boolean mIsEditingName = false;
107 private InputMethodManager mInputMethodManager;
Adam Cohendf2cc412011-04-27 16:56:57 -0700108
Adam Cohena65beee2011-06-27 21:32:23 -0700109 private static String sDefaultFolderName;
110 private static String sHintText;
111
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800112 /**
113 * Used to inflate the Workspace from XML.
114 *
115 * @param context The application's context.
116 * @param attrs The attribtues set containing the Workspace's customization values.
117 */
118 public Folder(Context context, AttributeSet attrs) {
119 super(context, attrs);
120 setAlwaysDrawnWithCacheEnabled(false);
Adam Cohendf2cc412011-04-27 16:56:57 -0700121 mInflater = LayoutInflater.from(context);
122 mIconCache = ((LauncherApplication)context.getApplicationContext()).getIconCache();
Adam Cohen8e776a62011-06-28 18:10:06 -0700123 mMaxCountX = LauncherModel.getCellCountX();
124 mMaxCountY = LauncherModel.getCellCountY();
Adam Cohen76fc0852011-06-17 13:26:23 -0700125
126 mInputMethodManager = (InputMethodManager)
127 mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
128
129 Resources res = getResources();
130 mExpandDuration = res.getInteger(R.integer.config_folderAnimDuration);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700131
132 if (sDefaultFolderName == null) {
133 sDefaultFolderName = res.getString(R.string.folder_name);
134 }
Adam Cohena65beee2011-06-27 21:32:23 -0700135 if (sHintText == null) {
136 sHintText = res.getString(R.string.folder_hint_text);
137 }
Adam Cohen4eac29a2011-07-11 17:53:37 -0700138
139 mLauncher = (Launcher) context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140 }
141
142 @Override
143 protected void onFinishInflate() {
144 super.onFinishInflate();
Adam Cohendf2cc412011-04-27 16:56:57 -0700145 mContent = (CellLayout) findViewById(R.id.folder_content);
Adam Cohen2801caf2011-05-13 20:57:39 -0700146 mContent.setGridSize(0, 0);
Adam Cohen76fc0852011-06-17 13:26:23 -0700147 mFolderName = (TextView) findViewById(R.id.folder_name);
148
149 // We find out how tall the text view wants to be (it is set to wrap_content), so that
150 // we can allocate the appropriate amount of space for it.
151 int measureSpec = MeasureSpec.UNSPECIFIED;
152 mFolderName.measure(measureSpec, measureSpec);
153 mFolderNameHeight = mFolderName.getMeasuredHeight();
154
155 // We disable action mode for now since it messes up the view on phones
156 mFolderName.setCustomSelectionActionModeCallback(mActionModeCallback);
157 mFolderName.setCursorVisible(false);
158 mFolderName.setOnEditorActionListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700159 mFolderName.setSelectAllOnFocus(true);
Adam Cohen7a14d0b2011-06-24 11:36:35 -0700160 mFolderName.setInputType(mFolderName.getInputType() |
161 InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700163
Adam Cohen76fc0852011-06-17 13:26:23 -0700164 private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
165 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
166 return false;
167 }
168
169 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
170 return false;
171 }
172
173 public void onDestroyActionMode(ActionMode mode) {
174 }
175
176 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
177 return false;
178 }
179 };
180
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181 public void onClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700182 Object tag = v.getTag();
183 if (tag instanceof ShortcutInfo) {
184 // refactor this code from Folder
185 ShortcutInfo item = (ShortcutInfo) tag;
186 int[] pos = new int[2];
187 v.getLocationOnScreen(pos);
188 item.intent.setSourceBounds(new Rect(pos[0], pos[1],
189 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
190 mLauncher.startActivitySafely(item.intent, item);
Adam Cohendf2cc412011-04-27 16:56:57 -0700191 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192 }
193
Adam Cohen76fc0852011-06-17 13:26:23 -0700194 public boolean onInterceptTouchEvent(MotionEvent ev) {
195 if (ev.getAction() == MotionEvent.ACTION_DOWN) {
196 mFolderName.getHitRect(mHitRect);
197 if (mHitRect.contains((int) ev.getX(), (int) ev.getY()) && !mIsEditingName) {
198 startEditingFolderName();
199 }
200 }
201 return false;
202 }
203
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800204 public boolean onLongClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700205 Object tag = v.getTag();
206 if (tag instanceof ShortcutInfo) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700207 ShortcutInfo item = (ShortcutInfo) tag;
208 if (!v.isInTouchMode()) {
209 return false;
210 }
211
212 mLauncher.getWorkspace().onDragStartedWithItem(v);
Adam Cohenac8c8762011-07-13 11:15:27 -0700213 mLauncher.getWorkspace().beginDragShared(v, this);
Adam Cohenbadf71e2011-05-26 19:08:29 -0700214 mIconDrawable = ((TextView) v).getCompoundDrawables()[1];
Adam Cohen76078c42011-06-09 15:06:52 -0700215
216 mCurrentDragInfo = item;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700217 mEmptyCell[0] = item.cellX;
218 mEmptyCell[1] = item.cellY;
219 mCurrentDragView = v;
Adam Cohenfc53cd22011-07-20 15:45:11 -0700220
221 mContent.removeView(mCurrentDragView);
222 mInfo.remove(mCurrentDragInfo);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700223 mDragInProgress = true;
224 mItemAddedBackToSelf = false;
Adam Cohendf2cc412011-04-27 16:56:57 -0700225 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800226 return true;
227 }
228
Adam Cohen76fc0852011-06-17 13:26:23 -0700229 public boolean isEditingName() {
230 return mIsEditingName;
231 }
232
233 public void startEditingFolderName() {
Adam Cohena65beee2011-06-27 21:32:23 -0700234 mFolderName.setHint("");
Adam Cohen76fc0852011-06-17 13:26:23 -0700235 mFolderName.setCursorVisible(true);
236 mIsEditingName = true;
237 }
238
239 public void dismissEditingName() {
240 mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
241 doneEditingFolderName(true);
242 }
243
244 public void doneEditingFolderName(boolean commit) {
Adam Cohena65beee2011-06-27 21:32:23 -0700245 mFolderName.setHint(sHintText);
Adam Cohen76fc0852011-06-17 13:26:23 -0700246 mInfo.setTitle(mFolderName.getText());
247 LauncherModel.updateItemInDatabase(mLauncher, mInfo);
248 mFolderName.setCursorVisible(false);
249 mFolderName.clearFocus();
Adam Cohene601a432011-07-26 21:51:30 -0700250 Selection.setSelection((Spannable) mFolderName.getText(), 0, 0);
Adam Cohen76fc0852011-06-17 13:26:23 -0700251 mIsEditingName = false;
252 }
253
254 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
255 if (actionId == EditorInfo.IME_ACTION_DONE) {
256 dismissEditingName();
257 return true;
258 }
259 return false;
260 }
261
262 public View getEditTextRegion() {
263 return mFolderName;
264 }
265
Adam Cohenbadf71e2011-05-26 19:08:29 -0700266 public Drawable getDragDrawable() {
267 return mIconDrawable;
268 }
269
Adam Cohen0c872ba2011-05-05 10:34:16 -0700270 /**
271 * We need to handle touch events to prevent them from falling through to the workspace below.
272 */
273 @Override
274 public boolean onTouchEvent(MotionEvent ev) {
275 return true;
276 }
277
Joe Onorato00acb122009-08-04 16:04:30 -0400278 public void setDragController(DragController dragController) {
279 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800280 }
281
Adam Cohen2801caf2011-05-13 20:57:39 -0700282 void setFolderIcon(FolderIcon icon) {
283 mFolderIcon = icon;
284 }
285
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800286 /**
287 * @return the FolderInfo object associated with this folder
288 */
289 FolderInfo getInfo() {
290 return mInfo;
291 }
292
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800293 void onOpen() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700294 // When the folder opens, we need to refresh the GridView's selection by
295 // forcing a layout
296 // TODO: find out if this is still necessary
297 mContent.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800298 }
299
300 void onClose() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700301 DragLayer parent = (DragLayer) getParent();
302 parent.removeView(Folder.this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700303 clearFocus();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800304 }
305
306 void bind(FolderInfo info) {
307 mInfo = info;
Adam Cohendf2cc412011-04-27 16:56:57 -0700308 ArrayList<ShortcutInfo> children = info.contents;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700309 ArrayList<ShortcutInfo> overflow = new ArrayList<ShortcutInfo>();
Adam Cohen7c693212011-05-18 15:26:57 -0700310 setupContentForNumItems(children.size());
Adam Cohendf2cc412011-04-27 16:56:57 -0700311 for (int i = 0; i < children.size(); i++) {
312 ShortcutInfo child = (ShortcutInfo) children.get(i);
Adam Cohenc508b2d2011-06-28 14:41:44 -0700313 if (!createAndAddShortcut(child)) {
314 overflow.add(child);
315 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700316 }
Adam Cohenc508b2d2011-06-28 14:41:44 -0700317
318 // If our folder has too many items we prune them from the list. This is an issue
319 // when upgrading from the old Folders implementation which could contain an unlimited
320 // number of items.
321 for (ShortcutInfo item: overflow) {
322 mInfo.remove(item);
323 LauncherModel.deleteItemFromDatabase(mLauncher, item);
324 }
325
Adam Cohen4dbe6d92011-05-18 17:14:15 -0700326 mItemsInvalidated = true;
Adam Cohena9cf38f2011-05-02 15:36:58 -0700327 mInfo.addListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700328
Adam Cohenafb01ee2011-06-23 15:38:03 -0700329 if (!sDefaultFolderName.contentEquals(mInfo.title)) {
Adam Cohen4ef610f2011-06-21 22:37:36 -0700330 mFolderName.setText(mInfo.title);
331 } else {
332 mFolderName.setText("");
333 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700334 }
335
336 /**
337 * Creates a new UserFolder, inflated from R.layout.user_folder.
338 *
339 * @param context The application's context.
340 *
341 * @return A new UserFolder.
342 */
343 static Folder fromXml(Context context) {
344 return (Folder) LayoutInflater.from(context).inflate(R.layout.user_folder, null);
345 }
346
347 /**
348 * This method is intended to make the UserFolder to be visually identical in size and position
349 * to its associated FolderIcon. This allows for a seamless transition into the expanded state.
350 */
351 private void positionAndSizeAsIcon() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700352 if (!(getParent() instanceof DragLayer)) return;
Adam Cohendf2cc412011-04-27 16:56:57 -0700353
Adam Cohen8e776a62011-06-28 18:10:06 -0700354 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohendf2cc412011-04-27 16:56:57 -0700355
Adam Cohen2801caf2011-05-13 20:57:39 -0700356 if (mMode == PARTIAL_GROW) {
357 setScaleX(0.8f);
358 setScaleY(0.8f);
359 setAlpha(0f);
360 } else {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700361 mLauncher.getDragLayer().getDescendantRectRelativeToSelf(mFolderIcon, mIconRect);
362 lp.width = mIconRect.width();
363 lp.height = mIconRect.height();
364 lp.x = mIconRect.left;
365 lp.y = mIconRect.top;
Adam Cohen2801caf2011-05-13 20:57:39 -0700366 mContent.setAlpha(0);
367 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700368 mState = STATE_SMALL;
369 }
370
371 public void animateOpen() {
Adam Cohen8dfcba42011-07-07 16:38:18 -0700372 if (mFirstOpen) {
373 setLayerType(LAYER_TYPE_HARDWARE, null);
374 buildLayer();
375 mFirstOpen = false;
376 }
377
Adam Cohen3e8f8112011-07-02 18:03:00 -0700378 positionAndSizeAsIcon();
379
Adam Cohen8e776a62011-06-28 18:10:06 -0700380 if (!(getParent() instanceof DragLayer)) return;
Adam Cohendf2cc412011-04-27 16:56:57 -0700381
Adam Cohen2801caf2011-05-13 20:57:39 -0700382 ObjectAnimator oa;
Adam Cohen8e776a62011-06-28 18:10:06 -0700383 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohendf2cc412011-04-27 16:56:57 -0700384
Adam Cohen2801caf2011-05-13 20:57:39 -0700385 centerAboutIcon();
386 if (mMode == PARTIAL_GROW) {
387 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1);
388 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
389 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
390 oa = ObjectAnimator.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
391 } else {
392 PropertyValuesHolder width = PropertyValuesHolder.ofInt("width", mNewSize.width());
393 PropertyValuesHolder height = PropertyValuesHolder.ofInt("height", mNewSize.height());
394 PropertyValuesHolder x = PropertyValuesHolder.ofInt("x", mNewSize.left);
395 PropertyValuesHolder y = PropertyValuesHolder.ofInt("y", mNewSize.top);
396 oa = ObjectAnimator.ofPropertyValuesHolder(lp, width, height, x, y);
397 oa.addUpdateListener(new AnimatorUpdateListener() {
398 public void onAnimationUpdate(ValueAnimator animation) {
399 requestLayout();
400 }
401 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700402
Adam Cohen2801caf2011-05-13 20:57:39 -0700403 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
404 ObjectAnimator alphaOa = ObjectAnimator.ofPropertyValuesHolder(mContent, alpha);
405 alphaOa.setDuration(mExpandDuration);
406 alphaOa.setInterpolator(new AccelerateInterpolator(2.0f));
407 alphaOa.start();
408 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700409
Adam Cohen2801caf2011-05-13 20:57:39 -0700410 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700411 @Override
412 public void onAnimationStart(Animator animation) {
413 mState = STATE_ANIMATING;
414 }
415 @Override
416 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700417 mState = STATE_OPEN;
Adam Cohen8dfcba42011-07-07 16:38:18 -0700418 setLayerType(LAYER_TYPE_NONE, null);
419 enableHardwareLayersForChildren();
Adam Cohendf2cc412011-04-27 16:56:57 -0700420 }
421 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700422 oa.setDuration(mExpandDuration);
423 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700424 }
425
Adam Cohen8dfcba42011-07-07 16:38:18 -0700426 void enableHardwareLayersForChildren() {
427 ArrayList<View> children = getItemsInReadingOrder();
428 for (View child: children) {
429 child.setLayerType(LAYER_TYPE_HARDWARE, null);
430 }
431 }
432
Adam Cohendf2cc412011-04-27 16:56:57 -0700433 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700434 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen8dfcba42011-07-07 16:38:18 -0700435 setLayerType(LAYER_TYPE_HARDWARE, null);
436 buildLayer();
Adam Cohendf2cc412011-04-27 16:56:57 -0700437
Adam Cohen2801caf2011-05-13 20:57:39 -0700438 ObjectAnimator oa;
Adam Cohen2801caf2011-05-13 20:57:39 -0700439 if (mMode == PARTIAL_GROW) {
440 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
441 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
442 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
443 oa = ObjectAnimator.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
444 } else {
Adam Cohen8e776a62011-06-28 18:10:06 -0700445 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohendf2cc412011-04-27 16:56:57 -0700446
Adam Cohen3e8f8112011-07-02 18:03:00 -0700447 PropertyValuesHolder width = PropertyValuesHolder.ofInt("width", mIconRect.width());
448 PropertyValuesHolder height = PropertyValuesHolder.ofInt("height", mIconRect.height());
449 PropertyValuesHolder x = PropertyValuesHolder.ofInt("x", mIconRect.left);
450 PropertyValuesHolder y = PropertyValuesHolder.ofInt("y", mIconRect.top);
Adam Cohen2801caf2011-05-13 20:57:39 -0700451 oa = ObjectAnimator.ofPropertyValuesHolder(lp, width, height, x, y);
452 oa.addUpdateListener(new AnimatorUpdateListener() {
453 public void onAnimationUpdate(ValueAnimator animation) {
454 requestLayout();
455 }
456 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700457
Adam Cohen2801caf2011-05-13 20:57:39 -0700458 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0f);
459 ObjectAnimator alphaOa = ObjectAnimator.ofPropertyValuesHolder(mContent, alpha);
460 alphaOa.setDuration(mExpandDuration);
461 alphaOa.setInterpolator(new DecelerateInterpolator(2.0f));
462 alphaOa.start();
463 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700464
Adam Cohen2801caf2011-05-13 20:57:39 -0700465 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700466 @Override
467 public void onAnimationEnd(Animator animation) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700468 onClose();
Adam Cohen2801caf2011-05-13 20:57:39 -0700469 onCloseComplete();
Adam Cohen2801caf2011-05-13 20:57:39 -0700470 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700471 }
472 @Override
473 public void onAnimationStart(Animator animation) {
474 mState = STATE_ANIMATING;
475 }
476 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700477 oa.setDuration(mExpandDuration);
478 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700479 }
480
481 void notifyDataSetChanged() {
482 // recreate all the children if the data set changes under us. We may want to do this more
483 // intelligently (ie just removing the views that should no longer exist)
484 mContent.removeAllViewsInLayout();
485 bind(mInfo);
486 }
487
Adam Cohencb3382b2011-05-24 14:07:08 -0700488 public boolean acceptDrop(DragObject d) {
489 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700490 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700491 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
492 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
493 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700494 }
495
Adam Cohendf2cc412011-04-27 16:56:57 -0700496 protected boolean findAndSetEmptyCells(ShortcutInfo item) {
497 int[] emptyCell = new int[2];
498 if (mContent.findCellForSpan(emptyCell, item.spanX, item.spanY)) {
499 item.cellX = emptyCell[0];
500 item.cellY = emptyCell[1];
Adam Cohendf2cc412011-04-27 16:56:57 -0700501 return true;
502 } else {
503 return false;
504 }
505 }
506
Adam Cohenc508b2d2011-06-28 14:41:44 -0700507 protected boolean createAndAddShortcut(ShortcutInfo item) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700508 final TextView textView =
Adam Cohene87b9242011-06-29 14:01:26 -0700509 (TextView) mInflater.inflate(R.layout.application, this, false);
Adam Cohendf2cc412011-04-27 16:56:57 -0700510 textView.setCompoundDrawablesWithIntrinsicBounds(null,
511 new FastBitmapDrawable(item.getIcon(mIconCache)), null, null);
512 textView.setText(item.title);
513 textView.setTag(item);
514
515 textView.setOnClickListener(this);
516 textView.setOnLongClickListener(this);
517
Adam Cohenc508b2d2011-06-28 14:41:44 -0700518 // We need to check here to verify that the given item's location isn't already occupied
519 // by another item. If it is, we need to find the next available slot and assign
520 // it that position. This is an issue when upgrading from the old Folders implementation
521 // which could contain an unlimited number of items.
Adam Cohen228da5a2011-07-27 22:23:47 -0700522 if (mContent.getChildAt(item.cellX, item.cellY) != null ||
523 item.cellX < 0 || item.cellY < 0) {
Adam Cohenc508b2d2011-06-28 14:41:44 -0700524 if (!findAndSetEmptyCells(item)) {
525 return false;
526 }
527 }
528
Adam Cohendf2cc412011-04-27 16:56:57 -0700529 CellLayout.LayoutParams lp =
530 new CellLayout.LayoutParams(item.cellX, item.cellY, item.spanX, item.spanY);
531 boolean insert = false;
532 mContent.addViewToCellLayout(textView, insert ? 0 : -1, (int)item.id, lp, true);
Adam Cohenc508b2d2011-06-28 14:41:44 -0700533 return true;
Adam Cohendf2cc412011-04-27 16:56:57 -0700534 }
535
Adam Cohencb3382b2011-05-24 14:07:08 -0700536 public void onDragEnter(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700537 mPreviousTargetCell[0] = -1;
538 mPreviousTargetCell[1] = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700539 mOnExitAlarm.cancelAlarm();
540 }
541
542 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
543 public void onAlarm(Alarm alarm) {
544 realTimeReorder(mEmptyCell, mTargetCell);
545 }
546 };
547
548 boolean readingOrderGreaterThan(int[] v1, int[] v2) {
549 if (v1[1] > v2[1] || (v1[1] == v2[1] && v1[0] > v2[0])) {
550 return true;
551 } else {
552 return false;
553 }
554 }
555
556 private void realTimeReorder(int[] empty, int[] target) {
557 boolean wrap;
558 int startX;
559 int endX;
560 int startY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700561 int delay = 0;
562 float delayAmount = 30;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700563 if (readingOrderGreaterThan(target, empty)) {
564 wrap = empty[0] >= mContent.getCountX() - 1;
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 : 0;
568 endX = y < target[1] ? mContent.getCountX() - 1 : 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 } else {
581 wrap = empty[0] == 0;
582 startY = wrap ? empty[1] - 1 : empty[1];
583 for (int y = startY; y >= target[1]; y--) {
584 startX = y == empty[1] ? empty[0] - 1 : mContent.getCountX() - 1;
585 endX = y > target[1] ? 0 : target[0];
586 for (int x = startX; x >= endX; x--) {
587 View v = mContent.getChildAt(x,y);
588 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen76fc0852011-06-17 13:26:23 -0700589 REORDER_ANIMATION_DURATION, delay)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700590 empty[0] = x;
591 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700592 delay += delayAmount;
593 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700594 }
595 }
596 }
597 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700598 }
599
Adam Cohencb3382b2011-05-24 14:07:08 -0700600 public void onDragOver(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700601 float[] r = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, d.dragView, null);
602 mTargetCell = mContent.findNearestArea((int) r[0], (int) r[1], 1, 1, mTargetCell);
603
604 if (mTargetCell[0] != mPreviousTargetCell[0] || mTargetCell[1] != mPreviousTargetCell[1]) {
605 mReorderAlarm.cancelAlarm();
606 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
607 mReorderAlarm.setAlarm(150);
608 mPreviousTargetCell[0] = mTargetCell[0];
609 mPreviousTargetCell[1] = mTargetCell[1];
610 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700611 }
612
Adam Cohenbfbfd262011-06-13 16:55:12 -0700613 // This is used to compute the visual center of the dragView. The idea is that
614 // the visual center represents the user's interpretation of where the item is, and hence
615 // is the appropriate point to use when determining drop location.
616 private float[] getDragViewVisualCenter(int x, int y, int xOffset, int yOffset,
617 DragView dragView, float[] recycle) {
618 float res[];
619 if (recycle == null) {
620 res = new float[2];
621 } else {
622 res = recycle;
623 }
624
625 // These represent the visual top and left of drag view if a dragRect was provided.
626 // If a dragRect was not provided, then they correspond to the actual view left and
627 // top, as the dragRect is in that case taken to be the entire dragView.
628 // R.dimen.dragViewOffsetY.
629 int left = x - xOffset;
630 int top = y - yOffset;
631
632 // In order to find the visual center, we shift by half the dragRect
633 res[0] = left + dragView.getDragRegion().width() / 2;
634 res[1] = top + dragView.getDragRegion().height() / 2;
635
636 return res;
637 }
638
639 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
640 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700641 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700642 }
643 };
644
Adam Cohen95bb8002011-07-03 23:40:28 -0700645 public void completeDragExit() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700646 mLauncher.closeFolder();
647 mCurrentDragInfo = null;
648 mCurrentDragView = null;
649 mSuppressOnAdd = false;
650 mRearrangeOnClose = true;
651 }
652
Adam Cohencb3382b2011-05-24 14:07:08 -0700653 public void onDragExit(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700654 // We only close the folder if this is a true drag exit, ie. not because a drop
655 // has occurred above the folder.
656 if (!d.dragComplete) {
657 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
658 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
659 }
660 mReorderAlarm.cancelAlarm();
Adam Cohen2801caf2011-05-13 20:57:39 -0700661 }
662
Adam Cohenc0dcf592011-06-01 15:30:43 -0700663 public void onDropCompleted(View target, DragObject d, boolean success) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700664 if (success) {
665 if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelf) {
666 replaceFolderWithFinalItem();
667 }
668 } else {
669 // The drag failed, we need to return the item to the folder
670 mFolderIcon.onDrop(d);
671
672 // We're going to trigger a "closeFolder" which may occur before this item has
673 // been added back to the folder -- this could cause the folder to be deleted
674 if (mOnExitAlarm.alarmPending()) {
675 mSuppressFolderDeletion = true;
676 }
677 }
678
679 if (target != this) {
680 if (mOnExitAlarm.alarmPending()) {
681 mOnExitAlarm.cancelAlarm();
682 completeDragExit();
683 }
684 }
685 mDeleteFolderOnDropCompleted = false;
686 mDragInProgress = false;
687 mItemAddedBackToSelf = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700688 mCurrentDragInfo = null;
689 mCurrentDragView = null;
690 mSuppressOnAdd = false;
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700691 }
Adam Cohen228da5a2011-07-27 22:23:47 -0700692
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700693 public void notifyDrop() {
694 if (mDragInProgress) {
695 mItemAddedBackToSelf = true;
Adam Cohen76078c42011-06-09 15:06:52 -0700696 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700697 }
698
699 public boolean isDropEnabled() {
700 return true;
701 }
702
Adam Cohencb3382b2011-05-24 14:07:08 -0700703 public DropTarget getDropTargetDelegate(DragObject d) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700704 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800705 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700706
Adam Cohen2801caf2011-05-13 20:57:39 -0700707 private void setupContentDimension(int count) {
708 ArrayList<View> list = getItemsInReadingOrder();
709
710 int countX = mContent.getCountX();
711 int countY = mContent.getCountY();
Adam Cohen7c693212011-05-18 15:26:57 -0700712 boolean done = false;
Adam Cohen2801caf2011-05-13 20:57:39 -0700713
Adam Cohen7c693212011-05-18 15:26:57 -0700714 while (!done) {
715 int oldCountX = countX;
716 int oldCountY = countY;
717 if (countX * countY < count) {
718 // Current grid is too small, expand it
719 if (countX <= countY && countX < mMaxCountX) {
720 countX++;
721 } else if (countY < mMaxCountY) {
722 countY++;
723 }
724 if (countY == 0) countY++;
725 } else if ((countY - 1) * countX >= count && countY >= countX) {
726 countY = Math.max(0, countY - 1);
727 } else if ((countX - 1) * countY >= count) {
728 countX = Math.max(0, countX - 1);
Adam Cohen2801caf2011-05-13 20:57:39 -0700729 }
Adam Cohen7c693212011-05-18 15:26:57 -0700730 done = countX == oldCountX && countY == oldCountY;
Adam Cohen2801caf2011-05-13 20:57:39 -0700731 }
Adam Cohen7c693212011-05-18 15:26:57 -0700732 mContent.setGridSize(countX, countY);
Adam Cohen2801caf2011-05-13 20:57:39 -0700733 arrangeChildren(list);
734 }
735
736 public boolean isFull() {
737 return getItemCount() >= mMaxCountX * mMaxCountY;
738 }
739
740 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700741 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700742
743 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf4bb1cd2011-07-22 14:36:03 -0700744 int height = getPaddingTop() + getPaddingBottom() + mContent.getDesiredHeight()
745 + mFolderNameHeight;
Adam Cohen8e776a62011-06-28 18:10:06 -0700746 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700747
Adam Cohen8e776a62011-06-28 18:10:06 -0700748 parent.getDescendantRectRelativeToSelf(mFolderIcon, mTempRect);
749
750 int centerX = mTempRect.centerX();
751 int centerY = mTempRect.centerY();
Adam Cohen2801caf2011-05-13 20:57:39 -0700752 int centeredLeft = centerX - width / 2;
753 int centeredTop = centerY - height / 2;
754
Adam Cohen35e7e642011-07-17 14:47:18 -0700755 // We first fetch the currently visible CellLayoutChildren
Winson Chung3d503fb2011-07-13 17:25:49 -0700756 CellLayout currentPage = mLauncher.getWorkspace().getCurrentDropLayout();
Adam Cohen35e7e642011-07-17 14:47:18 -0700757 CellLayoutChildren boundingLayout = currentPage.getChildrenLayout();
758 Rect bounds = new Rect();
759 parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
Adam Cohen2801caf2011-05-13 20:57:39 -0700760
Adam Cohen35e7e642011-07-17 14:47:18 -0700761 // We need to bound the folder to the currently visible CellLayoutChildren
762 int left = Math.min(Math.max(bounds.left, centeredLeft),
763 bounds.left + bounds.width() - width);
764 int top = Math.min(Math.max(bounds.top, centeredTop),
765 bounds.top + bounds.height() - height);
766 // If the folder doesn't fit within the bounds, center it about the desired bounds
767 if (width >= bounds.width()) {
768 left = bounds.left + (bounds.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700769 }
Adam Cohen35e7e642011-07-17 14:47:18 -0700770 if (height >= bounds.height()) {
771 top = bounds.top + (bounds.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700772 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700773
774 int folderPivotX = width / 2 + (centeredLeft - left);
775 int folderPivotY = height / 2 + (centeredTop - top);
776 setPivotX(folderPivotX);
777 setPivotY(folderPivotY);
778 int folderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
779 (1.0f * folderPivotX / width));
780 int folderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
781 (1.0f * folderPivotY / height));
782 mFolderIcon.setPivotX(folderIconPivotX);
783 mFolderIcon.setPivotY(folderIconPivotY);
784
785 if (mMode == PARTIAL_GROW) {
786 lp.width = width;
787 lp.height = height;
788 lp.x = left;
789 lp.y = top;
790 } else {
791 mNewSize.set(left, top, left + width, top + height);
792 }
793 }
794
795 private void setupContentForNumItems(int count) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700796 setupContentDimension(count);
797
Adam Cohen8e776a62011-06-28 18:10:06 -0700798 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700799 if (lp == null) {
Adam Cohen8e776a62011-06-28 18:10:06 -0700800 lp = new DragLayer.LayoutParams(0, 0);
801 lp.customPosition = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700802 setLayoutParams(lp);
803 }
804 centerAboutIcon();
805 }
806
Adam Cohen234c4cd2011-07-17 21:03:04 -0700807 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
808 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf4bb1cd2011-07-22 14:36:03 -0700809 int height = getPaddingTop() + getPaddingBottom() + mContent.getDesiredHeight()
810 + mFolderNameHeight;
Adam Cohen234c4cd2011-07-17 21:03:04 -0700811
812 int contentWidthSpec = MeasureSpec.makeMeasureSpec(mContent.getDesiredWidth(),
813 MeasureSpec.EXACTLY);
814 int contentHeightSpec = MeasureSpec.makeMeasureSpec(mContent.getDesiredHeight(),
815 MeasureSpec.EXACTLY);
816 mContent.measure(contentWidthSpec, contentHeightSpec);
817
818 mFolderName.measure(contentWidthSpec,
819 MeasureSpec.makeMeasureSpec(mFolderNameHeight, MeasureSpec.EXACTLY));
820 setMeasuredDimension(width, height);
821 }
822
Adam Cohen2801caf2011-05-13 20:57:39 -0700823 private void arrangeChildren(ArrayList<View> list) {
824 int[] vacant = new int[2];
825 if (list == null) {
826 list = getItemsInReadingOrder();
827 }
828 mContent.removeAllViews();
829
830 for (int i = 0; i < list.size(); i++) {
831 View v = list.get(i);
832 mContent.getVacantCell(vacant, 1, 1);
833 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) v.getLayoutParams();
834 lp.cellX = vacant[0];
835 lp.cellY = vacant[1];
836 ItemInfo info = (ItemInfo) v.getTag();
837 info.cellX = vacant[0];
838 info.cellY = vacant[1];
839 boolean insert = false;
840 mContent.addViewToCellLayout(v, insert ? 0 : -1, (int)info.id, lp, true);
Adam Cohen7c693212011-05-18 15:26:57 -0700841 LauncherModel.addOrMoveItemInDatabase(mLauncher, info, mInfo.id, 0,
842 info.cellX, info.cellY);
Adam Cohen2801caf2011-05-13 20:57:39 -0700843 }
Adam Cohen7c693212011-05-18 15:26:57 -0700844 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700845 }
846
Adam Cohena9cf38f2011-05-02 15:36:58 -0700847 public int getItemCount() {
848 return mContent.getChildrenLayout().getChildCount();
849 }
850
851 public View getItemAt(int index) {
852 return mContent.getChildrenLayout().getChildAt(index);
853 }
854
Adam Cohen2801caf2011-05-13 20:57:39 -0700855 private void onCloseComplete() {
856 if (mRearrangeOnClose) {
857 setupContentForNumItems(getItemCount());
858 mRearrangeOnClose = false;
859 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700860 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700861 if (!mDragInProgress && !mSuppressFolderDeletion) {
862 replaceFolderWithFinalItem();
863 } else if (mDragInProgress) {
864 mDeleteFolderOnDropCompleted = true;
865 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700866 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700867 mSuppressFolderDeletion = false;
Adam Cohenafb01ee2011-06-23 15:38:03 -0700868 }
869
870 private void replaceFolderWithFinalItem() {
871 ItemInfo finalItem = null;
872
873 if (getItemCount() == 1) {
874 finalItem = mInfo.contents.get(0);
875 }
876
877 // Remove the folder completely
Winson Chung3d503fb2011-07-13 17:25:49 -0700878 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screen);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700879 cellLayout.removeView(mFolderIcon);
880 if (mFolderIcon instanceof DropTarget) {
881 mDragController.removeDropTarget((DropTarget) mFolderIcon);
882 }
883 mLauncher.removeFolder(mInfo);
884
885 if (finalItem != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700886 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
887 mInfo.screen, mInfo.cellX, mInfo.cellY);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700888 }
Adam Cohen716b51e2011-06-30 12:09:54 -0700889 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700890
891 // Add the last remaining child to the workspace in place of the folder
892 if (finalItem != null) {
893 View child = mLauncher.createShortcut(R.layout.application, cellLayout,
894 (ShortcutInfo) finalItem);
895
Winson Chung3d503fb2011-07-13 17:25:49 -0700896 mLauncher.getWorkspace().addInScreen(child, mInfo.container, mInfo.screen, mInfo.cellX,
897 mInfo.cellY, mInfo.spanX, mInfo.spanY);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700898 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700899 }
900
Adam Cohenbfbfd262011-06-13 16:55:12 -0700901 public void onDrop(DragObject d) {
902 ShortcutInfo item;
903 if (d.dragInfo instanceof ApplicationInfo) {
904 // Came from all apps -- make a copy
905 item = ((ApplicationInfo) d.dragInfo).makeShortcut();
906 item.spanX = 1;
907 item.spanY = 1;
908 } else {
909 item = (ShortcutInfo) d.dragInfo;
910 }
911 // Dragged from self onto self
912 if (item == mCurrentDragInfo) {
913 ShortcutInfo si = (ShortcutInfo) mCurrentDragView.getTag();
914 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) mCurrentDragView.getLayoutParams();
915 si.cellX = lp.cellX = mEmptyCell[0];
916 si.cellX = lp.cellY = mEmptyCell[1];
917 mContent.addViewToCellLayout(mCurrentDragView, -1, (int)item.id, lp, true);
Adam Cohenfc53cd22011-07-20 15:45:11 -0700918 if (d.dragView.hasDrawn()) {
919 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, mCurrentDragView);
920 } else {
921 mCurrentDragView.setVisibility(VISIBLE);
922 }
Adam Cohene9166b22011-07-08 17:11:11 -0700923 mItemsInvalidated = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700924 setupContentDimension(getItemCount());
Adam Cohen716b51e2011-06-30 12:09:54 -0700925 mSuppressOnAdd = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700926 }
927 mInfo.add(item);
928 }
929
930 public void onAdd(ShortcutInfo item) {
931 mItemsInvalidated = true;
932 if (mSuppressOnAdd) return;
933 if (!findAndSetEmptyCells(item)) {
934 // The current layout is full, can we expand it?
935 setupContentForNumItems(getItemCount() + 1);
936 findAndSetEmptyCells(item);
937 }
938 createAndAddShortcut(item);
939 LauncherModel.addOrMoveItemInDatabase(
940 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
941 }
942
Adam Cohena9cf38f2011-05-02 15:36:58 -0700943 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -0700944 mItemsInvalidated = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700945 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -0700946 View v = getViewForInfo(item);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700947 mContent.removeView(v);
Adam Cohen2801caf2011-05-13 20:57:39 -0700948 if (mState == STATE_ANIMATING) {
949 mRearrangeOnClose = true;
950 } else {
951 setupContentForNumItems(getItemCount());
952 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700953 if (getItemCount() <= 1) {
954 replaceFolderWithFinalItem();
955 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700956 }
Adam Cohen7c693212011-05-18 15:26:57 -0700957
Adam Cohendf1e4e82011-06-24 15:57:39 -0700958 private View getViewForInfo(ShortcutInfo item) {
959 for (int j = 0; j < mContent.getCountY(); j++) {
960 for (int i = 0; i < mContent.getCountX(); i++) {
961 View v = mContent.getChildAt(i, j);
962 if (v.getTag() == item) {
963 return v;
964 }
965 }
966 }
967 return null;
968 }
969
Adam Cohen76078c42011-06-09 15:06:52 -0700970 public void onItemsChanged() {
971 }
Adam Cohen76fc0852011-06-17 13:26:23 -0700972 public void onTitleChanged(CharSequence title) {
973 }
Adam Cohen76078c42011-06-09 15:06:52 -0700974
Adam Cohen7c693212011-05-18 15:26:57 -0700975 public ArrayList<View> getItemsInReadingOrder() {
Adam Cohen76078c42011-06-09 15:06:52 -0700976 return getItemsInReadingOrder(true);
977 }
978
979 public ArrayList<View> getItemsInReadingOrder(boolean includeCurrentDragItem) {
Adam Cohen7c693212011-05-18 15:26:57 -0700980 if (mItemsInvalidated) {
981 mItemsInReadingOrder.clear();
982 for (int j = 0; j < mContent.getCountY(); j++) {
983 for (int i = 0; i < mContent.getCountX(); i++) {
984 View v = mContent.getChildAt(i, j);
985 if (v != null) {
Adam Cohen76078c42011-06-09 15:06:52 -0700986 ShortcutInfo info = (ShortcutInfo) v.getTag();
987 if (info != mCurrentDragInfo || includeCurrentDragItem) {
988 mItemsInReadingOrder.add(v);
989 }
Adam Cohen7c693212011-05-18 15:26:57 -0700990 }
991 }
992 }
993 mItemsInvalidated = false;
994 }
995 return mItemsInReadingOrder;
996 }
Adam Cohen8dfcba42011-07-07 16:38:18 -0700997
998 public void getLocationInDragLayer(int[] loc) {
999 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
1000 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001001}