blob: 4a707711d08185abe56fe3aa95b57fb5d425e555 [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;
Alan Viverette4cda5b72013-08-28 17:53:41 -070027import android.os.SystemClock;
Bjorn Bringertb66773c2013-09-25 16:12:24 +010028import android.support.v4.widget.AutoScrollHelper;
Adam Cohen7a14d0b2011-06-24 11:36:35 -070029import android.text.InputType;
Adam Cohene601a432011-07-26 21:51:30 -070030import android.text.Selection;
31import android.text.Spannable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.util.AttributeSet;
Adam Cohen3bf84d32012-05-07 20:17:14 -070033import android.util.Log;
Adam Cohen76fc0852011-06-17 13:26:23 -070034import android.view.ActionMode;
35import android.view.KeyEvent;
Adam Cohendf2cc412011-04-27 16:56:57 -070036import android.view.LayoutInflater;
Adam Cohen76fc0852011-06-17 13:26:23 -070037import android.view.Menu;
38import android.view.MenuItem;
Adam Cohen0c872ba2011-05-05 10:34:16 -070039import android.view.MotionEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.view.View;
Adam Cohen3371da02011-10-25 21:38:29 -070041import android.view.accessibility.AccessibilityEvent;
42import android.view.accessibility.AccessibilityManager;
Adam Cohen76fc0852011-06-17 13:26:23 -070043import android.view.inputmethod.EditorInfo;
44import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.widget.LinearLayout;
Adam Cohenf0f4eda2013-06-06 21:27:03 -070046import android.widget.ScrollView;
Adam Cohendf2cc412011-04-27 16:56:57 -070047import android.widget.TextView;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048
Daniel Sandler325dc232013-06-05 22:57:57 -040049import com.android.launcher3.FolderInfo.FolderListener;
Romain Guyedcce092010-03-04 13:03:17 -080050
Adam Cohenc0dcf592011-06-01 15:30:43 -070051import java.util.ArrayList;
Adam Cohen3bf84d32012-05-07 20:17:14 -070052import java.util.Collections;
53import java.util.Comparator;
Adam Cohenc0dcf592011-06-01 15:30:43 -070054
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055/**
56 * Represents a set of icons chosen by the user or generated by the system.
57 */
Adam Cohen8dfcba42011-07-07 16:38:18 -070058public class Folder extends LinearLayout implements DragSource, View.OnClickListener,
Adam Cohenea0818d2011-09-30 20:06:58 -070059 View.OnLongClickListener, DropTarget, FolderListener, TextView.OnEditorActionListener,
60 View.OnFocusChangeListener {
Adam Cohendf2cc412011-04-27 16:56:57 -070061 private static final String TAG = "Launcher.Folder";
62
Adam Cohen4eac29a2011-07-11 17:53:37 -070063 protected DragController mDragController;
64 protected Launcher mLauncher;
65 protected FolderInfo mInfo;
66
Adam Cohendf2cc412011-04-27 16:56:57 -070067 static final int STATE_NONE = -1;
68 static final int STATE_SMALL = 0;
69 static final int STATE_ANIMATING = 1;
70 static final int STATE_OPEN = 2;
71
Jorim Jaggi55bd9722014-01-16 15:30:42 -080072 private static final int CLOSE_FOLDER_DELAY_MS = 150;
73
Adam Cohendf2cc412011-04-27 16:56:57 -070074 private int mExpandDuration;
75 protected CellLayout mContent;
Adam Cohenf0f4eda2013-06-06 21:27:03 -070076 private ScrollView mScrollView;
Adam Cohendf2cc412011-04-27 16:56:57 -070077 private final LayoutInflater mInflater;
78 private final IconCache mIconCache;
79 private int mState = STATE_NONE;
Adam Cohenbfbfd262011-06-13 16:55:12 -070080 private static final int REORDER_ANIMATION_DURATION = 230;
Adam Cohenf0f4eda2013-06-06 21:27:03 -070081 private static final int REORDER_DELAY = 250;
Adam Cohen5d518fa2013-12-05 14:16:23 -080082 private static final int ON_EXIT_CLOSE_DELAY = 400;
Adam Cohen2801caf2011-05-13 20:57:39 -070083 private boolean mRearrangeOnClose = false;
84 private FolderIcon mFolderIcon;
85 private int mMaxCountX;
86 private int mMaxCountY;
Adam Cohen78dc83e2011-11-15 17:10:00 -080087 private int mMaxNumItems;
Adam Cohen7c693212011-05-18 15:26:57 -070088 private ArrayList<View> mItemsInReadingOrder = new ArrayList<View>();
89 boolean mItemsInvalidated = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -070090 private ShortcutInfo mCurrentDragInfo;
91 private View mCurrentDragView;
Jorim Jaggi55bd9722014-01-16 15:30:42 -080092 private boolean mIsExternalDrag;
Adam Cohenbfbfd262011-06-13 16:55:12 -070093 boolean mSuppressOnAdd = false;
94 private int[] mTargetCell = new int[2];
95 private int[] mPreviousTargetCell = new int[2];
96 private int[] mEmptyCell = new int[2];
97 private Alarm mReorderAlarm = new Alarm();
98 private Alarm mOnExitAlarm = new Alarm();
Adam Cohen76fc0852011-06-17 13:26:23 -070099 private int mFolderNameHeight;
Adam Cohen8e776a62011-06-28 18:10:06 -0700100 private Rect mTempRect = new Rect();
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700101 private boolean mDragInProgress = false;
102 private boolean mDeleteFolderOnDropCompleted = false;
103 private boolean mSuppressFolderDeletion = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700104 private boolean mItemAddedBackToSelfViaIcon = false;
Adam Cohenac56cff2011-09-28 20:45:37 -0700105 FolderEditText mFolderName;
Adam Cohen268c4752012-06-06 17:47:33 -0700106 private float mFolderIconPivotX;
107 private float mFolderIconPivotY;
Adam Cohen228da5a2011-07-27 22:23:47 -0700108
Adam Cohen76fc0852011-06-17 13:26:23 -0700109 private boolean mIsEditingName = false;
110 private InputMethodManager mInputMethodManager;
Adam Cohendf2cc412011-04-27 16:56:57 -0700111
Adam Cohena65beee2011-06-27 21:32:23 -0700112 private static String sDefaultFolderName;
113 private static String sHintText;
114
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700115 private FocusIndicatorView mFocusIndicatorHandler;
116
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700117 private int DRAG_MODE_NONE = 0;
118 private int DRAG_MODE_REORDER = 1;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700119 private int mDragMode = DRAG_MODE_NONE;
120
Adam Cohen1960ea42013-11-12 11:33:14 +0000121 // We avoid measuring the scroll view with a 0 width or height, as this
122 // results in CellLayout being measured as UNSPECIFIED, which it does
123 // not support.
124 private static final int MIN_CONTENT_DIMEN = 5;
125
Adam Cohenfb91f302012-06-11 15:45:18 -0700126 private boolean mDestroyed;
127
Alan Viverette4cda5b72013-08-28 17:53:41 -0700128 private AutoScrollHelper mAutoScrollHelper;
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700129
Michael Jurka1e2f4652013-07-08 18:03:46 -0700130 private Runnable mDeferredAction;
131 private boolean mDeferDropAfterUninstall;
132 private boolean mUninstallSuccessful;
133
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134 /**
135 * Used to inflate the Workspace from XML.
136 *
137 * @param context The application's context.
138 * @param attrs The attribtues set containing the Workspace's customization values.
139 */
140 public Folder(Context context, AttributeSet attrs) {
141 super(context, attrs);
Winson Chung5f8afe62013-08-12 16:19:28 -0700142
143 LauncherAppState app = LauncherAppState.getInstance();
144 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800145 setAlwaysDrawnWithCacheEnabled(false);
Adam Cohendf2cc412011-04-27 16:56:57 -0700146 mInflater = LayoutInflater.from(context);
Winson Chung5f8afe62013-08-12 16:19:28 -0700147 mIconCache = app.getIconCache();
Adam Cohen78dc83e2011-11-15 17:10:00 -0800148
149 Resources res = getResources();
Michael Jurka414300a2013-08-27 15:42:35 +0200150 mMaxCountX = (int) grid.numColumns;
Nilesh Agrawal5f7099a2014-01-02 15:54:57 -0800151 // Allow scrolling folders when DISABLE_ALL_APPS is true.
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800152 if (LauncherAppState.isDisableAllApps()) {
Nilesh Agrawal5f7099a2014-01-02 15:54:57 -0800153 mMaxCountY = mMaxNumItems = Integer.MAX_VALUE;
154 } else {
155 mMaxCountY = (int) grid.numRows;
156 mMaxNumItems = mMaxCountX * mMaxCountY;
157 }
Adam Cohen76fc0852011-06-17 13:26:23 -0700158
159 mInputMethodManager = (InputMethodManager)
Michael Jurka8b805b12012-04-18 14:23:14 -0700160 getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
Adam Cohen76fc0852011-06-17 13:26:23 -0700161
Adam Cohen76fc0852011-06-17 13:26:23 -0700162 mExpandDuration = res.getInteger(R.integer.config_folderAnimDuration);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700163
164 if (sDefaultFolderName == null) {
165 sDefaultFolderName = res.getString(R.string.folder_name);
166 }
Adam Cohena65beee2011-06-27 21:32:23 -0700167 if (sHintText == null) {
168 sHintText = res.getString(R.string.folder_hint_text);
169 }
Adam Cohen4eac29a2011-07-11 17:53:37 -0700170 mLauncher = (Launcher) context;
Adam Cohenac56cff2011-09-28 20:45:37 -0700171 // We need this view to be focusable in touch mode so that when text editing of the folder
172 // name is complete, we have something to focus on, thus hiding the cursor and giving
173 // reliable behvior when clicking the text field (since it will always gain focus on click).
174 setFocusableInTouchMode(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175 }
176
177 @Override
178 protected void onFinishInflate() {
179 super.onFinishInflate();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700180 mScrollView = (ScrollView) findViewById(R.id.scroll_view);
Adam Cohendf2cc412011-04-27 16:56:57 -0700181 mContent = (CellLayout) findViewById(R.id.folder_content);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700182
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700183 mFocusIndicatorHandler = new FocusIndicatorView(getContext());
184 mContent.addView(mFocusIndicatorHandler, 0);
185 mFocusIndicatorHandler.getLayoutParams().height = FocusIndicatorView.DEFAULT_LAYOUT_SIZE;
186 mFocusIndicatorHandler.getLayoutParams().width = FocusIndicatorView.DEFAULT_LAYOUT_SIZE;
187
Winson Chung5f8afe62013-08-12 16:19:28 -0700188 LauncherAppState app = LauncherAppState.getInstance();
189 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
190
Winson Chung5f8afe62013-08-12 16:19:28 -0700191 mContent.setCellDimensions(grid.folderCellWidthPx, grid.folderCellHeightPx);
Adam Cohen2801caf2011-05-13 20:57:39 -0700192 mContent.setGridSize(0, 0);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700193 mContent.getShortcutsAndWidgets().setMotionEventSplittingEnabled(false);
Adam Cohen2374abf2013-04-16 14:56:57 -0700194 mContent.setInvertIfRtl(true);
Adam Cohenac56cff2011-09-28 20:45:37 -0700195 mFolderName = (FolderEditText) findViewById(R.id.folder_name);
196 mFolderName.setFolder(this);
Adam Cohenea0818d2011-09-30 20:06:58 -0700197 mFolderName.setOnFocusChangeListener(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700198
199 // We find out how tall the text view wants to be (it is set to wrap_content), so that
200 // we can allocate the appropriate amount of space for it.
201 int measureSpec = MeasureSpec.UNSPECIFIED;
202 mFolderName.measure(measureSpec, measureSpec);
203 mFolderNameHeight = mFolderName.getMeasuredHeight();
204
205 // We disable action mode for now since it messes up the view on phones
206 mFolderName.setCustomSelectionActionModeCallback(mActionModeCallback);
Adam Cohen76fc0852011-06-17 13:26:23 -0700207 mFolderName.setOnEditorActionListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700208 mFolderName.setSelectAllOnFocus(true);
Adam Cohen7a14d0b2011-06-24 11:36:35 -0700209 mFolderName.setInputType(mFolderName.getInputType() |
210 InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
Alan Viverette4cda5b72013-08-28 17:53:41 -0700211 mAutoScrollHelper = new FolderAutoScrollHelper(mScrollView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800212 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700213
Adam Cohen76fc0852011-06-17 13:26:23 -0700214 private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
215 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
216 return false;
217 }
218
219 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
220 return false;
221 }
222
223 public void onDestroyActionMode(ActionMode mode) {
224 }
225
226 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
227 return false;
228 }
229 };
230
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800231 public void onClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700232 Object tag = v.getTag();
233 if (tag instanceof ShortcutInfo) {
Adam Cohenb5fe60c2013-06-06 22:03:51 -0700234 mLauncher.onClick(v);
Adam Cohendf2cc412011-04-27 16:56:57 -0700235 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800236 }
237
238 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -0700239 // Return if global dragging is not enabled
240 if (!mLauncher.isDraggingEnabled()) return true;
241
Adam Cohendf2cc412011-04-27 16:56:57 -0700242 Object tag = v.getTag();
243 if (tag instanceof ShortcutInfo) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700244 ShortcutInfo item = (ShortcutInfo) tag;
245 if (!v.isInTouchMode()) {
246 return false;
247 }
248
Winson Chunga6945242014-01-08 14:04:34 -0800249 mLauncher.getLauncherClings().dismissFolderCling(null);
Winson Chung7d7541e2011-09-16 20:14:36 -0700250
Adam Cohendf2cc412011-04-27 16:56:57 -0700251 mLauncher.getWorkspace().onDragStartedWithItem(v);
Adam Cohenac8c8762011-07-13 11:15:27 -0700252 mLauncher.getWorkspace().beginDragShared(v, this);
Adam Cohen76078c42011-06-09 15:06:52 -0700253
254 mCurrentDragInfo = item;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700255 mEmptyCell[0] = item.cellX;
256 mEmptyCell[1] = item.cellY;
257 mCurrentDragView = v;
Adam Cohenfc53cd22011-07-20 15:45:11 -0700258
259 mContent.removeView(mCurrentDragView);
260 mInfo.remove(mCurrentDragInfo);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700261 mDragInProgress = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700262 mItemAddedBackToSelfViaIcon = false;
Adam Cohendf2cc412011-04-27 16:56:57 -0700263 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800264 return true;
265 }
266
Adam Cohen76fc0852011-06-17 13:26:23 -0700267 public boolean isEditingName() {
268 return mIsEditingName;
269 }
270
271 public void startEditingFolderName() {
Adam Cohena65beee2011-06-27 21:32:23 -0700272 mFolderName.setHint("");
Adam Cohen76fc0852011-06-17 13:26:23 -0700273 mIsEditingName = true;
274 }
275
276 public void dismissEditingName() {
277 mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
278 doneEditingFolderName(true);
279 }
280
281 public void doneEditingFolderName(boolean commit) {
Adam Cohena65beee2011-06-27 21:32:23 -0700282 mFolderName.setHint(sHintText);
Adam Cohen1df26a32011-08-12 16:15:23 -0700283 // Convert to a string here to ensure that no other state associated with the text field
284 // gets saved.
Adam Cohen3371da02011-10-25 21:38:29 -0700285 String newTitle = mFolderName.getText().toString();
286 mInfo.setTitle(newTitle);
Adam Cohen76fc0852011-06-17 13:26:23 -0700287 LauncherModel.updateItemInDatabase(mLauncher, mInfo);
Adam Cohenac56cff2011-09-28 20:45:37 -0700288
Adam Cohen3371da02011-10-25 21:38:29 -0700289 if (commit) {
290 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700291 String.format(getContext().getString(R.string.folder_renamed), newTitle));
Adam Cohen3371da02011-10-25 21:38:29 -0700292 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700293 // In order to clear the focus from the text field, we set the focus on ourself. This
294 // ensures that every time the field is clicked, focus is gained, giving reliable behavior.
295 requestFocus();
296
Adam Cohene601a432011-07-26 21:51:30 -0700297 Selection.setSelection((Spannable) mFolderName.getText(), 0, 0);
Adam Cohen76fc0852011-06-17 13:26:23 -0700298 mIsEditingName = false;
299 }
300
301 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
302 if (actionId == EditorInfo.IME_ACTION_DONE) {
303 dismissEditingName();
304 return true;
305 }
306 return false;
307 }
308
309 public View getEditTextRegion() {
310 return mFolderName;
311 }
312
Anjali Koppalf05545f2014-03-10 19:18:43 -0700313 public CellLayout getContent() {
314 return mContent;
315 }
316
Adam Cohen0c872ba2011-05-05 10:34:16 -0700317 /**
318 * We need to handle touch events to prevent them from falling through to the workspace below.
319 */
320 @Override
321 public boolean onTouchEvent(MotionEvent ev) {
322 return true;
323 }
324
Joe Onorato00acb122009-08-04 16:04:30 -0400325 public void setDragController(DragController dragController) {
326 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800327 }
328
Adam Cohen2801caf2011-05-13 20:57:39 -0700329 void setFolderIcon(FolderIcon icon) {
330 mFolderIcon = icon;
331 }
332
Adam Cohen3371da02011-10-25 21:38:29 -0700333 @Override
334 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
335 // When the folder gets focus, we don't want to announce the list of items.
336 return true;
337 }
338
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800339 /**
340 * @return the FolderInfo object associated with this folder
341 */
342 FolderInfo getInfo() {
343 return mInfo;
344 }
345
Adam Cohen3bf84d32012-05-07 20:17:14 -0700346 private class GridComparator implements Comparator<ShortcutInfo> {
347 int mNumCols;
348 public GridComparator(int numCols) {
349 mNumCols = numCols;
350 }
351
352 @Override
353 public int compare(ShortcutInfo lhs, ShortcutInfo rhs) {
354 int lhIndex = lhs.cellY * mNumCols + lhs.cellX;
355 int rhIndex = rhs.cellY * mNumCols + rhs.cellX;
356 return (lhIndex - rhIndex);
357 }
Adam Cohen3bf84d32012-05-07 20:17:14 -0700358 }
359
360 private void placeInReadingOrder(ArrayList<ShortcutInfo> items) {
361 int maxX = 0;
362 int count = items.size();
363 for (int i = 0; i < count; i++) {
364 ShortcutInfo item = items.get(i);
365 if (item.cellX > maxX) {
366 maxX = item.cellX;
367 }
368 }
Adam Cohen691a5792012-05-11 14:27:30 -0700369
370 GridComparator gridComparator = new GridComparator(maxX + 1);
Adam Cohen3bf84d32012-05-07 20:17:14 -0700371 Collections.sort(items, gridComparator);
372 final int countX = mContent.getCountX();
373 for (int i = 0; i < count; i++) {
374 int x = i % countX;
375 int y = i / countX;
376 ShortcutInfo item = items.get(i);
377 item.cellX = x;
378 item.cellY = y;
379 }
380 }
381
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800382 void bind(FolderInfo info) {
383 mInfo = info;
Adam Cohendf2cc412011-04-27 16:56:57 -0700384 ArrayList<ShortcutInfo> children = info.contents;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700385 ArrayList<ShortcutInfo> overflow = new ArrayList<ShortcutInfo>();
Adam Cohen7c693212011-05-18 15:26:57 -0700386 setupContentForNumItems(children.size());
Adam Cohen3bf84d32012-05-07 20:17:14 -0700387 placeInReadingOrder(children);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700388 int count = 0;
Adam Cohendf2cc412011-04-27 16:56:57 -0700389 for (int i = 0; i < children.size(); i++) {
390 ShortcutInfo child = (ShortcutInfo) children.get(i);
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800391 if (createAndAddShortcut(child) == null) {
Adam Cohenc508b2d2011-06-28 14:41:44 -0700392 overflow.add(child);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700393 } else {
394 count++;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700395 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700396 }
Adam Cohenc508b2d2011-06-28 14:41:44 -0700397
Adam Cohen0057bbc2011-08-12 18:30:51 -0700398 // We rearrange the items in case there are any empty gaps
399 setupContentForNumItems(count);
400
Jason Monk4ff73882014-04-24 16:48:07 -0400401 // If our folder has too many items we prune them from the list. This is an issue
Adam Cohenc508b2d2011-06-28 14:41:44 -0700402 // when upgrading from the old Folders implementation which could contain an unlimited
403 // number of items.
404 for (ShortcutInfo item: overflow) {
405 mInfo.remove(item);
406 LauncherModel.deleteItemFromDatabase(mLauncher, item);
407 }
408
Adam Cohen4dbe6d92011-05-18 17:14:15 -0700409 mItemsInvalidated = true;
Adam Cohenac56cff2011-09-28 20:45:37 -0700410 updateTextViewFocus();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700411 mInfo.addListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700412
Adam Cohenafb01ee2011-06-23 15:38:03 -0700413 if (!sDefaultFolderName.contentEquals(mInfo.title)) {
Adam Cohen4ef610f2011-06-21 22:37:36 -0700414 mFolderName.setText(mInfo.title);
415 } else {
416 mFolderName.setText("");
417 }
Adam Cohen691a5792012-05-11 14:27:30 -0700418 updateItemLocationsInDatabase();
Winson Chung33231f52013-12-09 16:57:45 -0800419
420 // In case any children didn't come across during loading, clean up the folder accordingly
421 mFolderIcon.post(new Runnable() {
422 public void run() {
423 if (getItemCount() <= 1) {
424 replaceFolderWithFinalItem();
425 }
426 }
427 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700428 }
429
430 /**
431 * Creates a new UserFolder, inflated from R.layout.user_folder.
432 *
433 * @param context The application's context.
434 *
435 * @return A new UserFolder.
436 */
437 static Folder fromXml(Context context) {
438 return (Folder) LayoutInflater.from(context).inflate(R.layout.user_folder, null);
439 }
440
441 /**
442 * This method is intended to make the UserFolder to be visually identical in size and position
443 * to its associated FolderIcon. This allows for a seamless transition into the expanded state.
444 */
445 private void positionAndSizeAsIcon() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700446 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800447 setScaleX(0.8f);
448 setScaleY(0.8f);
449 setAlpha(0f);
Adam Cohendf2cc412011-04-27 16:56:57 -0700450 mState = STATE_SMALL;
451 }
452
453 public void animateOpen() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700454 positionAndSizeAsIcon();
455
Adam Cohen8e776a62011-06-28 18:10:06 -0700456 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen2801caf2011-05-13 20:57:39 -0700457 centerAboutIcon();
Adam Cohen662b5982011-12-13 17:45:21 -0800458 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1);
459 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
460 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
Michael Jurka032e6ba2013-04-22 15:08:42 +0200461 final ObjectAnimator oa =
Michael Jurka2ecf9952012-06-18 12:52:28 -0700462 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohen6441de02011-12-14 14:25:32 -0800463
Adam Cohen2801caf2011-05-13 20:57:39 -0700464 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700465 @Override
466 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700467 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700468 String.format(getContext().getString(R.string.folder_opened),
Adam Cohen3371da02011-10-25 21:38:29 -0700469 mContent.getCountX(), mContent.getCountY()));
Adam Cohendf2cc412011-04-27 16:56:57 -0700470 mState = STATE_ANIMATING;
471 }
472 @Override
473 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700474 mState = STATE_OPEN;
Michael Jurka0121c3e2012-05-31 08:36:04 -0700475 setLayerType(LAYER_TYPE_NONE, null);
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800476
477 // Only show cling if we are not in the middle of a drag - this would be quite jarring.
478 if (!mDragController.isDragging()) {
479 Cling cling = mLauncher.getLauncherClings().showFoldersCling();
480 if (cling != null) {
481 cling.bringScrimToFront();
482 bringToFront();
483 cling.bringToFront();
484 }
Winson Chung7a74ac92011-09-20 17:43:51 -0700485 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700486 setFocusOnFirstChild();
Adam Cohendf2cc412011-04-27 16:56:57 -0700487 }
488 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700489 oa.setDuration(mExpandDuration);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700490 setLayerType(LAYER_TYPE_HARDWARE, null);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100491 oa.start();
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800492
493 // Make sure the folder picks up the last drag move even if the finger doesn't move.
494 if (mDragController.isDragging()) {
495 mDragController.forceTouchMove();
496 }
497 }
498
499 public void beginExternalDrag(ShortcutInfo item) {
500 setupContentForNumItems(getItemCount() + 1);
501 findAndSetEmptyCells(item);
502
503 mCurrentDragInfo = item;
504 mEmptyCell[0] = item.cellX;
505 mEmptyCell[1] = item.cellY;
506 mIsExternalDrag = true;
507
508 mDragInProgress = true;
Adam Cohendf2cc412011-04-27 16:56:57 -0700509 }
510
Adam Cohen3371da02011-10-25 21:38:29 -0700511 private void sendCustomAccessibilityEvent(int type, String text) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700512 AccessibilityManager accessibilityManager = (AccessibilityManager)
513 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
514 if (accessibilityManager.isEnabled()) {
Adam Cohen3371da02011-10-25 21:38:29 -0700515 AccessibilityEvent event = AccessibilityEvent.obtain(type);
516 onInitializeAccessibilityEvent(event);
517 event.getText().add(text);
Michael Jurka8b805b12012-04-18 14:23:14 -0700518 accessibilityManager.sendAccessibilityEvent(event);
Adam Cohen3371da02011-10-25 21:38:29 -0700519 }
520 }
521
Adam Cohenac56cff2011-09-28 20:45:37 -0700522 private void setFocusOnFirstChild() {
523 View firstChild = mContent.getChildAt(0, 0);
524 if (firstChild != null) {
525 firstChild.requestFocus();
526 }
527 }
528
Adam Cohendf2cc412011-04-27 16:56:57 -0700529 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700530 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800531 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
532 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
533 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
Michael Jurka032e6ba2013-04-22 15:08:42 +0200534 final ObjectAnimator oa =
Michael Jurka2ecf9952012-06-18 12:52:28 -0700535 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohendf2cc412011-04-27 16:56:57 -0700536
Adam Cohen2801caf2011-05-13 20:57:39 -0700537 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700538 @Override
539 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700540 onCloseComplete();
Michael Jurka0121c3e2012-05-31 08:36:04 -0700541 setLayerType(LAYER_TYPE_NONE, null);
Adam Cohen2801caf2011-05-13 20:57:39 -0700542 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700543 }
544 @Override
545 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700546 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700547 getContext().getString(R.string.folder_closed));
Adam Cohendf2cc412011-04-27 16:56:57 -0700548 mState = STATE_ANIMATING;
549 }
550 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700551 oa.setDuration(mExpandDuration);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700552 setLayerType(LAYER_TYPE_HARDWARE, null);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100553 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700554 }
555
Adam Cohencb3382b2011-05-24 14:07:08 -0700556 public boolean acceptDrop(DragObject d) {
557 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700558 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700559 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
560 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
561 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700562 }
563
Adam Cohendf2cc412011-04-27 16:56:57 -0700564 protected boolean findAndSetEmptyCells(ShortcutInfo item) {
565 int[] emptyCell = new int[2];
566 if (mContent.findCellForSpan(emptyCell, item.spanX, item.spanY)) {
567 item.cellX = emptyCell[0];
568 item.cellY = emptyCell[1];
Adam Cohendf2cc412011-04-27 16:56:57 -0700569 return true;
570 } else {
571 return false;
572 }
573 }
574
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800575 protected View createAndAddShortcut(ShortcutInfo item) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700576 final BubbleTextView textView =
577 (BubbleTextView) mInflater.inflate(R.layout.folder_application, this, false);
578 textView.applyFromShortcutInfo(item, mIconCache, false);
579
Adam Cohendf2cc412011-04-27 16:56:57 -0700580 textView.setOnClickListener(this);
581 textView.setOnLongClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700582 textView.setOnFocusChangeListener(mFocusIndicatorHandler);
Adam Cohendf2cc412011-04-27 16:56:57 -0700583
Adam Cohenc508b2d2011-06-28 14:41:44 -0700584 // We need to check here to verify that the given item's location isn't already occupied
Adam Cohen3bf84d32012-05-07 20:17:14 -0700585 // by another item.
Adam Cohen0057bbc2011-08-12 18:30:51 -0700586 if (mContent.getChildAt(item.cellX, item.cellY) != null || item.cellX < 0 || item.cellY < 0
587 || item.cellX >= mContent.getCountX() || item.cellY >= mContent.getCountY()) {
Jason Monk4ff73882014-04-24 16:48:07 -0400588 // This shouldn't happen, log it.
Adam Cohen3bf84d32012-05-07 20:17:14 -0700589 Log.e(TAG, "Folder order not properly persisted during bind");
Adam Cohenc508b2d2011-06-28 14:41:44 -0700590 if (!findAndSetEmptyCells(item)) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800591 return null;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700592 }
593 }
594
Adam Cohendf2cc412011-04-27 16:56:57 -0700595 CellLayout.LayoutParams lp =
596 new CellLayout.LayoutParams(item.cellX, item.cellY, item.spanX, item.spanY);
597 boolean insert = false;
Adam Cohenac56cff2011-09-28 20:45:37 -0700598 textView.setOnKeyListener(new FolderKeyEventListener());
Adam Cohendf2cc412011-04-27 16:56:57 -0700599 mContent.addViewToCellLayout(textView, insert ? 0 : -1, (int)item.id, lp, true);
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800600 return textView;
Adam Cohendf2cc412011-04-27 16:56:57 -0700601 }
602
Adam Cohencb3382b2011-05-24 14:07:08 -0700603 public void onDragEnter(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700604 mPreviousTargetCell[0] = -1;
605 mPreviousTargetCell[1] = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700606 mOnExitAlarm.cancelAlarm();
607 }
608
609 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
610 public void onAlarm(Alarm alarm) {
611 realTimeReorder(mEmptyCell, mTargetCell);
612 }
613 };
614
615 boolean readingOrderGreaterThan(int[] v1, int[] v2) {
616 if (v1[1] > v2[1] || (v1[1] == v2[1] && v1[0] > v2[0])) {
617 return true;
618 } else {
619 return false;
620 }
621 }
622
623 private void realTimeReorder(int[] empty, int[] target) {
624 boolean wrap;
625 int startX;
626 int endX;
627 int startY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700628 int delay = 0;
629 float delayAmount = 30;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700630 if (readingOrderGreaterThan(target, empty)) {
631 wrap = empty[0] >= mContent.getCountX() - 1;
632 startY = wrap ? empty[1] + 1 : empty[1];
633 for (int y = startY; y <= target[1]; y++) {
634 startX = y == empty[1] ? empty[0] + 1 : 0;
635 endX = y < target[1] ? mContent.getCountX() - 1 : target[0];
636 for (int x = startX; x <= endX; x++) {
637 View v = mContent.getChildAt(x,y);
638 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen482ed822012-03-02 14:15:13 -0800639 REORDER_ANIMATION_DURATION, delay, true, true)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700640 empty[0] = x;
641 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700642 delay += delayAmount;
643 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700644 }
645 }
646 }
647 } else {
648 wrap = empty[0] == 0;
649 startY = wrap ? empty[1] - 1 : empty[1];
650 for (int y = startY; y >= target[1]; y--) {
651 startX = y == empty[1] ? empty[0] - 1 : mContent.getCountX() - 1;
652 endX = y > target[1] ? 0 : target[0];
653 for (int x = startX; x >= endX; x--) {
654 View v = mContent.getChildAt(x,y);
655 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen482ed822012-03-02 14:15:13 -0800656 REORDER_ANIMATION_DURATION, delay, true, true)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700657 empty[0] = x;
658 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700659 delay += delayAmount;
660 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700661 }
662 }
663 }
664 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700665 }
666
Adam Cohen2374abf2013-04-16 14:56:57 -0700667 public boolean isLayoutRtl() {
668 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
669 }
670
Adam Cohencb3382b2011-05-24 14:07:08 -0700671 public void onDragOver(DragObject d) {
Alan Viverette4cda5b72013-08-28 17:53:41 -0700672 final DragView dragView = d.dragView;
673 final int scrollOffset = mScrollView.getScrollY();
674 final float[] r = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, dragView, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700675 r[0] -= getPaddingLeft();
676 r[1] -= getPaddingTop();
677
Alan Viverette4cda5b72013-08-28 17:53:41 -0700678 final long downTime = SystemClock.uptimeMillis();
679 final MotionEvent translatedEv = MotionEvent.obtain(
680 downTime, downTime, MotionEvent.ACTION_MOVE, d.x, d.y, 0);
681
682 if (!mAutoScrollHelper.isEnabled()) {
683 mAutoScrollHelper.setEnabled(true);
684 }
685
686 final boolean handled = mAutoScrollHelper.onTouch(this, translatedEv);
687 translatedEv.recycle();
688
689 if (handled) {
690 mReorderAlarm.cancelAlarm();
691 } else {
692 mTargetCell = mContent.findNearestArea(
693 (int) r[0], (int) r[1] + scrollOffset, 1, 1, mTargetCell);
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700694 if (isLayoutRtl()) {
695 mTargetCell[0] = mContent.getCountX() - mTargetCell[0] - 1;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700696 }
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700697 if (mTargetCell[0] != mPreviousTargetCell[0]
698 || mTargetCell[1] != mPreviousTargetCell[1]) {
699 mReorderAlarm.cancelAlarm();
700 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
701 mReorderAlarm.setAlarm(REORDER_DELAY);
702 mPreviousTargetCell[0] = mTargetCell[0];
703 mPreviousTargetCell[1] = mTargetCell[1];
704 mDragMode = DRAG_MODE_REORDER;
705 } else {
706 mDragMode = DRAG_MODE_NONE;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700707 }
Adam Cohenbfbfd262011-06-13 16:55:12 -0700708 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700709 }
710
Adam Cohenbfbfd262011-06-13 16:55:12 -0700711 // This is used to compute the visual center of the dragView. The idea is that
712 // the visual center represents the user's interpretation of where the item is, and hence
713 // is the appropriate point to use when determining drop location.
714 private float[] getDragViewVisualCenter(int x, int y, int xOffset, int yOffset,
715 DragView dragView, float[] recycle) {
716 float res[];
717 if (recycle == null) {
718 res = new float[2];
719 } else {
720 res = recycle;
721 }
722
723 // These represent the visual top and left of drag view if a dragRect was provided.
724 // If a dragRect was not provided, then they correspond to the actual view left and
725 // top, as the dragRect is in that case taken to be the entire dragView.
726 // R.dimen.dragViewOffsetY.
727 int left = x - xOffset;
728 int top = y - yOffset;
729
730 // In order to find the visual center, we shift by half the dragRect
731 res[0] = left + dragView.getDragRegion().width() / 2;
732 res[1] = top + dragView.getDragRegion().height() / 2;
733
734 return res;
735 }
736
737 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
738 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700739 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700740 }
741 };
742
Adam Cohen95bb8002011-07-03 23:40:28 -0700743 public void completeDragExit() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700744 mLauncher.closeFolder();
745 mCurrentDragInfo = null;
746 mCurrentDragView = null;
747 mSuppressOnAdd = false;
748 mRearrangeOnClose = true;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800749 mIsExternalDrag = false;
Adam Cohen3e8f8112011-07-02 18:03:00 -0700750 }
751
Adam Cohencb3382b2011-05-24 14:07:08 -0700752 public void onDragExit(DragObject d) {
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700753 // Exiting folder; stop the auto scroller.
Alan Viverette4cda5b72013-08-28 17:53:41 -0700754 mAutoScrollHelper.setEnabled(false);
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700755 // We only close the folder if this is a true drag exit, ie. not because
756 // a drop has occurred above the folder.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700757 if (!d.dragComplete) {
758 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
759 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
760 }
761 mReorderAlarm.cancelAlarm();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700762 mDragMode = DRAG_MODE_NONE;
Adam Cohen2801caf2011-05-13 20:57:39 -0700763 }
764
Michael Jurka1e2f4652013-07-08 18:03:46 -0700765 public void onDropCompleted(final View target, final DragObject d,
766 final boolean isFlingToDelete, final boolean success) {
767 if (mDeferDropAfterUninstall) {
Michael Jurkaf3007582013-08-21 14:33:57 +0200768 Log.d(TAG, "Deferred handling drop because waiting for uninstall.");
Michael Jurka1e2f4652013-07-08 18:03:46 -0700769 mDeferredAction = new Runnable() {
770 public void run() {
771 onDropCompleted(target, d, isFlingToDelete, success);
772 mDeferredAction = null;
773 }
774 };
775 return;
776 }
777
778 boolean beingCalledAfterUninstall = mDeferredAction != null;
779 boolean successfulDrop =
780 success && (!beingCalledAfterUninstall || mUninstallSuccessful);
Winson Chung5f8afe62013-08-12 16:19:28 -0700781
Michael Jurka1e2f4652013-07-08 18:03:46 -0700782 if (successfulDrop) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800783 if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon && target != this) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700784 replaceFolderWithFinalItem();
785 }
786 } else {
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700787 setupContentForNumItems(getItemCount());
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700788 // The drag failed, we need to return the item to the folder
789 mFolderIcon.onDrop(d);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700790 }
791
792 if (target != this) {
793 if (mOnExitAlarm.alarmPending()) {
794 mOnExitAlarm.cancelAlarm();
Michael Jurka54554252013-08-01 12:52:23 +0200795 if (!successfulDrop) {
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700796 mSuppressFolderDeletion = true;
797 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700798 completeDragExit();
799 }
Adam Cohen9c58d822013-12-13 17:18:10 -0800800 }
801
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700802 mDeleteFolderOnDropCompleted = false;
803 mDragInProgress = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700804 mItemAddedBackToSelfViaIcon = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700805 mCurrentDragInfo = null;
806 mCurrentDragView = null;
807 mSuppressOnAdd = false;
Adam Cohen4045eb72011-10-06 11:44:26 -0700808
809 // Reordering may have occured, and we need to save the new item locations. We do this once
810 // at the end to prevent unnecessary database operations.
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700811 updateItemLocationsInDatabaseBatch();
Adam Cohen4045eb72011-10-06 11:44:26 -0700812 }
813
Michael Jurka1e2f4652013-07-08 18:03:46 -0700814 public void deferCompleteDropAfterUninstallActivity() {
815 mDeferDropAfterUninstall = true;
816 }
817
818 public void onUninstallActivityReturned(boolean success) {
819 mDeferDropAfterUninstall = false;
820 mUninstallSuccessful = success;
821 if (mDeferredAction != null) {
822 mDeferredAction.run();
823 }
824 }
825
Winson Chunga48487a2012-03-20 16:19:37 -0700826 @Override
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800827 public float getIntrinsicIconScaleFactor() {
828 return 1f;
829 }
830
831 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800832 public boolean supportsFlingToDelete() {
833 return true;
834 }
835
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000836 @Override
837 public boolean supportsAppInfoDropTarget() {
838 return false;
839 }
840
841 @Override
842 public boolean supportsDeleteDropTarget() {
843 return true;
844 }
845
Winson Chunga48487a2012-03-20 16:19:37 -0700846 public void onFlingToDelete(DragObject d, int x, int y, PointF vec) {
847 // Do nothing
848 }
849
850 @Override
851 public void onFlingToDeleteCompleted() {
852 // Do nothing
853 }
854
Adam Cohen4045eb72011-10-06 11:44:26 -0700855 private void updateItemLocationsInDatabase() {
856 ArrayList<View> list = getItemsInReadingOrder();
857 for (int i = 0; i < list.size(); i++) {
858 View v = list.get(i);
859 ItemInfo info = (ItemInfo) v.getTag();
860 LauncherModel.moveItemInDatabase(mLauncher, info, mInfo.id, 0,
Winson Chung33231f52013-12-09 16:57:45 -0800861 info.cellX, info.cellY);
Adam Cohen4045eb72011-10-06 11:44:26 -0700862 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700863 }
Adam Cohen228da5a2011-07-27 22:23:47 -0700864
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700865 private void updateItemLocationsInDatabaseBatch() {
866 ArrayList<View> list = getItemsInReadingOrder();
867 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
868 for (int i = 0; i < list.size(); i++) {
869 View v = list.get(i);
870 ItemInfo info = (ItemInfo) v.getTag();
871 items.add(info);
872 }
873
874 LauncherModel.moveItemsInDatabase(mLauncher, items, mInfo.id, 0);
875 }
876
Adam Cohene25af792013-06-06 23:08:25 -0700877 public void addItemLocationsInDatabase() {
878 ArrayList<View> list = getItemsInReadingOrder();
879 for (int i = 0; i < list.size(); i++) {
880 View v = list.get(i);
881 ItemInfo info = (ItemInfo) v.getTag();
882 LauncherModel.addItemToDatabase(mLauncher, info, mInfo.id, 0,
883 info.cellX, info.cellY, false);
884 }
885 }
886
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700887 public void notifyDrop() {
888 if (mDragInProgress) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700889 mItemAddedBackToSelfViaIcon = true;
Adam Cohen76078c42011-06-09 15:06:52 -0700890 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700891 }
892
893 public boolean isDropEnabled() {
894 return true;
895 }
896
Adam Cohen4045eb72011-10-06 11:44:26 -0700897 private void setupContentDimensions(int count) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700898 ArrayList<View> list = getItemsInReadingOrder();
899
900 int countX = mContent.getCountX();
901 int countY = mContent.getCountY();
Adam Cohen7c693212011-05-18 15:26:57 -0700902 boolean done = false;
Adam Cohen2801caf2011-05-13 20:57:39 -0700903
Adam Cohen7c693212011-05-18 15:26:57 -0700904 while (!done) {
905 int oldCountX = countX;
906 int oldCountY = countY;
907 if (countX * countY < count) {
908 // Current grid is too small, expand it
Adam Cohen78dc83e2011-11-15 17:10:00 -0800909 if ((countX <= countY || countY == mMaxCountY) && countX < mMaxCountX) {
Adam Cohen7c693212011-05-18 15:26:57 -0700910 countX++;
911 } else if (countY < mMaxCountY) {
912 countY++;
913 }
914 if (countY == 0) countY++;
915 } else if ((countY - 1) * countX >= count && countY >= countX) {
916 countY = Math.max(0, countY - 1);
917 } else if ((countX - 1) * countY >= count) {
918 countX = Math.max(0, countX - 1);
Adam Cohen2801caf2011-05-13 20:57:39 -0700919 }
Adam Cohen7c693212011-05-18 15:26:57 -0700920 done = countX == oldCountX && countY == oldCountY;
Adam Cohen2801caf2011-05-13 20:57:39 -0700921 }
Adam Cohen7c693212011-05-18 15:26:57 -0700922 mContent.setGridSize(countX, countY);
Adam Cohen2801caf2011-05-13 20:57:39 -0700923 arrangeChildren(list);
924 }
925
926 public boolean isFull() {
Adam Cohen78dc83e2011-11-15 17:10:00 -0800927 return getItemCount() >= mMaxNumItems;
Adam Cohen2801caf2011-05-13 20:57:39 -0700928 }
929
930 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700931 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700932
Winson Chung892c74d2013-08-22 16:15:50 -0700933 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700934 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700935 int height = getFolderHeight();
Adam Cohen2801caf2011-05-13 20:57:39 -0700936
Adam Cohen307fe232012-08-16 17:55:58 -0700937 float scale = parent.getDescendantRectRelativeToSelf(mFolderIcon, mTempRect);
Adam Cohen8e776a62011-06-28 18:10:06 -0700938
Winson Chungaf40f202013-09-18 18:26:31 -0700939 LauncherAppState app = LauncherAppState.getInstance();
940 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
941
Adam Cohen307fe232012-08-16 17:55:58 -0700942 int centerX = (int) (mTempRect.left + mTempRect.width() * scale / 2);
943 int centerY = (int) (mTempRect.top + mTempRect.height() * scale / 2);
Adam Cohen2801caf2011-05-13 20:57:39 -0700944 int centeredLeft = centerX - width / 2;
945 int centeredTop = centerY - height / 2;
Winson Chung3057b1c2013-10-10 17:35:35 -0700946 int currentPage = mLauncher.getWorkspace().getNextPage();
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800947 // In case the workspace is scrolling, we need to use the final scroll to compute
948 // the folders bounds.
949 mLauncher.getWorkspace().setFinalScrollForPageChange(currentPage);
Adam Cohen35e7e642011-07-17 14:47:18 -0700950 // We first fetch the currently visible CellLayoutChildren
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800951 CellLayout currentLayout = (CellLayout) mLauncher.getWorkspace().getChildAt(currentPage);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700952 ShortcutAndWidgetContainer boundingLayout = currentLayout.getShortcutsAndWidgets();
Adam Cohen35e7e642011-07-17 14:47:18 -0700953 Rect bounds = new Rect();
954 parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800955 // We reset the workspaces scroll
956 mLauncher.getWorkspace().resetFinalScrollForPageChange(currentPage);
Adam Cohen2801caf2011-05-13 20:57:39 -0700957
Adam Cohen35e7e642011-07-17 14:47:18 -0700958 // We need to bound the folder to the currently visible CellLayoutChildren
959 int left = Math.min(Math.max(bounds.left, centeredLeft),
960 bounds.left + bounds.width() - width);
961 int top = Math.min(Math.max(bounds.top, centeredTop),
962 bounds.top + bounds.height() - height);
Winson Chungaf40f202013-09-18 18:26:31 -0700963 if (grid.isPhone() && (grid.availableWidthPx - width) < grid.iconSizePx) {
964 // Center the folder if it is full (on phones only)
965 left = (grid.availableWidthPx - width) / 2;
966 } else if (width >= bounds.width()) {
967 // If the folder doesn't fit within the bounds, center it about the desired bounds
Adam Cohen35e7e642011-07-17 14:47:18 -0700968 left = bounds.left + (bounds.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700969 }
Adam Cohen35e7e642011-07-17 14:47:18 -0700970 if (height >= bounds.height()) {
971 top = bounds.top + (bounds.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700972 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700973
974 int folderPivotX = width / 2 + (centeredLeft - left);
975 int folderPivotY = height / 2 + (centeredTop - top);
976 setPivotX(folderPivotX);
977 setPivotY(folderPivotY);
Adam Cohen268c4752012-06-06 17:47:33 -0700978 mFolderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700979 (1.0f * folderPivotX / width));
Adam Cohen268c4752012-06-06 17:47:33 -0700980 mFolderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700981 (1.0f * folderPivotY / height));
Adam Cohen3bf84d32012-05-07 20:17:14 -0700982
Adam Cohen662b5982011-12-13 17:45:21 -0800983 lp.width = width;
984 lp.height = height;
985 lp.x = left;
986 lp.y = top;
Adam Cohen2801caf2011-05-13 20:57:39 -0700987 }
988
Adam Cohen268c4752012-06-06 17:47:33 -0700989 float getPivotXForIconAnimation() {
990 return mFolderIconPivotX;
991 }
992 float getPivotYForIconAnimation() {
993 return mFolderIconPivotY;
994 }
995
Adam Cohen2801caf2011-05-13 20:57:39 -0700996 private void setupContentForNumItems(int count) {
Adam Cohen4045eb72011-10-06 11:44:26 -0700997 setupContentDimensions(count);
Adam Cohen2801caf2011-05-13 20:57:39 -0700998
Adam Cohen8e776a62011-06-28 18:10:06 -0700999 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -07001000 if (lp == null) {
Adam Cohen8e776a62011-06-28 18:10:06 -07001001 lp = new DragLayer.LayoutParams(0, 0);
1002 lp.customPosition = true;
Adam Cohen2801caf2011-05-13 20:57:39 -07001003 setLayoutParams(lp);
1004 }
1005 centerAboutIcon();
1006 }
1007
Winson Chung892c74d2013-08-22 16:15:50 -07001008 private int getContentAreaHeight() {
1009 LauncherAppState app = LauncherAppState.getInstance();
1010 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1011 Rect workspacePadding = grid.getWorkspacePadding(grid.isLandscape ?
1012 CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
1013 int maxContentAreaHeight = grid.availableHeightPx -
Winson Chung892c74d2013-08-22 16:15:50 -07001014 workspacePadding.top - workspacePadding.bottom -
Winson Chung892c74d2013-08-22 16:15:50 -07001015 mFolderNameHeight;
Adam Cohen1960ea42013-11-12 11:33:14 +00001016 int height = Math.min(maxContentAreaHeight,
Winson Chung892c74d2013-08-22 16:15:50 -07001017 mContent.getDesiredHeight());
Adam Cohen1960ea42013-11-12 11:33:14 +00001018 return Math.max(height, MIN_CONTENT_DIMEN);
1019 }
1020
1021 private int getContentAreaWidth() {
1022 return Math.max(mContent.getDesiredWidth(), MIN_CONTENT_DIMEN);
Winson Chung892c74d2013-08-22 16:15:50 -07001023 }
1024
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001025 private int getFolderHeight() {
Winson Chung892c74d2013-08-22 16:15:50 -07001026 int height = getPaddingTop() + getPaddingBottom()
1027 + getContentAreaHeight() + mFolderNameHeight;
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001028 return height;
1029 }
Adam Cohen234c4cd2011-07-17 21:03:04 -07001030
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001031 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001032 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
1033 int height = getFolderHeight();
Adam Cohen1960ea42013-11-12 11:33:14 +00001034 int contentAreaWidthSpec = MeasureSpec.makeMeasureSpec(getContentAreaWidth(),
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001035 MeasureSpec.EXACTLY);
Winson Chung892c74d2013-08-22 16:15:50 -07001036 int contentAreaHeightSpec = MeasureSpec.makeMeasureSpec(getContentAreaHeight(),
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001037 MeasureSpec.EXACTLY);
Adam Cohen1960ea42013-11-12 11:33:14 +00001038
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08001039 if (LauncherAppState.isDisableAllApps()) {
Nilesh Agrawal5f7099a2014-01-02 15:54:57 -08001040 // Don't cap the height of the content to allow scrolling.
1041 mContent.setFixedSize(getContentAreaWidth(), mContent.getDesiredHeight());
1042 } else {
1043 mContent.setFixedSize(getContentAreaWidth(), getContentAreaHeight());
1044 }
1045
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001046 mScrollView.measure(contentAreaWidthSpec, contentAreaHeightSpec);
1047 mFolderName.measure(contentAreaWidthSpec,
Adam Cohen234c4cd2011-07-17 21:03:04 -07001048 MeasureSpec.makeMeasureSpec(mFolderNameHeight, MeasureSpec.EXACTLY));
1049 setMeasuredDimension(width, height);
1050 }
1051
Adam Cohen2801caf2011-05-13 20:57:39 -07001052 private void arrangeChildren(ArrayList<View> list) {
1053 int[] vacant = new int[2];
1054 if (list == null) {
1055 list = getItemsInReadingOrder();
1056 }
1057 mContent.removeAllViews();
1058
1059 for (int i = 0; i < list.size(); i++) {
1060 View v = list.get(i);
1061 mContent.getVacantCell(vacant, 1, 1);
1062 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) v.getLayoutParams();
1063 lp.cellX = vacant[0];
1064 lp.cellY = vacant[1];
1065 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohen2792a332011-09-26 21:09:47 -07001066 if (info.cellX != vacant[0] || info.cellY != vacant[1]) {
1067 info.cellX = vacant[0];
1068 info.cellY = vacant[1];
1069 LauncherModel.addOrMoveItemInDatabase(mLauncher, info, mInfo.id, 0,
1070 info.cellX, info.cellY);
1071 }
Adam Cohen2801caf2011-05-13 20:57:39 -07001072 boolean insert = false;
1073 mContent.addViewToCellLayout(v, insert ? 0 : -1, (int)info.id, lp, true);
1074 }
Adam Cohen7c693212011-05-18 15:26:57 -07001075 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -07001076 }
1077
Adam Cohena9cf38f2011-05-02 15:36:58 -07001078 public int getItemCount() {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001079 return mContent.getShortcutsAndWidgets().getChildCount();
Adam Cohena9cf38f2011-05-02 15:36:58 -07001080 }
1081
1082 public View getItemAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001083 return mContent.getShortcutsAndWidgets().getChildAt(index);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001084 }
1085
Adam Cohen2801caf2011-05-13 20:57:39 -07001086 private void onCloseComplete() {
Adam Cohen05e0f402011-08-01 12:12:49 -07001087 DragLayer parent = (DragLayer) getParent();
Michael Jurka5649c282012-06-18 10:33:21 -07001088 if (parent != null) {
1089 parent.removeView(this);
1090 }
Adam Cohen4554ee12011-08-03 16:13:21 -07001091 mDragController.removeDropTarget((DropTarget) this);
Adam Cohen05e0f402011-08-01 12:12:49 -07001092 clearFocus();
Adam Cohenac56cff2011-09-28 20:45:37 -07001093 mFolderIcon.requestFocus();
Adam Cohen05e0f402011-08-01 12:12:49 -07001094
Adam Cohen2801caf2011-05-13 20:57:39 -07001095 if (mRearrangeOnClose) {
1096 setupContentForNumItems(getItemCount());
1097 mRearrangeOnClose = false;
1098 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001099 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001100 if (!mDragInProgress && !mSuppressFolderDeletion) {
1101 replaceFolderWithFinalItem();
1102 } else if (mDragInProgress) {
1103 mDeleteFolderOnDropCompleted = true;
1104 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001105 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001106 mSuppressFolderDeletion = false;
Adam Cohenafb01ee2011-06-23 15:38:03 -07001107 }
1108
1109 private void replaceFolderWithFinalItem() {
Adam Cohenafb01ee2011-06-23 15:38:03 -07001110 // Add the last remaining child to the workspace in place of the folder
Adam Cohenfb91f302012-06-11 15:45:18 -07001111 Runnable onCompleteRunnable = new Runnable() {
1112 @Override
1113 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -07001114 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screenId);
Adam Cohenafb01ee2011-06-23 15:38:03 -07001115
Winson Chung33231f52013-12-09 16:57:45 -08001116 View child = null;
Adam Cohenfb91f302012-06-11 15:45:18 -07001117 // Move the item from the folder to the workspace, in the position of the folder
1118 if (getItemCount() == 1) {
1119 ShortcutInfo finalItem = mInfo.contents.get(0);
Adam Cohenc5e63f32012-07-12 16:16:57 -07001120 child = mLauncher.createShortcut(R.layout.application, cellLayout,
Adam Cohenfb91f302012-06-11 15:45:18 -07001121 finalItem);
1122 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -07001123 mInfo.screenId, mInfo.cellX, mInfo.cellY);
Adam Cohenfb91f302012-06-11 15:45:18 -07001124 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001125 if (getItemCount() <= 1) {
1126 // Remove the folder
1127 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
Dan Sandler0eb687f2014-01-10 13:24:55 -05001128 if (cellLayout != null) {
1129 // b/12446428 -- sometimes the cell layout has already gone away?
1130 cellLayout.removeView(mFolderIcon);
1131 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001132 if (mFolderIcon instanceof DropTarget) {
1133 mDragController.removeDropTarget((DropTarget) mFolderIcon);
1134 }
1135 mLauncher.removeFolder(mInfo);
1136 }
Adam Cohenc5e63f32012-07-12 16:16:57 -07001137 // We add the child after removing the folder to prevent both from existing at
Winson Chung0e6a7132013-08-23 12:55:10 -07001138 // the same time in the CellLayout. We need to add the new item with addInScreenFromBind()
1139 // to ensure that hotseat items are placed correctly.
Adam Cohenc5e63f32012-07-12 16:16:57 -07001140 if (child != null) {
Winson Chung0e6a7132013-08-23 12:55:10 -07001141 mLauncher.getWorkspace().addInScreenFromBind(child, mInfo.container, mInfo.screenId,
Adam Cohenc5e63f32012-07-12 16:16:57 -07001142 mInfo.cellX, mInfo.cellY, mInfo.spanX, mInfo.spanY);
1143 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001144 }
1145 };
1146 View finalChild = getItemAt(0);
1147 if (finalChild != null) {
1148 mFolderIcon.performDestroyAnimation(finalChild, onCompleteRunnable);
Winson Chung33231f52013-12-09 16:57:45 -08001149 } else {
1150 onCompleteRunnable.run();
Adam Cohenafb01ee2011-06-23 15:38:03 -07001151 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001152 mDestroyed = true;
1153 }
1154
1155 boolean isDestroyed() {
1156 return mDestroyed;
Adam Cohen2801caf2011-05-13 20:57:39 -07001157 }
1158
Adam Cohenac56cff2011-09-28 20:45:37 -07001159 // This method keeps track of the last item in the folder for the purposes
1160 // of keyboard focus
1161 private void updateTextViewFocus() {
1162 View lastChild = getItemAt(getItemCount() - 1);
1163 getItemAt(getItemCount() - 1);
1164 if (lastChild != null) {
1165 mFolderName.setNextFocusDownId(lastChild.getId());
1166 mFolderName.setNextFocusRightId(lastChild.getId());
1167 mFolderName.setNextFocusLeftId(lastChild.getId());
1168 mFolderName.setNextFocusUpId(lastChild.getId());
1169 }
1170 }
1171
Adam Cohenbfbfd262011-06-13 16:55:12 -07001172 public void onDrop(DragObject d) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001173 Runnable cleanUpRunnable = null;
1174
Adam Cohen689ff162014-05-08 17:27:56 -07001175 // If we are coming from All Apps space, we defer removing the extra empty screen
1176 // until the folder closes
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001177 if (d.dragSource != mLauncher.getWorkspace() && !(d.dragSource instanceof Folder)) {
1178 cleanUpRunnable = new Runnable() {
1179 @Override
1180 public void run() {
Adam Cohen689ff162014-05-08 17:27:56 -07001181 mLauncher.exitSpringLoadedDragModeDelayed(true,
1182 Launcher.EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
1183 null);
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001184 }
1185 };
Adam Cohenbfbfd262011-06-13 16:55:12 -07001186 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001187
1188 View currentDragView;
1189 ShortcutInfo si = mCurrentDragInfo;
1190 if (mIsExternalDrag) {
1191 si.cellX = mEmptyCell[0];
1192 si.cellY = mEmptyCell[1];
Sunny Goyal95abbb32014-08-04 10:53:22 -07001193
1194 // Actually move the item in the database if it was an external drag. Call this
1195 // before creating the view, so that ShortcutInfo is updated appropriately.
1196 LauncherModel.addOrMoveItemInDatabase(
1197 mLauncher, si, mInfo.id, 0, si.cellX, si.cellY);
1198
1199 // We only need to update the locations if it doesn't get handled in #onDropCompleted.
1200 if (d.dragSource != this) {
1201 updateItemLocationsInDatabaseBatch();
1202 }
1203 mIsExternalDrag = false;
1204
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001205 currentDragView = createAndAddShortcut(si);
1206 } else {
1207 currentDragView = mCurrentDragView;
1208 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) currentDragView.getLayoutParams();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001209 si.cellX = lp.cellX = mEmptyCell[0];
1210 si.cellX = lp.cellY = mEmptyCell[1];
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001211 mContent.addViewToCellLayout(currentDragView, -1, (int) si.id, lp, true);
Adam Cohenbfbfd262011-06-13 16:55:12 -07001212 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001213
1214 if (d.dragView.hasDrawn()) {
1215
1216 // Temporarily reset the scale such that the animation target gets calculated correctly.
1217 float scaleX = getScaleX();
1218 float scaleY = getScaleY();
1219 setScaleX(1.0f);
1220 setScaleY(1.0f);
1221 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, currentDragView,
1222 cleanUpRunnable, null);
1223 setScaleX(scaleX);
1224 setScaleY(scaleY);
1225 } else {
1226 d.deferDragViewCleanupPostAnimation = false;
1227 currentDragView.setVisibility(VISIBLE);
1228 }
1229 mItemsInvalidated = true;
1230 setupContentDimensions(getItemCount());
1231
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001232 // Temporarily suppress the listener, as we did all the work already here.
1233 mSuppressOnAdd = true;
1234 mInfo.add(si);
1235 mSuppressOnAdd = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001236 }
1237
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001238 // This is used so the item doesn't immediately appear in the folder when added. In one case
1239 // we need to create the illusion that the item isn't added back to the folder yet, to
1240 // to correspond to the animation of the icon back into the folder. This is
1241 public void hideItem(ShortcutInfo info) {
1242 View v = getViewForInfo(info);
1243 v.setVisibility(INVISIBLE);
1244 }
1245 public void showItem(ShortcutInfo info) {
1246 View v = getViewForInfo(info);
1247 v.setVisibility(VISIBLE);
1248 }
1249
Adam Cohenbfbfd262011-06-13 16:55:12 -07001250 public void onAdd(ShortcutInfo item) {
1251 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001252 // If the item was dropped onto this open folder, we have done the work associated
1253 // with adding the item to the folder, as indicated by mSuppressOnAdd being set
Adam Cohenbfbfd262011-06-13 16:55:12 -07001254 if (mSuppressOnAdd) return;
1255 if (!findAndSetEmptyCells(item)) {
1256 // The current layout is full, can we expand it?
1257 setupContentForNumItems(getItemCount() + 1);
1258 findAndSetEmptyCells(item);
1259 }
1260 createAndAddShortcut(item);
1261 LauncherModel.addOrMoveItemInDatabase(
1262 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
1263 }
1264
Adam Cohena9cf38f2011-05-02 15:36:58 -07001265 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -07001266 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001267 // If this item is being dragged from this open folder, we have already handled
1268 // the work associated with removing the item, so we don't have to do anything here.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001269 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001270 View v = getViewForInfo(item);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001271 mContent.removeView(v);
Adam Cohen2801caf2011-05-13 20:57:39 -07001272 if (mState == STATE_ANIMATING) {
1273 mRearrangeOnClose = true;
1274 } else {
1275 setupContentForNumItems(getItemCount());
1276 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001277 if (getItemCount() <= 1) {
1278 replaceFolderWithFinalItem();
1279 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001280 }
Adam Cohen7c693212011-05-18 15:26:57 -07001281
Adam Cohendf1e4e82011-06-24 15:57:39 -07001282 private View getViewForInfo(ShortcutInfo item) {
1283 for (int j = 0; j < mContent.getCountY(); j++) {
1284 for (int i = 0; i < mContent.getCountX(); i++) {
1285 View v = mContent.getChildAt(i, j);
1286 if (v.getTag() == item) {
1287 return v;
1288 }
1289 }
1290 }
1291 return null;
1292 }
1293
Adam Cohen76078c42011-06-09 15:06:52 -07001294 public void onItemsChanged() {
Adam Cohenac56cff2011-09-28 20:45:37 -07001295 updateTextViewFocus();
Adam Cohen76078c42011-06-09 15:06:52 -07001296 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001297
Adam Cohen76fc0852011-06-17 13:26:23 -07001298 public void onTitleChanged(CharSequence title) {
1299 }
Adam Cohen76078c42011-06-09 15:06:52 -07001300
Adam Cohen7c693212011-05-18 15:26:57 -07001301 public ArrayList<View> getItemsInReadingOrder() {
1302 if (mItemsInvalidated) {
1303 mItemsInReadingOrder.clear();
1304 for (int j = 0; j < mContent.getCountY(); j++) {
1305 for (int i = 0; i < mContent.getCountX(); i++) {
1306 View v = mContent.getChildAt(i, j);
1307 if (v != null) {
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001308 mItemsInReadingOrder.add(v);
Adam Cohen7c693212011-05-18 15:26:57 -07001309 }
1310 }
1311 }
1312 mItemsInvalidated = false;
1313 }
1314 return mItemsInReadingOrder;
1315 }
Adam Cohen8dfcba42011-07-07 16:38:18 -07001316
1317 public void getLocationInDragLayer(int[] loc) {
1318 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
1319 }
Adam Cohenea0818d2011-09-30 20:06:58 -07001320
1321 public void onFocusChange(View v, boolean hasFocus) {
1322 if (v == mFolderName && hasFocus) {
1323 startEditingFolderName();
1324 }
1325 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001326
1327 @Override
1328 public void getHitRectRelativeToDragLayer(Rect outRect) {
1329 getHitRect(outRect);
1330 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001331}