blob: 47c8a4a0fdec76fc4d72a9a777067553bdeb363d [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Adam Cohendf2cc412011-04-27 16:56:57 -070019import android.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
Adam Cohendf2cc412011-04-27 16:56:57 -070021import android.animation.ObjectAnimator;
22import android.animation.PropertyValuesHolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080023import android.content.Context;
Adam Cohen76fc0852011-06-17 13:26:23 -070024import android.content.res.Resources;
Winson Chung043f2af2012-03-01 16:09:54 -080025import android.graphics.PointF;
Romain Guyfb5411e2010-02-24 10:04:17 -080026import android.graphics.Rect;
Alan Viverette4cda5b72013-08-28 17:53:41 -070027import android.os.SystemClock;
Bjorn Bringertb66773c2013-09-25 16:12:24 +010028import android.support.v4.widget.AutoScrollHelper;
Adam Cohen7a14d0b2011-06-24 11:36:35 -070029import android.text.InputType;
Adam Cohene601a432011-07-26 21:51:30 -070030import android.text.Selection;
31import android.text.Spannable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.util.AttributeSet;
Adam Cohen3bf84d32012-05-07 20:17:14 -070033import android.util.Log;
Adam Cohen76fc0852011-06-17 13:26:23 -070034import android.view.ActionMode;
35import android.view.KeyEvent;
Adam Cohendf2cc412011-04-27 16:56:57 -070036import android.view.LayoutInflater;
Adam Cohen76fc0852011-06-17 13:26:23 -070037import android.view.Menu;
38import android.view.MenuItem;
Adam Cohen0c872ba2011-05-05 10:34:16 -070039import android.view.MotionEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.view.View;
Adam Cohen3371da02011-10-25 21:38:29 -070041import android.view.accessibility.AccessibilityEvent;
42import android.view.accessibility.AccessibilityManager;
Adam Cohen76fc0852011-06-17 13:26:23 -070043import android.view.inputmethod.EditorInfo;
44import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.widget.LinearLayout;
Adam Cohenf0f4eda2013-06-06 21:27:03 -070046import android.widget.ScrollView;
Adam Cohendf2cc412011-04-27 16:56:57 -070047import android.widget.TextView;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048
Daniel Sandler325dc232013-06-05 22:57:57 -040049import com.android.launcher3.FolderInfo.FolderListener;
Romain Guyedcce092010-03-04 13:03:17 -080050
Adam Cohenc0dcf592011-06-01 15:30:43 -070051import java.util.ArrayList;
Adam Cohen3bf84d32012-05-07 20:17:14 -070052import java.util.Collections;
53import java.util.Comparator;
Adam Cohenc0dcf592011-06-01 15:30:43 -070054
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055/**
56 * Represents a set of icons chosen by the user or generated by the system.
57 */
Adam Cohen8dfcba42011-07-07 16:38:18 -070058public class Folder extends LinearLayout implements DragSource, View.OnClickListener,
Adam Cohenea0818d2011-09-30 20:06:58 -070059 View.OnLongClickListener, DropTarget, FolderListener, TextView.OnEditorActionListener,
60 View.OnFocusChangeListener {
Adam Cohendf2cc412011-04-27 16:56:57 -070061 private static final String TAG = "Launcher.Folder";
62
Adam Cohen4eac29a2011-07-11 17:53:37 -070063 protected DragController mDragController;
64 protected Launcher mLauncher;
65 protected FolderInfo mInfo;
66
Adam Cohendf2cc412011-04-27 16:56:57 -070067 static final int STATE_NONE = -1;
68 static final int STATE_SMALL = 0;
69 static final int STATE_ANIMATING = 1;
70 static final int STATE_OPEN = 2;
71
Jorim Jaggi55bd9722014-01-16 15:30:42 -080072 private static final int CLOSE_FOLDER_DELAY_MS = 150;
73
Adam Cohendf2cc412011-04-27 16:56:57 -070074 private int mExpandDuration;
75 protected CellLayout mContent;
Adam Cohenf0f4eda2013-06-06 21:27:03 -070076 private ScrollView mScrollView;
Adam Cohendf2cc412011-04-27 16:56:57 -070077 private final LayoutInflater mInflater;
78 private final IconCache mIconCache;
79 private int mState = STATE_NONE;
Adam Cohenbfbfd262011-06-13 16:55:12 -070080 private static final int REORDER_ANIMATION_DURATION = 230;
Adam Cohenf0f4eda2013-06-06 21:27:03 -070081 private static final int REORDER_DELAY = 250;
Adam Cohen5d518fa2013-12-05 14:16:23 -080082 private static final int ON_EXIT_CLOSE_DELAY = 400;
Adam Cohen2801caf2011-05-13 20:57:39 -070083 private boolean mRearrangeOnClose = false;
84 private FolderIcon mFolderIcon;
85 private int mMaxCountX;
86 private int mMaxCountY;
Adam Cohen78dc83e2011-11-15 17:10:00 -080087 private int mMaxNumItems;
Adam Cohen7c693212011-05-18 15:26:57 -070088 private ArrayList<View> mItemsInReadingOrder = new ArrayList<View>();
89 boolean mItemsInvalidated = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -070090 private ShortcutInfo mCurrentDragInfo;
91 private View mCurrentDragView;
Jorim Jaggi55bd9722014-01-16 15:30:42 -080092 private boolean mIsExternalDrag;
Adam Cohenbfbfd262011-06-13 16:55:12 -070093 boolean mSuppressOnAdd = false;
94 private int[] mTargetCell = new int[2];
95 private int[] mPreviousTargetCell = new int[2];
96 private int[] mEmptyCell = new int[2];
97 private Alarm mReorderAlarm = new Alarm();
98 private Alarm mOnExitAlarm = new Alarm();
Adam Cohen76fc0852011-06-17 13:26:23 -070099 private int mFolderNameHeight;
Adam Cohen8e776a62011-06-28 18:10:06 -0700100 private Rect mTempRect = new Rect();
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700101 private boolean mDragInProgress = false;
102 private boolean mDeleteFolderOnDropCompleted = false;
103 private boolean mSuppressFolderDeletion = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700104 private boolean mItemAddedBackToSelfViaIcon = false;
Adam Cohenac56cff2011-09-28 20:45:37 -0700105 FolderEditText mFolderName;
Adam Cohen268c4752012-06-06 17:47:33 -0700106 private float mFolderIconPivotX;
107 private float mFolderIconPivotY;
Adam Cohen228da5a2011-07-27 22:23:47 -0700108
Adam Cohen76fc0852011-06-17 13:26:23 -0700109 private boolean mIsEditingName = false;
110 private InputMethodManager mInputMethodManager;
Adam Cohendf2cc412011-04-27 16:56:57 -0700111
Adam Cohena65beee2011-06-27 21:32:23 -0700112 private static String sDefaultFolderName;
113 private static String sHintText;
114
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700115 private int DRAG_MODE_NONE = 0;
116 private int DRAG_MODE_REORDER = 1;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700117 private int mDragMode = DRAG_MODE_NONE;
118
Adam Cohen1960ea42013-11-12 11:33:14 +0000119 // We avoid measuring the scroll view with a 0 width or height, as this
120 // results in CellLayout being measured as UNSPECIFIED, which it does
121 // not support.
122 private static final int MIN_CONTENT_DIMEN = 5;
123
Adam Cohenfb91f302012-06-11 15:45:18 -0700124 private boolean mDestroyed;
125
Alan Viverette4cda5b72013-08-28 17:53:41 -0700126 private AutoScrollHelper mAutoScrollHelper;
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700127
Michael Jurka1e2f4652013-07-08 18:03:46 -0700128 private Runnable mDeferredAction;
129 private boolean mDeferDropAfterUninstall;
130 private boolean mUninstallSuccessful;
131
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132 /**
133 * Used to inflate the Workspace from XML.
134 *
135 * @param context The application's context.
136 * @param attrs The attribtues set containing the Workspace's customization values.
137 */
138 public Folder(Context context, AttributeSet attrs) {
139 super(context, attrs);
Winson Chung5f8afe62013-08-12 16:19:28 -0700140
141 LauncherAppState app = LauncherAppState.getInstance();
142 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143 setAlwaysDrawnWithCacheEnabled(false);
Adam Cohendf2cc412011-04-27 16:56:57 -0700144 mInflater = LayoutInflater.from(context);
Winson Chung5f8afe62013-08-12 16:19:28 -0700145 mIconCache = app.getIconCache();
Adam Cohen78dc83e2011-11-15 17:10:00 -0800146
147 Resources res = getResources();
Michael Jurka414300a2013-08-27 15:42:35 +0200148 mMaxCountX = (int) grid.numColumns;
Nilesh Agrawal5f7099a2014-01-02 15:54:57 -0800149 // Allow scrolling folders when DISABLE_ALL_APPS is true.
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800150 if (LauncherAppState.isDisableAllApps()) {
Nilesh Agrawal5f7099a2014-01-02 15:54:57 -0800151 mMaxCountY = mMaxNumItems = Integer.MAX_VALUE;
152 } else {
153 mMaxCountY = (int) grid.numRows;
154 mMaxNumItems = mMaxCountX * mMaxCountY;
155 }
Adam Cohen76fc0852011-06-17 13:26:23 -0700156
157 mInputMethodManager = (InputMethodManager)
Michael Jurka8b805b12012-04-18 14:23:14 -0700158 getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
Adam Cohen76fc0852011-06-17 13:26:23 -0700159
Adam Cohen76fc0852011-06-17 13:26:23 -0700160 mExpandDuration = res.getInteger(R.integer.config_folderAnimDuration);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700161
162 if (sDefaultFolderName == null) {
163 sDefaultFolderName = res.getString(R.string.folder_name);
164 }
Adam Cohena65beee2011-06-27 21:32:23 -0700165 if (sHintText == null) {
166 sHintText = res.getString(R.string.folder_hint_text);
167 }
Adam Cohen4eac29a2011-07-11 17:53:37 -0700168 mLauncher = (Launcher) context;
Adam Cohenac56cff2011-09-28 20:45:37 -0700169 // We need this view to be focusable in touch mode so that when text editing of the folder
170 // name is complete, we have something to focus on, thus hiding the cursor and giving
171 // reliable behvior when clicking the text field (since it will always gain focus on click).
172 setFocusableInTouchMode(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173 }
174
175 @Override
176 protected void onFinishInflate() {
177 super.onFinishInflate();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700178 mScrollView = (ScrollView) findViewById(R.id.scroll_view);
Adam Cohendf2cc412011-04-27 16:56:57 -0700179 mContent = (CellLayout) findViewById(R.id.folder_content);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700180
Winson Chung5f8afe62013-08-12 16:19:28 -0700181 LauncherAppState app = LauncherAppState.getInstance();
182 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
183
Winson Chung5f8afe62013-08-12 16:19:28 -0700184 mContent.setCellDimensions(grid.folderCellWidthPx, grid.folderCellHeightPx);
Adam Cohen2801caf2011-05-13 20:57:39 -0700185 mContent.setGridSize(0, 0);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700186 mContent.getShortcutsAndWidgets().setMotionEventSplittingEnabled(false);
Adam Cohen2374abf2013-04-16 14:56:57 -0700187 mContent.setInvertIfRtl(true);
Adam Cohenac56cff2011-09-28 20:45:37 -0700188 mFolderName = (FolderEditText) findViewById(R.id.folder_name);
189 mFolderName.setFolder(this);
Adam Cohenea0818d2011-09-30 20:06:58 -0700190 mFolderName.setOnFocusChangeListener(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700191
192 // We find out how tall the text view wants to be (it is set to wrap_content), so that
193 // we can allocate the appropriate amount of space for it.
194 int measureSpec = MeasureSpec.UNSPECIFIED;
195 mFolderName.measure(measureSpec, measureSpec);
196 mFolderNameHeight = mFolderName.getMeasuredHeight();
197
198 // We disable action mode for now since it messes up the view on phones
199 mFolderName.setCustomSelectionActionModeCallback(mActionModeCallback);
Adam Cohen76fc0852011-06-17 13:26:23 -0700200 mFolderName.setOnEditorActionListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700201 mFolderName.setSelectAllOnFocus(true);
Adam Cohen7a14d0b2011-06-24 11:36:35 -0700202 mFolderName.setInputType(mFolderName.getInputType() |
203 InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
Alan Viverette4cda5b72013-08-28 17:53:41 -0700204 mAutoScrollHelper = new FolderAutoScrollHelper(mScrollView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800205 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700206
Adam Cohen76fc0852011-06-17 13:26:23 -0700207 private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
208 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
209 return false;
210 }
211
212 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
213 return false;
214 }
215
216 public void onDestroyActionMode(ActionMode mode) {
217 }
218
219 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
220 return false;
221 }
222 };
223
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800224 public void onClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700225 Object tag = v.getTag();
226 if (tag instanceof ShortcutInfo) {
Adam Cohenb5fe60c2013-06-06 22:03:51 -0700227 mLauncher.onClick(v);
Adam Cohendf2cc412011-04-27 16:56:57 -0700228 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800229 }
230
231 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -0700232 // Return if global dragging is not enabled
233 if (!mLauncher.isDraggingEnabled()) return true;
234
Adam Cohendf2cc412011-04-27 16:56:57 -0700235 Object tag = v.getTag();
236 if (tag instanceof ShortcutInfo) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700237 ShortcutInfo item = (ShortcutInfo) tag;
238 if (!v.isInTouchMode()) {
239 return false;
240 }
241
Winson Chunga6945242014-01-08 14:04:34 -0800242 mLauncher.getLauncherClings().dismissFolderCling(null);
Winson Chung7d7541e2011-09-16 20:14:36 -0700243
Adam Cohendf2cc412011-04-27 16:56:57 -0700244 mLauncher.getWorkspace().onDragStartedWithItem(v);
Adam Cohenac8c8762011-07-13 11:15:27 -0700245 mLauncher.getWorkspace().beginDragShared(v, this);
Adam Cohen76078c42011-06-09 15:06:52 -0700246
247 mCurrentDragInfo = item;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700248 mEmptyCell[0] = item.cellX;
249 mEmptyCell[1] = item.cellY;
250 mCurrentDragView = v;
Adam Cohenfc53cd22011-07-20 15:45:11 -0700251
252 mContent.removeView(mCurrentDragView);
253 mInfo.remove(mCurrentDragInfo);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700254 mDragInProgress = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700255 mItemAddedBackToSelfViaIcon = false;
Adam Cohendf2cc412011-04-27 16:56:57 -0700256 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800257 return true;
258 }
259
Adam Cohen76fc0852011-06-17 13:26:23 -0700260 public boolean isEditingName() {
261 return mIsEditingName;
262 }
263
264 public void startEditingFolderName() {
Adam Cohena65beee2011-06-27 21:32:23 -0700265 mFolderName.setHint("");
Adam Cohen76fc0852011-06-17 13:26:23 -0700266 mIsEditingName = true;
267 }
268
269 public void dismissEditingName() {
270 mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
271 doneEditingFolderName(true);
272 }
273
274 public void doneEditingFolderName(boolean commit) {
Adam Cohena65beee2011-06-27 21:32:23 -0700275 mFolderName.setHint(sHintText);
Adam Cohen1df26a32011-08-12 16:15:23 -0700276 // Convert to a string here to ensure that no other state associated with the text field
277 // gets saved.
Adam Cohen3371da02011-10-25 21:38:29 -0700278 String newTitle = mFolderName.getText().toString();
279 mInfo.setTitle(newTitle);
Adam Cohen76fc0852011-06-17 13:26:23 -0700280 LauncherModel.updateItemInDatabase(mLauncher, mInfo);
Adam Cohenac56cff2011-09-28 20:45:37 -0700281
Adam Cohen3371da02011-10-25 21:38:29 -0700282 if (commit) {
283 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700284 String.format(getContext().getString(R.string.folder_renamed), newTitle));
Adam Cohen3371da02011-10-25 21:38:29 -0700285 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700286 // In order to clear the focus from the text field, we set the focus on ourself. This
287 // ensures that every time the field is clicked, focus is gained, giving reliable behavior.
288 requestFocus();
289
Adam Cohene601a432011-07-26 21:51:30 -0700290 Selection.setSelection((Spannable) mFolderName.getText(), 0, 0);
Adam Cohen76fc0852011-06-17 13:26:23 -0700291 mIsEditingName = false;
292 }
293
294 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
295 if (actionId == EditorInfo.IME_ACTION_DONE) {
296 dismissEditingName();
297 return true;
298 }
299 return false;
300 }
301
302 public View getEditTextRegion() {
303 return mFolderName;
304 }
305
Anjali Koppalf05545f2014-03-10 19:18:43 -0700306 public CellLayout getContent() {
307 return mContent;
308 }
309
Adam Cohen0c872ba2011-05-05 10:34:16 -0700310 /**
311 * We need to handle touch events to prevent them from falling through to the workspace below.
312 */
313 @Override
314 public boolean onTouchEvent(MotionEvent ev) {
315 return true;
316 }
317
Joe Onorato00acb122009-08-04 16:04:30 -0400318 public void setDragController(DragController dragController) {
319 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800320 }
321
Adam Cohen2801caf2011-05-13 20:57:39 -0700322 void setFolderIcon(FolderIcon icon) {
323 mFolderIcon = icon;
324 }
325
Adam Cohen3371da02011-10-25 21:38:29 -0700326 @Override
327 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
328 // When the folder gets focus, we don't want to announce the list of items.
329 return true;
330 }
331
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800332 /**
333 * @return the FolderInfo object associated with this folder
334 */
335 FolderInfo getInfo() {
336 return mInfo;
337 }
338
Adam Cohen3bf84d32012-05-07 20:17:14 -0700339 private class GridComparator implements Comparator<ShortcutInfo> {
340 int mNumCols;
341 public GridComparator(int numCols) {
342 mNumCols = numCols;
343 }
344
345 @Override
346 public int compare(ShortcutInfo lhs, ShortcutInfo rhs) {
347 int lhIndex = lhs.cellY * mNumCols + lhs.cellX;
348 int rhIndex = rhs.cellY * mNumCols + rhs.cellX;
349 return (lhIndex - rhIndex);
350 }
Adam Cohen3bf84d32012-05-07 20:17:14 -0700351 }
352
353 private void placeInReadingOrder(ArrayList<ShortcutInfo> items) {
354 int maxX = 0;
355 int count = items.size();
356 for (int i = 0; i < count; i++) {
357 ShortcutInfo item = items.get(i);
358 if (item.cellX > maxX) {
359 maxX = item.cellX;
360 }
361 }
Adam Cohen691a5792012-05-11 14:27:30 -0700362
363 GridComparator gridComparator = new GridComparator(maxX + 1);
Adam Cohen3bf84d32012-05-07 20:17:14 -0700364 Collections.sort(items, gridComparator);
365 final int countX = mContent.getCountX();
366 for (int i = 0; i < count; i++) {
367 int x = i % countX;
368 int y = i / countX;
369 ShortcutInfo item = items.get(i);
370 item.cellX = x;
371 item.cellY = y;
372 }
373 }
374
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800375 void bind(FolderInfo info) {
376 mInfo = info;
Adam Cohendf2cc412011-04-27 16:56:57 -0700377 ArrayList<ShortcutInfo> children = info.contents;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700378 ArrayList<ShortcutInfo> overflow = new ArrayList<ShortcutInfo>();
Adam Cohen7c693212011-05-18 15:26:57 -0700379 setupContentForNumItems(children.size());
Adam Cohen3bf84d32012-05-07 20:17:14 -0700380 placeInReadingOrder(children);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700381 int count = 0;
Adam Cohendf2cc412011-04-27 16:56:57 -0700382 for (int i = 0; i < children.size(); i++) {
383 ShortcutInfo child = (ShortcutInfo) children.get(i);
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800384 if (createAndAddShortcut(child) == null) {
Adam Cohenc508b2d2011-06-28 14:41:44 -0700385 overflow.add(child);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700386 } else {
387 count++;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700388 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700389 }
Adam Cohenc508b2d2011-06-28 14:41:44 -0700390
Adam Cohen0057bbc2011-08-12 18:30:51 -0700391 // We rearrange the items in case there are any empty gaps
392 setupContentForNumItems(count);
393
Jason Monk4ff73882014-04-24 16:48:07 -0400394 // If our folder has too many items we prune them from the list. This is an issue
Adam Cohenc508b2d2011-06-28 14:41:44 -0700395 // when upgrading from the old Folders implementation which could contain an unlimited
396 // number of items.
397 for (ShortcutInfo item: overflow) {
398 mInfo.remove(item);
399 LauncherModel.deleteItemFromDatabase(mLauncher, item);
400 }
401
Adam Cohen4dbe6d92011-05-18 17:14:15 -0700402 mItemsInvalidated = true;
Adam Cohenac56cff2011-09-28 20:45:37 -0700403 updateTextViewFocus();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700404 mInfo.addListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700405
Adam Cohenafb01ee2011-06-23 15:38:03 -0700406 if (!sDefaultFolderName.contentEquals(mInfo.title)) {
Adam Cohen4ef610f2011-06-21 22:37:36 -0700407 mFolderName.setText(mInfo.title);
408 } else {
409 mFolderName.setText("");
410 }
Adam Cohen691a5792012-05-11 14:27:30 -0700411 updateItemLocationsInDatabase();
Winson Chung33231f52013-12-09 16:57:45 -0800412
413 // In case any children didn't come across during loading, clean up the folder accordingly
414 mFolderIcon.post(new Runnable() {
415 public void run() {
416 if (getItemCount() <= 1) {
417 replaceFolderWithFinalItem();
418 }
419 }
420 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700421 }
422
423 /**
424 * Creates a new UserFolder, inflated from R.layout.user_folder.
425 *
426 * @param context The application's context.
427 *
428 * @return A new UserFolder.
429 */
430 static Folder fromXml(Context context) {
431 return (Folder) LayoutInflater.from(context).inflate(R.layout.user_folder, null);
432 }
433
434 /**
435 * This method is intended to make the UserFolder to be visually identical in size and position
436 * to its associated FolderIcon. This allows for a seamless transition into the expanded state.
437 */
438 private void positionAndSizeAsIcon() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700439 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800440 setScaleX(0.8f);
441 setScaleY(0.8f);
442 setAlpha(0f);
Adam Cohendf2cc412011-04-27 16:56:57 -0700443 mState = STATE_SMALL;
444 }
445
446 public void animateOpen() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700447 positionAndSizeAsIcon();
448
Adam Cohen8e776a62011-06-28 18:10:06 -0700449 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen2801caf2011-05-13 20:57:39 -0700450 centerAboutIcon();
Adam Cohen662b5982011-12-13 17:45:21 -0800451 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1);
452 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
453 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
Michael Jurka032e6ba2013-04-22 15:08:42 +0200454 final ObjectAnimator oa =
Michael Jurka2ecf9952012-06-18 12:52:28 -0700455 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohen6441de02011-12-14 14:25:32 -0800456
Adam Cohen2801caf2011-05-13 20:57:39 -0700457 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700458 @Override
459 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700460 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700461 String.format(getContext().getString(R.string.folder_opened),
Adam Cohen3371da02011-10-25 21:38:29 -0700462 mContent.getCountX(), mContent.getCountY()));
Adam Cohendf2cc412011-04-27 16:56:57 -0700463 mState = STATE_ANIMATING;
464 }
465 @Override
466 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700467 mState = STATE_OPEN;
Michael Jurka0121c3e2012-05-31 08:36:04 -0700468 setLayerType(LAYER_TYPE_NONE, null);
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800469
470 // Only show cling if we are not in the middle of a drag - this would be quite jarring.
471 if (!mDragController.isDragging()) {
472 Cling cling = mLauncher.getLauncherClings().showFoldersCling();
473 if (cling != null) {
474 cling.bringScrimToFront();
475 bringToFront();
476 cling.bringToFront();
477 }
Winson Chung7a74ac92011-09-20 17:43:51 -0700478 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700479 setFocusOnFirstChild();
Adam Cohendf2cc412011-04-27 16:56:57 -0700480 }
481 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700482 oa.setDuration(mExpandDuration);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700483 setLayerType(LAYER_TYPE_HARDWARE, null);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100484 oa.start();
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800485
486 // Make sure the folder picks up the last drag move even if the finger doesn't move.
487 if (mDragController.isDragging()) {
488 mDragController.forceTouchMove();
489 }
490 }
491
492 public void beginExternalDrag(ShortcutInfo item) {
493 setupContentForNumItems(getItemCount() + 1);
494 findAndSetEmptyCells(item);
495
496 mCurrentDragInfo = item;
497 mEmptyCell[0] = item.cellX;
498 mEmptyCell[1] = item.cellY;
499 mIsExternalDrag = true;
500
501 mDragInProgress = true;
Adam Cohendf2cc412011-04-27 16:56:57 -0700502 }
503
Adam Cohen3371da02011-10-25 21:38:29 -0700504 private void sendCustomAccessibilityEvent(int type, String text) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700505 AccessibilityManager accessibilityManager = (AccessibilityManager)
506 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
507 if (accessibilityManager.isEnabled()) {
Adam Cohen3371da02011-10-25 21:38:29 -0700508 AccessibilityEvent event = AccessibilityEvent.obtain(type);
509 onInitializeAccessibilityEvent(event);
510 event.getText().add(text);
Michael Jurka8b805b12012-04-18 14:23:14 -0700511 accessibilityManager.sendAccessibilityEvent(event);
Adam Cohen3371da02011-10-25 21:38:29 -0700512 }
513 }
514
Adam Cohenac56cff2011-09-28 20:45:37 -0700515 private void setFocusOnFirstChild() {
516 View firstChild = mContent.getChildAt(0, 0);
517 if (firstChild != null) {
518 firstChild.requestFocus();
519 }
520 }
521
Adam Cohendf2cc412011-04-27 16:56:57 -0700522 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700523 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800524 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
525 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
526 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
Michael Jurka032e6ba2013-04-22 15:08:42 +0200527 final ObjectAnimator oa =
Michael Jurka2ecf9952012-06-18 12:52:28 -0700528 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohendf2cc412011-04-27 16:56:57 -0700529
Adam Cohen2801caf2011-05-13 20:57:39 -0700530 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700531 @Override
532 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700533 onCloseComplete();
Michael Jurka0121c3e2012-05-31 08:36:04 -0700534 setLayerType(LAYER_TYPE_NONE, null);
Adam Cohen2801caf2011-05-13 20:57:39 -0700535 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700536 }
537 @Override
538 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700539 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700540 getContext().getString(R.string.folder_closed));
Adam Cohendf2cc412011-04-27 16:56:57 -0700541 mState = STATE_ANIMATING;
542 }
543 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700544 oa.setDuration(mExpandDuration);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700545 setLayerType(LAYER_TYPE_HARDWARE, null);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100546 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700547 }
548
Adam Cohencb3382b2011-05-24 14:07:08 -0700549 public boolean acceptDrop(DragObject d) {
550 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700551 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700552 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
553 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
554 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700555 }
556
Adam Cohendf2cc412011-04-27 16:56:57 -0700557 protected boolean findAndSetEmptyCells(ShortcutInfo item) {
558 int[] emptyCell = new int[2];
559 if (mContent.findCellForSpan(emptyCell, item.spanX, item.spanY)) {
560 item.cellX = emptyCell[0];
561 item.cellY = emptyCell[1];
Adam Cohendf2cc412011-04-27 16:56:57 -0700562 return true;
563 } else {
564 return false;
565 }
566 }
567
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800568 protected View createAndAddShortcut(ShortcutInfo item) {
Adam Cohen96bb7982014-07-07 11:58:56 -0700569 final TextView textView =
570 (TextView) mInflater.inflate(R.layout.folder_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);
Kenny Guyc2bd8102014-06-30 12:30:31 +0100574 if (item.contentDescription != null) {
575 textView.setContentDescription(item.contentDescription);
576 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700577 textView.setTag(item);
Adam Cohendf2cc412011-04-27 16:56:57 -0700578 textView.setOnClickListener(this);
579 textView.setOnLongClickListener(this);
580
Adam Cohenc508b2d2011-06-28 14:41:44 -0700581 // We need to check here to verify that the given item's location isn't already occupied
Adam Cohen3bf84d32012-05-07 20:17:14 -0700582 // by another item.
Adam Cohen0057bbc2011-08-12 18:30:51 -0700583 if (mContent.getChildAt(item.cellX, item.cellY) != null || item.cellX < 0 || item.cellY < 0
584 || item.cellX >= mContent.getCountX() || item.cellY >= mContent.getCountY()) {
Jason Monk4ff73882014-04-24 16:48:07 -0400585 // This shouldn't happen, log it.
Adam Cohen3bf84d32012-05-07 20:17:14 -0700586 Log.e(TAG, "Folder order not properly persisted during bind");
Adam Cohenc508b2d2011-06-28 14:41:44 -0700587 if (!findAndSetEmptyCells(item)) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800588 return null;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700589 }
590 }
591
Adam Cohendf2cc412011-04-27 16:56:57 -0700592 CellLayout.LayoutParams lp =
593 new CellLayout.LayoutParams(item.cellX, item.cellY, item.spanX, item.spanY);
594 boolean insert = false;
Adam Cohenac56cff2011-09-28 20:45:37 -0700595 textView.setOnKeyListener(new FolderKeyEventListener());
Adam Cohendf2cc412011-04-27 16:56:57 -0700596 mContent.addViewToCellLayout(textView, insert ? 0 : -1, (int)item.id, lp, true);
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800597 return textView;
Adam Cohendf2cc412011-04-27 16:56:57 -0700598 }
599
Adam Cohencb3382b2011-05-24 14:07:08 -0700600 public void onDragEnter(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700601 mPreviousTargetCell[0] = -1;
602 mPreviousTargetCell[1] = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700603 mOnExitAlarm.cancelAlarm();
604 }
605
606 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
607 public void onAlarm(Alarm alarm) {
608 realTimeReorder(mEmptyCell, mTargetCell);
609 }
610 };
611
612 boolean readingOrderGreaterThan(int[] v1, int[] v2) {
613 if (v1[1] > v2[1] || (v1[1] == v2[1] && v1[0] > v2[0])) {
614 return true;
615 } else {
616 return false;
617 }
618 }
619
620 private void realTimeReorder(int[] empty, int[] target) {
621 boolean wrap;
622 int startX;
623 int endX;
624 int startY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700625 int delay = 0;
626 float delayAmount = 30;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700627 if (readingOrderGreaterThan(target, empty)) {
628 wrap = empty[0] >= mContent.getCountX() - 1;
629 startY = wrap ? empty[1] + 1 : empty[1];
630 for (int y = startY; y <= target[1]; y++) {
631 startX = y == empty[1] ? empty[0] + 1 : 0;
632 endX = y < target[1] ? mContent.getCountX() - 1 : target[0];
633 for (int x = startX; x <= endX; x++) {
634 View v = mContent.getChildAt(x,y);
635 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen482ed822012-03-02 14:15:13 -0800636 REORDER_ANIMATION_DURATION, delay, true, true)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700637 empty[0] = x;
638 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700639 delay += delayAmount;
640 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700641 }
642 }
643 }
644 } else {
645 wrap = empty[0] == 0;
646 startY = wrap ? empty[1] - 1 : empty[1];
647 for (int y = startY; y >= target[1]; y--) {
648 startX = y == empty[1] ? empty[0] - 1 : mContent.getCountX() - 1;
649 endX = y > target[1] ? 0 : target[0];
650 for (int x = startX; x >= endX; x--) {
651 View v = mContent.getChildAt(x,y);
652 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen482ed822012-03-02 14:15:13 -0800653 REORDER_ANIMATION_DURATION, delay, true, true)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700654 empty[0] = x;
655 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700656 delay += delayAmount;
657 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700658 }
659 }
660 }
661 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700662 }
663
Adam Cohen2374abf2013-04-16 14:56:57 -0700664 public boolean isLayoutRtl() {
665 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
666 }
667
Adam Cohencb3382b2011-05-24 14:07:08 -0700668 public void onDragOver(DragObject d) {
Alan Viverette4cda5b72013-08-28 17:53:41 -0700669 final DragView dragView = d.dragView;
670 final int scrollOffset = mScrollView.getScrollY();
671 final float[] r = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, dragView, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700672 r[0] -= getPaddingLeft();
673 r[1] -= getPaddingTop();
674
Alan Viverette4cda5b72013-08-28 17:53:41 -0700675 final long downTime = SystemClock.uptimeMillis();
676 final MotionEvent translatedEv = MotionEvent.obtain(
677 downTime, downTime, MotionEvent.ACTION_MOVE, d.x, d.y, 0);
678
679 if (!mAutoScrollHelper.isEnabled()) {
680 mAutoScrollHelper.setEnabled(true);
681 }
682
683 final boolean handled = mAutoScrollHelper.onTouch(this, translatedEv);
684 translatedEv.recycle();
685
686 if (handled) {
687 mReorderAlarm.cancelAlarm();
688 } else {
689 mTargetCell = mContent.findNearestArea(
690 (int) r[0], (int) r[1] + scrollOffset, 1, 1, mTargetCell);
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700691 if (isLayoutRtl()) {
692 mTargetCell[0] = mContent.getCountX() - mTargetCell[0] - 1;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700693 }
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700694 if (mTargetCell[0] != mPreviousTargetCell[0]
695 || mTargetCell[1] != mPreviousTargetCell[1]) {
696 mReorderAlarm.cancelAlarm();
697 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
698 mReorderAlarm.setAlarm(REORDER_DELAY);
699 mPreviousTargetCell[0] = mTargetCell[0];
700 mPreviousTargetCell[1] = mTargetCell[1];
701 mDragMode = DRAG_MODE_REORDER;
702 } else {
703 mDragMode = DRAG_MODE_NONE;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700704 }
Adam Cohenbfbfd262011-06-13 16:55:12 -0700705 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700706 }
707
Adam Cohenbfbfd262011-06-13 16:55:12 -0700708 // This is used to compute the visual center of the dragView. The idea is that
709 // the visual center represents the user's interpretation of where the item is, and hence
710 // is the appropriate point to use when determining drop location.
711 private float[] getDragViewVisualCenter(int x, int y, int xOffset, int yOffset,
712 DragView dragView, float[] recycle) {
713 float res[];
714 if (recycle == null) {
715 res = new float[2];
716 } else {
717 res = recycle;
718 }
719
720 // These represent the visual top and left of drag view if a dragRect was provided.
721 // If a dragRect was not provided, then they correspond to the actual view left and
722 // top, as the dragRect is in that case taken to be the entire dragView.
723 // R.dimen.dragViewOffsetY.
724 int left = x - xOffset;
725 int top = y - yOffset;
726
727 // In order to find the visual center, we shift by half the dragRect
728 res[0] = left + dragView.getDragRegion().width() / 2;
729 res[1] = top + dragView.getDragRegion().height() / 2;
730
731 return res;
732 }
733
734 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
735 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700736 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700737 }
738 };
739
Adam Cohen95bb8002011-07-03 23:40:28 -0700740 public void completeDragExit() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700741 mLauncher.closeFolder();
742 mCurrentDragInfo = null;
743 mCurrentDragView = null;
744 mSuppressOnAdd = false;
745 mRearrangeOnClose = true;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800746 mIsExternalDrag = false;
Adam Cohen3e8f8112011-07-02 18:03:00 -0700747 }
748
Adam Cohencb3382b2011-05-24 14:07:08 -0700749 public void onDragExit(DragObject d) {
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700750 // Exiting folder; stop the auto scroller.
Alan Viverette4cda5b72013-08-28 17:53:41 -0700751 mAutoScrollHelper.setEnabled(false);
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700752 // We only close the folder if this is a true drag exit, ie. not because
753 // a drop has occurred above the folder.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700754 if (!d.dragComplete) {
755 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
756 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
757 }
758 mReorderAlarm.cancelAlarm();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700759 mDragMode = DRAG_MODE_NONE;
Adam Cohen2801caf2011-05-13 20:57:39 -0700760 }
761
Michael Jurka1e2f4652013-07-08 18:03:46 -0700762 public void onDropCompleted(final View target, final DragObject d,
763 final boolean isFlingToDelete, final boolean success) {
764 if (mDeferDropAfterUninstall) {
Michael Jurkaf3007582013-08-21 14:33:57 +0200765 Log.d(TAG, "Deferred handling drop because waiting for uninstall.");
Michael Jurka1e2f4652013-07-08 18:03:46 -0700766 mDeferredAction = new Runnable() {
767 public void run() {
768 onDropCompleted(target, d, isFlingToDelete, success);
769 mDeferredAction = null;
770 }
771 };
772 return;
773 }
774
775 boolean beingCalledAfterUninstall = mDeferredAction != null;
776 boolean successfulDrop =
777 success && (!beingCalledAfterUninstall || mUninstallSuccessful);
Winson Chung5f8afe62013-08-12 16:19:28 -0700778
Michael Jurka1e2f4652013-07-08 18:03:46 -0700779 if (successfulDrop) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800780 if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon && target != this) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700781 replaceFolderWithFinalItem();
782 }
783 } else {
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700784 setupContentForNumItems(getItemCount());
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700785 // The drag failed, we need to return the item to the folder
786 mFolderIcon.onDrop(d);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700787 }
788
789 if (target != this) {
790 if (mOnExitAlarm.alarmPending()) {
791 mOnExitAlarm.cancelAlarm();
Michael Jurka54554252013-08-01 12:52:23 +0200792 if (!successfulDrop) {
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700793 mSuppressFolderDeletion = true;
794 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700795 completeDragExit();
796 }
Adam Cohen9c58d822013-12-13 17:18:10 -0800797 }
798
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700799 mDeleteFolderOnDropCompleted = false;
800 mDragInProgress = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700801 mItemAddedBackToSelfViaIcon = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700802 mCurrentDragInfo = null;
803 mCurrentDragView = null;
804 mSuppressOnAdd = false;
Adam Cohen4045eb72011-10-06 11:44:26 -0700805
806 // Reordering may have occured, and we need to save the new item locations. We do this once
807 // at the end to prevent unnecessary database operations.
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700808 updateItemLocationsInDatabaseBatch();
Adam Cohen4045eb72011-10-06 11:44:26 -0700809 }
810
Michael Jurka1e2f4652013-07-08 18:03:46 -0700811 public void deferCompleteDropAfterUninstallActivity() {
812 mDeferDropAfterUninstall = true;
813 }
814
815 public void onUninstallActivityReturned(boolean success) {
816 mDeferDropAfterUninstall = false;
817 mUninstallSuccessful = success;
818 if (mDeferredAction != null) {
819 mDeferredAction.run();
820 }
821 }
822
Winson Chunga48487a2012-03-20 16:19:37 -0700823 @Override
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800824 public float getIntrinsicIconScaleFactor() {
825 return 1f;
826 }
827
828 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800829 public boolean supportsFlingToDelete() {
830 return true;
831 }
832
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000833 @Override
834 public boolean supportsAppInfoDropTarget() {
835 return false;
836 }
837
838 @Override
839 public boolean supportsDeleteDropTarget() {
840 return true;
841 }
842
Winson Chunga48487a2012-03-20 16:19:37 -0700843 public void onFlingToDelete(DragObject d, int x, int y, PointF vec) {
844 // Do nothing
845 }
846
847 @Override
848 public void onFlingToDeleteCompleted() {
849 // Do nothing
850 }
851
Adam Cohen4045eb72011-10-06 11:44:26 -0700852 private void updateItemLocationsInDatabase() {
853 ArrayList<View> list = getItemsInReadingOrder();
854 for (int i = 0; i < list.size(); i++) {
855 View v = list.get(i);
856 ItemInfo info = (ItemInfo) v.getTag();
857 LauncherModel.moveItemInDatabase(mLauncher, info, mInfo.id, 0,
Winson Chung33231f52013-12-09 16:57:45 -0800858 info.cellX, info.cellY);
Adam Cohen4045eb72011-10-06 11:44:26 -0700859 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700860 }
Adam Cohen228da5a2011-07-27 22:23:47 -0700861
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700862 private void updateItemLocationsInDatabaseBatch() {
863 ArrayList<View> list = getItemsInReadingOrder();
864 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
865 for (int i = 0; i < list.size(); i++) {
866 View v = list.get(i);
867 ItemInfo info = (ItemInfo) v.getTag();
868 items.add(info);
869 }
870
871 LauncherModel.moveItemsInDatabase(mLauncher, items, mInfo.id, 0);
872 }
873
Adam Cohene25af792013-06-06 23:08:25 -0700874 public void addItemLocationsInDatabase() {
875 ArrayList<View> list = getItemsInReadingOrder();
876 for (int i = 0; i < list.size(); i++) {
877 View v = list.get(i);
878 ItemInfo info = (ItemInfo) v.getTag();
879 LauncherModel.addItemToDatabase(mLauncher, info, mInfo.id, 0,
880 info.cellX, info.cellY, false);
881 }
882 }
883
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700884 public void notifyDrop() {
885 if (mDragInProgress) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700886 mItemAddedBackToSelfViaIcon = true;
Adam Cohen76078c42011-06-09 15:06:52 -0700887 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700888 }
889
890 public boolean isDropEnabled() {
891 return true;
892 }
893
Adam Cohen4045eb72011-10-06 11:44:26 -0700894 private void setupContentDimensions(int count) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700895 ArrayList<View> list = getItemsInReadingOrder();
896
897 int countX = mContent.getCountX();
898 int countY = mContent.getCountY();
Adam Cohen7c693212011-05-18 15:26:57 -0700899 boolean done = false;
Adam Cohen2801caf2011-05-13 20:57:39 -0700900
Adam Cohen7c693212011-05-18 15:26:57 -0700901 while (!done) {
902 int oldCountX = countX;
903 int oldCountY = countY;
904 if (countX * countY < count) {
905 // Current grid is too small, expand it
Adam Cohen78dc83e2011-11-15 17:10:00 -0800906 if ((countX <= countY || countY == mMaxCountY) && countX < mMaxCountX) {
Adam Cohen7c693212011-05-18 15:26:57 -0700907 countX++;
908 } else if (countY < mMaxCountY) {
909 countY++;
910 }
911 if (countY == 0) countY++;
912 } else if ((countY - 1) * countX >= count && countY >= countX) {
913 countY = Math.max(0, countY - 1);
914 } else if ((countX - 1) * countY >= count) {
915 countX = Math.max(0, countX - 1);
Adam Cohen2801caf2011-05-13 20:57:39 -0700916 }
Adam Cohen7c693212011-05-18 15:26:57 -0700917 done = countX == oldCountX && countY == oldCountY;
Adam Cohen2801caf2011-05-13 20:57:39 -0700918 }
Adam Cohen7c693212011-05-18 15:26:57 -0700919 mContent.setGridSize(countX, countY);
Adam Cohen2801caf2011-05-13 20:57:39 -0700920 arrangeChildren(list);
921 }
922
923 public boolean isFull() {
Adam Cohen78dc83e2011-11-15 17:10:00 -0800924 return getItemCount() >= mMaxNumItems;
Adam Cohen2801caf2011-05-13 20:57:39 -0700925 }
926
927 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700928 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700929
Winson Chung892c74d2013-08-22 16:15:50 -0700930 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700931 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700932 int height = getFolderHeight();
Adam Cohen2801caf2011-05-13 20:57:39 -0700933
Adam Cohen307fe232012-08-16 17:55:58 -0700934 float scale = parent.getDescendantRectRelativeToSelf(mFolderIcon, mTempRect);
Adam Cohen8e776a62011-06-28 18:10:06 -0700935
Winson Chungaf40f202013-09-18 18:26:31 -0700936 LauncherAppState app = LauncherAppState.getInstance();
937 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
938
Adam Cohen307fe232012-08-16 17:55:58 -0700939 int centerX = (int) (mTempRect.left + mTempRect.width() * scale / 2);
940 int centerY = (int) (mTempRect.top + mTempRect.height() * scale / 2);
Adam Cohen2801caf2011-05-13 20:57:39 -0700941 int centeredLeft = centerX - width / 2;
942 int centeredTop = centerY - height / 2;
Winson Chung3057b1c2013-10-10 17:35:35 -0700943 int currentPage = mLauncher.getWorkspace().getNextPage();
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800944 // In case the workspace is scrolling, we need to use the final scroll to compute
945 // the folders bounds.
946 mLauncher.getWorkspace().setFinalScrollForPageChange(currentPage);
Adam Cohen35e7e642011-07-17 14:47:18 -0700947 // We first fetch the currently visible CellLayoutChildren
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800948 CellLayout currentLayout = (CellLayout) mLauncher.getWorkspace().getChildAt(currentPage);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700949 ShortcutAndWidgetContainer boundingLayout = currentLayout.getShortcutsAndWidgets();
Adam Cohen35e7e642011-07-17 14:47:18 -0700950 Rect bounds = new Rect();
951 parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800952 // We reset the workspaces scroll
953 mLauncher.getWorkspace().resetFinalScrollForPageChange(currentPage);
Adam Cohen2801caf2011-05-13 20:57:39 -0700954
Adam Cohen35e7e642011-07-17 14:47:18 -0700955 // We need to bound the folder to the currently visible CellLayoutChildren
956 int left = Math.min(Math.max(bounds.left, centeredLeft),
957 bounds.left + bounds.width() - width);
958 int top = Math.min(Math.max(bounds.top, centeredTop),
959 bounds.top + bounds.height() - height);
Winson Chungaf40f202013-09-18 18:26:31 -0700960 if (grid.isPhone() && (grid.availableWidthPx - width) < grid.iconSizePx) {
961 // Center the folder if it is full (on phones only)
962 left = (grid.availableWidthPx - width) / 2;
963 } else if (width >= bounds.width()) {
964 // If the folder doesn't fit within the bounds, center it about the desired bounds
Adam Cohen35e7e642011-07-17 14:47:18 -0700965 left = bounds.left + (bounds.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700966 }
Adam Cohen35e7e642011-07-17 14:47:18 -0700967 if (height >= bounds.height()) {
968 top = bounds.top + (bounds.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700969 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700970
971 int folderPivotX = width / 2 + (centeredLeft - left);
972 int folderPivotY = height / 2 + (centeredTop - top);
973 setPivotX(folderPivotX);
974 setPivotY(folderPivotY);
Adam Cohen268c4752012-06-06 17:47:33 -0700975 mFolderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700976 (1.0f * folderPivotX / width));
Adam Cohen268c4752012-06-06 17:47:33 -0700977 mFolderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700978 (1.0f * folderPivotY / height));
Adam Cohen3bf84d32012-05-07 20:17:14 -0700979
Adam Cohen662b5982011-12-13 17:45:21 -0800980 lp.width = width;
981 lp.height = height;
982 lp.x = left;
983 lp.y = top;
Adam Cohen2801caf2011-05-13 20:57:39 -0700984 }
985
Adam Cohen268c4752012-06-06 17:47:33 -0700986 float getPivotXForIconAnimation() {
987 return mFolderIconPivotX;
988 }
989 float getPivotYForIconAnimation() {
990 return mFolderIconPivotY;
991 }
992
Adam Cohen2801caf2011-05-13 20:57:39 -0700993 private void setupContentForNumItems(int count) {
Adam Cohen4045eb72011-10-06 11:44:26 -0700994 setupContentDimensions(count);
Adam Cohen2801caf2011-05-13 20:57:39 -0700995
Adam Cohen8e776a62011-06-28 18:10:06 -0700996 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700997 if (lp == null) {
Adam Cohen8e776a62011-06-28 18:10:06 -0700998 lp = new DragLayer.LayoutParams(0, 0);
999 lp.customPosition = true;
Adam Cohen2801caf2011-05-13 20:57:39 -07001000 setLayoutParams(lp);
1001 }
1002 centerAboutIcon();
1003 }
1004
Winson Chung892c74d2013-08-22 16:15:50 -07001005 private int getContentAreaHeight() {
1006 LauncherAppState app = LauncherAppState.getInstance();
1007 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1008 Rect workspacePadding = grid.getWorkspacePadding(grid.isLandscape ?
1009 CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
1010 int maxContentAreaHeight = grid.availableHeightPx -
Winson Chung892c74d2013-08-22 16:15:50 -07001011 workspacePadding.top - workspacePadding.bottom -
Winson Chung892c74d2013-08-22 16:15:50 -07001012 mFolderNameHeight;
Adam Cohen1960ea42013-11-12 11:33:14 +00001013 int height = Math.min(maxContentAreaHeight,
Winson Chung892c74d2013-08-22 16:15:50 -07001014 mContent.getDesiredHeight());
Adam Cohen1960ea42013-11-12 11:33:14 +00001015 return Math.max(height, MIN_CONTENT_DIMEN);
1016 }
1017
1018 private int getContentAreaWidth() {
1019 return Math.max(mContent.getDesiredWidth(), MIN_CONTENT_DIMEN);
Winson Chung892c74d2013-08-22 16:15:50 -07001020 }
1021
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001022 private int getFolderHeight() {
Winson Chung892c74d2013-08-22 16:15:50 -07001023 int height = getPaddingTop() + getPaddingBottom()
1024 + getContentAreaHeight() + mFolderNameHeight;
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001025 return height;
1026 }
Adam Cohen234c4cd2011-07-17 21:03:04 -07001027
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001028 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001029 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
1030 int height = getFolderHeight();
Adam Cohen1960ea42013-11-12 11:33:14 +00001031 int contentAreaWidthSpec = MeasureSpec.makeMeasureSpec(getContentAreaWidth(),
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001032 MeasureSpec.EXACTLY);
Winson Chung892c74d2013-08-22 16:15:50 -07001033 int contentAreaHeightSpec = MeasureSpec.makeMeasureSpec(getContentAreaHeight(),
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001034 MeasureSpec.EXACTLY);
Adam Cohen1960ea42013-11-12 11:33:14 +00001035
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08001036 if (LauncherAppState.isDisableAllApps()) {
Nilesh Agrawal5f7099a2014-01-02 15:54:57 -08001037 // Don't cap the height of the content to allow scrolling.
1038 mContent.setFixedSize(getContentAreaWidth(), mContent.getDesiredHeight());
1039 } else {
1040 mContent.setFixedSize(getContentAreaWidth(), getContentAreaHeight());
1041 }
1042
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001043 mScrollView.measure(contentAreaWidthSpec, contentAreaHeightSpec);
1044 mFolderName.measure(contentAreaWidthSpec,
Adam Cohen234c4cd2011-07-17 21:03:04 -07001045 MeasureSpec.makeMeasureSpec(mFolderNameHeight, MeasureSpec.EXACTLY));
1046 setMeasuredDimension(width, height);
1047 }
1048
Adam Cohen2801caf2011-05-13 20:57:39 -07001049 private void arrangeChildren(ArrayList<View> list) {
1050 int[] vacant = new int[2];
1051 if (list == null) {
1052 list = getItemsInReadingOrder();
1053 }
1054 mContent.removeAllViews();
1055
1056 for (int i = 0; i < list.size(); i++) {
1057 View v = list.get(i);
1058 mContent.getVacantCell(vacant, 1, 1);
1059 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) v.getLayoutParams();
1060 lp.cellX = vacant[0];
1061 lp.cellY = vacant[1];
1062 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohen2792a332011-09-26 21:09:47 -07001063 if (info.cellX != vacant[0] || info.cellY != vacant[1]) {
1064 info.cellX = vacant[0];
1065 info.cellY = vacant[1];
1066 LauncherModel.addOrMoveItemInDatabase(mLauncher, info, mInfo.id, 0,
1067 info.cellX, info.cellY);
1068 }
Adam Cohen2801caf2011-05-13 20:57:39 -07001069 boolean insert = false;
1070 mContent.addViewToCellLayout(v, insert ? 0 : -1, (int)info.id, lp, true);
1071 }
Adam Cohen7c693212011-05-18 15:26:57 -07001072 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -07001073 }
1074
Adam Cohena9cf38f2011-05-02 15:36:58 -07001075 public int getItemCount() {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001076 return mContent.getShortcutsAndWidgets().getChildCount();
Adam Cohena9cf38f2011-05-02 15:36:58 -07001077 }
1078
1079 public View getItemAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001080 return mContent.getShortcutsAndWidgets().getChildAt(index);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001081 }
1082
Adam Cohen2801caf2011-05-13 20:57:39 -07001083 private void onCloseComplete() {
Adam Cohen05e0f402011-08-01 12:12:49 -07001084 DragLayer parent = (DragLayer) getParent();
Michael Jurka5649c282012-06-18 10:33:21 -07001085 if (parent != null) {
1086 parent.removeView(this);
1087 }
Adam Cohen4554ee12011-08-03 16:13:21 -07001088 mDragController.removeDropTarget((DropTarget) this);
Adam Cohen05e0f402011-08-01 12:12:49 -07001089 clearFocus();
Adam Cohenac56cff2011-09-28 20:45:37 -07001090 mFolderIcon.requestFocus();
Adam Cohen05e0f402011-08-01 12:12:49 -07001091
Adam Cohen2801caf2011-05-13 20:57:39 -07001092 if (mRearrangeOnClose) {
1093 setupContentForNumItems(getItemCount());
1094 mRearrangeOnClose = false;
1095 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001096 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001097 if (!mDragInProgress && !mSuppressFolderDeletion) {
1098 replaceFolderWithFinalItem();
1099 } else if (mDragInProgress) {
1100 mDeleteFolderOnDropCompleted = true;
1101 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001102 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001103 mSuppressFolderDeletion = false;
Adam Cohenafb01ee2011-06-23 15:38:03 -07001104 }
1105
1106 private void replaceFolderWithFinalItem() {
Adam Cohenafb01ee2011-06-23 15:38:03 -07001107 // Add the last remaining child to the workspace in place of the folder
Adam Cohenfb91f302012-06-11 15:45:18 -07001108 Runnable onCompleteRunnable = new Runnable() {
1109 @Override
1110 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -07001111 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screenId);
Adam Cohenafb01ee2011-06-23 15:38:03 -07001112
Winson Chung33231f52013-12-09 16:57:45 -08001113 View child = null;
Adam Cohenfb91f302012-06-11 15:45:18 -07001114 // Move the item from the folder to the workspace, in the position of the folder
1115 if (getItemCount() == 1) {
1116 ShortcutInfo finalItem = mInfo.contents.get(0);
Adam Cohenc5e63f32012-07-12 16:16:57 -07001117 child = mLauncher.createShortcut(R.layout.application, cellLayout,
Adam Cohenfb91f302012-06-11 15:45:18 -07001118 finalItem);
1119 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -07001120 mInfo.screenId, mInfo.cellX, mInfo.cellY);
Adam Cohenfb91f302012-06-11 15:45:18 -07001121 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001122 if (getItemCount() <= 1) {
1123 // Remove the folder
1124 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
Dan Sandler0eb687f2014-01-10 13:24:55 -05001125 if (cellLayout != null) {
1126 // b/12446428 -- sometimes the cell layout has already gone away?
1127 cellLayout.removeView(mFolderIcon);
1128 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001129 if (mFolderIcon instanceof DropTarget) {
1130 mDragController.removeDropTarget((DropTarget) mFolderIcon);
1131 }
1132 mLauncher.removeFolder(mInfo);
1133 }
Adam Cohenc5e63f32012-07-12 16:16:57 -07001134 // We add the child after removing the folder to prevent both from existing at
Winson Chung0e6a7132013-08-23 12:55:10 -07001135 // the same time in the CellLayout. We need to add the new item with addInScreenFromBind()
1136 // to ensure that hotseat items are placed correctly.
Adam Cohenc5e63f32012-07-12 16:16:57 -07001137 if (child != null) {
Winson Chung0e6a7132013-08-23 12:55:10 -07001138 mLauncher.getWorkspace().addInScreenFromBind(child, mInfo.container, mInfo.screenId,
Adam Cohenc5e63f32012-07-12 16:16:57 -07001139 mInfo.cellX, mInfo.cellY, mInfo.spanX, mInfo.spanY);
1140 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001141 }
1142 };
1143 View finalChild = getItemAt(0);
1144 if (finalChild != null) {
1145 mFolderIcon.performDestroyAnimation(finalChild, onCompleteRunnable);
Winson Chung33231f52013-12-09 16:57:45 -08001146 } else {
1147 onCompleteRunnable.run();
Adam Cohenafb01ee2011-06-23 15:38:03 -07001148 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001149 mDestroyed = true;
1150 }
1151
1152 boolean isDestroyed() {
1153 return mDestroyed;
Adam Cohen2801caf2011-05-13 20:57:39 -07001154 }
1155
Adam Cohenac56cff2011-09-28 20:45:37 -07001156 // This method keeps track of the last item in the folder for the purposes
1157 // of keyboard focus
1158 private void updateTextViewFocus() {
1159 View lastChild = getItemAt(getItemCount() - 1);
1160 getItemAt(getItemCount() - 1);
1161 if (lastChild != null) {
1162 mFolderName.setNextFocusDownId(lastChild.getId());
1163 mFolderName.setNextFocusRightId(lastChild.getId());
1164 mFolderName.setNextFocusLeftId(lastChild.getId());
1165 mFolderName.setNextFocusUpId(lastChild.getId());
1166 }
1167 }
1168
Adam Cohenbfbfd262011-06-13 16:55:12 -07001169 public void onDrop(DragObject d) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001170 Runnable cleanUpRunnable = null;
1171
Adam Cohen689ff162014-05-08 17:27:56 -07001172 // If we are coming from All Apps space, we defer removing the extra empty screen
1173 // until the folder closes
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001174 if (d.dragSource != mLauncher.getWorkspace() && !(d.dragSource instanceof Folder)) {
1175 cleanUpRunnable = new Runnable() {
1176 @Override
1177 public void run() {
Adam Cohen689ff162014-05-08 17:27:56 -07001178 mLauncher.exitSpringLoadedDragModeDelayed(true,
1179 Launcher.EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
1180 null);
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001181 }
1182 };
Adam Cohenbfbfd262011-06-13 16:55:12 -07001183 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001184
1185 View currentDragView;
1186 ShortcutInfo si = mCurrentDragInfo;
1187 if (mIsExternalDrag) {
1188 si.cellX = mEmptyCell[0];
1189 si.cellY = mEmptyCell[1];
1190 currentDragView = createAndAddShortcut(si);
1191 } else {
1192 currentDragView = mCurrentDragView;
1193 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) currentDragView.getLayoutParams();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001194 si.cellX = lp.cellX = mEmptyCell[0];
1195 si.cellX = lp.cellY = mEmptyCell[1];
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001196 mContent.addViewToCellLayout(currentDragView, -1, (int) si.id, lp, true);
Adam Cohenbfbfd262011-06-13 16:55:12 -07001197 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001198
1199 if (d.dragView.hasDrawn()) {
1200
1201 // Temporarily reset the scale such that the animation target gets calculated correctly.
1202 float scaleX = getScaleX();
1203 float scaleY = getScaleY();
1204 setScaleX(1.0f);
1205 setScaleY(1.0f);
1206 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, currentDragView,
1207 cleanUpRunnable, null);
1208 setScaleX(scaleX);
1209 setScaleY(scaleY);
1210 } else {
1211 d.deferDragViewCleanupPostAnimation = false;
1212 currentDragView.setVisibility(VISIBLE);
1213 }
1214 mItemsInvalidated = true;
1215 setupContentDimensions(getItemCount());
1216
1217 // Actually move the item in the database if it was an external drag.
1218 if (mIsExternalDrag) {
1219 LauncherModel.addOrMoveItemInDatabase(
1220 mLauncher, si, mInfo.id, 0, si.cellX, si.cellY);
1221
1222 // We only need to update the locations if it doesn't get handled in #onDropCompleted.
1223 if (d.dragSource != this) {
1224 updateItemLocationsInDatabaseBatch();
1225 }
1226 mIsExternalDrag = false;
1227 }
1228
1229 // Temporarily suppress the listener, as we did all the work already here.
1230 mSuppressOnAdd = true;
1231 mInfo.add(si);
1232 mSuppressOnAdd = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001233 }
1234
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001235 // This is used so the item doesn't immediately appear in the folder when added. In one case
1236 // we need to create the illusion that the item isn't added back to the folder yet, to
1237 // to correspond to the animation of the icon back into the folder. This is
1238 public void hideItem(ShortcutInfo info) {
1239 View v = getViewForInfo(info);
1240 v.setVisibility(INVISIBLE);
1241 }
1242 public void showItem(ShortcutInfo info) {
1243 View v = getViewForInfo(info);
1244 v.setVisibility(VISIBLE);
1245 }
1246
Adam Cohenbfbfd262011-06-13 16:55:12 -07001247 public void onAdd(ShortcutInfo item) {
1248 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001249 // If the item was dropped onto this open folder, we have done the work associated
1250 // with adding the item to the folder, as indicated by mSuppressOnAdd being set
Adam Cohenbfbfd262011-06-13 16:55:12 -07001251 if (mSuppressOnAdd) return;
1252 if (!findAndSetEmptyCells(item)) {
1253 // The current layout is full, can we expand it?
1254 setupContentForNumItems(getItemCount() + 1);
1255 findAndSetEmptyCells(item);
1256 }
1257 createAndAddShortcut(item);
1258 LauncherModel.addOrMoveItemInDatabase(
1259 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
1260 }
1261
Adam Cohena9cf38f2011-05-02 15:36:58 -07001262 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -07001263 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001264 // If this item is being dragged from this open folder, we have already handled
1265 // the work associated with removing the item, so we don't have to do anything here.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001266 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001267 View v = getViewForInfo(item);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001268 mContent.removeView(v);
Adam Cohen2801caf2011-05-13 20:57:39 -07001269 if (mState == STATE_ANIMATING) {
1270 mRearrangeOnClose = true;
1271 } else {
1272 setupContentForNumItems(getItemCount());
1273 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001274 if (getItemCount() <= 1) {
1275 replaceFolderWithFinalItem();
1276 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001277 }
Adam Cohen7c693212011-05-18 15:26:57 -07001278
Adam Cohendf1e4e82011-06-24 15:57:39 -07001279 private View getViewForInfo(ShortcutInfo item) {
1280 for (int j = 0; j < mContent.getCountY(); j++) {
1281 for (int i = 0; i < mContent.getCountX(); i++) {
1282 View v = mContent.getChildAt(i, j);
1283 if (v.getTag() == item) {
1284 return v;
1285 }
1286 }
1287 }
1288 return null;
1289 }
1290
Adam Cohen76078c42011-06-09 15:06:52 -07001291 public void onItemsChanged() {
Adam Cohenac56cff2011-09-28 20:45:37 -07001292 updateTextViewFocus();
Adam Cohen76078c42011-06-09 15:06:52 -07001293 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001294
Adam Cohen76fc0852011-06-17 13:26:23 -07001295 public void onTitleChanged(CharSequence title) {
1296 }
Adam Cohen76078c42011-06-09 15:06:52 -07001297
Adam Cohen7c693212011-05-18 15:26:57 -07001298 public ArrayList<View> getItemsInReadingOrder() {
1299 if (mItemsInvalidated) {
1300 mItemsInReadingOrder.clear();
1301 for (int j = 0; j < mContent.getCountY(); j++) {
1302 for (int i = 0; i < mContent.getCountX(); i++) {
1303 View v = mContent.getChildAt(i, j);
1304 if (v != null) {
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001305 mItemsInReadingOrder.add(v);
Adam Cohen7c693212011-05-18 15:26:57 -07001306 }
1307 }
1308 }
1309 mItemsInvalidated = false;
1310 }
1311 return mItemsInReadingOrder;
1312 }
Adam Cohen8dfcba42011-07-07 16:38:18 -07001313
1314 public void getLocationInDragLayer(int[] loc) {
1315 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
1316 }
Adam Cohenea0818d2011-09-30 20:06:58 -07001317
1318 public void onFocusChange(View v, boolean hasFocus) {
1319 if (v == mFolderName && hasFocus) {
1320 startEditingFolderName();
1321 }
1322 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001323
1324 @Override
1325 public void getHitRectRelativeToDragLayer(Rect outRect) {
1326 getHitRect(outRect);
1327 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001328}