blob: b4c399266cd639dc2fcd7915dc04d4ad937df9f0 [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
Adam Cohen0c872ba2011-05-05 10:34:16 -0700306 /**
307 * We need to handle touch events to prevent them from falling through to the workspace below.
308 */
309 @Override
310 public boolean onTouchEvent(MotionEvent ev) {
311 return true;
312 }
313
Joe Onorato00acb122009-08-04 16:04:30 -0400314 public void setDragController(DragController dragController) {
315 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800316 }
317
Adam Cohen2801caf2011-05-13 20:57:39 -0700318 void setFolderIcon(FolderIcon icon) {
319 mFolderIcon = icon;
320 }
321
Adam Cohen3371da02011-10-25 21:38:29 -0700322 @Override
323 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
324 // When the folder gets focus, we don't want to announce the list of items.
325 return true;
326 }
327
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800328 /**
329 * @return the FolderInfo object associated with this folder
330 */
331 FolderInfo getInfo() {
332 return mInfo;
333 }
334
Adam Cohen3bf84d32012-05-07 20:17:14 -0700335 private class GridComparator implements Comparator<ShortcutInfo> {
336 int mNumCols;
337 public GridComparator(int numCols) {
338 mNumCols = numCols;
339 }
340
341 @Override
342 public int compare(ShortcutInfo lhs, ShortcutInfo rhs) {
343 int lhIndex = lhs.cellY * mNumCols + lhs.cellX;
344 int rhIndex = rhs.cellY * mNumCols + rhs.cellX;
345 return (lhIndex - rhIndex);
346 }
Adam Cohen3bf84d32012-05-07 20:17:14 -0700347 }
348
349 private void placeInReadingOrder(ArrayList<ShortcutInfo> items) {
350 int maxX = 0;
351 int count = items.size();
352 for (int i = 0; i < count; i++) {
353 ShortcutInfo item = items.get(i);
354 if (item.cellX > maxX) {
355 maxX = item.cellX;
356 }
357 }
Adam Cohen691a5792012-05-11 14:27:30 -0700358
359 GridComparator gridComparator = new GridComparator(maxX + 1);
Adam Cohen3bf84d32012-05-07 20:17:14 -0700360 Collections.sort(items, gridComparator);
361 final int countX = mContent.getCountX();
362 for (int i = 0; i < count; i++) {
363 int x = i % countX;
364 int y = i / countX;
365 ShortcutInfo item = items.get(i);
366 item.cellX = x;
367 item.cellY = y;
368 }
369 }
370
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800371 void bind(FolderInfo info) {
372 mInfo = info;
Adam Cohendf2cc412011-04-27 16:56:57 -0700373 ArrayList<ShortcutInfo> children = info.contents;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700374 ArrayList<ShortcutInfo> overflow = new ArrayList<ShortcutInfo>();
Adam Cohen7c693212011-05-18 15:26:57 -0700375 setupContentForNumItems(children.size());
Adam Cohen3bf84d32012-05-07 20:17:14 -0700376 placeInReadingOrder(children);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700377 int count = 0;
Adam Cohendf2cc412011-04-27 16:56:57 -0700378 for (int i = 0; i < children.size(); i++) {
379 ShortcutInfo child = (ShortcutInfo) children.get(i);
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800380 if (createAndAddShortcut(child) == null) {
Adam Cohenc508b2d2011-06-28 14:41:44 -0700381 overflow.add(child);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700382 } else {
383 count++;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700384 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700385 }
Adam Cohenc508b2d2011-06-28 14:41:44 -0700386
Adam Cohen0057bbc2011-08-12 18:30:51 -0700387 // We rearrange the items in case there are any empty gaps
388 setupContentForNumItems(count);
389
Adam Cohenc508b2d2011-06-28 14:41:44 -0700390 // If our folder has too many items we prune them from the list. This is an issue
391 // when upgrading from the old Folders implementation which could contain an unlimited
392 // number of items.
393 for (ShortcutInfo item: overflow) {
394 mInfo.remove(item);
395 LauncherModel.deleteItemFromDatabase(mLauncher, item);
396 }
397
Adam Cohen4dbe6d92011-05-18 17:14:15 -0700398 mItemsInvalidated = true;
Adam Cohenac56cff2011-09-28 20:45:37 -0700399 updateTextViewFocus();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700400 mInfo.addListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700401
Adam Cohenafb01ee2011-06-23 15:38:03 -0700402 if (!sDefaultFolderName.contentEquals(mInfo.title)) {
Adam Cohen4ef610f2011-06-21 22:37:36 -0700403 mFolderName.setText(mInfo.title);
404 } else {
405 mFolderName.setText("");
406 }
Adam Cohen691a5792012-05-11 14:27:30 -0700407 updateItemLocationsInDatabase();
Winson Chung33231f52013-12-09 16:57:45 -0800408
409 // In case any children didn't come across during loading, clean up the folder accordingly
410 mFolderIcon.post(new Runnable() {
411 public void run() {
412 if (getItemCount() <= 1) {
413 replaceFolderWithFinalItem();
414 }
415 }
416 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700417 }
418
419 /**
420 * Creates a new UserFolder, inflated from R.layout.user_folder.
421 *
422 * @param context The application's context.
423 *
424 * @return A new UserFolder.
425 */
426 static Folder fromXml(Context context) {
427 return (Folder) LayoutInflater.from(context).inflate(R.layout.user_folder, null);
428 }
429
430 /**
431 * This method is intended to make the UserFolder to be visually identical in size and position
432 * to its associated FolderIcon. This allows for a seamless transition into the expanded state.
433 */
434 private void positionAndSizeAsIcon() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700435 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800436 setScaleX(0.8f);
437 setScaleY(0.8f);
438 setAlpha(0f);
Adam Cohendf2cc412011-04-27 16:56:57 -0700439 mState = STATE_SMALL;
440 }
441
442 public void animateOpen() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700443 positionAndSizeAsIcon();
444
Adam Cohen8e776a62011-06-28 18:10:06 -0700445 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen2801caf2011-05-13 20:57:39 -0700446 centerAboutIcon();
Adam Cohen662b5982011-12-13 17:45:21 -0800447 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1);
448 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
449 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
Michael Jurka032e6ba2013-04-22 15:08:42 +0200450 final ObjectAnimator oa =
Michael Jurka2ecf9952012-06-18 12:52:28 -0700451 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohen6441de02011-12-14 14:25:32 -0800452
Adam Cohen2801caf2011-05-13 20:57:39 -0700453 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700454 @Override
455 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700456 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700457 String.format(getContext().getString(R.string.folder_opened),
Adam Cohen3371da02011-10-25 21:38:29 -0700458 mContent.getCountX(), mContent.getCountY()));
Adam Cohendf2cc412011-04-27 16:56:57 -0700459 mState = STATE_ANIMATING;
460 }
461 @Override
462 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700463 mState = STATE_OPEN;
Michael Jurka0121c3e2012-05-31 08:36:04 -0700464 setLayerType(LAYER_TYPE_NONE, null);
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800465
466 // Only show cling if we are not in the middle of a drag - this would be quite jarring.
467 if (!mDragController.isDragging()) {
468 Cling cling = mLauncher.getLauncherClings().showFoldersCling();
469 if (cling != null) {
470 cling.bringScrimToFront();
471 bringToFront();
472 cling.bringToFront();
473 }
Winson Chung7a74ac92011-09-20 17:43:51 -0700474 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700475 setFocusOnFirstChild();
Adam Cohendf2cc412011-04-27 16:56:57 -0700476 }
477 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700478 oa.setDuration(mExpandDuration);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700479 setLayerType(LAYER_TYPE_HARDWARE, null);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100480 oa.start();
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800481
482 // Make sure the folder picks up the last drag move even if the finger doesn't move.
483 if (mDragController.isDragging()) {
484 mDragController.forceTouchMove();
485 }
486 }
487
488 public void beginExternalDrag(ShortcutInfo item) {
489 setupContentForNumItems(getItemCount() + 1);
490 findAndSetEmptyCells(item);
491
492 mCurrentDragInfo = item;
493 mEmptyCell[0] = item.cellX;
494 mEmptyCell[1] = item.cellY;
495 mIsExternalDrag = true;
496
497 mDragInProgress = true;
Adam Cohendf2cc412011-04-27 16:56:57 -0700498 }
499
Adam Cohen3371da02011-10-25 21:38:29 -0700500 private void sendCustomAccessibilityEvent(int type, String text) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700501 AccessibilityManager accessibilityManager = (AccessibilityManager)
502 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
503 if (accessibilityManager.isEnabled()) {
Adam Cohen3371da02011-10-25 21:38:29 -0700504 AccessibilityEvent event = AccessibilityEvent.obtain(type);
505 onInitializeAccessibilityEvent(event);
506 event.getText().add(text);
Michael Jurka8b805b12012-04-18 14:23:14 -0700507 accessibilityManager.sendAccessibilityEvent(event);
Adam Cohen3371da02011-10-25 21:38:29 -0700508 }
509 }
510
Adam Cohenac56cff2011-09-28 20:45:37 -0700511 private void setFocusOnFirstChild() {
512 View firstChild = mContent.getChildAt(0, 0);
513 if (firstChild != null) {
514 firstChild.requestFocus();
515 }
516 }
517
Adam Cohendf2cc412011-04-27 16:56:57 -0700518 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700519 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800520 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
521 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
522 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
Michael Jurka032e6ba2013-04-22 15:08:42 +0200523 final ObjectAnimator oa =
Michael Jurka2ecf9952012-06-18 12:52:28 -0700524 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohendf2cc412011-04-27 16:56:57 -0700525
Adam Cohen2801caf2011-05-13 20:57:39 -0700526 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700527 @Override
528 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700529 onCloseComplete();
Michael Jurka0121c3e2012-05-31 08:36:04 -0700530 setLayerType(LAYER_TYPE_NONE, null);
Adam Cohen2801caf2011-05-13 20:57:39 -0700531 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700532 }
533 @Override
534 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700535 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700536 getContext().getString(R.string.folder_closed));
Adam Cohendf2cc412011-04-27 16:56:57 -0700537 mState = STATE_ANIMATING;
538 }
539 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700540 oa.setDuration(mExpandDuration);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700541 setLayerType(LAYER_TYPE_HARDWARE, null);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100542 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700543 }
544
Adam Cohencb3382b2011-05-24 14:07:08 -0700545 public boolean acceptDrop(DragObject d) {
546 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700547 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700548 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
549 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
550 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700551 }
552
Adam Cohendf2cc412011-04-27 16:56:57 -0700553 protected boolean findAndSetEmptyCells(ShortcutInfo item) {
554 int[] emptyCell = new int[2];
555 if (mContent.findCellForSpan(emptyCell, item.spanX, item.spanY)) {
556 item.cellX = emptyCell[0];
557 item.cellY = emptyCell[1];
Adam Cohendf2cc412011-04-27 16:56:57 -0700558 return true;
559 } else {
560 return false;
561 }
562 }
563
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800564 protected View createAndAddShortcut(ShortcutInfo item) {
Adam Cohen477828c2013-09-20 12:05:49 -0700565 final BubbleTextView textView =
566 (BubbleTextView) mInflater.inflate(R.layout.application, this, false);
Winson Chung54000492013-10-14 16:29:29 -0700567 textView.setCompoundDrawables(null,
Winson Chung0dbd7342013-10-13 22:46:20 -0700568 Utilities.createIconDrawable(item.getIcon(mIconCache)), null, null);
Adam Cohendf2cc412011-04-27 16:56:57 -0700569 textView.setText(item.title);
570 textView.setTag(item);
Adam Cohen477828c2013-09-20 12:05:49 -0700571 textView.setTextColor(getResources().getColor(R.color.folder_items_text_color));
572 textView.setShadowsEnabled(false);
Adam Cohen7397e622013-10-24 12:11:34 -0700573 textView.setGlowColor(getResources().getColor(R.color.folder_items_glow_color));
Adam Cohendf2cc412011-04-27 16:56:57 -0700574
575 textView.setOnClickListener(this);
576 textView.setOnLongClickListener(this);
577
Adam Cohenc508b2d2011-06-28 14:41:44 -0700578 // We need to check here to verify that the given item's location isn't already occupied
Adam Cohen3bf84d32012-05-07 20:17:14 -0700579 // by another item.
Adam Cohen0057bbc2011-08-12 18:30:51 -0700580 if (mContent.getChildAt(item.cellX, item.cellY) != null || item.cellX < 0 || item.cellY < 0
581 || item.cellX >= mContent.getCountX() || item.cellY >= mContent.getCountY()) {
Adam Cohen3bf84d32012-05-07 20:17:14 -0700582 // This shouldn't happen, log it.
583 Log.e(TAG, "Folder order not properly persisted during bind");
Adam Cohenc508b2d2011-06-28 14:41:44 -0700584 if (!findAndSetEmptyCells(item)) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800585 return null;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700586 }
587 }
588
Adam Cohendf2cc412011-04-27 16:56:57 -0700589 CellLayout.LayoutParams lp =
590 new CellLayout.LayoutParams(item.cellX, item.cellY, item.spanX, item.spanY);
591 boolean insert = false;
Adam Cohenac56cff2011-09-28 20:45:37 -0700592 textView.setOnKeyListener(new FolderKeyEventListener());
Adam Cohendf2cc412011-04-27 16:56:57 -0700593 mContent.addViewToCellLayout(textView, insert ? 0 : -1, (int)item.id, lp, true);
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800594 return textView;
Adam Cohendf2cc412011-04-27 16:56:57 -0700595 }
596
Adam Cohencb3382b2011-05-24 14:07:08 -0700597 public void onDragEnter(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700598 mPreviousTargetCell[0] = -1;
599 mPreviousTargetCell[1] = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700600 mOnExitAlarm.cancelAlarm();
601 }
602
603 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
604 public void onAlarm(Alarm alarm) {
605 realTimeReorder(mEmptyCell, mTargetCell);
606 }
607 };
608
609 boolean readingOrderGreaterThan(int[] v1, int[] v2) {
610 if (v1[1] > v2[1] || (v1[1] == v2[1] && v1[0] > v2[0])) {
611 return true;
612 } else {
613 return false;
614 }
615 }
616
617 private void realTimeReorder(int[] empty, int[] target) {
618 boolean wrap;
619 int startX;
620 int endX;
621 int startY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700622 int delay = 0;
623 float delayAmount = 30;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700624 if (readingOrderGreaterThan(target, empty)) {
625 wrap = empty[0] >= mContent.getCountX() - 1;
626 startY = wrap ? empty[1] + 1 : empty[1];
627 for (int y = startY; y <= target[1]; y++) {
628 startX = y == empty[1] ? empty[0] + 1 : 0;
629 endX = y < target[1] ? mContent.getCountX() - 1 : target[0];
630 for (int x = startX; x <= endX; x++) {
631 View v = mContent.getChildAt(x,y);
632 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen482ed822012-03-02 14:15:13 -0800633 REORDER_ANIMATION_DURATION, delay, true, true)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700634 empty[0] = x;
635 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700636 delay += delayAmount;
637 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700638 }
639 }
640 }
641 } else {
642 wrap = empty[0] == 0;
643 startY = wrap ? empty[1] - 1 : empty[1];
644 for (int y = startY; y >= target[1]; y--) {
645 startX = y == empty[1] ? empty[0] - 1 : mContent.getCountX() - 1;
646 endX = y > target[1] ? 0 : target[0];
647 for (int x = startX; x >= endX; x--) {
648 View v = mContent.getChildAt(x,y);
649 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen482ed822012-03-02 14:15:13 -0800650 REORDER_ANIMATION_DURATION, delay, true, true)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700651 empty[0] = x;
652 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700653 delay += delayAmount;
654 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700655 }
656 }
657 }
658 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700659 }
660
Adam Cohen2374abf2013-04-16 14:56:57 -0700661 public boolean isLayoutRtl() {
662 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
663 }
664
Adam Cohencb3382b2011-05-24 14:07:08 -0700665 public void onDragOver(DragObject d) {
Alan Viverette4cda5b72013-08-28 17:53:41 -0700666 final DragView dragView = d.dragView;
667 final int scrollOffset = mScrollView.getScrollY();
668 final float[] r = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, dragView, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700669 r[0] -= getPaddingLeft();
670 r[1] -= getPaddingTop();
671
Alan Viverette4cda5b72013-08-28 17:53:41 -0700672 final long downTime = SystemClock.uptimeMillis();
673 final MotionEvent translatedEv = MotionEvent.obtain(
674 downTime, downTime, MotionEvent.ACTION_MOVE, d.x, d.y, 0);
675
676 if (!mAutoScrollHelper.isEnabled()) {
677 mAutoScrollHelper.setEnabled(true);
678 }
679
680 final boolean handled = mAutoScrollHelper.onTouch(this, translatedEv);
681 translatedEv.recycle();
682
683 if (handled) {
684 mReorderAlarm.cancelAlarm();
685 } else {
686 mTargetCell = mContent.findNearestArea(
687 (int) r[0], (int) r[1] + scrollOffset, 1, 1, mTargetCell);
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700688 if (isLayoutRtl()) {
689 mTargetCell[0] = mContent.getCountX() - mTargetCell[0] - 1;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700690 }
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700691 if (mTargetCell[0] != mPreviousTargetCell[0]
692 || mTargetCell[1] != mPreviousTargetCell[1]) {
693 mReorderAlarm.cancelAlarm();
694 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
695 mReorderAlarm.setAlarm(REORDER_DELAY);
696 mPreviousTargetCell[0] = mTargetCell[0];
697 mPreviousTargetCell[1] = mTargetCell[1];
698 mDragMode = DRAG_MODE_REORDER;
699 } else {
700 mDragMode = DRAG_MODE_NONE;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700701 }
Adam Cohenbfbfd262011-06-13 16:55:12 -0700702 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700703 }
704
Adam Cohenbfbfd262011-06-13 16:55:12 -0700705 // This is used to compute the visual center of the dragView. The idea is that
706 // the visual center represents the user's interpretation of where the item is, and hence
707 // is the appropriate point to use when determining drop location.
708 private float[] getDragViewVisualCenter(int x, int y, int xOffset, int yOffset,
709 DragView dragView, float[] recycle) {
710 float res[];
711 if (recycle == null) {
712 res = new float[2];
713 } else {
714 res = recycle;
715 }
716
717 // These represent the visual top and left of drag view if a dragRect was provided.
718 // If a dragRect was not provided, then they correspond to the actual view left and
719 // top, as the dragRect is in that case taken to be the entire dragView.
720 // R.dimen.dragViewOffsetY.
721 int left = x - xOffset;
722 int top = y - yOffset;
723
724 // In order to find the visual center, we shift by half the dragRect
725 res[0] = left + dragView.getDragRegion().width() / 2;
726 res[1] = top + dragView.getDragRegion().height() / 2;
727
728 return res;
729 }
730
731 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
732 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700733 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700734 }
735 };
736
Adam Cohen95bb8002011-07-03 23:40:28 -0700737 public void completeDragExit() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700738 mLauncher.closeFolder();
739 mCurrentDragInfo = null;
740 mCurrentDragView = null;
741 mSuppressOnAdd = false;
742 mRearrangeOnClose = true;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800743 mIsExternalDrag = false;
Adam Cohen3e8f8112011-07-02 18:03:00 -0700744 }
745
Adam Cohencb3382b2011-05-24 14:07:08 -0700746 public void onDragExit(DragObject d) {
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700747 // Exiting folder; stop the auto scroller.
Alan Viverette4cda5b72013-08-28 17:53:41 -0700748 mAutoScrollHelper.setEnabled(false);
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700749 // We only close the folder if this is a true drag exit, ie. not because
750 // a drop has occurred above the folder.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700751 if (!d.dragComplete) {
752 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
753 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
754 }
755 mReorderAlarm.cancelAlarm();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700756 mDragMode = DRAG_MODE_NONE;
Adam Cohen2801caf2011-05-13 20:57:39 -0700757 }
758
Michael Jurka1e2f4652013-07-08 18:03:46 -0700759 public void onDropCompleted(final View target, final DragObject d,
760 final boolean isFlingToDelete, final boolean success) {
761 if (mDeferDropAfterUninstall) {
Michael Jurkaf3007582013-08-21 14:33:57 +0200762 Log.d(TAG, "Deferred handling drop because waiting for uninstall.");
Michael Jurka1e2f4652013-07-08 18:03:46 -0700763 mDeferredAction = new Runnable() {
764 public void run() {
765 onDropCompleted(target, d, isFlingToDelete, success);
766 mDeferredAction = null;
767 }
768 };
769 return;
770 }
771
772 boolean beingCalledAfterUninstall = mDeferredAction != null;
773 boolean successfulDrop =
774 success && (!beingCalledAfterUninstall || mUninstallSuccessful);
Winson Chung5f8afe62013-08-12 16:19:28 -0700775
Michael Jurka1e2f4652013-07-08 18:03:46 -0700776 if (successfulDrop) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800777 if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon && target != this) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700778 replaceFolderWithFinalItem();
779 }
780 } else {
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700781 setupContentForNumItems(getItemCount());
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700782 // The drag failed, we need to return the item to the folder
783 mFolderIcon.onDrop(d);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700784 }
785
786 if (target != this) {
787 if (mOnExitAlarm.alarmPending()) {
788 mOnExitAlarm.cancelAlarm();
Michael Jurka54554252013-08-01 12:52:23 +0200789 if (!successfulDrop) {
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700790 mSuppressFolderDeletion = true;
791 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700792 completeDragExit();
793 }
Adam Cohen9c58d822013-12-13 17:18:10 -0800794 }
795
796 // This is kind of hacky, but in general, dropping on the workspace handles removing
797 // the extra screen, but dropping elsewhere (back to self, or onto delete) doesn't.
798 if (target != mLauncher.getWorkspace()) {
Adam Cohenf0f2ffa2013-11-12 11:44:58 +0000799 mLauncher.getWorkspace().removeExtraEmptyScreen(true, null);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700800 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700801
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700802 mDeleteFolderOnDropCompleted = false;
803 mDragInProgress = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700804 mItemAddedBackToSelfViaIcon = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700805 mCurrentDragInfo = null;
806 mCurrentDragView = null;
807 mSuppressOnAdd = false;
Adam Cohen4045eb72011-10-06 11:44:26 -0700808
809 // Reordering may have occured, and we need to save the new item locations. We do this once
810 // at the end to prevent unnecessary database operations.
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700811 updateItemLocationsInDatabaseBatch();
Adam Cohen4045eb72011-10-06 11:44:26 -0700812 }
813
Michael Jurka1e2f4652013-07-08 18:03:46 -0700814 public void deferCompleteDropAfterUninstallActivity() {
815 mDeferDropAfterUninstall = true;
816 }
817
818 public void onUninstallActivityReturned(boolean success) {
819 mDeferDropAfterUninstall = false;
820 mUninstallSuccessful = success;
821 if (mDeferredAction != null) {
822 mDeferredAction.run();
823 }
824 }
825
Winson Chunga48487a2012-03-20 16:19:37 -0700826 @Override
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800827 public float getIntrinsicIconScaleFactor() {
828 return 1f;
829 }
830
831 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800832 public boolean supportsFlingToDelete() {
833 return true;
834 }
835
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000836 @Override
837 public boolean supportsAppInfoDropTarget() {
838 return false;
839 }
840
841 @Override
842 public boolean supportsDeleteDropTarget() {
843 return true;
844 }
845
Winson Chunga48487a2012-03-20 16:19:37 -0700846 public void onFlingToDelete(DragObject d, int x, int y, PointF vec) {
847 // Do nothing
848 }
849
850 @Override
851 public void onFlingToDeleteCompleted() {
852 // Do nothing
853 }
854
Adam Cohen4045eb72011-10-06 11:44:26 -0700855 private void updateItemLocationsInDatabase() {
856 ArrayList<View> list = getItemsInReadingOrder();
857 for (int i = 0; i < list.size(); i++) {
858 View v = list.get(i);
859 ItemInfo info = (ItemInfo) v.getTag();
860 LauncherModel.moveItemInDatabase(mLauncher, info, mInfo.id, 0,
Winson Chung33231f52013-12-09 16:57:45 -0800861 info.cellX, info.cellY);
Adam Cohen4045eb72011-10-06 11:44:26 -0700862 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700863 }
Adam Cohen228da5a2011-07-27 22:23:47 -0700864
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700865 private void updateItemLocationsInDatabaseBatch() {
866 ArrayList<View> list = getItemsInReadingOrder();
867 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
868 for (int i = 0; i < list.size(); i++) {
869 View v = list.get(i);
870 ItemInfo info = (ItemInfo) v.getTag();
871 items.add(info);
872 }
873
874 LauncherModel.moveItemsInDatabase(mLauncher, items, mInfo.id, 0);
875 }
876
Adam Cohene25af792013-06-06 23:08:25 -0700877 public void addItemLocationsInDatabase() {
878 ArrayList<View> list = getItemsInReadingOrder();
879 for (int i = 0; i < list.size(); i++) {
880 View v = list.get(i);
881 ItemInfo info = (ItemInfo) v.getTag();
882 LauncherModel.addItemToDatabase(mLauncher, info, mInfo.id, 0,
883 info.cellX, info.cellY, false);
884 }
885 }
886
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700887 public void notifyDrop() {
888 if (mDragInProgress) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700889 mItemAddedBackToSelfViaIcon = true;
Adam Cohen76078c42011-06-09 15:06:52 -0700890 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700891 }
892
893 public boolean isDropEnabled() {
894 return true;
895 }
896
Adam Cohen4045eb72011-10-06 11:44:26 -0700897 private void setupContentDimensions(int count) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700898 ArrayList<View> list = getItemsInReadingOrder();
899
900 int countX = mContent.getCountX();
901 int countY = mContent.getCountY();
Adam Cohen7c693212011-05-18 15:26:57 -0700902 boolean done = false;
Adam Cohen2801caf2011-05-13 20:57:39 -0700903
Adam Cohen7c693212011-05-18 15:26:57 -0700904 while (!done) {
905 int oldCountX = countX;
906 int oldCountY = countY;
907 if (countX * countY < count) {
908 // Current grid is too small, expand it
Adam Cohen78dc83e2011-11-15 17:10:00 -0800909 if ((countX <= countY || countY == mMaxCountY) && countX < mMaxCountX) {
Adam Cohen7c693212011-05-18 15:26:57 -0700910 countX++;
911 } else if (countY < mMaxCountY) {
912 countY++;
913 }
914 if (countY == 0) countY++;
915 } else if ((countY - 1) * countX >= count && countY >= countX) {
916 countY = Math.max(0, countY - 1);
917 } else if ((countX - 1) * countY >= count) {
918 countX = Math.max(0, countX - 1);
Adam Cohen2801caf2011-05-13 20:57:39 -0700919 }
Adam Cohen7c693212011-05-18 15:26:57 -0700920 done = countX == oldCountX && countY == oldCountY;
Adam Cohen2801caf2011-05-13 20:57:39 -0700921 }
Adam Cohen7c693212011-05-18 15:26:57 -0700922 mContent.setGridSize(countX, countY);
Adam Cohen2801caf2011-05-13 20:57:39 -0700923 arrangeChildren(list);
924 }
925
926 public boolean isFull() {
Adam Cohen78dc83e2011-11-15 17:10:00 -0800927 return getItemCount() >= mMaxNumItems;
Adam Cohen2801caf2011-05-13 20:57:39 -0700928 }
929
930 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700931 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700932
Winson Chung892c74d2013-08-22 16:15:50 -0700933 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700934 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700935 int height = getFolderHeight();
Adam Cohen2801caf2011-05-13 20:57:39 -0700936
Adam Cohen307fe232012-08-16 17:55:58 -0700937 float scale = parent.getDescendantRectRelativeToSelf(mFolderIcon, mTempRect);
Adam Cohen8e776a62011-06-28 18:10:06 -0700938
Winson Chungaf40f202013-09-18 18:26:31 -0700939 LauncherAppState app = LauncherAppState.getInstance();
940 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
941
Adam Cohen307fe232012-08-16 17:55:58 -0700942 int centerX = (int) (mTempRect.left + mTempRect.width() * scale / 2);
943 int centerY = (int) (mTempRect.top + mTempRect.height() * scale / 2);
Adam Cohen2801caf2011-05-13 20:57:39 -0700944 int centeredLeft = centerX - width / 2;
945 int centeredTop = centerY - height / 2;
Winson Chung3057b1c2013-10-10 17:35:35 -0700946 int currentPage = mLauncher.getWorkspace().getNextPage();
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800947 // In case the workspace is scrolling, we need to use the final scroll to compute
948 // the folders bounds.
949 mLauncher.getWorkspace().setFinalScrollForPageChange(currentPage);
Adam Cohen35e7e642011-07-17 14:47:18 -0700950 // We first fetch the currently visible CellLayoutChildren
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800951 CellLayout currentLayout = (CellLayout) mLauncher.getWorkspace().getChildAt(currentPage);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700952 ShortcutAndWidgetContainer boundingLayout = currentLayout.getShortcutsAndWidgets();
Adam Cohen35e7e642011-07-17 14:47:18 -0700953 Rect bounds = new Rect();
954 parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800955 // We reset the workspaces scroll
956 mLauncher.getWorkspace().resetFinalScrollForPageChange(currentPage);
Adam Cohen2801caf2011-05-13 20:57:39 -0700957
Adam Cohen35e7e642011-07-17 14:47:18 -0700958 // We need to bound the folder to the currently visible CellLayoutChildren
959 int left = Math.min(Math.max(bounds.left, centeredLeft),
960 bounds.left + bounds.width() - width);
961 int top = Math.min(Math.max(bounds.top, centeredTop),
962 bounds.top + bounds.height() - height);
Winson Chungaf40f202013-09-18 18:26:31 -0700963 if (grid.isPhone() && (grid.availableWidthPx - width) < grid.iconSizePx) {
964 // Center the folder if it is full (on phones only)
965 left = (grid.availableWidthPx - width) / 2;
966 } else if (width >= bounds.width()) {
967 // If the folder doesn't fit within the bounds, center it about the desired bounds
Adam Cohen35e7e642011-07-17 14:47:18 -0700968 left = bounds.left + (bounds.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700969 }
Adam Cohen35e7e642011-07-17 14:47:18 -0700970 if (height >= bounds.height()) {
971 top = bounds.top + (bounds.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700972 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700973
974 int folderPivotX = width / 2 + (centeredLeft - left);
975 int folderPivotY = height / 2 + (centeredTop - top);
976 setPivotX(folderPivotX);
977 setPivotY(folderPivotY);
Adam Cohen268c4752012-06-06 17:47:33 -0700978 mFolderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700979 (1.0f * folderPivotX / width));
Adam Cohen268c4752012-06-06 17:47:33 -0700980 mFolderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700981 (1.0f * folderPivotY / height));
Adam Cohen3bf84d32012-05-07 20:17:14 -0700982
Adam Cohen662b5982011-12-13 17:45:21 -0800983 lp.width = width;
984 lp.height = height;
985 lp.x = left;
986 lp.y = top;
Adam Cohen2801caf2011-05-13 20:57:39 -0700987 }
988
Adam Cohen268c4752012-06-06 17:47:33 -0700989 float getPivotXForIconAnimation() {
990 return mFolderIconPivotX;
991 }
992 float getPivotYForIconAnimation() {
993 return mFolderIconPivotY;
994 }
995
Adam Cohen2801caf2011-05-13 20:57:39 -0700996 private void setupContentForNumItems(int count) {
Adam Cohen4045eb72011-10-06 11:44:26 -0700997 setupContentDimensions(count);
Adam Cohen2801caf2011-05-13 20:57:39 -0700998
Adam Cohen8e776a62011-06-28 18:10:06 -0700999 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -07001000 if (lp == null) {
Adam Cohen8e776a62011-06-28 18:10:06 -07001001 lp = new DragLayer.LayoutParams(0, 0);
1002 lp.customPosition = true;
Adam Cohen2801caf2011-05-13 20:57:39 -07001003 setLayoutParams(lp);
1004 }
1005 centerAboutIcon();
1006 }
1007
Winson Chung892c74d2013-08-22 16:15:50 -07001008 private int getContentAreaHeight() {
1009 LauncherAppState app = LauncherAppState.getInstance();
1010 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1011 Rect workspacePadding = grid.getWorkspacePadding(grid.isLandscape ?
1012 CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
1013 int maxContentAreaHeight = grid.availableHeightPx -
Winson Chung892c74d2013-08-22 16:15:50 -07001014 workspacePadding.top - workspacePadding.bottom -
Winson Chung892c74d2013-08-22 16:15:50 -07001015 mFolderNameHeight;
Adam Cohen1960ea42013-11-12 11:33:14 +00001016 int height = Math.min(maxContentAreaHeight,
Winson Chung892c74d2013-08-22 16:15:50 -07001017 mContent.getDesiredHeight());
Adam Cohen1960ea42013-11-12 11:33:14 +00001018 return Math.max(height, MIN_CONTENT_DIMEN);
1019 }
1020
1021 private int getContentAreaWidth() {
1022 return Math.max(mContent.getDesiredWidth(), MIN_CONTENT_DIMEN);
Winson Chung892c74d2013-08-22 16:15:50 -07001023 }
1024
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001025 private int getFolderHeight() {
Winson Chung892c74d2013-08-22 16:15:50 -07001026 int height = getPaddingTop() + getPaddingBottom()
1027 + getContentAreaHeight() + mFolderNameHeight;
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001028 return height;
1029 }
Adam Cohen234c4cd2011-07-17 21:03:04 -07001030
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001031 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001032 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
1033 int height = getFolderHeight();
Adam Cohen1960ea42013-11-12 11:33:14 +00001034 int contentAreaWidthSpec = MeasureSpec.makeMeasureSpec(getContentAreaWidth(),
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001035 MeasureSpec.EXACTLY);
Winson Chung892c74d2013-08-22 16:15:50 -07001036 int contentAreaHeightSpec = MeasureSpec.makeMeasureSpec(getContentAreaHeight(),
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001037 MeasureSpec.EXACTLY);
Adam Cohen1960ea42013-11-12 11:33:14 +00001038
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08001039 if (LauncherAppState.isDisableAllApps()) {
Nilesh Agrawal5f7099a2014-01-02 15:54:57 -08001040 // Don't cap the height of the content to allow scrolling.
1041 mContent.setFixedSize(getContentAreaWidth(), mContent.getDesiredHeight());
1042 } else {
1043 mContent.setFixedSize(getContentAreaWidth(), getContentAreaHeight());
1044 }
1045
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001046 mScrollView.measure(contentAreaWidthSpec, contentAreaHeightSpec);
1047 mFolderName.measure(contentAreaWidthSpec,
Adam Cohen234c4cd2011-07-17 21:03:04 -07001048 MeasureSpec.makeMeasureSpec(mFolderNameHeight, MeasureSpec.EXACTLY));
1049 setMeasuredDimension(width, height);
1050 }
1051
Adam Cohen2801caf2011-05-13 20:57:39 -07001052 private void arrangeChildren(ArrayList<View> list) {
1053 int[] vacant = new int[2];
1054 if (list == null) {
1055 list = getItemsInReadingOrder();
1056 }
1057 mContent.removeAllViews();
1058
1059 for (int i = 0; i < list.size(); i++) {
1060 View v = list.get(i);
1061 mContent.getVacantCell(vacant, 1, 1);
1062 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) v.getLayoutParams();
1063 lp.cellX = vacant[0];
1064 lp.cellY = vacant[1];
1065 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohen2792a332011-09-26 21:09:47 -07001066 if (info.cellX != vacant[0] || info.cellY != vacant[1]) {
1067 info.cellX = vacant[0];
1068 info.cellY = vacant[1];
1069 LauncherModel.addOrMoveItemInDatabase(mLauncher, info, mInfo.id, 0,
1070 info.cellX, info.cellY);
1071 }
Adam Cohen2801caf2011-05-13 20:57:39 -07001072 boolean insert = false;
1073 mContent.addViewToCellLayout(v, insert ? 0 : -1, (int)info.id, lp, true);
1074 }
Adam Cohen7c693212011-05-18 15:26:57 -07001075 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -07001076 }
1077
Adam Cohena9cf38f2011-05-02 15:36:58 -07001078 public int getItemCount() {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001079 return mContent.getShortcutsAndWidgets().getChildCount();
Adam Cohena9cf38f2011-05-02 15:36:58 -07001080 }
1081
1082 public View getItemAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001083 return mContent.getShortcutsAndWidgets().getChildAt(index);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001084 }
1085
Adam Cohen2801caf2011-05-13 20:57:39 -07001086 private void onCloseComplete() {
Adam Cohen05e0f402011-08-01 12:12:49 -07001087 DragLayer parent = (DragLayer) getParent();
Michael Jurka5649c282012-06-18 10:33:21 -07001088 if (parent != null) {
1089 parent.removeView(this);
1090 }
Adam Cohen4554ee12011-08-03 16:13:21 -07001091 mDragController.removeDropTarget((DropTarget) this);
Adam Cohen05e0f402011-08-01 12:12:49 -07001092 clearFocus();
Adam Cohenac56cff2011-09-28 20:45:37 -07001093 mFolderIcon.requestFocus();
Adam Cohen05e0f402011-08-01 12:12:49 -07001094
Adam Cohen2801caf2011-05-13 20:57:39 -07001095 if (mRearrangeOnClose) {
1096 setupContentForNumItems(getItemCount());
1097 mRearrangeOnClose = false;
1098 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001099 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001100 if (!mDragInProgress && !mSuppressFolderDeletion) {
1101 replaceFolderWithFinalItem();
1102 } else if (mDragInProgress) {
1103 mDeleteFolderOnDropCompleted = true;
1104 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001105 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001106 mSuppressFolderDeletion = false;
Adam Cohenafb01ee2011-06-23 15:38:03 -07001107 }
1108
1109 private void replaceFolderWithFinalItem() {
Adam Cohenafb01ee2011-06-23 15:38:03 -07001110 // Add the last remaining child to the workspace in place of the folder
Adam Cohenfb91f302012-06-11 15:45:18 -07001111 Runnable onCompleteRunnable = new Runnable() {
1112 @Override
1113 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -07001114 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screenId);
Adam Cohenafb01ee2011-06-23 15:38:03 -07001115
Winson Chung33231f52013-12-09 16:57:45 -08001116 View child = null;
Adam Cohenfb91f302012-06-11 15:45:18 -07001117 // Move the item from the folder to the workspace, in the position of the folder
1118 if (getItemCount() == 1) {
1119 ShortcutInfo finalItem = mInfo.contents.get(0);
Adam Cohenc5e63f32012-07-12 16:16:57 -07001120 child = mLauncher.createShortcut(R.layout.application, cellLayout,
Adam Cohenfb91f302012-06-11 15:45:18 -07001121 finalItem);
1122 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -07001123 mInfo.screenId, mInfo.cellX, mInfo.cellY);
Adam Cohenfb91f302012-06-11 15:45:18 -07001124 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001125 if (getItemCount() <= 1) {
1126 // Remove the folder
1127 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
Dan Sandler0eb687f2014-01-10 13:24:55 -05001128 if (cellLayout != null) {
1129 // b/12446428 -- sometimes the cell layout has already gone away?
1130 cellLayout.removeView(mFolderIcon);
1131 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001132 if (mFolderIcon instanceof DropTarget) {
1133 mDragController.removeDropTarget((DropTarget) mFolderIcon);
1134 }
1135 mLauncher.removeFolder(mInfo);
1136 }
Adam Cohenc5e63f32012-07-12 16:16:57 -07001137 // We add the child after removing the folder to prevent both from existing at
Winson Chung0e6a7132013-08-23 12:55:10 -07001138 // the same time in the CellLayout. We need to add the new item with addInScreenFromBind()
1139 // to ensure that hotseat items are placed correctly.
Adam Cohenc5e63f32012-07-12 16:16:57 -07001140 if (child != null) {
Winson Chung0e6a7132013-08-23 12:55:10 -07001141 mLauncher.getWorkspace().addInScreenFromBind(child, mInfo.container, mInfo.screenId,
Adam Cohenc5e63f32012-07-12 16:16:57 -07001142 mInfo.cellX, mInfo.cellY, mInfo.spanX, mInfo.spanY);
1143 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001144 }
1145 };
1146 View finalChild = getItemAt(0);
1147 if (finalChild != null) {
1148 mFolderIcon.performDestroyAnimation(finalChild, onCompleteRunnable);
Winson Chung33231f52013-12-09 16:57:45 -08001149 } else {
1150 onCompleteRunnable.run();
Adam Cohenafb01ee2011-06-23 15:38:03 -07001151 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001152 mDestroyed = true;
1153 }
1154
1155 boolean isDestroyed() {
1156 return mDestroyed;
Adam Cohen2801caf2011-05-13 20:57:39 -07001157 }
1158
Adam Cohenac56cff2011-09-28 20:45:37 -07001159 // This method keeps track of the last item in the folder for the purposes
1160 // of keyboard focus
1161 private void updateTextViewFocus() {
1162 View lastChild = getItemAt(getItemCount() - 1);
1163 getItemAt(getItemCount() - 1);
1164 if (lastChild != null) {
1165 mFolderName.setNextFocusDownId(lastChild.getId());
1166 mFolderName.setNextFocusRightId(lastChild.getId());
1167 mFolderName.setNextFocusLeftId(lastChild.getId());
1168 mFolderName.setNextFocusUpId(lastChild.getId());
1169 }
1170 }
1171
Adam Cohenbfbfd262011-06-13 16:55:12 -07001172 public void onDrop(DragObject d) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001173 Runnable cleanUpRunnable = null;
1174
1175 // If we are coming from All Apps space, we need to remove the extra empty screen (which is
1176 // normally done in Workspace#onDropExternal, as well zoom back in and close the folder.
1177 if (d.dragSource != mLauncher.getWorkspace() && !(d.dragSource instanceof Folder)) {
1178 cleanUpRunnable = new Runnable() {
1179 @Override
1180 public void run() {
1181 mLauncher.getWorkspace().removeExtraEmptyScreen(false, new Runnable() {
1182 @Override
1183 public void run() {
1184 mLauncher.closeFolder();
1185 mLauncher.exitSpringLoadedDragModeDelayed(true,
1186 Launcher.EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT_FOLDER_CLOSE,
1187 null);
1188 }
1189 }, CLOSE_FOLDER_DELAY_MS, false);
1190 }
1191 };
Adam Cohenbfbfd262011-06-13 16:55:12 -07001192 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001193
1194 View currentDragView;
1195 ShortcutInfo si = mCurrentDragInfo;
1196 if (mIsExternalDrag) {
1197 si.cellX = mEmptyCell[0];
1198 si.cellY = mEmptyCell[1];
1199 currentDragView = createAndAddShortcut(si);
1200 } else {
1201 currentDragView = mCurrentDragView;
1202 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) currentDragView.getLayoutParams();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001203 si.cellX = lp.cellX = mEmptyCell[0];
1204 si.cellX = lp.cellY = mEmptyCell[1];
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001205 mContent.addViewToCellLayout(currentDragView, -1, (int) si.id, lp, true);
Adam Cohenbfbfd262011-06-13 16:55:12 -07001206 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001207
1208 if (d.dragView.hasDrawn()) {
1209
1210 // Temporarily reset the scale such that the animation target gets calculated correctly.
1211 float scaleX = getScaleX();
1212 float scaleY = getScaleY();
1213 setScaleX(1.0f);
1214 setScaleY(1.0f);
1215 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, currentDragView,
1216 cleanUpRunnable, null);
1217 setScaleX(scaleX);
1218 setScaleY(scaleY);
1219 } else {
1220 d.deferDragViewCleanupPostAnimation = false;
1221 currentDragView.setVisibility(VISIBLE);
1222 }
1223 mItemsInvalidated = true;
1224 setupContentDimensions(getItemCount());
1225
1226 // Actually move the item in the database if it was an external drag.
1227 if (mIsExternalDrag) {
1228 LauncherModel.addOrMoveItemInDatabase(
1229 mLauncher, si, mInfo.id, 0, si.cellX, si.cellY);
1230
1231 // We only need to update the locations if it doesn't get handled in #onDropCompleted.
1232 if (d.dragSource != this) {
1233 updateItemLocationsInDatabaseBatch();
1234 }
1235 mIsExternalDrag = false;
1236 }
1237
1238 // Temporarily suppress the listener, as we did all the work already here.
1239 mSuppressOnAdd = true;
1240 mInfo.add(si);
1241 mSuppressOnAdd = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001242 }
1243
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001244 // This is used so the item doesn't immediately appear in the folder when added. In one case
1245 // we need to create the illusion that the item isn't added back to the folder yet, to
1246 // to correspond to the animation of the icon back into the folder. This is
1247 public void hideItem(ShortcutInfo info) {
1248 View v = getViewForInfo(info);
1249 v.setVisibility(INVISIBLE);
1250 }
1251 public void showItem(ShortcutInfo info) {
1252 View v = getViewForInfo(info);
1253 v.setVisibility(VISIBLE);
1254 }
1255
Adam Cohenbfbfd262011-06-13 16:55:12 -07001256 public void onAdd(ShortcutInfo item) {
1257 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001258 // If the item was dropped onto this open folder, we have done the work associated
1259 // with adding the item to the folder, as indicated by mSuppressOnAdd being set
Adam Cohenbfbfd262011-06-13 16:55:12 -07001260 if (mSuppressOnAdd) return;
1261 if (!findAndSetEmptyCells(item)) {
1262 // The current layout is full, can we expand it?
1263 setupContentForNumItems(getItemCount() + 1);
1264 findAndSetEmptyCells(item);
1265 }
1266 createAndAddShortcut(item);
1267 LauncherModel.addOrMoveItemInDatabase(
1268 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
1269 }
1270
Adam Cohena9cf38f2011-05-02 15:36:58 -07001271 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -07001272 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001273 // If this item is being dragged from this open folder, we have already handled
1274 // the work associated with removing the item, so we don't have to do anything here.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001275 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001276 View v = getViewForInfo(item);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001277 mContent.removeView(v);
Adam Cohen2801caf2011-05-13 20:57:39 -07001278 if (mState == STATE_ANIMATING) {
1279 mRearrangeOnClose = true;
1280 } else {
1281 setupContentForNumItems(getItemCount());
1282 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001283 if (getItemCount() <= 1) {
1284 replaceFolderWithFinalItem();
1285 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001286 }
Adam Cohen7c693212011-05-18 15:26:57 -07001287
Adam Cohendf1e4e82011-06-24 15:57:39 -07001288 private View getViewForInfo(ShortcutInfo item) {
1289 for (int j = 0; j < mContent.getCountY(); j++) {
1290 for (int i = 0; i < mContent.getCountX(); i++) {
1291 View v = mContent.getChildAt(i, j);
1292 if (v.getTag() == item) {
1293 return v;
1294 }
1295 }
1296 }
1297 return null;
1298 }
1299
Adam Cohen76078c42011-06-09 15:06:52 -07001300 public void onItemsChanged() {
Adam Cohenac56cff2011-09-28 20:45:37 -07001301 updateTextViewFocus();
Adam Cohen76078c42011-06-09 15:06:52 -07001302 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001303
Adam Cohen76fc0852011-06-17 13:26:23 -07001304 public void onTitleChanged(CharSequence title) {
1305 }
Adam Cohen76078c42011-06-09 15:06:52 -07001306
Adam Cohen7c693212011-05-18 15:26:57 -07001307 public ArrayList<View> getItemsInReadingOrder() {
1308 if (mItemsInvalidated) {
1309 mItemsInReadingOrder.clear();
1310 for (int j = 0; j < mContent.getCountY(); j++) {
1311 for (int i = 0; i < mContent.getCountX(); i++) {
1312 View v = mContent.getChildAt(i, j);
1313 if (v != null) {
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001314 mItemsInReadingOrder.add(v);
Adam Cohen7c693212011-05-18 15:26:57 -07001315 }
1316 }
1317 }
1318 mItemsInvalidated = false;
1319 }
1320 return mItemsInReadingOrder;
1321 }
Adam Cohen8dfcba42011-07-07 16:38:18 -07001322
1323 public void getLocationInDragLayer(int[] loc) {
1324 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
1325 }
Adam Cohenea0818d2011-09-30 20:06:58 -07001326
1327 public void onFocusChange(View v, boolean hasFocus) {
1328 if (v == mFolderName && hasFocus) {
1329 startEditingFolderName();
1330 }
1331 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001332
1333 @Override
1334 public void getHitRectRelativeToDragLayer(Rect outRect) {
1335 getHitRect(outRect);
1336 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001337}