blob: 1fcfebc5ada5fdbf6accfbc7ecd3f9198e96193f [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;
Adam Cohen05e0f402011-08-01 12:12:49 -0700104 private boolean mItemAddedBackToSelfViaIcon = 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;
Adam Cohen05e0f402011-08-01 12:12:49 -0700224 mItemAddedBackToSelfViaIcon = 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 Cohen1df26a32011-08-12 16:15:23 -0700246 // Convert to a string here to ensure that no other state associated with the text field
247 // gets saved.
248 mInfo.setTitle(mFolderName.getText().toString());
Adam Cohen76fc0852011-06-17 13:26:23 -0700249 LauncherModel.updateItemInDatabase(mLauncher, mInfo);
250 mFolderName.setCursorVisible(false);
251 mFolderName.clearFocus();
Adam Cohene601a432011-07-26 21:51:30 -0700252 Selection.setSelection((Spannable) mFolderName.getText(), 0, 0);
Adam Cohen76fc0852011-06-17 13:26:23 -0700253 mIsEditingName = false;
254 }
255
256 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
257 if (actionId == EditorInfo.IME_ACTION_DONE) {
258 dismissEditingName();
259 return true;
260 }
261 return false;
262 }
263
264 public View getEditTextRegion() {
265 return mFolderName;
266 }
267
Adam Cohenbadf71e2011-05-26 19:08:29 -0700268 public Drawable getDragDrawable() {
269 return mIconDrawable;
270 }
271
Adam Cohen0c872ba2011-05-05 10:34:16 -0700272 /**
273 * We need to handle touch events to prevent them from falling through to the workspace below.
274 */
275 @Override
276 public boolean onTouchEvent(MotionEvent ev) {
277 return true;
278 }
279
Joe Onorato00acb122009-08-04 16:04:30 -0400280 public void setDragController(DragController dragController) {
281 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800282 }
283
Adam Cohen2801caf2011-05-13 20:57:39 -0700284 void setFolderIcon(FolderIcon icon) {
285 mFolderIcon = icon;
286 }
287
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800288 /**
289 * @return the FolderInfo object associated with this folder
290 */
291 FolderInfo getInfo() {
292 return mInfo;
293 }
294
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800295 void bind(FolderInfo info) {
296 mInfo = info;
Adam Cohendf2cc412011-04-27 16:56:57 -0700297 ArrayList<ShortcutInfo> children = info.contents;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700298 ArrayList<ShortcutInfo> overflow = new ArrayList<ShortcutInfo>();
Adam Cohen7c693212011-05-18 15:26:57 -0700299 setupContentForNumItems(children.size());
Adam Cohen0057bbc2011-08-12 18:30:51 -0700300 int count = 0;
Adam Cohendf2cc412011-04-27 16:56:57 -0700301 for (int i = 0; i < children.size(); i++) {
302 ShortcutInfo child = (ShortcutInfo) children.get(i);
Adam Cohenc508b2d2011-06-28 14:41:44 -0700303 if (!createAndAddShortcut(child)) {
304 overflow.add(child);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700305 } else {
306 count++;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700307 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700308 }
Adam Cohenc508b2d2011-06-28 14:41:44 -0700309
Adam Cohen0057bbc2011-08-12 18:30:51 -0700310 // We rearrange the items in case there are any empty gaps
311 setupContentForNumItems(count);
312
Adam Cohenc508b2d2011-06-28 14:41:44 -0700313 // If our folder has too many items we prune them from the list. This is an issue
314 // when upgrading from the old Folders implementation which could contain an unlimited
315 // number of items.
316 for (ShortcutInfo item: overflow) {
317 mInfo.remove(item);
318 LauncherModel.deleteItemFromDatabase(mLauncher, item);
319 }
320
Adam Cohen4dbe6d92011-05-18 17:14:15 -0700321 mItemsInvalidated = true;
Adam Cohena9cf38f2011-05-02 15:36:58 -0700322 mInfo.addListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700323
Adam Cohenafb01ee2011-06-23 15:38:03 -0700324 if (!sDefaultFolderName.contentEquals(mInfo.title)) {
Adam Cohen4ef610f2011-06-21 22:37:36 -0700325 mFolderName.setText(mInfo.title);
326 } else {
327 mFolderName.setText("");
328 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700329 }
330
331 /**
332 * Creates a new UserFolder, inflated from R.layout.user_folder.
333 *
334 * @param context The application's context.
335 *
336 * @return A new UserFolder.
337 */
338 static Folder fromXml(Context context) {
339 return (Folder) LayoutInflater.from(context).inflate(R.layout.user_folder, null);
340 }
341
342 /**
343 * This method is intended to make the UserFolder to be visually identical in size and position
344 * to its associated FolderIcon. This allows for a seamless transition into the expanded state.
345 */
346 private void positionAndSizeAsIcon() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700347 if (!(getParent() instanceof DragLayer)) return;
Adam Cohendf2cc412011-04-27 16:56:57 -0700348
Adam Cohen8e776a62011-06-28 18:10:06 -0700349 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohendf2cc412011-04-27 16:56:57 -0700350
Adam Cohen2801caf2011-05-13 20:57:39 -0700351 if (mMode == PARTIAL_GROW) {
352 setScaleX(0.8f);
353 setScaleY(0.8f);
354 setAlpha(0f);
355 } else {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700356 mLauncher.getDragLayer().getDescendantRectRelativeToSelf(mFolderIcon, mIconRect);
357 lp.width = mIconRect.width();
358 lp.height = mIconRect.height();
359 lp.x = mIconRect.left;
360 lp.y = mIconRect.top;
Adam Cohen2801caf2011-05-13 20:57:39 -0700361 mContent.setAlpha(0);
362 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700363 mState = STATE_SMALL;
364 }
365
366 public void animateOpen() {
Adam Cohen8dfcba42011-07-07 16:38:18 -0700367 if (mFirstOpen) {
368 setLayerType(LAYER_TYPE_HARDWARE, null);
369 buildLayer();
370 mFirstOpen = false;
371 }
372
Adam Cohen3e8f8112011-07-02 18:03:00 -0700373 positionAndSizeAsIcon();
374
Adam Cohen8e776a62011-06-28 18:10:06 -0700375 if (!(getParent() instanceof DragLayer)) return;
Adam Cohendf2cc412011-04-27 16:56:57 -0700376
Adam Cohen2801caf2011-05-13 20:57:39 -0700377 ObjectAnimator oa;
Adam Cohen8e776a62011-06-28 18:10:06 -0700378 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohendf2cc412011-04-27 16:56:57 -0700379
Adam Cohen2801caf2011-05-13 20:57:39 -0700380 centerAboutIcon();
381 if (mMode == PARTIAL_GROW) {
382 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1);
383 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
384 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
385 oa = ObjectAnimator.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
386 } else {
387 PropertyValuesHolder width = PropertyValuesHolder.ofInt("width", mNewSize.width());
388 PropertyValuesHolder height = PropertyValuesHolder.ofInt("height", mNewSize.height());
389 PropertyValuesHolder x = PropertyValuesHolder.ofInt("x", mNewSize.left);
390 PropertyValuesHolder y = PropertyValuesHolder.ofInt("y", mNewSize.top);
391 oa = ObjectAnimator.ofPropertyValuesHolder(lp, width, height, x, y);
392 oa.addUpdateListener(new AnimatorUpdateListener() {
393 public void onAnimationUpdate(ValueAnimator animation) {
394 requestLayout();
395 }
396 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700397
Adam Cohen2801caf2011-05-13 20:57:39 -0700398 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
399 ObjectAnimator alphaOa = ObjectAnimator.ofPropertyValuesHolder(mContent, alpha);
400 alphaOa.setDuration(mExpandDuration);
401 alphaOa.setInterpolator(new AccelerateInterpolator(2.0f));
402 alphaOa.start();
403 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700404
Adam Cohen2801caf2011-05-13 20:57:39 -0700405 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700406 @Override
407 public void onAnimationStart(Animator animation) {
408 mState = STATE_ANIMATING;
409 }
410 @Override
411 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700412 mState = STATE_OPEN;
Adam Cohen8dfcba42011-07-07 16:38:18 -0700413 setLayerType(LAYER_TYPE_NONE, null);
414 enableHardwareLayersForChildren();
Adam Cohendf2cc412011-04-27 16:56:57 -0700415 }
416 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700417 oa.setDuration(mExpandDuration);
418 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700419 }
420
Adam Cohen8dfcba42011-07-07 16:38:18 -0700421 void enableHardwareLayersForChildren() {
422 ArrayList<View> children = getItemsInReadingOrder();
423 for (View child: children) {
424 child.setLayerType(LAYER_TYPE_HARDWARE, null);
425 }
426 }
427
Adam Cohendf2cc412011-04-27 16:56:57 -0700428 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700429 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen8dfcba42011-07-07 16:38:18 -0700430 setLayerType(LAYER_TYPE_HARDWARE, null);
431 buildLayer();
Adam Cohendf2cc412011-04-27 16:56:57 -0700432
Adam Cohen2801caf2011-05-13 20:57:39 -0700433 ObjectAnimator oa;
Adam Cohen2801caf2011-05-13 20:57:39 -0700434 if (mMode == PARTIAL_GROW) {
435 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
436 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
437 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
438 oa = ObjectAnimator.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
439 } else {
Adam Cohen8e776a62011-06-28 18:10:06 -0700440 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohendf2cc412011-04-27 16:56:57 -0700441
Adam Cohen3e8f8112011-07-02 18:03:00 -0700442 PropertyValuesHolder width = PropertyValuesHolder.ofInt("width", mIconRect.width());
443 PropertyValuesHolder height = PropertyValuesHolder.ofInt("height", mIconRect.height());
444 PropertyValuesHolder x = PropertyValuesHolder.ofInt("x", mIconRect.left);
445 PropertyValuesHolder y = PropertyValuesHolder.ofInt("y", mIconRect.top);
Adam Cohen2801caf2011-05-13 20:57:39 -0700446 oa = ObjectAnimator.ofPropertyValuesHolder(lp, width, height, x, y);
447 oa.addUpdateListener(new AnimatorUpdateListener() {
448 public void onAnimationUpdate(ValueAnimator animation) {
449 requestLayout();
450 }
451 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700452
Adam Cohen2801caf2011-05-13 20:57:39 -0700453 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0f);
454 ObjectAnimator alphaOa = ObjectAnimator.ofPropertyValuesHolder(mContent, alpha);
455 alphaOa.setDuration(mExpandDuration);
456 alphaOa.setInterpolator(new DecelerateInterpolator(2.0f));
457 alphaOa.start();
458 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700459
Adam Cohen2801caf2011-05-13 20:57:39 -0700460 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700461 @Override
462 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700463 onCloseComplete();
Adam Cohen2801caf2011-05-13 20:57:39 -0700464 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700465 }
466 @Override
467 public void onAnimationStart(Animator animation) {
468 mState = STATE_ANIMATING;
469 }
470 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700471 oa.setDuration(mExpandDuration);
472 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700473 }
474
475 void notifyDataSetChanged() {
476 // recreate all the children if the data set changes under us. We may want to do this more
477 // intelligently (ie just removing the views that should no longer exist)
478 mContent.removeAllViewsInLayout();
479 bind(mInfo);
480 }
481
Adam Cohencb3382b2011-05-24 14:07:08 -0700482 public boolean acceptDrop(DragObject d) {
483 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700484 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700485 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
486 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
487 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700488 }
489
Adam Cohendf2cc412011-04-27 16:56:57 -0700490 protected boolean findAndSetEmptyCells(ShortcutInfo item) {
491 int[] emptyCell = new int[2];
492 if (mContent.findCellForSpan(emptyCell, item.spanX, item.spanY)) {
493 item.cellX = emptyCell[0];
494 item.cellY = emptyCell[1];
Adam Cohendf2cc412011-04-27 16:56:57 -0700495 return true;
496 } else {
497 return false;
498 }
499 }
500
Adam Cohenc508b2d2011-06-28 14:41:44 -0700501 protected boolean createAndAddShortcut(ShortcutInfo item) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700502 final TextView textView =
Adam Cohene87b9242011-06-29 14:01:26 -0700503 (TextView) mInflater.inflate(R.layout.application, this, false);
Adam Cohendf2cc412011-04-27 16:56:57 -0700504 textView.setCompoundDrawablesWithIntrinsicBounds(null,
505 new FastBitmapDrawable(item.getIcon(mIconCache)), null, null);
506 textView.setText(item.title);
507 textView.setTag(item);
508
509 textView.setOnClickListener(this);
510 textView.setOnLongClickListener(this);
511
Adam Cohenc508b2d2011-06-28 14:41:44 -0700512 // We need to check here to verify that the given item's location isn't already occupied
513 // by another item. If it is, we need to find the next available slot and assign
514 // it that position. This is an issue when upgrading from the old Folders implementation
515 // which could contain an unlimited number of items.
Adam Cohen0057bbc2011-08-12 18:30:51 -0700516 if (mContent.getChildAt(item.cellX, item.cellY) != null || item.cellX < 0 || item.cellY < 0
517 || item.cellX >= mContent.getCountX() || item.cellY >= mContent.getCountY()) {
Adam Cohenc508b2d2011-06-28 14:41:44 -0700518 if (!findAndSetEmptyCells(item)) {
519 return false;
520 }
521 }
522
Adam Cohendf2cc412011-04-27 16:56:57 -0700523 CellLayout.LayoutParams lp =
524 new CellLayout.LayoutParams(item.cellX, item.cellY, item.spanX, item.spanY);
525 boolean insert = false;
526 mContent.addViewToCellLayout(textView, insert ? 0 : -1, (int)item.id, lp, true);
Adam Cohenc508b2d2011-06-28 14:41:44 -0700527 return true;
Adam Cohendf2cc412011-04-27 16:56:57 -0700528 }
529
Adam Cohencb3382b2011-05-24 14:07:08 -0700530 public void onDragEnter(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700531 mPreviousTargetCell[0] = -1;
532 mPreviousTargetCell[1] = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700533 mOnExitAlarm.cancelAlarm();
534 }
535
536 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
537 public void onAlarm(Alarm alarm) {
538 realTimeReorder(mEmptyCell, mTargetCell);
539 }
540 };
541
542 boolean readingOrderGreaterThan(int[] v1, int[] v2) {
543 if (v1[1] > v2[1] || (v1[1] == v2[1] && v1[0] > v2[0])) {
544 return true;
545 } else {
546 return false;
547 }
548 }
549
550 private void realTimeReorder(int[] empty, int[] target) {
551 boolean wrap;
552 int startX;
553 int endX;
554 int startY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700555 int delay = 0;
556 float delayAmount = 30;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700557 if (readingOrderGreaterThan(target, empty)) {
558 wrap = empty[0] >= mContent.getCountX() - 1;
559 startY = wrap ? empty[1] + 1 : empty[1];
560 for (int y = startY; y <= target[1]; y++) {
561 startX = y == empty[1] ? empty[0] + 1 : 0;
562 endX = y < target[1] ? mContent.getCountX() - 1 : target[0];
563 for (int x = startX; x <= endX; x++) {
564 View v = mContent.getChildAt(x,y);
565 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen76fc0852011-06-17 13:26:23 -0700566 REORDER_ANIMATION_DURATION, delay)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700567 empty[0] = x;
568 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700569 delay += delayAmount;
570 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700571 }
572 }
573 }
574 } else {
575 wrap = empty[0] == 0;
576 startY = wrap ? empty[1] - 1 : empty[1];
577 for (int y = startY; y >= target[1]; y--) {
578 startX = y == empty[1] ? empty[0] - 1 : mContent.getCountX() - 1;
579 endX = y > target[1] ? 0 : target[0];
580 for (int x = startX; x >= endX; x--) {
581 View v = mContent.getChildAt(x,y);
582 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen76fc0852011-06-17 13:26:23 -0700583 REORDER_ANIMATION_DURATION, delay)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700584 empty[0] = x;
585 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700586 delay += delayAmount;
587 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700588 }
589 }
590 }
591 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700592 }
593
Adam Cohencb3382b2011-05-24 14:07:08 -0700594 public void onDragOver(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700595 float[] r = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, d.dragView, null);
596 mTargetCell = mContent.findNearestArea((int) r[0], (int) r[1], 1, 1, mTargetCell);
597
598 if (mTargetCell[0] != mPreviousTargetCell[0] || mTargetCell[1] != mPreviousTargetCell[1]) {
599 mReorderAlarm.cancelAlarm();
600 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
601 mReorderAlarm.setAlarm(150);
602 mPreviousTargetCell[0] = mTargetCell[0];
603 mPreviousTargetCell[1] = mTargetCell[1];
604 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700605 }
606
Adam Cohenbfbfd262011-06-13 16:55:12 -0700607 // This is used to compute the visual center of the dragView. The idea is that
608 // the visual center represents the user's interpretation of where the item is, and hence
609 // is the appropriate point to use when determining drop location.
610 private float[] getDragViewVisualCenter(int x, int y, int xOffset, int yOffset,
611 DragView dragView, float[] recycle) {
612 float res[];
613 if (recycle == null) {
614 res = new float[2];
615 } else {
616 res = recycle;
617 }
618
619 // These represent the visual top and left of drag view if a dragRect was provided.
620 // If a dragRect was not provided, then they correspond to the actual view left and
621 // top, as the dragRect is in that case taken to be the entire dragView.
622 // R.dimen.dragViewOffsetY.
623 int left = x - xOffset;
624 int top = y - yOffset;
625
626 // In order to find the visual center, we shift by half the dragRect
627 res[0] = left + dragView.getDragRegion().width() / 2;
628 res[1] = top + dragView.getDragRegion().height() / 2;
629
630 return res;
631 }
632
633 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
634 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700635 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700636 }
637 };
638
Adam Cohen95bb8002011-07-03 23:40:28 -0700639 public void completeDragExit() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700640 mLauncher.closeFolder();
641 mCurrentDragInfo = null;
642 mCurrentDragView = null;
643 mSuppressOnAdd = false;
644 mRearrangeOnClose = true;
645 }
646
Adam Cohencb3382b2011-05-24 14:07:08 -0700647 public void onDragExit(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700648 // We only close the folder if this is a true drag exit, ie. not because a drop
649 // has occurred above the folder.
650 if (!d.dragComplete) {
651 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
652 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
653 }
654 mReorderAlarm.cancelAlarm();
Adam Cohen2801caf2011-05-13 20:57:39 -0700655 }
656
Adam Cohenc0dcf592011-06-01 15:30:43 -0700657 public void onDropCompleted(View target, DragObject d, boolean success) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700658 if (success) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700659 if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700660 replaceFolderWithFinalItem();
661 }
662 } else {
663 // The drag failed, we need to return the item to the folder
664 mFolderIcon.onDrop(d);
665
666 // We're going to trigger a "closeFolder" which may occur before this item has
667 // been added back to the folder -- this could cause the folder to be deleted
668 if (mOnExitAlarm.alarmPending()) {
669 mSuppressFolderDeletion = true;
670 }
671 }
672
673 if (target != this) {
674 if (mOnExitAlarm.alarmPending()) {
675 mOnExitAlarm.cancelAlarm();
676 completeDragExit();
677 }
678 }
679 mDeleteFolderOnDropCompleted = false;
680 mDragInProgress = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700681 mItemAddedBackToSelfViaIcon = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700682 mCurrentDragInfo = null;
683 mCurrentDragView = null;
684 mSuppressOnAdd = false;
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700685 }
Adam Cohen228da5a2011-07-27 22:23:47 -0700686
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700687 public void notifyDrop() {
688 if (mDragInProgress) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700689 mItemAddedBackToSelfViaIcon = true;
Adam Cohen76078c42011-06-09 15:06:52 -0700690 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700691 }
692
693 public boolean isDropEnabled() {
694 return true;
695 }
696
Adam Cohencb3382b2011-05-24 14:07:08 -0700697 public DropTarget getDropTargetDelegate(DragObject d) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700698 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800699 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700700
Adam Cohen2801caf2011-05-13 20:57:39 -0700701 private void setupContentDimension(int count) {
702 ArrayList<View> list = getItemsInReadingOrder();
703
704 int countX = mContent.getCountX();
705 int countY = mContent.getCountY();
Adam Cohen7c693212011-05-18 15:26:57 -0700706 boolean done = false;
Adam Cohen2801caf2011-05-13 20:57:39 -0700707
Adam Cohen7c693212011-05-18 15:26:57 -0700708 while (!done) {
709 int oldCountX = countX;
710 int oldCountY = countY;
711 if (countX * countY < count) {
712 // Current grid is too small, expand it
713 if (countX <= countY && countX < mMaxCountX) {
714 countX++;
715 } else if (countY < mMaxCountY) {
716 countY++;
717 }
718 if (countY == 0) countY++;
719 } else if ((countY - 1) * countX >= count && countY >= countX) {
720 countY = Math.max(0, countY - 1);
721 } else if ((countX - 1) * countY >= count) {
722 countX = Math.max(0, countX - 1);
Adam Cohen2801caf2011-05-13 20:57:39 -0700723 }
Adam Cohen7c693212011-05-18 15:26:57 -0700724 done = countX == oldCountX && countY == oldCountY;
Adam Cohen2801caf2011-05-13 20:57:39 -0700725 }
Adam Cohen7c693212011-05-18 15:26:57 -0700726 mContent.setGridSize(countX, countY);
Adam Cohen2801caf2011-05-13 20:57:39 -0700727 arrangeChildren(list);
728 }
729
730 public boolean isFull() {
731 return getItemCount() >= mMaxCountX * mMaxCountY;
732 }
733
734 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700735 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700736
737 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf4bb1cd2011-07-22 14:36:03 -0700738 int height = getPaddingTop() + getPaddingBottom() + mContent.getDesiredHeight()
739 + mFolderNameHeight;
Adam Cohen8e776a62011-06-28 18:10:06 -0700740 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700741
Adam Cohen8e776a62011-06-28 18:10:06 -0700742 parent.getDescendantRectRelativeToSelf(mFolderIcon, mTempRect);
743
744 int centerX = mTempRect.centerX();
745 int centerY = mTempRect.centerY();
Adam Cohen2801caf2011-05-13 20:57:39 -0700746 int centeredLeft = centerX - width / 2;
747 int centeredTop = centerY - height / 2;
748
Adam Cohen35e7e642011-07-17 14:47:18 -0700749 // We first fetch the currently visible CellLayoutChildren
Winson Chung3d503fb2011-07-13 17:25:49 -0700750 CellLayout currentPage = mLauncher.getWorkspace().getCurrentDropLayout();
Adam Cohen35e7e642011-07-17 14:47:18 -0700751 CellLayoutChildren boundingLayout = currentPage.getChildrenLayout();
752 Rect bounds = new Rect();
753 parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
Adam Cohen2801caf2011-05-13 20:57:39 -0700754
Adam Cohen35e7e642011-07-17 14:47:18 -0700755 // We need to bound the folder to the currently visible CellLayoutChildren
756 int left = Math.min(Math.max(bounds.left, centeredLeft),
757 bounds.left + bounds.width() - width);
758 int top = Math.min(Math.max(bounds.top, centeredTop),
759 bounds.top + bounds.height() - height);
760 // If the folder doesn't fit within the bounds, center it about the desired bounds
761 if (width >= bounds.width()) {
762 left = bounds.left + (bounds.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700763 }
Adam Cohen35e7e642011-07-17 14:47:18 -0700764 if (height >= bounds.height()) {
765 top = bounds.top + (bounds.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700766 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700767
768 int folderPivotX = width / 2 + (centeredLeft - left);
769 int folderPivotY = height / 2 + (centeredTop - top);
770 setPivotX(folderPivotX);
771 setPivotY(folderPivotY);
772 int folderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
773 (1.0f * folderPivotX / width));
774 int folderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
775 (1.0f * folderPivotY / height));
776 mFolderIcon.setPivotX(folderIconPivotX);
777 mFolderIcon.setPivotY(folderIconPivotY);
778
779 if (mMode == PARTIAL_GROW) {
780 lp.width = width;
781 lp.height = height;
782 lp.x = left;
783 lp.y = top;
784 } else {
785 mNewSize.set(left, top, left + width, top + height);
786 }
787 }
788
789 private void setupContentForNumItems(int count) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700790 setupContentDimension(count);
791
Adam Cohen8e776a62011-06-28 18:10:06 -0700792 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700793 if (lp == null) {
Adam Cohen8e776a62011-06-28 18:10:06 -0700794 lp = new DragLayer.LayoutParams(0, 0);
795 lp.customPosition = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700796 setLayoutParams(lp);
797 }
798 centerAboutIcon();
799 }
800
Adam Cohen234c4cd2011-07-17 21:03:04 -0700801 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
802 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf4bb1cd2011-07-22 14:36:03 -0700803 int height = getPaddingTop() + getPaddingBottom() + mContent.getDesiredHeight()
804 + mFolderNameHeight;
Adam Cohen234c4cd2011-07-17 21:03:04 -0700805
806 int contentWidthSpec = MeasureSpec.makeMeasureSpec(mContent.getDesiredWidth(),
807 MeasureSpec.EXACTLY);
808 int contentHeightSpec = MeasureSpec.makeMeasureSpec(mContent.getDesiredHeight(),
809 MeasureSpec.EXACTLY);
810 mContent.measure(contentWidthSpec, contentHeightSpec);
811
812 mFolderName.measure(contentWidthSpec,
813 MeasureSpec.makeMeasureSpec(mFolderNameHeight, MeasureSpec.EXACTLY));
814 setMeasuredDimension(width, height);
815 }
816
Adam Cohen2801caf2011-05-13 20:57:39 -0700817 private void arrangeChildren(ArrayList<View> list) {
818 int[] vacant = new int[2];
819 if (list == null) {
820 list = getItemsInReadingOrder();
821 }
822 mContent.removeAllViews();
823
824 for (int i = 0; i < list.size(); i++) {
825 View v = list.get(i);
826 mContent.getVacantCell(vacant, 1, 1);
827 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) v.getLayoutParams();
828 lp.cellX = vacant[0];
829 lp.cellY = vacant[1];
830 ItemInfo info = (ItemInfo) v.getTag();
831 info.cellX = vacant[0];
832 info.cellY = vacant[1];
833 boolean insert = false;
834 mContent.addViewToCellLayout(v, insert ? 0 : -1, (int)info.id, lp, true);
Adam Cohen7c693212011-05-18 15:26:57 -0700835 LauncherModel.addOrMoveItemInDatabase(mLauncher, info, mInfo.id, 0,
836 info.cellX, info.cellY);
Adam Cohen2801caf2011-05-13 20:57:39 -0700837 }
Adam Cohen7c693212011-05-18 15:26:57 -0700838 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700839 }
840
Adam Cohena9cf38f2011-05-02 15:36:58 -0700841 public int getItemCount() {
842 return mContent.getChildrenLayout().getChildCount();
843 }
844
845 public View getItemAt(int index) {
846 return mContent.getChildrenLayout().getChildAt(index);
847 }
848
Adam Cohen2801caf2011-05-13 20:57:39 -0700849 private void onCloseComplete() {
Adam Cohen05e0f402011-08-01 12:12:49 -0700850 DragLayer parent = (DragLayer) getParent();
Adam Cohen4554ee12011-08-03 16:13:21 -0700851 parent.removeView(this);
852 mDragController.removeDropTarget((DropTarget) this);
Adam Cohen05e0f402011-08-01 12:12:49 -0700853 clearFocus();
854
Adam Cohen2801caf2011-05-13 20:57:39 -0700855 if (mRearrangeOnClose) {
856 setupContentForNumItems(getItemCount());
857 mRearrangeOnClose = false;
858 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700859 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700860 if (!mDragInProgress && !mSuppressFolderDeletion) {
861 replaceFolderWithFinalItem();
862 } else if (mDragInProgress) {
863 mDeleteFolderOnDropCompleted = true;
864 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700865 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700866 mSuppressFolderDeletion = false;
Adam Cohenafb01ee2011-06-23 15:38:03 -0700867 }
868
869 private void replaceFolderWithFinalItem() {
870 ItemInfo finalItem = null;
871
872 if (getItemCount() == 1) {
873 finalItem = mInfo.contents.get(0);
874 }
875
876 // Remove the folder completely
Winson Chung3d503fb2011-07-13 17:25:49 -0700877 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screen);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700878 cellLayout.removeView(mFolderIcon);
879 if (mFolderIcon instanceof DropTarget) {
880 mDragController.removeDropTarget((DropTarget) mFolderIcon);
881 }
882 mLauncher.removeFolder(mInfo);
883
884 if (finalItem != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700885 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
886 mInfo.screen, mInfo.cellX, mInfo.cellY);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700887 }
Adam Cohen716b51e2011-06-30 12:09:54 -0700888 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700889
890 // Add the last remaining child to the workspace in place of the folder
891 if (finalItem != null) {
892 View child = mLauncher.createShortcut(R.layout.application, cellLayout,
893 (ShortcutInfo) finalItem);
894
Winson Chung3d503fb2011-07-13 17:25:49 -0700895 mLauncher.getWorkspace().addInScreen(child, mInfo.container, mInfo.screen, mInfo.cellX,
896 mInfo.cellY, mInfo.spanX, mInfo.spanY);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700897 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700898 }
899
Adam Cohenbfbfd262011-06-13 16:55:12 -0700900 public void onDrop(DragObject d) {
901 ShortcutInfo item;
902 if (d.dragInfo instanceof ApplicationInfo) {
903 // Came from all apps -- make a copy
904 item = ((ApplicationInfo) d.dragInfo).makeShortcut();
905 item.spanX = 1;
906 item.spanY = 1;
907 } else {
908 item = (ShortcutInfo) d.dragInfo;
909 }
Adam Cohen05e0f402011-08-01 12:12:49 -0700910 // Dragged from self onto self, currently this is the only path possible, however
911 // we keep this as a distinct code path.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700912 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;
Adam Cohen05e0f402011-08-01 12:12:49 -0700932 // If the item was dropped onto this open folder, we have done the work associated
933 // with adding the item to the folder, as indicated by mSuppressOnAdd being set
Adam Cohenbfbfd262011-06-13 16:55:12 -0700934 if (mSuppressOnAdd) return;
935 if (!findAndSetEmptyCells(item)) {
936 // The current layout is full, can we expand it?
937 setupContentForNumItems(getItemCount() + 1);
938 findAndSetEmptyCells(item);
939 }
940 createAndAddShortcut(item);
941 LauncherModel.addOrMoveItemInDatabase(
942 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
943 }
944
Adam Cohena9cf38f2011-05-02 15:36:58 -0700945 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -0700946 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700947 // If this item is being dragged from this open folder, we have already handled
948 // the work associated with removing the item, so we don't have to do anything here.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700949 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -0700950 View v = getViewForInfo(item);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700951 mContent.removeView(v);
Adam Cohen2801caf2011-05-13 20:57:39 -0700952 if (mState == STATE_ANIMATING) {
953 mRearrangeOnClose = true;
954 } else {
955 setupContentForNumItems(getItemCount());
956 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700957 if (getItemCount() <= 1) {
958 replaceFolderWithFinalItem();
959 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700960 }
Adam Cohen7c693212011-05-18 15:26:57 -0700961
Adam Cohendf1e4e82011-06-24 15:57:39 -0700962 private View getViewForInfo(ShortcutInfo item) {
963 for (int j = 0; j < mContent.getCountY(); j++) {
964 for (int i = 0; i < mContent.getCountX(); i++) {
965 View v = mContent.getChildAt(i, j);
966 if (v.getTag() == item) {
967 return v;
968 }
969 }
970 }
971 return null;
972 }
973
Adam Cohen76078c42011-06-09 15:06:52 -0700974 public void onItemsChanged() {
975 }
Adam Cohen76fc0852011-06-17 13:26:23 -0700976 public void onTitleChanged(CharSequence title) {
977 }
Adam Cohen76078c42011-06-09 15:06:52 -0700978
Adam Cohen7c693212011-05-18 15:26:57 -0700979 public ArrayList<View> getItemsInReadingOrder() {
Adam Cohen76078c42011-06-09 15:06:52 -0700980 return getItemsInReadingOrder(true);
981 }
982
983 public ArrayList<View> getItemsInReadingOrder(boolean includeCurrentDragItem) {
Adam Cohen7c693212011-05-18 15:26:57 -0700984 if (mItemsInvalidated) {
985 mItemsInReadingOrder.clear();
986 for (int j = 0; j < mContent.getCountY(); j++) {
987 for (int i = 0; i < mContent.getCountX(); i++) {
988 View v = mContent.getChildAt(i, j);
989 if (v != null) {
Adam Cohen76078c42011-06-09 15:06:52 -0700990 ShortcutInfo info = (ShortcutInfo) v.getTag();
991 if (info != mCurrentDragInfo || includeCurrentDragItem) {
992 mItemsInReadingOrder.add(v);
993 }
Adam Cohen7c693212011-05-18 15:26:57 -0700994 }
995 }
996 }
997 mItemsInvalidated = false;
998 }
999 return mItemsInReadingOrder;
1000 }
Adam Cohen8dfcba42011-07-07 16:38:18 -07001001
1002 public void getLocationInDragLayer(int[] loc) {
1003 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
1004 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001005}