blob: ee150088b176239bc0fc5bc183242b3663274dd4 [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 {
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700693 setupContentForNumItems(getItemCount());
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700694 // The drag failed, we need to return the item to the folder
695 mFolderIcon.onDrop(d);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700696 }
697
698 if (target != this) {
699 if (mOnExitAlarm.alarmPending()) {
700 mOnExitAlarm.cancelAlarm();
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700701 if (!success) {
702 mSuppressFolderDeletion = true;
703 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700704 completeDragExit();
705 }
706 }
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700707
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700708 mDeleteFolderOnDropCompleted = false;
709 mDragInProgress = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700710 mItemAddedBackToSelfViaIcon = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700711 mCurrentDragInfo = null;
712 mCurrentDragView = null;
713 mSuppressOnAdd = false;
Adam Cohen4045eb72011-10-06 11:44:26 -0700714
715 // Reordering may have occured, and we need to save the new item locations. We do this once
716 // at the end to prevent unnecessary database operations.
717 updateItemLocationsInDatabase();
718 }
719
Winson Chunga48487a2012-03-20 16:19:37 -0700720 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800721 public boolean supportsFlingToDelete() {
722 return true;
723 }
724
Winson Chunga48487a2012-03-20 16:19:37 -0700725 public void onFlingToDelete(DragObject d, int x, int y, PointF vec) {
726 // Do nothing
727 }
728
729 @Override
730 public void onFlingToDeleteCompleted() {
731 // Do nothing
732 }
733
Adam Cohen4045eb72011-10-06 11:44:26 -0700734 private void updateItemLocationsInDatabase() {
735 ArrayList<View> list = getItemsInReadingOrder();
736 for (int i = 0; i < list.size(); i++) {
737 View v = list.get(i);
738 ItemInfo info = (ItemInfo) v.getTag();
739 LauncherModel.moveItemInDatabase(mLauncher, info, mInfo.id, 0,
740 info.cellX, info.cellY);
741 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700742 }
Adam Cohen228da5a2011-07-27 22:23:47 -0700743
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700744 public void notifyDrop() {
745 if (mDragInProgress) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700746 mItemAddedBackToSelfViaIcon = true;
Adam Cohen76078c42011-06-09 15:06:52 -0700747 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700748 }
749
750 public boolean isDropEnabled() {
751 return true;
752 }
753
Adam Cohencb3382b2011-05-24 14:07:08 -0700754 public DropTarget getDropTargetDelegate(DragObject d) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700755 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800756 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700757
Adam Cohen4045eb72011-10-06 11:44:26 -0700758 private void setupContentDimensions(int count) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700759 ArrayList<View> list = getItemsInReadingOrder();
760
761 int countX = mContent.getCountX();
762 int countY = mContent.getCountY();
Adam Cohen7c693212011-05-18 15:26:57 -0700763 boolean done = false;
Adam Cohen2801caf2011-05-13 20:57:39 -0700764
Adam Cohen7c693212011-05-18 15:26:57 -0700765 while (!done) {
766 int oldCountX = countX;
767 int oldCountY = countY;
768 if (countX * countY < count) {
769 // Current grid is too small, expand it
Adam Cohen78dc83e2011-11-15 17:10:00 -0800770 if ((countX <= countY || countY == mMaxCountY) && countX < mMaxCountX) {
Adam Cohen7c693212011-05-18 15:26:57 -0700771 countX++;
772 } else if (countY < mMaxCountY) {
773 countY++;
774 }
775 if (countY == 0) countY++;
776 } else if ((countY - 1) * countX >= count && countY >= countX) {
777 countY = Math.max(0, countY - 1);
778 } else if ((countX - 1) * countY >= count) {
779 countX = Math.max(0, countX - 1);
Adam Cohen2801caf2011-05-13 20:57:39 -0700780 }
Adam Cohen7c693212011-05-18 15:26:57 -0700781 done = countX == oldCountX && countY == oldCountY;
Adam Cohen2801caf2011-05-13 20:57:39 -0700782 }
Adam Cohen7c693212011-05-18 15:26:57 -0700783 mContent.setGridSize(countX, countY);
Adam Cohen2801caf2011-05-13 20:57:39 -0700784 arrangeChildren(list);
785 }
786
787 public boolean isFull() {
Adam Cohen78dc83e2011-11-15 17:10:00 -0800788 return getItemCount() >= mMaxNumItems;
Adam Cohen2801caf2011-05-13 20:57:39 -0700789 }
790
791 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700792 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700793
794 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf4bb1cd2011-07-22 14:36:03 -0700795 int height = getPaddingTop() + getPaddingBottom() + mContent.getDesiredHeight()
796 + mFolderNameHeight;
Adam Cohen8e776a62011-06-28 18:10:06 -0700797 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700798
Adam Cohen307fe232012-08-16 17:55:58 -0700799 float scale = parent.getDescendantRectRelativeToSelf(mFolderIcon, mTempRect);
Adam Cohen8e776a62011-06-28 18:10:06 -0700800
Adam Cohen307fe232012-08-16 17:55:58 -0700801 int centerX = (int) (mTempRect.left + mTempRect.width() * scale / 2);
802 int centerY = (int) (mTempRect.top + mTempRect.height() * scale / 2);
Adam Cohen2801caf2011-05-13 20:57:39 -0700803 int centeredLeft = centerX - width / 2;
804 int centeredTop = centerY - height / 2;
805
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800806 int currentPage = mLauncher.getWorkspace().getCurrentPage();
807 // In case the workspace is scrolling, we need to use the final scroll to compute
808 // the folders bounds.
809 mLauncher.getWorkspace().setFinalScrollForPageChange(currentPage);
Adam Cohen35e7e642011-07-17 14:47:18 -0700810 // We first fetch the currently visible CellLayoutChildren
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800811 CellLayout currentLayout = (CellLayout) mLauncher.getWorkspace().getChildAt(currentPage);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700812 ShortcutAndWidgetContainer boundingLayout = currentLayout.getShortcutsAndWidgets();
Adam Cohen35e7e642011-07-17 14:47:18 -0700813 Rect bounds = new Rect();
814 parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800815 // We reset the workspaces scroll
816 mLauncher.getWorkspace().resetFinalScrollForPageChange(currentPage);
Adam Cohen2801caf2011-05-13 20:57:39 -0700817
Adam Cohen35e7e642011-07-17 14:47:18 -0700818 // We need to bound the folder to the currently visible CellLayoutChildren
819 int left = Math.min(Math.max(bounds.left, centeredLeft),
820 bounds.left + bounds.width() - width);
821 int top = Math.min(Math.max(bounds.top, centeredTop),
822 bounds.top + bounds.height() - height);
823 // If the folder doesn't fit within the bounds, center it about the desired bounds
824 if (width >= bounds.width()) {
825 left = bounds.left + (bounds.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700826 }
Adam Cohen35e7e642011-07-17 14:47:18 -0700827 if (height >= bounds.height()) {
828 top = bounds.top + (bounds.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700829 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700830
831 int folderPivotX = width / 2 + (centeredLeft - left);
832 int folderPivotY = height / 2 + (centeredTop - top);
833 setPivotX(folderPivotX);
834 setPivotY(folderPivotY);
Adam Cohen268c4752012-06-06 17:47:33 -0700835 mFolderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700836 (1.0f * folderPivotX / width));
Adam Cohen268c4752012-06-06 17:47:33 -0700837 mFolderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700838 (1.0f * folderPivotY / height));
Adam Cohen3bf84d32012-05-07 20:17:14 -0700839
Adam Cohen662b5982011-12-13 17:45:21 -0800840 lp.width = width;
841 lp.height = height;
842 lp.x = left;
843 lp.y = top;
Adam Cohen2801caf2011-05-13 20:57:39 -0700844 }
845
Adam Cohen268c4752012-06-06 17:47:33 -0700846 float getPivotXForIconAnimation() {
847 return mFolderIconPivotX;
848 }
849 float getPivotYForIconAnimation() {
850 return mFolderIconPivotY;
851 }
852
Adam Cohen2801caf2011-05-13 20:57:39 -0700853 private void setupContentForNumItems(int count) {
Adam Cohen4045eb72011-10-06 11:44:26 -0700854 setupContentDimensions(count);
Adam Cohen2801caf2011-05-13 20:57:39 -0700855
Adam Cohen8e776a62011-06-28 18:10:06 -0700856 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700857 if (lp == null) {
Adam Cohen8e776a62011-06-28 18:10:06 -0700858 lp = new DragLayer.LayoutParams(0, 0);
859 lp.customPosition = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700860 setLayoutParams(lp);
861 }
862 centerAboutIcon();
863 }
864
Adam Cohen234c4cd2011-07-17 21:03:04 -0700865 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
866 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf4bb1cd2011-07-22 14:36:03 -0700867 int height = getPaddingTop() + getPaddingBottom() + mContent.getDesiredHeight()
868 + mFolderNameHeight;
Adam Cohen234c4cd2011-07-17 21:03:04 -0700869
870 int contentWidthSpec = MeasureSpec.makeMeasureSpec(mContent.getDesiredWidth(),
871 MeasureSpec.EXACTLY);
872 int contentHeightSpec = MeasureSpec.makeMeasureSpec(mContent.getDesiredHeight(),
873 MeasureSpec.EXACTLY);
874 mContent.measure(contentWidthSpec, contentHeightSpec);
875
876 mFolderName.measure(contentWidthSpec,
877 MeasureSpec.makeMeasureSpec(mFolderNameHeight, MeasureSpec.EXACTLY));
878 setMeasuredDimension(width, height);
879 }
880
Adam Cohen2801caf2011-05-13 20:57:39 -0700881 private void arrangeChildren(ArrayList<View> list) {
882 int[] vacant = new int[2];
883 if (list == null) {
884 list = getItemsInReadingOrder();
885 }
886 mContent.removeAllViews();
887
888 for (int i = 0; i < list.size(); i++) {
889 View v = list.get(i);
890 mContent.getVacantCell(vacant, 1, 1);
891 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) v.getLayoutParams();
892 lp.cellX = vacant[0];
893 lp.cellY = vacant[1];
894 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohen2792a332011-09-26 21:09:47 -0700895 if (info.cellX != vacant[0] || info.cellY != vacant[1]) {
896 info.cellX = vacant[0];
897 info.cellY = vacant[1];
898 LauncherModel.addOrMoveItemInDatabase(mLauncher, info, mInfo.id, 0,
899 info.cellX, info.cellY);
900 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700901 boolean insert = false;
902 mContent.addViewToCellLayout(v, insert ? 0 : -1, (int)info.id, lp, true);
903 }
Adam Cohen7c693212011-05-18 15:26:57 -0700904 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700905 }
906
Adam Cohena9cf38f2011-05-02 15:36:58 -0700907 public int getItemCount() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700908 return mContent.getShortcutsAndWidgets().getChildCount();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700909 }
910
911 public View getItemAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700912 return mContent.getShortcutsAndWidgets().getChildAt(index);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700913 }
914
Adam Cohen2801caf2011-05-13 20:57:39 -0700915 private void onCloseComplete() {
Adam Cohen05e0f402011-08-01 12:12:49 -0700916 DragLayer parent = (DragLayer) getParent();
Michael Jurka5649c282012-06-18 10:33:21 -0700917 if (parent != null) {
918 parent.removeView(this);
919 }
Adam Cohen4554ee12011-08-03 16:13:21 -0700920 mDragController.removeDropTarget((DropTarget) this);
Adam Cohen05e0f402011-08-01 12:12:49 -0700921 clearFocus();
Adam Cohenac56cff2011-09-28 20:45:37 -0700922 mFolderIcon.requestFocus();
Adam Cohen05e0f402011-08-01 12:12:49 -0700923
Adam Cohen2801caf2011-05-13 20:57:39 -0700924 if (mRearrangeOnClose) {
925 setupContentForNumItems(getItemCount());
926 mRearrangeOnClose = false;
927 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700928 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700929 if (!mDragInProgress && !mSuppressFolderDeletion) {
930 replaceFolderWithFinalItem();
931 } else if (mDragInProgress) {
932 mDeleteFolderOnDropCompleted = true;
933 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700934 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700935 mSuppressFolderDeletion = false;
Adam Cohenafb01ee2011-06-23 15:38:03 -0700936 }
937
938 private void replaceFolderWithFinalItem() {
Adam Cohenafb01ee2011-06-23 15:38:03 -0700939 // Add the last remaining child to the workspace in place of the folder
Adam Cohenfb91f302012-06-11 15:45:18 -0700940 Runnable onCompleteRunnable = new Runnable() {
941 @Override
942 public void run() {
943 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screen);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700944
Adam Cohenc5e63f32012-07-12 16:16:57 -0700945 View child = null;
Adam Cohenfb91f302012-06-11 15:45:18 -0700946 // Move the item from the folder to the workspace, in the position of the folder
947 if (getItemCount() == 1) {
948 ShortcutInfo finalItem = mInfo.contents.get(0);
Adam Cohenc5e63f32012-07-12 16:16:57 -0700949 child = mLauncher.createShortcut(R.layout.application, cellLayout,
Adam Cohenfb91f302012-06-11 15:45:18 -0700950 finalItem);
951 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
952 mInfo.screen, mInfo.cellX, mInfo.cellY);
Adam Cohenfb91f302012-06-11 15:45:18 -0700953 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700954 if (getItemCount() <= 1) {
955 // Remove the folder
956 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
957 cellLayout.removeView(mFolderIcon);
958 if (mFolderIcon instanceof DropTarget) {
959 mDragController.removeDropTarget((DropTarget) mFolderIcon);
960 }
961 mLauncher.removeFolder(mInfo);
962 }
Adam Cohenc5e63f32012-07-12 16:16:57 -0700963 // We add the child after removing the folder to prevent both from existing at
964 // the same time in the CellLayout.
965 if (child != null) {
966 mLauncher.getWorkspace().addInScreen(child, mInfo.container, mInfo.screen,
967 mInfo.cellX, mInfo.cellY, mInfo.spanX, mInfo.spanY);
968 }
Adam Cohenfb91f302012-06-11 15:45:18 -0700969 }
970 };
971 View finalChild = getItemAt(0);
972 if (finalChild != null) {
973 mFolderIcon.performDestroyAnimation(finalChild, onCompleteRunnable);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700974 }
Adam Cohenfb91f302012-06-11 15:45:18 -0700975 mDestroyed = true;
976 }
977
978 boolean isDestroyed() {
979 return mDestroyed;
Adam Cohen2801caf2011-05-13 20:57:39 -0700980 }
981
Adam Cohenac56cff2011-09-28 20:45:37 -0700982 // This method keeps track of the last item in the folder for the purposes
983 // of keyboard focus
984 private void updateTextViewFocus() {
985 View lastChild = getItemAt(getItemCount() - 1);
986 getItemAt(getItemCount() - 1);
987 if (lastChild != null) {
988 mFolderName.setNextFocusDownId(lastChild.getId());
989 mFolderName.setNextFocusRightId(lastChild.getId());
990 mFolderName.setNextFocusLeftId(lastChild.getId());
991 mFolderName.setNextFocusUpId(lastChild.getId());
992 }
993 }
994
Adam Cohenbfbfd262011-06-13 16:55:12 -0700995 public void onDrop(DragObject d) {
996 ShortcutInfo item;
997 if (d.dragInfo instanceof ApplicationInfo) {
998 // Came from all apps -- make a copy
999 item = ((ApplicationInfo) d.dragInfo).makeShortcut();
1000 item.spanX = 1;
1001 item.spanY = 1;
1002 } else {
1003 item = (ShortcutInfo) d.dragInfo;
1004 }
Adam Cohen05e0f402011-08-01 12:12:49 -07001005 // Dragged from self onto self, currently this is the only path possible, however
1006 // we keep this as a distinct code path.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001007 if (item == mCurrentDragInfo) {
1008 ShortcutInfo si = (ShortcutInfo) mCurrentDragView.getTag();
1009 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) mCurrentDragView.getLayoutParams();
1010 si.cellX = lp.cellX = mEmptyCell[0];
1011 si.cellX = lp.cellY = mEmptyCell[1];
1012 mContent.addViewToCellLayout(mCurrentDragView, -1, (int)item.id, lp, true);
Adam Cohenfc53cd22011-07-20 15:45:11 -07001013 if (d.dragView.hasDrawn()) {
1014 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, mCurrentDragView);
1015 } else {
Winson Chung2c4cf412012-05-08 14:03:21 -07001016 d.deferDragViewCleanupPostAnimation = false;
Adam Cohenfc53cd22011-07-20 15:45:11 -07001017 mCurrentDragView.setVisibility(VISIBLE);
1018 }
Adam Cohene9166b22011-07-08 17:11:11 -07001019 mItemsInvalidated = true;
Adam Cohen4045eb72011-10-06 11:44:26 -07001020 setupContentDimensions(getItemCount());
Adam Cohen716b51e2011-06-30 12:09:54 -07001021 mSuppressOnAdd = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001022 }
1023 mInfo.add(item);
1024 }
1025
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001026 // This is used so the item doesn't immediately appear in the folder when added. In one case
1027 // we need to create the illusion that the item isn't added back to the folder yet, to
1028 // to correspond to the animation of the icon back into the folder. This is
1029 public void hideItem(ShortcutInfo info) {
1030 View v = getViewForInfo(info);
1031 v.setVisibility(INVISIBLE);
1032 }
1033 public void showItem(ShortcutInfo info) {
1034 View v = getViewForInfo(info);
1035 v.setVisibility(VISIBLE);
1036 }
1037
Adam Cohenbfbfd262011-06-13 16:55:12 -07001038 public void onAdd(ShortcutInfo item) {
1039 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001040 // If the item was dropped onto this open folder, we have done the work associated
1041 // with adding the item to the folder, as indicated by mSuppressOnAdd being set
Adam Cohenbfbfd262011-06-13 16:55:12 -07001042 if (mSuppressOnAdd) return;
1043 if (!findAndSetEmptyCells(item)) {
1044 // The current layout is full, can we expand it?
1045 setupContentForNumItems(getItemCount() + 1);
1046 findAndSetEmptyCells(item);
1047 }
1048 createAndAddShortcut(item);
1049 LauncherModel.addOrMoveItemInDatabase(
1050 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
1051 }
1052
Adam Cohena9cf38f2011-05-02 15:36:58 -07001053 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -07001054 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001055 // If this item is being dragged from this open folder, we have already handled
1056 // the work associated with removing the item, so we don't have to do anything here.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001057 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001058 View v = getViewForInfo(item);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001059 mContent.removeView(v);
Adam Cohen2801caf2011-05-13 20:57:39 -07001060 if (mState == STATE_ANIMATING) {
1061 mRearrangeOnClose = true;
1062 } else {
1063 setupContentForNumItems(getItemCount());
1064 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001065 if (getItemCount() <= 1) {
1066 replaceFolderWithFinalItem();
1067 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001068 }
Adam Cohen7c693212011-05-18 15:26:57 -07001069
Adam Cohendf1e4e82011-06-24 15:57:39 -07001070 private View getViewForInfo(ShortcutInfo item) {
1071 for (int j = 0; j < mContent.getCountY(); j++) {
1072 for (int i = 0; i < mContent.getCountX(); i++) {
1073 View v = mContent.getChildAt(i, j);
1074 if (v.getTag() == item) {
1075 return v;
1076 }
1077 }
1078 }
1079 return null;
1080 }
1081
Adam Cohen76078c42011-06-09 15:06:52 -07001082 public void onItemsChanged() {
Adam Cohenac56cff2011-09-28 20:45:37 -07001083 updateTextViewFocus();
Adam Cohen76078c42011-06-09 15:06:52 -07001084 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001085
Adam Cohen76fc0852011-06-17 13:26:23 -07001086 public void onTitleChanged(CharSequence title) {
1087 }
Adam Cohen76078c42011-06-09 15:06:52 -07001088
Adam Cohen7c693212011-05-18 15:26:57 -07001089 public ArrayList<View> getItemsInReadingOrder() {
1090 if (mItemsInvalidated) {
1091 mItemsInReadingOrder.clear();
1092 for (int j = 0; j < mContent.getCountY(); j++) {
1093 for (int i = 0; i < mContent.getCountX(); i++) {
1094 View v = mContent.getChildAt(i, j);
1095 if (v != null) {
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001096 mItemsInReadingOrder.add(v);
Adam Cohen7c693212011-05-18 15:26:57 -07001097 }
1098 }
1099 }
1100 mItemsInvalidated = false;
1101 }
1102 return mItemsInReadingOrder;
1103 }
Adam Cohen8dfcba42011-07-07 16:38:18 -07001104
1105 public void getLocationInDragLayer(int[] loc) {
1106 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
1107 }
Adam Cohenea0818d2011-09-30 20:06:58 -07001108
1109 public void onFocusChange(View v, boolean hasFocus) {
1110 if (v == mFolderName && hasFocus) {
1111 startEditingFolderName();
1112 }
1113 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001114}