blob: 780d0ed260e4d1c7a6720ffe4068f49d93256c6e [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];
Adam Cohenac8c8762011-07-13 11:15:27 -070094 private int[] mTempXY = new int[2];
Adam Cohenbfbfd262011-06-13 16:55:12 -070095 private Alarm mReorderAlarm = new Alarm();
96 private Alarm mOnExitAlarm = new Alarm();
Adam Cohen76fc0852011-06-17 13:26:23 -070097 private TextView mFolderName;
98 private int mFolderNameHeight;
Adam Cohen4ef610f2011-06-21 22:37:36 -070099 private Rect mHitRect = new Rect();
Adam Cohen8e776a62011-06-28 18:10:06 -0700100 private Rect mTempRect = new Rect();
Adam Cohen8dfcba42011-07-07 16:38:18 -0700101 private boolean mFirstOpen = true;
Adam Cohen76fc0852011-06-17 13:26:23 -0700102
Adam Cohen228da5a2011-07-27 22:23:47 -0700103 // Internal variable to track whether the folder was destroyed due to only a single
104 // item remaining
105 private boolean mDestroyed = false;
106
Adam Cohen76fc0852011-06-17 13:26:23 -0700107 private boolean mIsEditingName = false;
108 private InputMethodManager mInputMethodManager;
Adam Cohendf2cc412011-04-27 16:56:57 -0700109
Adam Cohena65beee2011-06-27 21:32:23 -0700110 private static String sDefaultFolderName;
111 private static String sHintText;
112
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800113 /**
114 * Used to inflate the Workspace from XML.
115 *
116 * @param context The application's context.
117 * @param attrs The attribtues set containing the Workspace's customization values.
118 */
119 public Folder(Context context, AttributeSet attrs) {
120 super(context, attrs);
121 setAlwaysDrawnWithCacheEnabled(false);
Adam Cohendf2cc412011-04-27 16:56:57 -0700122 mInflater = LayoutInflater.from(context);
123 mIconCache = ((LauncherApplication)context.getApplicationContext()).getIconCache();
Adam Cohen8e776a62011-06-28 18:10:06 -0700124 mMaxCountX = LauncherModel.getCellCountX();
125 mMaxCountY = LauncherModel.getCellCountY();
Adam Cohen76fc0852011-06-17 13:26:23 -0700126
127 mInputMethodManager = (InputMethodManager)
128 mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
129
130 Resources res = getResources();
131 mExpandDuration = res.getInteger(R.integer.config_folderAnimDuration);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700132
133 if (sDefaultFolderName == null) {
134 sDefaultFolderName = res.getString(R.string.folder_name);
135 }
Adam Cohena65beee2011-06-27 21:32:23 -0700136 if (sHintText == null) {
137 sHintText = res.getString(R.string.folder_hint_text);
138 }
Adam Cohen4eac29a2011-07-11 17:53:37 -0700139
140 mLauncher = (Launcher) context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141 }
142
143 @Override
144 protected void onFinishInflate() {
145 super.onFinishInflate();
Adam Cohendf2cc412011-04-27 16:56:57 -0700146 mContent = (CellLayout) findViewById(R.id.folder_content);
Adam Cohen2801caf2011-05-13 20:57:39 -0700147 mContent.setGridSize(0, 0);
Adam Cohen76fc0852011-06-17 13:26:23 -0700148 mFolderName = (TextView) findViewById(R.id.folder_name);
149
150 // We find out how tall the text view wants to be (it is set to wrap_content), so that
151 // we can allocate the appropriate amount of space for it.
152 int measureSpec = MeasureSpec.UNSPECIFIED;
153 mFolderName.measure(measureSpec, measureSpec);
154 mFolderNameHeight = mFolderName.getMeasuredHeight();
155
156 // We disable action mode for now since it messes up the view on phones
157 mFolderName.setCustomSelectionActionModeCallback(mActionModeCallback);
158 mFolderName.setCursorVisible(false);
159 mFolderName.setOnEditorActionListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700160 mFolderName.setSelectAllOnFocus(true);
Adam Cohen7a14d0b2011-06-24 11:36:35 -0700161 mFolderName.setInputType(mFolderName.getInputType() |
162 InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700164
Adam Cohen76fc0852011-06-17 13:26:23 -0700165 private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
166 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
167 return false;
168 }
169
170 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
171 return false;
172 }
173
174 public void onDestroyActionMode(ActionMode mode) {
175 }
176
177 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
178 return false;
179 }
180 };
181
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182 public void onClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700183 Object tag = v.getTag();
184 if (tag instanceof ShortcutInfo) {
185 // refactor this code from Folder
186 ShortcutInfo item = (ShortcutInfo) tag;
187 int[] pos = new int[2];
188 v.getLocationOnScreen(pos);
189 item.intent.setSourceBounds(new Rect(pos[0], pos[1],
190 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
191 mLauncher.startActivitySafely(item.intent, item);
Adam Cohendf2cc412011-04-27 16:56:57 -0700192 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193 }
194
Adam Cohen76fc0852011-06-17 13:26:23 -0700195 public boolean onInterceptTouchEvent(MotionEvent ev) {
196 if (ev.getAction() == MotionEvent.ACTION_DOWN) {
197 mFolderName.getHitRect(mHitRect);
198 if (mHitRect.contains((int) ev.getX(), (int) ev.getY()) && !mIsEditingName) {
199 startEditingFolderName();
200 }
201 }
202 return false;
203 }
204
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800205 public boolean onLongClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700206 Object tag = v.getTag();
207 if (tag instanceof ShortcutInfo) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700208 ShortcutInfo item = (ShortcutInfo) tag;
209 if (!v.isInTouchMode()) {
210 return false;
211 }
212
213 mLauncher.getWorkspace().onDragStartedWithItem(v);
Adam Cohenac8c8762011-07-13 11:15:27 -0700214 mLauncher.getWorkspace().beginDragShared(v, this);
Adam Cohenbadf71e2011-05-26 19:08:29 -0700215 mIconDrawable = ((TextView) v).getCompoundDrawables()[1];
Adam Cohen76078c42011-06-09 15:06:52 -0700216
217 mCurrentDragInfo = item;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700218 mEmptyCell[0] = item.cellX;
219 mEmptyCell[1] = item.cellY;
220 mCurrentDragView = v;
Adam Cohenfc53cd22011-07-20 15:45:11 -0700221
222 mContent.removeView(mCurrentDragView);
223 mInfo.remove(mCurrentDragInfo);
Adam Cohendf2cc412011-04-27 16:56:57 -0700224 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800225 return true;
226 }
227
Adam Cohen76fc0852011-06-17 13:26:23 -0700228 public boolean isEditingName() {
229 return mIsEditingName;
230 }
231
232 public void startEditingFolderName() {
Adam Cohena65beee2011-06-27 21:32:23 -0700233 mFolderName.setHint("");
Adam Cohen76fc0852011-06-17 13:26:23 -0700234 mFolderName.setCursorVisible(true);
235 mIsEditingName = true;
236 }
237
238 public void dismissEditingName() {
239 mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
240 doneEditingFolderName(true);
241 }
242
243 public void doneEditingFolderName(boolean commit) {
Adam Cohena65beee2011-06-27 21:32:23 -0700244 mFolderName.setHint(sHintText);
Adam Cohen76fc0852011-06-17 13:26:23 -0700245 mInfo.setTitle(mFolderName.getText());
246 LauncherModel.updateItemInDatabase(mLauncher, mInfo);
247 mFolderName.setCursorVisible(false);
248 mFolderName.clearFocus();
Adam Cohene601a432011-07-26 21:51:30 -0700249 Selection.setSelection((Spannable) mFolderName.getText(), 0, 0);
Adam Cohen76fc0852011-06-17 13:26:23 -0700250 mIsEditingName = false;
251 }
252
253 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
254 if (actionId == EditorInfo.IME_ACTION_DONE) {
255 dismissEditingName();
256 return true;
257 }
258 return false;
259 }
260
261 public View getEditTextRegion() {
262 return mFolderName;
263 }
264
Adam Cohenbadf71e2011-05-26 19:08:29 -0700265 public Drawable getDragDrawable() {
266 return mIconDrawable;
267 }
268
Adam Cohen0c872ba2011-05-05 10:34:16 -0700269 /**
270 * We need to handle touch events to prevent them from falling through to the workspace below.
271 */
272 @Override
273 public boolean onTouchEvent(MotionEvent ev) {
274 return true;
275 }
276
Joe Onorato00acb122009-08-04 16:04:30 -0400277 public void setDragController(DragController dragController) {
278 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800279 }
280
Adam Cohen2801caf2011-05-13 20:57:39 -0700281 void setFolderIcon(FolderIcon icon) {
282 mFolderIcon = icon;
283 }
284
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800285 /**
286 * @return the FolderInfo object associated with this folder
287 */
288 FolderInfo getInfo() {
289 return mInfo;
290 }
291
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800292 void onOpen() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700293 // When the folder opens, we need to refresh the GridView's selection by
294 // forcing a layout
295 // TODO: find out if this is still necessary
296 mContent.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800297 }
298
299 void onClose() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700300 DragLayer parent = (DragLayer) getParent();
301 parent.removeView(Folder.this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700302 clearFocus();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800303 }
304
305 void bind(FolderInfo info) {
306 mInfo = info;
Adam Cohendf2cc412011-04-27 16:56:57 -0700307 ArrayList<ShortcutInfo> children = info.contents;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700308 ArrayList<ShortcutInfo> overflow = new ArrayList<ShortcutInfo>();
Adam Cohen7c693212011-05-18 15:26:57 -0700309 setupContentForNumItems(children.size());
Adam Cohendf2cc412011-04-27 16:56:57 -0700310 for (int i = 0; i < children.size(); i++) {
311 ShortcutInfo child = (ShortcutInfo) children.get(i);
Adam Cohenc508b2d2011-06-28 14:41:44 -0700312 if (!createAndAddShortcut(child)) {
313 overflow.add(child);
314 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700315 }
Adam Cohenc508b2d2011-06-28 14:41:44 -0700316
317 // If our folder has too many items we prune them from the list. This is an issue
318 // when upgrading from the old Folders implementation which could contain an unlimited
319 // number of items.
320 for (ShortcutInfo item: overflow) {
321 mInfo.remove(item);
322 LauncherModel.deleteItemFromDatabase(mLauncher, item);
323 }
324
Adam Cohen4dbe6d92011-05-18 17:14:15 -0700325 mItemsInvalidated = true;
Adam Cohena9cf38f2011-05-02 15:36:58 -0700326 mInfo.addListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700327
Adam Cohenafb01ee2011-06-23 15:38:03 -0700328 if (!sDefaultFolderName.contentEquals(mInfo.title)) {
Adam Cohen4ef610f2011-06-21 22:37:36 -0700329 mFolderName.setText(mInfo.title);
330 } else {
331 mFolderName.setText("");
332 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700333 }
334
335 /**
336 * Creates a new UserFolder, inflated from R.layout.user_folder.
337 *
338 * @param context The application's context.
339 *
340 * @return A new UserFolder.
341 */
342 static Folder fromXml(Context context) {
343 return (Folder) LayoutInflater.from(context).inflate(R.layout.user_folder, null);
344 }
345
346 /**
347 * This method is intended to make the UserFolder to be visually identical in size and position
348 * to its associated FolderIcon. This allows for a seamless transition into the expanded state.
349 */
350 private void positionAndSizeAsIcon() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700351 if (!(getParent() instanceof DragLayer)) return;
Adam Cohendf2cc412011-04-27 16:56:57 -0700352
Adam Cohen8e776a62011-06-28 18:10:06 -0700353 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohendf2cc412011-04-27 16:56:57 -0700354
Adam Cohen2801caf2011-05-13 20:57:39 -0700355 if (mMode == PARTIAL_GROW) {
356 setScaleX(0.8f);
357 setScaleY(0.8f);
358 setAlpha(0f);
359 } else {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700360 mLauncher.getDragLayer().getDescendantRectRelativeToSelf(mFolderIcon, mIconRect);
361 lp.width = mIconRect.width();
362 lp.height = mIconRect.height();
363 lp.x = mIconRect.left;
364 lp.y = mIconRect.top;
Adam Cohen2801caf2011-05-13 20:57:39 -0700365 mContent.setAlpha(0);
366 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700367 mState = STATE_SMALL;
368 }
369
370 public void animateOpen() {
Adam Cohen8dfcba42011-07-07 16:38:18 -0700371 if (mFirstOpen) {
372 setLayerType(LAYER_TYPE_HARDWARE, null);
373 buildLayer();
374 mFirstOpen = false;
375 }
376
Adam Cohen3e8f8112011-07-02 18:03:00 -0700377 positionAndSizeAsIcon();
378
Adam Cohen8e776a62011-06-28 18:10:06 -0700379 if (!(getParent() instanceof DragLayer)) return;
Adam Cohendf2cc412011-04-27 16:56:57 -0700380
Adam Cohen2801caf2011-05-13 20:57:39 -0700381 ObjectAnimator oa;
Adam Cohen8e776a62011-06-28 18:10:06 -0700382 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohendf2cc412011-04-27 16:56:57 -0700383
Adam Cohen2801caf2011-05-13 20:57:39 -0700384 centerAboutIcon();
385 if (mMode == PARTIAL_GROW) {
386 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1);
387 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
388 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
389 oa = ObjectAnimator.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
390 } else {
391 PropertyValuesHolder width = PropertyValuesHolder.ofInt("width", mNewSize.width());
392 PropertyValuesHolder height = PropertyValuesHolder.ofInt("height", mNewSize.height());
393 PropertyValuesHolder x = PropertyValuesHolder.ofInt("x", mNewSize.left);
394 PropertyValuesHolder y = PropertyValuesHolder.ofInt("y", mNewSize.top);
395 oa = ObjectAnimator.ofPropertyValuesHolder(lp, width, height, x, y);
396 oa.addUpdateListener(new AnimatorUpdateListener() {
397 public void onAnimationUpdate(ValueAnimator animation) {
398 requestLayout();
399 }
400 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700401
Adam Cohen2801caf2011-05-13 20:57:39 -0700402 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
403 ObjectAnimator alphaOa = ObjectAnimator.ofPropertyValuesHolder(mContent, alpha);
404 alphaOa.setDuration(mExpandDuration);
405 alphaOa.setInterpolator(new AccelerateInterpolator(2.0f));
406 alphaOa.start();
407 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700408
Adam Cohen2801caf2011-05-13 20:57:39 -0700409 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700410 @Override
411 public void onAnimationStart(Animator animation) {
412 mState = STATE_ANIMATING;
413 }
414 @Override
415 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700416 mState = STATE_OPEN;
Adam Cohen8dfcba42011-07-07 16:38:18 -0700417 setLayerType(LAYER_TYPE_NONE, null);
418 enableHardwareLayersForChildren();
Adam Cohendf2cc412011-04-27 16:56:57 -0700419 }
420 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700421 oa.setDuration(mExpandDuration);
422 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700423 }
424
Adam Cohen8dfcba42011-07-07 16:38:18 -0700425 void enableHardwareLayersForChildren() {
426 ArrayList<View> children = getItemsInReadingOrder();
427 for (View child: children) {
428 child.setLayerType(LAYER_TYPE_HARDWARE, null);
429 }
430 }
431
Adam Cohendf2cc412011-04-27 16:56:57 -0700432 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700433 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen8dfcba42011-07-07 16:38:18 -0700434 setLayerType(LAYER_TYPE_HARDWARE, null);
435 buildLayer();
Adam Cohendf2cc412011-04-27 16:56:57 -0700436
Adam Cohen2801caf2011-05-13 20:57:39 -0700437 ObjectAnimator oa;
Adam Cohen2801caf2011-05-13 20:57:39 -0700438 if (mMode == PARTIAL_GROW) {
439 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
440 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
441 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
442 oa = ObjectAnimator.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
443 } else {
Adam Cohen8e776a62011-06-28 18:10:06 -0700444 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohendf2cc412011-04-27 16:56:57 -0700445
Adam Cohen3e8f8112011-07-02 18:03:00 -0700446 PropertyValuesHolder width = PropertyValuesHolder.ofInt("width", mIconRect.width());
447 PropertyValuesHolder height = PropertyValuesHolder.ofInt("height", mIconRect.height());
448 PropertyValuesHolder x = PropertyValuesHolder.ofInt("x", mIconRect.left);
449 PropertyValuesHolder y = PropertyValuesHolder.ofInt("y", mIconRect.top);
Adam Cohen2801caf2011-05-13 20:57:39 -0700450 oa = ObjectAnimator.ofPropertyValuesHolder(lp, width, height, x, y);
451 oa.addUpdateListener(new AnimatorUpdateListener() {
452 public void onAnimationUpdate(ValueAnimator animation) {
453 requestLayout();
454 }
455 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700456
Adam Cohen2801caf2011-05-13 20:57:39 -0700457 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0f);
458 ObjectAnimator alphaOa = ObjectAnimator.ofPropertyValuesHolder(mContent, alpha);
459 alphaOa.setDuration(mExpandDuration);
460 alphaOa.setInterpolator(new DecelerateInterpolator(2.0f));
461 alphaOa.start();
462 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700463
Adam Cohen2801caf2011-05-13 20:57:39 -0700464 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700465 @Override
466 public void onAnimationEnd(Animator animation) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700467 onClose();
Adam Cohen2801caf2011-05-13 20:57:39 -0700468 onCloseComplete();
Adam Cohen2801caf2011-05-13 20:57:39 -0700469 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700470 }
471 @Override
472 public void onAnimationStart(Animator animation) {
473 mState = STATE_ANIMATING;
474 }
475 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700476 oa.setDuration(mExpandDuration);
477 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700478 }
479
480 void notifyDataSetChanged() {
481 // recreate all the children if the data set changes under us. We may want to do this more
482 // intelligently (ie just removing the views that should no longer exist)
483 mContent.removeAllViewsInLayout();
484 bind(mInfo);
485 }
486
Adam Cohencb3382b2011-05-24 14:07:08 -0700487 public boolean acceptDrop(DragObject d) {
488 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700489 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700490 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
491 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
492 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700493 }
494
Adam Cohendf2cc412011-04-27 16:56:57 -0700495 protected boolean findAndSetEmptyCells(ShortcutInfo item) {
496 int[] emptyCell = new int[2];
497 if (mContent.findCellForSpan(emptyCell, item.spanX, item.spanY)) {
498 item.cellX = emptyCell[0];
499 item.cellY = emptyCell[1];
Adam Cohendf2cc412011-04-27 16:56:57 -0700500 return true;
501 } else {
502 return false;
503 }
504 }
505
Adam Cohenc508b2d2011-06-28 14:41:44 -0700506 protected boolean createAndAddShortcut(ShortcutInfo item) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700507 final TextView textView =
Adam Cohene87b9242011-06-29 14:01:26 -0700508 (TextView) mInflater.inflate(R.layout.application, this, false);
Adam Cohendf2cc412011-04-27 16:56:57 -0700509 textView.setCompoundDrawablesWithIntrinsicBounds(null,
510 new FastBitmapDrawable(item.getIcon(mIconCache)), null, null);
511 textView.setText(item.title);
512 textView.setTag(item);
513
514 textView.setOnClickListener(this);
515 textView.setOnLongClickListener(this);
516
Adam Cohenc508b2d2011-06-28 14:41:44 -0700517 // We need to check here to verify that the given item's location isn't already occupied
518 // by another item. If it is, we need to find the next available slot and assign
519 // it that position. This is an issue when upgrading from the old Folders implementation
520 // which could contain an unlimited number of items.
Adam Cohen228da5a2011-07-27 22:23:47 -0700521 if (mContent.getChildAt(item.cellX, item.cellY) != null ||
522 item.cellX < 0 || item.cellY < 0) {
Adam Cohenc508b2d2011-06-28 14:41:44 -0700523 if (!findAndSetEmptyCells(item)) {
524 return false;
525 }
526 }
527
Adam Cohendf2cc412011-04-27 16:56:57 -0700528 CellLayout.LayoutParams lp =
529 new CellLayout.LayoutParams(item.cellX, item.cellY, item.spanX, item.spanY);
530 boolean insert = false;
531 mContent.addViewToCellLayout(textView, insert ? 0 : -1, (int)item.id, lp, true);
Adam Cohenc508b2d2011-06-28 14:41:44 -0700532 return true;
Adam Cohendf2cc412011-04-27 16:56:57 -0700533 }
534
Adam Cohencb3382b2011-05-24 14:07:08 -0700535 public void onDragEnter(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700536 mPreviousTargetCell[0] = -1;
537 mPreviousTargetCell[1] = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700538 mOnExitAlarm.cancelAlarm();
539 }
540
541 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
542 public void onAlarm(Alarm alarm) {
543 realTimeReorder(mEmptyCell, mTargetCell);
544 }
545 };
546
547 boolean readingOrderGreaterThan(int[] v1, int[] v2) {
548 if (v1[1] > v2[1] || (v1[1] == v2[1] && v1[0] > v2[0])) {
549 return true;
550 } else {
551 return false;
552 }
553 }
554
555 private void realTimeReorder(int[] empty, int[] target) {
556 boolean wrap;
557 int startX;
558 int endX;
559 int startY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700560 int delay = 0;
561 float delayAmount = 30;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700562 if (readingOrderGreaterThan(target, empty)) {
563 wrap = empty[0] >= mContent.getCountX() - 1;
564 startY = wrap ? empty[1] + 1 : empty[1];
565 for (int y = startY; y <= target[1]; y++) {
566 startX = y == empty[1] ? empty[0] + 1 : 0;
567 endX = y < target[1] ? mContent.getCountX() - 1 : target[0];
568 for (int x = startX; x <= endX; x++) {
569 View v = mContent.getChildAt(x,y);
570 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen76fc0852011-06-17 13:26:23 -0700571 REORDER_ANIMATION_DURATION, delay)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700572 empty[0] = x;
573 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700574 delay += delayAmount;
575 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700576 }
577 }
578 }
579 } else {
580 wrap = empty[0] == 0;
581 startY = wrap ? empty[1] - 1 : empty[1];
582 for (int y = startY; y >= target[1]; y--) {
583 startX = y == empty[1] ? empty[0] - 1 : mContent.getCountX() - 1;
584 endX = y > target[1] ? 0 : target[0];
585 for (int x = startX; x >= endX; x--) {
586 View v = mContent.getChildAt(x,y);
587 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen76fc0852011-06-17 13:26:23 -0700588 REORDER_ANIMATION_DURATION, delay)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700589 empty[0] = x;
590 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700591 delay += delayAmount;
592 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700593 }
594 }
595 }
596 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700597 }
598
Adam Cohencb3382b2011-05-24 14:07:08 -0700599 public void onDragOver(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700600 float[] r = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, d.dragView, null);
601 mTargetCell = mContent.findNearestArea((int) r[0], (int) r[1], 1, 1, mTargetCell);
602
603 if (mTargetCell[0] != mPreviousTargetCell[0] || mTargetCell[1] != mPreviousTargetCell[1]) {
604 mReorderAlarm.cancelAlarm();
605 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
606 mReorderAlarm.setAlarm(150);
607 mPreviousTargetCell[0] = mTargetCell[0];
608 mPreviousTargetCell[1] = mTargetCell[1];
609 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700610 }
611
Adam Cohenbfbfd262011-06-13 16:55:12 -0700612 // This is used to compute the visual center of the dragView. The idea is that
613 // the visual center represents the user's interpretation of where the item is, and hence
614 // is the appropriate point to use when determining drop location.
615 private float[] getDragViewVisualCenter(int x, int y, int xOffset, int yOffset,
616 DragView dragView, float[] recycle) {
617 float res[];
618 if (recycle == null) {
619 res = new float[2];
620 } else {
621 res = recycle;
622 }
623
624 // These represent the visual top and left of drag view if a dragRect was provided.
625 // If a dragRect was not provided, then they correspond to the actual view left and
626 // top, as the dragRect is in that case taken to be the entire dragView.
627 // R.dimen.dragViewOffsetY.
628 int left = x - xOffset;
629 int top = y - yOffset;
630
631 // In order to find the visual center, we shift by half the dragRect
632 res[0] = left + dragView.getDragRegion().width() / 2;
633 res[1] = top + dragView.getDragRegion().height() / 2;
634
635 return res;
636 }
637
638 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
639 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700640 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700641 }
642 };
643
Adam Cohen95bb8002011-07-03 23:40:28 -0700644 public void completeDragExit() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700645 mLauncher.closeFolder();
646 mCurrentDragInfo = null;
647 mCurrentDragView = null;
648 mSuppressOnAdd = false;
649 mRearrangeOnClose = true;
650 }
651
Adam Cohencb3382b2011-05-24 14:07:08 -0700652 public void onDragExit(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700653 // We only close the folder if this is a true drag exit, ie. not because a drop
654 // has occurred above the folder.
655 if (!d.dragComplete) {
656 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
657 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
658 }
659 mReorderAlarm.cancelAlarm();
Adam Cohen2801caf2011-05-13 20:57:39 -0700660 }
661
Adam Cohenc0dcf592011-06-01 15:30:43 -0700662 public void onDropCompleted(View target, DragObject d, boolean success) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700663 mCurrentDragInfo = null;
664 mCurrentDragView = null;
665 mSuppressOnAdd = false;
Adam Cohen228da5a2011-07-27 22:23:47 -0700666 if (target != this) {
667 mOnExitAlarm.cancelAlarm();
668 completeDragExit();
669 }
670
Adam Cohen76078c42011-06-09 15:06:52 -0700671 if (!success) {
Adam Cohen228da5a2011-07-27 22:23:47 -0700672 if (!mDestroyed) {
673 mFolderIcon.onDrop(d);
674 } else {
675 // TODO: if the folder was removed, recreate it
Adam Cohen3e8f8112011-07-02 18:03:00 -0700676 }
Adam Cohen76078c42011-06-09 15:06:52 -0700677 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700678 }
679
680 public boolean isDropEnabled() {
681 return true;
682 }
683
Adam Cohencb3382b2011-05-24 14:07:08 -0700684 public DropTarget getDropTargetDelegate(DragObject d) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700685 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800686 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700687
Adam Cohen2801caf2011-05-13 20:57:39 -0700688 private void setupContentDimension(int count) {
689 ArrayList<View> list = getItemsInReadingOrder();
690
691 int countX = mContent.getCountX();
692 int countY = mContent.getCountY();
Adam Cohen7c693212011-05-18 15:26:57 -0700693 boolean done = false;
Adam Cohen2801caf2011-05-13 20:57:39 -0700694
Adam Cohen7c693212011-05-18 15:26:57 -0700695 while (!done) {
696 int oldCountX = countX;
697 int oldCountY = countY;
698 if (countX * countY < count) {
699 // Current grid is too small, expand it
700 if (countX <= countY && countX < mMaxCountX) {
701 countX++;
702 } else if (countY < mMaxCountY) {
703 countY++;
704 }
705 if (countY == 0) countY++;
706 } else if ((countY - 1) * countX >= count && countY >= countX) {
707 countY = Math.max(0, countY - 1);
708 } else if ((countX - 1) * countY >= count) {
709 countX = Math.max(0, countX - 1);
Adam Cohen2801caf2011-05-13 20:57:39 -0700710 }
Adam Cohen7c693212011-05-18 15:26:57 -0700711 done = countX == oldCountX && countY == oldCountY;
Adam Cohen2801caf2011-05-13 20:57:39 -0700712 }
Adam Cohen7c693212011-05-18 15:26:57 -0700713 mContent.setGridSize(countX, countY);
Adam Cohen2801caf2011-05-13 20:57:39 -0700714 arrangeChildren(list);
715 }
716
717 public boolean isFull() {
718 return getItemCount() >= mMaxCountX * mMaxCountY;
719 }
720
721 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700722 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700723
724 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf4bb1cd2011-07-22 14:36:03 -0700725 int height = getPaddingTop() + getPaddingBottom() + mContent.getDesiredHeight()
726 + mFolderNameHeight;
Adam Cohen8e776a62011-06-28 18:10:06 -0700727 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700728
Adam Cohen8e776a62011-06-28 18:10:06 -0700729 parent.getDescendantRectRelativeToSelf(mFolderIcon, mTempRect);
730
731 int centerX = mTempRect.centerX();
732 int centerY = mTempRect.centerY();
Adam Cohen2801caf2011-05-13 20:57:39 -0700733 int centeredLeft = centerX - width / 2;
734 int centeredTop = centerY - height / 2;
735
Adam Cohen35e7e642011-07-17 14:47:18 -0700736 // We first fetch the currently visible CellLayoutChildren
Winson Chung3d503fb2011-07-13 17:25:49 -0700737 CellLayout currentPage = mLauncher.getWorkspace().getCurrentDropLayout();
Adam Cohen35e7e642011-07-17 14:47:18 -0700738 CellLayoutChildren boundingLayout = currentPage.getChildrenLayout();
739 Rect bounds = new Rect();
740 parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
Adam Cohen2801caf2011-05-13 20:57:39 -0700741
Adam Cohen35e7e642011-07-17 14:47:18 -0700742 // We need to bound the folder to the currently visible CellLayoutChildren
743 int left = Math.min(Math.max(bounds.left, centeredLeft),
744 bounds.left + bounds.width() - width);
745 int top = Math.min(Math.max(bounds.top, centeredTop),
746 bounds.top + bounds.height() - height);
747 // If the folder doesn't fit within the bounds, center it about the desired bounds
748 if (width >= bounds.width()) {
749 left = bounds.left + (bounds.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700750 }
Adam Cohen35e7e642011-07-17 14:47:18 -0700751 if (height >= bounds.height()) {
752 top = bounds.top + (bounds.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700753 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700754
755 int folderPivotX = width / 2 + (centeredLeft - left);
756 int folderPivotY = height / 2 + (centeredTop - top);
757 setPivotX(folderPivotX);
758 setPivotY(folderPivotY);
759 int folderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
760 (1.0f * folderPivotX / width));
761 int folderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
762 (1.0f * folderPivotY / height));
763 mFolderIcon.setPivotX(folderIconPivotX);
764 mFolderIcon.setPivotY(folderIconPivotY);
765
766 if (mMode == PARTIAL_GROW) {
767 lp.width = width;
768 lp.height = height;
769 lp.x = left;
770 lp.y = top;
771 } else {
772 mNewSize.set(left, top, left + width, top + height);
773 }
774 }
775
776 private void setupContentForNumItems(int count) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700777 setupContentDimension(count);
778
Adam Cohen8e776a62011-06-28 18:10:06 -0700779 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700780 if (lp == null) {
Adam Cohen8e776a62011-06-28 18:10:06 -0700781 lp = new DragLayer.LayoutParams(0, 0);
782 lp.customPosition = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700783 setLayoutParams(lp);
784 }
785 centerAboutIcon();
786 }
787
Adam Cohen234c4cd2011-07-17 21:03:04 -0700788 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
789 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf4bb1cd2011-07-22 14:36:03 -0700790 int height = getPaddingTop() + getPaddingBottom() + mContent.getDesiredHeight()
791 + mFolderNameHeight;
Adam Cohen234c4cd2011-07-17 21:03:04 -0700792
793 int contentWidthSpec = MeasureSpec.makeMeasureSpec(mContent.getDesiredWidth(),
794 MeasureSpec.EXACTLY);
795 int contentHeightSpec = MeasureSpec.makeMeasureSpec(mContent.getDesiredHeight(),
796 MeasureSpec.EXACTLY);
797 mContent.measure(contentWidthSpec, contentHeightSpec);
798
799 mFolderName.measure(contentWidthSpec,
800 MeasureSpec.makeMeasureSpec(mFolderNameHeight, MeasureSpec.EXACTLY));
801 setMeasuredDimension(width, height);
802 }
803
Adam Cohen2801caf2011-05-13 20:57:39 -0700804 private void arrangeChildren(ArrayList<View> list) {
805 int[] vacant = new int[2];
806 if (list == null) {
807 list = getItemsInReadingOrder();
808 }
809 mContent.removeAllViews();
810
811 for (int i = 0; i < list.size(); i++) {
812 View v = list.get(i);
813 mContent.getVacantCell(vacant, 1, 1);
814 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) v.getLayoutParams();
815 lp.cellX = vacant[0];
816 lp.cellY = vacant[1];
817 ItemInfo info = (ItemInfo) v.getTag();
818 info.cellX = vacant[0];
819 info.cellY = vacant[1];
820 boolean insert = false;
821 mContent.addViewToCellLayout(v, insert ? 0 : -1, (int)info.id, lp, true);
Adam Cohen7c693212011-05-18 15:26:57 -0700822 LauncherModel.addOrMoveItemInDatabase(mLauncher, info, mInfo.id, 0,
823 info.cellX, info.cellY);
Adam Cohen2801caf2011-05-13 20:57:39 -0700824 }
Adam Cohen7c693212011-05-18 15:26:57 -0700825 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700826 }
827
Adam Cohena9cf38f2011-05-02 15:36:58 -0700828 public int getItemCount() {
829 return mContent.getChildrenLayout().getChildCount();
830 }
831
832 public View getItemAt(int index) {
833 return mContent.getChildrenLayout().getChildAt(index);
834 }
835
Adam Cohen2801caf2011-05-13 20:57:39 -0700836 private void onCloseComplete() {
837 if (mRearrangeOnClose) {
838 setupContentForNumItems(getItemCount());
839 mRearrangeOnClose = false;
840 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700841 if (getItemCount() <= 1) {
842 replaceFolderWithFinalItem();
843 }
844 }
845
846 private void replaceFolderWithFinalItem() {
847 ItemInfo finalItem = null;
848
Adam Cohen228da5a2011-07-27 22:23:47 -0700849 mDestroyed = true;
Adam Cohenafb01ee2011-06-23 15:38:03 -0700850 if (getItemCount() == 1) {
851 finalItem = mInfo.contents.get(0);
852 }
853
854 // Remove the folder completely
Winson Chung3d503fb2011-07-13 17:25:49 -0700855 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screen);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700856 cellLayout.removeView(mFolderIcon);
857 if (mFolderIcon instanceof DropTarget) {
858 mDragController.removeDropTarget((DropTarget) mFolderIcon);
859 }
860 mLauncher.removeFolder(mInfo);
861
862 if (finalItem != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700863 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
864 mInfo.screen, mInfo.cellX, mInfo.cellY);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700865 }
Adam Cohen716b51e2011-06-30 12:09:54 -0700866 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700867
868 // Add the last remaining child to the workspace in place of the folder
869 if (finalItem != null) {
870 View child = mLauncher.createShortcut(R.layout.application, cellLayout,
871 (ShortcutInfo) finalItem);
872
Winson Chung3d503fb2011-07-13 17:25:49 -0700873 mLauncher.getWorkspace().addInScreen(child, mInfo.container, mInfo.screen, mInfo.cellX,
874 mInfo.cellY, mInfo.spanX, mInfo.spanY);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700875 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700876 }
877
Adam Cohenbfbfd262011-06-13 16:55:12 -0700878 public void onDrop(DragObject d) {
879 ShortcutInfo item;
880 if (d.dragInfo instanceof ApplicationInfo) {
881 // Came from all apps -- make a copy
882 item = ((ApplicationInfo) d.dragInfo).makeShortcut();
883 item.spanX = 1;
884 item.spanY = 1;
885 } else {
886 item = (ShortcutInfo) d.dragInfo;
887 }
888 // Dragged from self onto self
889 if (item == mCurrentDragInfo) {
890 ShortcutInfo si = (ShortcutInfo) mCurrentDragView.getTag();
891 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) mCurrentDragView.getLayoutParams();
892 si.cellX = lp.cellX = mEmptyCell[0];
893 si.cellX = lp.cellY = mEmptyCell[1];
894 mContent.addViewToCellLayout(mCurrentDragView, -1, (int)item.id, lp, true);
Adam Cohenfc53cd22011-07-20 15:45:11 -0700895 if (d.dragView.hasDrawn()) {
896 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, mCurrentDragView);
897 } else {
898 mCurrentDragView.setVisibility(VISIBLE);
899 }
Adam Cohene9166b22011-07-08 17:11:11 -0700900 mItemsInvalidated = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700901 setupContentDimension(getItemCount());
Adam Cohen716b51e2011-06-30 12:09:54 -0700902 mSuppressOnAdd = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700903 }
904 mInfo.add(item);
905 }
906
907 public void onAdd(ShortcutInfo item) {
908 mItemsInvalidated = true;
909 if (mSuppressOnAdd) return;
910 if (!findAndSetEmptyCells(item)) {
911 // The current layout is full, can we expand it?
912 setupContentForNumItems(getItemCount() + 1);
913 findAndSetEmptyCells(item);
914 }
915 createAndAddShortcut(item);
916 LauncherModel.addOrMoveItemInDatabase(
917 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
918 }
919
Adam Cohena9cf38f2011-05-02 15:36:58 -0700920 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -0700921 mItemsInvalidated = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700922 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -0700923 View v = getViewForInfo(item);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700924 mContent.removeView(v);
Adam Cohen2801caf2011-05-13 20:57:39 -0700925 if (mState == STATE_ANIMATING) {
926 mRearrangeOnClose = true;
927 } else {
928 setupContentForNumItems(getItemCount());
929 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700930 if (getItemCount() <= 1) {
931 replaceFolderWithFinalItem();
932 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700933 }
Adam Cohen7c693212011-05-18 15:26:57 -0700934
Adam Cohendf1e4e82011-06-24 15:57:39 -0700935 private View getViewForInfo(ShortcutInfo item) {
936 for (int j = 0; j < mContent.getCountY(); j++) {
937 for (int i = 0; i < mContent.getCountX(); i++) {
938 View v = mContent.getChildAt(i, j);
939 if (v.getTag() == item) {
940 return v;
941 }
942 }
943 }
944 return null;
945 }
946
Adam Cohen76078c42011-06-09 15:06:52 -0700947 public void onItemsChanged() {
948 }
Adam Cohen76fc0852011-06-17 13:26:23 -0700949 public void onTitleChanged(CharSequence title) {
950 }
Adam Cohen76078c42011-06-09 15:06:52 -0700951
Adam Cohen7c693212011-05-18 15:26:57 -0700952 public ArrayList<View> getItemsInReadingOrder() {
Adam Cohen76078c42011-06-09 15:06:52 -0700953 return getItemsInReadingOrder(true);
954 }
955
956 public ArrayList<View> getItemsInReadingOrder(boolean includeCurrentDragItem) {
Adam Cohen7c693212011-05-18 15:26:57 -0700957 if (mItemsInvalidated) {
958 mItemsInReadingOrder.clear();
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 != null) {
Adam Cohen76078c42011-06-09 15:06:52 -0700963 ShortcutInfo info = (ShortcutInfo) v.getTag();
964 if (info != mCurrentDragInfo || includeCurrentDragItem) {
965 mItemsInReadingOrder.add(v);
966 }
Adam Cohen7c693212011-05-18 15:26:57 -0700967 }
968 }
969 }
970 mItemsInvalidated = false;
971 }
972 return mItemsInReadingOrder;
973 }
Adam Cohen8dfcba42011-07-07 16:38:18 -0700974
975 public void getLocationInDragLayer(int[] loc) {
976 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
977 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800978}