blob: f5bd7dbbbf841e6b36f37b04ce18fcb29208282d [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 Cohen3371da02011-10-25 21:38:29 -070040import android.view.accessibility.AccessibilityEvent;
41import android.view.accessibility.AccessibilityManager;
Adam Cohen2801caf2011-05-13 20:57:39 -070042import android.view.animation.AccelerateInterpolator;
43import android.view.animation.DecelerateInterpolator;
Adam Cohen76fc0852011-06-17 13:26:23 -070044import android.view.inputmethod.EditorInfo;
45import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.widget.LinearLayout;
Adam Cohendf2cc412011-04-27 16:56:57 -070047import android.widget.TextView;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048
Romain Guyedcce092010-03-04 13:03:17 -080049import com.android.launcher.R;
Adam Cohena9cf38f2011-05-02 15:36:58 -070050import com.android.launcher2.FolderInfo.FolderListener;
Romain Guyedcce092010-03-04 13:03:17 -080051
Adam Cohenc0dcf592011-06-01 15:30:43 -070052import java.util.ArrayList;
53
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054/**
55 * Represents a set of icons chosen by the user or generated by the system.
56 */
Adam Cohen8dfcba42011-07-07 16:38:18 -070057public class Folder extends LinearLayout implements DragSource, View.OnClickListener,
Adam Cohenea0818d2011-09-30 20:06:58 -070058 View.OnLongClickListener, DropTarget, FolderListener, TextView.OnEditorActionListener,
59 View.OnFocusChangeListener {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060
Adam Cohendf2cc412011-04-27 16:56:57 -070061 private static final String TAG = "Launcher.Folder";
62
Adam Cohen4eac29a2011-07-11 17:53:37 -070063 protected DragController mDragController;
64 protected Launcher mLauncher;
65 protected FolderInfo mInfo;
66
Adam Cohendf2cc412011-04-27 16:56:57 -070067 static final int STATE_NONE = -1;
68 static final int STATE_SMALL = 0;
69 static final int STATE_ANIMATING = 1;
70 static final int STATE_OPEN = 2;
71
72 private int mExpandDuration;
73 protected CellLayout mContent;
74 private final LayoutInflater mInflater;
75 private final IconCache mIconCache;
76 private int mState = STATE_NONE;
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 boolean mRearrangeOnClose = false;
80 private FolderIcon mFolderIcon;
81 private int mMaxCountX;
82 private int mMaxCountY;
Adam Cohen78dc83e2011-11-15 17:10:00 -080083 private int mMaxNumItems;
Adam Cohen7c693212011-05-18 15:26:57 -070084 private ArrayList<View> mItemsInReadingOrder = new ArrayList<View>();
Adam Cohenbadf71e2011-05-26 19:08:29 -070085 private Drawable mIconDrawable;
Adam Cohen7c693212011-05-18 15:26:57 -070086 boolean mItemsInvalidated = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -070087 private ShortcutInfo mCurrentDragInfo;
88 private View mCurrentDragView;
89 boolean mSuppressOnAdd = false;
90 private int[] mTargetCell = new int[2];
91 private int[] mPreviousTargetCell = new int[2];
92 private int[] mEmptyCell = new int[2];
93 private Alarm mReorderAlarm = new Alarm();
94 private Alarm mOnExitAlarm = new Alarm();
Adam Cohen76fc0852011-06-17 13:26:23 -070095 private int mFolderNameHeight;
Adam Cohen8e776a62011-06-28 18:10:06 -070096 private Rect mTempRect = new Rect();
Adam Cohen67bd9cc2011-07-29 14:07:04 -070097 private boolean mDragInProgress = false;
98 private boolean mDeleteFolderOnDropCompleted = false;
99 private boolean mSuppressFolderDeletion = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700100 private boolean mItemAddedBackToSelfViaIcon = false;
Adam Cohenac56cff2011-09-28 20:45:37 -0700101 FolderEditText mFolderName;
Adam Cohen228da5a2011-07-27 22:23:47 -0700102
Adam Cohen76fc0852011-06-17 13:26:23 -0700103 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 Cohen78dc83e2011-11-15 17:10:00 -0800120
121 Resources res = getResources();
122 mMaxCountX = res.getInteger(R.integer.folder_max_count_x);
123 mMaxCountY = res.getInteger(R.integer.folder_max_count_y);
124 mMaxNumItems = res.getInteger(R.integer.folder_max_num_items);
125 if (mMaxCountX < 0 || mMaxCountY < 0 || mMaxNumItems < 0) {
126 mMaxCountX = LauncherModel.getCellCountX();
127 mMaxCountY = LauncherModel.getCellCountY();
128 mMaxNumItems = mMaxCountX * mMaxCountY;
129 }
Adam Cohen76fc0852011-06-17 13:26:23 -0700130
131 mInputMethodManager = (InputMethodManager)
132 mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
133
Adam Cohen76fc0852011-06-17 13:26:23 -0700134 mExpandDuration = res.getInteger(R.integer.config_folderAnimDuration);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700135
136 if (sDefaultFolderName == null) {
137 sDefaultFolderName = res.getString(R.string.folder_name);
138 }
Adam Cohena65beee2011-06-27 21:32:23 -0700139 if (sHintText == null) {
140 sHintText = res.getString(R.string.folder_hint_text);
141 }
Adam Cohen4eac29a2011-07-11 17:53:37 -0700142 mLauncher = (Launcher) context;
Adam Cohenac56cff2011-09-28 20:45:37 -0700143 // We need this view to be focusable in touch mode so that when text editing of the folder
144 // name is complete, we have something to focus on, thus hiding the cursor and giving
145 // reliable behvior when clicking the text field (since it will always gain focus on click).
146 setFocusableInTouchMode(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147 }
148
149 @Override
150 protected void onFinishInflate() {
151 super.onFinishInflate();
Adam Cohendf2cc412011-04-27 16:56:57 -0700152 mContent = (CellLayout) findViewById(R.id.folder_content);
Adam Cohen2801caf2011-05-13 20:57:39 -0700153 mContent.setGridSize(0, 0);
Adam Cohenb970a8a2011-11-18 16:29:58 -0800154 mContent.getChildrenLayout().setMotionEventSplittingEnabled(false);
Adam Cohenac56cff2011-09-28 20:45:37 -0700155 mFolderName = (FolderEditText) findViewById(R.id.folder_name);
156 mFolderName.setFolder(this);
Adam Cohenea0818d2011-09-30 20:06:58 -0700157 mFolderName.setOnFocusChangeListener(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700158
159 // We find out how tall the text view wants to be (it is set to wrap_content), so that
160 // we can allocate the appropriate amount of space for it.
161 int measureSpec = MeasureSpec.UNSPECIFIED;
162 mFolderName.measure(measureSpec, measureSpec);
163 mFolderNameHeight = mFolderName.getMeasuredHeight();
164
165 // We disable action mode for now since it messes up the view on phones
166 mFolderName.setCustomSelectionActionModeCallback(mActionModeCallback);
Adam Cohen76fc0852011-06-17 13:26:23 -0700167 mFolderName.setOnEditorActionListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700168 mFolderName.setSelectAllOnFocus(true);
Adam Cohen7a14d0b2011-06-24 11:36:35 -0700169 mFolderName.setInputType(mFolderName.getInputType() |
170 InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700172
Adam Cohen76fc0852011-06-17 13:26:23 -0700173 private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
174 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
175 return false;
176 }
177
178 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
179 return false;
180 }
181
182 public void onDestroyActionMode(ActionMode mode) {
183 }
184
185 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
186 return false;
187 }
188 };
189
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190 public void onClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700191 Object tag = v.getTag();
192 if (tag instanceof ShortcutInfo) {
193 // refactor this code from Folder
194 ShortcutInfo item = (ShortcutInfo) tag;
195 int[] pos = new int[2];
196 v.getLocationOnScreen(pos);
197 item.intent.setSourceBounds(new Rect(pos[0], pos[1],
198 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
199 mLauncher.startActivitySafely(item.intent, item);
Adam Cohendf2cc412011-04-27 16:56:57 -0700200 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800201 }
202
203 public boolean onLongClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700204 Object tag = v.getTag();
205 if (tag instanceof ShortcutInfo) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700206 ShortcutInfo item = (ShortcutInfo) tag;
207 if (!v.isInTouchMode()) {
208 return false;
209 }
210
Winson Chung7d7541e2011-09-16 20:14:36 -0700211 mLauncher.dismissFolderCling(null);
212
Adam Cohendf2cc412011-04-27 16:56:57 -0700213 mLauncher.getWorkspace().onDragStartedWithItem(v);
Adam Cohenac8c8762011-07-13 11:15:27 -0700214 mLauncher.getWorkspace().beginDragShared(v, this);
Adam Cohenbadf71e2011-05-26 19:08:29 -0700215 mIconDrawable = ((TextView) v).getCompoundDrawables()[1];
Adam Cohen76078c42011-06-09 15:06:52 -0700216
217 mCurrentDragInfo = item;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700218 mEmptyCell[0] = item.cellX;
219 mEmptyCell[1] = item.cellY;
220 mCurrentDragView = v;
Adam Cohenfc53cd22011-07-20 15:45:11 -0700221
222 mContent.removeView(mCurrentDragView);
223 mInfo.remove(mCurrentDragInfo);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700224 mDragInProgress = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700225 mItemAddedBackToSelfViaIcon = false;
Adam Cohendf2cc412011-04-27 16:56:57 -0700226 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800227 return true;
228 }
229
Adam Cohen76fc0852011-06-17 13:26:23 -0700230 public boolean isEditingName() {
231 return mIsEditingName;
232 }
233
234 public void startEditingFolderName() {
Adam Cohena65beee2011-06-27 21:32:23 -0700235 mFolderName.setHint("");
Adam Cohen76fc0852011-06-17 13:26:23 -0700236 mIsEditingName = true;
237 }
238
239 public void dismissEditingName() {
240 mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
241 doneEditingFolderName(true);
242 }
243
244 public void doneEditingFolderName(boolean commit) {
Adam Cohena65beee2011-06-27 21:32:23 -0700245 mFolderName.setHint(sHintText);
Adam Cohen1df26a32011-08-12 16:15:23 -0700246 // Convert to a string here to ensure that no other state associated with the text field
247 // gets saved.
Adam Cohen3371da02011-10-25 21:38:29 -0700248 String newTitle = mFolderName.getText().toString();
249 mInfo.setTitle(newTitle);
Adam Cohen76fc0852011-06-17 13:26:23 -0700250 LauncherModel.updateItemInDatabase(mLauncher, mInfo);
Adam Cohenac56cff2011-09-28 20:45:37 -0700251
Adam Cohen3371da02011-10-25 21:38:29 -0700252 if (commit) {
253 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
254 String.format(mContext.getString(R.string.folder_renamed), newTitle));
255 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700256 // In order to clear the focus from the text field, we set the focus on ourself. This
257 // ensures that every time the field is clicked, focus is gained, giving reliable behavior.
258 requestFocus();
259
Adam Cohene601a432011-07-26 21:51:30 -0700260 Selection.setSelection((Spannable) mFolderName.getText(), 0, 0);
Adam Cohen76fc0852011-06-17 13:26:23 -0700261 mIsEditingName = false;
262 }
263
264 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
265 if (actionId == EditorInfo.IME_ACTION_DONE) {
266 dismissEditingName();
267 return true;
268 }
269 return false;
270 }
271
272 public View getEditTextRegion() {
273 return mFolderName;
274 }
275
Adam Cohenbadf71e2011-05-26 19:08:29 -0700276 public Drawable getDragDrawable() {
277 return mIconDrawable;
278 }
279
Adam Cohen0c872ba2011-05-05 10:34:16 -0700280 /**
281 * We need to handle touch events to prevent them from falling through to the workspace below.
282 */
283 @Override
284 public boolean onTouchEvent(MotionEvent ev) {
285 return true;
286 }
287
Joe Onorato00acb122009-08-04 16:04:30 -0400288 public void setDragController(DragController dragController) {
289 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800290 }
291
Adam Cohen2801caf2011-05-13 20:57:39 -0700292 void setFolderIcon(FolderIcon icon) {
293 mFolderIcon = icon;
294 }
295
Adam Cohen3371da02011-10-25 21:38:29 -0700296 @Override
297 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
298 // When the folder gets focus, we don't want to announce the list of items.
299 return true;
300 }
301
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800302 /**
303 * @return the FolderInfo object associated with this folder
304 */
305 FolderInfo getInfo() {
306 return mInfo;
307 }
308
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800309 void bind(FolderInfo info) {
310 mInfo = info;
Adam Cohendf2cc412011-04-27 16:56:57 -0700311 ArrayList<ShortcutInfo> children = info.contents;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700312 ArrayList<ShortcutInfo> overflow = new ArrayList<ShortcutInfo>();
Adam Cohen7c693212011-05-18 15:26:57 -0700313 setupContentForNumItems(children.size());
Adam Cohen0057bbc2011-08-12 18:30:51 -0700314 int count = 0;
Adam Cohendf2cc412011-04-27 16:56:57 -0700315 for (int i = 0; i < children.size(); i++) {
316 ShortcutInfo child = (ShortcutInfo) children.get(i);
Adam Cohenc508b2d2011-06-28 14:41:44 -0700317 if (!createAndAddShortcut(child)) {
318 overflow.add(child);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700319 } else {
320 count++;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700321 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700322 }
Adam Cohenc508b2d2011-06-28 14:41:44 -0700323
Adam Cohen0057bbc2011-08-12 18:30:51 -0700324 // We rearrange the items in case there are any empty gaps
325 setupContentForNumItems(count);
326
Adam Cohenc508b2d2011-06-28 14:41:44 -0700327 // If our folder has too many items we prune them from the list. This is an issue
328 // when upgrading from the old Folders implementation which could contain an unlimited
329 // number of items.
330 for (ShortcutInfo item: overflow) {
331 mInfo.remove(item);
332 LauncherModel.deleteItemFromDatabase(mLauncher, item);
333 }
334
Adam Cohen4dbe6d92011-05-18 17:14:15 -0700335 mItemsInvalidated = true;
Adam Cohenac56cff2011-09-28 20:45:37 -0700336 updateTextViewFocus();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700337 mInfo.addListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700338
Adam Cohenafb01ee2011-06-23 15:38:03 -0700339 if (!sDefaultFolderName.contentEquals(mInfo.title)) {
Adam Cohen4ef610f2011-06-21 22:37:36 -0700340 mFolderName.setText(mInfo.title);
341 } else {
342 mFolderName.setText("");
343 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700344 }
345
346 /**
347 * Creates a new UserFolder, inflated from R.layout.user_folder.
348 *
349 * @param context The application's context.
350 *
351 * @return A new UserFolder.
352 */
353 static Folder fromXml(Context context) {
354 return (Folder) LayoutInflater.from(context).inflate(R.layout.user_folder, null);
355 }
356
357 /**
358 * This method is intended to make the UserFolder to be visually identical in size and position
359 * to its associated FolderIcon. This allows for a seamless transition into the expanded state.
360 */
361 private void positionAndSizeAsIcon() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700362 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800363 setScaleX(0.8f);
364 setScaleY(0.8f);
365 setAlpha(0f);
Adam Cohendf2cc412011-04-27 16:56:57 -0700366 mState = STATE_SMALL;
367 }
368
369 public void animateOpen() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700370 positionAndSizeAsIcon();
371
Adam Cohen8e776a62011-06-28 18:10:06 -0700372 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen2801caf2011-05-13 20:57:39 -0700373 centerAboutIcon();
Adam Cohen662b5982011-12-13 17:45:21 -0800374 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1);
375 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
376 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
377 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohen6441de02011-12-14 14:25:32 -0800378
Adam Cohen2801caf2011-05-13 20:57:39 -0700379 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700380 @Override
381 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700382 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
383 String.format(mContext.getString(R.string.folder_opened),
384 mContent.getCountX(), mContent.getCountY()));
Adam Cohendf2cc412011-04-27 16:56:57 -0700385 mState = STATE_ANIMATING;
386 }
387 @Override
388 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700389 mState = STATE_OPEN;
Winson Chung7a74ac92011-09-20 17:43:51 -0700390
391 Cling cling = mLauncher.showFirstRunFoldersCling();
392 if (cling != null) {
393 cling.bringToFront();
394 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700395 setFocusOnFirstChild();
Adam Cohendf2cc412011-04-27 16:56:57 -0700396 }
397 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700398 oa.setDuration(mExpandDuration);
399 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700400 }
401
Adam Cohen3371da02011-10-25 21:38:29 -0700402 private void sendCustomAccessibilityEvent(int type, String text) {
403 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
404 AccessibilityEvent event = AccessibilityEvent.obtain(type);
405 onInitializeAccessibilityEvent(event);
406 event.getText().add(text);
407 AccessibilityManager.getInstance(mContext).sendAccessibilityEvent(event);
408 }
409 }
410
Adam Cohenac56cff2011-09-28 20:45:37 -0700411 private void setFocusOnFirstChild() {
412 View firstChild = mContent.getChildAt(0, 0);
413 if (firstChild != null) {
414 firstChild.requestFocus();
415 }
416 }
417
Adam Cohendf2cc412011-04-27 16:56:57 -0700418 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700419 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800420 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
421 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
422 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
423 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohendf2cc412011-04-27 16:56:57 -0700424
Adam Cohen2801caf2011-05-13 20:57:39 -0700425 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700426 @Override
427 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700428 onCloseComplete();
Adam Cohen2801caf2011-05-13 20:57:39 -0700429 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700430 }
431 @Override
432 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700433 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
434 mContext.getString(R.string.folder_closed));
Adam Cohendf2cc412011-04-27 16:56:57 -0700435 mState = STATE_ANIMATING;
436 }
437 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700438 oa.setDuration(mExpandDuration);
439 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700440 }
441
442 void notifyDataSetChanged() {
443 // recreate all the children if the data set changes under us. We may want to do this more
444 // intelligently (ie just removing the views that should no longer exist)
445 mContent.removeAllViewsInLayout();
446 bind(mInfo);
447 }
448
Adam Cohencb3382b2011-05-24 14:07:08 -0700449 public boolean acceptDrop(DragObject d) {
450 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700451 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700452 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
453 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
454 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700455 }
456
Adam Cohendf2cc412011-04-27 16:56:57 -0700457 protected boolean findAndSetEmptyCells(ShortcutInfo item) {
458 int[] emptyCell = new int[2];
459 if (mContent.findCellForSpan(emptyCell, item.spanX, item.spanY)) {
460 item.cellX = emptyCell[0];
461 item.cellY = emptyCell[1];
Adam Cohendf2cc412011-04-27 16:56:57 -0700462 return true;
463 } else {
464 return false;
465 }
466 }
467
Adam Cohenc508b2d2011-06-28 14:41:44 -0700468 protected boolean createAndAddShortcut(ShortcutInfo item) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700469 final TextView textView =
Adam Cohene87b9242011-06-29 14:01:26 -0700470 (TextView) mInflater.inflate(R.layout.application, this, false);
Adam Cohendf2cc412011-04-27 16:56:57 -0700471 textView.setCompoundDrawablesWithIntrinsicBounds(null,
472 new FastBitmapDrawable(item.getIcon(mIconCache)), null, null);
473 textView.setText(item.title);
474 textView.setTag(item);
475
476 textView.setOnClickListener(this);
477 textView.setOnLongClickListener(this);
478
Adam Cohenc508b2d2011-06-28 14:41:44 -0700479 // We need to check here to verify that the given item's location isn't already occupied
480 // by another item. If it is, we need to find the next available slot and assign
481 // it that position. This is an issue when upgrading from the old Folders implementation
482 // which could contain an unlimited number of items.
Adam Cohen0057bbc2011-08-12 18:30:51 -0700483 if (mContent.getChildAt(item.cellX, item.cellY) != null || item.cellX < 0 || item.cellY < 0
484 || item.cellX >= mContent.getCountX() || item.cellY >= mContent.getCountY()) {
Adam Cohenc508b2d2011-06-28 14:41:44 -0700485 if (!findAndSetEmptyCells(item)) {
486 return false;
487 }
488 }
489
Adam Cohendf2cc412011-04-27 16:56:57 -0700490 CellLayout.LayoutParams lp =
491 new CellLayout.LayoutParams(item.cellX, item.cellY, item.spanX, item.spanY);
492 boolean insert = false;
Adam Cohenac56cff2011-09-28 20:45:37 -0700493 textView.setOnKeyListener(new FolderKeyEventListener());
Adam Cohendf2cc412011-04-27 16:56:57 -0700494 mContent.addViewToCellLayout(textView, insert ? 0 : -1, (int)item.id, lp, true);
Adam Cohenc508b2d2011-06-28 14:41:44 -0700495 return true;
Adam Cohendf2cc412011-04-27 16:56:57 -0700496 }
497
Adam Cohencb3382b2011-05-24 14:07:08 -0700498 public void onDragEnter(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700499 mPreviousTargetCell[0] = -1;
500 mPreviousTargetCell[1] = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700501 mOnExitAlarm.cancelAlarm();
502 }
503
504 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
505 public void onAlarm(Alarm alarm) {
506 realTimeReorder(mEmptyCell, mTargetCell);
507 }
508 };
509
510 boolean readingOrderGreaterThan(int[] v1, int[] v2) {
511 if (v1[1] > v2[1] || (v1[1] == v2[1] && v1[0] > v2[0])) {
512 return true;
513 } else {
514 return false;
515 }
516 }
517
518 private void realTimeReorder(int[] empty, int[] target) {
519 boolean wrap;
520 int startX;
521 int endX;
522 int startY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700523 int delay = 0;
524 float delayAmount = 30;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700525 if (readingOrderGreaterThan(target, empty)) {
526 wrap = empty[0] >= mContent.getCountX() - 1;
527 startY = wrap ? empty[1] + 1 : empty[1];
528 for (int y = startY; y <= target[1]; y++) {
529 startX = y == empty[1] ? empty[0] + 1 : 0;
530 endX = y < target[1] ? mContent.getCountX() - 1 : target[0];
531 for (int x = startX; x <= endX; x++) {
532 View v = mContent.getChildAt(x,y);
533 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen76fc0852011-06-17 13:26:23 -0700534 REORDER_ANIMATION_DURATION, delay)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700535 empty[0] = x;
536 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700537 delay += delayAmount;
538 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700539 }
540 }
541 }
542 } else {
543 wrap = empty[0] == 0;
544 startY = wrap ? empty[1] - 1 : empty[1];
545 for (int y = startY; y >= target[1]; y--) {
546 startX = y == empty[1] ? empty[0] - 1 : mContent.getCountX() - 1;
547 endX = y > target[1] ? 0 : target[0];
548 for (int x = startX; x >= endX; x--) {
549 View v = mContent.getChildAt(x,y);
550 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen76fc0852011-06-17 13:26:23 -0700551 REORDER_ANIMATION_DURATION, delay)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700552 empty[0] = x;
553 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700554 delay += delayAmount;
555 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700556 }
557 }
558 }
559 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700560 }
561
Adam Cohencb3382b2011-05-24 14:07:08 -0700562 public void onDragOver(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700563 float[] r = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, d.dragView, null);
564 mTargetCell = mContent.findNearestArea((int) r[0], (int) r[1], 1, 1, mTargetCell);
565
566 if (mTargetCell[0] != mPreviousTargetCell[0] || mTargetCell[1] != mPreviousTargetCell[1]) {
567 mReorderAlarm.cancelAlarm();
568 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
569 mReorderAlarm.setAlarm(150);
570 mPreviousTargetCell[0] = mTargetCell[0];
571 mPreviousTargetCell[1] = mTargetCell[1];
572 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700573 }
574
Adam Cohenbfbfd262011-06-13 16:55:12 -0700575 // This is used to compute the visual center of the dragView. The idea is that
576 // the visual center represents the user's interpretation of where the item is, and hence
577 // is the appropriate point to use when determining drop location.
578 private float[] getDragViewVisualCenter(int x, int y, int xOffset, int yOffset,
579 DragView dragView, float[] recycle) {
580 float res[];
581 if (recycle == null) {
582 res = new float[2];
583 } else {
584 res = recycle;
585 }
586
587 // These represent the visual top and left of drag view if a dragRect was provided.
588 // If a dragRect was not provided, then they correspond to the actual view left and
589 // top, as the dragRect is in that case taken to be the entire dragView.
590 // R.dimen.dragViewOffsetY.
591 int left = x - xOffset;
592 int top = y - yOffset;
593
594 // In order to find the visual center, we shift by half the dragRect
595 res[0] = left + dragView.getDragRegion().width() / 2;
596 res[1] = top + dragView.getDragRegion().height() / 2;
597
598 return res;
599 }
600
601 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
602 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700603 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700604 }
605 };
606
Adam Cohen95bb8002011-07-03 23:40:28 -0700607 public void completeDragExit() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700608 mLauncher.closeFolder();
609 mCurrentDragInfo = null;
610 mCurrentDragView = null;
611 mSuppressOnAdd = false;
612 mRearrangeOnClose = true;
613 }
614
Adam Cohencb3382b2011-05-24 14:07:08 -0700615 public void onDragExit(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700616 // We only close the folder if this is a true drag exit, ie. not because a drop
617 // has occurred above the folder.
618 if (!d.dragComplete) {
619 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
620 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
621 }
622 mReorderAlarm.cancelAlarm();
Adam Cohen2801caf2011-05-13 20:57:39 -0700623 }
624
Adam Cohenc0dcf592011-06-01 15:30:43 -0700625 public void onDropCompleted(View target, DragObject d, boolean success) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700626 if (success) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700627 if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700628 replaceFolderWithFinalItem();
629 }
630 } else {
631 // The drag failed, we need to return the item to the folder
632 mFolderIcon.onDrop(d);
633
634 // We're going to trigger a "closeFolder" which may occur before this item has
635 // been added back to the folder -- this could cause the folder to be deleted
636 if (mOnExitAlarm.alarmPending()) {
637 mSuppressFolderDeletion = true;
638 }
639 }
640
641 if (target != this) {
642 if (mOnExitAlarm.alarmPending()) {
643 mOnExitAlarm.cancelAlarm();
644 completeDragExit();
645 }
646 }
647 mDeleteFolderOnDropCompleted = false;
648 mDragInProgress = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700649 mItemAddedBackToSelfViaIcon = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700650 mCurrentDragInfo = null;
651 mCurrentDragView = null;
652 mSuppressOnAdd = false;
Adam Cohen4045eb72011-10-06 11:44:26 -0700653
654 // Reordering may have occured, and we need to save the new item locations. We do this once
655 // at the end to prevent unnecessary database operations.
656 updateItemLocationsInDatabase();
657 }
658
659 private void updateItemLocationsInDatabase() {
660 ArrayList<View> list = getItemsInReadingOrder();
661 for (int i = 0; i < list.size(); i++) {
662 View v = list.get(i);
663 ItemInfo info = (ItemInfo) v.getTag();
664 LauncherModel.moveItemInDatabase(mLauncher, info, mInfo.id, 0,
665 info.cellX, info.cellY);
666 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700667 }
Adam Cohen228da5a2011-07-27 22:23:47 -0700668
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700669 public void notifyDrop() {
670 if (mDragInProgress) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700671 mItemAddedBackToSelfViaIcon = true;
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 Cohen4045eb72011-10-06 11:44:26 -0700683 private void setupContentDimensions(int count) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700684 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
Adam Cohen78dc83e2011-11-15 17:10:00 -0800695 if ((countX <= countY || countY == mMaxCountY) && countX < mMaxCountX) {
Adam Cohen7c693212011-05-18 15:26:57 -0700696 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() {
Adam Cohen78dc83e2011-11-15 17:10:00 -0800713 return getItemCount() >= mMaxNumItems;
Adam Cohen2801caf2011-05-13 20:57:39 -0700714 }
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 Cohenf4bb1cd2011-07-22 14:36:03 -0700720 int height = getPaddingTop() + getPaddingBottom() + mContent.getDesiredHeight()
721 + mFolderNameHeight;
Adam Cohen8e776a62011-06-28 18:10:06 -0700722 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700723
Adam Cohen8e776a62011-06-28 18:10:06 -0700724 parent.getDescendantRectRelativeToSelf(mFolderIcon, mTempRect);
725
726 int centerX = mTempRect.centerX();
727 int centerY = mTempRect.centerY();
Adam Cohen2801caf2011-05-13 20:57:39 -0700728 int centeredLeft = centerX - width / 2;
729 int centeredTop = centerY - height / 2;
730
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800731 int currentPage = mLauncher.getWorkspace().getCurrentPage();
732 // In case the workspace is scrolling, we need to use the final scroll to compute
733 // the folders bounds.
734 mLauncher.getWorkspace().setFinalScrollForPageChange(currentPage);
Adam Cohen35e7e642011-07-17 14:47:18 -0700735 // We first fetch the currently visible CellLayoutChildren
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800736 CellLayout currentLayout = (CellLayout) mLauncher.getWorkspace().getChildAt(currentPage);
737 CellLayoutChildren boundingLayout = currentLayout.getChildrenLayout();
Adam Cohen35e7e642011-07-17 14:47:18 -0700738 Rect bounds = new Rect();
739 parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800740 // We reset the workspaces scroll
741 mLauncher.getWorkspace().resetFinalScrollForPageChange(currentPage);
Adam Cohen2801caf2011-05-13 20:57:39 -0700742
Adam Cohen35e7e642011-07-17 14:47:18 -0700743 // We need to bound the folder to the currently visible CellLayoutChildren
744 int left = Math.min(Math.max(bounds.left, centeredLeft),
745 bounds.left + bounds.width() - width);
746 int top = Math.min(Math.max(bounds.top, centeredTop),
747 bounds.top + bounds.height() - height);
748 // If the folder doesn't fit within the bounds, center it about the desired bounds
749 if (width >= bounds.width()) {
750 left = bounds.left + (bounds.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700751 }
Adam Cohen35e7e642011-07-17 14:47:18 -0700752 if (height >= bounds.height()) {
753 top = bounds.top + (bounds.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700754 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700755
756 int folderPivotX = width / 2 + (centeredLeft - left);
757 int folderPivotY = height / 2 + (centeredTop - top);
758 setPivotX(folderPivotX);
759 setPivotY(folderPivotY);
760 int folderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
761 (1.0f * folderPivotX / width));
762 int folderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
763 (1.0f * folderPivotY / height));
764 mFolderIcon.setPivotX(folderIconPivotX);
765 mFolderIcon.setPivotY(folderIconPivotY);
766
Adam Cohen662b5982011-12-13 17:45:21 -0800767 lp.width = width;
768 lp.height = height;
769 lp.x = left;
770 lp.y = top;
Adam Cohen2801caf2011-05-13 20:57:39 -0700771 }
772
773 private void setupContentForNumItems(int count) {
Adam Cohen4045eb72011-10-06 11:44:26 -0700774 setupContentDimensions(count);
Adam Cohen2801caf2011-05-13 20:57:39 -0700775
Adam Cohen8e776a62011-06-28 18:10:06 -0700776 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700777 if (lp == null) {
Adam Cohen8e776a62011-06-28 18:10:06 -0700778 lp = new DragLayer.LayoutParams(0, 0);
779 lp.customPosition = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700780 setLayoutParams(lp);
781 }
782 centerAboutIcon();
783 }
784
Adam Cohen234c4cd2011-07-17 21:03:04 -0700785 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
786 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf4bb1cd2011-07-22 14:36:03 -0700787 int height = getPaddingTop() + getPaddingBottom() + mContent.getDesiredHeight()
788 + mFolderNameHeight;
Adam Cohen234c4cd2011-07-17 21:03:04 -0700789
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();
Adam Cohen2792a332011-09-26 21:09:47 -0700815 if (info.cellX != vacant[0] || info.cellY != vacant[1]) {
816 info.cellX = vacant[0];
817 info.cellY = vacant[1];
818 LauncherModel.addOrMoveItemInDatabase(mLauncher, info, mInfo.id, 0,
819 info.cellX, info.cellY);
820 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700821 boolean insert = false;
822 mContent.addViewToCellLayout(v, insert ? 0 : -1, (int)info.id, lp, true);
823 }
Adam Cohen7c693212011-05-18 15:26:57 -0700824 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700825 }
826
Adam Cohena9cf38f2011-05-02 15:36:58 -0700827 public int getItemCount() {
828 return mContent.getChildrenLayout().getChildCount();
829 }
830
831 public View getItemAt(int index) {
832 return mContent.getChildrenLayout().getChildAt(index);
833 }
834
Adam Cohen2801caf2011-05-13 20:57:39 -0700835 private void onCloseComplete() {
Adam Cohen05e0f402011-08-01 12:12:49 -0700836 DragLayer parent = (DragLayer) getParent();
Adam Cohen4554ee12011-08-03 16:13:21 -0700837 parent.removeView(this);
838 mDragController.removeDropTarget((DropTarget) this);
Adam Cohen05e0f402011-08-01 12:12:49 -0700839 clearFocus();
Adam Cohenac56cff2011-09-28 20:45:37 -0700840 mFolderIcon.requestFocus();
Adam Cohen05e0f402011-08-01 12:12:49 -0700841
Adam Cohen2801caf2011-05-13 20:57:39 -0700842 if (mRearrangeOnClose) {
843 setupContentForNumItems(getItemCount());
844 mRearrangeOnClose = false;
845 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700846 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700847 if (!mDragInProgress && !mSuppressFolderDeletion) {
848 replaceFolderWithFinalItem();
849 } else if (mDragInProgress) {
850 mDeleteFolderOnDropCompleted = true;
851 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700852 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700853 mSuppressFolderDeletion = false;
Adam Cohenafb01ee2011-06-23 15:38:03 -0700854 }
855
856 private void replaceFolderWithFinalItem() {
857 ItemInfo finalItem = null;
858
859 if (getItemCount() == 1) {
860 finalItem = mInfo.contents.get(0);
861 }
862
863 // Remove the folder completely
Winson Chung3d503fb2011-07-13 17:25:49 -0700864 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screen);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700865 cellLayout.removeView(mFolderIcon);
866 if (mFolderIcon instanceof DropTarget) {
867 mDragController.removeDropTarget((DropTarget) mFolderIcon);
868 }
869 mLauncher.removeFolder(mInfo);
870
871 if (finalItem != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700872 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
873 mInfo.screen, mInfo.cellX, mInfo.cellY);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700874 }
Adam Cohen716b51e2011-06-30 12:09:54 -0700875 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700876
877 // Add the last remaining child to the workspace in place of the folder
878 if (finalItem != null) {
879 View child = mLauncher.createShortcut(R.layout.application, cellLayout,
880 (ShortcutInfo) finalItem);
881
Winson Chung3d503fb2011-07-13 17:25:49 -0700882 mLauncher.getWorkspace().addInScreen(child, mInfo.container, mInfo.screen, mInfo.cellX,
883 mInfo.cellY, mInfo.spanX, mInfo.spanY);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700884 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700885 }
886
Adam Cohenac56cff2011-09-28 20:45:37 -0700887 // This method keeps track of the last item in the folder for the purposes
888 // of keyboard focus
889 private void updateTextViewFocus() {
890 View lastChild = getItemAt(getItemCount() - 1);
891 getItemAt(getItemCount() - 1);
892 if (lastChild != null) {
893 mFolderName.setNextFocusDownId(lastChild.getId());
894 mFolderName.setNextFocusRightId(lastChild.getId());
895 mFolderName.setNextFocusLeftId(lastChild.getId());
896 mFolderName.setNextFocusUpId(lastChild.getId());
897 }
898 }
899
Adam Cohenbfbfd262011-06-13 16:55:12 -0700900 public void onDrop(DragObject d) {
901 ShortcutInfo item;
902 if (d.dragInfo instanceof ApplicationInfo) {
903 // Came from all apps -- make a copy
904 item = ((ApplicationInfo) d.dragInfo).makeShortcut();
905 item.spanX = 1;
906 item.spanY = 1;
907 } else {
908 item = (ShortcutInfo) d.dragInfo;
909 }
Adam Cohen05e0f402011-08-01 12:12:49 -0700910 // Dragged from self onto self, currently this is the only path possible, however
911 // we keep this as a distinct code path.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700912 if (item == mCurrentDragInfo) {
913 ShortcutInfo si = (ShortcutInfo) mCurrentDragView.getTag();
914 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) mCurrentDragView.getLayoutParams();
915 si.cellX = lp.cellX = mEmptyCell[0];
916 si.cellX = lp.cellY = mEmptyCell[1];
917 mContent.addViewToCellLayout(mCurrentDragView, -1, (int)item.id, lp, true);
Adam Cohenfc53cd22011-07-20 15:45:11 -0700918 if (d.dragView.hasDrawn()) {
919 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, mCurrentDragView);
920 } else {
921 mCurrentDragView.setVisibility(VISIBLE);
922 }
Adam Cohene9166b22011-07-08 17:11:11 -0700923 mItemsInvalidated = true;
Adam Cohen4045eb72011-10-06 11:44:26 -0700924 setupContentDimensions(getItemCount());
Adam Cohen716b51e2011-06-30 12:09:54 -0700925 mSuppressOnAdd = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700926 }
927 mInfo.add(item);
928 }
929
930 public void onAdd(ShortcutInfo item) {
931 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700932 // If the item was dropped onto this open folder, we have done the work associated
933 // with adding the item to the folder, as indicated by mSuppressOnAdd being set
Adam Cohenbfbfd262011-06-13 16:55:12 -0700934 if (mSuppressOnAdd) return;
935 if (!findAndSetEmptyCells(item)) {
936 // The current layout is full, can we expand it?
937 setupContentForNumItems(getItemCount() + 1);
938 findAndSetEmptyCells(item);
939 }
940 createAndAddShortcut(item);
941 LauncherModel.addOrMoveItemInDatabase(
942 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
943 }
944
Adam Cohena9cf38f2011-05-02 15:36:58 -0700945 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -0700946 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700947 // If this item is being dragged from this open folder, we have already handled
948 // the work associated with removing the item, so we don't have to do anything here.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700949 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -0700950 View v = getViewForInfo(item);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700951 mContent.removeView(v);
Adam Cohen2801caf2011-05-13 20:57:39 -0700952 if (mState == STATE_ANIMATING) {
953 mRearrangeOnClose = true;
954 } else {
955 setupContentForNumItems(getItemCount());
956 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700957 if (getItemCount() <= 1) {
958 replaceFolderWithFinalItem();
959 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700960 }
Adam Cohen7c693212011-05-18 15:26:57 -0700961
Adam Cohendf1e4e82011-06-24 15:57:39 -0700962 private View getViewForInfo(ShortcutInfo item) {
963 for (int j = 0; j < mContent.getCountY(); j++) {
964 for (int i = 0; i < mContent.getCountX(); i++) {
965 View v = mContent.getChildAt(i, j);
966 if (v.getTag() == item) {
967 return v;
968 }
969 }
970 }
971 return null;
972 }
973
Adam Cohen76078c42011-06-09 15:06:52 -0700974 public void onItemsChanged() {
Adam Cohenac56cff2011-09-28 20:45:37 -0700975 updateTextViewFocus();
Adam Cohen76078c42011-06-09 15:06:52 -0700976 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700977
Adam Cohen76fc0852011-06-17 13:26:23 -0700978 public void onTitleChanged(CharSequence title) {
979 }
Adam Cohen76078c42011-06-09 15:06:52 -0700980
Adam Cohen7c693212011-05-18 15:26:57 -0700981 public ArrayList<View> getItemsInReadingOrder() {
Adam Cohen76078c42011-06-09 15:06:52 -0700982 return getItemsInReadingOrder(true);
983 }
984
985 public ArrayList<View> getItemsInReadingOrder(boolean includeCurrentDragItem) {
Adam Cohen7c693212011-05-18 15:26:57 -0700986 if (mItemsInvalidated) {
987 mItemsInReadingOrder.clear();
988 for (int j = 0; j < mContent.getCountY(); j++) {
989 for (int i = 0; i < mContent.getCountX(); i++) {
990 View v = mContent.getChildAt(i, j);
991 if (v != null) {
Adam Cohen76078c42011-06-09 15:06:52 -0700992 ShortcutInfo info = (ShortcutInfo) v.getTag();
993 if (info != mCurrentDragInfo || includeCurrentDragItem) {
994 mItemsInReadingOrder.add(v);
995 }
Adam Cohen7c693212011-05-18 15:26:57 -0700996 }
997 }
998 }
999 mItemsInvalidated = false;
1000 }
1001 return mItemsInReadingOrder;
1002 }
Adam Cohen8dfcba42011-07-07 16:38:18 -07001003
1004 public void getLocationInDragLayer(int[] loc) {
1005 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
1006 }
Adam Cohenea0818d2011-09-30 20:06:58 -07001007
1008 public void onFocusChange(View v, boolean hasFocus) {
1009 if (v == mFolderName && hasFocus) {
1010 startEditingFolderName();
1011 }
1012 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001013}