blob: 8a198f87572a9f661eeafeb19c861ddbe951c093 [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,
Adam Cohenea0818d2011-09-30 20:06:58 -070056 View.OnLongClickListener, DropTarget, FolderListener, TextView.OnEditorActionListener,
57 View.OnFocusChangeListener {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080058
Adam Cohendf2cc412011-04-27 16:56:57 -070059 private static final String TAG = "Launcher.Folder";
60
Adam Cohen4eac29a2011-07-11 17:53:37 -070061 protected DragController mDragController;
62 protected Launcher mLauncher;
63 protected FolderInfo mInfo;
64
Adam Cohendf2cc412011-04-27 16:56:57 -070065 static final int STATE_NONE = -1;
66 static final int STATE_SMALL = 0;
67 static final int STATE_ANIMATING = 1;
68 static final int STATE_OPEN = 2;
69
70 private int mExpandDuration;
71 protected CellLayout mContent;
72 private final LayoutInflater mInflater;
73 private final IconCache mIconCache;
74 private int mState = STATE_NONE;
Adam Cohen2801caf2011-05-13 20:57:39 -070075 private static final int FULL_GROW = 0;
76 private static final int PARTIAL_GROW = 1;
Adam Cohenbfbfd262011-06-13 16:55:12 -070077 private static final int REORDER_ANIMATION_DURATION = 230;
78 private static final int ON_EXIT_CLOSE_DELAY = 800;
Adam Cohen2801caf2011-05-13 20:57:39 -070079 private int mMode = PARTIAL_GROW;
80 private boolean mRearrangeOnClose = false;
81 private FolderIcon mFolderIcon;
82 private int mMaxCountX;
83 private int mMaxCountY;
84 private Rect mNewSize = new Rect();
Adam Cohen3e8f8112011-07-02 18:03:00 -070085 private Rect mIconRect = new Rect();
Adam Cohen7c693212011-05-18 15:26:57 -070086 private ArrayList<View> mItemsInReadingOrder = new ArrayList<View>();
Adam Cohenbadf71e2011-05-26 19:08:29 -070087 private Drawable mIconDrawable;
Adam Cohen7c693212011-05-18 15:26:57 -070088 boolean mItemsInvalidated = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -070089 private ShortcutInfo mCurrentDragInfo;
90 private View mCurrentDragView;
91 boolean mSuppressOnAdd = false;
92 private int[] mTargetCell = new int[2];
93 private int[] mPreviousTargetCell = new int[2];
94 private int[] mEmptyCell = new int[2];
95 private Alarm mReorderAlarm = new Alarm();
96 private Alarm mOnExitAlarm = new Alarm();
Adam Cohen76fc0852011-06-17 13:26:23 -070097 private int mFolderNameHeight;
Adam Cohen4ef610f2011-06-21 22:37:36 -070098 private Rect mHitRect = new Rect();
Adam Cohen8e776a62011-06-28 18:10:06 -070099 private Rect mTempRect = new Rect();
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700100 private boolean mDragInProgress = false;
101 private boolean mDeleteFolderOnDropCompleted = false;
102 private boolean mSuppressFolderDeletion = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700103 private boolean mItemAddedBackToSelfViaIcon = false;
Adam Cohenac56cff2011-09-28 20:45:37 -0700104 FolderEditText mFolderName;
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 mLauncher = (Launcher) context;
Adam Cohenac56cff2011-09-28 20:45:37 -0700139 // We need this view to be focusable in touch mode so that when text editing of the folder
140 // name is complete, we have something to focus on, thus hiding the cursor and giving
141 // reliable behvior when clicking the text field (since it will always gain focus on click).
142 setFocusableInTouchMode(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143 }
144
145 @Override
146 protected void onFinishInflate() {
147 super.onFinishInflate();
Adam Cohendf2cc412011-04-27 16:56:57 -0700148 mContent = (CellLayout) findViewById(R.id.folder_content);
Adam Cohen2801caf2011-05-13 20:57:39 -0700149 mContent.setGridSize(0, 0);
Adam Cohenac56cff2011-09-28 20:45:37 -0700150 mFolderName = (FolderEditText) findViewById(R.id.folder_name);
151 mFolderName.setFolder(this);
Adam Cohenea0818d2011-09-30 20:06:58 -0700152 mFolderName.setOnFocusChangeListener(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700153
154 // We find out how tall the text view wants to be (it is set to wrap_content), so that
155 // we can allocate the appropriate amount of space for it.
156 int measureSpec = MeasureSpec.UNSPECIFIED;
157 mFolderName.measure(measureSpec, measureSpec);
158 mFolderNameHeight = mFolderName.getMeasuredHeight();
159
160 // We disable action mode for now since it messes up the view on phones
161 mFolderName.setCustomSelectionActionModeCallback(mActionModeCallback);
Adam Cohen76fc0852011-06-17 13:26:23 -0700162 mFolderName.setOnEditorActionListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700163 mFolderName.setSelectAllOnFocus(true);
Adam Cohen7a14d0b2011-06-24 11:36:35 -0700164 mFolderName.setInputType(mFolderName.getInputType() |
165 InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700167
Adam Cohen76fc0852011-06-17 13:26:23 -0700168 private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
169 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
170 return false;
171 }
172
173 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
174 return false;
175 }
176
177 public void onDestroyActionMode(ActionMode mode) {
178 }
179
180 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
181 return false;
182 }
183 };
184
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185 public void onClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700186 Object tag = v.getTag();
187 if (tag instanceof ShortcutInfo) {
188 // refactor this code from Folder
189 ShortcutInfo item = (ShortcutInfo) tag;
190 int[] pos = new int[2];
191 v.getLocationOnScreen(pos);
192 item.intent.setSourceBounds(new Rect(pos[0], pos[1],
193 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
194 mLauncher.startActivitySafely(item.intent, item);
Adam Cohendf2cc412011-04-27 16:56:57 -0700195 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800196 }
197
198 public boolean onLongClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700199 Object tag = v.getTag();
200 if (tag instanceof ShortcutInfo) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700201 ShortcutInfo item = (ShortcutInfo) tag;
202 if (!v.isInTouchMode()) {
203 return false;
204 }
205
Winson Chung7d7541e2011-09-16 20:14:36 -0700206 mLauncher.dismissFolderCling(null);
207
Adam Cohendf2cc412011-04-27 16:56:57 -0700208 mLauncher.getWorkspace().onDragStartedWithItem(v);
Adam Cohenac8c8762011-07-13 11:15:27 -0700209 mLauncher.getWorkspace().beginDragShared(v, this);
Adam Cohenbadf71e2011-05-26 19:08:29 -0700210 mIconDrawable = ((TextView) v).getCompoundDrawables()[1];
Adam Cohen76078c42011-06-09 15:06:52 -0700211
212 mCurrentDragInfo = item;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700213 mEmptyCell[0] = item.cellX;
214 mEmptyCell[1] = item.cellY;
215 mCurrentDragView = v;
Adam Cohenfc53cd22011-07-20 15:45:11 -0700216
217 mContent.removeView(mCurrentDragView);
218 mInfo.remove(mCurrentDragInfo);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700219 mDragInProgress = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700220 mItemAddedBackToSelfViaIcon = false;
Adam Cohendf2cc412011-04-27 16:56:57 -0700221 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800222 return true;
223 }
224
Adam Cohen76fc0852011-06-17 13:26:23 -0700225 public boolean isEditingName() {
226 return mIsEditingName;
227 }
228
229 public void startEditingFolderName() {
Adam Cohena65beee2011-06-27 21:32:23 -0700230 mFolderName.setHint("");
Adam Cohen76fc0852011-06-17 13:26:23 -0700231 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 Cohen1df26a32011-08-12 16:15:23 -0700241 // Convert to a string here to ensure that no other state associated with the text field
242 // gets saved.
243 mInfo.setTitle(mFolderName.getText().toString());
Adam Cohen76fc0852011-06-17 13:26:23 -0700244 LauncherModel.updateItemInDatabase(mLauncher, mInfo);
Adam Cohenac56cff2011-09-28 20:45:37 -0700245
246 // In order to clear the focus from the text field, we set the focus on ourself. This
247 // ensures that every time the field is clicked, focus is gained, giving reliable behavior.
248 requestFocus();
249
Adam Cohene601a432011-07-26 21:51:30 -0700250 Selection.setSelection((Spannable) mFolderName.getText(), 0, 0);
Adam Cohen76fc0852011-06-17 13:26:23 -0700251 mIsEditingName = false;
252 }
253
254 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
255 if (actionId == EditorInfo.IME_ACTION_DONE) {
256 dismissEditingName();
257 return true;
258 }
259 return false;
260 }
261
262 public View getEditTextRegion() {
263 return mFolderName;
264 }
265
Adam Cohenbadf71e2011-05-26 19:08:29 -0700266 public Drawable getDragDrawable() {
267 return mIconDrawable;
268 }
269
Adam Cohen0c872ba2011-05-05 10:34:16 -0700270 /**
271 * We need to handle touch events to prevent them from falling through to the workspace below.
272 */
273 @Override
274 public boolean onTouchEvent(MotionEvent ev) {
275 return true;
276 }
277
Joe Onorato00acb122009-08-04 16:04:30 -0400278 public void setDragController(DragController dragController) {
279 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800280 }
281
Adam Cohen2801caf2011-05-13 20:57:39 -0700282 void setFolderIcon(FolderIcon icon) {
283 mFolderIcon = icon;
284 }
285
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800286 /**
287 * @return the FolderInfo object associated with this folder
288 */
289 FolderInfo getInfo() {
290 return mInfo;
291 }
292
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800293 void bind(FolderInfo info) {
294 mInfo = info;
Adam Cohendf2cc412011-04-27 16:56:57 -0700295 ArrayList<ShortcutInfo> children = info.contents;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700296 ArrayList<ShortcutInfo> overflow = new ArrayList<ShortcutInfo>();
Adam Cohen7c693212011-05-18 15:26:57 -0700297 setupContentForNumItems(children.size());
Adam Cohen0057bbc2011-08-12 18:30:51 -0700298 int count = 0;
Adam Cohendf2cc412011-04-27 16:56:57 -0700299 for (int i = 0; i < children.size(); i++) {
300 ShortcutInfo child = (ShortcutInfo) children.get(i);
Adam Cohenc508b2d2011-06-28 14:41:44 -0700301 if (!createAndAddShortcut(child)) {
302 overflow.add(child);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700303 } else {
304 count++;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700305 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700306 }
Adam Cohenc508b2d2011-06-28 14:41:44 -0700307
Adam Cohen0057bbc2011-08-12 18:30:51 -0700308 // We rearrange the items in case there are any empty gaps
309 setupContentForNumItems(count);
310
Adam Cohenc508b2d2011-06-28 14:41:44 -0700311 // If our folder has too many items we prune them from the list. This is an issue
312 // when upgrading from the old Folders implementation which could contain an unlimited
313 // number of items.
314 for (ShortcutInfo item: overflow) {
315 mInfo.remove(item);
316 LauncherModel.deleteItemFromDatabase(mLauncher, item);
317 }
318
Adam Cohen4dbe6d92011-05-18 17:14:15 -0700319 mItemsInvalidated = true;
Adam Cohenac56cff2011-09-28 20:45:37 -0700320 updateTextViewFocus();
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 Cohen3e8f8112011-07-02 18:03:00 -0700366 positionAndSizeAsIcon();
367
Adam Cohen8e776a62011-06-28 18:10:06 -0700368 if (!(getParent() instanceof DragLayer)) return;
Adam Cohendf2cc412011-04-27 16:56:57 -0700369
Adam Cohen2801caf2011-05-13 20:57:39 -0700370 ObjectAnimator oa;
Adam Cohen8e776a62011-06-28 18:10:06 -0700371 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohendf2cc412011-04-27 16:56:57 -0700372
Adam Cohen2801caf2011-05-13 20:57:39 -0700373 centerAboutIcon();
374 if (mMode == PARTIAL_GROW) {
375 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1);
376 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
377 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
378 oa = ObjectAnimator.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
379 } else {
380 PropertyValuesHolder width = PropertyValuesHolder.ofInt("width", mNewSize.width());
381 PropertyValuesHolder height = PropertyValuesHolder.ofInt("height", mNewSize.height());
382 PropertyValuesHolder x = PropertyValuesHolder.ofInt("x", mNewSize.left);
383 PropertyValuesHolder y = PropertyValuesHolder.ofInt("y", mNewSize.top);
384 oa = ObjectAnimator.ofPropertyValuesHolder(lp, width, height, x, y);
385 oa.addUpdateListener(new AnimatorUpdateListener() {
386 public void onAnimationUpdate(ValueAnimator animation) {
387 requestLayout();
388 }
389 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700390
Adam Cohen2801caf2011-05-13 20:57:39 -0700391 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
392 ObjectAnimator alphaOa = ObjectAnimator.ofPropertyValuesHolder(mContent, alpha);
393 alphaOa.setDuration(mExpandDuration);
394 alphaOa.setInterpolator(new AccelerateInterpolator(2.0f));
395 alphaOa.start();
396 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700397
Adam Cohen2801caf2011-05-13 20:57:39 -0700398 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700399 @Override
400 public void onAnimationStart(Animator animation) {
401 mState = STATE_ANIMATING;
402 }
403 @Override
404 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700405 mState = STATE_OPEN;
Winson Chung7a74ac92011-09-20 17:43:51 -0700406
407 Cling cling = mLauncher.showFirstRunFoldersCling();
408 if (cling != null) {
409 cling.bringToFront();
410 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700411 setFocusOnFirstChild();
Adam Cohendf2cc412011-04-27 16:56:57 -0700412 }
413 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700414 oa.setDuration(mExpandDuration);
415 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700416 }
417
Adam Cohenac56cff2011-09-28 20:45:37 -0700418 private void setFocusOnFirstChild() {
419 View firstChild = mContent.getChildAt(0, 0);
420 if (firstChild != null) {
421 firstChild.requestFocus();
422 }
423 }
424
Adam Cohendf2cc412011-04-27 16:56:57 -0700425 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700426 if (!(getParent() instanceof DragLayer)) return;
Adam Cohendf2cc412011-04-27 16:56:57 -0700427
Adam Cohen2801caf2011-05-13 20:57:39 -0700428 ObjectAnimator oa;
Adam Cohen2801caf2011-05-13 20:57:39 -0700429 if (mMode == PARTIAL_GROW) {
430 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
431 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
432 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
433 oa = ObjectAnimator.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
434 } else {
Adam Cohen8e776a62011-06-28 18:10:06 -0700435 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohendf2cc412011-04-27 16:56:57 -0700436
Adam Cohen3e8f8112011-07-02 18:03:00 -0700437 PropertyValuesHolder width = PropertyValuesHolder.ofInt("width", mIconRect.width());
438 PropertyValuesHolder height = PropertyValuesHolder.ofInt("height", mIconRect.height());
439 PropertyValuesHolder x = PropertyValuesHolder.ofInt("x", mIconRect.left);
440 PropertyValuesHolder y = PropertyValuesHolder.ofInt("y", mIconRect.top);
Adam Cohen2801caf2011-05-13 20:57:39 -0700441 oa = ObjectAnimator.ofPropertyValuesHolder(lp, width, height, x, y);
442 oa.addUpdateListener(new AnimatorUpdateListener() {
443 public void onAnimationUpdate(ValueAnimator animation) {
444 requestLayout();
445 }
446 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700447
Adam Cohen2801caf2011-05-13 20:57:39 -0700448 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0f);
449 ObjectAnimator alphaOa = ObjectAnimator.ofPropertyValuesHolder(mContent, alpha);
450 alphaOa.setDuration(mExpandDuration);
451 alphaOa.setInterpolator(new DecelerateInterpolator(2.0f));
452 alphaOa.start();
453 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700454
Adam Cohen2801caf2011-05-13 20:57:39 -0700455 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700456 @Override
457 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700458 onCloseComplete();
Adam Cohen2801caf2011-05-13 20:57:39 -0700459 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700460 }
461 @Override
462 public void onAnimationStart(Animator animation) {
463 mState = STATE_ANIMATING;
464 }
465 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700466 oa.setDuration(mExpandDuration);
467 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700468 }
469
470 void notifyDataSetChanged() {
471 // recreate all the children if the data set changes under us. We may want to do this more
472 // intelligently (ie just removing the views that should no longer exist)
473 mContent.removeAllViewsInLayout();
474 bind(mInfo);
475 }
476
Adam Cohencb3382b2011-05-24 14:07:08 -0700477 public boolean acceptDrop(DragObject d) {
478 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700479 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700480 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
481 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
482 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700483 }
484
Adam Cohendf2cc412011-04-27 16:56:57 -0700485 protected boolean findAndSetEmptyCells(ShortcutInfo item) {
486 int[] emptyCell = new int[2];
487 if (mContent.findCellForSpan(emptyCell, item.spanX, item.spanY)) {
488 item.cellX = emptyCell[0];
489 item.cellY = emptyCell[1];
Adam Cohendf2cc412011-04-27 16:56:57 -0700490 return true;
491 } else {
492 return false;
493 }
494 }
495
Adam Cohenc508b2d2011-06-28 14:41:44 -0700496 protected boolean createAndAddShortcut(ShortcutInfo item) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700497 final TextView textView =
Adam Cohene87b9242011-06-29 14:01:26 -0700498 (TextView) mInflater.inflate(R.layout.application, this, false);
Adam Cohendf2cc412011-04-27 16:56:57 -0700499 textView.setCompoundDrawablesWithIntrinsicBounds(null,
500 new FastBitmapDrawable(item.getIcon(mIconCache)), null, null);
501 textView.setText(item.title);
502 textView.setTag(item);
503
504 textView.setOnClickListener(this);
505 textView.setOnLongClickListener(this);
506
Adam Cohenc508b2d2011-06-28 14:41:44 -0700507 // We need to check here to verify that the given item's location isn't already occupied
508 // by another item. If it is, we need to find the next available slot and assign
509 // it that position. This is an issue when upgrading from the old Folders implementation
510 // which could contain an unlimited number of items.
Adam Cohen0057bbc2011-08-12 18:30:51 -0700511 if (mContent.getChildAt(item.cellX, item.cellY) != null || item.cellX < 0 || item.cellY < 0
512 || item.cellX >= mContent.getCountX() || item.cellY >= mContent.getCountY()) {
Adam Cohenc508b2d2011-06-28 14:41:44 -0700513 if (!findAndSetEmptyCells(item)) {
514 return false;
515 }
516 }
517
Adam Cohendf2cc412011-04-27 16:56:57 -0700518 CellLayout.LayoutParams lp =
519 new CellLayout.LayoutParams(item.cellX, item.cellY, item.spanX, item.spanY);
520 boolean insert = false;
Adam Cohenac56cff2011-09-28 20:45:37 -0700521 textView.setOnKeyListener(new FolderKeyEventListener());
Adam Cohendf2cc412011-04-27 16:56:57 -0700522 mContent.addViewToCellLayout(textView, insert ? 0 : -1, (int)item.id, lp, true);
Adam Cohenc508b2d2011-06-28 14:41:44 -0700523 return true;
Adam Cohendf2cc412011-04-27 16:56:57 -0700524 }
525
Adam Cohencb3382b2011-05-24 14:07:08 -0700526 public void onDragEnter(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700527 mPreviousTargetCell[0] = -1;
528 mPreviousTargetCell[1] = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700529 mOnExitAlarm.cancelAlarm();
530 }
531
532 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
533 public void onAlarm(Alarm alarm) {
534 realTimeReorder(mEmptyCell, mTargetCell);
535 }
536 };
537
538 boolean readingOrderGreaterThan(int[] v1, int[] v2) {
539 if (v1[1] > v2[1] || (v1[1] == v2[1] && v1[0] > v2[0])) {
540 return true;
541 } else {
542 return false;
543 }
544 }
545
546 private void realTimeReorder(int[] empty, int[] target) {
547 boolean wrap;
548 int startX;
549 int endX;
550 int startY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700551 int delay = 0;
552 float delayAmount = 30;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700553 if (readingOrderGreaterThan(target, empty)) {
554 wrap = empty[0] >= mContent.getCountX() - 1;
555 startY = wrap ? empty[1] + 1 : empty[1];
556 for (int y = startY; y <= target[1]; y++) {
557 startX = y == empty[1] ? empty[0] + 1 : 0;
558 endX = y < target[1] ? mContent.getCountX() - 1 : target[0];
559 for (int x = startX; x <= endX; x++) {
560 View v = mContent.getChildAt(x,y);
561 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen76fc0852011-06-17 13:26:23 -0700562 REORDER_ANIMATION_DURATION, delay)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700563 empty[0] = x;
564 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700565 delay += delayAmount;
566 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700567 }
568 }
569 }
570 } else {
571 wrap = empty[0] == 0;
572 startY = wrap ? empty[1] - 1 : empty[1];
573 for (int y = startY; y >= target[1]; y--) {
574 startX = y == empty[1] ? empty[0] - 1 : mContent.getCountX() - 1;
575 endX = y > target[1] ? 0 : target[0];
576 for (int x = startX; x >= endX; x--) {
577 View v = mContent.getChildAt(x,y);
578 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen76fc0852011-06-17 13:26:23 -0700579 REORDER_ANIMATION_DURATION, delay)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700580 empty[0] = x;
581 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700582 delay += delayAmount;
583 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700584 }
585 }
586 }
587 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700588 }
589
Adam Cohencb3382b2011-05-24 14:07:08 -0700590 public void onDragOver(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700591 float[] r = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, d.dragView, null);
592 mTargetCell = mContent.findNearestArea((int) r[0], (int) r[1], 1, 1, mTargetCell);
593
594 if (mTargetCell[0] != mPreviousTargetCell[0] || mTargetCell[1] != mPreviousTargetCell[1]) {
595 mReorderAlarm.cancelAlarm();
596 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
597 mReorderAlarm.setAlarm(150);
598 mPreviousTargetCell[0] = mTargetCell[0];
599 mPreviousTargetCell[1] = mTargetCell[1];
600 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700601 }
602
Adam Cohenbfbfd262011-06-13 16:55:12 -0700603 // This is used to compute the visual center of the dragView. The idea is that
604 // the visual center represents the user's interpretation of where the item is, and hence
605 // is the appropriate point to use when determining drop location.
606 private float[] getDragViewVisualCenter(int x, int y, int xOffset, int yOffset,
607 DragView dragView, float[] recycle) {
608 float res[];
609 if (recycle == null) {
610 res = new float[2];
611 } else {
612 res = recycle;
613 }
614
615 // These represent the visual top and left of drag view if a dragRect was provided.
616 // If a dragRect was not provided, then they correspond to the actual view left and
617 // top, as the dragRect is in that case taken to be the entire dragView.
618 // R.dimen.dragViewOffsetY.
619 int left = x - xOffset;
620 int top = y - yOffset;
621
622 // In order to find the visual center, we shift by half the dragRect
623 res[0] = left + dragView.getDragRegion().width() / 2;
624 res[1] = top + dragView.getDragRegion().height() / 2;
625
626 return res;
627 }
628
629 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
630 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700631 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700632 }
633 };
634
Adam Cohen95bb8002011-07-03 23:40:28 -0700635 public void completeDragExit() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700636 mLauncher.closeFolder();
637 mCurrentDragInfo = null;
638 mCurrentDragView = null;
639 mSuppressOnAdd = false;
640 mRearrangeOnClose = true;
641 }
642
Adam Cohencb3382b2011-05-24 14:07:08 -0700643 public void onDragExit(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700644 // We only close the folder if this is a true drag exit, ie. not because a drop
645 // has occurred above the folder.
646 if (!d.dragComplete) {
647 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
648 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
649 }
650 mReorderAlarm.cancelAlarm();
Adam Cohen2801caf2011-05-13 20:57:39 -0700651 }
652
Adam Cohenc0dcf592011-06-01 15:30:43 -0700653 public void onDropCompleted(View target, DragObject d, boolean success) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700654 if (success) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700655 if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700656 replaceFolderWithFinalItem();
657 }
658 } else {
659 // The drag failed, we need to return the item to the folder
660 mFolderIcon.onDrop(d);
661
662 // We're going to trigger a "closeFolder" which may occur before this item has
663 // been added back to the folder -- this could cause the folder to be deleted
664 if (mOnExitAlarm.alarmPending()) {
665 mSuppressFolderDeletion = true;
666 }
667 }
668
669 if (target != this) {
670 if (mOnExitAlarm.alarmPending()) {
671 mOnExitAlarm.cancelAlarm();
672 completeDragExit();
673 }
674 }
675 mDeleteFolderOnDropCompleted = false;
676 mDragInProgress = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700677 mItemAddedBackToSelfViaIcon = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700678 mCurrentDragInfo = null;
679 mCurrentDragView = null;
680 mSuppressOnAdd = false;
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700681 }
Adam Cohen228da5a2011-07-27 22:23:47 -0700682
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700683 public void notifyDrop() {
684 if (mDragInProgress) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700685 mItemAddedBackToSelfViaIcon = true;
Adam Cohen76078c42011-06-09 15:06:52 -0700686 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700687 }
688
689 public boolean isDropEnabled() {
690 return true;
691 }
692
Adam Cohencb3382b2011-05-24 14:07:08 -0700693 public DropTarget getDropTargetDelegate(DragObject d) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700694 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800695 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700696
Adam Cohen2801caf2011-05-13 20:57:39 -0700697 private void setupContentDimension(int count) {
698 ArrayList<View> list = getItemsInReadingOrder();
699
700 int countX = mContent.getCountX();
701 int countY = mContent.getCountY();
Adam Cohen7c693212011-05-18 15:26:57 -0700702 boolean done = false;
Adam Cohen2801caf2011-05-13 20:57:39 -0700703
Adam Cohen7c693212011-05-18 15:26:57 -0700704 while (!done) {
705 int oldCountX = countX;
706 int oldCountY = countY;
707 if (countX * countY < count) {
708 // Current grid is too small, expand it
709 if (countX <= countY && countX < mMaxCountX) {
710 countX++;
711 } else if (countY < mMaxCountY) {
712 countY++;
713 }
714 if (countY == 0) countY++;
715 } else if ((countY - 1) * countX >= count && countY >= countX) {
716 countY = Math.max(0, countY - 1);
717 } else if ((countX - 1) * countY >= count) {
718 countX = Math.max(0, countX - 1);
Adam Cohen2801caf2011-05-13 20:57:39 -0700719 }
Adam Cohen7c693212011-05-18 15:26:57 -0700720 done = countX == oldCountX && countY == oldCountY;
Adam Cohen2801caf2011-05-13 20:57:39 -0700721 }
Adam Cohen7c693212011-05-18 15:26:57 -0700722 mContent.setGridSize(countX, countY);
Adam Cohen2801caf2011-05-13 20:57:39 -0700723 arrangeChildren(list);
724 }
725
726 public boolean isFull() {
727 return getItemCount() >= mMaxCountX * mMaxCountY;
728 }
729
730 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700731 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700732
733 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf4bb1cd2011-07-22 14:36:03 -0700734 int height = getPaddingTop() + getPaddingBottom() + mContent.getDesiredHeight()
735 + mFolderNameHeight;
Adam Cohen8e776a62011-06-28 18:10:06 -0700736 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700737
Adam Cohen8e776a62011-06-28 18:10:06 -0700738 parent.getDescendantRectRelativeToSelf(mFolderIcon, mTempRect);
739
740 int centerX = mTempRect.centerX();
741 int centerY = mTempRect.centerY();
Adam Cohen2801caf2011-05-13 20:57:39 -0700742 int centeredLeft = centerX - width / 2;
743 int centeredTop = centerY - height / 2;
744
Adam Cohen35e7e642011-07-17 14:47:18 -0700745 // We first fetch the currently visible CellLayoutChildren
Winson Chung3d503fb2011-07-13 17:25:49 -0700746 CellLayout currentPage = mLauncher.getWorkspace().getCurrentDropLayout();
Adam Cohen35e7e642011-07-17 14:47:18 -0700747 CellLayoutChildren boundingLayout = currentPage.getChildrenLayout();
748 Rect bounds = new Rect();
749 parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
Adam Cohen2801caf2011-05-13 20:57:39 -0700750
Adam Cohen35e7e642011-07-17 14:47:18 -0700751 // We need to bound the folder to the currently visible CellLayoutChildren
752 int left = Math.min(Math.max(bounds.left, centeredLeft),
753 bounds.left + bounds.width() - width);
754 int top = Math.min(Math.max(bounds.top, centeredTop),
755 bounds.top + bounds.height() - height);
756 // If the folder doesn't fit within the bounds, center it about the desired bounds
757 if (width >= bounds.width()) {
758 left = bounds.left + (bounds.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700759 }
Adam Cohen35e7e642011-07-17 14:47:18 -0700760 if (height >= bounds.height()) {
761 top = bounds.top + (bounds.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700762 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700763
764 int folderPivotX = width / 2 + (centeredLeft - left);
765 int folderPivotY = height / 2 + (centeredTop - top);
766 setPivotX(folderPivotX);
767 setPivotY(folderPivotY);
768 int folderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
769 (1.0f * folderPivotX / width));
770 int folderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
771 (1.0f * folderPivotY / height));
772 mFolderIcon.setPivotX(folderIconPivotX);
773 mFolderIcon.setPivotY(folderIconPivotY);
774
775 if (mMode == PARTIAL_GROW) {
776 lp.width = width;
777 lp.height = height;
778 lp.x = left;
779 lp.y = top;
780 } else {
781 mNewSize.set(left, top, left + width, top + height);
782 }
783 }
784
785 private void setupContentForNumItems(int count) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700786 setupContentDimension(count);
787
Adam Cohen8e776a62011-06-28 18:10:06 -0700788 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700789 if (lp == null) {
Adam Cohen8e776a62011-06-28 18:10:06 -0700790 lp = new DragLayer.LayoutParams(0, 0);
791 lp.customPosition = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700792 setLayoutParams(lp);
793 }
794 centerAboutIcon();
795 }
796
Adam Cohen234c4cd2011-07-17 21:03:04 -0700797 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
798 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf4bb1cd2011-07-22 14:36:03 -0700799 int height = getPaddingTop() + getPaddingBottom() + mContent.getDesiredHeight()
800 + mFolderNameHeight;
Adam Cohen234c4cd2011-07-17 21:03:04 -0700801
802 int contentWidthSpec = MeasureSpec.makeMeasureSpec(mContent.getDesiredWidth(),
803 MeasureSpec.EXACTLY);
804 int contentHeightSpec = MeasureSpec.makeMeasureSpec(mContent.getDesiredHeight(),
805 MeasureSpec.EXACTLY);
806 mContent.measure(contentWidthSpec, contentHeightSpec);
807
808 mFolderName.measure(contentWidthSpec,
809 MeasureSpec.makeMeasureSpec(mFolderNameHeight, MeasureSpec.EXACTLY));
810 setMeasuredDimension(width, height);
811 }
812
Adam Cohen2801caf2011-05-13 20:57:39 -0700813 private void arrangeChildren(ArrayList<View> list) {
814 int[] vacant = new int[2];
815 if (list == null) {
816 list = getItemsInReadingOrder();
817 }
818 mContent.removeAllViews();
819
820 for (int i = 0; i < list.size(); i++) {
821 View v = list.get(i);
822 mContent.getVacantCell(vacant, 1, 1);
823 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) v.getLayoutParams();
824 lp.cellX = vacant[0];
825 lp.cellY = vacant[1];
826 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohen2792a332011-09-26 21:09:47 -0700827 if (info.cellX != vacant[0] || info.cellY != vacant[1]) {
828 info.cellX = vacant[0];
829 info.cellY = vacant[1];
830 LauncherModel.addOrMoveItemInDatabase(mLauncher, info, mInfo.id, 0,
831 info.cellX, info.cellY);
832 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700833 boolean insert = false;
834 mContent.addViewToCellLayout(v, insert ? 0 : -1, (int)info.id, lp, true);
835 }
Adam Cohen7c693212011-05-18 15:26:57 -0700836 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700837 }
838
Adam Cohena9cf38f2011-05-02 15:36:58 -0700839 public int getItemCount() {
840 return mContent.getChildrenLayout().getChildCount();
841 }
842
843 public View getItemAt(int index) {
844 return mContent.getChildrenLayout().getChildAt(index);
845 }
846
Adam Cohen2801caf2011-05-13 20:57:39 -0700847 private void onCloseComplete() {
Adam Cohen05e0f402011-08-01 12:12:49 -0700848 DragLayer parent = (DragLayer) getParent();
Adam Cohen4554ee12011-08-03 16:13:21 -0700849 parent.removeView(this);
850 mDragController.removeDropTarget((DropTarget) this);
Adam Cohen05e0f402011-08-01 12:12:49 -0700851 clearFocus();
Adam Cohenac56cff2011-09-28 20:45:37 -0700852 mFolderIcon.requestFocus();
Adam Cohen05e0f402011-08-01 12:12:49 -0700853
Adam Cohen2801caf2011-05-13 20:57:39 -0700854 if (mRearrangeOnClose) {
855 setupContentForNumItems(getItemCount());
856 mRearrangeOnClose = false;
857 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700858 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700859 if (!mDragInProgress && !mSuppressFolderDeletion) {
860 replaceFolderWithFinalItem();
861 } else if (mDragInProgress) {
862 mDeleteFolderOnDropCompleted = true;
863 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700864 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700865 mSuppressFolderDeletion = false;
Adam Cohenafb01ee2011-06-23 15:38:03 -0700866 }
867
868 private void replaceFolderWithFinalItem() {
869 ItemInfo finalItem = null;
870
871 if (getItemCount() == 1) {
872 finalItem = mInfo.contents.get(0);
873 }
874
875 // Remove the folder completely
Winson Chung3d503fb2011-07-13 17:25:49 -0700876 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screen);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700877 cellLayout.removeView(mFolderIcon);
878 if (mFolderIcon instanceof DropTarget) {
879 mDragController.removeDropTarget((DropTarget) mFolderIcon);
880 }
881 mLauncher.removeFolder(mInfo);
882
883 if (finalItem != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700884 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
885 mInfo.screen, mInfo.cellX, mInfo.cellY);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700886 }
Adam Cohen716b51e2011-06-30 12:09:54 -0700887 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700888
889 // Add the last remaining child to the workspace in place of the folder
890 if (finalItem != null) {
891 View child = mLauncher.createShortcut(R.layout.application, cellLayout,
892 (ShortcutInfo) finalItem);
893
Winson Chung3d503fb2011-07-13 17:25:49 -0700894 mLauncher.getWorkspace().addInScreen(child, mInfo.container, mInfo.screen, mInfo.cellX,
895 mInfo.cellY, mInfo.spanX, mInfo.spanY);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700896 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700897 }
898
Adam Cohenac56cff2011-09-28 20:45:37 -0700899 // This method keeps track of the last item in the folder for the purposes
900 // of keyboard focus
901 private void updateTextViewFocus() {
902 View lastChild = getItemAt(getItemCount() - 1);
903 getItemAt(getItemCount() - 1);
904 if (lastChild != null) {
905 mFolderName.setNextFocusDownId(lastChild.getId());
906 mFolderName.setNextFocusRightId(lastChild.getId());
907 mFolderName.setNextFocusLeftId(lastChild.getId());
908 mFolderName.setNextFocusUpId(lastChild.getId());
909 }
910 }
911
Adam Cohenbfbfd262011-06-13 16:55:12 -0700912 public void onDrop(DragObject d) {
913 ShortcutInfo item;
914 if (d.dragInfo instanceof ApplicationInfo) {
915 // Came from all apps -- make a copy
916 item = ((ApplicationInfo) d.dragInfo).makeShortcut();
917 item.spanX = 1;
918 item.spanY = 1;
919 } else {
920 item = (ShortcutInfo) d.dragInfo;
921 }
Adam Cohen05e0f402011-08-01 12:12:49 -0700922 // Dragged from self onto self, currently this is the only path possible, however
923 // we keep this as a distinct code path.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700924 if (item == mCurrentDragInfo) {
925 ShortcutInfo si = (ShortcutInfo) mCurrentDragView.getTag();
926 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) mCurrentDragView.getLayoutParams();
927 si.cellX = lp.cellX = mEmptyCell[0];
928 si.cellX = lp.cellY = mEmptyCell[1];
929 mContent.addViewToCellLayout(mCurrentDragView, -1, (int)item.id, lp, true);
Adam Cohenfc53cd22011-07-20 15:45:11 -0700930 if (d.dragView.hasDrawn()) {
931 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, mCurrentDragView);
932 } else {
933 mCurrentDragView.setVisibility(VISIBLE);
934 }
Adam Cohene9166b22011-07-08 17:11:11 -0700935 mItemsInvalidated = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700936 setupContentDimension(getItemCount());
Adam Cohen716b51e2011-06-30 12:09:54 -0700937 mSuppressOnAdd = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700938 }
939 mInfo.add(item);
940 }
941
942 public void onAdd(ShortcutInfo item) {
943 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700944 // If the item was dropped onto this open folder, we have done the work associated
945 // with adding the item to the folder, as indicated by mSuppressOnAdd being set
Adam Cohenbfbfd262011-06-13 16:55:12 -0700946 if (mSuppressOnAdd) return;
947 if (!findAndSetEmptyCells(item)) {
948 // The current layout is full, can we expand it?
949 setupContentForNumItems(getItemCount() + 1);
950 findAndSetEmptyCells(item);
951 }
952 createAndAddShortcut(item);
953 LauncherModel.addOrMoveItemInDatabase(
954 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
955 }
956
Adam Cohena9cf38f2011-05-02 15:36:58 -0700957 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -0700958 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700959 // If this item is being dragged from this open folder, we have already handled
960 // the work associated with removing the item, so we don't have to do anything here.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700961 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -0700962 View v = getViewForInfo(item);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700963 mContent.removeView(v);
Adam Cohen2801caf2011-05-13 20:57:39 -0700964 if (mState == STATE_ANIMATING) {
965 mRearrangeOnClose = true;
966 } else {
967 setupContentForNumItems(getItemCount());
968 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700969 if (getItemCount() <= 1) {
970 replaceFolderWithFinalItem();
971 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700972 }
Adam Cohen7c693212011-05-18 15:26:57 -0700973
Adam Cohendf1e4e82011-06-24 15:57:39 -0700974 private View getViewForInfo(ShortcutInfo item) {
975 for (int j = 0; j < mContent.getCountY(); j++) {
976 for (int i = 0; i < mContent.getCountX(); i++) {
977 View v = mContent.getChildAt(i, j);
978 if (v.getTag() == item) {
979 return v;
980 }
981 }
982 }
983 return null;
984 }
985
Adam Cohen76078c42011-06-09 15:06:52 -0700986 public void onItemsChanged() {
Adam Cohenac56cff2011-09-28 20:45:37 -0700987 updateTextViewFocus();
Adam Cohen76078c42011-06-09 15:06:52 -0700988 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700989
Adam Cohen76fc0852011-06-17 13:26:23 -0700990 public void onTitleChanged(CharSequence title) {
991 }
Adam Cohen76078c42011-06-09 15:06:52 -0700992
Adam Cohen7c693212011-05-18 15:26:57 -0700993 public ArrayList<View> getItemsInReadingOrder() {
Adam Cohen76078c42011-06-09 15:06:52 -0700994 return getItemsInReadingOrder(true);
995 }
996
997 public ArrayList<View> getItemsInReadingOrder(boolean includeCurrentDragItem) {
Adam Cohen7c693212011-05-18 15:26:57 -0700998 if (mItemsInvalidated) {
999 mItemsInReadingOrder.clear();
1000 for (int j = 0; j < mContent.getCountY(); j++) {
1001 for (int i = 0; i < mContent.getCountX(); i++) {
1002 View v = mContent.getChildAt(i, j);
1003 if (v != null) {
Adam Cohen76078c42011-06-09 15:06:52 -07001004 ShortcutInfo info = (ShortcutInfo) v.getTag();
1005 if (info != mCurrentDragInfo || includeCurrentDragItem) {
1006 mItemsInReadingOrder.add(v);
1007 }
Adam Cohen7c693212011-05-18 15:26:57 -07001008 }
1009 }
1010 }
1011 mItemsInvalidated = false;
1012 }
1013 return mItemsInReadingOrder;
1014 }
Adam Cohen8dfcba42011-07-07 16:38:18 -07001015
1016 public void getLocationInDragLayer(int[] loc) {
1017 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
1018 }
Adam Cohenea0818d2011-09-30 20:06:58 -07001019
1020 public void onFocusChange(View v, boolean hasFocus) {
1021 if (v == mFolderName && hasFocus) {
1022 startEditingFolderName();
1023 }
1024 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001025}