blob: d3b5d4171abb51593d2deaf3deacd9443c4faf37 [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;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.util.AttributeSet;
Adam Cohen76fc0852011-06-17 13:26:23 -070031import android.view.ActionMode;
32import android.view.KeyEvent;
Adam Cohendf2cc412011-04-27 16:56:57 -070033import android.view.LayoutInflater;
Adam Cohen76fc0852011-06-17 13:26:23 -070034import android.view.Menu;
35import android.view.MenuItem;
Adam Cohen0c872ba2011-05-05 10:34:16 -070036import android.view.MotionEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080037import android.view.View;
Adam Cohen234c4cd2011-07-17 21:03:04 -070038import android.view.View.MeasureSpec;
Adam Cohen2801caf2011-05-13 20:57:39 -070039import android.view.animation.AccelerateInterpolator;
40import android.view.animation.DecelerateInterpolator;
Adam Cohen76fc0852011-06-17 13:26:23 -070041import android.view.inputmethod.EditorInfo;
42import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.widget.AdapterView;
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
103 private boolean mIsEditingName = false;
104 private InputMethodManager mInputMethodManager;
Adam Cohendf2cc412011-04-27 16:56:57 -0700105
Adam Cohena65beee2011-06-27 21:32:23 -0700106 private static String sDefaultFolderName;
107 private static String sHintText;
108
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800109 /**
110 * Used to inflate the Workspace from XML.
111 *
112 * @param context The application's context.
113 * @param attrs The attribtues set containing the Workspace's customization values.
114 */
115 public Folder(Context context, AttributeSet attrs) {
116 super(context, attrs);
117 setAlwaysDrawnWithCacheEnabled(false);
Adam Cohendf2cc412011-04-27 16:56:57 -0700118 mInflater = LayoutInflater.from(context);
119 mIconCache = ((LauncherApplication)context.getApplicationContext()).getIconCache();
Adam Cohen8e776a62011-06-28 18:10:06 -0700120 mMaxCountX = LauncherModel.getCellCountX();
121 mMaxCountY = LauncherModel.getCellCountY();
Adam Cohen76fc0852011-06-17 13:26:23 -0700122
123 mInputMethodManager = (InputMethodManager)
124 mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
125
126 Resources res = getResources();
127 mExpandDuration = res.getInteger(R.integer.config_folderAnimDuration);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700128
129 if (sDefaultFolderName == null) {
130 sDefaultFolderName = res.getString(R.string.folder_name);
131 }
Adam Cohena65beee2011-06-27 21:32:23 -0700132 if (sHintText == null) {
133 sHintText = res.getString(R.string.folder_hint_text);
134 }
Adam Cohen4eac29a2011-07-11 17:53:37 -0700135
136 mLauncher = (Launcher) context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137 }
138
139 @Override
140 protected void onFinishInflate() {
141 super.onFinishInflate();
Adam Cohendf2cc412011-04-27 16:56:57 -0700142 mContent = (CellLayout) findViewById(R.id.folder_content);
Adam Cohen2801caf2011-05-13 20:57:39 -0700143 mContent.setGridSize(0, 0);
Adam Cohen76fc0852011-06-17 13:26:23 -0700144 mFolderName = (TextView) findViewById(R.id.folder_name);
145
146 // We find out how tall the text view wants to be (it is set to wrap_content), so that
147 // we can allocate the appropriate amount of space for it.
148 int measureSpec = MeasureSpec.UNSPECIFIED;
149 mFolderName.measure(measureSpec, measureSpec);
150 mFolderNameHeight = mFolderName.getMeasuredHeight();
151
152 // We disable action mode for now since it messes up the view on phones
153 mFolderName.setCustomSelectionActionModeCallback(mActionModeCallback);
154 mFolderName.setCursorVisible(false);
155 mFolderName.setOnEditorActionListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700156 mFolderName.setSelectAllOnFocus(true);
Adam Cohen7a14d0b2011-06-24 11:36:35 -0700157 mFolderName.setInputType(mFolderName.getInputType() |
158 InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700160
Adam Cohen76fc0852011-06-17 13:26:23 -0700161 private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
162 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
163 return false;
164 }
165
166 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
167 return false;
168 }
169
170 public void onDestroyActionMode(ActionMode mode) {
171 }
172
173 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
174 return false;
175 }
176 };
177
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178 public void onClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700179 Object tag = v.getTag();
180 if (tag instanceof ShortcutInfo) {
181 // refactor this code from Folder
182 ShortcutInfo item = (ShortcutInfo) tag;
183 int[] pos = new int[2];
184 v.getLocationOnScreen(pos);
185 item.intent.setSourceBounds(new Rect(pos[0], pos[1],
186 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
187 mLauncher.startActivitySafely(item.intent, item);
Adam Cohendf2cc412011-04-27 16:56:57 -0700188 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800189 }
190
Adam Cohen76fc0852011-06-17 13:26:23 -0700191 public boolean onInterceptTouchEvent(MotionEvent ev) {
192 if (ev.getAction() == MotionEvent.ACTION_DOWN) {
193 mFolderName.getHitRect(mHitRect);
194 if (mHitRect.contains((int) ev.getX(), (int) ev.getY()) && !mIsEditingName) {
195 startEditingFolderName();
196 }
197 }
198 return false;
199 }
200
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800201 public boolean onLongClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700202 Object tag = v.getTag();
203 if (tag instanceof ShortcutInfo) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700204 ShortcutInfo item = (ShortcutInfo) tag;
205 if (!v.isInTouchMode()) {
206 return false;
207 }
208
209 mLauncher.getWorkspace().onDragStartedWithItem(v);
Adam Cohenac8c8762011-07-13 11:15:27 -0700210 mLauncher.getWorkspace().beginDragShared(v, this);
Adam Cohenbadf71e2011-05-26 19:08:29 -0700211 mIconDrawable = ((TextView) v).getCompoundDrawables()[1];
Adam Cohen76078c42011-06-09 15:06:52 -0700212
213 mCurrentDragInfo = item;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700214 mEmptyCell[0] = item.cellX;
215 mEmptyCell[1] = item.cellY;
216 mCurrentDragView = v;
Adam Cohenfc53cd22011-07-20 15:45:11 -0700217
218 mContent.removeView(mCurrentDragView);
219 mInfo.remove(mCurrentDragInfo);
Adam Cohendf2cc412011-04-27 16:56:57 -0700220 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800221 return true;
222 }
223
Adam Cohen76fc0852011-06-17 13:26:23 -0700224 public boolean isEditingName() {
225 return mIsEditingName;
226 }
227
228 public void startEditingFolderName() {
Adam Cohena65beee2011-06-27 21:32:23 -0700229 mFolderName.setHint("");
Adam Cohen76fc0852011-06-17 13:26:23 -0700230 mFolderName.setCursorVisible(true);
231 mIsEditingName = true;
232 }
233
234 public void dismissEditingName() {
235 mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
236 doneEditingFolderName(true);
237 }
238
239 public void doneEditingFolderName(boolean commit) {
Adam Cohena65beee2011-06-27 21:32:23 -0700240 mFolderName.setHint(sHintText);
Adam Cohen76fc0852011-06-17 13:26:23 -0700241 mInfo.setTitle(mFolderName.getText());
242 LauncherModel.updateItemInDatabase(mLauncher, mInfo);
243 mFolderName.setCursorVisible(false);
244 mFolderName.clearFocus();
245 mIsEditingName = false;
246 }
247
248 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
249 if (actionId == EditorInfo.IME_ACTION_DONE) {
250 dismissEditingName();
251 return true;
252 }
253 return false;
254 }
255
256 public View getEditTextRegion() {
257 return mFolderName;
258 }
259
Adam Cohenbadf71e2011-05-26 19:08:29 -0700260 public Drawable getDragDrawable() {
261 return mIconDrawable;
262 }
263
Adam Cohen0c872ba2011-05-05 10:34:16 -0700264 /**
265 * We need to handle touch events to prevent them from falling through to the workspace below.
266 */
267 @Override
268 public boolean onTouchEvent(MotionEvent ev) {
269 return true;
270 }
271
Joe Onorato00acb122009-08-04 16:04:30 -0400272 public void setDragController(DragController dragController) {
273 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800274 }
275
Adam Cohen2801caf2011-05-13 20:57:39 -0700276 void setFolderIcon(FolderIcon icon) {
277 mFolderIcon = icon;
278 }
279
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800280 /**
281 * @return the FolderInfo object associated with this folder
282 */
283 FolderInfo getInfo() {
284 return mInfo;
285 }
286
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800287 void onOpen() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700288 // When the folder opens, we need to refresh the GridView's selection by
289 // forcing a layout
290 // TODO: find out if this is still necessary
291 mContent.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800292 }
293
294 void onClose() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700295 DragLayer parent = (DragLayer) getParent();
296 parent.removeView(Folder.this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700297 clearFocus();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800298 }
299
300 void bind(FolderInfo info) {
301 mInfo = info;
Adam Cohendf2cc412011-04-27 16:56:57 -0700302 ArrayList<ShortcutInfo> children = info.contents;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700303 ArrayList<ShortcutInfo> overflow = new ArrayList<ShortcutInfo>();
Adam Cohen7c693212011-05-18 15:26:57 -0700304 setupContentForNumItems(children.size());
Adam Cohendf2cc412011-04-27 16:56:57 -0700305 for (int i = 0; i < children.size(); i++) {
306 ShortcutInfo child = (ShortcutInfo) children.get(i);
Adam Cohenc508b2d2011-06-28 14:41:44 -0700307 if (!createAndAddShortcut(child)) {
308 overflow.add(child);
309 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700310 }
Adam Cohenc508b2d2011-06-28 14:41:44 -0700311
312 // If our folder has too many items we prune them from the list. This is an issue
313 // when upgrading from the old Folders implementation which could contain an unlimited
314 // number of items.
315 for (ShortcutInfo item: overflow) {
316 mInfo.remove(item);
317 LauncherModel.deleteItemFromDatabase(mLauncher, item);
318 }
319
Adam Cohen4dbe6d92011-05-18 17:14:15 -0700320 mItemsInvalidated = true;
Adam Cohena9cf38f2011-05-02 15:36:58 -0700321 mInfo.addListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700322
Adam Cohenafb01ee2011-06-23 15:38:03 -0700323 if (!sDefaultFolderName.contentEquals(mInfo.title)) {
Adam Cohen4ef610f2011-06-21 22:37:36 -0700324 mFolderName.setText(mInfo.title);
325 } else {
326 mFolderName.setText("");
327 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700328 }
329
330 /**
331 * Creates a new UserFolder, inflated from R.layout.user_folder.
332 *
333 * @param context The application's context.
334 *
335 * @return A new UserFolder.
336 */
337 static Folder fromXml(Context context) {
338 return (Folder) LayoutInflater.from(context).inflate(R.layout.user_folder, null);
339 }
340
341 /**
342 * This method is intended to make the UserFolder to be visually identical in size and position
343 * to its associated FolderIcon. This allows for a seamless transition into the expanded state.
344 */
345 private void positionAndSizeAsIcon() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700346 if (!(getParent() instanceof DragLayer)) return;
Adam Cohendf2cc412011-04-27 16:56:57 -0700347
Adam Cohen8e776a62011-06-28 18:10:06 -0700348 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohendf2cc412011-04-27 16:56:57 -0700349
Adam Cohen2801caf2011-05-13 20:57:39 -0700350 if (mMode == PARTIAL_GROW) {
351 setScaleX(0.8f);
352 setScaleY(0.8f);
353 setAlpha(0f);
354 } else {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700355 mLauncher.getDragLayer().getDescendantRectRelativeToSelf(mFolderIcon, mIconRect);
356 lp.width = mIconRect.width();
357 lp.height = mIconRect.height();
358 lp.x = mIconRect.left;
359 lp.y = mIconRect.top;
Adam Cohen2801caf2011-05-13 20:57:39 -0700360 mContent.setAlpha(0);
361 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700362 mState = STATE_SMALL;
363 }
364
365 public void animateOpen() {
Adam Cohen8dfcba42011-07-07 16:38:18 -0700366 if (mFirstOpen) {
367 setLayerType(LAYER_TYPE_HARDWARE, null);
368 buildLayer();
369 mFirstOpen = false;
370 }
371
Adam Cohen3e8f8112011-07-02 18:03:00 -0700372 positionAndSizeAsIcon();
373
Adam Cohen8e776a62011-06-28 18:10:06 -0700374 if (!(getParent() instanceof DragLayer)) return;
Adam Cohendf2cc412011-04-27 16:56:57 -0700375
Adam Cohen2801caf2011-05-13 20:57:39 -0700376 ObjectAnimator oa;
Adam Cohen8e776a62011-06-28 18:10:06 -0700377 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohendf2cc412011-04-27 16:56:57 -0700378
Adam Cohen2801caf2011-05-13 20:57:39 -0700379 centerAboutIcon();
380 if (mMode == PARTIAL_GROW) {
381 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1);
382 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
383 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
384 oa = ObjectAnimator.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
385 } else {
386 PropertyValuesHolder width = PropertyValuesHolder.ofInt("width", mNewSize.width());
387 PropertyValuesHolder height = PropertyValuesHolder.ofInt("height", mNewSize.height());
388 PropertyValuesHolder x = PropertyValuesHolder.ofInt("x", mNewSize.left);
389 PropertyValuesHolder y = PropertyValuesHolder.ofInt("y", mNewSize.top);
390 oa = ObjectAnimator.ofPropertyValuesHolder(lp, width, height, x, y);
391 oa.addUpdateListener(new AnimatorUpdateListener() {
392 public void onAnimationUpdate(ValueAnimator animation) {
393 requestLayout();
394 }
395 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700396
Adam Cohen2801caf2011-05-13 20:57:39 -0700397 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
398 ObjectAnimator alphaOa = ObjectAnimator.ofPropertyValuesHolder(mContent, alpha);
399 alphaOa.setDuration(mExpandDuration);
400 alphaOa.setInterpolator(new AccelerateInterpolator(2.0f));
401 alphaOa.start();
402 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700403
Adam Cohen2801caf2011-05-13 20:57:39 -0700404 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700405 @Override
406 public void onAnimationStart(Animator animation) {
407 mState = STATE_ANIMATING;
408 }
409 @Override
410 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700411 mState = STATE_OPEN;
Adam Cohen8dfcba42011-07-07 16:38:18 -0700412 setLayerType(LAYER_TYPE_NONE, null);
413 enableHardwareLayersForChildren();
Adam Cohendf2cc412011-04-27 16:56:57 -0700414 }
415 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700416 oa.setDuration(mExpandDuration);
417 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700418 }
419
Adam Cohen8dfcba42011-07-07 16:38:18 -0700420 void enableHardwareLayersForChildren() {
421 ArrayList<View> children = getItemsInReadingOrder();
422 for (View child: children) {
423 child.setLayerType(LAYER_TYPE_HARDWARE, null);
424 }
425 }
426
Adam Cohendf2cc412011-04-27 16:56:57 -0700427 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700428 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen8dfcba42011-07-07 16:38:18 -0700429 setLayerType(LAYER_TYPE_HARDWARE, null);
430 buildLayer();
Adam Cohendf2cc412011-04-27 16:56:57 -0700431
Adam Cohen2801caf2011-05-13 20:57:39 -0700432 ObjectAnimator oa;
Adam Cohen2801caf2011-05-13 20:57:39 -0700433 if (mMode == PARTIAL_GROW) {
434 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
435 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
436 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
437 oa = ObjectAnimator.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
438 } else {
Adam Cohen8e776a62011-06-28 18:10:06 -0700439 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohendf2cc412011-04-27 16:56:57 -0700440
Adam Cohen3e8f8112011-07-02 18:03:00 -0700441 PropertyValuesHolder width = PropertyValuesHolder.ofInt("width", mIconRect.width());
442 PropertyValuesHolder height = PropertyValuesHolder.ofInt("height", mIconRect.height());
443 PropertyValuesHolder x = PropertyValuesHolder.ofInt("x", mIconRect.left);
444 PropertyValuesHolder y = PropertyValuesHolder.ofInt("y", mIconRect.top);
Adam Cohen2801caf2011-05-13 20:57:39 -0700445 oa = ObjectAnimator.ofPropertyValuesHolder(lp, width, height, x, y);
446 oa.addUpdateListener(new AnimatorUpdateListener() {
447 public void onAnimationUpdate(ValueAnimator animation) {
448 requestLayout();
449 }
450 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700451
Adam Cohen2801caf2011-05-13 20:57:39 -0700452 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0f);
453 ObjectAnimator alphaOa = ObjectAnimator.ofPropertyValuesHolder(mContent, alpha);
454 alphaOa.setDuration(mExpandDuration);
455 alphaOa.setInterpolator(new DecelerateInterpolator(2.0f));
456 alphaOa.start();
457 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700458
Adam Cohen2801caf2011-05-13 20:57:39 -0700459 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700460 @Override
461 public void onAnimationEnd(Animator animation) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700462 onClose();
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.
516 if (mContent.getChildAt(item.cellX, item.cellY) != null) {
517 if (!findAndSetEmptyCells(item)) {
518 return false;
519 }
520 }
521
Adam Cohendf2cc412011-04-27 16:56:57 -0700522 CellLayout.LayoutParams lp =
523 new CellLayout.LayoutParams(item.cellX, item.cellY, item.spanX, item.spanY);
524 boolean insert = false;
525 mContent.addViewToCellLayout(textView, insert ? 0 : -1, (int)item.id, lp, true);
Adam Cohenc508b2d2011-06-28 14:41:44 -0700526 return true;
Adam Cohendf2cc412011-04-27 16:56:57 -0700527 }
528
Adam Cohencb3382b2011-05-24 14:07:08 -0700529 public void onDragEnter(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700530 mPreviousTargetCell[0] = -1;
531 mPreviousTargetCell[1] = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700532 mOnExitAlarm.cancelAlarm();
533 }
534
535 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
536 public void onAlarm(Alarm alarm) {
537 realTimeReorder(mEmptyCell, mTargetCell);
538 }
539 };
540
541 boolean readingOrderGreaterThan(int[] v1, int[] v2) {
542 if (v1[1] > v2[1] || (v1[1] == v2[1] && v1[0] > v2[0])) {
543 return true;
544 } else {
545 return false;
546 }
547 }
548
549 private void realTimeReorder(int[] empty, int[] target) {
550 boolean wrap;
551 int startX;
552 int endX;
553 int startY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700554 int delay = 0;
555 float delayAmount = 30;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700556 if (readingOrderGreaterThan(target, empty)) {
557 wrap = empty[0] >= mContent.getCountX() - 1;
558 startY = wrap ? empty[1] + 1 : empty[1];
559 for (int y = startY; y <= target[1]; y++) {
560 startX = y == empty[1] ? empty[0] + 1 : 0;
561 endX = y < target[1] ? mContent.getCountX() - 1 : target[0];
562 for (int x = startX; x <= endX; x++) {
563 View v = mContent.getChildAt(x,y);
564 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen76fc0852011-06-17 13:26:23 -0700565 REORDER_ANIMATION_DURATION, delay)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700566 empty[0] = x;
567 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700568 delay += delayAmount;
569 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700570 }
571 }
572 }
573 } else {
574 wrap = empty[0] == 0;
575 startY = wrap ? empty[1] - 1 : empty[1];
576 for (int y = startY; y >= target[1]; y--) {
577 startX = y == empty[1] ? empty[0] - 1 : mContent.getCountX() - 1;
578 endX = y > target[1] ? 0 : target[0];
579 for (int x = startX; x >= endX; x--) {
580 View v = mContent.getChildAt(x,y);
581 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen76fc0852011-06-17 13:26:23 -0700582 REORDER_ANIMATION_DURATION, delay)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700583 empty[0] = x;
584 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700585 delay += delayAmount;
586 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700587 }
588 }
589 }
590 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700591 }
592
Adam Cohencb3382b2011-05-24 14:07:08 -0700593 public void onDragOver(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700594 float[] r = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, d.dragView, null);
595 mTargetCell = mContent.findNearestArea((int) r[0], (int) r[1], 1, 1, mTargetCell);
596
597 if (mTargetCell[0] != mPreviousTargetCell[0] || mTargetCell[1] != mPreviousTargetCell[1]) {
598 mReorderAlarm.cancelAlarm();
599 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
600 mReorderAlarm.setAlarm(150);
601 mPreviousTargetCell[0] = mTargetCell[0];
602 mPreviousTargetCell[1] = mTargetCell[1];
603 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700604 }
605
Adam Cohenbfbfd262011-06-13 16:55:12 -0700606 // This is used to compute the visual center of the dragView. The idea is that
607 // the visual center represents the user's interpretation of where the item is, and hence
608 // is the appropriate point to use when determining drop location.
609 private float[] getDragViewVisualCenter(int x, int y, int xOffset, int yOffset,
610 DragView dragView, float[] recycle) {
611 float res[];
612 if (recycle == null) {
613 res = new float[2];
614 } else {
615 res = recycle;
616 }
617
618 // These represent the visual top and left of drag view if a dragRect was provided.
619 // If a dragRect was not provided, then they correspond to the actual view left and
620 // top, as the dragRect is in that case taken to be the entire dragView.
621 // R.dimen.dragViewOffsetY.
622 int left = x - xOffset;
623 int top = y - yOffset;
624
625 // In order to find the visual center, we shift by half the dragRect
626 res[0] = left + dragView.getDragRegion().width() / 2;
627 res[1] = top + dragView.getDragRegion().height() / 2;
628
629 return res;
630 }
631
632 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
633 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700634 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700635 }
636 };
637
Adam Cohen95bb8002011-07-03 23:40:28 -0700638 public void completeDragExit() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700639 mLauncher.closeFolder();
640 mCurrentDragInfo = null;
641 mCurrentDragView = null;
642 mSuppressOnAdd = false;
643 mRearrangeOnClose = true;
644 }
645
Adam Cohencb3382b2011-05-24 14:07:08 -0700646 public void onDragExit(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700647 // We only close the folder if this is a true drag exit, ie. not because a drop
648 // has occurred above the folder.
649 if (!d.dragComplete) {
650 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
651 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
652 }
653 mReorderAlarm.cancelAlarm();
Adam Cohen2801caf2011-05-13 20:57:39 -0700654 }
655
Adam Cohenc0dcf592011-06-01 15:30:43 -0700656 public void onDropCompleted(View target, DragObject d, boolean success) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700657 mCurrentDragInfo = null;
658 mCurrentDragView = null;
659 mSuppressOnAdd = false;
Adam Cohen76078c42011-06-09 15:06:52 -0700660 if (!success) {
661 if (d.dragView != null) {
662 if (target instanceof CellLayout) {
663 // TODO: we should animate the item back to the folder in this case
664 }
665 }
Adam Cohenbfbfd262011-06-13 16:55:12 -0700666 // TODO: if the drag fails, we need to re-add the item
Adam Cohen3e8f8112011-07-02 18:03:00 -0700667 } else {
668 if (target != this) {
669 mOnExitAlarm.cancelAlarm();
670 completeDragExit();
671 }
Adam Cohen76078c42011-06-09 15:06:52 -0700672 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700673 }
674
675 public boolean isDropEnabled() {
676 return true;
677 }
678
Adam Cohencb3382b2011-05-24 14:07:08 -0700679 public DropTarget getDropTargetDelegate(DragObject d) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700680 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800681 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700682
Adam Cohen2801caf2011-05-13 20:57:39 -0700683 private void setupContentDimension(int count) {
684 ArrayList<View> list = getItemsInReadingOrder();
685
686 int countX = mContent.getCountX();
687 int countY = mContent.getCountY();
Adam Cohen7c693212011-05-18 15:26:57 -0700688 boolean done = false;
Adam Cohen2801caf2011-05-13 20:57:39 -0700689
Adam Cohen7c693212011-05-18 15:26:57 -0700690 while (!done) {
691 int oldCountX = countX;
692 int oldCountY = countY;
693 if (countX * countY < count) {
694 // Current grid is too small, expand it
695 if (countX <= countY && countX < mMaxCountX) {
696 countX++;
697 } else if (countY < mMaxCountY) {
698 countY++;
699 }
700 if (countY == 0) countY++;
701 } else if ((countY - 1) * countX >= count && countY >= countX) {
702 countY = Math.max(0, countY - 1);
703 } else if ((countX - 1) * countY >= count) {
704 countX = Math.max(0, countX - 1);
Adam Cohen2801caf2011-05-13 20:57:39 -0700705 }
Adam Cohen7c693212011-05-18 15:26:57 -0700706 done = countX == oldCountX && countY == oldCountY;
Adam Cohen2801caf2011-05-13 20:57:39 -0700707 }
Adam Cohen7c693212011-05-18 15:26:57 -0700708 mContent.setGridSize(countX, countY);
Adam Cohen2801caf2011-05-13 20:57:39 -0700709 arrangeChildren(list);
710 }
711
712 public boolean isFull() {
713 return getItemCount() >= mMaxCountX * mMaxCountY;
714 }
715
716 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700717 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700718
719 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohen76fc0852011-06-17 13:26:23 -0700720 // Technically there is no padding at the bottom, but we add space equal to the padding
721 // and have to account for that here.
722 int height = getPaddingTop() + mContent.getDesiredHeight() + mFolderNameHeight;
Adam Cohen8e776a62011-06-28 18:10:06 -0700723 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700724
Adam Cohen8e776a62011-06-28 18:10:06 -0700725 parent.getDescendantRectRelativeToSelf(mFolderIcon, mTempRect);
726
727 int centerX = mTempRect.centerX();
728 int centerY = mTempRect.centerY();
Adam Cohen2801caf2011-05-13 20:57:39 -0700729 int centeredLeft = centerX - width / 2;
730 int centeredTop = centerY - height / 2;
731
Adam Cohen35e7e642011-07-17 14:47:18 -0700732 // We first fetch the currently visible CellLayoutChildren
Winson Chung3d503fb2011-07-13 17:25:49 -0700733 CellLayout currentPage = mLauncher.getWorkspace().getCurrentDropLayout();
Adam Cohen35e7e642011-07-17 14:47:18 -0700734 CellLayoutChildren boundingLayout = currentPage.getChildrenLayout();
735 Rect bounds = new Rect();
736 parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
Adam Cohen2801caf2011-05-13 20:57:39 -0700737
Adam Cohen35e7e642011-07-17 14:47:18 -0700738 // We need to bound the folder to the currently visible CellLayoutChildren
739 int left = Math.min(Math.max(bounds.left, centeredLeft),
740 bounds.left + bounds.width() - width);
741 int top = Math.min(Math.max(bounds.top, centeredTop),
742 bounds.top + bounds.height() - height);
743 // If the folder doesn't fit within the bounds, center it about the desired bounds
744 if (width >= bounds.width()) {
745 left = bounds.left + (bounds.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700746 }
Adam Cohen35e7e642011-07-17 14:47:18 -0700747 if (height >= bounds.height()) {
748 top = bounds.top + (bounds.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700749 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700750
751 int folderPivotX = width / 2 + (centeredLeft - left);
752 int folderPivotY = height / 2 + (centeredTop - top);
753 setPivotX(folderPivotX);
754 setPivotY(folderPivotY);
755 int folderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
756 (1.0f * folderPivotX / width));
757 int folderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
758 (1.0f * folderPivotY / height));
759 mFolderIcon.setPivotX(folderIconPivotX);
760 mFolderIcon.setPivotY(folderIconPivotY);
761
762 if (mMode == PARTIAL_GROW) {
763 lp.width = width;
764 lp.height = height;
765 lp.x = left;
766 lp.y = top;
767 } else {
768 mNewSize.set(left, top, left + width, top + height);
769 }
770 }
771
772 private void setupContentForNumItems(int count) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700773 setupContentDimension(count);
774
Adam Cohen8e776a62011-06-28 18:10:06 -0700775 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700776 if (lp == null) {
Adam Cohen8e776a62011-06-28 18:10:06 -0700777 lp = new DragLayer.LayoutParams(0, 0);
778 lp.customPosition = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700779 setLayoutParams(lp);
780 }
781 centerAboutIcon();
782 }
783
Adam Cohen234c4cd2011-07-17 21:03:04 -0700784 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
785 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
786 // Technically there is no padding at the bottom, but we add space equal to the padding
787 // and have to account for that here.
788 int height = getPaddingTop() + mContent.getDesiredHeight() + mFolderNameHeight;
789
790 int contentWidthSpec = MeasureSpec.makeMeasureSpec(mContent.getDesiredWidth(),
791 MeasureSpec.EXACTLY);
792 int contentHeightSpec = MeasureSpec.makeMeasureSpec(mContent.getDesiredHeight(),
793 MeasureSpec.EXACTLY);
794 mContent.measure(contentWidthSpec, contentHeightSpec);
795
796 mFolderName.measure(contentWidthSpec,
797 MeasureSpec.makeMeasureSpec(mFolderNameHeight, MeasureSpec.EXACTLY));
798 setMeasuredDimension(width, height);
799 }
800
Adam Cohen2801caf2011-05-13 20:57:39 -0700801 private void arrangeChildren(ArrayList<View> list) {
802 int[] vacant = new int[2];
803 if (list == null) {
804 list = getItemsInReadingOrder();
805 }
806 mContent.removeAllViews();
807
808 for (int i = 0; i < list.size(); i++) {
809 View v = list.get(i);
810 mContent.getVacantCell(vacant, 1, 1);
811 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) v.getLayoutParams();
812 lp.cellX = vacant[0];
813 lp.cellY = vacant[1];
814 ItemInfo info = (ItemInfo) v.getTag();
815 info.cellX = vacant[0];
816 info.cellY = vacant[1];
817 boolean insert = false;
818 mContent.addViewToCellLayout(v, insert ? 0 : -1, (int)info.id, lp, true);
Adam Cohen7c693212011-05-18 15:26:57 -0700819 LauncherModel.addOrMoveItemInDatabase(mLauncher, info, mInfo.id, 0,
820 info.cellX, info.cellY);
Adam Cohen2801caf2011-05-13 20:57:39 -0700821 }
Adam Cohen7c693212011-05-18 15:26:57 -0700822 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700823 }
824
Adam Cohena9cf38f2011-05-02 15:36:58 -0700825 public int getItemCount() {
826 return mContent.getChildrenLayout().getChildCount();
827 }
828
829 public View getItemAt(int index) {
830 return mContent.getChildrenLayout().getChildAt(index);
831 }
832
Adam Cohen2801caf2011-05-13 20:57:39 -0700833 private void onCloseComplete() {
834 if (mRearrangeOnClose) {
835 setupContentForNumItems(getItemCount());
836 mRearrangeOnClose = false;
837 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700838 if (getItemCount() <= 1) {
839 replaceFolderWithFinalItem();
840 }
841 }
842
843 private void replaceFolderWithFinalItem() {
844 ItemInfo finalItem = null;
845
846 if (getItemCount() == 1) {
847 finalItem = mInfo.contents.get(0);
848 }
849
850 // Remove the folder completely
Winson Chung3d503fb2011-07-13 17:25:49 -0700851 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screen);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700852 cellLayout.removeView(mFolderIcon);
853 if (mFolderIcon instanceof DropTarget) {
854 mDragController.removeDropTarget((DropTarget) mFolderIcon);
855 }
856 mLauncher.removeFolder(mInfo);
857
858 if (finalItem != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700859 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
860 mInfo.screen, mInfo.cellX, mInfo.cellY);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700861 }
Adam Cohen716b51e2011-06-30 12:09:54 -0700862 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700863
864 // Add the last remaining child to the workspace in place of the folder
865 if (finalItem != null) {
866 View child = mLauncher.createShortcut(R.layout.application, cellLayout,
867 (ShortcutInfo) finalItem);
868
Winson Chung3d503fb2011-07-13 17:25:49 -0700869 mLauncher.getWorkspace().addInScreen(child, mInfo.container, mInfo.screen, mInfo.cellX,
870 mInfo.cellY, mInfo.spanX, mInfo.spanY);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700871 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700872 }
873
Adam Cohenbfbfd262011-06-13 16:55:12 -0700874 public void onDrop(DragObject d) {
875 ShortcutInfo item;
876 if (d.dragInfo instanceof ApplicationInfo) {
877 // Came from all apps -- make a copy
878 item = ((ApplicationInfo) d.dragInfo).makeShortcut();
879 item.spanX = 1;
880 item.spanY = 1;
881 } else {
882 item = (ShortcutInfo) d.dragInfo;
883 }
884 // Dragged from self onto self
885 if (item == mCurrentDragInfo) {
886 ShortcutInfo si = (ShortcutInfo) mCurrentDragView.getTag();
887 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) mCurrentDragView.getLayoutParams();
888 si.cellX = lp.cellX = mEmptyCell[0];
889 si.cellX = lp.cellY = mEmptyCell[1];
890 mContent.addViewToCellLayout(mCurrentDragView, -1, (int)item.id, lp, true);
Adam Cohenfc53cd22011-07-20 15:45:11 -0700891 if (d.dragView.hasDrawn()) {
892 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, mCurrentDragView);
893 } else {
894 mCurrentDragView.setVisibility(VISIBLE);
895 }
Adam Cohene9166b22011-07-08 17:11:11 -0700896 mItemsInvalidated = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700897 setupContentDimension(getItemCount());
Adam Cohen716b51e2011-06-30 12:09:54 -0700898 mSuppressOnAdd = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700899 }
900 mInfo.add(item);
901 }
902
903 public void onAdd(ShortcutInfo item) {
904 mItemsInvalidated = true;
905 if (mSuppressOnAdd) return;
906 if (!findAndSetEmptyCells(item)) {
907 // The current layout is full, can we expand it?
908 setupContentForNumItems(getItemCount() + 1);
909 findAndSetEmptyCells(item);
910 }
911 createAndAddShortcut(item);
912 LauncherModel.addOrMoveItemInDatabase(
913 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
914 }
915
Adam Cohena9cf38f2011-05-02 15:36:58 -0700916 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -0700917 mItemsInvalidated = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700918 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -0700919 View v = getViewForInfo(item);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700920 mContent.removeView(v);
Adam Cohen2801caf2011-05-13 20:57:39 -0700921 if (mState == STATE_ANIMATING) {
922 mRearrangeOnClose = true;
923 } else {
924 setupContentForNumItems(getItemCount());
925 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700926 if (getItemCount() <= 1) {
927 replaceFolderWithFinalItem();
928 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700929 }
Adam Cohen7c693212011-05-18 15:26:57 -0700930
Adam Cohendf1e4e82011-06-24 15:57:39 -0700931 private View getViewForInfo(ShortcutInfo item) {
932 for (int j = 0; j < mContent.getCountY(); j++) {
933 for (int i = 0; i < mContent.getCountX(); i++) {
934 View v = mContent.getChildAt(i, j);
935 if (v.getTag() == item) {
936 return v;
937 }
938 }
939 }
940 return null;
941 }
942
Adam Cohen76078c42011-06-09 15:06:52 -0700943 public void onItemsChanged() {
944 }
Adam Cohen76fc0852011-06-17 13:26:23 -0700945 public void onTitleChanged(CharSequence title) {
946 }
Adam Cohen76078c42011-06-09 15:06:52 -0700947
Adam Cohen7c693212011-05-18 15:26:57 -0700948 public ArrayList<View> getItemsInReadingOrder() {
Adam Cohen76078c42011-06-09 15:06:52 -0700949 return getItemsInReadingOrder(true);
950 }
951
952 public ArrayList<View> getItemsInReadingOrder(boolean includeCurrentDragItem) {
Adam Cohen7c693212011-05-18 15:26:57 -0700953 if (mItemsInvalidated) {
954 mItemsInReadingOrder.clear();
955 for (int j = 0; j < mContent.getCountY(); j++) {
956 for (int i = 0; i < mContent.getCountX(); i++) {
957 View v = mContent.getChildAt(i, j);
958 if (v != null) {
Adam Cohen76078c42011-06-09 15:06:52 -0700959 ShortcutInfo info = (ShortcutInfo) v.getTag();
960 if (info != mCurrentDragInfo || includeCurrentDragItem) {
961 mItemsInReadingOrder.add(v);
962 }
Adam Cohen7c693212011-05-18 15:26:57 -0700963 }
964 }
965 }
966 mItemsInvalidated = false;
967 }
968 return mItemsInReadingOrder;
969 }
Adam Cohen8dfcba42011-07-07 16:38:18 -0700970
971 public void getLocationInDragLayer(int[] loc) {
972 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
973 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800974}