blob: fb226e57d84baefc98c7ce02488de42179a88aad [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
Adam Cohenc508b2d2011-06-28 14:41:44 -0700394 // If our folder has too many items we prune them from the list. This is an issue
395 // 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) {
Adam Cohen477828c2013-09-20 12:05:49 -0700569 final BubbleTextView textView =
570 (BubbleTextView) mInflater.inflate(R.layout.application, this, false);
Winson Chung54000492013-10-14 16:29:29 -0700571 textView.setCompoundDrawables(null,
Winson Chung0dbd7342013-10-13 22:46:20 -0700572 Utilities.createIconDrawable(item.getIcon(mIconCache)), null, null);
Adam Cohendf2cc412011-04-27 16:56:57 -0700573 textView.setText(item.title);
574 textView.setTag(item);
Adam Cohen477828c2013-09-20 12:05:49 -0700575 textView.setTextColor(getResources().getColor(R.color.folder_items_text_color));
576 textView.setShadowsEnabled(false);
Adam Cohen7397e622013-10-24 12:11:34 -0700577 textView.setGlowColor(getResources().getColor(R.color.folder_items_glow_color));
Adam Cohendf2cc412011-04-27 16:56:57 -0700578
579 textView.setOnClickListener(this);
580 textView.setOnLongClickListener(this);
581
Adam Cohenc508b2d2011-06-28 14:41:44 -0700582 // We need to check here to verify that the given item's location isn't already occupied
Adam Cohen3bf84d32012-05-07 20:17:14 -0700583 // by another item.
Adam Cohen0057bbc2011-08-12 18:30:51 -0700584 if (mContent.getChildAt(item.cellX, item.cellY) != null || item.cellX < 0 || item.cellY < 0
585 || item.cellX >= mContent.getCountX() || item.cellY >= mContent.getCountY()) {
Adam Cohen3bf84d32012-05-07 20:17:14 -0700586 // This shouldn't happen, log it.
587 Log.e(TAG, "Folder order not properly persisted during bind");
Adam Cohenc508b2d2011-06-28 14:41:44 -0700588 if (!findAndSetEmptyCells(item)) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800589 return null;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700590 }
591 }
592
Adam Cohendf2cc412011-04-27 16:56:57 -0700593 CellLayout.LayoutParams lp =
594 new CellLayout.LayoutParams(item.cellX, item.cellY, item.spanX, item.spanY);
595 boolean insert = false;
Adam Cohenac56cff2011-09-28 20:45:37 -0700596 textView.setOnKeyListener(new FolderKeyEventListener());
Adam Cohendf2cc412011-04-27 16:56:57 -0700597 mContent.addViewToCellLayout(textView, insert ? 0 : -1, (int)item.id, lp, true);
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800598 return textView;
Adam Cohendf2cc412011-04-27 16:56:57 -0700599 }
600
Adam Cohencb3382b2011-05-24 14:07:08 -0700601 public void onDragEnter(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700602 mPreviousTargetCell[0] = -1;
603 mPreviousTargetCell[1] = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700604 mOnExitAlarm.cancelAlarm();
605 }
606
607 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
608 public void onAlarm(Alarm alarm) {
609 realTimeReorder(mEmptyCell, mTargetCell);
610 }
611 };
612
613 boolean readingOrderGreaterThan(int[] v1, int[] v2) {
614 if (v1[1] > v2[1] || (v1[1] == v2[1] && v1[0] > v2[0])) {
615 return true;
616 } else {
617 return false;
618 }
619 }
620
621 private void realTimeReorder(int[] empty, int[] target) {
622 boolean wrap;
623 int startX;
624 int endX;
625 int startY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700626 int delay = 0;
627 float delayAmount = 30;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700628 if (readingOrderGreaterThan(target, empty)) {
629 wrap = empty[0] >= mContent.getCountX() - 1;
630 startY = wrap ? empty[1] + 1 : empty[1];
631 for (int y = startY; y <= target[1]; y++) {
632 startX = y == empty[1] ? empty[0] + 1 : 0;
633 endX = y < target[1] ? mContent.getCountX() - 1 : target[0];
634 for (int x = startX; x <= endX; x++) {
635 View v = mContent.getChildAt(x,y);
636 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen482ed822012-03-02 14:15:13 -0800637 REORDER_ANIMATION_DURATION, delay, true, true)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700638 empty[0] = x;
639 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700640 delay += delayAmount;
641 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700642 }
643 }
644 }
645 } else {
646 wrap = empty[0] == 0;
647 startY = wrap ? empty[1] - 1 : empty[1];
648 for (int y = startY; y >= target[1]; y--) {
649 startX = y == empty[1] ? empty[0] - 1 : mContent.getCountX() - 1;
650 endX = y > target[1] ? 0 : target[0];
651 for (int x = startX; x >= endX; x--) {
652 View v = mContent.getChildAt(x,y);
653 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen482ed822012-03-02 14:15:13 -0800654 REORDER_ANIMATION_DURATION, delay, true, true)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700655 empty[0] = x;
656 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700657 delay += delayAmount;
658 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700659 }
660 }
661 }
662 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700663 }
664
Adam Cohen2374abf2013-04-16 14:56:57 -0700665 public boolean isLayoutRtl() {
666 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
667 }
668
Adam Cohencb3382b2011-05-24 14:07:08 -0700669 public void onDragOver(DragObject d) {
Alan Viverette4cda5b72013-08-28 17:53:41 -0700670 final DragView dragView = d.dragView;
671 final int scrollOffset = mScrollView.getScrollY();
672 final float[] r = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, dragView, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700673 r[0] -= getPaddingLeft();
674 r[1] -= getPaddingTop();
675
Alan Viverette4cda5b72013-08-28 17:53:41 -0700676 final long downTime = SystemClock.uptimeMillis();
677 final MotionEvent translatedEv = MotionEvent.obtain(
678 downTime, downTime, MotionEvent.ACTION_MOVE, d.x, d.y, 0);
679
680 if (!mAutoScrollHelper.isEnabled()) {
681 mAutoScrollHelper.setEnabled(true);
682 }
683
684 final boolean handled = mAutoScrollHelper.onTouch(this, translatedEv);
685 translatedEv.recycle();
686
687 if (handled) {
688 mReorderAlarm.cancelAlarm();
689 } else {
690 mTargetCell = mContent.findNearestArea(
691 (int) r[0], (int) r[1] + scrollOffset, 1, 1, mTargetCell);
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700692 if (isLayoutRtl()) {
693 mTargetCell[0] = mContent.getCountX() - mTargetCell[0] - 1;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700694 }
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700695 if (mTargetCell[0] != mPreviousTargetCell[0]
696 || mTargetCell[1] != mPreviousTargetCell[1]) {
697 mReorderAlarm.cancelAlarm();
698 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
699 mReorderAlarm.setAlarm(REORDER_DELAY);
700 mPreviousTargetCell[0] = mTargetCell[0];
701 mPreviousTargetCell[1] = mTargetCell[1];
702 mDragMode = DRAG_MODE_REORDER;
703 } else {
704 mDragMode = DRAG_MODE_NONE;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700705 }
Adam Cohenbfbfd262011-06-13 16:55:12 -0700706 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700707 }
708
Adam Cohenbfbfd262011-06-13 16:55:12 -0700709 // This is used to compute the visual center of the dragView. The idea is that
710 // the visual center represents the user's interpretation of where the item is, and hence
711 // is the appropriate point to use when determining drop location.
712 private float[] getDragViewVisualCenter(int x, int y, int xOffset, int yOffset,
713 DragView dragView, float[] recycle) {
714 float res[];
715 if (recycle == null) {
716 res = new float[2];
717 } else {
718 res = recycle;
719 }
720
721 // These represent the visual top and left of drag view if a dragRect was provided.
722 // If a dragRect was not provided, then they correspond to the actual view left and
723 // top, as the dragRect is in that case taken to be the entire dragView.
724 // R.dimen.dragViewOffsetY.
725 int left = x - xOffset;
726 int top = y - yOffset;
727
728 // In order to find the visual center, we shift by half the dragRect
729 res[0] = left + dragView.getDragRegion().width() / 2;
730 res[1] = top + dragView.getDragRegion().height() / 2;
731
732 return res;
733 }
734
735 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
736 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700737 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700738 }
739 };
740
Adam Cohen95bb8002011-07-03 23:40:28 -0700741 public void completeDragExit() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700742 mLauncher.closeFolder();
743 mCurrentDragInfo = null;
744 mCurrentDragView = null;
745 mSuppressOnAdd = false;
746 mRearrangeOnClose = true;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800747 mIsExternalDrag = false;
Adam Cohen3e8f8112011-07-02 18:03:00 -0700748 }
749
Adam Cohencb3382b2011-05-24 14:07:08 -0700750 public void onDragExit(DragObject d) {
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700751 // Exiting folder; stop the auto scroller.
Alan Viverette4cda5b72013-08-28 17:53:41 -0700752 mAutoScrollHelper.setEnabled(false);
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700753 // We only close the folder if this is a true drag exit, ie. not because
754 // a drop has occurred above the folder.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700755 if (!d.dragComplete) {
756 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
757 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
758 }
759 mReorderAlarm.cancelAlarm();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700760 mDragMode = DRAG_MODE_NONE;
Adam Cohen2801caf2011-05-13 20:57:39 -0700761 }
762
Michael Jurka1e2f4652013-07-08 18:03:46 -0700763 public void onDropCompleted(final View target, final DragObject d,
764 final boolean isFlingToDelete, final boolean success) {
765 if (mDeferDropAfterUninstall) {
Michael Jurkaf3007582013-08-21 14:33:57 +0200766 Log.d(TAG, "Deferred handling drop because waiting for uninstall.");
Michael Jurka1e2f4652013-07-08 18:03:46 -0700767 mDeferredAction = new Runnable() {
768 public void run() {
769 onDropCompleted(target, d, isFlingToDelete, success);
770 mDeferredAction = null;
771 }
772 };
773 return;
774 }
775
776 boolean beingCalledAfterUninstall = mDeferredAction != null;
777 boolean successfulDrop =
778 success && (!beingCalledAfterUninstall || mUninstallSuccessful);
Winson Chung5f8afe62013-08-12 16:19:28 -0700779
Michael Jurka1e2f4652013-07-08 18:03:46 -0700780 if (successfulDrop) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800781 if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon && target != this) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700782 replaceFolderWithFinalItem();
783 }
784 } else {
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700785 setupContentForNumItems(getItemCount());
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700786 // The drag failed, we need to return the item to the folder
787 mFolderIcon.onDrop(d);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700788 }
789
790 if (target != this) {
791 if (mOnExitAlarm.alarmPending()) {
792 mOnExitAlarm.cancelAlarm();
Michael Jurka54554252013-08-01 12:52:23 +0200793 if (!successfulDrop) {
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700794 mSuppressFolderDeletion = true;
795 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700796 completeDragExit();
797 }
Adam Cohen9c58d822013-12-13 17:18:10 -0800798 }
799
800 // This is kind of hacky, but in general, dropping on the workspace handles removing
801 // the extra screen, but dropping elsewhere (back to self, or onto delete) doesn't.
802 if (target != mLauncher.getWorkspace()) {
Adam Cohenf0f2ffa2013-11-12 11:44:58 +0000803 mLauncher.getWorkspace().removeExtraEmptyScreen(true, null);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700804 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700805
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700806 mDeleteFolderOnDropCompleted = false;
807 mDragInProgress = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700808 mItemAddedBackToSelfViaIcon = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700809 mCurrentDragInfo = null;
810 mCurrentDragView = null;
811 mSuppressOnAdd = false;
Adam Cohen4045eb72011-10-06 11:44:26 -0700812
813 // Reordering may have occured, and we need to save the new item locations. We do this once
814 // at the end to prevent unnecessary database operations.
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700815 updateItemLocationsInDatabaseBatch();
Adam Cohen4045eb72011-10-06 11:44:26 -0700816 }
817
Michael Jurka1e2f4652013-07-08 18:03:46 -0700818 public void deferCompleteDropAfterUninstallActivity() {
819 mDeferDropAfterUninstall = true;
820 }
821
822 public void onUninstallActivityReturned(boolean success) {
823 mDeferDropAfterUninstall = false;
824 mUninstallSuccessful = success;
825 if (mDeferredAction != null) {
826 mDeferredAction.run();
827 }
828 }
829
Winson Chunga48487a2012-03-20 16:19:37 -0700830 @Override
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800831 public float getIntrinsicIconScaleFactor() {
832 return 1f;
833 }
834
835 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800836 public boolean supportsFlingToDelete() {
837 return true;
838 }
839
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000840 @Override
841 public boolean supportsAppInfoDropTarget() {
842 return false;
843 }
844
845 @Override
846 public boolean supportsDeleteDropTarget() {
847 return true;
848 }
849
Winson Chunga48487a2012-03-20 16:19:37 -0700850 public void onFlingToDelete(DragObject d, int x, int y, PointF vec) {
851 // Do nothing
852 }
853
854 @Override
855 public void onFlingToDeleteCompleted() {
856 // Do nothing
857 }
858
Adam Cohen4045eb72011-10-06 11:44:26 -0700859 private void updateItemLocationsInDatabase() {
860 ArrayList<View> list = getItemsInReadingOrder();
861 for (int i = 0; i < list.size(); i++) {
862 View v = list.get(i);
863 ItemInfo info = (ItemInfo) v.getTag();
864 LauncherModel.moveItemInDatabase(mLauncher, info, mInfo.id, 0,
Winson Chung33231f52013-12-09 16:57:45 -0800865 info.cellX, info.cellY);
Adam Cohen4045eb72011-10-06 11:44:26 -0700866 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700867 }
Adam Cohen228da5a2011-07-27 22:23:47 -0700868
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700869 private void updateItemLocationsInDatabaseBatch() {
870 ArrayList<View> list = getItemsInReadingOrder();
871 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
872 for (int i = 0; i < list.size(); i++) {
873 View v = list.get(i);
874 ItemInfo info = (ItemInfo) v.getTag();
875 items.add(info);
876 }
877
878 LauncherModel.moveItemsInDatabase(mLauncher, items, mInfo.id, 0);
879 }
880
Adam Cohene25af792013-06-06 23:08:25 -0700881 public void addItemLocationsInDatabase() {
882 ArrayList<View> list = getItemsInReadingOrder();
883 for (int i = 0; i < list.size(); i++) {
884 View v = list.get(i);
885 ItemInfo info = (ItemInfo) v.getTag();
886 LauncherModel.addItemToDatabase(mLauncher, info, mInfo.id, 0,
887 info.cellX, info.cellY, false);
888 }
889 }
890
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700891 public void notifyDrop() {
892 if (mDragInProgress) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700893 mItemAddedBackToSelfViaIcon = true;
Adam Cohen76078c42011-06-09 15:06:52 -0700894 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700895 }
896
897 public boolean isDropEnabled() {
898 return true;
899 }
900
Adam Cohen4045eb72011-10-06 11:44:26 -0700901 private void setupContentDimensions(int count) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700902 ArrayList<View> list = getItemsInReadingOrder();
903
904 int countX = mContent.getCountX();
905 int countY = mContent.getCountY();
Adam Cohen7c693212011-05-18 15:26:57 -0700906 boolean done = false;
Adam Cohen2801caf2011-05-13 20:57:39 -0700907
Adam Cohen7c693212011-05-18 15:26:57 -0700908 while (!done) {
909 int oldCountX = countX;
910 int oldCountY = countY;
911 if (countX * countY < count) {
912 // Current grid is too small, expand it
Adam Cohen78dc83e2011-11-15 17:10:00 -0800913 if ((countX <= countY || countY == mMaxCountY) && countX < mMaxCountX) {
Adam Cohen7c693212011-05-18 15:26:57 -0700914 countX++;
915 } else if (countY < mMaxCountY) {
916 countY++;
917 }
918 if (countY == 0) countY++;
919 } else if ((countY - 1) * countX >= count && countY >= countX) {
920 countY = Math.max(0, countY - 1);
921 } else if ((countX - 1) * countY >= count) {
922 countX = Math.max(0, countX - 1);
Adam Cohen2801caf2011-05-13 20:57:39 -0700923 }
Adam Cohen7c693212011-05-18 15:26:57 -0700924 done = countX == oldCountX && countY == oldCountY;
Adam Cohen2801caf2011-05-13 20:57:39 -0700925 }
Adam Cohen7c693212011-05-18 15:26:57 -0700926 mContent.setGridSize(countX, countY);
Adam Cohen2801caf2011-05-13 20:57:39 -0700927 arrangeChildren(list);
928 }
929
930 public boolean isFull() {
Adam Cohen78dc83e2011-11-15 17:10:00 -0800931 return getItemCount() >= mMaxNumItems;
Adam Cohen2801caf2011-05-13 20:57:39 -0700932 }
933
934 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700935 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700936
Winson Chung892c74d2013-08-22 16:15:50 -0700937 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700938 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700939 int height = getFolderHeight();
Adam Cohen2801caf2011-05-13 20:57:39 -0700940
Adam Cohen307fe232012-08-16 17:55:58 -0700941 float scale = parent.getDescendantRectRelativeToSelf(mFolderIcon, mTempRect);
Adam Cohen8e776a62011-06-28 18:10:06 -0700942
Winson Chungaf40f202013-09-18 18:26:31 -0700943 LauncherAppState app = LauncherAppState.getInstance();
944 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
945
Adam Cohen307fe232012-08-16 17:55:58 -0700946 int centerX = (int) (mTempRect.left + mTempRect.width() * scale / 2);
947 int centerY = (int) (mTempRect.top + mTempRect.height() * scale / 2);
Adam Cohen2801caf2011-05-13 20:57:39 -0700948 int centeredLeft = centerX - width / 2;
949 int centeredTop = centerY - height / 2;
Winson Chung3057b1c2013-10-10 17:35:35 -0700950 int currentPage = mLauncher.getWorkspace().getNextPage();
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800951 // In case the workspace is scrolling, we need to use the final scroll to compute
952 // the folders bounds.
953 mLauncher.getWorkspace().setFinalScrollForPageChange(currentPage);
Adam Cohen35e7e642011-07-17 14:47:18 -0700954 // We first fetch the currently visible CellLayoutChildren
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800955 CellLayout currentLayout = (CellLayout) mLauncher.getWorkspace().getChildAt(currentPage);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700956 ShortcutAndWidgetContainer boundingLayout = currentLayout.getShortcutsAndWidgets();
Adam Cohen35e7e642011-07-17 14:47:18 -0700957 Rect bounds = new Rect();
958 parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800959 // We reset the workspaces scroll
960 mLauncher.getWorkspace().resetFinalScrollForPageChange(currentPage);
Adam Cohen2801caf2011-05-13 20:57:39 -0700961
Adam Cohen35e7e642011-07-17 14:47:18 -0700962 // We need to bound the folder to the currently visible CellLayoutChildren
963 int left = Math.min(Math.max(bounds.left, centeredLeft),
964 bounds.left + bounds.width() - width);
965 int top = Math.min(Math.max(bounds.top, centeredTop),
966 bounds.top + bounds.height() - height);
Winson Chungaf40f202013-09-18 18:26:31 -0700967 if (grid.isPhone() && (grid.availableWidthPx - width) < grid.iconSizePx) {
968 // Center the folder if it is full (on phones only)
969 left = (grid.availableWidthPx - width) / 2;
970 } else if (width >= bounds.width()) {
971 // If the folder doesn't fit within the bounds, center it about the desired bounds
Adam Cohen35e7e642011-07-17 14:47:18 -0700972 left = bounds.left + (bounds.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700973 }
Adam Cohen35e7e642011-07-17 14:47:18 -0700974 if (height >= bounds.height()) {
975 top = bounds.top + (bounds.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700976 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700977
978 int folderPivotX = width / 2 + (centeredLeft - left);
979 int folderPivotY = height / 2 + (centeredTop - top);
980 setPivotX(folderPivotX);
981 setPivotY(folderPivotY);
Adam Cohen268c4752012-06-06 17:47:33 -0700982 mFolderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700983 (1.0f * folderPivotX / width));
Adam Cohen268c4752012-06-06 17:47:33 -0700984 mFolderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700985 (1.0f * folderPivotY / height));
Adam Cohen3bf84d32012-05-07 20:17:14 -0700986
Adam Cohen662b5982011-12-13 17:45:21 -0800987 lp.width = width;
988 lp.height = height;
989 lp.x = left;
990 lp.y = top;
Adam Cohen2801caf2011-05-13 20:57:39 -0700991 }
992
Adam Cohen268c4752012-06-06 17:47:33 -0700993 float getPivotXForIconAnimation() {
994 return mFolderIconPivotX;
995 }
996 float getPivotYForIconAnimation() {
997 return mFolderIconPivotY;
998 }
999
Adam Cohen2801caf2011-05-13 20:57:39 -07001000 private void setupContentForNumItems(int count) {
Adam Cohen4045eb72011-10-06 11:44:26 -07001001 setupContentDimensions(count);
Adam Cohen2801caf2011-05-13 20:57:39 -07001002
Adam Cohen8e776a62011-06-28 18:10:06 -07001003 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -07001004 if (lp == null) {
Adam Cohen8e776a62011-06-28 18:10:06 -07001005 lp = new DragLayer.LayoutParams(0, 0);
1006 lp.customPosition = true;
Adam Cohen2801caf2011-05-13 20:57:39 -07001007 setLayoutParams(lp);
1008 }
1009 centerAboutIcon();
1010 }
1011
Winson Chung892c74d2013-08-22 16:15:50 -07001012 private int getContentAreaHeight() {
1013 LauncherAppState app = LauncherAppState.getInstance();
1014 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1015 Rect workspacePadding = grid.getWorkspacePadding(grid.isLandscape ?
1016 CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
1017 int maxContentAreaHeight = grid.availableHeightPx -
Winson Chung892c74d2013-08-22 16:15:50 -07001018 workspacePadding.top - workspacePadding.bottom -
Winson Chung892c74d2013-08-22 16:15:50 -07001019 mFolderNameHeight;
Adam Cohen1960ea42013-11-12 11:33:14 +00001020 int height = Math.min(maxContentAreaHeight,
Winson Chung892c74d2013-08-22 16:15:50 -07001021 mContent.getDesiredHeight());
Adam Cohen1960ea42013-11-12 11:33:14 +00001022 return Math.max(height, MIN_CONTENT_DIMEN);
1023 }
1024
1025 private int getContentAreaWidth() {
1026 return Math.max(mContent.getDesiredWidth(), MIN_CONTENT_DIMEN);
Winson Chung892c74d2013-08-22 16:15:50 -07001027 }
1028
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001029 private int getFolderHeight() {
Winson Chung892c74d2013-08-22 16:15:50 -07001030 int height = getPaddingTop() + getPaddingBottom()
1031 + getContentAreaHeight() + mFolderNameHeight;
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001032 return height;
1033 }
Adam Cohen234c4cd2011-07-17 21:03:04 -07001034
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001035 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001036 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
1037 int height = getFolderHeight();
Adam Cohen1960ea42013-11-12 11:33:14 +00001038 int contentAreaWidthSpec = MeasureSpec.makeMeasureSpec(getContentAreaWidth(),
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001039 MeasureSpec.EXACTLY);
Winson Chung892c74d2013-08-22 16:15:50 -07001040 int contentAreaHeightSpec = MeasureSpec.makeMeasureSpec(getContentAreaHeight(),
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001041 MeasureSpec.EXACTLY);
Adam Cohen1960ea42013-11-12 11:33:14 +00001042
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08001043 if (LauncherAppState.isDisableAllApps()) {
Nilesh Agrawal5f7099a2014-01-02 15:54:57 -08001044 // Don't cap the height of the content to allow scrolling.
1045 mContent.setFixedSize(getContentAreaWidth(), mContent.getDesiredHeight());
1046 } else {
1047 mContent.setFixedSize(getContentAreaWidth(), getContentAreaHeight());
1048 }
1049
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001050 mScrollView.measure(contentAreaWidthSpec, contentAreaHeightSpec);
1051 mFolderName.measure(contentAreaWidthSpec,
Adam Cohen234c4cd2011-07-17 21:03:04 -07001052 MeasureSpec.makeMeasureSpec(mFolderNameHeight, MeasureSpec.EXACTLY));
1053 setMeasuredDimension(width, height);
1054 }
1055
Adam Cohen2801caf2011-05-13 20:57:39 -07001056 private void arrangeChildren(ArrayList<View> list) {
1057 int[] vacant = new int[2];
1058 if (list == null) {
1059 list = getItemsInReadingOrder();
1060 }
1061 mContent.removeAllViews();
1062
1063 for (int i = 0; i < list.size(); i++) {
1064 View v = list.get(i);
1065 mContent.getVacantCell(vacant, 1, 1);
1066 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) v.getLayoutParams();
1067 lp.cellX = vacant[0];
1068 lp.cellY = vacant[1];
1069 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohen2792a332011-09-26 21:09:47 -07001070 if (info.cellX != vacant[0] || info.cellY != vacant[1]) {
1071 info.cellX = vacant[0];
1072 info.cellY = vacant[1];
1073 LauncherModel.addOrMoveItemInDatabase(mLauncher, info, mInfo.id, 0,
1074 info.cellX, info.cellY);
1075 }
Adam Cohen2801caf2011-05-13 20:57:39 -07001076 boolean insert = false;
1077 mContent.addViewToCellLayout(v, insert ? 0 : -1, (int)info.id, lp, true);
1078 }
Adam Cohen7c693212011-05-18 15:26:57 -07001079 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -07001080 }
1081
Adam Cohena9cf38f2011-05-02 15:36:58 -07001082 public int getItemCount() {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001083 return mContent.getShortcutsAndWidgets().getChildCount();
Adam Cohena9cf38f2011-05-02 15:36:58 -07001084 }
1085
1086 public View getItemAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001087 return mContent.getShortcutsAndWidgets().getChildAt(index);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001088 }
1089
Adam Cohen2801caf2011-05-13 20:57:39 -07001090 private void onCloseComplete() {
Adam Cohen05e0f402011-08-01 12:12:49 -07001091 DragLayer parent = (DragLayer) getParent();
Michael Jurka5649c282012-06-18 10:33:21 -07001092 if (parent != null) {
1093 parent.removeView(this);
1094 }
Adam Cohen4554ee12011-08-03 16:13:21 -07001095 mDragController.removeDropTarget((DropTarget) this);
Adam Cohen05e0f402011-08-01 12:12:49 -07001096 clearFocus();
Adam Cohenac56cff2011-09-28 20:45:37 -07001097 mFolderIcon.requestFocus();
Adam Cohen05e0f402011-08-01 12:12:49 -07001098
Adam Cohen2801caf2011-05-13 20:57:39 -07001099 if (mRearrangeOnClose) {
1100 setupContentForNumItems(getItemCount());
1101 mRearrangeOnClose = false;
1102 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001103 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001104 if (!mDragInProgress && !mSuppressFolderDeletion) {
1105 replaceFolderWithFinalItem();
1106 } else if (mDragInProgress) {
1107 mDeleteFolderOnDropCompleted = true;
1108 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001109 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001110 mSuppressFolderDeletion = false;
Adam Cohenafb01ee2011-06-23 15:38:03 -07001111 }
1112
1113 private void replaceFolderWithFinalItem() {
Adam Cohenafb01ee2011-06-23 15:38:03 -07001114 // Add the last remaining child to the workspace in place of the folder
Adam Cohenfb91f302012-06-11 15:45:18 -07001115 Runnable onCompleteRunnable = new Runnable() {
1116 @Override
1117 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -07001118 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screenId);
Adam Cohenafb01ee2011-06-23 15:38:03 -07001119
Winson Chung33231f52013-12-09 16:57:45 -08001120 View child = null;
Adam Cohenfb91f302012-06-11 15:45:18 -07001121 // Move the item from the folder to the workspace, in the position of the folder
1122 if (getItemCount() == 1) {
1123 ShortcutInfo finalItem = mInfo.contents.get(0);
Adam Cohenc5e63f32012-07-12 16:16:57 -07001124 child = mLauncher.createShortcut(R.layout.application, cellLayout,
Adam Cohenfb91f302012-06-11 15:45:18 -07001125 finalItem);
1126 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -07001127 mInfo.screenId, mInfo.cellX, mInfo.cellY);
Adam Cohenfb91f302012-06-11 15:45:18 -07001128 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001129 if (getItemCount() <= 1) {
1130 // Remove the folder
1131 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
Dan Sandler0eb687f2014-01-10 13:24:55 -05001132 if (cellLayout != null) {
1133 // b/12446428 -- sometimes the cell layout has already gone away?
1134 cellLayout.removeView(mFolderIcon);
1135 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001136 if (mFolderIcon instanceof DropTarget) {
1137 mDragController.removeDropTarget((DropTarget) mFolderIcon);
1138 }
1139 mLauncher.removeFolder(mInfo);
1140 }
Adam Cohenc5e63f32012-07-12 16:16:57 -07001141 // We add the child after removing the folder to prevent both from existing at
Winson Chung0e6a7132013-08-23 12:55:10 -07001142 // the same time in the CellLayout. We need to add the new item with addInScreenFromBind()
1143 // to ensure that hotseat items are placed correctly.
Adam Cohenc5e63f32012-07-12 16:16:57 -07001144 if (child != null) {
Winson Chung0e6a7132013-08-23 12:55:10 -07001145 mLauncher.getWorkspace().addInScreenFromBind(child, mInfo.container, mInfo.screenId,
Adam Cohenc5e63f32012-07-12 16:16:57 -07001146 mInfo.cellX, mInfo.cellY, mInfo.spanX, mInfo.spanY);
1147 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001148 }
1149 };
1150 View finalChild = getItemAt(0);
1151 if (finalChild != null) {
1152 mFolderIcon.performDestroyAnimation(finalChild, onCompleteRunnable);
Winson Chung33231f52013-12-09 16:57:45 -08001153 } else {
1154 onCompleteRunnable.run();
Adam Cohenafb01ee2011-06-23 15:38:03 -07001155 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001156 mDestroyed = true;
1157 }
1158
1159 boolean isDestroyed() {
1160 return mDestroyed;
Adam Cohen2801caf2011-05-13 20:57:39 -07001161 }
1162
Adam Cohenac56cff2011-09-28 20:45:37 -07001163 // This method keeps track of the last item in the folder for the purposes
1164 // of keyboard focus
1165 private void updateTextViewFocus() {
1166 View lastChild = getItemAt(getItemCount() - 1);
1167 getItemAt(getItemCount() - 1);
1168 if (lastChild != null) {
1169 mFolderName.setNextFocusDownId(lastChild.getId());
1170 mFolderName.setNextFocusRightId(lastChild.getId());
1171 mFolderName.setNextFocusLeftId(lastChild.getId());
1172 mFolderName.setNextFocusUpId(lastChild.getId());
1173 }
1174 }
1175
Adam Cohenbfbfd262011-06-13 16:55:12 -07001176 public void onDrop(DragObject d) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001177 Runnable cleanUpRunnable = null;
1178
1179 // If we are coming from All Apps space, we need to remove the extra empty screen (which is
1180 // normally done in Workspace#onDropExternal, as well zoom back in and close the folder.
1181 if (d.dragSource != mLauncher.getWorkspace() && !(d.dragSource instanceof Folder)) {
1182 cleanUpRunnable = new Runnable() {
1183 @Override
1184 public void run() {
1185 mLauncher.getWorkspace().removeExtraEmptyScreen(false, new Runnable() {
1186 @Override
1187 public void run() {
1188 mLauncher.closeFolder();
1189 mLauncher.exitSpringLoadedDragModeDelayed(true,
1190 Launcher.EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT_FOLDER_CLOSE,
1191 null);
1192 }
1193 }, CLOSE_FOLDER_DELAY_MS, false);
1194 }
1195 };
Adam Cohenbfbfd262011-06-13 16:55:12 -07001196 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001197
1198 View currentDragView;
1199 ShortcutInfo si = mCurrentDragInfo;
1200 if (mIsExternalDrag) {
1201 si.cellX = mEmptyCell[0];
1202 si.cellY = mEmptyCell[1];
1203 currentDragView = createAndAddShortcut(si);
1204 } else {
1205 currentDragView = mCurrentDragView;
1206 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) currentDragView.getLayoutParams();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001207 si.cellX = lp.cellX = mEmptyCell[0];
1208 si.cellX = lp.cellY = mEmptyCell[1];
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001209 mContent.addViewToCellLayout(currentDragView, -1, (int) si.id, lp, true);
Adam Cohenbfbfd262011-06-13 16:55:12 -07001210 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001211
1212 if (d.dragView.hasDrawn()) {
1213
1214 // Temporarily reset the scale such that the animation target gets calculated correctly.
1215 float scaleX = getScaleX();
1216 float scaleY = getScaleY();
1217 setScaleX(1.0f);
1218 setScaleY(1.0f);
1219 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, currentDragView,
1220 cleanUpRunnable, null);
1221 setScaleX(scaleX);
1222 setScaleY(scaleY);
1223 } else {
1224 d.deferDragViewCleanupPostAnimation = false;
1225 currentDragView.setVisibility(VISIBLE);
1226 }
1227 mItemsInvalidated = true;
1228 setupContentDimensions(getItemCount());
1229
1230 // Actually move the item in the database if it was an external drag.
1231 if (mIsExternalDrag) {
1232 LauncherModel.addOrMoveItemInDatabase(
1233 mLauncher, si, mInfo.id, 0, si.cellX, si.cellY);
1234
1235 // We only need to update the locations if it doesn't get handled in #onDropCompleted.
1236 if (d.dragSource != this) {
1237 updateItemLocationsInDatabaseBatch();
1238 }
1239 mIsExternalDrag = false;
1240 }
1241
1242 // Temporarily suppress the listener, as we did all the work already here.
1243 mSuppressOnAdd = true;
1244 mInfo.add(si);
1245 mSuppressOnAdd = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001246 }
1247
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001248 // This is used so the item doesn't immediately appear in the folder when added. In one case
1249 // we need to create the illusion that the item isn't added back to the folder yet, to
1250 // to correspond to the animation of the icon back into the folder. This is
1251 public void hideItem(ShortcutInfo info) {
1252 View v = getViewForInfo(info);
1253 v.setVisibility(INVISIBLE);
1254 }
1255 public void showItem(ShortcutInfo info) {
1256 View v = getViewForInfo(info);
1257 v.setVisibility(VISIBLE);
1258 }
1259
Adam Cohenbfbfd262011-06-13 16:55:12 -07001260 public void onAdd(ShortcutInfo item) {
1261 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001262 // If the item was dropped onto this open folder, we have done the work associated
1263 // with adding the item to the folder, as indicated by mSuppressOnAdd being set
Adam Cohenbfbfd262011-06-13 16:55:12 -07001264 if (mSuppressOnAdd) return;
1265 if (!findAndSetEmptyCells(item)) {
1266 // The current layout is full, can we expand it?
1267 setupContentForNumItems(getItemCount() + 1);
1268 findAndSetEmptyCells(item);
1269 }
1270 createAndAddShortcut(item);
1271 LauncherModel.addOrMoveItemInDatabase(
1272 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
1273 }
1274
Adam Cohena9cf38f2011-05-02 15:36:58 -07001275 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -07001276 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001277 // If this item is being dragged from this open folder, we have already handled
1278 // the work associated with removing the item, so we don't have to do anything here.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001279 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001280 View v = getViewForInfo(item);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001281 mContent.removeView(v);
Adam Cohen2801caf2011-05-13 20:57:39 -07001282 if (mState == STATE_ANIMATING) {
1283 mRearrangeOnClose = true;
1284 } else {
1285 setupContentForNumItems(getItemCount());
1286 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001287 if (getItemCount() <= 1) {
1288 replaceFolderWithFinalItem();
1289 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001290 }
Adam Cohen7c693212011-05-18 15:26:57 -07001291
Adam Cohendf1e4e82011-06-24 15:57:39 -07001292 private View getViewForInfo(ShortcutInfo item) {
1293 for (int j = 0; j < mContent.getCountY(); j++) {
1294 for (int i = 0; i < mContent.getCountX(); i++) {
1295 View v = mContent.getChildAt(i, j);
1296 if (v.getTag() == item) {
1297 return v;
1298 }
1299 }
1300 }
1301 return null;
1302 }
1303
Adam Cohen76078c42011-06-09 15:06:52 -07001304 public void onItemsChanged() {
Adam Cohenac56cff2011-09-28 20:45:37 -07001305 updateTextViewFocus();
Adam Cohen76078c42011-06-09 15:06:52 -07001306 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001307
Adam Cohen76fc0852011-06-17 13:26:23 -07001308 public void onTitleChanged(CharSequence title) {
1309 }
Adam Cohen76078c42011-06-09 15:06:52 -07001310
Adam Cohen7c693212011-05-18 15:26:57 -07001311 public ArrayList<View> getItemsInReadingOrder() {
1312 if (mItemsInvalidated) {
1313 mItemsInReadingOrder.clear();
1314 for (int j = 0; j < mContent.getCountY(); j++) {
1315 for (int i = 0; i < mContent.getCountX(); i++) {
1316 View v = mContent.getChildAt(i, j);
1317 if (v != null) {
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001318 mItemsInReadingOrder.add(v);
Adam Cohen7c693212011-05-18 15:26:57 -07001319 }
1320 }
1321 }
1322 mItemsInvalidated = false;
1323 }
1324 return mItemsInReadingOrder;
1325 }
Adam Cohen8dfcba42011-07-07 16:38:18 -07001326
1327 public void getLocationInDragLayer(int[] loc) {
1328 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
1329 }
Adam Cohenea0818d2011-09-30 20:06:58 -07001330
1331 public void onFocusChange(View v, boolean hasFocus) {
1332 if (v == mFolderName && hasFocus) {
1333 startEditingFolderName();
1334 }
1335 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001336
1337 @Override
1338 public void getHitRectRelativeToDragLayer(Rect outRect) {
1339 getHitRect(outRect);
1340 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001341}