blob: c58189cde75fbe6b438f1a2fae9cc8eaab1af173 [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;
109
Adam Cohenfb91f302012-06-11 15:45:18 -0700110 private boolean mDestroyed;
111
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800112 /**
113 * Used to inflate the Workspace from XML.
114 *
115 * @param context The application's context.
116 * @param attrs The attribtues set containing the Workspace's customization values.
117 */
118 public Folder(Context context, AttributeSet attrs) {
119 super(context, attrs);
120 setAlwaysDrawnWithCacheEnabled(false);
Adam Cohendf2cc412011-04-27 16:56:57 -0700121 mInflater = LayoutInflater.from(context);
122 mIconCache = ((LauncherApplication)context.getApplicationContext()).getIconCache();
Adam Cohen78dc83e2011-11-15 17:10:00 -0800123
124 Resources res = getResources();
125 mMaxCountX = res.getInteger(R.integer.folder_max_count_x);
126 mMaxCountY = res.getInteger(R.integer.folder_max_count_y);
127 mMaxNumItems = res.getInteger(R.integer.folder_max_num_items);
128 if (mMaxCountX < 0 || mMaxCountY < 0 || mMaxNumItems < 0) {
129 mMaxCountX = LauncherModel.getCellCountX();
130 mMaxCountY = LauncherModel.getCellCountY();
131 mMaxNumItems = mMaxCountX * mMaxCountY;
132 }
Adam Cohen76fc0852011-06-17 13:26:23 -0700133
134 mInputMethodManager = (InputMethodManager)
Michael Jurka8b805b12012-04-18 14:23:14 -0700135 getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
Adam Cohen76fc0852011-06-17 13:26:23 -0700136
Adam Cohen76fc0852011-06-17 13:26:23 -0700137 mExpandDuration = res.getInteger(R.integer.config_folderAnimDuration);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700138
139 if (sDefaultFolderName == null) {
140 sDefaultFolderName = res.getString(R.string.folder_name);
141 }
Adam Cohena65beee2011-06-27 21:32:23 -0700142 if (sHintText == null) {
143 sHintText = res.getString(R.string.folder_hint_text);
144 }
Adam Cohen4eac29a2011-07-11 17:53:37 -0700145 mLauncher = (Launcher) context;
Adam Cohenac56cff2011-09-28 20:45:37 -0700146 // We need this view to be focusable in touch mode so that when text editing of the folder
147 // name is complete, we have something to focus on, thus hiding the cursor and giving
148 // reliable behvior when clicking the text field (since it will always gain focus on click).
149 setFocusableInTouchMode(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150 }
151
152 @Override
153 protected void onFinishInflate() {
154 super.onFinishInflate();
Adam Cohendf2cc412011-04-27 16:56:57 -0700155 mContent = (CellLayout) findViewById(R.id.folder_content);
Adam Cohen2801caf2011-05-13 20:57:39 -0700156 mContent.setGridSize(0, 0);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700157 mContent.getShortcutsAndWidgets().setMotionEventSplittingEnabled(false);
Adam Cohen2374abf2013-04-16 14:56:57 -0700158 mContent.setInvertIfRtl(true);
Adam Cohenac56cff2011-09-28 20:45:37 -0700159 mFolderName = (FolderEditText) findViewById(R.id.folder_name);
160 mFolderName.setFolder(this);
Adam Cohenea0818d2011-09-30 20:06:58 -0700161 mFolderName.setOnFocusChangeListener(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700162
163 // We find out how tall the text view wants to be (it is set to wrap_content), so that
164 // we can allocate the appropriate amount of space for it.
165 int measureSpec = MeasureSpec.UNSPECIFIED;
166 mFolderName.measure(measureSpec, measureSpec);
167 mFolderNameHeight = mFolderName.getMeasuredHeight();
168
169 // We disable action mode for now since it messes up the view on phones
170 mFolderName.setCustomSelectionActionModeCallback(mActionModeCallback);
Adam Cohen76fc0852011-06-17 13:26:23 -0700171 mFolderName.setOnEditorActionListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700172 mFolderName.setSelectAllOnFocus(true);
Adam Cohen7a14d0b2011-06-24 11:36:35 -0700173 mFolderName.setInputType(mFolderName.getInputType() |
174 InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700176
Adam Cohen76fc0852011-06-17 13:26:23 -0700177 private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
178 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
179 return false;
180 }
181
182 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
183 return false;
184 }
185
186 public void onDestroyActionMode(ActionMode mode) {
187 }
188
189 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
190 return false;
191 }
192 };
193
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194 public void onClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700195 Object tag = v.getTag();
196 if (tag instanceof ShortcutInfo) {
197 // refactor this code from Folder
198 ShortcutInfo item = (ShortcutInfo) tag;
199 int[] pos = new int[2];
200 v.getLocationOnScreen(pos);
201 item.intent.setSourceBounds(new Rect(pos[0], pos[1],
202 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -0700203
204 mLauncher.startActivitySafely(v, item.intent, item);
Adam Cohendf2cc412011-04-27 16:56:57 -0700205 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800206 }
207
208 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -0700209 // Return if global dragging is not enabled
210 if (!mLauncher.isDraggingEnabled()) return true;
211
Adam Cohendf2cc412011-04-27 16:56:57 -0700212 Object tag = v.getTag();
213 if (tag instanceof ShortcutInfo) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700214 ShortcutInfo item = (ShortcutInfo) tag;
215 if (!v.isInTouchMode()) {
216 return false;
217 }
218
Winson Chung7d7541e2011-09-16 20:14:36 -0700219 mLauncher.dismissFolderCling(null);
220
Adam Cohendf2cc412011-04-27 16:56:57 -0700221 mLauncher.getWorkspace().onDragStartedWithItem(v);
Adam Cohenac8c8762011-07-13 11:15:27 -0700222 mLauncher.getWorkspace().beginDragShared(v, this);
Adam Cohenbadf71e2011-05-26 19:08:29 -0700223 mIconDrawable = ((TextView) v).getCompoundDrawables()[1];
Adam Cohen76078c42011-06-09 15:06:52 -0700224
225 mCurrentDragInfo = item;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700226 mEmptyCell[0] = item.cellX;
227 mEmptyCell[1] = item.cellY;
228 mCurrentDragView = v;
Adam Cohenfc53cd22011-07-20 15:45:11 -0700229
230 mContent.removeView(mCurrentDragView);
231 mInfo.remove(mCurrentDragInfo);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700232 mDragInProgress = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700233 mItemAddedBackToSelfViaIcon = false;
Adam Cohendf2cc412011-04-27 16:56:57 -0700234 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800235 return true;
236 }
237
Adam Cohen76fc0852011-06-17 13:26:23 -0700238 public boolean isEditingName() {
239 return mIsEditingName;
240 }
241
242 public void startEditingFolderName() {
Adam Cohena65beee2011-06-27 21:32:23 -0700243 mFolderName.setHint("");
Adam Cohen76fc0852011-06-17 13:26:23 -0700244 mIsEditingName = true;
245 }
246
247 public void dismissEditingName() {
248 mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
249 doneEditingFolderName(true);
250 }
251
252 public void doneEditingFolderName(boolean commit) {
Adam Cohena65beee2011-06-27 21:32:23 -0700253 mFolderName.setHint(sHintText);
Adam Cohen1df26a32011-08-12 16:15:23 -0700254 // Convert to a string here to ensure that no other state associated with the text field
255 // gets saved.
Adam Cohen3371da02011-10-25 21:38:29 -0700256 String newTitle = mFolderName.getText().toString();
257 mInfo.setTitle(newTitle);
Adam Cohen76fc0852011-06-17 13:26:23 -0700258 LauncherModel.updateItemInDatabase(mLauncher, mInfo);
Adam Cohenac56cff2011-09-28 20:45:37 -0700259
Adam Cohen3371da02011-10-25 21:38:29 -0700260 if (commit) {
261 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700262 String.format(getContext().getString(R.string.folder_renamed), newTitle));
Adam Cohen3371da02011-10-25 21:38:29 -0700263 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700264 // In order to clear the focus from the text field, we set the focus on ourself. This
265 // ensures that every time the field is clicked, focus is gained, giving reliable behavior.
266 requestFocus();
267
Adam Cohene601a432011-07-26 21:51:30 -0700268 Selection.setSelection((Spannable) mFolderName.getText(), 0, 0);
Adam Cohen76fc0852011-06-17 13:26:23 -0700269 mIsEditingName = false;
270 }
271
272 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
273 if (actionId == EditorInfo.IME_ACTION_DONE) {
274 dismissEditingName();
275 return true;
276 }
277 return false;
278 }
279
280 public View getEditTextRegion() {
281 return mFolderName;
282 }
283
Adam Cohenbadf71e2011-05-26 19:08:29 -0700284 public Drawable getDragDrawable() {
285 return mIconDrawable;
286 }
287
Adam Cohen0c872ba2011-05-05 10:34:16 -0700288 /**
289 * We need to handle touch events to prevent them from falling through to the workspace below.
290 */
291 @Override
292 public boolean onTouchEvent(MotionEvent ev) {
293 return true;
294 }
295
Joe Onorato00acb122009-08-04 16:04:30 -0400296 public void setDragController(DragController dragController) {
297 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800298 }
299
Adam Cohen2801caf2011-05-13 20:57:39 -0700300 void setFolderIcon(FolderIcon icon) {
301 mFolderIcon = icon;
302 }
303
Adam Cohen3371da02011-10-25 21:38:29 -0700304 @Override
305 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
306 // When the folder gets focus, we don't want to announce the list of items.
307 return true;
308 }
309
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800310 /**
311 * @return the FolderInfo object associated with this folder
312 */
313 FolderInfo getInfo() {
314 return mInfo;
315 }
316
Adam Cohen3bf84d32012-05-07 20:17:14 -0700317 private class GridComparator implements Comparator<ShortcutInfo> {
318 int mNumCols;
319 public GridComparator(int numCols) {
320 mNumCols = numCols;
321 }
322
323 @Override
324 public int compare(ShortcutInfo lhs, ShortcutInfo rhs) {
325 int lhIndex = lhs.cellY * mNumCols + lhs.cellX;
326 int rhIndex = rhs.cellY * mNumCols + rhs.cellX;
327 return (lhIndex - rhIndex);
328 }
Adam Cohen3bf84d32012-05-07 20:17:14 -0700329 }
330
331 private void placeInReadingOrder(ArrayList<ShortcutInfo> items) {
332 int maxX = 0;
333 int count = items.size();
334 for (int i = 0; i < count; i++) {
335 ShortcutInfo item = items.get(i);
336 if (item.cellX > maxX) {
337 maxX = item.cellX;
338 }
339 }
Adam Cohen691a5792012-05-11 14:27:30 -0700340
341 GridComparator gridComparator = new GridComparator(maxX + 1);
Adam Cohen3bf84d32012-05-07 20:17:14 -0700342 Collections.sort(items, gridComparator);
343 final int countX = mContent.getCountX();
344 for (int i = 0; i < count; i++) {
345 int x = i % countX;
346 int y = i / countX;
347 ShortcutInfo item = items.get(i);
348 item.cellX = x;
349 item.cellY = y;
350 }
351 }
352
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800353 void bind(FolderInfo info) {
354 mInfo = info;
Adam Cohendf2cc412011-04-27 16:56:57 -0700355 ArrayList<ShortcutInfo> children = info.contents;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700356 ArrayList<ShortcutInfo> overflow = new ArrayList<ShortcutInfo>();
Adam Cohen7c693212011-05-18 15:26:57 -0700357 setupContentForNumItems(children.size());
Adam Cohen3bf84d32012-05-07 20:17:14 -0700358 placeInReadingOrder(children);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700359 int count = 0;
Adam Cohendf2cc412011-04-27 16:56:57 -0700360 for (int i = 0; i < children.size(); i++) {
361 ShortcutInfo child = (ShortcutInfo) children.get(i);
Adam Cohenc508b2d2011-06-28 14:41:44 -0700362 if (!createAndAddShortcut(child)) {
363 overflow.add(child);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700364 } else {
365 count++;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700366 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700367 }
Adam Cohenc508b2d2011-06-28 14:41:44 -0700368
Adam Cohen0057bbc2011-08-12 18:30:51 -0700369 // We rearrange the items in case there are any empty gaps
370 setupContentForNumItems(count);
371
Adam Cohenc508b2d2011-06-28 14:41:44 -0700372 // If our folder has too many items we prune them from the list. This is an issue
373 // when upgrading from the old Folders implementation which could contain an unlimited
374 // number of items.
375 for (ShortcutInfo item: overflow) {
376 mInfo.remove(item);
377 LauncherModel.deleteItemFromDatabase(mLauncher, item);
378 }
379
Adam Cohen4dbe6d92011-05-18 17:14:15 -0700380 mItemsInvalidated = true;
Adam Cohenac56cff2011-09-28 20:45:37 -0700381 updateTextViewFocus();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700382 mInfo.addListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700383
Adam Cohenafb01ee2011-06-23 15:38:03 -0700384 if (!sDefaultFolderName.contentEquals(mInfo.title)) {
Adam Cohen4ef610f2011-06-21 22:37:36 -0700385 mFolderName.setText(mInfo.title);
386 } else {
387 mFolderName.setText("");
388 }
Adam Cohen691a5792012-05-11 14:27:30 -0700389 updateItemLocationsInDatabase();
Adam Cohendf2cc412011-04-27 16:56:57 -0700390 }
391
392 /**
393 * Creates a new UserFolder, inflated from R.layout.user_folder.
394 *
395 * @param context The application's context.
396 *
397 * @return A new UserFolder.
398 */
399 static Folder fromXml(Context context) {
400 return (Folder) LayoutInflater.from(context).inflate(R.layout.user_folder, null);
401 }
402
403 /**
404 * This method is intended to make the UserFolder to be visually identical in size and position
405 * to its associated FolderIcon. This allows for a seamless transition into the expanded state.
406 */
407 private void positionAndSizeAsIcon() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700408 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800409 setScaleX(0.8f);
410 setScaleY(0.8f);
411 setAlpha(0f);
Adam Cohendf2cc412011-04-27 16:56:57 -0700412 mState = STATE_SMALL;
413 }
414
415 public void animateOpen() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700416 positionAndSizeAsIcon();
417
Adam Cohen8e776a62011-06-28 18:10:06 -0700418 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen2801caf2011-05-13 20:57:39 -0700419 centerAboutIcon();
Adam Cohen662b5982011-12-13 17:45:21 -0800420 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1);
421 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
422 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
Michael Jurka032e6ba2013-04-22 15:08:42 +0200423 final ObjectAnimator oa =
Michael Jurka2ecf9952012-06-18 12:52:28 -0700424 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohen6441de02011-12-14 14:25:32 -0800425
Adam Cohen2801caf2011-05-13 20:57:39 -0700426 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700427 @Override
428 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700429 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700430 String.format(getContext().getString(R.string.folder_opened),
Adam Cohen3371da02011-10-25 21:38:29 -0700431 mContent.getCountX(), mContent.getCountY()));
Adam Cohendf2cc412011-04-27 16:56:57 -0700432 mState = STATE_ANIMATING;
433 }
434 @Override
435 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700436 mState = STATE_OPEN;
Michael Jurka0121c3e2012-05-31 08:36:04 -0700437 setLayerType(LAYER_TYPE_NONE, null);
Winson Chung7a74ac92011-09-20 17:43:51 -0700438 Cling cling = mLauncher.showFirstRunFoldersCling();
439 if (cling != null) {
440 cling.bringToFront();
441 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700442 setFocusOnFirstChild();
Adam Cohendf2cc412011-04-27 16:56:57 -0700443 }
444 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700445 oa.setDuration(mExpandDuration);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700446 setLayerType(LAYER_TYPE_HARDWARE, null);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100447 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700448 }
449
Adam Cohen3371da02011-10-25 21:38:29 -0700450 private void sendCustomAccessibilityEvent(int type, String text) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700451 AccessibilityManager accessibilityManager = (AccessibilityManager)
452 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
453 if (accessibilityManager.isEnabled()) {
Adam Cohen3371da02011-10-25 21:38:29 -0700454 AccessibilityEvent event = AccessibilityEvent.obtain(type);
455 onInitializeAccessibilityEvent(event);
456 event.getText().add(text);
Michael Jurka8b805b12012-04-18 14:23:14 -0700457 accessibilityManager.sendAccessibilityEvent(event);
Adam Cohen3371da02011-10-25 21:38:29 -0700458 }
459 }
460
Adam Cohenac56cff2011-09-28 20:45:37 -0700461 private void setFocusOnFirstChild() {
462 View firstChild = mContent.getChildAt(0, 0);
463 if (firstChild != null) {
464 firstChild.requestFocus();
465 }
466 }
467
Adam Cohendf2cc412011-04-27 16:56:57 -0700468 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700469 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800470 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
471 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
472 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
Michael Jurka032e6ba2013-04-22 15:08:42 +0200473 final ObjectAnimator oa =
Michael Jurka2ecf9952012-06-18 12:52:28 -0700474 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohendf2cc412011-04-27 16:56:57 -0700475
Adam Cohen2801caf2011-05-13 20:57:39 -0700476 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700477 @Override
478 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700479 onCloseComplete();
Michael Jurka0121c3e2012-05-31 08:36:04 -0700480 setLayerType(LAYER_TYPE_NONE, null);
Adam Cohen2801caf2011-05-13 20:57:39 -0700481 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700482 }
483 @Override
484 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700485 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700486 getContext().getString(R.string.folder_closed));
Adam Cohendf2cc412011-04-27 16:56:57 -0700487 mState = STATE_ANIMATING;
488 }
489 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700490 oa.setDuration(mExpandDuration);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700491 setLayerType(LAYER_TYPE_HARDWARE, null);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100492 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700493 }
494
495 void notifyDataSetChanged() {
496 // recreate all the children if the data set changes under us. We may want to do this more
497 // intelligently (ie just removing the views that should no longer exist)
498 mContent.removeAllViewsInLayout();
499 bind(mInfo);
500 }
501
Adam Cohencb3382b2011-05-24 14:07:08 -0700502 public boolean acceptDrop(DragObject d) {
503 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700504 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700505 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
506 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
507 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700508 }
509
Adam Cohendf2cc412011-04-27 16:56:57 -0700510 protected boolean findAndSetEmptyCells(ShortcutInfo item) {
511 int[] emptyCell = new int[2];
512 if (mContent.findCellForSpan(emptyCell, item.spanX, item.spanY)) {
513 item.cellX = emptyCell[0];
514 item.cellY = emptyCell[1];
Adam Cohendf2cc412011-04-27 16:56:57 -0700515 return true;
516 } else {
517 return false;
518 }
519 }
520
Adam Cohenc508b2d2011-06-28 14:41:44 -0700521 protected boolean createAndAddShortcut(ShortcutInfo item) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700522 final TextView textView =
Adam Cohene87b9242011-06-29 14:01:26 -0700523 (TextView) mInflater.inflate(R.layout.application, this, false);
Adam Cohendf2cc412011-04-27 16:56:57 -0700524 textView.setCompoundDrawablesWithIntrinsicBounds(null,
525 new FastBitmapDrawable(item.getIcon(mIconCache)), null, null);
526 textView.setText(item.title);
527 textView.setTag(item);
528
529 textView.setOnClickListener(this);
530 textView.setOnLongClickListener(this);
531
Adam Cohenc508b2d2011-06-28 14:41:44 -0700532 // We need to check here to verify that the given item's location isn't already occupied
Adam Cohen3bf84d32012-05-07 20:17:14 -0700533 // by another item.
Adam Cohen0057bbc2011-08-12 18:30:51 -0700534 if (mContent.getChildAt(item.cellX, item.cellY) != null || item.cellX < 0 || item.cellY < 0
535 || item.cellX >= mContent.getCountX() || item.cellY >= mContent.getCountY()) {
Adam Cohen3bf84d32012-05-07 20:17:14 -0700536 // This shouldn't happen, log it.
537 Log.e(TAG, "Folder order not properly persisted during bind");
Adam Cohenc508b2d2011-06-28 14:41:44 -0700538 if (!findAndSetEmptyCells(item)) {
539 return false;
540 }
541 }
542
Adam Cohendf2cc412011-04-27 16:56:57 -0700543 CellLayout.LayoutParams lp =
544 new CellLayout.LayoutParams(item.cellX, item.cellY, item.spanX, item.spanY);
545 boolean insert = false;
Adam Cohenac56cff2011-09-28 20:45:37 -0700546 textView.setOnKeyListener(new FolderKeyEventListener());
Adam Cohendf2cc412011-04-27 16:56:57 -0700547 mContent.addViewToCellLayout(textView, insert ? 0 : -1, (int)item.id, lp, true);
Adam Cohenc508b2d2011-06-28 14:41:44 -0700548 return true;
Adam Cohendf2cc412011-04-27 16:56:57 -0700549 }
550
Adam Cohencb3382b2011-05-24 14:07:08 -0700551 public void onDragEnter(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700552 mPreviousTargetCell[0] = -1;
553 mPreviousTargetCell[1] = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700554 mOnExitAlarm.cancelAlarm();
555 }
556
557 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
558 public void onAlarm(Alarm alarm) {
559 realTimeReorder(mEmptyCell, mTargetCell);
560 }
561 };
562
563 boolean readingOrderGreaterThan(int[] v1, int[] v2) {
564 if (v1[1] > v2[1] || (v1[1] == v2[1] && v1[0] > v2[0])) {
565 return true;
566 } else {
567 return false;
568 }
569 }
570
571 private void realTimeReorder(int[] empty, int[] target) {
572 boolean wrap;
573 int startX;
574 int endX;
575 int startY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700576 int delay = 0;
577 float delayAmount = 30;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700578 if (readingOrderGreaterThan(target, empty)) {
579 wrap = empty[0] >= mContent.getCountX() - 1;
580 startY = wrap ? empty[1] + 1 : empty[1];
581 for (int y = startY; y <= target[1]; y++) {
582 startX = y == empty[1] ? empty[0] + 1 : 0;
583 endX = y < target[1] ? mContent.getCountX() - 1 : target[0];
584 for (int x = startX; x <= endX; x++) {
585 View v = mContent.getChildAt(x,y);
586 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen482ed822012-03-02 14:15:13 -0800587 REORDER_ANIMATION_DURATION, delay, true, true)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700588 empty[0] = x;
589 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700590 delay += delayAmount;
591 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700592 }
593 }
594 }
595 } else {
596 wrap = empty[0] == 0;
597 startY = wrap ? empty[1] - 1 : empty[1];
598 for (int y = startY; y >= target[1]; y--) {
599 startX = y == empty[1] ? empty[0] - 1 : mContent.getCountX() - 1;
600 endX = y > target[1] ? 0 : target[0];
601 for (int x = startX; x >= endX; x--) {
602 View v = mContent.getChildAt(x,y);
603 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen482ed822012-03-02 14:15:13 -0800604 REORDER_ANIMATION_DURATION, delay, true, true)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700605 empty[0] = x;
606 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700607 delay += delayAmount;
608 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700609 }
610 }
611 }
612 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700613 }
614
Adam Cohen2374abf2013-04-16 14:56:57 -0700615 public boolean isLayoutRtl() {
616 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
617 }
618
Adam Cohencb3382b2011-05-24 14:07:08 -0700619 public void onDragOver(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700620 float[] r = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, d.dragView, null);
621 mTargetCell = mContent.findNearestArea((int) r[0], (int) r[1], 1, 1, mTargetCell);
622
Adam Cohen2374abf2013-04-16 14:56:57 -0700623 if (isLayoutRtl()) {
624 mTargetCell[0] = mContent.getCountX() - mTargetCell[0] - 1;
625 }
626
Adam Cohenbfbfd262011-06-13 16:55:12 -0700627 if (mTargetCell[0] != mPreviousTargetCell[0] || mTargetCell[1] != mPreviousTargetCell[1]) {
628 mReorderAlarm.cancelAlarm();
629 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
630 mReorderAlarm.setAlarm(150);
631 mPreviousTargetCell[0] = mTargetCell[0];
632 mPreviousTargetCell[1] = mTargetCell[1];
633 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700634 }
635
Adam Cohenbfbfd262011-06-13 16:55:12 -0700636 // This is used to compute the visual center of the dragView. The idea is that
637 // the visual center represents the user's interpretation of where the item is, and hence
638 // is the appropriate point to use when determining drop location.
639 private float[] getDragViewVisualCenter(int x, int y, int xOffset, int yOffset,
640 DragView dragView, float[] recycle) {
641 float res[];
642 if (recycle == null) {
643 res = new float[2];
644 } else {
645 res = recycle;
646 }
647
648 // These represent the visual top and left of drag view if a dragRect was provided.
649 // If a dragRect was not provided, then they correspond to the actual view left and
650 // top, as the dragRect is in that case taken to be the entire dragView.
651 // R.dimen.dragViewOffsetY.
652 int left = x - xOffset;
653 int top = y - yOffset;
654
655 // In order to find the visual center, we shift by half the dragRect
656 res[0] = left + dragView.getDragRegion().width() / 2;
657 res[1] = top + dragView.getDragRegion().height() / 2;
658
659 return res;
660 }
661
662 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
663 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700664 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700665 }
666 };
667
Adam Cohen95bb8002011-07-03 23:40:28 -0700668 public void completeDragExit() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700669 mLauncher.closeFolder();
670 mCurrentDragInfo = null;
671 mCurrentDragView = null;
672 mSuppressOnAdd = false;
673 mRearrangeOnClose = true;
674 }
675
Adam Cohencb3382b2011-05-24 14:07:08 -0700676 public void onDragExit(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700677 // We only close the folder if this is a true drag exit, ie. not because a drop
678 // has occurred above the folder.
679 if (!d.dragComplete) {
680 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
681 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
682 }
683 mReorderAlarm.cancelAlarm();
Adam Cohen2801caf2011-05-13 20:57:39 -0700684 }
685
Winson Chunga48487a2012-03-20 16:19:37 -0700686 public void onDropCompleted(View target, DragObject d, boolean isFlingToDelete,
687 boolean success) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700688 if (success) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700689 if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700690 replaceFolderWithFinalItem();
691 }
692 } else {
693 // The drag failed, we need to return the item to the folder
694 mFolderIcon.onDrop(d);
695
696 // We're going to trigger a "closeFolder" which may occur before this item has
697 // been added back to the folder -- this could cause the folder to be deleted
698 if (mOnExitAlarm.alarmPending()) {
699 mSuppressFolderDeletion = true;
700 }
701 }
702
703 if (target != this) {
704 if (mOnExitAlarm.alarmPending()) {
705 mOnExitAlarm.cancelAlarm();
706 completeDragExit();
707 }
708 }
709 mDeleteFolderOnDropCompleted = false;
710 mDragInProgress = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700711 mItemAddedBackToSelfViaIcon = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700712 mCurrentDragInfo = null;
713 mCurrentDragView = null;
714 mSuppressOnAdd = false;
Adam Cohen4045eb72011-10-06 11:44:26 -0700715
716 // Reordering may have occured, and we need to save the new item locations. We do this once
717 // at the end to prevent unnecessary database operations.
718 updateItemLocationsInDatabase();
719 }
720
Winson Chunga48487a2012-03-20 16:19:37 -0700721 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800722 public boolean supportsFlingToDelete() {
723 return true;
724 }
725
Winson Chunga48487a2012-03-20 16:19:37 -0700726 public void onFlingToDelete(DragObject d, int x, int y, PointF vec) {
727 // Do nothing
728 }
729
730 @Override
731 public void onFlingToDeleteCompleted() {
732 // Do nothing
733 }
734
Adam Cohen4045eb72011-10-06 11:44:26 -0700735 private void updateItemLocationsInDatabase() {
736 ArrayList<View> list = getItemsInReadingOrder();
737 for (int i = 0; i < list.size(); i++) {
738 View v = list.get(i);
739 ItemInfo info = (ItemInfo) v.getTag();
740 LauncherModel.moveItemInDatabase(mLauncher, info, mInfo.id, 0,
741 info.cellX, info.cellY);
742 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700743 }
Adam Cohen228da5a2011-07-27 22:23:47 -0700744
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700745 public void notifyDrop() {
746 if (mDragInProgress) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700747 mItemAddedBackToSelfViaIcon = true;
Adam Cohen76078c42011-06-09 15:06:52 -0700748 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700749 }
750
751 public boolean isDropEnabled() {
752 return true;
753 }
754
Adam Cohencb3382b2011-05-24 14:07:08 -0700755 public DropTarget getDropTargetDelegate(DragObject d) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700756 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800757 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700758
Adam Cohen4045eb72011-10-06 11:44:26 -0700759 private void setupContentDimensions(int count) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700760 ArrayList<View> list = getItemsInReadingOrder();
761
762 int countX = mContent.getCountX();
763 int countY = mContent.getCountY();
Adam Cohen7c693212011-05-18 15:26:57 -0700764 boolean done = false;
Adam Cohen2801caf2011-05-13 20:57:39 -0700765
Adam Cohen7c693212011-05-18 15:26:57 -0700766 while (!done) {
767 int oldCountX = countX;
768 int oldCountY = countY;
769 if (countX * countY < count) {
770 // Current grid is too small, expand it
Adam Cohen78dc83e2011-11-15 17:10:00 -0800771 if ((countX <= countY || countY == mMaxCountY) && countX < mMaxCountX) {
Adam Cohen7c693212011-05-18 15:26:57 -0700772 countX++;
773 } else if (countY < mMaxCountY) {
774 countY++;
775 }
776 if (countY == 0) countY++;
777 } else if ((countY - 1) * countX >= count && countY >= countX) {
778 countY = Math.max(0, countY - 1);
779 } else if ((countX - 1) * countY >= count) {
780 countX = Math.max(0, countX - 1);
Adam Cohen2801caf2011-05-13 20:57:39 -0700781 }
Adam Cohen7c693212011-05-18 15:26:57 -0700782 done = countX == oldCountX && countY == oldCountY;
Adam Cohen2801caf2011-05-13 20:57:39 -0700783 }
Adam Cohen7c693212011-05-18 15:26:57 -0700784 mContent.setGridSize(countX, countY);
Adam Cohen2801caf2011-05-13 20:57:39 -0700785 arrangeChildren(list);
786 }
787
788 public boolean isFull() {
Adam Cohen78dc83e2011-11-15 17:10:00 -0800789 return getItemCount() >= mMaxNumItems;
Adam Cohen2801caf2011-05-13 20:57:39 -0700790 }
791
792 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700793 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700794
795 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf4bb1cd2011-07-22 14:36:03 -0700796 int height = getPaddingTop() + getPaddingBottom() + mContent.getDesiredHeight()
797 + mFolderNameHeight;
Adam Cohen8e776a62011-06-28 18:10:06 -0700798 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700799
Adam Cohen307fe232012-08-16 17:55:58 -0700800 float scale = parent.getDescendantRectRelativeToSelf(mFolderIcon, mTempRect);
Adam Cohen8e776a62011-06-28 18:10:06 -0700801
Adam Cohen307fe232012-08-16 17:55:58 -0700802 int centerX = (int) (mTempRect.left + mTempRect.width() * scale / 2);
803 int centerY = (int) (mTempRect.top + mTempRect.height() * scale / 2);
Adam Cohen2801caf2011-05-13 20:57:39 -0700804 int centeredLeft = centerX - width / 2;
805 int centeredTop = centerY - height / 2;
806
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800807 int currentPage = mLauncher.getWorkspace().getCurrentPage();
808 // In case the workspace is scrolling, we need to use the final scroll to compute
809 // the folders bounds.
810 mLauncher.getWorkspace().setFinalScrollForPageChange(currentPage);
Adam Cohen35e7e642011-07-17 14:47:18 -0700811 // We first fetch the currently visible CellLayoutChildren
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800812 CellLayout currentLayout = (CellLayout) mLauncher.getWorkspace().getChildAt(currentPage);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700813 ShortcutAndWidgetContainer boundingLayout = currentLayout.getShortcutsAndWidgets();
Adam Cohen35e7e642011-07-17 14:47:18 -0700814 Rect bounds = new Rect();
815 parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800816 // We reset the workspaces scroll
817 mLauncher.getWorkspace().resetFinalScrollForPageChange(currentPage);
Adam Cohen2801caf2011-05-13 20:57:39 -0700818
Adam Cohen35e7e642011-07-17 14:47:18 -0700819 // We need to bound the folder to the currently visible CellLayoutChildren
820 int left = Math.min(Math.max(bounds.left, centeredLeft),
821 bounds.left + bounds.width() - width);
822 int top = Math.min(Math.max(bounds.top, centeredTop),
823 bounds.top + bounds.height() - height);
824 // If the folder doesn't fit within the bounds, center it about the desired bounds
825 if (width >= bounds.width()) {
826 left = bounds.left + (bounds.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700827 }
Adam Cohen35e7e642011-07-17 14:47:18 -0700828 if (height >= bounds.height()) {
829 top = bounds.top + (bounds.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700830 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700831
832 int folderPivotX = width / 2 + (centeredLeft - left);
833 int folderPivotY = height / 2 + (centeredTop - top);
834 setPivotX(folderPivotX);
835 setPivotY(folderPivotY);
Adam Cohen268c4752012-06-06 17:47:33 -0700836 mFolderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700837 (1.0f * folderPivotX / width));
Adam Cohen268c4752012-06-06 17:47:33 -0700838 mFolderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700839 (1.0f * folderPivotY / height));
Adam Cohen3bf84d32012-05-07 20:17:14 -0700840
Adam Cohen662b5982011-12-13 17:45:21 -0800841 lp.width = width;
842 lp.height = height;
843 lp.x = left;
844 lp.y = top;
Adam Cohen2801caf2011-05-13 20:57:39 -0700845 }
846
Adam Cohen268c4752012-06-06 17:47:33 -0700847 float getPivotXForIconAnimation() {
848 return mFolderIconPivotX;
849 }
850 float getPivotYForIconAnimation() {
851 return mFolderIconPivotY;
852 }
853
Adam Cohen2801caf2011-05-13 20:57:39 -0700854 private void setupContentForNumItems(int count) {
Adam Cohen4045eb72011-10-06 11:44:26 -0700855 setupContentDimensions(count);
Adam Cohen2801caf2011-05-13 20:57:39 -0700856
Adam Cohen8e776a62011-06-28 18:10:06 -0700857 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700858 if (lp == null) {
Adam Cohen8e776a62011-06-28 18:10:06 -0700859 lp = new DragLayer.LayoutParams(0, 0);
860 lp.customPosition = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700861 setLayoutParams(lp);
862 }
863 centerAboutIcon();
864 }
865
Adam Cohen234c4cd2011-07-17 21:03:04 -0700866 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
867 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf4bb1cd2011-07-22 14:36:03 -0700868 int height = getPaddingTop() + getPaddingBottom() + mContent.getDesiredHeight()
869 + mFolderNameHeight;
Adam Cohen234c4cd2011-07-17 21:03:04 -0700870
871 int contentWidthSpec = MeasureSpec.makeMeasureSpec(mContent.getDesiredWidth(),
872 MeasureSpec.EXACTLY);
873 int contentHeightSpec = MeasureSpec.makeMeasureSpec(mContent.getDesiredHeight(),
874 MeasureSpec.EXACTLY);
875 mContent.measure(contentWidthSpec, contentHeightSpec);
876
877 mFolderName.measure(contentWidthSpec,
878 MeasureSpec.makeMeasureSpec(mFolderNameHeight, MeasureSpec.EXACTLY));
879 setMeasuredDimension(width, height);
880 }
881
Adam Cohen2801caf2011-05-13 20:57:39 -0700882 private void arrangeChildren(ArrayList<View> list) {
883 int[] vacant = new int[2];
884 if (list == null) {
885 list = getItemsInReadingOrder();
886 }
887 mContent.removeAllViews();
888
889 for (int i = 0; i < list.size(); i++) {
890 View v = list.get(i);
891 mContent.getVacantCell(vacant, 1, 1);
892 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) v.getLayoutParams();
893 lp.cellX = vacant[0];
894 lp.cellY = vacant[1];
895 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohen2792a332011-09-26 21:09:47 -0700896 if (info.cellX != vacant[0] || info.cellY != vacant[1]) {
897 info.cellX = vacant[0];
898 info.cellY = vacant[1];
899 LauncherModel.addOrMoveItemInDatabase(mLauncher, info, mInfo.id, 0,
900 info.cellX, info.cellY);
901 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700902 boolean insert = false;
903 mContent.addViewToCellLayout(v, insert ? 0 : -1, (int)info.id, lp, true);
904 }
Adam Cohen7c693212011-05-18 15:26:57 -0700905 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700906 }
907
Adam Cohena9cf38f2011-05-02 15:36:58 -0700908 public int getItemCount() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700909 return mContent.getShortcutsAndWidgets().getChildCount();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700910 }
911
912 public View getItemAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700913 return mContent.getShortcutsAndWidgets().getChildAt(index);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700914 }
915
Adam Cohen2801caf2011-05-13 20:57:39 -0700916 private void onCloseComplete() {
Adam Cohen05e0f402011-08-01 12:12:49 -0700917 DragLayer parent = (DragLayer) getParent();
Michael Jurka5649c282012-06-18 10:33:21 -0700918 if (parent != null) {
919 parent.removeView(this);
920 }
Adam Cohen4554ee12011-08-03 16:13:21 -0700921 mDragController.removeDropTarget((DropTarget) this);
Adam Cohen05e0f402011-08-01 12:12:49 -0700922 clearFocus();
Adam Cohenac56cff2011-09-28 20:45:37 -0700923 mFolderIcon.requestFocus();
Adam Cohen05e0f402011-08-01 12:12:49 -0700924
Adam Cohen2801caf2011-05-13 20:57:39 -0700925 if (mRearrangeOnClose) {
926 setupContentForNumItems(getItemCount());
927 mRearrangeOnClose = false;
928 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700929 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700930 if (!mDragInProgress && !mSuppressFolderDeletion) {
931 replaceFolderWithFinalItem();
932 } else if (mDragInProgress) {
933 mDeleteFolderOnDropCompleted = true;
934 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700935 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700936 mSuppressFolderDeletion = false;
Adam Cohenafb01ee2011-06-23 15:38:03 -0700937 }
938
939 private void replaceFolderWithFinalItem() {
Adam Cohenafb01ee2011-06-23 15:38:03 -0700940 // Add the last remaining child to the workspace in place of the folder
Adam Cohenfb91f302012-06-11 15:45:18 -0700941 Runnable onCompleteRunnable = new Runnable() {
942 @Override
943 public void run() {
944 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screen);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700945
Adam Cohenc5e63f32012-07-12 16:16:57 -0700946 View child = null;
Adam Cohenfb91f302012-06-11 15:45:18 -0700947 // Move the item from the folder to the workspace, in the position of the folder
948 if (getItemCount() == 1) {
949 ShortcutInfo finalItem = mInfo.contents.get(0);
Adam Cohenc5e63f32012-07-12 16:16:57 -0700950 child = mLauncher.createShortcut(R.layout.application, cellLayout,
Adam Cohenfb91f302012-06-11 15:45:18 -0700951 finalItem);
952 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
953 mInfo.screen, mInfo.cellX, mInfo.cellY);
Adam Cohenfb91f302012-06-11 15:45:18 -0700954 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700955 if (getItemCount() <= 1) {
956 // Remove the folder
957 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
958 cellLayout.removeView(mFolderIcon);
959 if (mFolderIcon instanceof DropTarget) {
960 mDragController.removeDropTarget((DropTarget) mFolderIcon);
961 }
962 mLauncher.removeFolder(mInfo);
963 }
Adam Cohenc5e63f32012-07-12 16:16:57 -0700964 // We add the child after removing the folder to prevent both from existing at
965 // the same time in the CellLayout.
966 if (child != null) {
967 mLauncher.getWorkspace().addInScreen(child, mInfo.container, mInfo.screen,
968 mInfo.cellX, mInfo.cellY, mInfo.spanX, mInfo.spanY);
969 }
Adam Cohenfb91f302012-06-11 15:45:18 -0700970 }
971 };
972 View finalChild = getItemAt(0);
973 if (finalChild != null) {
974 mFolderIcon.performDestroyAnimation(finalChild, onCompleteRunnable);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700975 }
Adam Cohenfb91f302012-06-11 15:45:18 -0700976 mDestroyed = true;
977 }
978
979 boolean isDestroyed() {
980 return mDestroyed;
Adam Cohen2801caf2011-05-13 20:57:39 -0700981 }
982
Adam Cohenac56cff2011-09-28 20:45:37 -0700983 // This method keeps track of the last item in the folder for the purposes
984 // of keyboard focus
985 private void updateTextViewFocus() {
986 View lastChild = getItemAt(getItemCount() - 1);
987 getItemAt(getItemCount() - 1);
988 if (lastChild != null) {
989 mFolderName.setNextFocusDownId(lastChild.getId());
990 mFolderName.setNextFocusRightId(lastChild.getId());
991 mFolderName.setNextFocusLeftId(lastChild.getId());
992 mFolderName.setNextFocusUpId(lastChild.getId());
993 }
994 }
995
Adam Cohenbfbfd262011-06-13 16:55:12 -0700996 public void onDrop(DragObject d) {
997 ShortcutInfo item;
998 if (d.dragInfo instanceof ApplicationInfo) {
999 // Came from all apps -- make a copy
1000 item = ((ApplicationInfo) d.dragInfo).makeShortcut();
1001 item.spanX = 1;
1002 item.spanY = 1;
1003 } else {
1004 item = (ShortcutInfo) d.dragInfo;
1005 }
Adam Cohen05e0f402011-08-01 12:12:49 -07001006 // Dragged from self onto self, currently this is the only path possible, however
1007 // we keep this as a distinct code path.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001008 if (item == mCurrentDragInfo) {
1009 ShortcutInfo si = (ShortcutInfo) mCurrentDragView.getTag();
1010 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) mCurrentDragView.getLayoutParams();
1011 si.cellX = lp.cellX = mEmptyCell[0];
1012 si.cellX = lp.cellY = mEmptyCell[1];
1013 mContent.addViewToCellLayout(mCurrentDragView, -1, (int)item.id, lp, true);
Adam Cohenfc53cd22011-07-20 15:45:11 -07001014 if (d.dragView.hasDrawn()) {
1015 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, mCurrentDragView);
1016 } else {
Winson Chung2c4cf412012-05-08 14:03:21 -07001017 d.deferDragViewCleanupPostAnimation = false;
Adam Cohenfc53cd22011-07-20 15:45:11 -07001018 mCurrentDragView.setVisibility(VISIBLE);
1019 }
Adam Cohene9166b22011-07-08 17:11:11 -07001020 mItemsInvalidated = true;
Adam Cohen4045eb72011-10-06 11:44:26 -07001021 setupContentDimensions(getItemCount());
Adam Cohen716b51e2011-06-30 12:09:54 -07001022 mSuppressOnAdd = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001023 }
1024 mInfo.add(item);
1025 }
1026
1027 public void onAdd(ShortcutInfo item) {
1028 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001029 // If the item was dropped onto this open folder, we have done the work associated
1030 // with adding the item to the folder, as indicated by mSuppressOnAdd being set
Adam Cohenbfbfd262011-06-13 16:55:12 -07001031 if (mSuppressOnAdd) return;
1032 if (!findAndSetEmptyCells(item)) {
1033 // The current layout is full, can we expand it?
1034 setupContentForNumItems(getItemCount() + 1);
1035 findAndSetEmptyCells(item);
1036 }
1037 createAndAddShortcut(item);
1038 LauncherModel.addOrMoveItemInDatabase(
1039 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
1040 }
1041
Adam Cohena9cf38f2011-05-02 15:36:58 -07001042 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -07001043 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001044 // If this item is being dragged from this open folder, we have already handled
1045 // the work associated with removing the item, so we don't have to do anything here.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001046 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001047 View v = getViewForInfo(item);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001048 mContent.removeView(v);
Adam Cohen2801caf2011-05-13 20:57:39 -07001049 if (mState == STATE_ANIMATING) {
1050 mRearrangeOnClose = true;
1051 } else {
1052 setupContentForNumItems(getItemCount());
1053 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001054 if (getItemCount() <= 1) {
1055 replaceFolderWithFinalItem();
1056 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001057 }
Adam Cohen7c693212011-05-18 15:26:57 -07001058
Adam Cohendf1e4e82011-06-24 15:57:39 -07001059 private View getViewForInfo(ShortcutInfo item) {
1060 for (int j = 0; j < mContent.getCountY(); j++) {
1061 for (int i = 0; i < mContent.getCountX(); i++) {
1062 View v = mContent.getChildAt(i, j);
1063 if (v.getTag() == item) {
1064 return v;
1065 }
1066 }
1067 }
1068 return null;
1069 }
1070
Adam Cohen76078c42011-06-09 15:06:52 -07001071 public void onItemsChanged() {
Adam Cohenac56cff2011-09-28 20:45:37 -07001072 updateTextViewFocus();
Adam Cohen76078c42011-06-09 15:06:52 -07001073 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001074
Adam Cohen76fc0852011-06-17 13:26:23 -07001075 public void onTitleChanged(CharSequence title) {
1076 }
Adam Cohen76078c42011-06-09 15:06:52 -07001077
Adam Cohen7c693212011-05-18 15:26:57 -07001078 public ArrayList<View> getItemsInReadingOrder() {
Adam Cohen76078c42011-06-09 15:06:52 -07001079 return getItemsInReadingOrder(true);
1080 }
1081
1082 public ArrayList<View> getItemsInReadingOrder(boolean includeCurrentDragItem) {
Adam Cohen7c693212011-05-18 15:26:57 -07001083 if (mItemsInvalidated) {
1084 mItemsInReadingOrder.clear();
1085 for (int j = 0; j < mContent.getCountY(); j++) {
1086 for (int i = 0; i < mContent.getCountX(); i++) {
1087 View v = mContent.getChildAt(i, j);
1088 if (v != null) {
Adam Cohen76078c42011-06-09 15:06:52 -07001089 ShortcutInfo info = (ShortcutInfo) v.getTag();
1090 if (info != mCurrentDragInfo || includeCurrentDragItem) {
1091 mItemsInReadingOrder.add(v);
1092 }
Adam Cohen7c693212011-05-18 15:26:57 -07001093 }
1094 }
1095 }
1096 mItemsInvalidated = false;
1097 }
1098 return mItemsInReadingOrder;
1099 }
Adam Cohen8dfcba42011-07-07 16:38:18 -07001100
1101 public void getLocationInDragLayer(int[] loc) {
1102 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
1103 }
Adam Cohenea0818d2011-09-30 20:06:58 -07001104
1105 public void onFocusChange(View v, boolean hasFocus) {
1106 if (v == mFolderName && hasFocus) {
1107 startEditingFolderName();
1108 }
1109 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001110}