blob: 655e5c3687c815f8903a274e0df0d8fd594a54ab [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
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700115 private int DRAG_MODE_NONE = 0;
116 private int DRAG_MODE_REORDER = 1;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700117 private int mDragMode = DRAG_MODE_NONE;
118
Adam Cohen1960ea42013-11-12 11:33:14 +0000119 // We avoid measuring the scroll view with a 0 width or height, as this
120 // results in CellLayout being measured as UNSPECIFIED, which it does
121 // not support.
122 private static final int MIN_CONTENT_DIMEN = 5;
123
Adam Cohenfb91f302012-06-11 15:45:18 -0700124 private boolean mDestroyed;
125
Alan Viverette4cda5b72013-08-28 17:53:41 -0700126 private AutoScrollHelper mAutoScrollHelper;
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700127
Michael Jurka1e2f4652013-07-08 18:03:46 -0700128 private Runnable mDeferredAction;
129 private boolean mDeferDropAfterUninstall;
130 private boolean mUninstallSuccessful;
131
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132 /**
133 * Used to inflate the Workspace from XML.
134 *
135 * @param context The application's context.
136 * @param attrs The attribtues set containing the Workspace's customization values.
137 */
138 public Folder(Context context, AttributeSet attrs) {
139 super(context, attrs);
Winson Chung5f8afe62013-08-12 16:19:28 -0700140
141 LauncherAppState app = LauncherAppState.getInstance();
142 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143 setAlwaysDrawnWithCacheEnabled(false);
Adam Cohendf2cc412011-04-27 16:56:57 -0700144 mInflater = LayoutInflater.from(context);
Winson Chung5f8afe62013-08-12 16:19:28 -0700145 mIconCache = app.getIconCache();
Adam Cohen78dc83e2011-11-15 17:10:00 -0800146
147 Resources res = getResources();
Michael Jurka414300a2013-08-27 15:42:35 +0200148 mMaxCountX = (int) grid.numColumns;
Nilesh Agrawal5f7099a2014-01-02 15:54:57 -0800149 // Allow scrolling folders when DISABLE_ALL_APPS is true.
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800150 if (LauncherAppState.isDisableAllApps()) {
Nilesh Agrawal5f7099a2014-01-02 15:54:57 -0800151 mMaxCountY = mMaxNumItems = Integer.MAX_VALUE;
152 } else {
153 mMaxCountY = (int) grid.numRows;
154 mMaxNumItems = mMaxCountX * mMaxCountY;
155 }
Adam Cohen76fc0852011-06-17 13:26:23 -0700156
157 mInputMethodManager = (InputMethodManager)
Michael Jurka8b805b12012-04-18 14:23:14 -0700158 getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
Adam Cohen76fc0852011-06-17 13:26:23 -0700159
Adam Cohen76fc0852011-06-17 13:26:23 -0700160 mExpandDuration = res.getInteger(R.integer.config_folderAnimDuration);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700161
162 if (sDefaultFolderName == null) {
163 sDefaultFolderName = res.getString(R.string.folder_name);
164 }
Adam Cohena65beee2011-06-27 21:32:23 -0700165 if (sHintText == null) {
166 sHintText = res.getString(R.string.folder_hint_text);
167 }
Adam Cohen4eac29a2011-07-11 17:53:37 -0700168 mLauncher = (Launcher) context;
Adam Cohenac56cff2011-09-28 20:45:37 -0700169 // We need this view to be focusable in touch mode so that when text editing of the folder
170 // name is complete, we have something to focus on, thus hiding the cursor and giving
171 // reliable behvior when clicking the text field (since it will always gain focus on click).
172 setFocusableInTouchMode(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173 }
174
175 @Override
176 protected void onFinishInflate() {
177 super.onFinishInflate();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700178 mScrollView = (ScrollView) findViewById(R.id.scroll_view);
Adam Cohendf2cc412011-04-27 16:56:57 -0700179 mContent = (CellLayout) findViewById(R.id.folder_content);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700180
Winson Chung5f8afe62013-08-12 16:19:28 -0700181 LauncherAppState app = LauncherAppState.getInstance();
182 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
183
Winson Chung5f8afe62013-08-12 16:19:28 -0700184 mContent.setCellDimensions(grid.folderCellWidthPx, grid.folderCellHeightPx);
Adam Cohen2801caf2011-05-13 20:57:39 -0700185 mContent.setGridSize(0, 0);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700186 mContent.getShortcutsAndWidgets().setMotionEventSplittingEnabled(false);
Adam Cohen2374abf2013-04-16 14:56:57 -0700187 mContent.setInvertIfRtl(true);
Adam Cohenac56cff2011-09-28 20:45:37 -0700188 mFolderName = (FolderEditText) findViewById(R.id.folder_name);
189 mFolderName.setFolder(this);
Adam Cohenea0818d2011-09-30 20:06:58 -0700190 mFolderName.setOnFocusChangeListener(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700191
192 // We find out how tall the text view wants to be (it is set to wrap_content), so that
193 // we can allocate the appropriate amount of space for it.
194 int measureSpec = MeasureSpec.UNSPECIFIED;
195 mFolderName.measure(measureSpec, measureSpec);
196 mFolderNameHeight = mFolderName.getMeasuredHeight();
197
198 // We disable action mode for now since it messes up the view on phones
199 mFolderName.setCustomSelectionActionModeCallback(mActionModeCallback);
Adam Cohen76fc0852011-06-17 13:26:23 -0700200 mFolderName.setOnEditorActionListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700201 mFolderName.setSelectAllOnFocus(true);
Adam Cohen7a14d0b2011-06-24 11:36:35 -0700202 mFolderName.setInputType(mFolderName.getInputType() |
203 InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
Alan Viverette4cda5b72013-08-28 17:53:41 -0700204 mAutoScrollHelper = new FolderAutoScrollHelper(mScrollView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800205 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700206
Adam Cohen76fc0852011-06-17 13:26:23 -0700207 private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
208 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
209 return false;
210 }
211
212 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
213 return false;
214 }
215
216 public void onDestroyActionMode(ActionMode mode) {
217 }
218
219 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
220 return false;
221 }
222 };
223
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800224 public void onClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700225 Object tag = v.getTag();
226 if (tag instanceof ShortcutInfo) {
Adam Cohenb5fe60c2013-06-06 22:03:51 -0700227 mLauncher.onClick(v);
Adam Cohendf2cc412011-04-27 16:56:57 -0700228 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800229 }
230
231 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -0700232 // Return if global dragging is not enabled
233 if (!mLauncher.isDraggingEnabled()) return true;
234
Adam Cohendf2cc412011-04-27 16:56:57 -0700235 Object tag = v.getTag();
236 if (tag instanceof ShortcutInfo) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700237 ShortcutInfo item = (ShortcutInfo) tag;
238 if (!v.isInTouchMode()) {
239 return false;
240 }
241
Winson Chunga6945242014-01-08 14:04:34 -0800242 mLauncher.getLauncherClings().dismissFolderCling(null);
Winson Chung7d7541e2011-09-16 20:14:36 -0700243
Adam Cohendf2cc412011-04-27 16:56:57 -0700244 mLauncher.getWorkspace().onDragStartedWithItem(v);
Adam Cohenac8c8762011-07-13 11:15:27 -0700245 mLauncher.getWorkspace().beginDragShared(v, this);
Adam Cohen76078c42011-06-09 15:06:52 -0700246
247 mCurrentDragInfo = item;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700248 mEmptyCell[0] = item.cellX;
249 mEmptyCell[1] = item.cellY;
250 mCurrentDragView = v;
Adam Cohenfc53cd22011-07-20 15:45:11 -0700251
252 mContent.removeView(mCurrentDragView);
253 mInfo.remove(mCurrentDragInfo);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700254 mDragInProgress = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700255 mItemAddedBackToSelfViaIcon = false;
Adam Cohendf2cc412011-04-27 16:56:57 -0700256 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800257 return true;
258 }
259
Adam Cohen76fc0852011-06-17 13:26:23 -0700260 public boolean isEditingName() {
261 return mIsEditingName;
262 }
263
264 public void startEditingFolderName() {
Adam Cohena65beee2011-06-27 21:32:23 -0700265 mFolderName.setHint("");
Adam Cohen76fc0852011-06-17 13:26:23 -0700266 mIsEditingName = true;
267 }
268
269 public void dismissEditingName() {
270 mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
271 doneEditingFolderName(true);
272 }
273
274 public void doneEditingFolderName(boolean commit) {
Adam Cohena65beee2011-06-27 21:32:23 -0700275 mFolderName.setHint(sHintText);
Adam Cohen1df26a32011-08-12 16:15:23 -0700276 // Convert to a string here to ensure that no other state associated with the text field
277 // gets saved.
Adam Cohen3371da02011-10-25 21:38:29 -0700278 String newTitle = mFolderName.getText().toString();
279 mInfo.setTitle(newTitle);
Adam Cohen76fc0852011-06-17 13:26:23 -0700280 LauncherModel.updateItemInDatabase(mLauncher, mInfo);
Adam Cohenac56cff2011-09-28 20:45:37 -0700281
Adam Cohen3371da02011-10-25 21:38:29 -0700282 if (commit) {
283 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700284 String.format(getContext().getString(R.string.folder_renamed), newTitle));
Adam Cohen3371da02011-10-25 21:38:29 -0700285 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700286 // In order to clear the focus from the text field, we set the focus on ourself. This
287 // ensures that every time the field is clicked, focus is gained, giving reliable behavior.
288 requestFocus();
289
Adam Cohene601a432011-07-26 21:51:30 -0700290 Selection.setSelection((Spannable) mFolderName.getText(), 0, 0);
Adam Cohen76fc0852011-06-17 13:26:23 -0700291 mIsEditingName = false;
292 }
293
294 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
295 if (actionId == EditorInfo.IME_ACTION_DONE) {
296 dismissEditingName();
297 return true;
298 }
299 return false;
300 }
301
302 public View getEditTextRegion() {
303 return mFolderName;
304 }
305
Anjali Koppalf05545f2014-03-10 19:18:43 -0700306 public CellLayout getContent() {
307 return mContent;
308 }
309
Adam Cohen0c872ba2011-05-05 10:34:16 -0700310 /**
311 * We need to handle touch events to prevent them from falling through to the workspace below.
312 */
313 @Override
314 public boolean onTouchEvent(MotionEvent ev) {
315 return true;
316 }
317
Joe Onorato00acb122009-08-04 16:04:30 -0400318 public void setDragController(DragController dragController) {
319 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800320 }
321
Adam Cohen2801caf2011-05-13 20:57:39 -0700322 void setFolderIcon(FolderIcon icon) {
323 mFolderIcon = icon;
324 }
325
Adam Cohen3371da02011-10-25 21:38:29 -0700326 @Override
327 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
328 // When the folder gets focus, we don't want to announce the list of items.
329 return true;
330 }
331
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800332 /**
333 * @return the FolderInfo object associated with this folder
334 */
335 FolderInfo getInfo() {
336 return mInfo;
337 }
338
Adam Cohen3bf84d32012-05-07 20:17:14 -0700339 private class GridComparator implements Comparator<ShortcutInfo> {
340 int mNumCols;
341 public GridComparator(int numCols) {
342 mNumCols = numCols;
343 }
344
345 @Override
346 public int compare(ShortcutInfo lhs, ShortcutInfo rhs) {
347 int lhIndex = lhs.cellY * mNumCols + lhs.cellX;
348 int rhIndex = rhs.cellY * mNumCols + rhs.cellX;
349 return (lhIndex - rhIndex);
350 }
Adam Cohen3bf84d32012-05-07 20:17:14 -0700351 }
352
353 private void placeInReadingOrder(ArrayList<ShortcutInfo> items) {
354 int maxX = 0;
355 int count = items.size();
356 for (int i = 0; i < count; i++) {
357 ShortcutInfo item = items.get(i);
358 if (item.cellX > maxX) {
359 maxX = item.cellX;
360 }
361 }
Adam Cohen691a5792012-05-11 14:27:30 -0700362
363 GridComparator gridComparator = new GridComparator(maxX + 1);
Adam Cohen3bf84d32012-05-07 20:17:14 -0700364 Collections.sort(items, gridComparator);
365 final int countX = mContent.getCountX();
366 for (int i = 0; i < count; i++) {
367 int x = i % countX;
368 int y = i / countX;
369 ShortcutInfo item = items.get(i);
370 item.cellX = x;
371 item.cellY = y;
372 }
373 }
374
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800375 void bind(FolderInfo info) {
376 mInfo = info;
Adam Cohendf2cc412011-04-27 16:56:57 -0700377 ArrayList<ShortcutInfo> children = info.contents;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700378 ArrayList<ShortcutInfo> overflow = new ArrayList<ShortcutInfo>();
Adam Cohen7c693212011-05-18 15:26:57 -0700379 setupContentForNumItems(children.size());
Adam Cohen3bf84d32012-05-07 20:17:14 -0700380 placeInReadingOrder(children);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700381 int count = 0;
Adam Cohendf2cc412011-04-27 16:56:57 -0700382 for (int i = 0; i < children.size(); i++) {
383 ShortcutInfo child = (ShortcutInfo) children.get(i);
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800384 if (createAndAddShortcut(child) == null) {
Adam Cohenc508b2d2011-06-28 14:41:44 -0700385 overflow.add(child);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700386 } else {
387 count++;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700388 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700389 }
Adam Cohenc508b2d2011-06-28 14:41:44 -0700390
Adam Cohen0057bbc2011-08-12 18:30:51 -0700391 // We rearrange the items in case there are any empty gaps
392 setupContentForNumItems(count);
393
Jason Monk4ff73882014-04-24 16:48:07 -0400394 // If our folder has too many items we prune them from the list. This is an issue
Adam Cohenc508b2d2011-06-28 14:41:44 -0700395 // when upgrading from the old Folders implementation which could contain an unlimited
396 // number of items.
397 for (ShortcutInfo item: overflow) {
398 mInfo.remove(item);
399 LauncherModel.deleteItemFromDatabase(mLauncher, item);
400 }
401
Adam Cohen4dbe6d92011-05-18 17:14:15 -0700402 mItemsInvalidated = true;
Adam Cohenac56cff2011-09-28 20:45:37 -0700403 updateTextViewFocus();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700404 mInfo.addListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700405
Adam Cohenafb01ee2011-06-23 15:38:03 -0700406 if (!sDefaultFolderName.contentEquals(mInfo.title)) {
Adam Cohen4ef610f2011-06-21 22:37:36 -0700407 mFolderName.setText(mInfo.title);
408 } else {
409 mFolderName.setText("");
410 }
Adam Cohen691a5792012-05-11 14:27:30 -0700411 updateItemLocationsInDatabase();
Winson Chung33231f52013-12-09 16:57:45 -0800412
413 // In case any children didn't come across during loading, clean up the folder accordingly
414 mFolderIcon.post(new Runnable() {
415 public void run() {
416 if (getItemCount() <= 1) {
417 replaceFolderWithFinalItem();
418 }
419 }
420 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700421 }
422
423 /**
424 * Creates a new UserFolder, inflated from R.layout.user_folder.
425 *
426 * @param context The application's context.
427 *
428 * @return A new UserFolder.
429 */
430 static Folder fromXml(Context context) {
431 return (Folder) LayoutInflater.from(context).inflate(R.layout.user_folder, null);
432 }
433
434 /**
435 * This method is intended to make the UserFolder to be visually identical in size and position
436 * to its associated FolderIcon. This allows for a seamless transition into the expanded state.
437 */
438 private void positionAndSizeAsIcon() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700439 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800440 setScaleX(0.8f);
441 setScaleY(0.8f);
442 setAlpha(0f);
Adam Cohendf2cc412011-04-27 16:56:57 -0700443 mState = STATE_SMALL;
444 }
445
446 public void animateOpen() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700447 positionAndSizeAsIcon();
448
Adam Cohen8e776a62011-06-28 18:10:06 -0700449 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen2801caf2011-05-13 20:57:39 -0700450 centerAboutIcon();
Adam Cohen662b5982011-12-13 17:45:21 -0800451 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1);
452 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
453 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
Michael Jurka032e6ba2013-04-22 15:08:42 +0200454 final ObjectAnimator oa =
Michael Jurka2ecf9952012-06-18 12:52:28 -0700455 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohen6441de02011-12-14 14:25:32 -0800456
Adam Cohen2801caf2011-05-13 20:57:39 -0700457 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700458 @Override
459 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700460 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700461 String.format(getContext().getString(R.string.folder_opened),
Adam Cohen3371da02011-10-25 21:38:29 -0700462 mContent.getCountX(), mContent.getCountY()));
Adam Cohendf2cc412011-04-27 16:56:57 -0700463 mState = STATE_ANIMATING;
464 }
465 @Override
466 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700467 mState = STATE_OPEN;
Michael Jurka0121c3e2012-05-31 08:36:04 -0700468 setLayerType(LAYER_TYPE_NONE, null);
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800469
470 // Only show cling if we are not in the middle of a drag - this would be quite jarring.
471 if (!mDragController.isDragging()) {
472 Cling cling = mLauncher.getLauncherClings().showFoldersCling();
473 if (cling != null) {
474 cling.bringScrimToFront();
475 bringToFront();
476 cling.bringToFront();
477 }
Winson Chung7a74ac92011-09-20 17:43:51 -0700478 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700479 setFocusOnFirstChild();
Adam Cohendf2cc412011-04-27 16:56:57 -0700480 }
481 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700482 oa.setDuration(mExpandDuration);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700483 setLayerType(LAYER_TYPE_HARDWARE, null);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100484 oa.start();
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800485
486 // Make sure the folder picks up the last drag move even if the finger doesn't move.
487 if (mDragController.isDragging()) {
488 mDragController.forceTouchMove();
489 }
490 }
491
492 public void beginExternalDrag(ShortcutInfo item) {
493 setupContentForNumItems(getItemCount() + 1);
494 findAndSetEmptyCells(item);
495
496 mCurrentDragInfo = item;
497 mEmptyCell[0] = item.cellX;
498 mEmptyCell[1] = item.cellY;
499 mIsExternalDrag = true;
500
501 mDragInProgress = true;
Adam Cohendf2cc412011-04-27 16:56:57 -0700502 }
503
Adam Cohen3371da02011-10-25 21:38:29 -0700504 private void sendCustomAccessibilityEvent(int type, String text) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700505 AccessibilityManager accessibilityManager = (AccessibilityManager)
506 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
507 if (accessibilityManager.isEnabled()) {
Adam Cohen3371da02011-10-25 21:38:29 -0700508 AccessibilityEvent event = AccessibilityEvent.obtain(type);
509 onInitializeAccessibilityEvent(event);
510 event.getText().add(text);
Michael Jurka8b805b12012-04-18 14:23:14 -0700511 accessibilityManager.sendAccessibilityEvent(event);
Adam Cohen3371da02011-10-25 21:38:29 -0700512 }
513 }
514
Adam Cohenac56cff2011-09-28 20:45:37 -0700515 private void setFocusOnFirstChild() {
516 View firstChild = mContent.getChildAt(0, 0);
517 if (firstChild != null) {
518 firstChild.requestFocus();
519 }
520 }
521
Adam Cohendf2cc412011-04-27 16:56:57 -0700522 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700523 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800524 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
525 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
526 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
Michael Jurka032e6ba2013-04-22 15:08:42 +0200527 final ObjectAnimator oa =
Michael Jurka2ecf9952012-06-18 12:52:28 -0700528 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohendf2cc412011-04-27 16:56:57 -0700529
Adam Cohen2801caf2011-05-13 20:57:39 -0700530 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700531 @Override
532 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700533 onCloseComplete();
Michael Jurka0121c3e2012-05-31 08:36:04 -0700534 setLayerType(LAYER_TYPE_NONE, null);
Adam Cohen2801caf2011-05-13 20:57:39 -0700535 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700536 }
537 @Override
538 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700539 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700540 getContext().getString(R.string.folder_closed));
Adam Cohendf2cc412011-04-27 16:56:57 -0700541 mState = STATE_ANIMATING;
542 }
543 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700544 oa.setDuration(mExpandDuration);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700545 setLayerType(LAYER_TYPE_HARDWARE, null);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100546 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700547 }
548
Adam Cohencb3382b2011-05-24 14:07:08 -0700549 public boolean acceptDrop(DragObject d) {
550 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700551 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700552 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
553 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
554 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700555 }
556
Adam Cohendf2cc412011-04-27 16:56:57 -0700557 protected boolean findAndSetEmptyCells(ShortcutInfo item) {
558 int[] emptyCell = new int[2];
559 if (mContent.findCellForSpan(emptyCell, item.spanX, item.spanY)) {
560 item.cellX = emptyCell[0];
561 item.cellY = emptyCell[1];
Adam Cohendf2cc412011-04-27 16:56:57 -0700562 return true;
563 } else {
564 return false;
565 }
566 }
567
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800568 protected View createAndAddShortcut(ShortcutInfo item) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700569 final BubbleTextView textView =
570 (BubbleTextView) mInflater.inflate(R.layout.folder_application, this, false);
571 textView.applyFromShortcutInfo(item, mIconCache, false);
572
Adam Cohendf2cc412011-04-27 16:56:57 -0700573 textView.setOnClickListener(this);
574 textView.setOnLongClickListener(this);
575
Adam Cohenc508b2d2011-06-28 14:41:44 -0700576 // We need to check here to verify that the given item's location isn't already occupied
Adam Cohen3bf84d32012-05-07 20:17:14 -0700577 // by another item.
Adam Cohen0057bbc2011-08-12 18:30:51 -0700578 if (mContent.getChildAt(item.cellX, item.cellY) != null || item.cellX < 0 || item.cellY < 0
579 || item.cellX >= mContent.getCountX() || item.cellY >= mContent.getCountY()) {
Jason Monk4ff73882014-04-24 16:48:07 -0400580 // This shouldn't happen, log it.
Adam Cohen3bf84d32012-05-07 20:17:14 -0700581 Log.e(TAG, "Folder order not properly persisted during bind");
Adam Cohenc508b2d2011-06-28 14:41:44 -0700582 if (!findAndSetEmptyCells(item)) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800583 return null;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700584 }
585 }
586
Adam Cohendf2cc412011-04-27 16:56:57 -0700587 CellLayout.LayoutParams lp =
588 new CellLayout.LayoutParams(item.cellX, item.cellY, item.spanX, item.spanY);
589 boolean insert = false;
Adam Cohenac56cff2011-09-28 20:45:37 -0700590 textView.setOnKeyListener(new FolderKeyEventListener());
Adam Cohendf2cc412011-04-27 16:56:57 -0700591 mContent.addViewToCellLayout(textView, insert ? 0 : -1, (int)item.id, lp, true);
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800592 return textView;
Adam Cohendf2cc412011-04-27 16:56:57 -0700593 }
594
Adam Cohencb3382b2011-05-24 14:07:08 -0700595 public void onDragEnter(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700596 mPreviousTargetCell[0] = -1;
597 mPreviousTargetCell[1] = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700598 mOnExitAlarm.cancelAlarm();
599 }
600
601 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
602 public void onAlarm(Alarm alarm) {
603 realTimeReorder(mEmptyCell, mTargetCell);
604 }
605 };
606
607 boolean readingOrderGreaterThan(int[] v1, int[] v2) {
608 if (v1[1] > v2[1] || (v1[1] == v2[1] && v1[0] > v2[0])) {
609 return true;
610 } else {
611 return false;
612 }
613 }
614
615 private void realTimeReorder(int[] empty, int[] target) {
616 boolean wrap;
617 int startX;
618 int endX;
619 int startY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700620 int delay = 0;
621 float delayAmount = 30;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700622 if (readingOrderGreaterThan(target, empty)) {
623 wrap = empty[0] >= mContent.getCountX() - 1;
624 startY = wrap ? empty[1] + 1 : empty[1];
625 for (int y = startY; y <= target[1]; y++) {
626 startX = y == empty[1] ? empty[0] + 1 : 0;
627 endX = y < target[1] ? mContent.getCountX() - 1 : target[0];
628 for (int x = startX; x <= endX; x++) {
629 View v = mContent.getChildAt(x,y);
630 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen482ed822012-03-02 14:15:13 -0800631 REORDER_ANIMATION_DURATION, delay, true, true)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700632 empty[0] = x;
633 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700634 delay += delayAmount;
635 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700636 }
637 }
638 }
639 } else {
640 wrap = empty[0] == 0;
641 startY = wrap ? empty[1] - 1 : empty[1];
642 for (int y = startY; y >= target[1]; y--) {
643 startX = y == empty[1] ? empty[0] - 1 : mContent.getCountX() - 1;
644 endX = y > target[1] ? 0 : target[0];
645 for (int x = startX; x >= endX; x--) {
646 View v = mContent.getChildAt(x,y);
647 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen482ed822012-03-02 14:15:13 -0800648 REORDER_ANIMATION_DURATION, delay, true, true)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700649 empty[0] = x;
650 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700651 delay += delayAmount;
652 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700653 }
654 }
655 }
656 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700657 }
658
Adam Cohen2374abf2013-04-16 14:56:57 -0700659 public boolean isLayoutRtl() {
660 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
661 }
662
Adam Cohencb3382b2011-05-24 14:07:08 -0700663 public void onDragOver(DragObject d) {
Alan Viverette4cda5b72013-08-28 17:53:41 -0700664 final DragView dragView = d.dragView;
665 final int scrollOffset = mScrollView.getScrollY();
666 final float[] r = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, dragView, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700667 r[0] -= getPaddingLeft();
668 r[1] -= getPaddingTop();
669
Alan Viverette4cda5b72013-08-28 17:53:41 -0700670 final long downTime = SystemClock.uptimeMillis();
671 final MotionEvent translatedEv = MotionEvent.obtain(
672 downTime, downTime, MotionEvent.ACTION_MOVE, d.x, d.y, 0);
673
674 if (!mAutoScrollHelper.isEnabled()) {
675 mAutoScrollHelper.setEnabled(true);
676 }
677
678 final boolean handled = mAutoScrollHelper.onTouch(this, translatedEv);
679 translatedEv.recycle();
680
681 if (handled) {
682 mReorderAlarm.cancelAlarm();
683 } else {
684 mTargetCell = mContent.findNearestArea(
685 (int) r[0], (int) r[1] + scrollOffset, 1, 1, mTargetCell);
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700686 if (isLayoutRtl()) {
687 mTargetCell[0] = mContent.getCountX() - mTargetCell[0] - 1;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700688 }
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700689 if (mTargetCell[0] != mPreviousTargetCell[0]
690 || mTargetCell[1] != mPreviousTargetCell[1]) {
691 mReorderAlarm.cancelAlarm();
692 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
693 mReorderAlarm.setAlarm(REORDER_DELAY);
694 mPreviousTargetCell[0] = mTargetCell[0];
695 mPreviousTargetCell[1] = mTargetCell[1];
696 mDragMode = DRAG_MODE_REORDER;
697 } else {
698 mDragMode = DRAG_MODE_NONE;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700699 }
Adam Cohenbfbfd262011-06-13 16:55:12 -0700700 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700701 }
702
Adam Cohenbfbfd262011-06-13 16:55:12 -0700703 // This is used to compute the visual center of the dragView. The idea is that
704 // the visual center represents the user's interpretation of where the item is, and hence
705 // is the appropriate point to use when determining drop location.
706 private float[] getDragViewVisualCenter(int x, int y, int xOffset, int yOffset,
707 DragView dragView, float[] recycle) {
708 float res[];
709 if (recycle == null) {
710 res = new float[2];
711 } else {
712 res = recycle;
713 }
714
715 // These represent the visual top and left of drag view if a dragRect was provided.
716 // If a dragRect was not provided, then they correspond to the actual view left and
717 // top, as the dragRect is in that case taken to be the entire dragView.
718 // R.dimen.dragViewOffsetY.
719 int left = x - xOffset;
720 int top = y - yOffset;
721
722 // In order to find the visual center, we shift by half the dragRect
723 res[0] = left + dragView.getDragRegion().width() / 2;
724 res[1] = top + dragView.getDragRegion().height() / 2;
725
726 return res;
727 }
728
729 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
730 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700731 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700732 }
733 };
734
Adam Cohen95bb8002011-07-03 23:40:28 -0700735 public void completeDragExit() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700736 mLauncher.closeFolder();
737 mCurrentDragInfo = null;
738 mCurrentDragView = null;
739 mSuppressOnAdd = false;
740 mRearrangeOnClose = true;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800741 mIsExternalDrag = false;
Adam Cohen3e8f8112011-07-02 18:03:00 -0700742 }
743
Adam Cohencb3382b2011-05-24 14:07:08 -0700744 public void onDragExit(DragObject d) {
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700745 // Exiting folder; stop the auto scroller.
Alan Viverette4cda5b72013-08-28 17:53:41 -0700746 mAutoScrollHelper.setEnabled(false);
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700747 // We only close the folder if this is a true drag exit, ie. not because
748 // a drop has occurred above the folder.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700749 if (!d.dragComplete) {
750 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
751 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
752 }
753 mReorderAlarm.cancelAlarm();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700754 mDragMode = DRAG_MODE_NONE;
Adam Cohen2801caf2011-05-13 20:57:39 -0700755 }
756
Michael Jurka1e2f4652013-07-08 18:03:46 -0700757 public void onDropCompleted(final View target, final DragObject d,
758 final boolean isFlingToDelete, final boolean success) {
759 if (mDeferDropAfterUninstall) {
Michael Jurkaf3007582013-08-21 14:33:57 +0200760 Log.d(TAG, "Deferred handling drop because waiting for uninstall.");
Michael Jurka1e2f4652013-07-08 18:03:46 -0700761 mDeferredAction = new Runnable() {
762 public void run() {
763 onDropCompleted(target, d, isFlingToDelete, success);
764 mDeferredAction = null;
765 }
766 };
767 return;
768 }
769
770 boolean beingCalledAfterUninstall = mDeferredAction != null;
771 boolean successfulDrop =
772 success && (!beingCalledAfterUninstall || mUninstallSuccessful);
Winson Chung5f8afe62013-08-12 16:19:28 -0700773
Michael Jurka1e2f4652013-07-08 18:03:46 -0700774 if (successfulDrop) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800775 if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon && target != this) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700776 replaceFolderWithFinalItem();
777 }
778 } else {
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700779 setupContentForNumItems(getItemCount());
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700780 // The drag failed, we need to return the item to the folder
781 mFolderIcon.onDrop(d);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700782 }
783
784 if (target != this) {
785 if (mOnExitAlarm.alarmPending()) {
786 mOnExitAlarm.cancelAlarm();
Michael Jurka54554252013-08-01 12:52:23 +0200787 if (!successfulDrop) {
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700788 mSuppressFolderDeletion = true;
789 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700790 completeDragExit();
791 }
Adam Cohen9c58d822013-12-13 17:18:10 -0800792 }
793
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700794 mDeleteFolderOnDropCompleted = false;
795 mDragInProgress = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700796 mItemAddedBackToSelfViaIcon = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700797 mCurrentDragInfo = null;
798 mCurrentDragView = null;
799 mSuppressOnAdd = false;
Adam Cohen4045eb72011-10-06 11:44:26 -0700800
801 // Reordering may have occured, and we need to save the new item locations. We do this once
802 // at the end to prevent unnecessary database operations.
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700803 updateItemLocationsInDatabaseBatch();
Adam Cohen4045eb72011-10-06 11:44:26 -0700804 }
805
Michael Jurka1e2f4652013-07-08 18:03:46 -0700806 public void deferCompleteDropAfterUninstallActivity() {
807 mDeferDropAfterUninstall = true;
808 }
809
810 public void onUninstallActivityReturned(boolean success) {
811 mDeferDropAfterUninstall = false;
812 mUninstallSuccessful = success;
813 if (mDeferredAction != null) {
814 mDeferredAction.run();
815 }
816 }
817
Winson Chunga48487a2012-03-20 16:19:37 -0700818 @Override
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800819 public float getIntrinsicIconScaleFactor() {
820 return 1f;
821 }
822
823 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800824 public boolean supportsFlingToDelete() {
825 return true;
826 }
827
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000828 @Override
829 public boolean supportsAppInfoDropTarget() {
830 return false;
831 }
832
833 @Override
834 public boolean supportsDeleteDropTarget() {
835 return true;
836 }
837
Winson Chunga48487a2012-03-20 16:19:37 -0700838 public void onFlingToDelete(DragObject d, int x, int y, PointF vec) {
839 // Do nothing
840 }
841
842 @Override
843 public void onFlingToDeleteCompleted() {
844 // Do nothing
845 }
846
Adam Cohen4045eb72011-10-06 11:44:26 -0700847 private void updateItemLocationsInDatabase() {
848 ArrayList<View> list = getItemsInReadingOrder();
849 for (int i = 0; i < list.size(); i++) {
850 View v = list.get(i);
851 ItemInfo info = (ItemInfo) v.getTag();
852 LauncherModel.moveItemInDatabase(mLauncher, info, mInfo.id, 0,
Winson Chung33231f52013-12-09 16:57:45 -0800853 info.cellX, info.cellY);
Adam Cohen4045eb72011-10-06 11:44:26 -0700854 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700855 }
Adam Cohen228da5a2011-07-27 22:23:47 -0700856
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700857 private void updateItemLocationsInDatabaseBatch() {
858 ArrayList<View> list = getItemsInReadingOrder();
859 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
860 for (int i = 0; i < list.size(); i++) {
861 View v = list.get(i);
862 ItemInfo info = (ItemInfo) v.getTag();
863 items.add(info);
864 }
865
866 LauncherModel.moveItemsInDatabase(mLauncher, items, mInfo.id, 0);
867 }
868
Adam Cohene25af792013-06-06 23:08:25 -0700869 public void addItemLocationsInDatabase() {
870 ArrayList<View> list = getItemsInReadingOrder();
871 for (int i = 0; i < list.size(); i++) {
872 View v = list.get(i);
873 ItemInfo info = (ItemInfo) v.getTag();
874 LauncherModel.addItemToDatabase(mLauncher, info, mInfo.id, 0,
875 info.cellX, info.cellY, false);
876 }
877 }
878
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700879 public void notifyDrop() {
880 if (mDragInProgress) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700881 mItemAddedBackToSelfViaIcon = true;
Adam Cohen76078c42011-06-09 15:06:52 -0700882 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700883 }
884
885 public boolean isDropEnabled() {
886 return true;
887 }
888
Adam Cohen4045eb72011-10-06 11:44:26 -0700889 private void setupContentDimensions(int count) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700890 ArrayList<View> list = getItemsInReadingOrder();
891
892 int countX = mContent.getCountX();
893 int countY = mContent.getCountY();
Adam Cohen7c693212011-05-18 15:26:57 -0700894 boolean done = false;
Adam Cohen2801caf2011-05-13 20:57:39 -0700895
Adam Cohen7c693212011-05-18 15:26:57 -0700896 while (!done) {
897 int oldCountX = countX;
898 int oldCountY = countY;
899 if (countX * countY < count) {
900 // Current grid is too small, expand it
Adam Cohen78dc83e2011-11-15 17:10:00 -0800901 if ((countX <= countY || countY == mMaxCountY) && countX < mMaxCountX) {
Adam Cohen7c693212011-05-18 15:26:57 -0700902 countX++;
903 } else if (countY < mMaxCountY) {
904 countY++;
905 }
906 if (countY == 0) countY++;
907 } else if ((countY - 1) * countX >= count && countY >= countX) {
908 countY = Math.max(0, countY - 1);
909 } else if ((countX - 1) * countY >= count) {
910 countX = Math.max(0, countX - 1);
Adam Cohen2801caf2011-05-13 20:57:39 -0700911 }
Adam Cohen7c693212011-05-18 15:26:57 -0700912 done = countX == oldCountX && countY == oldCountY;
Adam Cohen2801caf2011-05-13 20:57:39 -0700913 }
Adam Cohen7c693212011-05-18 15:26:57 -0700914 mContent.setGridSize(countX, countY);
Adam Cohen2801caf2011-05-13 20:57:39 -0700915 arrangeChildren(list);
916 }
917
918 public boolean isFull() {
Adam Cohen78dc83e2011-11-15 17:10:00 -0800919 return getItemCount() >= mMaxNumItems;
Adam Cohen2801caf2011-05-13 20:57:39 -0700920 }
921
922 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700923 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700924
Winson Chung892c74d2013-08-22 16:15:50 -0700925 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700926 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700927 int height = getFolderHeight();
Adam Cohen2801caf2011-05-13 20:57:39 -0700928
Adam Cohen307fe232012-08-16 17:55:58 -0700929 float scale = parent.getDescendantRectRelativeToSelf(mFolderIcon, mTempRect);
Adam Cohen8e776a62011-06-28 18:10:06 -0700930
Winson Chungaf40f202013-09-18 18:26:31 -0700931 LauncherAppState app = LauncherAppState.getInstance();
932 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
933
Adam Cohen307fe232012-08-16 17:55:58 -0700934 int centerX = (int) (mTempRect.left + mTempRect.width() * scale / 2);
935 int centerY = (int) (mTempRect.top + mTempRect.height() * scale / 2);
Adam Cohen2801caf2011-05-13 20:57:39 -0700936 int centeredLeft = centerX - width / 2;
937 int centeredTop = centerY - height / 2;
Winson Chung3057b1c2013-10-10 17:35:35 -0700938 int currentPage = mLauncher.getWorkspace().getNextPage();
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800939 // In case the workspace is scrolling, we need to use the final scroll to compute
940 // the folders bounds.
941 mLauncher.getWorkspace().setFinalScrollForPageChange(currentPage);
Adam Cohen35e7e642011-07-17 14:47:18 -0700942 // We first fetch the currently visible CellLayoutChildren
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800943 CellLayout currentLayout = (CellLayout) mLauncher.getWorkspace().getChildAt(currentPage);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700944 ShortcutAndWidgetContainer boundingLayout = currentLayout.getShortcutsAndWidgets();
Adam Cohen35e7e642011-07-17 14:47:18 -0700945 Rect bounds = new Rect();
946 parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800947 // We reset the workspaces scroll
948 mLauncher.getWorkspace().resetFinalScrollForPageChange(currentPage);
Adam Cohen2801caf2011-05-13 20:57:39 -0700949
Adam Cohen35e7e642011-07-17 14:47:18 -0700950 // We need to bound the folder to the currently visible CellLayoutChildren
951 int left = Math.min(Math.max(bounds.left, centeredLeft),
952 bounds.left + bounds.width() - width);
953 int top = Math.min(Math.max(bounds.top, centeredTop),
954 bounds.top + bounds.height() - height);
Winson Chungaf40f202013-09-18 18:26:31 -0700955 if (grid.isPhone() && (grid.availableWidthPx - width) < grid.iconSizePx) {
956 // Center the folder if it is full (on phones only)
957 left = (grid.availableWidthPx - width) / 2;
958 } else if (width >= bounds.width()) {
959 // If the folder doesn't fit within the bounds, center it about the desired bounds
Adam Cohen35e7e642011-07-17 14:47:18 -0700960 left = bounds.left + (bounds.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700961 }
Adam Cohen35e7e642011-07-17 14:47:18 -0700962 if (height >= bounds.height()) {
963 top = bounds.top + (bounds.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700964 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700965
966 int folderPivotX = width / 2 + (centeredLeft - left);
967 int folderPivotY = height / 2 + (centeredTop - top);
968 setPivotX(folderPivotX);
969 setPivotY(folderPivotY);
Adam Cohen268c4752012-06-06 17:47:33 -0700970 mFolderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700971 (1.0f * folderPivotX / width));
Adam Cohen268c4752012-06-06 17:47:33 -0700972 mFolderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700973 (1.0f * folderPivotY / height));
Adam Cohen3bf84d32012-05-07 20:17:14 -0700974
Adam Cohen662b5982011-12-13 17:45:21 -0800975 lp.width = width;
976 lp.height = height;
977 lp.x = left;
978 lp.y = top;
Adam Cohen2801caf2011-05-13 20:57:39 -0700979 }
980
Adam Cohen268c4752012-06-06 17:47:33 -0700981 float getPivotXForIconAnimation() {
982 return mFolderIconPivotX;
983 }
984 float getPivotYForIconAnimation() {
985 return mFolderIconPivotY;
986 }
987
Adam Cohen2801caf2011-05-13 20:57:39 -0700988 private void setupContentForNumItems(int count) {
Adam Cohen4045eb72011-10-06 11:44:26 -0700989 setupContentDimensions(count);
Adam Cohen2801caf2011-05-13 20:57:39 -0700990
Adam Cohen8e776a62011-06-28 18:10:06 -0700991 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700992 if (lp == null) {
Adam Cohen8e776a62011-06-28 18:10:06 -0700993 lp = new DragLayer.LayoutParams(0, 0);
994 lp.customPosition = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700995 setLayoutParams(lp);
996 }
997 centerAboutIcon();
998 }
999
Winson Chung892c74d2013-08-22 16:15:50 -07001000 private int getContentAreaHeight() {
1001 LauncherAppState app = LauncherAppState.getInstance();
1002 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1003 Rect workspacePadding = grid.getWorkspacePadding(grid.isLandscape ?
1004 CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
1005 int maxContentAreaHeight = grid.availableHeightPx -
Winson Chung892c74d2013-08-22 16:15:50 -07001006 workspacePadding.top - workspacePadding.bottom -
Winson Chung892c74d2013-08-22 16:15:50 -07001007 mFolderNameHeight;
Adam Cohen1960ea42013-11-12 11:33:14 +00001008 int height = Math.min(maxContentAreaHeight,
Winson Chung892c74d2013-08-22 16:15:50 -07001009 mContent.getDesiredHeight());
Adam Cohen1960ea42013-11-12 11:33:14 +00001010 return Math.max(height, MIN_CONTENT_DIMEN);
1011 }
1012
1013 private int getContentAreaWidth() {
1014 return Math.max(mContent.getDesiredWidth(), MIN_CONTENT_DIMEN);
Winson Chung892c74d2013-08-22 16:15:50 -07001015 }
1016
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001017 private int getFolderHeight() {
Winson Chung892c74d2013-08-22 16:15:50 -07001018 int height = getPaddingTop() + getPaddingBottom()
1019 + getContentAreaHeight() + mFolderNameHeight;
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001020 return height;
1021 }
Adam Cohen234c4cd2011-07-17 21:03:04 -07001022
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001023 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001024 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
1025 int height = getFolderHeight();
Adam Cohen1960ea42013-11-12 11:33:14 +00001026 int contentAreaWidthSpec = MeasureSpec.makeMeasureSpec(getContentAreaWidth(),
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001027 MeasureSpec.EXACTLY);
Winson Chung892c74d2013-08-22 16:15:50 -07001028 int contentAreaHeightSpec = MeasureSpec.makeMeasureSpec(getContentAreaHeight(),
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001029 MeasureSpec.EXACTLY);
Adam Cohen1960ea42013-11-12 11:33:14 +00001030
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08001031 if (LauncherAppState.isDisableAllApps()) {
Nilesh Agrawal5f7099a2014-01-02 15:54:57 -08001032 // Don't cap the height of the content to allow scrolling.
1033 mContent.setFixedSize(getContentAreaWidth(), mContent.getDesiredHeight());
1034 } else {
1035 mContent.setFixedSize(getContentAreaWidth(), getContentAreaHeight());
1036 }
1037
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001038 mScrollView.measure(contentAreaWidthSpec, contentAreaHeightSpec);
1039 mFolderName.measure(contentAreaWidthSpec,
Adam Cohen234c4cd2011-07-17 21:03:04 -07001040 MeasureSpec.makeMeasureSpec(mFolderNameHeight, MeasureSpec.EXACTLY));
1041 setMeasuredDimension(width, height);
1042 }
1043
Adam Cohen2801caf2011-05-13 20:57:39 -07001044 private void arrangeChildren(ArrayList<View> list) {
1045 int[] vacant = new int[2];
1046 if (list == null) {
1047 list = getItemsInReadingOrder();
1048 }
1049 mContent.removeAllViews();
1050
1051 for (int i = 0; i < list.size(); i++) {
1052 View v = list.get(i);
1053 mContent.getVacantCell(vacant, 1, 1);
1054 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) v.getLayoutParams();
1055 lp.cellX = vacant[0];
1056 lp.cellY = vacant[1];
1057 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohen2792a332011-09-26 21:09:47 -07001058 if (info.cellX != vacant[0] || info.cellY != vacant[1]) {
1059 info.cellX = vacant[0];
1060 info.cellY = vacant[1];
1061 LauncherModel.addOrMoveItemInDatabase(mLauncher, info, mInfo.id, 0,
1062 info.cellX, info.cellY);
1063 }
Adam Cohen2801caf2011-05-13 20:57:39 -07001064 boolean insert = false;
1065 mContent.addViewToCellLayout(v, insert ? 0 : -1, (int)info.id, lp, true);
1066 }
Adam Cohen7c693212011-05-18 15:26:57 -07001067 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -07001068 }
1069
Adam Cohena9cf38f2011-05-02 15:36:58 -07001070 public int getItemCount() {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001071 return mContent.getShortcutsAndWidgets().getChildCount();
Adam Cohena9cf38f2011-05-02 15:36:58 -07001072 }
1073
1074 public View getItemAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001075 return mContent.getShortcutsAndWidgets().getChildAt(index);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001076 }
1077
Adam Cohen2801caf2011-05-13 20:57:39 -07001078 private void onCloseComplete() {
Adam Cohen05e0f402011-08-01 12:12:49 -07001079 DragLayer parent = (DragLayer) getParent();
Michael Jurka5649c282012-06-18 10:33:21 -07001080 if (parent != null) {
1081 parent.removeView(this);
1082 }
Adam Cohen4554ee12011-08-03 16:13:21 -07001083 mDragController.removeDropTarget((DropTarget) this);
Adam Cohen05e0f402011-08-01 12:12:49 -07001084 clearFocus();
Adam Cohenac56cff2011-09-28 20:45:37 -07001085 mFolderIcon.requestFocus();
Adam Cohen05e0f402011-08-01 12:12:49 -07001086
Adam Cohen2801caf2011-05-13 20:57:39 -07001087 if (mRearrangeOnClose) {
1088 setupContentForNumItems(getItemCount());
1089 mRearrangeOnClose = false;
1090 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001091 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001092 if (!mDragInProgress && !mSuppressFolderDeletion) {
1093 replaceFolderWithFinalItem();
1094 } else if (mDragInProgress) {
1095 mDeleteFolderOnDropCompleted = true;
1096 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001097 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001098 mSuppressFolderDeletion = false;
Adam Cohenafb01ee2011-06-23 15:38:03 -07001099 }
1100
1101 private void replaceFolderWithFinalItem() {
Adam Cohenafb01ee2011-06-23 15:38:03 -07001102 // Add the last remaining child to the workspace in place of the folder
Adam Cohenfb91f302012-06-11 15:45:18 -07001103 Runnable onCompleteRunnable = new Runnable() {
1104 @Override
1105 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -07001106 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screenId);
Adam Cohenafb01ee2011-06-23 15:38:03 -07001107
Winson Chung33231f52013-12-09 16:57:45 -08001108 View child = null;
Adam Cohenfb91f302012-06-11 15:45:18 -07001109 // Move the item from the folder to the workspace, in the position of the folder
1110 if (getItemCount() == 1) {
1111 ShortcutInfo finalItem = mInfo.contents.get(0);
Adam Cohenc5e63f32012-07-12 16:16:57 -07001112 child = mLauncher.createShortcut(R.layout.application, cellLayout,
Adam Cohenfb91f302012-06-11 15:45:18 -07001113 finalItem);
1114 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -07001115 mInfo.screenId, mInfo.cellX, mInfo.cellY);
Adam Cohenfb91f302012-06-11 15:45:18 -07001116 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001117 if (getItemCount() <= 1) {
1118 // Remove the folder
1119 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
Dan Sandler0eb687f2014-01-10 13:24:55 -05001120 if (cellLayout != null) {
1121 // b/12446428 -- sometimes the cell layout has already gone away?
1122 cellLayout.removeView(mFolderIcon);
1123 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001124 if (mFolderIcon instanceof DropTarget) {
1125 mDragController.removeDropTarget((DropTarget) mFolderIcon);
1126 }
1127 mLauncher.removeFolder(mInfo);
1128 }
Adam Cohenc5e63f32012-07-12 16:16:57 -07001129 // We add the child after removing the folder to prevent both from existing at
Winson Chung0e6a7132013-08-23 12:55:10 -07001130 // the same time in the CellLayout. We need to add the new item with addInScreenFromBind()
1131 // to ensure that hotseat items are placed correctly.
Adam Cohenc5e63f32012-07-12 16:16:57 -07001132 if (child != null) {
Winson Chung0e6a7132013-08-23 12:55:10 -07001133 mLauncher.getWorkspace().addInScreenFromBind(child, mInfo.container, mInfo.screenId,
Adam Cohenc5e63f32012-07-12 16:16:57 -07001134 mInfo.cellX, mInfo.cellY, mInfo.spanX, mInfo.spanY);
1135 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001136 }
1137 };
1138 View finalChild = getItemAt(0);
1139 if (finalChild != null) {
1140 mFolderIcon.performDestroyAnimation(finalChild, onCompleteRunnable);
Winson Chung33231f52013-12-09 16:57:45 -08001141 } else {
1142 onCompleteRunnable.run();
Adam Cohenafb01ee2011-06-23 15:38:03 -07001143 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001144 mDestroyed = true;
1145 }
1146
1147 boolean isDestroyed() {
1148 return mDestroyed;
Adam Cohen2801caf2011-05-13 20:57:39 -07001149 }
1150
Adam Cohenac56cff2011-09-28 20:45:37 -07001151 // This method keeps track of the last item in the folder for the purposes
1152 // of keyboard focus
1153 private void updateTextViewFocus() {
1154 View lastChild = getItemAt(getItemCount() - 1);
1155 getItemAt(getItemCount() - 1);
1156 if (lastChild != null) {
1157 mFolderName.setNextFocusDownId(lastChild.getId());
1158 mFolderName.setNextFocusRightId(lastChild.getId());
1159 mFolderName.setNextFocusLeftId(lastChild.getId());
1160 mFolderName.setNextFocusUpId(lastChild.getId());
1161 }
1162 }
1163
Adam Cohenbfbfd262011-06-13 16:55:12 -07001164 public void onDrop(DragObject d) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001165 Runnable cleanUpRunnable = null;
1166
Adam Cohen689ff162014-05-08 17:27:56 -07001167 // If we are coming from All Apps space, we defer removing the extra empty screen
1168 // until the folder closes
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001169 if (d.dragSource != mLauncher.getWorkspace() && !(d.dragSource instanceof Folder)) {
1170 cleanUpRunnable = new Runnable() {
1171 @Override
1172 public void run() {
Adam Cohen689ff162014-05-08 17:27:56 -07001173 mLauncher.exitSpringLoadedDragModeDelayed(true,
1174 Launcher.EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
1175 null);
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001176 }
1177 };
Adam Cohenbfbfd262011-06-13 16:55:12 -07001178 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001179
1180 View currentDragView;
1181 ShortcutInfo si = mCurrentDragInfo;
1182 if (mIsExternalDrag) {
1183 si.cellX = mEmptyCell[0];
1184 si.cellY = mEmptyCell[1];
1185 currentDragView = createAndAddShortcut(si);
1186 } else {
1187 currentDragView = mCurrentDragView;
1188 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) currentDragView.getLayoutParams();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001189 si.cellX = lp.cellX = mEmptyCell[0];
1190 si.cellX = lp.cellY = mEmptyCell[1];
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001191 mContent.addViewToCellLayout(currentDragView, -1, (int) si.id, lp, true);
Adam Cohenbfbfd262011-06-13 16:55:12 -07001192 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001193
1194 if (d.dragView.hasDrawn()) {
1195
1196 // Temporarily reset the scale such that the animation target gets calculated correctly.
1197 float scaleX = getScaleX();
1198 float scaleY = getScaleY();
1199 setScaleX(1.0f);
1200 setScaleY(1.0f);
1201 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, currentDragView,
1202 cleanUpRunnable, null);
1203 setScaleX(scaleX);
1204 setScaleY(scaleY);
1205 } else {
1206 d.deferDragViewCleanupPostAnimation = false;
1207 currentDragView.setVisibility(VISIBLE);
1208 }
1209 mItemsInvalidated = true;
1210 setupContentDimensions(getItemCount());
1211
1212 // Actually move the item in the database if it was an external drag.
1213 if (mIsExternalDrag) {
1214 LauncherModel.addOrMoveItemInDatabase(
1215 mLauncher, si, mInfo.id, 0, si.cellX, si.cellY);
1216
1217 // We only need to update the locations if it doesn't get handled in #onDropCompleted.
1218 if (d.dragSource != this) {
1219 updateItemLocationsInDatabaseBatch();
1220 }
1221 mIsExternalDrag = false;
1222 }
1223
1224 // Temporarily suppress the listener, as we did all the work already here.
1225 mSuppressOnAdd = true;
1226 mInfo.add(si);
1227 mSuppressOnAdd = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001228 }
1229
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001230 // This is used so the item doesn't immediately appear in the folder when added. In one case
1231 // we need to create the illusion that the item isn't added back to the folder yet, to
1232 // to correspond to the animation of the icon back into the folder. This is
1233 public void hideItem(ShortcutInfo info) {
1234 View v = getViewForInfo(info);
1235 v.setVisibility(INVISIBLE);
1236 }
1237 public void showItem(ShortcutInfo info) {
1238 View v = getViewForInfo(info);
1239 v.setVisibility(VISIBLE);
1240 }
1241
Adam Cohenbfbfd262011-06-13 16:55:12 -07001242 public void onAdd(ShortcutInfo item) {
1243 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001244 // If the item was dropped onto this open folder, we have done the work associated
1245 // with adding the item to the folder, as indicated by mSuppressOnAdd being set
Adam Cohenbfbfd262011-06-13 16:55:12 -07001246 if (mSuppressOnAdd) return;
1247 if (!findAndSetEmptyCells(item)) {
1248 // The current layout is full, can we expand it?
1249 setupContentForNumItems(getItemCount() + 1);
1250 findAndSetEmptyCells(item);
1251 }
1252 createAndAddShortcut(item);
1253 LauncherModel.addOrMoveItemInDatabase(
1254 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
1255 }
1256
Adam Cohena9cf38f2011-05-02 15:36:58 -07001257 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -07001258 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001259 // If this item is being dragged from this open folder, we have already handled
1260 // the work associated with removing the item, so we don't have to do anything here.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001261 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001262 View v = getViewForInfo(item);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001263 mContent.removeView(v);
Adam Cohen2801caf2011-05-13 20:57:39 -07001264 if (mState == STATE_ANIMATING) {
1265 mRearrangeOnClose = true;
1266 } else {
1267 setupContentForNumItems(getItemCount());
1268 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001269 if (getItemCount() <= 1) {
1270 replaceFolderWithFinalItem();
1271 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001272 }
Adam Cohen7c693212011-05-18 15:26:57 -07001273
Adam Cohendf1e4e82011-06-24 15:57:39 -07001274 private View getViewForInfo(ShortcutInfo item) {
1275 for (int j = 0; j < mContent.getCountY(); j++) {
1276 for (int i = 0; i < mContent.getCountX(); i++) {
1277 View v = mContent.getChildAt(i, j);
1278 if (v.getTag() == item) {
1279 return v;
1280 }
1281 }
1282 }
1283 return null;
1284 }
1285
Adam Cohen76078c42011-06-09 15:06:52 -07001286 public void onItemsChanged() {
Adam Cohenac56cff2011-09-28 20:45:37 -07001287 updateTextViewFocus();
Adam Cohen76078c42011-06-09 15:06:52 -07001288 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001289
Adam Cohen76fc0852011-06-17 13:26:23 -07001290 public void onTitleChanged(CharSequence title) {
1291 }
Adam Cohen76078c42011-06-09 15:06:52 -07001292
Adam Cohen7c693212011-05-18 15:26:57 -07001293 public ArrayList<View> getItemsInReadingOrder() {
1294 if (mItemsInvalidated) {
1295 mItemsInReadingOrder.clear();
1296 for (int j = 0; j < mContent.getCountY(); j++) {
1297 for (int i = 0; i < mContent.getCountX(); i++) {
1298 View v = mContent.getChildAt(i, j);
1299 if (v != null) {
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001300 mItemsInReadingOrder.add(v);
Adam Cohen7c693212011-05-18 15:26:57 -07001301 }
1302 }
1303 }
1304 mItemsInvalidated = false;
1305 }
1306 return mItemsInReadingOrder;
1307 }
Adam Cohen8dfcba42011-07-07 16:38:18 -07001308
1309 public void getLocationInDragLayer(int[] loc) {
1310 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
1311 }
Adam Cohenea0818d2011-09-30 20:06:58 -07001312
1313 public void onFocusChange(View v, boolean hasFocus) {
1314 if (v == mFolderName && hasFocus) {
1315 startEditingFolderName();
1316 }
1317 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001318
1319 @Override
1320 public void getHitRectRelativeToDragLayer(Rect outRect) {
1321 getHitRect(outRect);
1322 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001323}