blob: 98809d528caf4880b26b0d70120e49a0163e5673 [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 Cohenc4fe9ea2014-08-18 18:54:10 -070021import android.animation.AnimatorSet;
Adam Cohendf2cc412011-04-27 16:56:57 -070022import android.animation.ObjectAnimator;
23import android.animation.PropertyValuesHolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080024import android.content.Context;
Adam Cohen76fc0852011-06-17 13:26:23 -070025import android.content.res.Resources;
Winson Chung043f2af2012-03-01 16:09:54 -080026import android.graphics.PointF;
Romain Guyfb5411e2010-02-24 10:04:17 -080027import android.graphics.Rect;
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;
Adam Cohen3bf84d32012-05-07 20:17:14 -070034import android.util.Log;
Adam Cohen76fc0852011-06-17 13:26:23 -070035import android.view.ActionMode;
36import android.view.KeyEvent;
Adam Cohendf2cc412011-04-27 16:56:57 -070037import android.view.LayoutInflater;
Adam Cohen76fc0852011-06-17 13:26:23 -070038import android.view.Menu;
39import android.view.MenuItem;
Adam Cohen0c872ba2011-05-05 10:34:16 -070040import android.view.MotionEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.view.View;
Adam Cohen3371da02011-10-25 21:38:29 -070042import android.view.accessibility.AccessibilityEvent;
43import android.view.accessibility.AccessibilityManager;
Adam Cohenc4fe9ea2014-08-18 18:54:10 -070044import android.view.animation.AccelerateInterpolator;
Adam Cohen76fc0852011-06-17 13:26:23 -070045import android.view.inputmethod.EditorInfo;
46import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.widget.LinearLayout;
Adam Cohenf0f4eda2013-06-06 21:27:03 -070048import android.widget.ScrollView;
Adam Cohendf2cc412011-04-27 16:56:57 -070049import android.widget.TextView;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050
Daniel Sandler325dc232013-06-05 22:57:57 -040051import com.android.launcher3.FolderInfo.FolderListener;
Romain Guyedcce092010-03-04 13:03:17 -080052
Adam Cohenc0dcf592011-06-01 15:30:43 -070053import java.util.ArrayList;
Adam Cohen3bf84d32012-05-07 20:17:14 -070054import java.util.Collections;
55import java.util.Comparator;
Adam Cohenc0dcf592011-06-01 15:30:43 -070056
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057/**
58 * Represents a set of icons chosen by the user or generated by the system.
59 */
Adam Cohen8dfcba42011-07-07 16:38:18 -070060public class Folder extends LinearLayout implements DragSource, View.OnClickListener,
Adam Cohenea0818d2011-09-30 20:06:58 -070061 View.OnLongClickListener, DropTarget, FolderListener, TextView.OnEditorActionListener,
62 View.OnFocusChangeListener {
Adam Cohendf2cc412011-04-27 16:56:57 -070063 private static final String TAG = "Launcher.Folder";
64
Adam Cohen4eac29a2011-07-11 17:53:37 -070065 protected DragController mDragController;
66 protected Launcher mLauncher;
67 protected FolderInfo mInfo;
68
Adam Cohendf2cc412011-04-27 16:56:57 -070069 static final int STATE_NONE = -1;
70 static final int STATE_SMALL = 0;
71 static final int STATE_ANIMATING = 1;
72 static final int STATE_OPEN = 2;
73
Jorim Jaggi55bd9722014-01-16 15:30:42 -080074 private static final int CLOSE_FOLDER_DELAY_MS = 150;
75
Adam Cohendf2cc412011-04-27 16:56:57 -070076 private int mExpandDuration;
Adam Cohenc4fe9ea2014-08-18 18:54:10 -070077 private int mMaterialExpandDuration;
78 private int mMaterialExpandStagger;
Adam Cohendf2cc412011-04-27 16:56:57 -070079 protected CellLayout mContent;
Adam Cohenf0f4eda2013-06-06 21:27:03 -070080 private ScrollView mScrollView;
Adam Cohendf2cc412011-04-27 16:56:57 -070081 private final LayoutInflater mInflater;
82 private final IconCache mIconCache;
83 private int mState = STATE_NONE;
Adam Cohenbfbfd262011-06-13 16:55:12 -070084 private static final int REORDER_ANIMATION_DURATION = 230;
Adam Cohenf0f4eda2013-06-06 21:27:03 -070085 private static final int REORDER_DELAY = 250;
Adam Cohen5d518fa2013-12-05 14:16:23 -080086 private static final int ON_EXIT_CLOSE_DELAY = 400;
Adam Cohen2801caf2011-05-13 20:57:39 -070087 private boolean mRearrangeOnClose = false;
88 private FolderIcon mFolderIcon;
89 private int mMaxCountX;
90 private int mMaxCountY;
Adam Cohen78dc83e2011-11-15 17:10:00 -080091 private int mMaxNumItems;
Adam Cohen7c693212011-05-18 15:26:57 -070092 private ArrayList<View> mItemsInReadingOrder = new ArrayList<View>();
93 boolean mItemsInvalidated = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -070094 private ShortcutInfo mCurrentDragInfo;
95 private View mCurrentDragView;
Jorim Jaggi55bd9722014-01-16 15:30:42 -080096 private boolean mIsExternalDrag;
Adam Cohenbfbfd262011-06-13 16:55:12 -070097 boolean mSuppressOnAdd = false;
98 private int[] mTargetCell = new int[2];
99 private int[] mPreviousTargetCell = new int[2];
100 private int[] mEmptyCell = new int[2];
101 private Alarm mReorderAlarm = new Alarm();
102 private Alarm mOnExitAlarm = new Alarm();
Adam Cohen76fc0852011-06-17 13:26:23 -0700103 private int mFolderNameHeight;
Adam Cohen8e776a62011-06-28 18:10:06 -0700104 private Rect mTempRect = new Rect();
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700105 private boolean mDragInProgress = false;
106 private boolean mDeleteFolderOnDropCompleted = false;
107 private boolean mSuppressFolderDeletion = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700108 private boolean mItemAddedBackToSelfViaIcon = false;
Adam Cohenac56cff2011-09-28 20:45:37 -0700109 FolderEditText mFolderName;
Adam Cohen268c4752012-06-06 17:47:33 -0700110 private float mFolderIconPivotX;
111 private float mFolderIconPivotY;
Adam Cohen228da5a2011-07-27 22:23:47 -0700112
Adam Cohen76fc0852011-06-17 13:26:23 -0700113 private boolean mIsEditingName = false;
114 private InputMethodManager mInputMethodManager;
Adam Cohendf2cc412011-04-27 16:56:57 -0700115
Adam Cohena65beee2011-06-27 21:32:23 -0700116 private static String sDefaultFolderName;
117 private static String sHintText;
118
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700119 private int DRAG_MODE_NONE = 0;
120 private int DRAG_MODE_REORDER = 1;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700121 private int mDragMode = DRAG_MODE_NONE;
122
Adam Cohen1960ea42013-11-12 11:33:14 +0000123 // We avoid measuring the scroll view with a 0 width or height, as this
124 // results in CellLayout being measured as UNSPECIFIED, which it does
125 // not support.
126 private static final int MIN_CONTENT_DIMEN = 5;
127
Adam Cohenfb91f302012-06-11 15:45:18 -0700128 private boolean mDestroyed;
129
Alan Viverette4cda5b72013-08-28 17:53:41 -0700130 private AutoScrollHelper mAutoScrollHelper;
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700131
Michael Jurka1e2f4652013-07-08 18:03:46 -0700132 private Runnable mDeferredAction;
133 private boolean mDeferDropAfterUninstall;
134 private boolean mUninstallSuccessful;
135
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136 /**
137 * Used to inflate the Workspace from XML.
138 *
139 * @param context The application's context.
140 * @param attrs The attribtues set containing the Workspace's customization values.
141 */
142 public Folder(Context context, AttributeSet attrs) {
143 super(context, attrs);
Winson Chung5f8afe62013-08-12 16:19:28 -0700144
145 LauncherAppState app = LauncherAppState.getInstance();
146 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147 setAlwaysDrawnWithCacheEnabled(false);
Adam Cohendf2cc412011-04-27 16:56:57 -0700148 mInflater = LayoutInflater.from(context);
Winson Chung5f8afe62013-08-12 16:19:28 -0700149 mIconCache = app.getIconCache();
Adam Cohen78dc83e2011-11-15 17:10:00 -0800150
151 Resources res = getResources();
Michael Jurka414300a2013-08-27 15:42:35 +0200152 mMaxCountX = (int) grid.numColumns;
Nilesh Agrawal5f7099a2014-01-02 15:54:57 -0800153 // Allow scrolling folders when DISABLE_ALL_APPS is true.
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800154 if (LauncherAppState.isDisableAllApps()) {
Nilesh Agrawal5f7099a2014-01-02 15:54:57 -0800155 mMaxCountY = mMaxNumItems = Integer.MAX_VALUE;
156 } else {
157 mMaxCountY = (int) grid.numRows;
158 mMaxNumItems = mMaxCountX * mMaxCountY;
159 }
Adam Cohen76fc0852011-06-17 13:26:23 -0700160
161 mInputMethodManager = (InputMethodManager)
Michael Jurka8b805b12012-04-18 14:23:14 -0700162 getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
Adam Cohen76fc0852011-06-17 13:26:23 -0700163
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700164 mExpandDuration = res.getInteger(R.integer.config_folderExpandDuration);
165 mMaterialExpandDuration = res.getInteger(R.integer.config_materialFolderExpandDuration);
166 mMaterialExpandStagger = res.getInteger(R.integer.config_materialFolderExpandStagger);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700167
168 if (sDefaultFolderName == null) {
169 sDefaultFolderName = res.getString(R.string.folder_name);
170 }
Adam Cohena65beee2011-06-27 21:32:23 -0700171 if (sHintText == null) {
172 sHintText = res.getString(R.string.folder_hint_text);
173 }
Adam Cohen4eac29a2011-07-11 17:53:37 -0700174 mLauncher = (Launcher) context;
Adam Cohenac56cff2011-09-28 20:45:37 -0700175 // We need this view to be focusable in touch mode so that when text editing of the folder
176 // name is complete, we have something to focus on, thus hiding the cursor and giving
177 // reliable behvior when clicking the text field (since it will always gain focus on click).
178 setFocusableInTouchMode(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179 }
180
181 @Override
182 protected void onFinishInflate() {
183 super.onFinishInflate();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700184 mScrollView = (ScrollView) findViewById(R.id.scroll_view);
Adam Cohendf2cc412011-04-27 16:56:57 -0700185 mContent = (CellLayout) findViewById(R.id.folder_content);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700186
Winson Chung5f8afe62013-08-12 16:19:28 -0700187 LauncherAppState app = LauncherAppState.getInstance();
188 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
189
Winson Chung5f8afe62013-08-12 16:19:28 -0700190 mContent.setCellDimensions(grid.folderCellWidthPx, grid.folderCellHeightPx);
Adam Cohen2801caf2011-05-13 20:57:39 -0700191 mContent.setGridSize(0, 0);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700192 mContent.getShortcutsAndWidgets().setMotionEventSplittingEnabled(false);
Adam Cohen2374abf2013-04-16 14:56:57 -0700193 mContent.setInvertIfRtl(true);
Adam Cohenac56cff2011-09-28 20:45:37 -0700194 mFolderName = (FolderEditText) findViewById(R.id.folder_name);
195 mFolderName.setFolder(this);
Adam Cohenea0818d2011-09-30 20:06:58 -0700196 mFolderName.setOnFocusChangeListener(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700197
198 // We find out how tall the text view wants to be (it is set to wrap_content), so that
199 // we can allocate the appropriate amount of space for it.
200 int measureSpec = MeasureSpec.UNSPECIFIED;
201 mFolderName.measure(measureSpec, measureSpec);
202 mFolderNameHeight = mFolderName.getMeasuredHeight();
203
204 // We disable action mode for now since it messes up the view on phones
205 mFolderName.setCustomSelectionActionModeCallback(mActionModeCallback);
Adam Cohen76fc0852011-06-17 13:26:23 -0700206 mFolderName.setOnEditorActionListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700207 mFolderName.setSelectAllOnFocus(true);
Adam Cohen7a14d0b2011-06-24 11:36:35 -0700208 mFolderName.setInputType(mFolderName.getInputType() |
209 InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
Alan Viverette4cda5b72013-08-28 17:53:41 -0700210 mAutoScrollHelper = new FolderAutoScrollHelper(mScrollView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800211 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700212
Adam Cohen76fc0852011-06-17 13:26:23 -0700213 private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
214 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
215 return false;
216 }
217
218 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
219 return false;
220 }
221
222 public void onDestroyActionMode(ActionMode mode) {
223 }
224
225 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
226 return false;
227 }
228 };
229
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800230 public void onClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700231 Object tag = v.getTag();
232 if (tag instanceof ShortcutInfo) {
Adam Cohenb5fe60c2013-06-06 22:03:51 -0700233 mLauncher.onClick(v);
Adam Cohendf2cc412011-04-27 16:56:57 -0700234 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800235 }
236
237 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -0700238 // Return if global dragging is not enabled
239 if (!mLauncher.isDraggingEnabled()) return true;
240
Adam Cohendf2cc412011-04-27 16:56:57 -0700241 Object tag = v.getTag();
242 if (tag instanceof ShortcutInfo) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700243 ShortcutInfo item = (ShortcutInfo) tag;
244 if (!v.isInTouchMode()) {
245 return false;
246 }
247
Winson Chunga6945242014-01-08 14:04:34 -0800248 mLauncher.getLauncherClings().dismissFolderCling(null);
Winson Chung7d7541e2011-09-16 20:14:36 -0700249
Adam Cohendf2cc412011-04-27 16:56:57 -0700250 mLauncher.getWorkspace().onDragStartedWithItem(v);
Adam Cohenac8c8762011-07-13 11:15:27 -0700251 mLauncher.getWorkspace().beginDragShared(v, this);
Adam Cohen76078c42011-06-09 15:06:52 -0700252
253 mCurrentDragInfo = item;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700254 mEmptyCell[0] = item.cellX;
255 mEmptyCell[1] = item.cellY;
256 mCurrentDragView = v;
Adam Cohenfc53cd22011-07-20 15:45:11 -0700257
258 mContent.removeView(mCurrentDragView);
259 mInfo.remove(mCurrentDragInfo);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700260 mDragInProgress = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700261 mItemAddedBackToSelfViaIcon = false;
Adam Cohendf2cc412011-04-27 16:56:57 -0700262 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800263 return true;
264 }
265
Adam Cohen76fc0852011-06-17 13:26:23 -0700266 public boolean isEditingName() {
267 return mIsEditingName;
268 }
269
270 public void startEditingFolderName() {
Adam Cohena65beee2011-06-27 21:32:23 -0700271 mFolderName.setHint("");
Adam Cohen76fc0852011-06-17 13:26:23 -0700272 mIsEditingName = true;
273 }
274
275 public void dismissEditingName() {
276 mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
277 doneEditingFolderName(true);
278 }
279
280 public void doneEditingFolderName(boolean commit) {
Adam Cohena65beee2011-06-27 21:32:23 -0700281 mFolderName.setHint(sHintText);
Adam Cohen1df26a32011-08-12 16:15:23 -0700282 // Convert to a string here to ensure that no other state associated with the text field
283 // gets saved.
Adam Cohen3371da02011-10-25 21:38:29 -0700284 String newTitle = mFolderName.getText().toString();
285 mInfo.setTitle(newTitle);
Adam Cohen76fc0852011-06-17 13:26:23 -0700286 LauncherModel.updateItemInDatabase(mLauncher, mInfo);
Adam Cohenac56cff2011-09-28 20:45:37 -0700287
Adam Cohen3371da02011-10-25 21:38:29 -0700288 if (commit) {
289 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700290 String.format(getContext().getString(R.string.folder_renamed), newTitle));
Adam Cohen3371da02011-10-25 21:38:29 -0700291 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700292 // In order to clear the focus from the text field, we set the focus on ourself. This
293 // ensures that every time the field is clicked, focus is gained, giving reliable behavior.
294 requestFocus();
295
Adam Cohene601a432011-07-26 21:51:30 -0700296 Selection.setSelection((Spannable) mFolderName.getText(), 0, 0);
Adam Cohen76fc0852011-06-17 13:26:23 -0700297 mIsEditingName = false;
298 }
299
300 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
301 if (actionId == EditorInfo.IME_ACTION_DONE) {
302 dismissEditingName();
303 return true;
304 }
305 return false;
306 }
307
308 public View getEditTextRegion() {
309 return mFolderName;
310 }
311
Anjali Koppalf05545f2014-03-10 19:18:43 -0700312 public CellLayout getContent() {
313 return mContent;
314 }
315
Adam Cohen0c872ba2011-05-05 10:34:16 -0700316 /**
317 * We need to handle touch events to prevent them from falling through to the workspace below.
318 */
319 @Override
320 public boolean onTouchEvent(MotionEvent ev) {
321 return true;
322 }
323
Joe Onorato00acb122009-08-04 16:04:30 -0400324 public void setDragController(DragController dragController) {
325 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800326 }
327
Adam Cohen2801caf2011-05-13 20:57:39 -0700328 void setFolderIcon(FolderIcon icon) {
329 mFolderIcon = icon;
330 }
331
Adam Cohen3371da02011-10-25 21:38:29 -0700332 @Override
333 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
334 // When the folder gets focus, we don't want to announce the list of items.
335 return true;
336 }
337
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800338 /**
339 * @return the FolderInfo object associated with this folder
340 */
341 FolderInfo getInfo() {
342 return mInfo;
343 }
344
Adam Cohen3bf84d32012-05-07 20:17:14 -0700345 private class GridComparator implements Comparator<ShortcutInfo> {
346 int mNumCols;
347 public GridComparator(int numCols) {
348 mNumCols = numCols;
349 }
350
351 @Override
352 public int compare(ShortcutInfo lhs, ShortcutInfo rhs) {
353 int lhIndex = lhs.cellY * mNumCols + lhs.cellX;
354 int rhIndex = rhs.cellY * mNumCols + rhs.cellX;
355 return (lhIndex - rhIndex);
356 }
Adam Cohen3bf84d32012-05-07 20:17:14 -0700357 }
358
359 private void placeInReadingOrder(ArrayList<ShortcutInfo> items) {
360 int maxX = 0;
361 int count = items.size();
362 for (int i = 0; i < count; i++) {
363 ShortcutInfo item = items.get(i);
364 if (item.cellX > maxX) {
365 maxX = item.cellX;
366 }
367 }
Adam Cohen691a5792012-05-11 14:27:30 -0700368
369 GridComparator gridComparator = new GridComparator(maxX + 1);
Adam Cohen3bf84d32012-05-07 20:17:14 -0700370 Collections.sort(items, gridComparator);
371 final int countX = mContent.getCountX();
372 for (int i = 0; i < count; i++) {
373 int x = i % countX;
374 int y = i / countX;
375 ShortcutInfo item = items.get(i);
376 item.cellX = x;
377 item.cellY = y;
378 }
379 }
380
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800381 void bind(FolderInfo info) {
382 mInfo = info;
Adam Cohendf2cc412011-04-27 16:56:57 -0700383 ArrayList<ShortcutInfo> children = info.contents;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700384 ArrayList<ShortcutInfo> overflow = new ArrayList<ShortcutInfo>();
Adam Cohen7c693212011-05-18 15:26:57 -0700385 setupContentForNumItems(children.size());
Adam Cohen3bf84d32012-05-07 20:17:14 -0700386 placeInReadingOrder(children);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700387 int count = 0;
Adam Cohendf2cc412011-04-27 16:56:57 -0700388 for (int i = 0; i < children.size(); i++) {
389 ShortcutInfo child = (ShortcutInfo) children.get(i);
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800390 if (createAndAddShortcut(child) == null) {
Adam Cohenc508b2d2011-06-28 14:41:44 -0700391 overflow.add(child);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700392 } else {
393 count++;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700394 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700395 }
Adam Cohenc508b2d2011-06-28 14:41:44 -0700396
Adam Cohen0057bbc2011-08-12 18:30:51 -0700397 // We rearrange the items in case there are any empty gaps
398 setupContentForNumItems(count);
399
Jason Monk4ff73882014-04-24 16:48:07 -0400400 // If our folder has too many items we prune them from the list. This is an issue
Adam Cohenc508b2d2011-06-28 14:41:44 -0700401 // when upgrading from the old Folders implementation which could contain an unlimited
402 // number of items.
403 for (ShortcutInfo item: overflow) {
404 mInfo.remove(item);
405 LauncherModel.deleteItemFromDatabase(mLauncher, item);
406 }
407
Adam Cohen4dbe6d92011-05-18 17:14:15 -0700408 mItemsInvalidated = true;
Adam Cohenac56cff2011-09-28 20:45:37 -0700409 updateTextViewFocus();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700410 mInfo.addListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700411
Adam Cohenafb01ee2011-06-23 15:38:03 -0700412 if (!sDefaultFolderName.contentEquals(mInfo.title)) {
Adam Cohen4ef610f2011-06-21 22:37:36 -0700413 mFolderName.setText(mInfo.title);
414 } else {
415 mFolderName.setText("");
416 }
Adam Cohen691a5792012-05-11 14:27:30 -0700417 updateItemLocationsInDatabase();
Winson Chung33231f52013-12-09 16:57:45 -0800418
419 // In case any children didn't come across during loading, clean up the folder accordingly
420 mFolderIcon.post(new Runnable() {
421 public void run() {
422 if (getItemCount() <= 1) {
423 replaceFolderWithFinalItem();
424 }
425 }
426 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700427 }
428
429 /**
430 * Creates a new UserFolder, inflated from R.layout.user_folder.
431 *
432 * @param context The application's context.
433 *
434 * @return A new UserFolder.
435 */
436 static Folder fromXml(Context context) {
437 return (Folder) LayoutInflater.from(context).inflate(R.layout.user_folder, null);
438 }
439
440 /**
441 * This method is intended to make the UserFolder to be visually identical in size and position
442 * to its associated FolderIcon. This allows for a seamless transition into the expanded state.
443 */
444 private void positionAndSizeAsIcon() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700445 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800446 setScaleX(0.8f);
447 setScaleY(0.8f);
448 setAlpha(0f);
Adam Cohendf2cc412011-04-27 16:56:57 -0700449 mState = STATE_SMALL;
450 }
451
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700452 private void prepareReveal() {
453 setScaleX(1f);
454 setScaleY(1f);
455 setAlpha(1f);
456 mState = STATE_SMALL;
457 }
458
Adam Cohendf2cc412011-04-27 16:56:57 -0700459 public void animateOpen() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700460 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen6441de02011-12-14 14:25:32 -0800461
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700462 Animator openFolderAnim = null;
463 final Runnable onCompleteRunnable;
464 if (!Utilities.isLmp()) {
465 positionAndSizeAsIcon();
466 centerAboutIcon();
467
468 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1);
469 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
470 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
471 final ObjectAnimator oa =
472 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
473 oa.setDuration(mExpandDuration);
474 openFolderAnim = oa;
475
476 setLayerType(LAYER_TYPE_HARDWARE, null);
477 onCompleteRunnable = new Runnable() {
478 @Override
479 public void run() {
480 setLayerType(LAYER_TYPE_NONE, null);
481 }
482 };
483 } else {
484 prepareReveal();
485 centerAboutIcon();
486
487 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
488 int height = getFolderHeight();
489
490 float transX = - 0.075f * (width / 2 - getPivotX());
491 float transY = - 0.075f * (height / 2 - getPivotY());
492 setTranslationX(transX);
493 setTranslationY(transY);
494 PropertyValuesHolder tx = PropertyValuesHolder.ofFloat("translationX", transX, 0);
495 PropertyValuesHolder ty = PropertyValuesHolder.ofFloat("translationY", transY, 0);
496
497 int rx = (int) Math.max(Math.max(width - getPivotX(), 0), getPivotX());
498 int ry = (int) Math.max(Math.max(height - getPivotY(), 0), getPivotY());
499 float radius = (float) Math.sqrt(rx * rx + ry * ry);
500 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
501 Animator reveal = LauncherAnimUtils.createCircularReveal(this, (int) getPivotX(),
502 (int) getPivotY(), 0, radius);
503 reveal.setDuration(mMaterialExpandDuration);
504 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
505
506 mContent.setAlpha(0f);
507 Animator iconsAlpha = LauncherAnimUtils.ofFloat(mContent, "alpha", 0f, 1f);
508 iconsAlpha.setDuration(mMaterialExpandDuration);
509 iconsAlpha.setStartDelay(mMaterialExpandStagger);
510 iconsAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
511
512 mFolderName.setAlpha(0f);
513 Animator textAlpha = LauncherAnimUtils.ofFloat(mFolderName, "alpha", 0f, 1f);
514 textAlpha.setDuration(mMaterialExpandDuration);
515 textAlpha.setStartDelay(mMaterialExpandStagger);
516 textAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
517
518 Animator drift = LauncherAnimUtils.ofPropertyValuesHolder(this, tx, ty);
519 drift.setDuration(mMaterialExpandDuration);
520 drift.setStartDelay(mMaterialExpandStagger);
521 drift.setInterpolator(new LogDecelerateInterpolator(60, 0));
522
523 anim.play(drift);
524 anim.play(iconsAlpha);
525 anim.play(textAlpha);
526 anim.play(reveal);
527
528 openFolderAnim = anim;
529
530 mContent.setLayerType(LAYER_TYPE_HARDWARE, null);
531 onCompleteRunnable = new Runnable() {
532 @Override
533 public void run() {
534 mContent.setLayerType(LAYER_TYPE_NONE, null);
535 }
536 };
537 }
538 openFolderAnim.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700539 @Override
540 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700541 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700542 String.format(getContext().getString(R.string.folder_opened),
Adam Cohen3371da02011-10-25 21:38:29 -0700543 mContent.getCountX(), mContent.getCountY()));
Adam Cohendf2cc412011-04-27 16:56:57 -0700544 mState = STATE_ANIMATING;
545 }
546 @Override
547 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700548 mState = STATE_OPEN;
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700549
550 if (onCompleteRunnable != null) {
551 onCompleteRunnable.run();
552 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800553
554 // Only show cling if we are not in the middle of a drag - this would be quite jarring.
555 if (!mDragController.isDragging()) {
556 Cling cling = mLauncher.getLauncherClings().showFoldersCling();
557 if (cling != null) {
558 cling.bringScrimToFront();
559 bringToFront();
560 cling.bringToFront();
561 }
Winson Chung7a74ac92011-09-20 17:43:51 -0700562 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700563 setFocusOnFirstChild();
Adam Cohendf2cc412011-04-27 16:56:57 -0700564 }
565 });
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700566 openFolderAnim.start();
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800567
568 // Make sure the folder picks up the last drag move even if the finger doesn't move.
569 if (mDragController.isDragging()) {
570 mDragController.forceTouchMove();
571 }
572 }
573
574 public void beginExternalDrag(ShortcutInfo item) {
575 setupContentForNumItems(getItemCount() + 1);
576 findAndSetEmptyCells(item);
577
578 mCurrentDragInfo = item;
579 mEmptyCell[0] = item.cellX;
580 mEmptyCell[1] = item.cellY;
581 mIsExternalDrag = true;
582
583 mDragInProgress = true;
Adam Cohendf2cc412011-04-27 16:56:57 -0700584 }
585
Adam Cohen3371da02011-10-25 21:38:29 -0700586 private void sendCustomAccessibilityEvent(int type, String text) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700587 AccessibilityManager accessibilityManager = (AccessibilityManager)
588 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
589 if (accessibilityManager.isEnabled()) {
Adam Cohen3371da02011-10-25 21:38:29 -0700590 AccessibilityEvent event = AccessibilityEvent.obtain(type);
591 onInitializeAccessibilityEvent(event);
592 event.getText().add(text);
Michael Jurka8b805b12012-04-18 14:23:14 -0700593 accessibilityManager.sendAccessibilityEvent(event);
Adam Cohen3371da02011-10-25 21:38:29 -0700594 }
595 }
596
Adam Cohenac56cff2011-09-28 20:45:37 -0700597 private void setFocusOnFirstChild() {
598 View firstChild = mContent.getChildAt(0, 0);
599 if (firstChild != null) {
600 firstChild.requestFocus();
601 }
602 }
603
Adam Cohendf2cc412011-04-27 16:56:57 -0700604 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700605 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800606 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
607 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
608 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
Michael Jurka032e6ba2013-04-22 15:08:42 +0200609 final ObjectAnimator oa =
Michael Jurka2ecf9952012-06-18 12:52:28 -0700610 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohendf2cc412011-04-27 16:56:57 -0700611
Adam Cohen2801caf2011-05-13 20:57:39 -0700612 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700613 @Override
614 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700615 onCloseComplete();
Michael Jurka0121c3e2012-05-31 08:36:04 -0700616 setLayerType(LAYER_TYPE_NONE, null);
Adam Cohen2801caf2011-05-13 20:57:39 -0700617 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700618 }
619 @Override
620 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700621 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700622 getContext().getString(R.string.folder_closed));
Adam Cohendf2cc412011-04-27 16:56:57 -0700623 mState = STATE_ANIMATING;
624 }
625 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700626 oa.setDuration(mExpandDuration);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700627 setLayerType(LAYER_TYPE_HARDWARE, null);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100628 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700629 }
630
Adam Cohencb3382b2011-05-24 14:07:08 -0700631 public boolean acceptDrop(DragObject d) {
632 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700633 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700634 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
635 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
636 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700637 }
638
Adam Cohendf2cc412011-04-27 16:56:57 -0700639 protected boolean findAndSetEmptyCells(ShortcutInfo item) {
640 int[] emptyCell = new int[2];
641 if (mContent.findCellForSpan(emptyCell, item.spanX, item.spanY)) {
642 item.cellX = emptyCell[0];
643 item.cellY = emptyCell[1];
Adam Cohendf2cc412011-04-27 16:56:57 -0700644 return true;
645 } else {
646 return false;
647 }
648 }
649
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800650 protected View createAndAddShortcut(ShortcutInfo item) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700651 final BubbleTextView textView =
652 (BubbleTextView) mInflater.inflate(R.layout.folder_application, this, false);
653 textView.applyFromShortcutInfo(item, mIconCache, false);
654
Adam Cohendf2cc412011-04-27 16:56:57 -0700655 textView.setOnClickListener(this);
656 textView.setOnLongClickListener(this);
657
Adam Cohenc508b2d2011-06-28 14:41:44 -0700658 // We need to check here to verify that the given item's location isn't already occupied
Adam Cohen3bf84d32012-05-07 20:17:14 -0700659 // by another item.
Adam Cohen0057bbc2011-08-12 18:30:51 -0700660 if (mContent.getChildAt(item.cellX, item.cellY) != null || item.cellX < 0 || item.cellY < 0
661 || item.cellX >= mContent.getCountX() || item.cellY >= mContent.getCountY()) {
Jason Monk4ff73882014-04-24 16:48:07 -0400662 // This shouldn't happen, log it.
Adam Cohen3bf84d32012-05-07 20:17:14 -0700663 Log.e(TAG, "Folder order not properly persisted during bind");
Adam Cohenc508b2d2011-06-28 14:41:44 -0700664 if (!findAndSetEmptyCells(item)) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800665 return null;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700666 }
667 }
668
Adam Cohendf2cc412011-04-27 16:56:57 -0700669 CellLayout.LayoutParams lp =
670 new CellLayout.LayoutParams(item.cellX, item.cellY, item.spanX, item.spanY);
671 boolean insert = false;
Adam Cohenac56cff2011-09-28 20:45:37 -0700672 textView.setOnKeyListener(new FolderKeyEventListener());
Adam Cohendf2cc412011-04-27 16:56:57 -0700673 mContent.addViewToCellLayout(textView, insert ? 0 : -1, (int)item.id, lp, true);
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800674 return textView;
Adam Cohendf2cc412011-04-27 16:56:57 -0700675 }
676
Adam Cohencb3382b2011-05-24 14:07:08 -0700677 public void onDragEnter(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700678 mPreviousTargetCell[0] = -1;
679 mPreviousTargetCell[1] = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700680 mOnExitAlarm.cancelAlarm();
681 }
682
683 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
684 public void onAlarm(Alarm alarm) {
685 realTimeReorder(mEmptyCell, mTargetCell);
686 }
687 };
688
689 boolean readingOrderGreaterThan(int[] v1, int[] v2) {
690 if (v1[1] > v2[1] || (v1[1] == v2[1] && v1[0] > v2[0])) {
691 return true;
692 } else {
693 return false;
694 }
695 }
696
697 private void realTimeReorder(int[] empty, int[] target) {
698 boolean wrap;
699 int startX;
700 int endX;
701 int startY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700702 int delay = 0;
703 float delayAmount = 30;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700704 if (readingOrderGreaterThan(target, empty)) {
705 wrap = empty[0] >= mContent.getCountX() - 1;
706 startY = wrap ? empty[1] + 1 : empty[1];
707 for (int y = startY; y <= target[1]; y++) {
708 startX = y == empty[1] ? empty[0] + 1 : 0;
709 endX = y < target[1] ? mContent.getCountX() - 1 : target[0];
710 for (int x = startX; x <= endX; x++) {
711 View v = mContent.getChildAt(x,y);
712 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen482ed822012-03-02 14:15:13 -0800713 REORDER_ANIMATION_DURATION, delay, true, true)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700714 empty[0] = x;
715 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700716 delay += delayAmount;
717 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700718 }
719 }
720 }
721 } else {
722 wrap = empty[0] == 0;
723 startY = wrap ? empty[1] - 1 : empty[1];
724 for (int y = startY; y >= target[1]; y--) {
725 startX = y == empty[1] ? empty[0] - 1 : mContent.getCountX() - 1;
726 endX = y > target[1] ? 0 : target[0];
727 for (int x = startX; x >= endX; x--) {
728 View v = mContent.getChildAt(x,y);
729 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen482ed822012-03-02 14:15:13 -0800730 REORDER_ANIMATION_DURATION, delay, true, true)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700731 empty[0] = x;
732 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700733 delay += delayAmount;
734 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700735 }
736 }
737 }
738 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700739 }
740
Adam Cohen2374abf2013-04-16 14:56:57 -0700741 public boolean isLayoutRtl() {
742 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
743 }
744
Adam Cohencb3382b2011-05-24 14:07:08 -0700745 public void onDragOver(DragObject d) {
Alan Viverette4cda5b72013-08-28 17:53:41 -0700746 final DragView dragView = d.dragView;
747 final int scrollOffset = mScrollView.getScrollY();
748 final float[] r = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, dragView, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700749 r[0] -= getPaddingLeft();
750 r[1] -= getPaddingTop();
751
Alan Viverette4cda5b72013-08-28 17:53:41 -0700752 final long downTime = SystemClock.uptimeMillis();
753 final MotionEvent translatedEv = MotionEvent.obtain(
754 downTime, downTime, MotionEvent.ACTION_MOVE, d.x, d.y, 0);
755
756 if (!mAutoScrollHelper.isEnabled()) {
757 mAutoScrollHelper.setEnabled(true);
758 }
759
760 final boolean handled = mAutoScrollHelper.onTouch(this, translatedEv);
761 translatedEv.recycle();
762
763 if (handled) {
764 mReorderAlarm.cancelAlarm();
765 } else {
766 mTargetCell = mContent.findNearestArea(
767 (int) r[0], (int) r[1] + scrollOffset, 1, 1, mTargetCell);
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700768 if (isLayoutRtl()) {
769 mTargetCell[0] = mContent.getCountX() - mTargetCell[0] - 1;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700770 }
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700771 if (mTargetCell[0] != mPreviousTargetCell[0]
772 || mTargetCell[1] != mPreviousTargetCell[1]) {
773 mReorderAlarm.cancelAlarm();
774 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
775 mReorderAlarm.setAlarm(REORDER_DELAY);
776 mPreviousTargetCell[0] = mTargetCell[0];
777 mPreviousTargetCell[1] = mTargetCell[1];
778 mDragMode = DRAG_MODE_REORDER;
779 } else {
780 mDragMode = DRAG_MODE_NONE;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700781 }
Adam Cohenbfbfd262011-06-13 16:55:12 -0700782 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700783 }
784
Adam Cohenbfbfd262011-06-13 16:55:12 -0700785 // This is used to compute the visual center of the dragView. The idea is that
786 // the visual center represents the user's interpretation of where the item is, and hence
787 // is the appropriate point to use when determining drop location.
788 private float[] getDragViewVisualCenter(int x, int y, int xOffset, int yOffset,
789 DragView dragView, float[] recycle) {
790 float res[];
791 if (recycle == null) {
792 res = new float[2];
793 } else {
794 res = recycle;
795 }
796
797 // These represent the visual top and left of drag view if a dragRect was provided.
798 // If a dragRect was not provided, then they correspond to the actual view left and
799 // top, as the dragRect is in that case taken to be the entire dragView.
800 // R.dimen.dragViewOffsetY.
801 int left = x - xOffset;
802 int top = y - yOffset;
803
804 // In order to find the visual center, we shift by half the dragRect
805 res[0] = left + dragView.getDragRegion().width() / 2;
806 res[1] = top + dragView.getDragRegion().height() / 2;
807
808 return res;
809 }
810
811 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
812 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700813 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700814 }
815 };
816
Adam Cohen95bb8002011-07-03 23:40:28 -0700817 public void completeDragExit() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700818 mLauncher.closeFolder();
819 mCurrentDragInfo = null;
820 mCurrentDragView = null;
821 mSuppressOnAdd = false;
822 mRearrangeOnClose = true;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800823 mIsExternalDrag = false;
Adam Cohen3e8f8112011-07-02 18:03:00 -0700824 }
825
Adam Cohencb3382b2011-05-24 14:07:08 -0700826 public void onDragExit(DragObject d) {
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700827 // Exiting folder; stop the auto scroller.
Alan Viverette4cda5b72013-08-28 17:53:41 -0700828 mAutoScrollHelper.setEnabled(false);
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700829 // We only close the folder if this is a true drag exit, ie. not because
830 // a drop has occurred above the folder.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700831 if (!d.dragComplete) {
832 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
833 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
834 }
835 mReorderAlarm.cancelAlarm();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700836 mDragMode = DRAG_MODE_NONE;
Adam Cohen2801caf2011-05-13 20:57:39 -0700837 }
838
Michael Jurka1e2f4652013-07-08 18:03:46 -0700839 public void onDropCompleted(final View target, final DragObject d,
840 final boolean isFlingToDelete, final boolean success) {
841 if (mDeferDropAfterUninstall) {
Michael Jurkaf3007582013-08-21 14:33:57 +0200842 Log.d(TAG, "Deferred handling drop because waiting for uninstall.");
Michael Jurka1e2f4652013-07-08 18:03:46 -0700843 mDeferredAction = new Runnable() {
844 public void run() {
845 onDropCompleted(target, d, isFlingToDelete, success);
846 mDeferredAction = null;
847 }
848 };
849 return;
850 }
851
852 boolean beingCalledAfterUninstall = mDeferredAction != null;
853 boolean successfulDrop =
854 success && (!beingCalledAfterUninstall || mUninstallSuccessful);
Winson Chung5f8afe62013-08-12 16:19:28 -0700855
Michael Jurka1e2f4652013-07-08 18:03:46 -0700856 if (successfulDrop) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800857 if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon && target != this) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700858 replaceFolderWithFinalItem();
859 }
860 } else {
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700861 setupContentForNumItems(getItemCount());
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700862 // The drag failed, we need to return the item to the folder
863 mFolderIcon.onDrop(d);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700864 }
865
866 if (target != this) {
867 if (mOnExitAlarm.alarmPending()) {
868 mOnExitAlarm.cancelAlarm();
Michael Jurka54554252013-08-01 12:52:23 +0200869 if (!successfulDrop) {
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700870 mSuppressFolderDeletion = true;
871 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700872 completeDragExit();
873 }
Adam Cohen9c58d822013-12-13 17:18:10 -0800874 }
875
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700876 mDeleteFolderOnDropCompleted = false;
877 mDragInProgress = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700878 mItemAddedBackToSelfViaIcon = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700879 mCurrentDragInfo = null;
880 mCurrentDragView = null;
881 mSuppressOnAdd = false;
Adam Cohen4045eb72011-10-06 11:44:26 -0700882
883 // Reordering may have occured, and we need to save the new item locations. We do this once
884 // at the end to prevent unnecessary database operations.
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700885 updateItemLocationsInDatabaseBatch();
Adam Cohen4045eb72011-10-06 11:44:26 -0700886 }
887
Michael Jurka1e2f4652013-07-08 18:03:46 -0700888 public void deferCompleteDropAfterUninstallActivity() {
889 mDeferDropAfterUninstall = true;
890 }
891
892 public void onUninstallActivityReturned(boolean success) {
893 mDeferDropAfterUninstall = false;
894 mUninstallSuccessful = success;
895 if (mDeferredAction != null) {
896 mDeferredAction.run();
897 }
898 }
899
Winson Chunga48487a2012-03-20 16:19:37 -0700900 @Override
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800901 public float getIntrinsicIconScaleFactor() {
902 return 1f;
903 }
904
905 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800906 public boolean supportsFlingToDelete() {
907 return true;
908 }
909
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000910 @Override
911 public boolean supportsAppInfoDropTarget() {
912 return false;
913 }
914
915 @Override
916 public boolean supportsDeleteDropTarget() {
917 return true;
918 }
919
Winson Chunga48487a2012-03-20 16:19:37 -0700920 public void onFlingToDelete(DragObject d, int x, int y, PointF vec) {
921 // Do nothing
922 }
923
924 @Override
925 public void onFlingToDeleteCompleted() {
926 // Do nothing
927 }
928
Adam Cohen4045eb72011-10-06 11:44:26 -0700929 private void updateItemLocationsInDatabase() {
930 ArrayList<View> list = getItemsInReadingOrder();
931 for (int i = 0; i < list.size(); i++) {
932 View v = list.get(i);
933 ItemInfo info = (ItemInfo) v.getTag();
934 LauncherModel.moveItemInDatabase(mLauncher, info, mInfo.id, 0,
Winson Chung33231f52013-12-09 16:57:45 -0800935 info.cellX, info.cellY);
Adam Cohen4045eb72011-10-06 11:44:26 -0700936 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700937 }
Adam Cohen228da5a2011-07-27 22:23:47 -0700938
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700939 private void updateItemLocationsInDatabaseBatch() {
940 ArrayList<View> list = getItemsInReadingOrder();
941 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
942 for (int i = 0; i < list.size(); i++) {
943 View v = list.get(i);
944 ItemInfo info = (ItemInfo) v.getTag();
945 items.add(info);
946 }
947
948 LauncherModel.moveItemsInDatabase(mLauncher, items, mInfo.id, 0);
949 }
950
Adam Cohene25af792013-06-06 23:08:25 -0700951 public void addItemLocationsInDatabase() {
952 ArrayList<View> list = getItemsInReadingOrder();
953 for (int i = 0; i < list.size(); i++) {
954 View v = list.get(i);
955 ItemInfo info = (ItemInfo) v.getTag();
956 LauncherModel.addItemToDatabase(mLauncher, info, mInfo.id, 0,
957 info.cellX, info.cellY, false);
958 }
959 }
960
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700961 public void notifyDrop() {
962 if (mDragInProgress) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700963 mItemAddedBackToSelfViaIcon = true;
Adam Cohen76078c42011-06-09 15:06:52 -0700964 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700965 }
966
967 public boolean isDropEnabled() {
968 return true;
969 }
970
Adam Cohen4045eb72011-10-06 11:44:26 -0700971 private void setupContentDimensions(int count) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700972 ArrayList<View> list = getItemsInReadingOrder();
973
974 int countX = mContent.getCountX();
975 int countY = mContent.getCountY();
Adam Cohen7c693212011-05-18 15:26:57 -0700976 boolean done = false;
Adam Cohen2801caf2011-05-13 20:57:39 -0700977
Adam Cohen7c693212011-05-18 15:26:57 -0700978 while (!done) {
979 int oldCountX = countX;
980 int oldCountY = countY;
981 if (countX * countY < count) {
982 // Current grid is too small, expand it
Adam Cohen78dc83e2011-11-15 17:10:00 -0800983 if ((countX <= countY || countY == mMaxCountY) && countX < mMaxCountX) {
Adam Cohen7c693212011-05-18 15:26:57 -0700984 countX++;
985 } else if (countY < mMaxCountY) {
986 countY++;
987 }
988 if (countY == 0) countY++;
989 } else if ((countY - 1) * countX >= count && countY >= countX) {
990 countY = Math.max(0, countY - 1);
991 } else if ((countX - 1) * countY >= count) {
992 countX = Math.max(0, countX - 1);
Adam Cohen2801caf2011-05-13 20:57:39 -0700993 }
Adam Cohen7c693212011-05-18 15:26:57 -0700994 done = countX == oldCountX && countY == oldCountY;
Adam Cohen2801caf2011-05-13 20:57:39 -0700995 }
Adam Cohen7c693212011-05-18 15:26:57 -0700996 mContent.setGridSize(countX, countY);
Adam Cohen2801caf2011-05-13 20:57:39 -0700997 arrangeChildren(list);
998 }
999
1000 public boolean isFull() {
Adam Cohen78dc83e2011-11-15 17:10:00 -08001001 return getItemCount() >= mMaxNumItems;
Adam Cohen2801caf2011-05-13 20:57:39 -07001002 }
1003
1004 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -07001005 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -07001006
Winson Chung892c74d2013-08-22 16:15:50 -07001007 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -07001008 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001009 int height = getFolderHeight();
Adam Cohen2801caf2011-05-13 20:57:39 -07001010
Adam Cohen307fe232012-08-16 17:55:58 -07001011 float scale = parent.getDescendantRectRelativeToSelf(mFolderIcon, mTempRect);
Adam Cohen8e776a62011-06-28 18:10:06 -07001012
Winson Chungaf40f202013-09-18 18:26:31 -07001013 LauncherAppState app = LauncherAppState.getInstance();
1014 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1015
Adam Cohen307fe232012-08-16 17:55:58 -07001016 int centerX = (int) (mTempRect.left + mTempRect.width() * scale / 2);
1017 int centerY = (int) (mTempRect.top + mTempRect.height() * scale / 2);
Adam Cohen2801caf2011-05-13 20:57:39 -07001018 int centeredLeft = centerX - width / 2;
1019 int centeredTop = centerY - height / 2;
Winson Chung3057b1c2013-10-10 17:35:35 -07001020 int currentPage = mLauncher.getWorkspace().getNextPage();
Adam Cohen7cc1bc42011-12-13 21:28:43 -08001021 // In case the workspace is scrolling, we need to use the final scroll to compute
1022 // the folders bounds.
1023 mLauncher.getWorkspace().setFinalScrollForPageChange(currentPage);
Adam Cohen35e7e642011-07-17 14:47:18 -07001024 // We first fetch the currently visible CellLayoutChildren
Adam Cohen7cc1bc42011-12-13 21:28:43 -08001025 CellLayout currentLayout = (CellLayout) mLauncher.getWorkspace().getChildAt(currentPage);
Michael Jurkaa52570f2012-03-20 03:18:20 -07001026 ShortcutAndWidgetContainer boundingLayout = currentLayout.getShortcutsAndWidgets();
Adam Cohen35e7e642011-07-17 14:47:18 -07001027 Rect bounds = new Rect();
1028 parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
Adam Cohen7cc1bc42011-12-13 21:28:43 -08001029 // We reset the workspaces scroll
1030 mLauncher.getWorkspace().resetFinalScrollForPageChange(currentPage);
Adam Cohen2801caf2011-05-13 20:57:39 -07001031
Adam Cohen35e7e642011-07-17 14:47:18 -07001032 // We need to bound the folder to the currently visible CellLayoutChildren
1033 int left = Math.min(Math.max(bounds.left, centeredLeft),
1034 bounds.left + bounds.width() - width);
1035 int top = Math.min(Math.max(bounds.top, centeredTop),
1036 bounds.top + bounds.height() - height);
Winson Chungaf40f202013-09-18 18:26:31 -07001037 if (grid.isPhone() && (grid.availableWidthPx - width) < grid.iconSizePx) {
1038 // Center the folder if it is full (on phones only)
1039 left = (grid.availableWidthPx - width) / 2;
1040 } else if (width >= bounds.width()) {
1041 // If the folder doesn't fit within the bounds, center it about the desired bounds
Adam Cohen35e7e642011-07-17 14:47:18 -07001042 left = bounds.left + (bounds.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -07001043 }
Adam Cohen35e7e642011-07-17 14:47:18 -07001044 if (height >= bounds.height()) {
1045 top = bounds.top + (bounds.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -07001046 }
Adam Cohen2801caf2011-05-13 20:57:39 -07001047
1048 int folderPivotX = width / 2 + (centeredLeft - left);
1049 int folderPivotY = height / 2 + (centeredTop - top);
1050 setPivotX(folderPivotX);
1051 setPivotY(folderPivotY);
Adam Cohen268c4752012-06-06 17:47:33 -07001052 mFolderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
Adam Cohen2801caf2011-05-13 20:57:39 -07001053 (1.0f * folderPivotX / width));
Adam Cohen268c4752012-06-06 17:47:33 -07001054 mFolderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
Adam Cohen2801caf2011-05-13 20:57:39 -07001055 (1.0f * folderPivotY / height));
Adam Cohen3bf84d32012-05-07 20:17:14 -07001056
Adam Cohen662b5982011-12-13 17:45:21 -08001057 lp.width = width;
1058 lp.height = height;
1059 lp.x = left;
1060 lp.y = top;
Adam Cohen2801caf2011-05-13 20:57:39 -07001061 }
1062
Adam Cohen268c4752012-06-06 17:47:33 -07001063 float getPivotXForIconAnimation() {
1064 return mFolderIconPivotX;
1065 }
1066 float getPivotYForIconAnimation() {
1067 return mFolderIconPivotY;
1068 }
1069
Adam Cohen2801caf2011-05-13 20:57:39 -07001070 private void setupContentForNumItems(int count) {
Adam Cohen4045eb72011-10-06 11:44:26 -07001071 setupContentDimensions(count);
Adam Cohen2801caf2011-05-13 20:57:39 -07001072
Adam Cohen8e776a62011-06-28 18:10:06 -07001073 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -07001074 if (lp == null) {
Adam Cohen8e776a62011-06-28 18:10:06 -07001075 lp = new DragLayer.LayoutParams(0, 0);
1076 lp.customPosition = true;
Adam Cohen2801caf2011-05-13 20:57:39 -07001077 setLayoutParams(lp);
1078 }
1079 centerAboutIcon();
1080 }
1081
Winson Chung892c74d2013-08-22 16:15:50 -07001082 private int getContentAreaHeight() {
1083 LauncherAppState app = LauncherAppState.getInstance();
1084 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1085 Rect workspacePadding = grid.getWorkspacePadding(grid.isLandscape ?
1086 CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
1087 int maxContentAreaHeight = grid.availableHeightPx -
Winson Chung892c74d2013-08-22 16:15:50 -07001088 workspacePadding.top - workspacePadding.bottom -
Winson Chung892c74d2013-08-22 16:15:50 -07001089 mFolderNameHeight;
Adam Cohen1960ea42013-11-12 11:33:14 +00001090 int height = Math.min(maxContentAreaHeight,
Winson Chung892c74d2013-08-22 16:15:50 -07001091 mContent.getDesiredHeight());
Adam Cohen1960ea42013-11-12 11:33:14 +00001092 return Math.max(height, MIN_CONTENT_DIMEN);
1093 }
1094
1095 private int getContentAreaWidth() {
1096 return Math.max(mContent.getDesiredWidth(), MIN_CONTENT_DIMEN);
Winson Chung892c74d2013-08-22 16:15:50 -07001097 }
1098
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001099 private int getFolderHeight() {
Winson Chung892c74d2013-08-22 16:15:50 -07001100 int height = getPaddingTop() + getPaddingBottom()
1101 + getContentAreaHeight() + mFolderNameHeight;
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001102 return height;
1103 }
Adam Cohen234c4cd2011-07-17 21:03:04 -07001104
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001105 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001106 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
1107 int height = getFolderHeight();
Adam Cohen1960ea42013-11-12 11:33:14 +00001108 int contentAreaWidthSpec = MeasureSpec.makeMeasureSpec(getContentAreaWidth(),
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001109 MeasureSpec.EXACTLY);
Winson Chung892c74d2013-08-22 16:15:50 -07001110 int contentAreaHeightSpec = MeasureSpec.makeMeasureSpec(getContentAreaHeight(),
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001111 MeasureSpec.EXACTLY);
Adam Cohen1960ea42013-11-12 11:33:14 +00001112
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08001113 if (LauncherAppState.isDisableAllApps()) {
Nilesh Agrawal5f7099a2014-01-02 15:54:57 -08001114 // Don't cap the height of the content to allow scrolling.
1115 mContent.setFixedSize(getContentAreaWidth(), mContent.getDesiredHeight());
1116 } else {
1117 mContent.setFixedSize(getContentAreaWidth(), getContentAreaHeight());
1118 }
1119
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001120 mScrollView.measure(contentAreaWidthSpec, contentAreaHeightSpec);
1121 mFolderName.measure(contentAreaWidthSpec,
Adam Cohen234c4cd2011-07-17 21:03:04 -07001122 MeasureSpec.makeMeasureSpec(mFolderNameHeight, MeasureSpec.EXACTLY));
1123 setMeasuredDimension(width, height);
1124 }
1125
Adam Cohen2801caf2011-05-13 20:57:39 -07001126 private void arrangeChildren(ArrayList<View> list) {
1127 int[] vacant = new int[2];
1128 if (list == null) {
1129 list = getItemsInReadingOrder();
1130 }
1131 mContent.removeAllViews();
1132
1133 for (int i = 0; i < list.size(); i++) {
1134 View v = list.get(i);
1135 mContent.getVacantCell(vacant, 1, 1);
1136 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) v.getLayoutParams();
1137 lp.cellX = vacant[0];
1138 lp.cellY = vacant[1];
1139 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohen2792a332011-09-26 21:09:47 -07001140 if (info.cellX != vacant[0] || info.cellY != vacant[1]) {
1141 info.cellX = vacant[0];
1142 info.cellY = vacant[1];
1143 LauncherModel.addOrMoveItemInDatabase(mLauncher, info, mInfo.id, 0,
1144 info.cellX, info.cellY);
1145 }
Adam Cohen2801caf2011-05-13 20:57:39 -07001146 boolean insert = false;
1147 mContent.addViewToCellLayout(v, insert ? 0 : -1, (int)info.id, lp, true);
1148 }
Adam Cohen7c693212011-05-18 15:26:57 -07001149 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -07001150 }
1151
Adam Cohena9cf38f2011-05-02 15:36:58 -07001152 public int getItemCount() {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001153 return mContent.getShortcutsAndWidgets().getChildCount();
Adam Cohena9cf38f2011-05-02 15:36:58 -07001154 }
1155
1156 public View getItemAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001157 return mContent.getShortcutsAndWidgets().getChildAt(index);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001158 }
1159
Adam Cohen2801caf2011-05-13 20:57:39 -07001160 private void onCloseComplete() {
Adam Cohen05e0f402011-08-01 12:12:49 -07001161 DragLayer parent = (DragLayer) getParent();
Michael Jurka5649c282012-06-18 10:33:21 -07001162 if (parent != null) {
1163 parent.removeView(this);
1164 }
Adam Cohen4554ee12011-08-03 16:13:21 -07001165 mDragController.removeDropTarget((DropTarget) this);
Adam Cohen05e0f402011-08-01 12:12:49 -07001166 clearFocus();
Adam Cohenac56cff2011-09-28 20:45:37 -07001167 mFolderIcon.requestFocus();
Adam Cohen05e0f402011-08-01 12:12:49 -07001168
Adam Cohen2801caf2011-05-13 20:57:39 -07001169 if (mRearrangeOnClose) {
1170 setupContentForNumItems(getItemCount());
1171 mRearrangeOnClose = false;
1172 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001173 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001174 if (!mDragInProgress && !mSuppressFolderDeletion) {
1175 replaceFolderWithFinalItem();
1176 } else if (mDragInProgress) {
1177 mDeleteFolderOnDropCompleted = true;
1178 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001179 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001180 mSuppressFolderDeletion = false;
Adam Cohenafb01ee2011-06-23 15:38:03 -07001181 }
1182
1183 private void replaceFolderWithFinalItem() {
Adam Cohenafb01ee2011-06-23 15:38:03 -07001184 // Add the last remaining child to the workspace in place of the folder
Adam Cohenfb91f302012-06-11 15:45:18 -07001185 Runnable onCompleteRunnable = new Runnable() {
1186 @Override
1187 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -07001188 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screenId);
Adam Cohenafb01ee2011-06-23 15:38:03 -07001189
Winson Chung33231f52013-12-09 16:57:45 -08001190 View child = null;
Adam Cohenfb91f302012-06-11 15:45:18 -07001191 // Move the item from the folder to the workspace, in the position of the folder
1192 if (getItemCount() == 1) {
1193 ShortcutInfo finalItem = mInfo.contents.get(0);
Adam Cohenc5e63f32012-07-12 16:16:57 -07001194 child = mLauncher.createShortcut(R.layout.application, cellLayout,
Adam Cohenfb91f302012-06-11 15:45:18 -07001195 finalItem);
1196 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -07001197 mInfo.screenId, mInfo.cellX, mInfo.cellY);
Adam Cohenfb91f302012-06-11 15:45:18 -07001198 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001199 if (getItemCount() <= 1) {
1200 // Remove the folder
1201 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
Dan Sandler0eb687f2014-01-10 13:24:55 -05001202 if (cellLayout != null) {
1203 // b/12446428 -- sometimes the cell layout has already gone away?
1204 cellLayout.removeView(mFolderIcon);
1205 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001206 if (mFolderIcon instanceof DropTarget) {
1207 mDragController.removeDropTarget((DropTarget) mFolderIcon);
1208 }
1209 mLauncher.removeFolder(mInfo);
1210 }
Adam Cohenc5e63f32012-07-12 16:16:57 -07001211 // We add the child after removing the folder to prevent both from existing at
Winson Chung0e6a7132013-08-23 12:55:10 -07001212 // the same time in the CellLayout. We need to add the new item with addInScreenFromBind()
1213 // to ensure that hotseat items are placed correctly.
Adam Cohenc5e63f32012-07-12 16:16:57 -07001214 if (child != null) {
Winson Chung0e6a7132013-08-23 12:55:10 -07001215 mLauncher.getWorkspace().addInScreenFromBind(child, mInfo.container, mInfo.screenId,
Adam Cohenc5e63f32012-07-12 16:16:57 -07001216 mInfo.cellX, mInfo.cellY, mInfo.spanX, mInfo.spanY);
1217 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001218 }
1219 };
1220 View finalChild = getItemAt(0);
1221 if (finalChild != null) {
1222 mFolderIcon.performDestroyAnimation(finalChild, onCompleteRunnable);
Winson Chung33231f52013-12-09 16:57:45 -08001223 } else {
1224 onCompleteRunnable.run();
Adam Cohenafb01ee2011-06-23 15:38:03 -07001225 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001226 mDestroyed = true;
1227 }
1228
1229 boolean isDestroyed() {
1230 return mDestroyed;
Adam Cohen2801caf2011-05-13 20:57:39 -07001231 }
1232
Adam Cohenac56cff2011-09-28 20:45:37 -07001233 // This method keeps track of the last item in the folder for the purposes
1234 // of keyboard focus
1235 private void updateTextViewFocus() {
1236 View lastChild = getItemAt(getItemCount() - 1);
1237 getItemAt(getItemCount() - 1);
1238 if (lastChild != null) {
1239 mFolderName.setNextFocusDownId(lastChild.getId());
1240 mFolderName.setNextFocusRightId(lastChild.getId());
1241 mFolderName.setNextFocusLeftId(lastChild.getId());
1242 mFolderName.setNextFocusUpId(lastChild.getId());
1243 }
1244 }
1245
Adam Cohenbfbfd262011-06-13 16:55:12 -07001246 public void onDrop(DragObject d) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001247 Runnable cleanUpRunnable = null;
1248
Adam Cohen689ff162014-05-08 17:27:56 -07001249 // If we are coming from All Apps space, we defer removing the extra empty screen
1250 // until the folder closes
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001251 if (d.dragSource != mLauncher.getWorkspace() && !(d.dragSource instanceof Folder)) {
1252 cleanUpRunnable = new Runnable() {
1253 @Override
1254 public void run() {
Adam Cohen689ff162014-05-08 17:27:56 -07001255 mLauncher.exitSpringLoadedDragModeDelayed(true,
1256 Launcher.EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
1257 null);
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001258 }
1259 };
Adam Cohenbfbfd262011-06-13 16:55:12 -07001260 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001261
1262 View currentDragView;
1263 ShortcutInfo si = mCurrentDragInfo;
1264 if (mIsExternalDrag) {
1265 si.cellX = mEmptyCell[0];
1266 si.cellY = mEmptyCell[1];
Sunny Goyal95abbb32014-08-04 10:53:22 -07001267
1268 // Actually move the item in the database if it was an external drag. Call this
1269 // before creating the view, so that ShortcutInfo is updated appropriately.
1270 LauncherModel.addOrMoveItemInDatabase(
1271 mLauncher, si, mInfo.id, 0, si.cellX, si.cellY);
1272
1273 // We only need to update the locations if it doesn't get handled in #onDropCompleted.
1274 if (d.dragSource != this) {
1275 updateItemLocationsInDatabaseBatch();
1276 }
1277 mIsExternalDrag = false;
1278
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001279 currentDragView = createAndAddShortcut(si);
1280 } else {
1281 currentDragView = mCurrentDragView;
1282 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) currentDragView.getLayoutParams();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001283 si.cellX = lp.cellX = mEmptyCell[0];
1284 si.cellX = lp.cellY = mEmptyCell[1];
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001285 mContent.addViewToCellLayout(currentDragView, -1, (int) si.id, lp, true);
Adam Cohenbfbfd262011-06-13 16:55:12 -07001286 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001287
1288 if (d.dragView.hasDrawn()) {
1289
1290 // Temporarily reset the scale such that the animation target gets calculated correctly.
1291 float scaleX = getScaleX();
1292 float scaleY = getScaleY();
1293 setScaleX(1.0f);
1294 setScaleY(1.0f);
1295 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, currentDragView,
1296 cleanUpRunnable, null);
1297 setScaleX(scaleX);
1298 setScaleY(scaleY);
1299 } else {
1300 d.deferDragViewCleanupPostAnimation = false;
1301 currentDragView.setVisibility(VISIBLE);
1302 }
1303 mItemsInvalidated = true;
1304 setupContentDimensions(getItemCount());
1305
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001306 // Temporarily suppress the listener, as we did all the work already here.
1307 mSuppressOnAdd = true;
1308 mInfo.add(si);
1309 mSuppressOnAdd = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001310 }
1311
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001312 // This is used so the item doesn't immediately appear in the folder when added. In one case
1313 // we need to create the illusion that the item isn't added back to the folder yet, to
1314 // to correspond to the animation of the icon back into the folder. This is
1315 public void hideItem(ShortcutInfo info) {
1316 View v = getViewForInfo(info);
1317 v.setVisibility(INVISIBLE);
1318 }
1319 public void showItem(ShortcutInfo info) {
1320 View v = getViewForInfo(info);
1321 v.setVisibility(VISIBLE);
1322 }
1323
Adam Cohenbfbfd262011-06-13 16:55:12 -07001324 public void onAdd(ShortcutInfo item) {
1325 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001326 // If the item was dropped onto this open folder, we have done the work associated
1327 // with adding the item to the folder, as indicated by mSuppressOnAdd being set
Adam Cohenbfbfd262011-06-13 16:55:12 -07001328 if (mSuppressOnAdd) return;
1329 if (!findAndSetEmptyCells(item)) {
1330 // The current layout is full, can we expand it?
1331 setupContentForNumItems(getItemCount() + 1);
1332 findAndSetEmptyCells(item);
1333 }
1334 createAndAddShortcut(item);
1335 LauncherModel.addOrMoveItemInDatabase(
1336 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
1337 }
1338
Adam Cohena9cf38f2011-05-02 15:36:58 -07001339 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -07001340 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001341 // If this item is being dragged from this open folder, we have already handled
1342 // the work associated with removing the item, so we don't have to do anything here.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001343 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001344 View v = getViewForInfo(item);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001345 mContent.removeView(v);
Adam Cohen2801caf2011-05-13 20:57:39 -07001346 if (mState == STATE_ANIMATING) {
1347 mRearrangeOnClose = true;
1348 } else {
1349 setupContentForNumItems(getItemCount());
1350 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001351 if (getItemCount() <= 1) {
1352 replaceFolderWithFinalItem();
1353 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001354 }
Adam Cohen7c693212011-05-18 15:26:57 -07001355
Adam Cohendf1e4e82011-06-24 15:57:39 -07001356 private View getViewForInfo(ShortcutInfo item) {
1357 for (int j = 0; j < mContent.getCountY(); j++) {
1358 for (int i = 0; i < mContent.getCountX(); i++) {
1359 View v = mContent.getChildAt(i, j);
1360 if (v.getTag() == item) {
1361 return v;
1362 }
1363 }
1364 }
1365 return null;
1366 }
1367
Adam Cohen76078c42011-06-09 15:06:52 -07001368 public void onItemsChanged() {
Adam Cohenac56cff2011-09-28 20:45:37 -07001369 updateTextViewFocus();
Adam Cohen76078c42011-06-09 15:06:52 -07001370 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001371
Adam Cohen76fc0852011-06-17 13:26:23 -07001372 public void onTitleChanged(CharSequence title) {
1373 }
Adam Cohen76078c42011-06-09 15:06:52 -07001374
Adam Cohen7c693212011-05-18 15:26:57 -07001375 public ArrayList<View> getItemsInReadingOrder() {
1376 if (mItemsInvalidated) {
1377 mItemsInReadingOrder.clear();
1378 for (int j = 0; j < mContent.getCountY(); j++) {
1379 for (int i = 0; i < mContent.getCountX(); i++) {
1380 View v = mContent.getChildAt(i, j);
1381 if (v != null) {
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001382 mItemsInReadingOrder.add(v);
Adam Cohen7c693212011-05-18 15:26:57 -07001383 }
1384 }
1385 }
1386 mItemsInvalidated = false;
1387 }
1388 return mItemsInReadingOrder;
1389 }
Adam Cohen8dfcba42011-07-07 16:38:18 -07001390
1391 public void getLocationInDragLayer(int[] loc) {
1392 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
1393 }
Adam Cohenea0818d2011-09-30 20:06:58 -07001394
1395 public void onFocusChange(View v, boolean hasFocus) {
1396 if (v == mFolderName && hasFocus) {
1397 startEditingFolderName();
1398 }
1399 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001400
1401 @Override
1402 public void getHitRectRelativeToDragLayer(Rect outRect) {
1403 getHitRect(outRect);
1404 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001405}