blob: e8be3eff2e7669b03acdd5d7fd3890193390c8cb [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;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080023import android.content.Context;
Adam Cohen76fc0852011-06-17 13:26:23 -070024import android.content.res.Resources;
Winson Chung043f2af2012-03-01 16:09:54 -080025import android.graphics.PointF;
Romain Guyfb5411e2010-02-24 10:04:17 -080026import android.graphics.Rect;
Adam Cohenbadf71e2011-05-26 19:08:29 -070027import android.graphics.drawable.Drawable;
Adam Cohen7a14d0b2011-06-24 11:36:35 -070028import android.text.InputType;
Adam Cohene601a432011-07-26 21:51:30 -070029import android.text.Selection;
30import android.text.Spannable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.util.AttributeSet;
Adam Cohen3bf84d32012-05-07 20:17:14 -070032import android.util.Log;
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 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;
Adam Cohen3bf84d32012-05-07 20:17:14 -070051import java.util.Collections;
52import java.util.Comparator;
Adam Cohenc0dcf592011-06-01 15:30:43 -070053
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 {
Adam Cohendf2cc412011-04-27 16:56:57 -070060 private static final String TAG = "Launcher.Folder";
61
Adam Cohen4eac29a2011-07-11 17:53:37 -070062 protected DragController mDragController;
63 protected Launcher mLauncher;
64 protected FolderInfo mInfo;
65
Adam Cohendf2cc412011-04-27 16:56:57 -070066 static final int STATE_NONE = -1;
67 static final int STATE_SMALL = 0;
68 static final int STATE_ANIMATING = 1;
69 static final int STATE_OPEN = 2;
70
71 private int mExpandDuration;
72 protected CellLayout mContent;
73 private final LayoutInflater mInflater;
74 private final IconCache mIconCache;
75 private int mState = STATE_NONE;
Adam Cohenbfbfd262011-06-13 16:55:12 -070076 private static final int REORDER_ANIMATION_DURATION = 230;
77 private static final int ON_EXIT_CLOSE_DELAY = 800;
Adam Cohen2801caf2011-05-13 20:57:39 -070078 private boolean mRearrangeOnClose = false;
79 private FolderIcon mFolderIcon;
80 private int mMaxCountX;
81 private int mMaxCountY;
Adam Cohen78dc83e2011-11-15 17:10:00 -080082 private int mMaxNumItems;
Adam Cohen7c693212011-05-18 15:26:57 -070083 private ArrayList<View> mItemsInReadingOrder = new ArrayList<View>();
Adam Cohenbadf71e2011-05-26 19:08:29 -070084 private Drawable mIconDrawable;
Adam Cohen7c693212011-05-18 15:26:57 -070085 boolean mItemsInvalidated = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -070086 private ShortcutInfo mCurrentDragInfo;
87 private View mCurrentDragView;
88 boolean mSuppressOnAdd = false;
89 private int[] mTargetCell = new int[2];
90 private int[] mPreviousTargetCell = new int[2];
91 private int[] mEmptyCell = new int[2];
92 private Alarm mReorderAlarm = new Alarm();
93 private Alarm mOnExitAlarm = new Alarm();
Adam Cohen76fc0852011-06-17 13:26:23 -070094 private int mFolderNameHeight;
Adam Cohen8e776a62011-06-28 18:10:06 -070095 private Rect mTempRect = new Rect();
Adam Cohen67bd9cc2011-07-29 14:07:04 -070096 private boolean mDragInProgress = false;
97 private boolean mDeleteFolderOnDropCompleted = false;
98 private boolean mSuppressFolderDeletion = false;
Adam Cohen05e0f402011-08-01 12:12:49 -070099 private boolean mItemAddedBackToSelfViaIcon = false;
Adam Cohenac56cff2011-09-28 20:45:37 -0700100 FolderEditText mFolderName;
Adam Cohen268c4752012-06-06 17:47:33 -0700101 private float mFolderIconPivotX;
102 private float mFolderIconPivotY;
Adam Cohen228da5a2011-07-27 22:23:47 -0700103
Adam Cohen76fc0852011-06-17 13:26:23 -0700104 private boolean mIsEditingName = false;
105 private InputMethodManager mInputMethodManager;
Adam Cohendf2cc412011-04-27 16:56:57 -0700106
Adam Cohena65beee2011-06-27 21:32:23 -0700107 private static String sDefaultFolderName;
108 private static String sHintText;
Michael Jurka0121c3e2012-05-31 08:36:04 -0700109 private ObjectAnimator mOpenCloseAnimator;
Adam Cohena65beee2011-06-27 21:32:23 -0700110
Adam Cohenfb91f302012-06-11 15:45:18 -0700111 private boolean mDestroyed;
112
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800113 /**
114 * Used to inflate the Workspace from XML.
115 *
116 * @param context The application's context.
117 * @param attrs The attribtues set containing the Workspace's customization values.
118 */
119 public Folder(Context context, AttributeSet attrs) {
120 super(context, attrs);
121 setAlwaysDrawnWithCacheEnabled(false);
Adam Cohendf2cc412011-04-27 16:56:57 -0700122 mInflater = LayoutInflater.from(context);
123 mIconCache = ((LauncherApplication)context.getApplicationContext()).getIconCache();
Adam Cohen78dc83e2011-11-15 17:10:00 -0800124
125 Resources res = getResources();
126 mMaxCountX = res.getInteger(R.integer.folder_max_count_x);
127 mMaxCountY = res.getInteger(R.integer.folder_max_count_y);
128 mMaxNumItems = res.getInteger(R.integer.folder_max_num_items);
129 if (mMaxCountX < 0 || mMaxCountY < 0 || mMaxNumItems < 0) {
130 mMaxCountX = LauncherModel.getCellCountX();
131 mMaxCountY = LauncherModel.getCellCountY();
132 mMaxNumItems = mMaxCountX * mMaxCountY;
133 }
Adam Cohen76fc0852011-06-17 13:26:23 -0700134
135 mInputMethodManager = (InputMethodManager)
Michael Jurka8b805b12012-04-18 14:23:14 -0700136 getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
Adam Cohen76fc0852011-06-17 13:26:23 -0700137
Adam Cohen76fc0852011-06-17 13:26:23 -0700138 mExpandDuration = res.getInteger(R.integer.config_folderAnimDuration);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700139
140 if (sDefaultFolderName == null) {
141 sDefaultFolderName = res.getString(R.string.folder_name);
142 }
Adam Cohena65beee2011-06-27 21:32:23 -0700143 if (sHintText == null) {
144 sHintText = res.getString(R.string.folder_hint_text);
145 }
Adam Cohen4eac29a2011-07-11 17:53:37 -0700146 mLauncher = (Launcher) context;
Adam Cohenac56cff2011-09-28 20:45:37 -0700147 // We need this view to be focusable in touch mode so that when text editing of the folder
148 // name is complete, we have something to focus on, thus hiding the cursor and giving
149 // reliable behvior when clicking the text field (since it will always gain focus on click).
150 setFocusableInTouchMode(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151 }
152
153 @Override
154 protected void onFinishInflate() {
155 super.onFinishInflate();
Adam Cohendf2cc412011-04-27 16:56:57 -0700156 mContent = (CellLayout) findViewById(R.id.folder_content);
Adam Cohen2801caf2011-05-13 20:57:39 -0700157 mContent.setGridSize(0, 0);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700158 mContent.getShortcutsAndWidgets().setMotionEventSplittingEnabled(false);
Adam Cohen2374abf2013-04-16 14:56:57 -0700159 mContent.setInvertIfRtl(true);
Adam Cohenac56cff2011-09-28 20:45:37 -0700160 mFolderName = (FolderEditText) findViewById(R.id.folder_name);
161 mFolderName.setFolder(this);
Adam Cohenea0818d2011-09-30 20:06:58 -0700162 mFolderName.setOnFocusChangeListener(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700163
164 // We find out how tall the text view wants to be (it is set to wrap_content), so that
165 // we can allocate the appropriate amount of space for it.
166 int measureSpec = MeasureSpec.UNSPECIFIED;
167 mFolderName.measure(measureSpec, measureSpec);
168 mFolderNameHeight = mFolderName.getMeasuredHeight();
169
170 // We disable action mode for now since it messes up the view on phones
171 mFolderName.setCustomSelectionActionModeCallback(mActionModeCallback);
Adam Cohen76fc0852011-06-17 13:26:23 -0700172 mFolderName.setOnEditorActionListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700173 mFolderName.setSelectAllOnFocus(true);
Adam Cohen7a14d0b2011-06-24 11:36:35 -0700174 mFolderName.setInputType(mFolderName.getInputType() |
175 InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700177
Adam Cohen76fc0852011-06-17 13:26:23 -0700178 private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
179 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
180 return false;
181 }
182
183 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
184 return false;
185 }
186
187 public void onDestroyActionMode(ActionMode mode) {
188 }
189
190 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
191 return false;
192 }
193 };
194
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800195 public void onClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700196 Object tag = v.getTag();
197 if (tag instanceof ShortcutInfo) {
198 // refactor this code from Folder
199 ShortcutInfo item = (ShortcutInfo) tag;
200 int[] pos = new int[2];
201 v.getLocationOnScreen(pos);
202 item.intent.setSourceBounds(new Rect(pos[0], pos[1],
203 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -0700204
205 mLauncher.startActivitySafely(v, item.intent, item);
Adam Cohendf2cc412011-04-27 16:56:57 -0700206 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800207 }
208
209 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -0700210 // Return if global dragging is not enabled
211 if (!mLauncher.isDraggingEnabled()) return true;
212
Adam Cohendf2cc412011-04-27 16:56:57 -0700213 Object tag = v.getTag();
214 if (tag instanceof ShortcutInfo) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700215 ShortcutInfo item = (ShortcutInfo) tag;
216 if (!v.isInTouchMode()) {
217 return false;
218 }
219
Winson Chung7d7541e2011-09-16 20:14:36 -0700220 mLauncher.dismissFolderCling(null);
221
Adam Cohendf2cc412011-04-27 16:56:57 -0700222 mLauncher.getWorkspace().onDragStartedWithItem(v);
Adam Cohenac8c8762011-07-13 11:15:27 -0700223 mLauncher.getWorkspace().beginDragShared(v, this);
Adam Cohenbadf71e2011-05-26 19:08:29 -0700224 mIconDrawable = ((TextView) v).getCompoundDrawables()[1];
Adam Cohen76078c42011-06-09 15:06:52 -0700225
226 mCurrentDragInfo = item;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700227 mEmptyCell[0] = item.cellX;
228 mEmptyCell[1] = item.cellY;
229 mCurrentDragView = v;
Adam Cohenfc53cd22011-07-20 15:45:11 -0700230
231 mContent.removeView(mCurrentDragView);
232 mInfo.remove(mCurrentDragInfo);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700233 mDragInProgress = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700234 mItemAddedBackToSelfViaIcon = false;
Adam Cohendf2cc412011-04-27 16:56:57 -0700235 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800236 return true;
237 }
238
Adam Cohen76fc0852011-06-17 13:26:23 -0700239 public boolean isEditingName() {
240 return mIsEditingName;
241 }
242
243 public void startEditingFolderName() {
Adam Cohena65beee2011-06-27 21:32:23 -0700244 mFolderName.setHint("");
Adam Cohen76fc0852011-06-17 13:26:23 -0700245 mIsEditingName = true;
246 }
247
248 public void dismissEditingName() {
249 mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
250 doneEditingFolderName(true);
251 }
252
253 public void doneEditingFolderName(boolean commit) {
Adam Cohena65beee2011-06-27 21:32:23 -0700254 mFolderName.setHint(sHintText);
Adam Cohen1df26a32011-08-12 16:15:23 -0700255 // Convert to a string here to ensure that no other state associated with the text field
256 // gets saved.
Adam Cohen3371da02011-10-25 21:38:29 -0700257 String newTitle = mFolderName.getText().toString();
258 mInfo.setTitle(newTitle);
Adam Cohen76fc0852011-06-17 13:26:23 -0700259 LauncherModel.updateItemInDatabase(mLauncher, mInfo);
Adam Cohenac56cff2011-09-28 20:45:37 -0700260
Adam Cohen3371da02011-10-25 21:38:29 -0700261 if (commit) {
262 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700263 String.format(getContext().getString(R.string.folder_renamed), newTitle));
Adam Cohen3371da02011-10-25 21:38:29 -0700264 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700265 // In order to clear the focus from the text field, we set the focus on ourself. This
266 // ensures that every time the field is clicked, focus is gained, giving reliable behavior.
267 requestFocus();
268
Adam Cohene601a432011-07-26 21:51:30 -0700269 Selection.setSelection((Spannable) mFolderName.getText(), 0, 0);
Adam Cohen76fc0852011-06-17 13:26:23 -0700270 mIsEditingName = false;
271 }
272
273 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
274 if (actionId == EditorInfo.IME_ACTION_DONE) {
275 dismissEditingName();
276 return true;
277 }
278 return false;
279 }
280
281 public View getEditTextRegion() {
282 return mFolderName;
283 }
284
Adam Cohenbadf71e2011-05-26 19:08:29 -0700285 public Drawable getDragDrawable() {
286 return mIconDrawable;
287 }
288
Adam Cohen0c872ba2011-05-05 10:34:16 -0700289 /**
290 * We need to handle touch events to prevent them from falling through to the workspace below.
291 */
292 @Override
293 public boolean onTouchEvent(MotionEvent ev) {
294 return true;
295 }
296
Joe Onorato00acb122009-08-04 16:04:30 -0400297 public void setDragController(DragController dragController) {
298 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800299 }
300
Adam Cohen2801caf2011-05-13 20:57:39 -0700301 void setFolderIcon(FolderIcon icon) {
302 mFolderIcon = icon;
303 }
304
Adam Cohen3371da02011-10-25 21:38:29 -0700305 @Override
306 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
307 // When the folder gets focus, we don't want to announce the list of items.
308 return true;
309 }
310
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800311 /**
312 * @return the FolderInfo object associated with this folder
313 */
314 FolderInfo getInfo() {
315 return mInfo;
316 }
317
Adam Cohen3bf84d32012-05-07 20:17:14 -0700318 private class GridComparator implements Comparator<ShortcutInfo> {
319 int mNumCols;
320 public GridComparator(int numCols) {
321 mNumCols = numCols;
322 }
323
324 @Override
325 public int compare(ShortcutInfo lhs, ShortcutInfo rhs) {
326 int lhIndex = lhs.cellY * mNumCols + lhs.cellX;
327 int rhIndex = rhs.cellY * mNumCols + rhs.cellX;
328 return (lhIndex - rhIndex);
329 }
Adam Cohen3bf84d32012-05-07 20:17:14 -0700330 }
331
332 private void placeInReadingOrder(ArrayList<ShortcutInfo> items) {
333 int maxX = 0;
334 int count = items.size();
335 for (int i = 0; i < count; i++) {
336 ShortcutInfo item = items.get(i);
337 if (item.cellX > maxX) {
338 maxX = item.cellX;
339 }
340 }
Adam Cohen691a5792012-05-11 14:27:30 -0700341
342 GridComparator gridComparator = new GridComparator(maxX + 1);
Adam Cohen3bf84d32012-05-07 20:17:14 -0700343 Collections.sort(items, gridComparator);
344 final int countX = mContent.getCountX();
345 for (int i = 0; i < count; i++) {
346 int x = i % countX;
347 int y = i / countX;
348 ShortcutInfo item = items.get(i);
349 item.cellX = x;
350 item.cellY = y;
351 }
352 }
353
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800354 void bind(FolderInfo info) {
355 mInfo = info;
Adam Cohendf2cc412011-04-27 16:56:57 -0700356 ArrayList<ShortcutInfo> children = info.contents;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700357 ArrayList<ShortcutInfo> overflow = new ArrayList<ShortcutInfo>();
Adam Cohen7c693212011-05-18 15:26:57 -0700358 setupContentForNumItems(children.size());
Adam Cohen3bf84d32012-05-07 20:17:14 -0700359 placeInReadingOrder(children);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700360 int count = 0;
Adam Cohendf2cc412011-04-27 16:56:57 -0700361 for (int i = 0; i < children.size(); i++) {
362 ShortcutInfo child = (ShortcutInfo) children.get(i);
Adam Cohenc508b2d2011-06-28 14:41:44 -0700363 if (!createAndAddShortcut(child)) {
364 overflow.add(child);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700365 } else {
366 count++;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700367 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700368 }
Adam Cohenc508b2d2011-06-28 14:41:44 -0700369
Adam Cohen0057bbc2011-08-12 18:30:51 -0700370 // We rearrange the items in case there are any empty gaps
371 setupContentForNumItems(count);
372
Adam Cohenc508b2d2011-06-28 14:41:44 -0700373 // If our folder has too many items we prune them from the list. This is an issue
374 // when upgrading from the old Folders implementation which could contain an unlimited
375 // number of items.
376 for (ShortcutInfo item: overflow) {
377 mInfo.remove(item);
378 LauncherModel.deleteItemFromDatabase(mLauncher, item);
379 }
380
Adam Cohen4dbe6d92011-05-18 17:14:15 -0700381 mItemsInvalidated = true;
Adam Cohenac56cff2011-09-28 20:45:37 -0700382 updateTextViewFocus();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700383 mInfo.addListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700384
Adam Cohenafb01ee2011-06-23 15:38:03 -0700385 if (!sDefaultFolderName.contentEquals(mInfo.title)) {
Adam Cohen4ef610f2011-06-21 22:37:36 -0700386 mFolderName.setText(mInfo.title);
387 } else {
388 mFolderName.setText("");
389 }
Adam Cohen691a5792012-05-11 14:27:30 -0700390 updateItemLocationsInDatabase();
Adam Cohendf2cc412011-04-27 16:56:57 -0700391 }
392
393 /**
394 * Creates a new UserFolder, inflated from R.layout.user_folder.
395 *
396 * @param context The application's context.
397 *
398 * @return A new UserFolder.
399 */
400 static Folder fromXml(Context context) {
401 return (Folder) LayoutInflater.from(context).inflate(R.layout.user_folder, null);
402 }
403
404 /**
405 * This method is intended to make the UserFolder to be visually identical in size and position
406 * to its associated FolderIcon. This allows for a seamless transition into the expanded state.
407 */
408 private void positionAndSizeAsIcon() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700409 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800410 setScaleX(0.8f);
411 setScaleY(0.8f);
412 setAlpha(0f);
Adam Cohendf2cc412011-04-27 16:56:57 -0700413 mState = STATE_SMALL;
414 }
415
416 public void animateOpen() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700417 positionAndSizeAsIcon();
418
Adam Cohen8e776a62011-06-28 18:10:06 -0700419 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen2801caf2011-05-13 20:57:39 -0700420 centerAboutIcon();
Adam Cohen662b5982011-12-13 17:45:21 -0800421 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1);
422 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
423 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700424 final ObjectAnimator oa = mOpenCloseAnimator =
Michael Jurka2ecf9952012-06-18 12:52:28 -0700425 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohen6441de02011-12-14 14:25:32 -0800426
Adam Cohen2801caf2011-05-13 20:57:39 -0700427 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700428 @Override
429 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700430 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700431 String.format(getContext().getString(R.string.folder_opened),
Adam Cohen3371da02011-10-25 21:38:29 -0700432 mContent.getCountX(), mContent.getCountY()));
Adam Cohendf2cc412011-04-27 16:56:57 -0700433 mState = STATE_ANIMATING;
434 }
435 @Override
436 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700437 mState = STATE_OPEN;
Michael Jurka0121c3e2012-05-31 08:36:04 -0700438 setLayerType(LAYER_TYPE_NONE, null);
Winson Chung7a74ac92011-09-20 17:43:51 -0700439 Cling cling = mLauncher.showFirstRunFoldersCling();
440 if (cling != null) {
441 cling.bringToFront();
442 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700443 setFocusOnFirstChild();
Adam Cohendf2cc412011-04-27 16:56:57 -0700444 }
445 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700446 oa.setDuration(mExpandDuration);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700447 setLayerType(LAYER_TYPE_HARDWARE, null);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100448 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700449 }
450
Adam Cohen3371da02011-10-25 21:38:29 -0700451 private void sendCustomAccessibilityEvent(int type, String text) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700452 AccessibilityManager accessibilityManager = (AccessibilityManager)
453 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
454 if (accessibilityManager.isEnabled()) {
Adam Cohen3371da02011-10-25 21:38:29 -0700455 AccessibilityEvent event = AccessibilityEvent.obtain(type);
456 onInitializeAccessibilityEvent(event);
457 event.getText().add(text);
Michael Jurka8b805b12012-04-18 14:23:14 -0700458 accessibilityManager.sendAccessibilityEvent(event);
Adam Cohen3371da02011-10-25 21:38:29 -0700459 }
460 }
461
Adam Cohenac56cff2011-09-28 20:45:37 -0700462 private void setFocusOnFirstChild() {
463 View firstChild = mContent.getChildAt(0, 0);
464 if (firstChild != null) {
465 firstChild.requestFocus();
466 }
467 }
468
Adam Cohendf2cc412011-04-27 16:56:57 -0700469 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700470 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800471 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
472 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
473 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700474 final ObjectAnimator oa = mOpenCloseAnimator =
Michael Jurka2ecf9952012-06-18 12:52:28 -0700475 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohendf2cc412011-04-27 16:56:57 -0700476
Adam Cohen2801caf2011-05-13 20:57:39 -0700477 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700478 @Override
479 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700480 onCloseComplete();
Michael Jurka0121c3e2012-05-31 08:36:04 -0700481 setLayerType(LAYER_TYPE_NONE, null);
Adam Cohen2801caf2011-05-13 20:57:39 -0700482 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700483 }
484 @Override
485 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700486 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700487 getContext().getString(R.string.folder_closed));
Adam Cohendf2cc412011-04-27 16:56:57 -0700488 mState = STATE_ANIMATING;
489 }
490 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700491 oa.setDuration(mExpandDuration);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700492 setLayerType(LAYER_TYPE_HARDWARE, null);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100493 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700494 }
495
496 void notifyDataSetChanged() {
497 // recreate all the children if the data set changes under us. We may want to do this more
498 // intelligently (ie just removing the views that should no longer exist)
499 mContent.removeAllViewsInLayout();
500 bind(mInfo);
501 }
502
Adam Cohencb3382b2011-05-24 14:07:08 -0700503 public boolean acceptDrop(DragObject d) {
504 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700505 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700506 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
507 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
508 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700509 }
510
Adam Cohendf2cc412011-04-27 16:56:57 -0700511 protected boolean findAndSetEmptyCells(ShortcutInfo item) {
512 int[] emptyCell = new int[2];
513 if (mContent.findCellForSpan(emptyCell, item.spanX, item.spanY)) {
514 item.cellX = emptyCell[0];
515 item.cellY = emptyCell[1];
Adam Cohendf2cc412011-04-27 16:56:57 -0700516 return true;
517 } else {
518 return false;
519 }
520 }
521
Adam Cohenc508b2d2011-06-28 14:41:44 -0700522 protected boolean createAndAddShortcut(ShortcutInfo item) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700523 final TextView textView =
Adam Cohene87b9242011-06-29 14:01:26 -0700524 (TextView) mInflater.inflate(R.layout.application, this, false);
Adam Cohendf2cc412011-04-27 16:56:57 -0700525 textView.setCompoundDrawablesWithIntrinsicBounds(null,
526 new FastBitmapDrawable(item.getIcon(mIconCache)), null, null);
527 textView.setText(item.title);
528 textView.setTag(item);
529
530 textView.setOnClickListener(this);
531 textView.setOnLongClickListener(this);
532
Adam Cohenc508b2d2011-06-28 14:41:44 -0700533 // We need to check here to verify that the given item's location isn't already occupied
Adam Cohen3bf84d32012-05-07 20:17:14 -0700534 // by another item.
Adam Cohen0057bbc2011-08-12 18:30:51 -0700535 if (mContent.getChildAt(item.cellX, item.cellY) != null || item.cellX < 0 || item.cellY < 0
536 || item.cellX >= mContent.getCountX() || item.cellY >= mContent.getCountY()) {
Adam Cohen3bf84d32012-05-07 20:17:14 -0700537 // This shouldn't happen, log it.
538 Log.e(TAG, "Folder order not properly persisted during bind");
Adam Cohenc508b2d2011-06-28 14:41:44 -0700539 if (!findAndSetEmptyCells(item)) {
540 return false;
541 }
542 }
543
Adam Cohendf2cc412011-04-27 16:56:57 -0700544 CellLayout.LayoutParams lp =
545 new CellLayout.LayoutParams(item.cellX, item.cellY, item.spanX, item.spanY);
546 boolean insert = false;
Adam Cohenac56cff2011-09-28 20:45:37 -0700547 textView.setOnKeyListener(new FolderKeyEventListener());
Adam Cohendf2cc412011-04-27 16:56:57 -0700548 mContent.addViewToCellLayout(textView, insert ? 0 : -1, (int)item.id, lp, true);
Adam Cohenc508b2d2011-06-28 14:41:44 -0700549 return true;
Adam Cohendf2cc412011-04-27 16:56:57 -0700550 }
551
Adam Cohencb3382b2011-05-24 14:07:08 -0700552 public void onDragEnter(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700553 mPreviousTargetCell[0] = -1;
554 mPreviousTargetCell[1] = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700555 mOnExitAlarm.cancelAlarm();
556 }
557
558 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
559 public void onAlarm(Alarm alarm) {
560 realTimeReorder(mEmptyCell, mTargetCell);
561 }
562 };
563
564 boolean readingOrderGreaterThan(int[] v1, int[] v2) {
565 if (v1[1] > v2[1] || (v1[1] == v2[1] && v1[0] > v2[0])) {
566 return true;
567 } else {
568 return false;
569 }
570 }
571
572 private void realTimeReorder(int[] empty, int[] target) {
573 boolean wrap;
574 int startX;
575 int endX;
576 int startY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700577 int delay = 0;
578 float delayAmount = 30;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700579 if (readingOrderGreaterThan(target, empty)) {
580 wrap = empty[0] >= mContent.getCountX() - 1;
581 startY = wrap ? empty[1] + 1 : empty[1];
582 for (int y = startY; y <= target[1]; y++) {
583 startX = y == empty[1] ? empty[0] + 1 : 0;
584 endX = y < target[1] ? mContent.getCountX() - 1 : target[0];
585 for (int x = startX; x <= endX; x++) {
586 View v = mContent.getChildAt(x,y);
587 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen482ed822012-03-02 14:15:13 -0800588 REORDER_ANIMATION_DURATION, delay, true, true)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700589 empty[0] = x;
590 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700591 delay += delayAmount;
592 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700593 }
594 }
595 }
596 } else {
597 wrap = empty[0] == 0;
598 startY = wrap ? empty[1] - 1 : empty[1];
599 for (int y = startY; y >= target[1]; y--) {
600 startX = y == empty[1] ? empty[0] - 1 : mContent.getCountX() - 1;
601 endX = y > target[1] ? 0 : target[0];
602 for (int x = startX; x >= endX; x--) {
603 View v = mContent.getChildAt(x,y);
604 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen482ed822012-03-02 14:15:13 -0800605 REORDER_ANIMATION_DURATION, delay, true, true)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700606 empty[0] = x;
607 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700608 delay += delayAmount;
609 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700610 }
611 }
612 }
613 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700614 }
615
Adam Cohen2374abf2013-04-16 14:56:57 -0700616 public boolean isLayoutRtl() {
617 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
618 }
619
Adam Cohencb3382b2011-05-24 14:07:08 -0700620 public void onDragOver(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700621 float[] r = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, d.dragView, null);
622 mTargetCell = mContent.findNearestArea((int) r[0], (int) r[1], 1, 1, mTargetCell);
623
Adam Cohen2374abf2013-04-16 14:56:57 -0700624 if (isLayoutRtl()) {
625 mTargetCell[0] = mContent.getCountX() - mTargetCell[0] - 1;
626 }
627
Adam Cohenbfbfd262011-06-13 16:55:12 -0700628 if (mTargetCell[0] != mPreviousTargetCell[0] || mTargetCell[1] != mPreviousTargetCell[1]) {
629 mReorderAlarm.cancelAlarm();
630 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
631 mReorderAlarm.setAlarm(150);
632 mPreviousTargetCell[0] = mTargetCell[0];
633 mPreviousTargetCell[1] = mTargetCell[1];
634 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700635 }
636
Adam Cohenbfbfd262011-06-13 16:55:12 -0700637 // This is used to compute the visual center of the dragView. The idea is that
638 // the visual center represents the user's interpretation of where the item is, and hence
639 // is the appropriate point to use when determining drop location.
640 private float[] getDragViewVisualCenter(int x, int y, int xOffset, int yOffset,
641 DragView dragView, float[] recycle) {
642 float res[];
643 if (recycle == null) {
644 res = new float[2];
645 } else {
646 res = recycle;
647 }
648
649 // These represent the visual top and left of drag view if a dragRect was provided.
650 // If a dragRect was not provided, then they correspond to the actual view left and
651 // top, as the dragRect is in that case taken to be the entire dragView.
652 // R.dimen.dragViewOffsetY.
653 int left = x - xOffset;
654 int top = y - yOffset;
655
656 // In order to find the visual center, we shift by half the dragRect
657 res[0] = left + dragView.getDragRegion().width() / 2;
658 res[1] = top + dragView.getDragRegion().height() / 2;
659
660 return res;
661 }
662
663 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
664 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700665 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700666 }
667 };
668
Adam Cohen95bb8002011-07-03 23:40:28 -0700669 public void completeDragExit() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700670 mLauncher.closeFolder();
671 mCurrentDragInfo = null;
672 mCurrentDragView = null;
673 mSuppressOnAdd = false;
674 mRearrangeOnClose = true;
675 }
676
Adam Cohencb3382b2011-05-24 14:07:08 -0700677 public void onDragExit(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700678 // We only close the folder if this is a true drag exit, ie. not because a drop
679 // has occurred above the folder.
680 if (!d.dragComplete) {
681 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
682 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
683 }
684 mReorderAlarm.cancelAlarm();
Adam Cohen2801caf2011-05-13 20:57:39 -0700685 }
686
Winson Chunga48487a2012-03-20 16:19:37 -0700687 public void onDropCompleted(View target, DragObject d, boolean isFlingToDelete,
688 boolean success) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700689 if (success) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700690 if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700691 replaceFolderWithFinalItem();
692 }
693 } else {
694 // The drag failed, we need to return the item to the folder
695 mFolderIcon.onDrop(d);
696
697 // We're going to trigger a "closeFolder" which may occur before this item has
698 // been added back to the folder -- this could cause the folder to be deleted
699 if (mOnExitAlarm.alarmPending()) {
700 mSuppressFolderDeletion = true;
701 }
702 }
703
704 if (target != this) {
705 if (mOnExitAlarm.alarmPending()) {
706 mOnExitAlarm.cancelAlarm();
707 completeDragExit();
708 }
709 }
710 mDeleteFolderOnDropCompleted = false;
711 mDragInProgress = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700712 mItemAddedBackToSelfViaIcon = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700713 mCurrentDragInfo = null;
714 mCurrentDragView = null;
715 mSuppressOnAdd = false;
Adam Cohen4045eb72011-10-06 11:44:26 -0700716
717 // Reordering may have occured, and we need to save the new item locations. We do this once
718 // at the end to prevent unnecessary database operations.
719 updateItemLocationsInDatabase();
720 }
721
Winson Chunga48487a2012-03-20 16:19:37 -0700722 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800723 public boolean supportsFlingToDelete() {
724 return true;
725 }
726
Winson Chunga48487a2012-03-20 16:19:37 -0700727 public void onFlingToDelete(DragObject d, int x, int y, PointF vec) {
728 // Do nothing
729 }
730
731 @Override
732 public void onFlingToDeleteCompleted() {
733 // Do nothing
734 }
735
Adam Cohen4045eb72011-10-06 11:44:26 -0700736 private void updateItemLocationsInDatabase() {
737 ArrayList<View> list = getItemsInReadingOrder();
738 for (int i = 0; i < list.size(); i++) {
739 View v = list.get(i);
740 ItemInfo info = (ItemInfo) v.getTag();
741 LauncherModel.moveItemInDatabase(mLauncher, info, mInfo.id, 0,
742 info.cellX, info.cellY);
743 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700744 }
Adam Cohen228da5a2011-07-27 22:23:47 -0700745
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700746 public void notifyDrop() {
747 if (mDragInProgress) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700748 mItemAddedBackToSelfViaIcon = true;
Adam Cohen76078c42011-06-09 15:06:52 -0700749 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700750 }
751
752 public boolean isDropEnabled() {
753 return true;
754 }
755
Adam Cohencb3382b2011-05-24 14:07:08 -0700756 public DropTarget getDropTargetDelegate(DragObject d) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700757 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800758 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700759
Adam Cohen4045eb72011-10-06 11:44:26 -0700760 private void setupContentDimensions(int count) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700761 ArrayList<View> list = getItemsInReadingOrder();
762
763 int countX = mContent.getCountX();
764 int countY = mContent.getCountY();
Adam Cohen7c693212011-05-18 15:26:57 -0700765 boolean done = false;
Adam Cohen2801caf2011-05-13 20:57:39 -0700766
Adam Cohen7c693212011-05-18 15:26:57 -0700767 while (!done) {
768 int oldCountX = countX;
769 int oldCountY = countY;
770 if (countX * countY < count) {
771 // Current grid is too small, expand it
Adam Cohen78dc83e2011-11-15 17:10:00 -0800772 if ((countX <= countY || countY == mMaxCountY) && countX < mMaxCountX) {
Adam Cohen7c693212011-05-18 15:26:57 -0700773 countX++;
774 } else if (countY < mMaxCountY) {
775 countY++;
776 }
777 if (countY == 0) countY++;
778 } else if ((countY - 1) * countX >= count && countY >= countX) {
779 countY = Math.max(0, countY - 1);
780 } else if ((countX - 1) * countY >= count) {
781 countX = Math.max(0, countX - 1);
Adam Cohen2801caf2011-05-13 20:57:39 -0700782 }
Adam Cohen7c693212011-05-18 15:26:57 -0700783 done = countX == oldCountX && countY == oldCountY;
Adam Cohen2801caf2011-05-13 20:57:39 -0700784 }
Adam Cohen7c693212011-05-18 15:26:57 -0700785 mContent.setGridSize(countX, countY);
Adam Cohen2801caf2011-05-13 20:57:39 -0700786 arrangeChildren(list);
787 }
788
789 public boolean isFull() {
Adam Cohen78dc83e2011-11-15 17:10:00 -0800790 return getItemCount() >= mMaxNumItems;
Adam Cohen2801caf2011-05-13 20:57:39 -0700791 }
792
793 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700794 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700795
796 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf4bb1cd2011-07-22 14:36:03 -0700797 int height = getPaddingTop() + getPaddingBottom() + mContent.getDesiredHeight()
798 + mFolderNameHeight;
Adam Cohen8e776a62011-06-28 18:10:06 -0700799 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700800
Adam Cohen307fe232012-08-16 17:55:58 -0700801 float scale = parent.getDescendantRectRelativeToSelf(mFolderIcon, mTempRect);
Adam Cohen8e776a62011-06-28 18:10:06 -0700802
Adam Cohen307fe232012-08-16 17:55:58 -0700803 int centerX = (int) (mTempRect.left + mTempRect.width() * scale / 2);
804 int centerY = (int) (mTempRect.top + mTempRect.height() * scale / 2);
Adam Cohen2801caf2011-05-13 20:57:39 -0700805 int centeredLeft = centerX - width / 2;
806 int centeredTop = centerY - height / 2;
807
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800808 int currentPage = mLauncher.getWorkspace().getCurrentPage();
809 // In case the workspace is scrolling, we need to use the final scroll to compute
810 // the folders bounds.
811 mLauncher.getWorkspace().setFinalScrollForPageChange(currentPage);
Adam Cohen35e7e642011-07-17 14:47:18 -0700812 // We first fetch the currently visible CellLayoutChildren
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800813 CellLayout currentLayout = (CellLayout) mLauncher.getWorkspace().getChildAt(currentPage);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700814 ShortcutAndWidgetContainer boundingLayout = currentLayout.getShortcutsAndWidgets();
Adam Cohen35e7e642011-07-17 14:47:18 -0700815 Rect bounds = new Rect();
816 parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800817 // We reset the workspaces scroll
818 mLauncher.getWorkspace().resetFinalScrollForPageChange(currentPage);
Adam Cohen2801caf2011-05-13 20:57:39 -0700819
Adam Cohen35e7e642011-07-17 14:47:18 -0700820 // We need to bound the folder to the currently visible CellLayoutChildren
821 int left = Math.min(Math.max(bounds.left, centeredLeft),
822 bounds.left + bounds.width() - width);
823 int top = Math.min(Math.max(bounds.top, centeredTop),
824 bounds.top + bounds.height() - height);
825 // If the folder doesn't fit within the bounds, center it about the desired bounds
826 if (width >= bounds.width()) {
827 left = bounds.left + (bounds.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700828 }
Adam Cohen35e7e642011-07-17 14:47:18 -0700829 if (height >= bounds.height()) {
830 top = bounds.top + (bounds.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700831 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700832
833 int folderPivotX = width / 2 + (centeredLeft - left);
834 int folderPivotY = height / 2 + (centeredTop - top);
835 setPivotX(folderPivotX);
836 setPivotY(folderPivotY);
Adam Cohen268c4752012-06-06 17:47:33 -0700837 mFolderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700838 (1.0f * folderPivotX / width));
Adam Cohen268c4752012-06-06 17:47:33 -0700839 mFolderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700840 (1.0f * folderPivotY / height));
Adam Cohen3bf84d32012-05-07 20:17:14 -0700841
Adam Cohen662b5982011-12-13 17:45:21 -0800842 lp.width = width;
843 lp.height = height;
844 lp.x = left;
845 lp.y = top;
Adam Cohen2801caf2011-05-13 20:57:39 -0700846 }
847
Adam Cohen268c4752012-06-06 17:47:33 -0700848 float getPivotXForIconAnimation() {
849 return mFolderIconPivotX;
850 }
851 float getPivotYForIconAnimation() {
852 return mFolderIconPivotY;
853 }
854
Adam Cohen2801caf2011-05-13 20:57:39 -0700855 private void setupContentForNumItems(int count) {
Adam Cohen4045eb72011-10-06 11:44:26 -0700856 setupContentDimensions(count);
Adam Cohen2801caf2011-05-13 20:57:39 -0700857
Adam Cohen8e776a62011-06-28 18:10:06 -0700858 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700859 if (lp == null) {
Adam Cohen8e776a62011-06-28 18:10:06 -0700860 lp = new DragLayer.LayoutParams(0, 0);
861 lp.customPosition = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700862 setLayoutParams(lp);
863 }
864 centerAboutIcon();
865 }
866
Adam Cohen234c4cd2011-07-17 21:03:04 -0700867 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
868 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf4bb1cd2011-07-22 14:36:03 -0700869 int height = getPaddingTop() + getPaddingBottom() + mContent.getDesiredHeight()
870 + mFolderNameHeight;
Adam Cohen234c4cd2011-07-17 21:03:04 -0700871
872 int contentWidthSpec = MeasureSpec.makeMeasureSpec(mContent.getDesiredWidth(),
873 MeasureSpec.EXACTLY);
874 int contentHeightSpec = MeasureSpec.makeMeasureSpec(mContent.getDesiredHeight(),
875 MeasureSpec.EXACTLY);
876 mContent.measure(contentWidthSpec, contentHeightSpec);
877
878 mFolderName.measure(contentWidthSpec,
879 MeasureSpec.makeMeasureSpec(mFolderNameHeight, MeasureSpec.EXACTLY));
880 setMeasuredDimension(width, height);
881 }
882
Adam Cohen2801caf2011-05-13 20:57:39 -0700883 private void arrangeChildren(ArrayList<View> list) {
884 int[] vacant = new int[2];
885 if (list == null) {
886 list = getItemsInReadingOrder();
887 }
888 mContent.removeAllViews();
889
890 for (int i = 0; i < list.size(); i++) {
891 View v = list.get(i);
892 mContent.getVacantCell(vacant, 1, 1);
893 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) v.getLayoutParams();
894 lp.cellX = vacant[0];
895 lp.cellY = vacant[1];
896 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohen2792a332011-09-26 21:09:47 -0700897 if (info.cellX != vacant[0] || info.cellY != vacant[1]) {
898 info.cellX = vacant[0];
899 info.cellY = vacant[1];
900 LauncherModel.addOrMoveItemInDatabase(mLauncher, info, mInfo.id, 0,
901 info.cellX, info.cellY);
902 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700903 boolean insert = false;
904 mContent.addViewToCellLayout(v, insert ? 0 : -1, (int)info.id, lp, true);
905 }
Adam Cohen7c693212011-05-18 15:26:57 -0700906 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700907 }
908
Adam Cohena9cf38f2011-05-02 15:36:58 -0700909 public int getItemCount() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700910 return mContent.getShortcutsAndWidgets().getChildCount();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700911 }
912
913 public View getItemAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700914 return mContent.getShortcutsAndWidgets().getChildAt(index);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700915 }
916
Adam Cohen2801caf2011-05-13 20:57:39 -0700917 private void onCloseComplete() {
Adam Cohen05e0f402011-08-01 12:12:49 -0700918 DragLayer parent = (DragLayer) getParent();
Michael Jurka5649c282012-06-18 10:33:21 -0700919 if (parent != null) {
920 parent.removeView(this);
921 }
Adam Cohen4554ee12011-08-03 16:13:21 -0700922 mDragController.removeDropTarget((DropTarget) this);
Adam Cohen05e0f402011-08-01 12:12:49 -0700923 clearFocus();
Adam Cohenac56cff2011-09-28 20:45:37 -0700924 mFolderIcon.requestFocus();
Adam Cohen05e0f402011-08-01 12:12:49 -0700925
Adam Cohen2801caf2011-05-13 20:57:39 -0700926 if (mRearrangeOnClose) {
927 setupContentForNumItems(getItemCount());
928 mRearrangeOnClose = false;
929 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700930 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700931 if (!mDragInProgress && !mSuppressFolderDeletion) {
932 replaceFolderWithFinalItem();
933 } else if (mDragInProgress) {
934 mDeleteFolderOnDropCompleted = true;
935 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700936 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700937 mSuppressFolderDeletion = false;
Adam Cohenafb01ee2011-06-23 15:38:03 -0700938 }
939
940 private void replaceFolderWithFinalItem() {
Adam Cohenafb01ee2011-06-23 15:38:03 -0700941 // Add the last remaining child to the workspace in place of the folder
Adam Cohenfb91f302012-06-11 15:45:18 -0700942 Runnable onCompleteRunnable = new Runnable() {
943 @Override
944 public void run() {
945 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screen);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700946
Adam Cohenc5e63f32012-07-12 16:16:57 -0700947 View child = null;
Adam Cohenfb91f302012-06-11 15:45:18 -0700948 // Move the item from the folder to the workspace, in the position of the folder
949 if (getItemCount() == 1) {
950 ShortcutInfo finalItem = mInfo.contents.get(0);
Adam Cohenc5e63f32012-07-12 16:16:57 -0700951 child = mLauncher.createShortcut(R.layout.application, cellLayout,
Adam Cohenfb91f302012-06-11 15:45:18 -0700952 finalItem);
953 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
954 mInfo.screen, mInfo.cellX, mInfo.cellY);
Adam Cohenfb91f302012-06-11 15:45:18 -0700955 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700956 if (getItemCount() <= 1) {
957 // Remove the folder
958 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
959 cellLayout.removeView(mFolderIcon);
960 if (mFolderIcon instanceof DropTarget) {
961 mDragController.removeDropTarget((DropTarget) mFolderIcon);
962 }
963 mLauncher.removeFolder(mInfo);
964 }
Adam Cohenc5e63f32012-07-12 16:16:57 -0700965 // We add the child after removing the folder to prevent both from existing at
966 // the same time in the CellLayout.
967 if (child != null) {
968 mLauncher.getWorkspace().addInScreen(child, mInfo.container, mInfo.screen,
969 mInfo.cellX, mInfo.cellY, mInfo.spanX, mInfo.spanY);
970 }
Adam Cohenfb91f302012-06-11 15:45:18 -0700971 }
972 };
973 View finalChild = getItemAt(0);
974 if (finalChild != null) {
975 mFolderIcon.performDestroyAnimation(finalChild, onCompleteRunnable);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700976 }
Adam Cohenfb91f302012-06-11 15:45:18 -0700977 mDestroyed = true;
978 }
979
980 boolean isDestroyed() {
981 return mDestroyed;
Adam Cohen2801caf2011-05-13 20:57:39 -0700982 }
983
Adam Cohenac56cff2011-09-28 20:45:37 -0700984 // This method keeps track of the last item in the folder for the purposes
985 // of keyboard focus
986 private void updateTextViewFocus() {
987 View lastChild = getItemAt(getItemCount() - 1);
988 getItemAt(getItemCount() - 1);
989 if (lastChild != null) {
990 mFolderName.setNextFocusDownId(lastChild.getId());
991 mFolderName.setNextFocusRightId(lastChild.getId());
992 mFolderName.setNextFocusLeftId(lastChild.getId());
993 mFolderName.setNextFocusUpId(lastChild.getId());
994 }
995 }
996
Adam Cohenbfbfd262011-06-13 16:55:12 -0700997 public void onDrop(DragObject d) {
998 ShortcutInfo item;
999 if (d.dragInfo instanceof ApplicationInfo) {
1000 // Came from all apps -- make a copy
1001 item = ((ApplicationInfo) d.dragInfo).makeShortcut();
1002 item.spanX = 1;
1003 item.spanY = 1;
1004 } else {
1005 item = (ShortcutInfo) d.dragInfo;
1006 }
Adam Cohen05e0f402011-08-01 12:12:49 -07001007 // Dragged from self onto self, currently this is the only path possible, however
1008 // we keep this as a distinct code path.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001009 if (item == mCurrentDragInfo) {
1010 ShortcutInfo si = (ShortcutInfo) mCurrentDragView.getTag();
1011 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) mCurrentDragView.getLayoutParams();
1012 si.cellX = lp.cellX = mEmptyCell[0];
1013 si.cellX = lp.cellY = mEmptyCell[1];
1014 mContent.addViewToCellLayout(mCurrentDragView, -1, (int)item.id, lp, true);
Adam Cohenfc53cd22011-07-20 15:45:11 -07001015 if (d.dragView.hasDrawn()) {
1016 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, mCurrentDragView);
1017 } else {
Winson Chung2c4cf412012-05-08 14:03:21 -07001018 d.deferDragViewCleanupPostAnimation = false;
Adam Cohenfc53cd22011-07-20 15:45:11 -07001019 mCurrentDragView.setVisibility(VISIBLE);
1020 }
Adam Cohene9166b22011-07-08 17:11:11 -07001021 mItemsInvalidated = true;
Adam Cohen4045eb72011-10-06 11:44:26 -07001022 setupContentDimensions(getItemCount());
Adam Cohen716b51e2011-06-30 12:09:54 -07001023 mSuppressOnAdd = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001024 }
1025 mInfo.add(item);
1026 }
1027
1028 public void onAdd(ShortcutInfo item) {
1029 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001030 // If the item was dropped onto this open folder, we have done the work associated
1031 // with adding the item to the folder, as indicated by mSuppressOnAdd being set
Adam Cohenbfbfd262011-06-13 16:55:12 -07001032 if (mSuppressOnAdd) return;
1033 if (!findAndSetEmptyCells(item)) {
1034 // The current layout is full, can we expand it?
1035 setupContentForNumItems(getItemCount() + 1);
1036 findAndSetEmptyCells(item);
1037 }
1038 createAndAddShortcut(item);
1039 LauncherModel.addOrMoveItemInDatabase(
1040 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
1041 }
1042
Adam Cohena9cf38f2011-05-02 15:36:58 -07001043 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -07001044 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001045 // If this item is being dragged from this open folder, we have already handled
1046 // the work associated with removing the item, so we don't have to do anything here.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001047 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001048 View v = getViewForInfo(item);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001049 mContent.removeView(v);
Adam Cohen2801caf2011-05-13 20:57:39 -07001050 if (mState == STATE_ANIMATING) {
1051 mRearrangeOnClose = true;
1052 } else {
1053 setupContentForNumItems(getItemCount());
1054 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001055 if (getItemCount() <= 1) {
1056 replaceFolderWithFinalItem();
1057 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001058 }
Adam Cohen7c693212011-05-18 15:26:57 -07001059
Adam Cohendf1e4e82011-06-24 15:57:39 -07001060 private View getViewForInfo(ShortcutInfo item) {
1061 for (int j = 0; j < mContent.getCountY(); j++) {
1062 for (int i = 0; i < mContent.getCountX(); i++) {
1063 View v = mContent.getChildAt(i, j);
1064 if (v.getTag() == item) {
1065 return v;
1066 }
1067 }
1068 }
1069 return null;
1070 }
1071
Adam Cohen76078c42011-06-09 15:06:52 -07001072 public void onItemsChanged() {
Adam Cohenac56cff2011-09-28 20:45:37 -07001073 updateTextViewFocus();
Adam Cohen76078c42011-06-09 15:06:52 -07001074 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001075
Adam Cohen76fc0852011-06-17 13:26:23 -07001076 public void onTitleChanged(CharSequence title) {
1077 }
Adam Cohen76078c42011-06-09 15:06:52 -07001078
Adam Cohen7c693212011-05-18 15:26:57 -07001079 public ArrayList<View> getItemsInReadingOrder() {
Adam Cohen76078c42011-06-09 15:06:52 -07001080 return getItemsInReadingOrder(true);
1081 }
1082
1083 public ArrayList<View> getItemsInReadingOrder(boolean includeCurrentDragItem) {
Adam Cohen7c693212011-05-18 15:26:57 -07001084 if (mItemsInvalidated) {
1085 mItemsInReadingOrder.clear();
1086 for (int j = 0; j < mContent.getCountY(); j++) {
1087 for (int i = 0; i < mContent.getCountX(); i++) {
1088 View v = mContent.getChildAt(i, j);
1089 if (v != null) {
Adam Cohen76078c42011-06-09 15:06:52 -07001090 ShortcutInfo info = (ShortcutInfo) v.getTag();
1091 if (info != mCurrentDragInfo || includeCurrentDragItem) {
1092 mItemsInReadingOrder.add(v);
1093 }
Adam Cohen7c693212011-05-18 15:26:57 -07001094 }
1095 }
1096 }
1097 mItemsInvalidated = false;
1098 }
1099 return mItemsInReadingOrder;
1100 }
Adam Cohen8dfcba42011-07-07 16:38:18 -07001101
1102 public void getLocationInDragLayer(int[] loc) {
1103 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
1104 }
Adam Cohenea0818d2011-09-30 20:06:58 -07001105
1106 public void onFocusChange(View v, boolean hasFocus) {
1107 if (v == mFolderName && hasFocus) {
1108 startEditingFolderName();
1109 }
1110 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001111}