blob: 37a947c3aec5b34296a060144377989fb50986da [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 Cohen9c58d822013-12-13 17:18:10 -0800775 }
776
777 // This is kind of hacky, but in general, dropping on the workspace handles removing
778 // the extra screen, but dropping elsewhere (back to self, or onto delete) doesn't.
779 if (target != mLauncher.getWorkspace()) {
Adam Cohenf0f2ffa2013-11-12 11:44:58 +0000780 mLauncher.getWorkspace().removeExtraEmptyScreen(true, null);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700781 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700782
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700783 mDeleteFolderOnDropCompleted = false;
784 mDragInProgress = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700785 mItemAddedBackToSelfViaIcon = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700786 mCurrentDragInfo = null;
787 mCurrentDragView = null;
788 mSuppressOnAdd = false;
Adam Cohen4045eb72011-10-06 11:44:26 -0700789
790 // Reordering may have occured, and we need to save the new item locations. We do this once
791 // at the end to prevent unnecessary database operations.
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700792 updateItemLocationsInDatabaseBatch();
Adam Cohen4045eb72011-10-06 11:44:26 -0700793 }
794
Michael Jurka1e2f4652013-07-08 18:03:46 -0700795 public void deferCompleteDropAfterUninstallActivity() {
796 mDeferDropAfterUninstall = true;
797 }
798
799 public void onUninstallActivityReturned(boolean success) {
800 mDeferDropAfterUninstall = false;
801 mUninstallSuccessful = success;
802 if (mDeferredAction != null) {
803 mDeferredAction.run();
804 }
805 }
806
Winson Chunga48487a2012-03-20 16:19:37 -0700807 @Override
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800808 public float getIntrinsicIconScaleFactor() {
809 return 1f;
810 }
811
812 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800813 public boolean supportsFlingToDelete() {
814 return true;
815 }
816
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000817 @Override
818 public boolean supportsAppInfoDropTarget() {
819 return false;
820 }
821
822 @Override
823 public boolean supportsDeleteDropTarget() {
824 return true;
825 }
826
Winson Chunga48487a2012-03-20 16:19:37 -0700827 public void onFlingToDelete(DragObject d, int x, int y, PointF vec) {
828 // Do nothing
829 }
830
831 @Override
832 public void onFlingToDeleteCompleted() {
833 // Do nothing
834 }
835
Adam Cohen4045eb72011-10-06 11:44:26 -0700836 private void updateItemLocationsInDatabase() {
837 ArrayList<View> list = getItemsInReadingOrder();
838 for (int i = 0; i < list.size(); i++) {
839 View v = list.get(i);
840 ItemInfo info = (ItemInfo) v.getTag();
841 LauncherModel.moveItemInDatabase(mLauncher, info, mInfo.id, 0,
Winson Chung33231f52013-12-09 16:57:45 -0800842 info.cellX, info.cellY);
Adam Cohen4045eb72011-10-06 11:44:26 -0700843 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700844 }
Adam Cohen228da5a2011-07-27 22:23:47 -0700845
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700846 private void updateItemLocationsInDatabaseBatch() {
847 ArrayList<View> list = getItemsInReadingOrder();
848 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
849 for (int i = 0; i < list.size(); i++) {
850 View v = list.get(i);
851 ItemInfo info = (ItemInfo) v.getTag();
852 items.add(info);
853 }
854
855 LauncherModel.moveItemsInDatabase(mLauncher, items, mInfo.id, 0);
856 }
857
Adam Cohene25af792013-06-06 23:08:25 -0700858 public void addItemLocationsInDatabase() {
859 ArrayList<View> list = getItemsInReadingOrder();
860 for (int i = 0; i < list.size(); i++) {
861 View v = list.get(i);
862 ItemInfo info = (ItemInfo) v.getTag();
863 LauncherModel.addItemToDatabase(mLauncher, info, mInfo.id, 0,
864 info.cellX, info.cellY, false);
865 }
866 }
867
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700868 public void notifyDrop() {
869 if (mDragInProgress) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700870 mItemAddedBackToSelfViaIcon = true;
Adam Cohen76078c42011-06-09 15:06:52 -0700871 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700872 }
873
874 public boolean isDropEnabled() {
875 return true;
876 }
877
Adam Cohen4045eb72011-10-06 11:44:26 -0700878 private void setupContentDimensions(int count) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700879 ArrayList<View> list = getItemsInReadingOrder();
880
881 int countX = mContent.getCountX();
882 int countY = mContent.getCountY();
Adam Cohen7c693212011-05-18 15:26:57 -0700883 boolean done = false;
Adam Cohen2801caf2011-05-13 20:57:39 -0700884
Adam Cohen7c693212011-05-18 15:26:57 -0700885 while (!done) {
886 int oldCountX = countX;
887 int oldCountY = countY;
888 if (countX * countY < count) {
889 // Current grid is too small, expand it
Adam Cohen78dc83e2011-11-15 17:10:00 -0800890 if ((countX <= countY || countY == mMaxCountY) && countX < mMaxCountX) {
Adam Cohen7c693212011-05-18 15:26:57 -0700891 countX++;
892 } else if (countY < mMaxCountY) {
893 countY++;
894 }
895 if (countY == 0) countY++;
896 } else if ((countY - 1) * countX >= count && countY >= countX) {
897 countY = Math.max(0, countY - 1);
898 } else if ((countX - 1) * countY >= count) {
899 countX = Math.max(0, countX - 1);
Adam Cohen2801caf2011-05-13 20:57:39 -0700900 }
Adam Cohen7c693212011-05-18 15:26:57 -0700901 done = countX == oldCountX && countY == oldCountY;
Adam Cohen2801caf2011-05-13 20:57:39 -0700902 }
Adam Cohen7c693212011-05-18 15:26:57 -0700903 mContent.setGridSize(countX, countY);
Adam Cohen2801caf2011-05-13 20:57:39 -0700904 arrangeChildren(list);
905 }
906
907 public boolean isFull() {
Adam Cohen78dc83e2011-11-15 17:10:00 -0800908 return getItemCount() >= mMaxNumItems;
Adam Cohen2801caf2011-05-13 20:57:39 -0700909 }
910
911 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700912 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700913
Winson Chung892c74d2013-08-22 16:15:50 -0700914 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700915 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700916 int height = getFolderHeight();
Adam Cohen2801caf2011-05-13 20:57:39 -0700917
Adam Cohen307fe232012-08-16 17:55:58 -0700918 float scale = parent.getDescendantRectRelativeToSelf(mFolderIcon, mTempRect);
Adam Cohen8e776a62011-06-28 18:10:06 -0700919
Winson Chungaf40f202013-09-18 18:26:31 -0700920 LauncherAppState app = LauncherAppState.getInstance();
921 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
922
Adam Cohen307fe232012-08-16 17:55:58 -0700923 int centerX = (int) (mTempRect.left + mTempRect.width() * scale / 2);
924 int centerY = (int) (mTempRect.top + mTempRect.height() * scale / 2);
Adam Cohen2801caf2011-05-13 20:57:39 -0700925 int centeredLeft = centerX - width / 2;
926 int centeredTop = centerY - height / 2;
Winson Chung3057b1c2013-10-10 17:35:35 -0700927 int currentPage = mLauncher.getWorkspace().getNextPage();
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800928 // In case the workspace is scrolling, we need to use the final scroll to compute
929 // the folders bounds.
930 mLauncher.getWorkspace().setFinalScrollForPageChange(currentPage);
Adam Cohen35e7e642011-07-17 14:47:18 -0700931 // We first fetch the currently visible CellLayoutChildren
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800932 CellLayout currentLayout = (CellLayout) mLauncher.getWorkspace().getChildAt(currentPage);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700933 ShortcutAndWidgetContainer boundingLayout = currentLayout.getShortcutsAndWidgets();
Adam Cohen35e7e642011-07-17 14:47:18 -0700934 Rect bounds = new Rect();
935 parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800936 // We reset the workspaces scroll
937 mLauncher.getWorkspace().resetFinalScrollForPageChange(currentPage);
Adam Cohen2801caf2011-05-13 20:57:39 -0700938
Adam Cohen35e7e642011-07-17 14:47:18 -0700939 // We need to bound the folder to the currently visible CellLayoutChildren
940 int left = Math.min(Math.max(bounds.left, centeredLeft),
941 bounds.left + bounds.width() - width);
942 int top = Math.min(Math.max(bounds.top, centeredTop),
943 bounds.top + bounds.height() - height);
Winson Chungaf40f202013-09-18 18:26:31 -0700944 if (grid.isPhone() && (grid.availableWidthPx - width) < grid.iconSizePx) {
945 // Center the folder if it is full (on phones only)
946 left = (grid.availableWidthPx - width) / 2;
947 } else if (width >= bounds.width()) {
948 // If the folder doesn't fit within the bounds, center it about the desired bounds
Adam Cohen35e7e642011-07-17 14:47:18 -0700949 left = bounds.left + (bounds.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700950 }
Adam Cohen35e7e642011-07-17 14:47:18 -0700951 if (height >= bounds.height()) {
952 top = bounds.top + (bounds.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700953 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700954
955 int folderPivotX = width / 2 + (centeredLeft - left);
956 int folderPivotY = height / 2 + (centeredTop - top);
957 setPivotX(folderPivotX);
958 setPivotY(folderPivotY);
Adam Cohen268c4752012-06-06 17:47:33 -0700959 mFolderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700960 (1.0f * folderPivotX / width));
Adam Cohen268c4752012-06-06 17:47:33 -0700961 mFolderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700962 (1.0f * folderPivotY / height));
Adam Cohen3bf84d32012-05-07 20:17:14 -0700963
Adam Cohen662b5982011-12-13 17:45:21 -0800964 lp.width = width;
965 lp.height = height;
966 lp.x = left;
967 lp.y = top;
Adam Cohen2801caf2011-05-13 20:57:39 -0700968 }
969
Adam Cohen268c4752012-06-06 17:47:33 -0700970 float getPivotXForIconAnimation() {
971 return mFolderIconPivotX;
972 }
973 float getPivotYForIconAnimation() {
974 return mFolderIconPivotY;
975 }
976
Adam Cohen2801caf2011-05-13 20:57:39 -0700977 private void setupContentForNumItems(int count) {
Adam Cohen4045eb72011-10-06 11:44:26 -0700978 setupContentDimensions(count);
Adam Cohen2801caf2011-05-13 20:57:39 -0700979
Adam Cohen8e776a62011-06-28 18:10:06 -0700980 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700981 if (lp == null) {
Adam Cohen8e776a62011-06-28 18:10:06 -0700982 lp = new DragLayer.LayoutParams(0, 0);
983 lp.customPosition = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700984 setLayoutParams(lp);
985 }
986 centerAboutIcon();
987 }
988
Winson Chung892c74d2013-08-22 16:15:50 -0700989 private int getContentAreaHeight() {
990 LauncherAppState app = LauncherAppState.getInstance();
991 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
992 Rect workspacePadding = grid.getWorkspacePadding(grid.isLandscape ?
993 CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
994 int maxContentAreaHeight = grid.availableHeightPx -
Winson Chung892c74d2013-08-22 16:15:50 -0700995 workspacePadding.top - workspacePadding.bottom -
Winson Chung892c74d2013-08-22 16:15:50 -0700996 mFolderNameHeight;
Adam Cohen1960ea42013-11-12 11:33:14 +0000997 int height = Math.min(maxContentAreaHeight,
Winson Chung892c74d2013-08-22 16:15:50 -0700998 mContent.getDesiredHeight());
Adam Cohen1960ea42013-11-12 11:33:14 +0000999 return Math.max(height, MIN_CONTENT_DIMEN);
1000 }
1001
1002 private int getContentAreaWidth() {
1003 return Math.max(mContent.getDesiredWidth(), MIN_CONTENT_DIMEN);
Winson Chung892c74d2013-08-22 16:15:50 -07001004 }
1005
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001006 private int getFolderHeight() {
Winson Chung892c74d2013-08-22 16:15:50 -07001007 int height = getPaddingTop() + getPaddingBottom()
1008 + getContentAreaHeight() + mFolderNameHeight;
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001009 return height;
1010 }
Adam Cohen234c4cd2011-07-17 21:03:04 -07001011
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001012 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001013 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
1014 int height = getFolderHeight();
Adam Cohen1960ea42013-11-12 11:33:14 +00001015 int contentAreaWidthSpec = MeasureSpec.makeMeasureSpec(getContentAreaWidth(),
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001016 MeasureSpec.EXACTLY);
Winson Chung892c74d2013-08-22 16:15:50 -07001017 int contentAreaHeightSpec = MeasureSpec.makeMeasureSpec(getContentAreaHeight(),
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001018 MeasureSpec.EXACTLY);
Adam Cohen1960ea42013-11-12 11:33:14 +00001019
1020 mContent.setFixedSize(getContentAreaWidth(), getContentAreaHeight());
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001021 mScrollView.measure(contentAreaWidthSpec, contentAreaHeightSpec);
1022 mFolderName.measure(contentAreaWidthSpec,
Adam Cohen234c4cd2011-07-17 21:03:04 -07001023 MeasureSpec.makeMeasureSpec(mFolderNameHeight, MeasureSpec.EXACTLY));
1024 setMeasuredDimension(width, height);
1025 }
1026
Adam Cohen2801caf2011-05-13 20:57:39 -07001027 private void arrangeChildren(ArrayList<View> list) {
1028 int[] vacant = new int[2];
1029 if (list == null) {
1030 list = getItemsInReadingOrder();
1031 }
1032 mContent.removeAllViews();
1033
1034 for (int i = 0; i < list.size(); i++) {
1035 View v = list.get(i);
1036 mContent.getVacantCell(vacant, 1, 1);
1037 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) v.getLayoutParams();
1038 lp.cellX = vacant[0];
1039 lp.cellY = vacant[1];
1040 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohen2792a332011-09-26 21:09:47 -07001041 if (info.cellX != vacant[0] || info.cellY != vacant[1]) {
1042 info.cellX = vacant[0];
1043 info.cellY = vacant[1];
1044 LauncherModel.addOrMoveItemInDatabase(mLauncher, info, mInfo.id, 0,
1045 info.cellX, info.cellY);
1046 }
Adam Cohen2801caf2011-05-13 20:57:39 -07001047 boolean insert = false;
1048 mContent.addViewToCellLayout(v, insert ? 0 : -1, (int)info.id, lp, true);
1049 }
Adam Cohen7c693212011-05-18 15:26:57 -07001050 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -07001051 }
1052
Adam Cohena9cf38f2011-05-02 15:36:58 -07001053 public int getItemCount() {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001054 return mContent.getShortcutsAndWidgets().getChildCount();
Adam Cohena9cf38f2011-05-02 15:36:58 -07001055 }
1056
1057 public View getItemAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001058 return mContent.getShortcutsAndWidgets().getChildAt(index);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001059 }
1060
Adam Cohen2801caf2011-05-13 20:57:39 -07001061 private void onCloseComplete() {
Adam Cohen05e0f402011-08-01 12:12:49 -07001062 DragLayer parent = (DragLayer) getParent();
Michael Jurka5649c282012-06-18 10:33:21 -07001063 if (parent != null) {
1064 parent.removeView(this);
1065 }
Adam Cohen4554ee12011-08-03 16:13:21 -07001066 mDragController.removeDropTarget((DropTarget) this);
Adam Cohen05e0f402011-08-01 12:12:49 -07001067 clearFocus();
Adam Cohenac56cff2011-09-28 20:45:37 -07001068 mFolderIcon.requestFocus();
Adam Cohen05e0f402011-08-01 12:12:49 -07001069
Adam Cohen2801caf2011-05-13 20:57:39 -07001070 if (mRearrangeOnClose) {
1071 setupContentForNumItems(getItemCount());
1072 mRearrangeOnClose = false;
1073 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001074 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001075 if (!mDragInProgress && !mSuppressFolderDeletion) {
1076 replaceFolderWithFinalItem();
1077 } else if (mDragInProgress) {
1078 mDeleteFolderOnDropCompleted = true;
1079 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001080 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001081 mSuppressFolderDeletion = false;
Adam Cohenafb01ee2011-06-23 15:38:03 -07001082 }
1083
1084 private void replaceFolderWithFinalItem() {
Adam Cohenafb01ee2011-06-23 15:38:03 -07001085 // Add the last remaining child to the workspace in place of the folder
Adam Cohenfb91f302012-06-11 15:45:18 -07001086 Runnable onCompleteRunnable = new Runnable() {
1087 @Override
1088 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -07001089 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screenId);
Adam Cohenafb01ee2011-06-23 15:38:03 -07001090
Winson Chung33231f52013-12-09 16:57:45 -08001091 View child = null;
Adam Cohenfb91f302012-06-11 15:45:18 -07001092 // Move the item from the folder to the workspace, in the position of the folder
1093 if (getItemCount() == 1) {
1094 ShortcutInfo finalItem = mInfo.contents.get(0);
Adam Cohenc5e63f32012-07-12 16:16:57 -07001095 child = mLauncher.createShortcut(R.layout.application, cellLayout,
Adam Cohenfb91f302012-06-11 15:45:18 -07001096 finalItem);
1097 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -07001098 mInfo.screenId, mInfo.cellX, mInfo.cellY);
Adam Cohenfb91f302012-06-11 15:45:18 -07001099 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001100 if (getItemCount() <= 1) {
1101 // Remove the folder
1102 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
1103 cellLayout.removeView(mFolderIcon);
1104 if (mFolderIcon instanceof DropTarget) {
1105 mDragController.removeDropTarget((DropTarget) mFolderIcon);
1106 }
1107 mLauncher.removeFolder(mInfo);
1108 }
Adam Cohenc5e63f32012-07-12 16:16:57 -07001109 // We add the child after removing the folder to prevent both from existing at
Winson Chung0e6a7132013-08-23 12:55:10 -07001110 // the same time in the CellLayout. We need to add the new item with addInScreenFromBind()
1111 // to ensure that hotseat items are placed correctly.
Adam Cohenc5e63f32012-07-12 16:16:57 -07001112 if (child != null) {
Winson Chung0e6a7132013-08-23 12:55:10 -07001113 mLauncher.getWorkspace().addInScreenFromBind(child, mInfo.container, mInfo.screenId,
Adam Cohenc5e63f32012-07-12 16:16:57 -07001114 mInfo.cellX, mInfo.cellY, mInfo.spanX, mInfo.spanY);
1115 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001116 }
1117 };
1118 View finalChild = getItemAt(0);
1119 if (finalChild != null) {
1120 mFolderIcon.performDestroyAnimation(finalChild, onCompleteRunnable);
Winson Chung33231f52013-12-09 16:57:45 -08001121 } else {
1122 onCompleteRunnable.run();
Adam Cohenafb01ee2011-06-23 15:38:03 -07001123 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001124 mDestroyed = true;
1125 }
1126
1127 boolean isDestroyed() {
1128 return mDestroyed;
Adam Cohen2801caf2011-05-13 20:57:39 -07001129 }
1130
Adam Cohenac56cff2011-09-28 20:45:37 -07001131 // This method keeps track of the last item in the folder for the purposes
1132 // of keyboard focus
1133 private void updateTextViewFocus() {
1134 View lastChild = getItemAt(getItemCount() - 1);
1135 getItemAt(getItemCount() - 1);
1136 if (lastChild != null) {
1137 mFolderName.setNextFocusDownId(lastChild.getId());
1138 mFolderName.setNextFocusRightId(lastChild.getId());
1139 mFolderName.setNextFocusLeftId(lastChild.getId());
1140 mFolderName.setNextFocusUpId(lastChild.getId());
1141 }
1142 }
1143
Adam Cohenbfbfd262011-06-13 16:55:12 -07001144 public void onDrop(DragObject d) {
1145 ShortcutInfo item;
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001146 if (d.dragInfo instanceof AppInfo) {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001147 // Came from all apps -- make a copy
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001148 item = ((AppInfo) d.dragInfo).makeShortcut();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001149 item.spanX = 1;
1150 item.spanY = 1;
1151 } else {
1152 item = (ShortcutInfo) d.dragInfo;
1153 }
Adam Cohen05e0f402011-08-01 12:12:49 -07001154 // Dragged from self onto self, currently this is the only path possible, however
1155 // we keep this as a distinct code path.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001156 if (item == mCurrentDragInfo) {
1157 ShortcutInfo si = (ShortcutInfo) mCurrentDragView.getTag();
1158 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) mCurrentDragView.getLayoutParams();
1159 si.cellX = lp.cellX = mEmptyCell[0];
1160 si.cellX = lp.cellY = mEmptyCell[1];
1161 mContent.addViewToCellLayout(mCurrentDragView, -1, (int)item.id, lp, true);
Adam Cohenfc53cd22011-07-20 15:45:11 -07001162 if (d.dragView.hasDrawn()) {
1163 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, mCurrentDragView);
1164 } else {
Winson Chung2c4cf412012-05-08 14:03:21 -07001165 d.deferDragViewCleanupPostAnimation = false;
Adam Cohenfc53cd22011-07-20 15:45:11 -07001166 mCurrentDragView.setVisibility(VISIBLE);
1167 }
Adam Cohene9166b22011-07-08 17:11:11 -07001168 mItemsInvalidated = true;
Adam Cohen4045eb72011-10-06 11:44:26 -07001169 setupContentDimensions(getItemCount());
Adam Cohen716b51e2011-06-30 12:09:54 -07001170 mSuppressOnAdd = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001171 }
1172 mInfo.add(item);
1173 }
1174
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001175 // This is used so the item doesn't immediately appear in the folder when added. In one case
1176 // we need to create the illusion that the item isn't added back to the folder yet, to
1177 // to correspond to the animation of the icon back into the folder. This is
1178 public void hideItem(ShortcutInfo info) {
1179 View v = getViewForInfo(info);
1180 v.setVisibility(INVISIBLE);
1181 }
1182 public void showItem(ShortcutInfo info) {
1183 View v = getViewForInfo(info);
1184 v.setVisibility(VISIBLE);
1185 }
1186
Adam Cohenbfbfd262011-06-13 16:55:12 -07001187 public void onAdd(ShortcutInfo item) {
1188 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001189 // If the item was dropped onto this open folder, we have done the work associated
1190 // with adding the item to the folder, as indicated by mSuppressOnAdd being set
Adam Cohenbfbfd262011-06-13 16:55:12 -07001191 if (mSuppressOnAdd) return;
1192 if (!findAndSetEmptyCells(item)) {
1193 // The current layout is full, can we expand it?
1194 setupContentForNumItems(getItemCount() + 1);
1195 findAndSetEmptyCells(item);
1196 }
1197 createAndAddShortcut(item);
1198 LauncherModel.addOrMoveItemInDatabase(
1199 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
1200 }
1201
Adam Cohena9cf38f2011-05-02 15:36:58 -07001202 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -07001203 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001204 // If this item is being dragged from this open folder, we have already handled
1205 // the work associated with removing the item, so we don't have to do anything here.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001206 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001207 View v = getViewForInfo(item);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001208 mContent.removeView(v);
Adam Cohen2801caf2011-05-13 20:57:39 -07001209 if (mState == STATE_ANIMATING) {
1210 mRearrangeOnClose = true;
1211 } else {
1212 setupContentForNumItems(getItemCount());
1213 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001214 if (getItemCount() <= 1) {
1215 replaceFolderWithFinalItem();
1216 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001217 }
Adam Cohen7c693212011-05-18 15:26:57 -07001218
Adam Cohendf1e4e82011-06-24 15:57:39 -07001219 private View getViewForInfo(ShortcutInfo item) {
1220 for (int j = 0; j < mContent.getCountY(); j++) {
1221 for (int i = 0; i < mContent.getCountX(); i++) {
1222 View v = mContent.getChildAt(i, j);
1223 if (v.getTag() == item) {
1224 return v;
1225 }
1226 }
1227 }
1228 return null;
1229 }
1230
Adam Cohen76078c42011-06-09 15:06:52 -07001231 public void onItemsChanged() {
Adam Cohenac56cff2011-09-28 20:45:37 -07001232 updateTextViewFocus();
Adam Cohen76078c42011-06-09 15:06:52 -07001233 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001234
Adam Cohen76fc0852011-06-17 13:26:23 -07001235 public void onTitleChanged(CharSequence title) {
1236 }
Adam Cohen76078c42011-06-09 15:06:52 -07001237
Adam Cohen7c693212011-05-18 15:26:57 -07001238 public ArrayList<View> getItemsInReadingOrder() {
1239 if (mItemsInvalidated) {
1240 mItemsInReadingOrder.clear();
1241 for (int j = 0; j < mContent.getCountY(); j++) {
1242 for (int i = 0; i < mContent.getCountX(); i++) {
1243 View v = mContent.getChildAt(i, j);
1244 if (v != null) {
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001245 mItemsInReadingOrder.add(v);
Adam Cohen7c693212011-05-18 15:26:57 -07001246 }
1247 }
1248 }
1249 mItemsInvalidated = false;
1250 }
1251 return mItemsInReadingOrder;
1252 }
Adam Cohen8dfcba42011-07-07 16:38:18 -07001253
1254 public void getLocationInDragLayer(int[] loc) {
1255 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
1256 }
Adam Cohenea0818d2011-09-30 20:06:58 -07001257
1258 public void onFocusChange(View v, boolean hasFocus) {
1259 if (v == mFolderName && hasFocus) {
1260 startEditingFolderName();
1261 }
1262 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001263
1264 @Override
1265 public void getHitRectRelativeToDragLayer(Rect outRect) {
1266 getHitRect(outRect);
1267 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001268}