blob: 9637bbb48d43c8d1f351a05a08e65183572e25bf [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
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
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;
Alan Viverette4cda5b72013-08-28 17:53:41 -070028import android.os.SystemClock;
Bjorn Bringertb66773c2013-09-25 16:12:24 +010029import android.support.v4.widget.AutoScrollHelper;
Adam Cohen7a14d0b2011-06-24 11:36:35 -070030import android.text.InputType;
Adam Cohene601a432011-07-26 21:51:30 -070031import android.text.Selection;
32import android.text.Spannable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.util.AttributeSet;
Winson Chung892c74d2013-08-22 16:15:50 -070034import android.util.DisplayMetrics;
Adam Cohen3bf84d32012-05-07 20:17:14 -070035import android.util.Log;
Winson Chung892c74d2013-08-22 16:15:50 -070036import android.util.TypedValue;
Adam Cohen76fc0852011-06-17 13:26:23 -070037import android.view.ActionMode;
38import android.view.KeyEvent;
Adam Cohendf2cc412011-04-27 16:56:57 -070039import android.view.LayoutInflater;
Adam Cohen76fc0852011-06-17 13:26:23 -070040import android.view.Menu;
41import android.view.MenuItem;
Adam Cohen0c872ba2011-05-05 10:34:16 -070042import android.view.MotionEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.view.View;
Adam Cohen3371da02011-10-25 21:38:29 -070044import android.view.accessibility.AccessibilityEvent;
45import android.view.accessibility.AccessibilityManager;
Mindy DelliCarpini2e7471c2013-06-27 08:45:11 -070046import android.view.animation.AccelerateInterpolator;
47import android.view.animation.Interpolator;
Adam Cohen76fc0852011-06-17 13:26:23 -070048import android.view.inputmethod.EditorInfo;
49import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.widget.LinearLayout;
Adam Cohenf0f4eda2013-06-06 21:27:03 -070051import android.widget.ScrollView;
Adam Cohendf2cc412011-04-27 16:56:57 -070052import android.widget.TextView;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053
Daniel Sandler325dc232013-06-05 22:57:57 -040054import com.android.launcher3.FolderInfo.FolderListener;
Romain Guyedcce092010-03-04 13:03:17 -080055
Adam Cohenc0dcf592011-06-01 15:30:43 -070056import java.util.ArrayList;
Adam Cohen3bf84d32012-05-07 20:17:14 -070057import java.util.Collections;
58import java.util.Comparator;
Adam Cohenc0dcf592011-06-01 15:30:43 -070059
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060/**
61 * Represents a set of icons chosen by the user or generated by the system.
62 */
Adam Cohen8dfcba42011-07-07 16:38:18 -070063public class Folder extends LinearLayout implements DragSource, View.OnClickListener,
Adam Cohenea0818d2011-09-30 20:06:58 -070064 View.OnLongClickListener, DropTarget, FolderListener, TextView.OnEditorActionListener,
65 View.OnFocusChangeListener {
Adam Cohendf2cc412011-04-27 16:56:57 -070066 private static final String TAG = "Launcher.Folder";
67
Adam Cohen4eac29a2011-07-11 17:53:37 -070068 protected DragController mDragController;
69 protected Launcher mLauncher;
70 protected FolderInfo mInfo;
71
Adam Cohendf2cc412011-04-27 16:56:57 -070072 static final int STATE_NONE = -1;
73 static final int STATE_SMALL = 0;
74 static final int STATE_ANIMATING = 1;
75 static final int STATE_OPEN = 2;
76
77 private int mExpandDuration;
78 protected CellLayout mContent;
Adam Cohenf0f4eda2013-06-06 21:27:03 -070079 private ScrollView mScrollView;
Adam Cohendf2cc412011-04-27 16:56:57 -070080 private final LayoutInflater mInflater;
81 private final IconCache mIconCache;
82 private int mState = STATE_NONE;
Adam Cohenbfbfd262011-06-13 16:55:12 -070083 private static final int REORDER_ANIMATION_DURATION = 230;
Adam Cohenf0f4eda2013-06-06 21:27:03 -070084 private static final int REORDER_DELAY = 250;
Adam Cohen5d518fa2013-12-05 14:16:23 -080085 private static final int ON_EXIT_CLOSE_DELAY = 400;
Adam Cohen2801caf2011-05-13 20:57:39 -070086 private boolean mRearrangeOnClose = false;
87 private FolderIcon mFolderIcon;
88 private int mMaxCountX;
89 private int mMaxCountY;
Adam Cohen78dc83e2011-11-15 17:10:00 -080090 private int mMaxNumItems;
Adam Cohen7c693212011-05-18 15:26:57 -070091 private ArrayList<View> mItemsInReadingOrder = new ArrayList<View>();
Adam Cohenbadf71e2011-05-26 19:08:29 -070092 private Drawable mIconDrawable;
Adam Cohen7c693212011-05-18 15:26:57 -070093 boolean mItemsInvalidated = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -070094 private ShortcutInfo mCurrentDragInfo;
95 private View mCurrentDragView;
96 boolean mSuppressOnAdd = false;
97 private int[] mTargetCell = new int[2];
98 private int[] mPreviousTargetCell = new int[2];
99 private int[] mEmptyCell = new int[2];
100 private Alarm mReorderAlarm = new Alarm();
101 private Alarm mOnExitAlarm = new Alarm();
Adam Cohen76fc0852011-06-17 13:26:23 -0700102 private int mFolderNameHeight;
Adam Cohen8e776a62011-06-28 18:10:06 -0700103 private Rect mTempRect = new Rect();
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700104 private boolean mDragInProgress = false;
105 private boolean mDeleteFolderOnDropCompleted = false;
106 private boolean mSuppressFolderDeletion = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700107 private boolean mItemAddedBackToSelfViaIcon = false;
Adam Cohenac56cff2011-09-28 20:45:37 -0700108 FolderEditText mFolderName;
Adam Cohen268c4752012-06-06 17:47:33 -0700109 private float mFolderIconPivotX;
110 private float mFolderIconPivotY;
Adam Cohen228da5a2011-07-27 22:23:47 -0700111
Adam Cohen76fc0852011-06-17 13:26:23 -0700112 private boolean mIsEditingName = false;
113 private InputMethodManager mInputMethodManager;
Adam Cohendf2cc412011-04-27 16:56:57 -0700114
Adam Cohena65beee2011-06-27 21:32:23 -0700115 private static String sDefaultFolderName;
116 private static String sHintText;
117
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700118 private int DRAG_MODE_NONE = 0;
119 private int DRAG_MODE_REORDER = 1;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700120 private int mDragMode = DRAG_MODE_NONE;
121
Adam Cohen1960ea42013-11-12 11:33:14 +0000122 // We avoid measuring the scroll view with a 0 width or height, as this
123 // results in CellLayout being measured as UNSPECIFIED, which it does
124 // not support.
125 private static final int MIN_CONTENT_DIMEN = 5;
126
Adam Cohenfb91f302012-06-11 15:45:18 -0700127 private boolean mDestroyed;
128
Alan Viverette4cda5b72013-08-28 17:53:41 -0700129 private AutoScrollHelper mAutoScrollHelper;
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700130
Michael Jurka1e2f4652013-07-08 18:03:46 -0700131 private Runnable mDeferredAction;
132 private boolean mDeferDropAfterUninstall;
133 private boolean mUninstallSuccessful;
134
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135 /**
136 * Used to inflate the Workspace from XML.
137 *
138 * @param context The application's context.
139 * @param attrs The attribtues set containing the Workspace's customization values.
140 */
141 public Folder(Context context, AttributeSet attrs) {
142 super(context, attrs);
Winson Chung5f8afe62013-08-12 16:19:28 -0700143
144 LauncherAppState app = LauncherAppState.getInstance();
145 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146 setAlwaysDrawnWithCacheEnabled(false);
Adam Cohendf2cc412011-04-27 16:56:57 -0700147 mInflater = LayoutInflater.from(context);
Winson Chung5f8afe62013-08-12 16:19:28 -0700148 mIconCache = app.getIconCache();
Adam Cohen78dc83e2011-11-15 17:10:00 -0800149
150 Resources res = getResources();
Michael Jurka414300a2013-08-27 15:42:35 +0200151 mMaxCountX = (int) grid.numColumns;
Adam Cohen477828c2013-09-20 12:05:49 -0700152 mMaxCountY = (int) grid.numRows;
153 mMaxNumItems = mMaxCountX * mMaxCountY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700154
155 mInputMethodManager = (InputMethodManager)
Michael Jurka8b805b12012-04-18 14:23:14 -0700156 getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
Adam Cohen76fc0852011-06-17 13:26:23 -0700157
Adam Cohen76fc0852011-06-17 13:26:23 -0700158 mExpandDuration = res.getInteger(R.integer.config_folderAnimDuration);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700159
160 if (sDefaultFolderName == null) {
161 sDefaultFolderName = res.getString(R.string.folder_name);
162 }
Adam Cohena65beee2011-06-27 21:32:23 -0700163 if (sHintText == null) {
164 sHintText = res.getString(R.string.folder_hint_text);
165 }
Adam Cohen4eac29a2011-07-11 17:53:37 -0700166 mLauncher = (Launcher) context;
Adam Cohenac56cff2011-09-28 20:45:37 -0700167 // We need this view to be focusable in touch mode so that when text editing of the folder
168 // name is complete, we have something to focus on, thus hiding the cursor and giving
169 // reliable behvior when clicking the text field (since it will always gain focus on click).
170 setFocusableInTouchMode(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171 }
172
173 @Override
174 protected void onFinishInflate() {
175 super.onFinishInflate();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700176 mScrollView = (ScrollView) findViewById(R.id.scroll_view);
Adam Cohendf2cc412011-04-27 16:56:57 -0700177 mContent = (CellLayout) findViewById(R.id.folder_content);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700178
Winson Chung5f8afe62013-08-12 16:19:28 -0700179 LauncherAppState app = LauncherAppState.getInstance();
180 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
181
Winson Chung5f8afe62013-08-12 16:19:28 -0700182 mContent.setCellDimensions(grid.folderCellWidthPx, grid.folderCellHeightPx);
Adam Cohen2801caf2011-05-13 20:57:39 -0700183 mContent.setGridSize(0, 0);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700184 mContent.getShortcutsAndWidgets().setMotionEventSplittingEnabled(false);
Adam Cohen2374abf2013-04-16 14:56:57 -0700185 mContent.setInvertIfRtl(true);
Adam Cohenac56cff2011-09-28 20:45:37 -0700186 mFolderName = (FolderEditText) findViewById(R.id.folder_name);
187 mFolderName.setFolder(this);
Adam Cohenea0818d2011-09-30 20:06:58 -0700188 mFolderName.setOnFocusChangeListener(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700189
190 // We find out how tall the text view wants to be (it is set to wrap_content), so that
191 // we can allocate the appropriate amount of space for it.
192 int measureSpec = MeasureSpec.UNSPECIFIED;
193 mFolderName.measure(measureSpec, measureSpec);
194 mFolderNameHeight = mFolderName.getMeasuredHeight();
195
196 // We disable action mode for now since it messes up the view on phones
197 mFolderName.setCustomSelectionActionModeCallback(mActionModeCallback);
Adam Cohen76fc0852011-06-17 13:26:23 -0700198 mFolderName.setOnEditorActionListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700199 mFolderName.setSelectAllOnFocus(true);
Adam Cohen7a14d0b2011-06-24 11:36:35 -0700200 mFolderName.setInputType(mFolderName.getInputType() |
201 InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
Alan Viverette4cda5b72013-08-28 17:53:41 -0700202 mAutoScrollHelper = new FolderAutoScrollHelper(mScrollView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800203 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700204
Adam Cohen76fc0852011-06-17 13:26:23 -0700205 private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
206 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
207 return false;
208 }
209
210 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
211 return false;
212 }
213
214 public void onDestroyActionMode(ActionMode mode) {
215 }
216
217 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
218 return false;
219 }
220 };
221
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800222 public void onClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700223 Object tag = v.getTag();
224 if (tag instanceof ShortcutInfo) {
Adam Cohenb5fe60c2013-06-06 22:03:51 -0700225 mLauncher.onClick(v);
Adam Cohendf2cc412011-04-27 16:56:57 -0700226 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800227 }
228
229 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -0700230 // Return if global dragging is not enabled
231 if (!mLauncher.isDraggingEnabled()) return true;
232
Adam Cohendf2cc412011-04-27 16:56:57 -0700233 Object tag = v.getTag();
234 if (tag instanceof ShortcutInfo) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700235 ShortcutInfo item = (ShortcutInfo) tag;
236 if (!v.isInTouchMode()) {
237 return false;
238 }
239
Winson Chung7d7541e2011-09-16 20:14:36 -0700240 mLauncher.dismissFolderCling(null);
241
Adam Cohendf2cc412011-04-27 16:56:57 -0700242 mLauncher.getWorkspace().onDragStartedWithItem(v);
Adam Cohenac8c8762011-07-13 11:15:27 -0700243 mLauncher.getWorkspace().beginDragShared(v, this);
Adam Cohenbadf71e2011-05-26 19:08:29 -0700244 mIconDrawable = ((TextView) v).getCompoundDrawables()[1];
Adam Cohen76078c42011-06-09 15:06:52 -0700245
246 mCurrentDragInfo = item;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700247 mEmptyCell[0] = item.cellX;
248 mEmptyCell[1] = item.cellY;
249 mCurrentDragView = v;
Adam Cohenfc53cd22011-07-20 15:45:11 -0700250
251 mContent.removeView(mCurrentDragView);
252 mInfo.remove(mCurrentDragInfo);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700253 mDragInProgress = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700254 mItemAddedBackToSelfViaIcon = false;
Adam Cohendf2cc412011-04-27 16:56:57 -0700255 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800256 return true;
257 }
258
Adam Cohen76fc0852011-06-17 13:26:23 -0700259 public boolean isEditingName() {
260 return mIsEditingName;
261 }
262
263 public void startEditingFolderName() {
Adam Cohena65beee2011-06-27 21:32:23 -0700264 mFolderName.setHint("");
Adam Cohen76fc0852011-06-17 13:26:23 -0700265 mIsEditingName = true;
266 }
267
268 public void dismissEditingName() {
269 mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
270 doneEditingFolderName(true);
271 }
272
273 public void doneEditingFolderName(boolean commit) {
Adam Cohena65beee2011-06-27 21:32:23 -0700274 mFolderName.setHint(sHintText);
Adam Cohen1df26a32011-08-12 16:15:23 -0700275 // Convert to a string here to ensure that no other state associated with the text field
276 // gets saved.
Adam Cohen3371da02011-10-25 21:38:29 -0700277 String newTitle = mFolderName.getText().toString();
278 mInfo.setTitle(newTitle);
Adam Cohen76fc0852011-06-17 13:26:23 -0700279 LauncherModel.updateItemInDatabase(mLauncher, mInfo);
Adam Cohenac56cff2011-09-28 20:45:37 -0700280
Adam Cohen3371da02011-10-25 21:38:29 -0700281 if (commit) {
282 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700283 String.format(getContext().getString(R.string.folder_renamed), newTitle));
Adam Cohen3371da02011-10-25 21:38:29 -0700284 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700285 // In order to clear the focus from the text field, we set the focus on ourself. This
286 // ensures that every time the field is clicked, focus is gained, giving reliable behavior.
287 requestFocus();
288
Adam Cohene601a432011-07-26 21:51:30 -0700289 Selection.setSelection((Spannable) mFolderName.getText(), 0, 0);
Adam Cohen76fc0852011-06-17 13:26:23 -0700290 mIsEditingName = false;
291 }
292
293 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
294 if (actionId == EditorInfo.IME_ACTION_DONE) {
295 dismissEditingName();
296 return true;
297 }
298 return false;
299 }
300
301 public View getEditTextRegion() {
302 return mFolderName;
303 }
304
Adam Cohenbadf71e2011-05-26 19:08:29 -0700305 public Drawable getDragDrawable() {
306 return mIconDrawable;
307 }
308
Adam Cohen0c872ba2011-05-05 10:34:16 -0700309 /**
310 * We need to handle touch events to prevent them from falling through to the workspace below.
311 */
312 @Override
313 public boolean onTouchEvent(MotionEvent ev) {
314 return true;
315 }
316
Joe Onorato00acb122009-08-04 16:04:30 -0400317 public void setDragController(DragController dragController) {
318 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800319 }
320
Adam Cohen2801caf2011-05-13 20:57:39 -0700321 void setFolderIcon(FolderIcon icon) {
322 mFolderIcon = icon;
323 }
324
Adam Cohen3371da02011-10-25 21:38:29 -0700325 @Override
326 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
327 // When the folder gets focus, we don't want to announce the list of items.
328 return true;
329 }
330
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800331 /**
332 * @return the FolderInfo object associated with this folder
333 */
334 FolderInfo getInfo() {
335 return mInfo;
336 }
337
Adam Cohen3bf84d32012-05-07 20:17:14 -0700338 private class GridComparator implements Comparator<ShortcutInfo> {
339 int mNumCols;
340 public GridComparator(int numCols) {
341 mNumCols = numCols;
342 }
343
344 @Override
345 public int compare(ShortcutInfo lhs, ShortcutInfo rhs) {
346 int lhIndex = lhs.cellY * mNumCols + lhs.cellX;
347 int rhIndex = rhs.cellY * mNumCols + rhs.cellX;
348 return (lhIndex - rhIndex);
349 }
Adam Cohen3bf84d32012-05-07 20:17:14 -0700350 }
351
352 private void placeInReadingOrder(ArrayList<ShortcutInfo> items) {
353 int maxX = 0;
354 int count = items.size();
355 for (int i = 0; i < count; i++) {
356 ShortcutInfo item = items.get(i);
357 if (item.cellX > maxX) {
358 maxX = item.cellX;
359 }
360 }
Adam Cohen691a5792012-05-11 14:27:30 -0700361
362 GridComparator gridComparator = new GridComparator(maxX + 1);
Adam Cohen3bf84d32012-05-07 20:17:14 -0700363 Collections.sort(items, gridComparator);
364 final int countX = mContent.getCountX();
365 for (int i = 0; i < count; i++) {
366 int x = i % countX;
367 int y = i / countX;
368 ShortcutInfo item = items.get(i);
369 item.cellX = x;
370 item.cellY = y;
371 }
372 }
373
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800374 void bind(FolderInfo info) {
375 mInfo = info;
Adam Cohendf2cc412011-04-27 16:56:57 -0700376 ArrayList<ShortcutInfo> children = info.contents;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700377 ArrayList<ShortcutInfo> overflow = new ArrayList<ShortcutInfo>();
Adam Cohen7c693212011-05-18 15:26:57 -0700378 setupContentForNumItems(children.size());
Adam Cohen3bf84d32012-05-07 20:17:14 -0700379 placeInReadingOrder(children);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700380 int count = 0;
Adam Cohendf2cc412011-04-27 16:56:57 -0700381 for (int i = 0; i < children.size(); i++) {
382 ShortcutInfo child = (ShortcutInfo) children.get(i);
Adam Cohenc508b2d2011-06-28 14:41:44 -0700383 if (!createAndAddShortcut(child)) {
384 overflow.add(child);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700385 } else {
386 count++;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700387 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700388 }
Adam Cohenc508b2d2011-06-28 14:41:44 -0700389
Adam Cohen0057bbc2011-08-12 18:30:51 -0700390 // We rearrange the items in case there are any empty gaps
391 setupContentForNumItems(count);
392
Adam Cohenc508b2d2011-06-28 14:41:44 -0700393 // If our folder has too many items we prune them from the list. This is an issue
394 // when upgrading from the old Folders implementation which could contain an unlimited
395 // number of items.
396 for (ShortcutInfo item: overflow) {
397 mInfo.remove(item);
398 LauncherModel.deleteItemFromDatabase(mLauncher, item);
399 }
400
Adam Cohen4dbe6d92011-05-18 17:14:15 -0700401 mItemsInvalidated = true;
Adam Cohenac56cff2011-09-28 20:45:37 -0700402 updateTextViewFocus();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700403 mInfo.addListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700404
Adam Cohenafb01ee2011-06-23 15:38:03 -0700405 if (!sDefaultFolderName.contentEquals(mInfo.title)) {
Adam Cohen4ef610f2011-06-21 22:37:36 -0700406 mFolderName.setText(mInfo.title);
407 } else {
408 mFolderName.setText("");
409 }
Adam Cohen691a5792012-05-11 14:27:30 -0700410 updateItemLocationsInDatabase();
Winson Chung33231f52013-12-09 16:57:45 -0800411
412 // In case any children didn't come across during loading, clean up the folder accordingly
413 mFolderIcon.post(new Runnable() {
414 public void run() {
415 if (getItemCount() <= 1) {
416 replaceFolderWithFinalItem();
417 }
418 }
419 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700420 }
421
422 /**
423 * Creates a new UserFolder, inflated from R.layout.user_folder.
424 *
425 * @param context The application's context.
426 *
427 * @return A new UserFolder.
428 */
429 static Folder fromXml(Context context) {
430 return (Folder) LayoutInflater.from(context).inflate(R.layout.user_folder, null);
431 }
432
433 /**
434 * This method is intended to make the UserFolder to be visually identical in size and position
435 * to its associated FolderIcon. This allows for a seamless transition into the expanded state.
436 */
437 private void positionAndSizeAsIcon() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700438 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800439 setScaleX(0.8f);
440 setScaleY(0.8f);
441 setAlpha(0f);
Adam Cohendf2cc412011-04-27 16:56:57 -0700442 mState = STATE_SMALL;
443 }
444
445 public void animateOpen() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700446 positionAndSizeAsIcon();
447
Adam Cohen8e776a62011-06-28 18:10:06 -0700448 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen2801caf2011-05-13 20:57:39 -0700449 centerAboutIcon();
Adam Cohen662b5982011-12-13 17:45:21 -0800450 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1);
451 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
452 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
Michael Jurka032e6ba2013-04-22 15:08:42 +0200453 final ObjectAnimator oa =
Michael Jurka2ecf9952012-06-18 12:52:28 -0700454 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohen6441de02011-12-14 14:25:32 -0800455
Adam Cohen2801caf2011-05-13 20:57:39 -0700456 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700457 @Override
458 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700459 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700460 String.format(getContext().getString(R.string.folder_opened),
Adam Cohen3371da02011-10-25 21:38:29 -0700461 mContent.getCountX(), mContent.getCountY()));
Adam Cohendf2cc412011-04-27 16:56:57 -0700462 mState = STATE_ANIMATING;
463 }
464 @Override
465 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700466 mState = STATE_OPEN;
Michael Jurka0121c3e2012-05-31 08:36:04 -0700467 setLayerType(LAYER_TYPE_NONE, null);
Winson Chung7a74ac92011-09-20 17:43:51 -0700468 Cling cling = mLauncher.showFirstRunFoldersCling();
469 if (cling != null) {
Winson Chungfa545132013-09-26 17:45:32 -0700470 cling.bringScrimToFront();
Winson Chungaf40f202013-09-18 18:26:31 -0700471 bringToFront();
Winson Chungfa545132013-09-26 17:45:32 -0700472 cling.bringToFront();
Winson Chung7a74ac92011-09-20 17:43:51 -0700473 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700474 setFocusOnFirstChild();
Adam Cohendf2cc412011-04-27 16:56:57 -0700475 }
476 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700477 oa.setDuration(mExpandDuration);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700478 setLayerType(LAYER_TYPE_HARDWARE, null);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100479 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700480 }
481
Adam Cohen3371da02011-10-25 21:38:29 -0700482 private void sendCustomAccessibilityEvent(int type, String text) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700483 AccessibilityManager accessibilityManager = (AccessibilityManager)
484 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
485 if (accessibilityManager.isEnabled()) {
Adam Cohen3371da02011-10-25 21:38:29 -0700486 AccessibilityEvent event = AccessibilityEvent.obtain(type);
487 onInitializeAccessibilityEvent(event);
488 event.getText().add(text);
Michael Jurka8b805b12012-04-18 14:23:14 -0700489 accessibilityManager.sendAccessibilityEvent(event);
Adam Cohen3371da02011-10-25 21:38:29 -0700490 }
491 }
492
Adam Cohenac56cff2011-09-28 20:45:37 -0700493 private void setFocusOnFirstChild() {
494 View firstChild = mContent.getChildAt(0, 0);
495 if (firstChild != null) {
496 firstChild.requestFocus();
497 }
498 }
499
Adam Cohendf2cc412011-04-27 16:56:57 -0700500 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700501 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800502 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
503 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
504 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
Michael Jurka032e6ba2013-04-22 15:08:42 +0200505 final ObjectAnimator oa =
Michael Jurka2ecf9952012-06-18 12:52:28 -0700506 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohendf2cc412011-04-27 16:56:57 -0700507
Adam Cohen2801caf2011-05-13 20:57:39 -0700508 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700509 @Override
510 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700511 onCloseComplete();
Michael Jurka0121c3e2012-05-31 08:36:04 -0700512 setLayerType(LAYER_TYPE_NONE, null);
Adam Cohen2801caf2011-05-13 20:57:39 -0700513 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700514 }
515 @Override
516 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700517 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700518 getContext().getString(R.string.folder_closed));
Adam Cohendf2cc412011-04-27 16:56:57 -0700519 mState = STATE_ANIMATING;
520 }
521 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700522 oa.setDuration(mExpandDuration);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700523 setLayerType(LAYER_TYPE_HARDWARE, null);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100524 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700525 }
526
Adam Cohencb3382b2011-05-24 14:07:08 -0700527 public boolean acceptDrop(DragObject d) {
528 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700529 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700530 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
531 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
532 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700533 }
534
Adam Cohendf2cc412011-04-27 16:56:57 -0700535 protected boolean findAndSetEmptyCells(ShortcutInfo item) {
536 int[] emptyCell = new int[2];
537 if (mContent.findCellForSpan(emptyCell, item.spanX, item.spanY)) {
538 item.cellX = emptyCell[0];
539 item.cellY = emptyCell[1];
Adam Cohendf2cc412011-04-27 16:56:57 -0700540 return true;
541 } else {
542 return false;
543 }
544 }
545
Adam Cohenc508b2d2011-06-28 14:41:44 -0700546 protected boolean createAndAddShortcut(ShortcutInfo item) {
Adam Cohen477828c2013-09-20 12:05:49 -0700547 final BubbleTextView textView =
548 (BubbleTextView) mInflater.inflate(R.layout.application, this, false);
Winson Chung54000492013-10-14 16:29:29 -0700549 textView.setCompoundDrawables(null,
Winson Chung0dbd7342013-10-13 22:46:20 -0700550 Utilities.createIconDrawable(item.getIcon(mIconCache)), null, null);
Adam Cohendf2cc412011-04-27 16:56:57 -0700551 textView.setText(item.title);
552 textView.setTag(item);
Adam Cohen477828c2013-09-20 12:05:49 -0700553 textView.setTextColor(getResources().getColor(R.color.folder_items_text_color));
554 textView.setShadowsEnabled(false);
Adam Cohen7397e622013-10-24 12:11:34 -0700555 textView.setGlowColor(getResources().getColor(R.color.folder_items_glow_color));
Adam Cohendf2cc412011-04-27 16:56:57 -0700556
557 textView.setOnClickListener(this);
558 textView.setOnLongClickListener(this);
559
Adam Cohenc508b2d2011-06-28 14:41:44 -0700560 // We need to check here to verify that the given item's location isn't already occupied
Adam Cohen3bf84d32012-05-07 20:17:14 -0700561 // by another item.
Adam Cohen0057bbc2011-08-12 18:30:51 -0700562 if (mContent.getChildAt(item.cellX, item.cellY) != null || item.cellX < 0 || item.cellY < 0
563 || item.cellX >= mContent.getCountX() || item.cellY >= mContent.getCountY()) {
Adam Cohen3bf84d32012-05-07 20:17:14 -0700564 // This shouldn't happen, log it.
565 Log.e(TAG, "Folder order not properly persisted during bind");
Adam Cohenc508b2d2011-06-28 14:41:44 -0700566 if (!findAndSetEmptyCells(item)) {
567 return false;
568 }
569 }
570
Adam Cohendf2cc412011-04-27 16:56:57 -0700571 CellLayout.LayoutParams lp =
572 new CellLayout.LayoutParams(item.cellX, item.cellY, item.spanX, item.spanY);
573 boolean insert = false;
Adam Cohenac56cff2011-09-28 20:45:37 -0700574 textView.setOnKeyListener(new FolderKeyEventListener());
Adam Cohendf2cc412011-04-27 16:56:57 -0700575 mContent.addViewToCellLayout(textView, insert ? 0 : -1, (int)item.id, lp, true);
Adam Cohenc508b2d2011-06-28 14:41:44 -0700576 return true;
Adam Cohendf2cc412011-04-27 16:56:57 -0700577 }
578
Adam Cohencb3382b2011-05-24 14:07:08 -0700579 public void onDragEnter(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700580 mPreviousTargetCell[0] = -1;
581 mPreviousTargetCell[1] = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700582 mOnExitAlarm.cancelAlarm();
583 }
584
585 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
586 public void onAlarm(Alarm alarm) {
587 realTimeReorder(mEmptyCell, mTargetCell);
588 }
589 };
590
591 boolean readingOrderGreaterThan(int[] v1, int[] v2) {
592 if (v1[1] > v2[1] || (v1[1] == v2[1] && v1[0] > v2[0])) {
593 return true;
594 } else {
595 return false;
596 }
597 }
598
599 private void realTimeReorder(int[] empty, int[] target) {
600 boolean wrap;
601 int startX;
602 int endX;
603 int startY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700604 int delay = 0;
605 float delayAmount = 30;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700606 if (readingOrderGreaterThan(target, empty)) {
607 wrap = empty[0] >= mContent.getCountX() - 1;
608 startY = wrap ? empty[1] + 1 : empty[1];
609 for (int y = startY; y <= target[1]; y++) {
610 startX = y == empty[1] ? empty[0] + 1 : 0;
611 endX = y < target[1] ? mContent.getCountX() - 1 : target[0];
612 for (int x = startX; x <= endX; x++) {
613 View v = mContent.getChildAt(x,y);
614 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen482ed822012-03-02 14:15:13 -0800615 REORDER_ANIMATION_DURATION, delay, true, true)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700616 empty[0] = x;
617 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700618 delay += delayAmount;
619 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700620 }
621 }
622 }
623 } else {
624 wrap = empty[0] == 0;
625 startY = wrap ? empty[1] - 1 : empty[1];
626 for (int y = startY; y >= target[1]; y--) {
627 startX = y == empty[1] ? empty[0] - 1 : mContent.getCountX() - 1;
628 endX = y > target[1] ? 0 : target[0];
629 for (int x = startX; x >= endX; x--) {
630 View v = mContent.getChildAt(x,y);
631 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen482ed822012-03-02 14:15:13 -0800632 REORDER_ANIMATION_DURATION, delay, true, true)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700633 empty[0] = x;
634 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700635 delay += delayAmount;
636 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700637 }
638 }
639 }
640 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700641 }
642
Adam Cohen2374abf2013-04-16 14:56:57 -0700643 public boolean isLayoutRtl() {
644 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
645 }
646
Adam Cohencb3382b2011-05-24 14:07:08 -0700647 public void onDragOver(DragObject d) {
Alan Viverette4cda5b72013-08-28 17:53:41 -0700648 final DragView dragView = d.dragView;
649 final int scrollOffset = mScrollView.getScrollY();
650 final float[] r = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, dragView, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700651 r[0] -= getPaddingLeft();
652 r[1] -= getPaddingTop();
653
Alan Viverette4cda5b72013-08-28 17:53:41 -0700654 final long downTime = SystemClock.uptimeMillis();
655 final MotionEvent translatedEv = MotionEvent.obtain(
656 downTime, downTime, MotionEvent.ACTION_MOVE, d.x, d.y, 0);
657
658 if (!mAutoScrollHelper.isEnabled()) {
659 mAutoScrollHelper.setEnabled(true);
660 }
661
662 final boolean handled = mAutoScrollHelper.onTouch(this, translatedEv);
663 translatedEv.recycle();
664
665 if (handled) {
666 mReorderAlarm.cancelAlarm();
667 } else {
668 mTargetCell = mContent.findNearestArea(
669 (int) r[0], (int) r[1] + scrollOffset, 1, 1, mTargetCell);
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700670 if (isLayoutRtl()) {
671 mTargetCell[0] = mContent.getCountX() - mTargetCell[0] - 1;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700672 }
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700673 if (mTargetCell[0] != mPreviousTargetCell[0]
674 || mTargetCell[1] != mPreviousTargetCell[1]) {
675 mReorderAlarm.cancelAlarm();
676 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
677 mReorderAlarm.setAlarm(REORDER_DELAY);
678 mPreviousTargetCell[0] = mTargetCell[0];
679 mPreviousTargetCell[1] = mTargetCell[1];
680 mDragMode = DRAG_MODE_REORDER;
681 } else {
682 mDragMode = DRAG_MODE_NONE;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700683 }
Adam Cohenbfbfd262011-06-13 16:55:12 -0700684 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700685 }
686
Adam Cohenbfbfd262011-06-13 16:55:12 -0700687 // This is used to compute the visual center of the dragView. The idea is that
688 // the visual center represents the user's interpretation of where the item is, and hence
689 // is the appropriate point to use when determining drop location.
690 private float[] getDragViewVisualCenter(int x, int y, int xOffset, int yOffset,
691 DragView dragView, float[] recycle) {
692 float res[];
693 if (recycle == null) {
694 res = new float[2];
695 } else {
696 res = recycle;
697 }
698
699 // These represent the visual top and left of drag view if a dragRect was provided.
700 // If a dragRect was not provided, then they correspond to the actual view left and
701 // top, as the dragRect is in that case taken to be the entire dragView.
702 // R.dimen.dragViewOffsetY.
703 int left = x - xOffset;
704 int top = y - yOffset;
705
706 // In order to find the visual center, we shift by half the dragRect
707 res[0] = left + dragView.getDragRegion().width() / 2;
708 res[1] = top + dragView.getDragRegion().height() / 2;
709
710 return res;
711 }
712
713 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
714 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700715 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700716 }
717 };
718
Adam Cohen95bb8002011-07-03 23:40:28 -0700719 public void completeDragExit() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700720 mLauncher.closeFolder();
721 mCurrentDragInfo = null;
722 mCurrentDragView = null;
723 mSuppressOnAdd = false;
724 mRearrangeOnClose = true;
725 }
726
Adam Cohencb3382b2011-05-24 14:07:08 -0700727 public void onDragExit(DragObject d) {
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700728 // Exiting folder; stop the auto scroller.
Alan Viverette4cda5b72013-08-28 17:53:41 -0700729 mAutoScrollHelper.setEnabled(false);
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700730 // We only close the folder if this is a true drag exit, ie. not because
731 // a drop has occurred above the folder.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700732 if (!d.dragComplete) {
733 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
734 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
735 }
736 mReorderAlarm.cancelAlarm();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700737 mDragMode = DRAG_MODE_NONE;
Adam Cohen2801caf2011-05-13 20:57:39 -0700738 }
739
Michael Jurka1e2f4652013-07-08 18:03:46 -0700740 public void onDropCompleted(final View target, final DragObject d,
741 final boolean isFlingToDelete, final boolean success) {
742 if (mDeferDropAfterUninstall) {
Michael Jurkaf3007582013-08-21 14:33:57 +0200743 Log.d(TAG, "Deferred handling drop because waiting for uninstall.");
Michael Jurka1e2f4652013-07-08 18:03:46 -0700744 mDeferredAction = new Runnable() {
745 public void run() {
746 onDropCompleted(target, d, isFlingToDelete, success);
747 mDeferredAction = null;
748 }
749 };
750 return;
751 }
752
753 boolean beingCalledAfterUninstall = mDeferredAction != null;
754 boolean successfulDrop =
755 success && (!beingCalledAfterUninstall || mUninstallSuccessful);
Winson Chung5f8afe62013-08-12 16:19:28 -0700756
Michael Jurka1e2f4652013-07-08 18:03:46 -0700757 if (successfulDrop) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700758 if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700759 replaceFolderWithFinalItem();
760 }
761 } else {
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700762 setupContentForNumItems(getItemCount());
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700763 // The drag failed, we need to return the item to the folder
764 mFolderIcon.onDrop(d);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700765 }
766
767 if (target != this) {
768 if (mOnExitAlarm.alarmPending()) {
769 mOnExitAlarm.cancelAlarm();
Michael Jurka54554252013-08-01 12:52:23 +0200770 if (!successfulDrop) {
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700771 mSuppressFolderDeletion = true;
772 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700773 completeDragExit();
774 }
Adam Cohenf0f2ffa2013-11-12 11:44:58 +0000775 } else {
776 mLauncher.getWorkspace().removeExtraEmptyScreen(true, null);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700777 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700778
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700779 mDeleteFolderOnDropCompleted = false;
780 mDragInProgress = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700781 mItemAddedBackToSelfViaIcon = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700782 mCurrentDragInfo = null;
783 mCurrentDragView = null;
784 mSuppressOnAdd = false;
Adam Cohen4045eb72011-10-06 11:44:26 -0700785
786 // Reordering may have occured, and we need to save the new item locations. We do this once
787 // at the end to prevent unnecessary database operations.
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700788 updateItemLocationsInDatabaseBatch();
Adam Cohen4045eb72011-10-06 11:44:26 -0700789 }
790
Michael Jurka1e2f4652013-07-08 18:03:46 -0700791 public void deferCompleteDropAfterUninstallActivity() {
792 mDeferDropAfterUninstall = true;
793 }
794
795 public void onUninstallActivityReturned(boolean success) {
796 mDeferDropAfterUninstall = false;
797 mUninstallSuccessful = success;
798 if (mDeferredAction != null) {
799 mDeferredAction.run();
800 }
801 }
802
Winson Chunga48487a2012-03-20 16:19:37 -0700803 @Override
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800804 public float getIntrinsicIconScaleFactor() {
805 return 1f;
806 }
807
808 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800809 public boolean supportsFlingToDelete() {
810 return true;
811 }
812
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000813 @Override
814 public boolean supportsAppInfoDropTarget() {
815 return false;
816 }
817
818 @Override
819 public boolean supportsDeleteDropTarget() {
820 return true;
821 }
822
Winson Chunga48487a2012-03-20 16:19:37 -0700823 public void onFlingToDelete(DragObject d, int x, int y, PointF vec) {
824 // Do nothing
825 }
826
827 @Override
828 public void onFlingToDeleteCompleted() {
829 // Do nothing
830 }
831
Adam Cohen4045eb72011-10-06 11:44:26 -0700832 private void updateItemLocationsInDatabase() {
833 ArrayList<View> list = getItemsInReadingOrder();
834 for (int i = 0; i < list.size(); i++) {
835 View v = list.get(i);
836 ItemInfo info = (ItemInfo) v.getTag();
837 LauncherModel.moveItemInDatabase(mLauncher, info, mInfo.id, 0,
Winson Chung33231f52013-12-09 16:57:45 -0800838 info.cellX, info.cellY);
Adam Cohen4045eb72011-10-06 11:44:26 -0700839 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700840 }
Adam Cohen228da5a2011-07-27 22:23:47 -0700841
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700842 private void updateItemLocationsInDatabaseBatch() {
843 ArrayList<View> list = getItemsInReadingOrder();
844 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
845 for (int i = 0; i < list.size(); i++) {
846 View v = list.get(i);
847 ItemInfo info = (ItemInfo) v.getTag();
848 items.add(info);
849 }
850
851 LauncherModel.moveItemsInDatabase(mLauncher, items, mInfo.id, 0);
852 }
853
Adam Cohene25af792013-06-06 23:08:25 -0700854 public void addItemLocationsInDatabase() {
855 ArrayList<View> list = getItemsInReadingOrder();
856 for (int i = 0; i < list.size(); i++) {
857 View v = list.get(i);
858 ItemInfo info = (ItemInfo) v.getTag();
859 LauncherModel.addItemToDatabase(mLauncher, info, mInfo.id, 0,
860 info.cellX, info.cellY, false);
861 }
862 }
863
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700864 public void notifyDrop() {
865 if (mDragInProgress) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700866 mItemAddedBackToSelfViaIcon = true;
Adam Cohen76078c42011-06-09 15:06:52 -0700867 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700868 }
869
870 public boolean isDropEnabled() {
871 return true;
872 }
873
Adam Cohen4045eb72011-10-06 11:44:26 -0700874 private void setupContentDimensions(int count) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700875 ArrayList<View> list = getItemsInReadingOrder();
876
877 int countX = mContent.getCountX();
878 int countY = mContent.getCountY();
Adam Cohen7c693212011-05-18 15:26:57 -0700879 boolean done = false;
Adam Cohen2801caf2011-05-13 20:57:39 -0700880
Adam Cohen7c693212011-05-18 15:26:57 -0700881 while (!done) {
882 int oldCountX = countX;
883 int oldCountY = countY;
884 if (countX * countY < count) {
885 // Current grid is too small, expand it
Adam Cohen78dc83e2011-11-15 17:10:00 -0800886 if ((countX <= countY || countY == mMaxCountY) && countX < mMaxCountX) {
Adam Cohen7c693212011-05-18 15:26:57 -0700887 countX++;
888 } else if (countY < mMaxCountY) {
889 countY++;
890 }
891 if (countY == 0) countY++;
892 } else if ((countY - 1) * countX >= count && countY >= countX) {
893 countY = Math.max(0, countY - 1);
894 } else if ((countX - 1) * countY >= count) {
895 countX = Math.max(0, countX - 1);
Adam Cohen2801caf2011-05-13 20:57:39 -0700896 }
Adam Cohen7c693212011-05-18 15:26:57 -0700897 done = countX == oldCountX && countY == oldCountY;
Adam Cohen2801caf2011-05-13 20:57:39 -0700898 }
Adam Cohen7c693212011-05-18 15:26:57 -0700899 mContent.setGridSize(countX, countY);
Adam Cohen2801caf2011-05-13 20:57:39 -0700900 arrangeChildren(list);
901 }
902
903 public boolean isFull() {
Adam Cohen78dc83e2011-11-15 17:10:00 -0800904 return getItemCount() >= mMaxNumItems;
Adam Cohen2801caf2011-05-13 20:57:39 -0700905 }
906
907 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700908 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700909
Winson Chung892c74d2013-08-22 16:15:50 -0700910 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700911 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700912 int height = getFolderHeight();
Adam Cohen2801caf2011-05-13 20:57:39 -0700913
Adam Cohen307fe232012-08-16 17:55:58 -0700914 float scale = parent.getDescendantRectRelativeToSelf(mFolderIcon, mTempRect);
Adam Cohen8e776a62011-06-28 18:10:06 -0700915
Winson Chungaf40f202013-09-18 18:26:31 -0700916 LauncherAppState app = LauncherAppState.getInstance();
917 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
918
Adam Cohen307fe232012-08-16 17:55:58 -0700919 int centerX = (int) (mTempRect.left + mTempRect.width() * scale / 2);
920 int centerY = (int) (mTempRect.top + mTempRect.height() * scale / 2);
Adam Cohen2801caf2011-05-13 20:57:39 -0700921 int centeredLeft = centerX - width / 2;
922 int centeredTop = centerY - height / 2;
Winson Chung3057b1c2013-10-10 17:35:35 -0700923 int currentPage = mLauncher.getWorkspace().getNextPage();
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800924 // In case the workspace is scrolling, we need to use the final scroll to compute
925 // the folders bounds.
926 mLauncher.getWorkspace().setFinalScrollForPageChange(currentPage);
Adam Cohen35e7e642011-07-17 14:47:18 -0700927 // We first fetch the currently visible CellLayoutChildren
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800928 CellLayout currentLayout = (CellLayout) mLauncher.getWorkspace().getChildAt(currentPage);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700929 ShortcutAndWidgetContainer boundingLayout = currentLayout.getShortcutsAndWidgets();
Adam Cohen35e7e642011-07-17 14:47:18 -0700930 Rect bounds = new Rect();
931 parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800932 // We reset the workspaces scroll
933 mLauncher.getWorkspace().resetFinalScrollForPageChange(currentPage);
Adam Cohen2801caf2011-05-13 20:57:39 -0700934
Adam Cohen35e7e642011-07-17 14:47:18 -0700935 // We need to bound the folder to the currently visible CellLayoutChildren
936 int left = Math.min(Math.max(bounds.left, centeredLeft),
937 bounds.left + bounds.width() - width);
938 int top = Math.min(Math.max(bounds.top, centeredTop),
939 bounds.top + bounds.height() - height);
Winson Chungaf40f202013-09-18 18:26:31 -0700940 if (grid.isPhone() && (grid.availableWidthPx - width) < grid.iconSizePx) {
941 // Center the folder if it is full (on phones only)
942 left = (grid.availableWidthPx - width) / 2;
943 } else if (width >= bounds.width()) {
944 // If the folder doesn't fit within the bounds, center it about the desired bounds
Adam Cohen35e7e642011-07-17 14:47:18 -0700945 left = bounds.left + (bounds.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700946 }
Adam Cohen35e7e642011-07-17 14:47:18 -0700947 if (height >= bounds.height()) {
948 top = bounds.top + (bounds.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700949 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700950
951 int folderPivotX = width / 2 + (centeredLeft - left);
952 int folderPivotY = height / 2 + (centeredTop - top);
953 setPivotX(folderPivotX);
954 setPivotY(folderPivotY);
Adam Cohen268c4752012-06-06 17:47:33 -0700955 mFolderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700956 (1.0f * folderPivotX / width));
Adam Cohen268c4752012-06-06 17:47:33 -0700957 mFolderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700958 (1.0f * folderPivotY / height));
Adam Cohen3bf84d32012-05-07 20:17:14 -0700959
Adam Cohen662b5982011-12-13 17:45:21 -0800960 lp.width = width;
961 lp.height = height;
962 lp.x = left;
963 lp.y = top;
Adam Cohen2801caf2011-05-13 20:57:39 -0700964 }
965
Adam Cohen268c4752012-06-06 17:47:33 -0700966 float getPivotXForIconAnimation() {
967 return mFolderIconPivotX;
968 }
969 float getPivotYForIconAnimation() {
970 return mFolderIconPivotY;
971 }
972
Adam Cohen2801caf2011-05-13 20:57:39 -0700973 private void setupContentForNumItems(int count) {
Adam Cohen4045eb72011-10-06 11:44:26 -0700974 setupContentDimensions(count);
Adam Cohen2801caf2011-05-13 20:57:39 -0700975
Adam Cohen8e776a62011-06-28 18:10:06 -0700976 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700977 if (lp == null) {
Adam Cohen8e776a62011-06-28 18:10:06 -0700978 lp = new DragLayer.LayoutParams(0, 0);
979 lp.customPosition = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700980 setLayoutParams(lp);
981 }
982 centerAboutIcon();
983 }
984
Winson Chung892c74d2013-08-22 16:15:50 -0700985 private int getContentAreaHeight() {
986 LauncherAppState app = LauncherAppState.getInstance();
987 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
988 Rect workspacePadding = grid.getWorkspacePadding(grid.isLandscape ?
989 CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
990 int maxContentAreaHeight = grid.availableHeightPx -
Winson Chung892c74d2013-08-22 16:15:50 -0700991 workspacePadding.top - workspacePadding.bottom -
Winson Chung892c74d2013-08-22 16:15:50 -0700992 mFolderNameHeight;
Adam Cohen1960ea42013-11-12 11:33:14 +0000993 int height = Math.min(maxContentAreaHeight,
Winson Chung892c74d2013-08-22 16:15:50 -0700994 mContent.getDesiredHeight());
Adam Cohen1960ea42013-11-12 11:33:14 +0000995 return Math.max(height, MIN_CONTENT_DIMEN);
996 }
997
998 private int getContentAreaWidth() {
999 return Math.max(mContent.getDesiredWidth(), MIN_CONTENT_DIMEN);
Winson Chung892c74d2013-08-22 16:15:50 -07001000 }
1001
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001002 private int getFolderHeight() {
Winson Chung892c74d2013-08-22 16:15:50 -07001003 int height = getPaddingTop() + getPaddingBottom()
1004 + getContentAreaHeight() + mFolderNameHeight;
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001005 return height;
1006 }
Adam Cohen234c4cd2011-07-17 21:03:04 -07001007
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001008 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001009 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
1010 int height = getFolderHeight();
Adam Cohen1960ea42013-11-12 11:33:14 +00001011 int contentAreaWidthSpec = MeasureSpec.makeMeasureSpec(getContentAreaWidth(),
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001012 MeasureSpec.EXACTLY);
Winson Chung892c74d2013-08-22 16:15:50 -07001013 int contentAreaHeightSpec = MeasureSpec.makeMeasureSpec(getContentAreaHeight(),
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001014 MeasureSpec.EXACTLY);
Adam Cohen1960ea42013-11-12 11:33:14 +00001015
1016 mContent.setFixedSize(getContentAreaWidth(), getContentAreaHeight());
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001017 mScrollView.measure(contentAreaWidthSpec, contentAreaHeightSpec);
1018 mFolderName.measure(contentAreaWidthSpec,
Adam Cohen234c4cd2011-07-17 21:03:04 -07001019 MeasureSpec.makeMeasureSpec(mFolderNameHeight, MeasureSpec.EXACTLY));
1020 setMeasuredDimension(width, height);
1021 }
1022
Adam Cohen2801caf2011-05-13 20:57:39 -07001023 private void arrangeChildren(ArrayList<View> list) {
1024 int[] vacant = new int[2];
1025 if (list == null) {
1026 list = getItemsInReadingOrder();
1027 }
1028 mContent.removeAllViews();
1029
1030 for (int i = 0; i < list.size(); i++) {
1031 View v = list.get(i);
1032 mContent.getVacantCell(vacant, 1, 1);
1033 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) v.getLayoutParams();
1034 lp.cellX = vacant[0];
1035 lp.cellY = vacant[1];
1036 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohen2792a332011-09-26 21:09:47 -07001037 if (info.cellX != vacant[0] || info.cellY != vacant[1]) {
1038 info.cellX = vacant[0];
1039 info.cellY = vacant[1];
1040 LauncherModel.addOrMoveItemInDatabase(mLauncher, info, mInfo.id, 0,
1041 info.cellX, info.cellY);
1042 }
Adam Cohen2801caf2011-05-13 20:57:39 -07001043 boolean insert = false;
1044 mContent.addViewToCellLayout(v, insert ? 0 : -1, (int)info.id, lp, true);
1045 }
Adam Cohen7c693212011-05-18 15:26:57 -07001046 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -07001047 }
1048
Adam Cohena9cf38f2011-05-02 15:36:58 -07001049 public int getItemCount() {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001050 return mContent.getShortcutsAndWidgets().getChildCount();
Adam Cohena9cf38f2011-05-02 15:36:58 -07001051 }
1052
1053 public View getItemAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001054 return mContent.getShortcutsAndWidgets().getChildAt(index);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001055 }
1056
Adam Cohen2801caf2011-05-13 20:57:39 -07001057 private void onCloseComplete() {
Adam Cohen05e0f402011-08-01 12:12:49 -07001058 DragLayer parent = (DragLayer) getParent();
Michael Jurka5649c282012-06-18 10:33:21 -07001059 if (parent != null) {
1060 parent.removeView(this);
1061 }
Adam Cohen4554ee12011-08-03 16:13:21 -07001062 mDragController.removeDropTarget((DropTarget) this);
Adam Cohen05e0f402011-08-01 12:12:49 -07001063 clearFocus();
Adam Cohenac56cff2011-09-28 20:45:37 -07001064 mFolderIcon.requestFocus();
Adam Cohen05e0f402011-08-01 12:12:49 -07001065
Adam Cohen2801caf2011-05-13 20:57:39 -07001066 if (mRearrangeOnClose) {
1067 setupContentForNumItems(getItemCount());
1068 mRearrangeOnClose = false;
1069 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001070 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001071 if (!mDragInProgress && !mSuppressFolderDeletion) {
1072 replaceFolderWithFinalItem();
1073 } else if (mDragInProgress) {
1074 mDeleteFolderOnDropCompleted = true;
1075 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001076 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001077 mSuppressFolderDeletion = false;
Adam Cohenafb01ee2011-06-23 15:38:03 -07001078 }
1079
1080 private void replaceFolderWithFinalItem() {
Adam Cohenafb01ee2011-06-23 15:38:03 -07001081 // Add the last remaining child to the workspace in place of the folder
Adam Cohenfb91f302012-06-11 15:45:18 -07001082 Runnable onCompleteRunnable = new Runnable() {
1083 @Override
1084 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -07001085 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screenId);
Adam Cohenafb01ee2011-06-23 15:38:03 -07001086
Winson Chung33231f52013-12-09 16:57:45 -08001087 View child = null;
Adam Cohenfb91f302012-06-11 15:45:18 -07001088 // Move the item from the folder to the workspace, in the position of the folder
1089 if (getItemCount() == 1) {
1090 ShortcutInfo finalItem = mInfo.contents.get(0);
Adam Cohenc5e63f32012-07-12 16:16:57 -07001091 child = mLauncher.createShortcut(R.layout.application, cellLayout,
Adam Cohenfb91f302012-06-11 15:45:18 -07001092 finalItem);
1093 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -07001094 mInfo.screenId, mInfo.cellX, mInfo.cellY);
Adam Cohenfb91f302012-06-11 15:45:18 -07001095 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001096 if (getItemCount() <= 1) {
1097 // Remove the folder
1098 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
1099 cellLayout.removeView(mFolderIcon);
1100 if (mFolderIcon instanceof DropTarget) {
1101 mDragController.removeDropTarget((DropTarget) mFolderIcon);
1102 }
1103 mLauncher.removeFolder(mInfo);
1104 }
Adam Cohenc5e63f32012-07-12 16:16:57 -07001105 // We add the child after removing the folder to prevent both from existing at
Winson Chung0e6a7132013-08-23 12:55:10 -07001106 // the same time in the CellLayout. We need to add the new item with addInScreenFromBind()
1107 // to ensure that hotseat items are placed correctly.
Adam Cohenc5e63f32012-07-12 16:16:57 -07001108 if (child != null) {
Winson Chung0e6a7132013-08-23 12:55:10 -07001109 mLauncher.getWorkspace().addInScreenFromBind(child, mInfo.container, mInfo.screenId,
Adam Cohenc5e63f32012-07-12 16:16:57 -07001110 mInfo.cellX, mInfo.cellY, mInfo.spanX, mInfo.spanY);
1111 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001112 }
1113 };
1114 View finalChild = getItemAt(0);
1115 if (finalChild != null) {
1116 mFolderIcon.performDestroyAnimation(finalChild, onCompleteRunnable);
Winson Chung33231f52013-12-09 16:57:45 -08001117 } else {
1118 onCompleteRunnable.run();
Adam Cohenafb01ee2011-06-23 15:38:03 -07001119 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001120 mDestroyed = true;
1121 }
1122
1123 boolean isDestroyed() {
1124 return mDestroyed;
Adam Cohen2801caf2011-05-13 20:57:39 -07001125 }
1126
Adam Cohenac56cff2011-09-28 20:45:37 -07001127 // This method keeps track of the last item in the folder for the purposes
1128 // of keyboard focus
1129 private void updateTextViewFocus() {
1130 View lastChild = getItemAt(getItemCount() - 1);
1131 getItemAt(getItemCount() - 1);
1132 if (lastChild != null) {
1133 mFolderName.setNextFocusDownId(lastChild.getId());
1134 mFolderName.setNextFocusRightId(lastChild.getId());
1135 mFolderName.setNextFocusLeftId(lastChild.getId());
1136 mFolderName.setNextFocusUpId(lastChild.getId());
1137 }
1138 }
1139
Adam Cohenbfbfd262011-06-13 16:55:12 -07001140 public void onDrop(DragObject d) {
1141 ShortcutInfo item;
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001142 if (d.dragInfo instanceof AppInfo) {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001143 // Came from all apps -- make a copy
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001144 item = ((AppInfo) d.dragInfo).makeShortcut();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001145 item.spanX = 1;
1146 item.spanY = 1;
1147 } else {
1148 item = (ShortcutInfo) d.dragInfo;
1149 }
Adam Cohen05e0f402011-08-01 12:12:49 -07001150 // Dragged from self onto self, currently this is the only path possible, however
1151 // we keep this as a distinct code path.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001152 if (item == mCurrentDragInfo) {
1153 ShortcutInfo si = (ShortcutInfo) mCurrentDragView.getTag();
1154 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) mCurrentDragView.getLayoutParams();
1155 si.cellX = lp.cellX = mEmptyCell[0];
1156 si.cellX = lp.cellY = mEmptyCell[1];
1157 mContent.addViewToCellLayout(mCurrentDragView, -1, (int)item.id, lp, true);
Adam Cohenfc53cd22011-07-20 15:45:11 -07001158 if (d.dragView.hasDrawn()) {
1159 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, mCurrentDragView);
1160 } else {
Winson Chung2c4cf412012-05-08 14:03:21 -07001161 d.deferDragViewCleanupPostAnimation = false;
Adam Cohenfc53cd22011-07-20 15:45:11 -07001162 mCurrentDragView.setVisibility(VISIBLE);
1163 }
Adam Cohene9166b22011-07-08 17:11:11 -07001164 mItemsInvalidated = true;
Adam Cohen4045eb72011-10-06 11:44:26 -07001165 setupContentDimensions(getItemCount());
Adam Cohen716b51e2011-06-30 12:09:54 -07001166 mSuppressOnAdd = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001167 }
1168 mInfo.add(item);
1169 }
1170
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001171 // This is used so the item doesn't immediately appear in the folder when added. In one case
1172 // we need to create the illusion that the item isn't added back to the folder yet, to
1173 // to correspond to the animation of the icon back into the folder. This is
1174 public void hideItem(ShortcutInfo info) {
1175 View v = getViewForInfo(info);
1176 v.setVisibility(INVISIBLE);
1177 }
1178 public void showItem(ShortcutInfo info) {
1179 View v = getViewForInfo(info);
1180 v.setVisibility(VISIBLE);
1181 }
1182
Adam Cohenbfbfd262011-06-13 16:55:12 -07001183 public void onAdd(ShortcutInfo item) {
1184 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001185 // If the item was dropped onto this open folder, we have done the work associated
1186 // with adding the item to the folder, as indicated by mSuppressOnAdd being set
Adam Cohenbfbfd262011-06-13 16:55:12 -07001187 if (mSuppressOnAdd) return;
1188 if (!findAndSetEmptyCells(item)) {
1189 // The current layout is full, can we expand it?
1190 setupContentForNumItems(getItemCount() + 1);
1191 findAndSetEmptyCells(item);
1192 }
1193 createAndAddShortcut(item);
1194 LauncherModel.addOrMoveItemInDatabase(
1195 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
1196 }
1197
Adam Cohena9cf38f2011-05-02 15:36:58 -07001198 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -07001199 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001200 // If this item is being dragged from this open folder, we have already handled
1201 // the work associated with removing the item, so we don't have to do anything here.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001202 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001203 View v = getViewForInfo(item);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001204 mContent.removeView(v);
Adam Cohen2801caf2011-05-13 20:57:39 -07001205 if (mState == STATE_ANIMATING) {
1206 mRearrangeOnClose = true;
1207 } else {
1208 setupContentForNumItems(getItemCount());
1209 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001210 if (getItemCount() <= 1) {
1211 replaceFolderWithFinalItem();
1212 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001213 }
Adam Cohen7c693212011-05-18 15:26:57 -07001214
Adam Cohendf1e4e82011-06-24 15:57:39 -07001215 private View getViewForInfo(ShortcutInfo item) {
1216 for (int j = 0; j < mContent.getCountY(); j++) {
1217 for (int i = 0; i < mContent.getCountX(); i++) {
1218 View v = mContent.getChildAt(i, j);
1219 if (v.getTag() == item) {
1220 return v;
1221 }
1222 }
1223 }
1224 return null;
1225 }
1226
Adam Cohen76078c42011-06-09 15:06:52 -07001227 public void onItemsChanged() {
Adam Cohenac56cff2011-09-28 20:45:37 -07001228 updateTextViewFocus();
Adam Cohen76078c42011-06-09 15:06:52 -07001229 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001230
Adam Cohen76fc0852011-06-17 13:26:23 -07001231 public void onTitleChanged(CharSequence title) {
1232 }
Adam Cohen76078c42011-06-09 15:06:52 -07001233
Adam Cohen7c693212011-05-18 15:26:57 -07001234 public ArrayList<View> getItemsInReadingOrder() {
1235 if (mItemsInvalidated) {
1236 mItemsInReadingOrder.clear();
1237 for (int j = 0; j < mContent.getCountY(); j++) {
1238 for (int i = 0; i < mContent.getCountX(); i++) {
1239 View v = mContent.getChildAt(i, j);
1240 if (v != null) {
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001241 mItemsInReadingOrder.add(v);
Adam Cohen7c693212011-05-18 15:26:57 -07001242 }
1243 }
1244 }
1245 mItemsInvalidated = false;
1246 }
1247 return mItemsInReadingOrder;
1248 }
Adam Cohen8dfcba42011-07-07 16:38:18 -07001249
1250 public void getLocationInDragLayer(int[] loc) {
1251 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
1252 }
Adam Cohenea0818d2011-09-30 20:06:58 -07001253
1254 public void onFocusChange(View v, boolean hasFocus) {
1255 if (v == mFolderName && hasFocus) {
1256 startEditingFolderName();
1257 }
1258 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001259
1260 @Override
1261 public void getHitRectRelativeToDragLayer(Rect outRect) {
1262 getHitRect(outRect);
1263 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001264}