blob: 2216b9ecad630bad81faa51ed8445a7206fcbf09 [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 Cohen477828c2013-09-20 12:05:49 -0700569 final BubbleTextView textView =
570 (BubbleTextView) mInflater.inflate(R.layout.application, this, false);
Winson Chung54000492013-10-14 16:29:29 -0700571 textView.setCompoundDrawables(null,
Winson Chung0dbd7342013-10-13 22:46:20 -0700572 Utilities.createIconDrawable(item.getIcon(mIconCache)), null, null);
Adam Cohendf2cc412011-04-27 16:56:57 -0700573 textView.setText(item.title);
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 Cohen477828c2013-09-20 12:05:49 -0700578 textView.setTextColor(getResources().getColor(R.color.folder_items_text_color));
579 textView.setShadowsEnabled(false);
Adam Cohen7397e622013-10-24 12:11:34 -0700580 textView.setGlowColor(getResources().getColor(R.color.folder_items_glow_color));
Chris Wren40c5ed32014-06-24 18:24:23 -0400581 textView.applyState();
Adam Cohendf2cc412011-04-27 16:56:57 -0700582
583 textView.setOnClickListener(this);
584 textView.setOnLongClickListener(this);
585
Adam Cohenc508b2d2011-06-28 14:41:44 -0700586 // We need to check here to verify that the given item's location isn't already occupied
Adam Cohen3bf84d32012-05-07 20:17:14 -0700587 // by another item.
Adam Cohen0057bbc2011-08-12 18:30:51 -0700588 if (mContent.getChildAt(item.cellX, item.cellY) != null || item.cellX < 0 || item.cellY < 0
589 || item.cellX >= mContent.getCountX() || item.cellY >= mContent.getCountY()) {
Jason Monk4ff73882014-04-24 16:48:07 -0400590 // This shouldn't happen, log it.
Adam Cohen3bf84d32012-05-07 20:17:14 -0700591 Log.e(TAG, "Folder order not properly persisted during bind");
Adam Cohenc508b2d2011-06-28 14:41:44 -0700592 if (!findAndSetEmptyCells(item)) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800593 return null;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700594 }
595 }
596
Adam Cohendf2cc412011-04-27 16:56:57 -0700597 CellLayout.LayoutParams lp =
598 new CellLayout.LayoutParams(item.cellX, item.cellY, item.spanX, item.spanY);
599 boolean insert = false;
Adam Cohenac56cff2011-09-28 20:45:37 -0700600 textView.setOnKeyListener(new FolderKeyEventListener());
Adam Cohendf2cc412011-04-27 16:56:57 -0700601 mContent.addViewToCellLayout(textView, insert ? 0 : -1, (int)item.id, lp, true);
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800602 return textView;
Adam Cohendf2cc412011-04-27 16:56:57 -0700603 }
604
Adam Cohencb3382b2011-05-24 14:07:08 -0700605 public void onDragEnter(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700606 mPreviousTargetCell[0] = -1;
607 mPreviousTargetCell[1] = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700608 mOnExitAlarm.cancelAlarm();
609 }
610
611 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
612 public void onAlarm(Alarm alarm) {
613 realTimeReorder(mEmptyCell, mTargetCell);
614 }
615 };
616
617 boolean readingOrderGreaterThan(int[] v1, int[] v2) {
618 if (v1[1] > v2[1] || (v1[1] == v2[1] && v1[0] > v2[0])) {
619 return true;
620 } else {
621 return false;
622 }
623 }
624
625 private void realTimeReorder(int[] empty, int[] target) {
626 boolean wrap;
627 int startX;
628 int endX;
629 int startY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700630 int delay = 0;
631 float delayAmount = 30;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700632 if (readingOrderGreaterThan(target, empty)) {
633 wrap = empty[0] >= mContent.getCountX() - 1;
634 startY = wrap ? empty[1] + 1 : empty[1];
635 for (int y = startY; y <= target[1]; y++) {
636 startX = y == empty[1] ? empty[0] + 1 : 0;
637 endX = y < target[1] ? mContent.getCountX() - 1 : target[0];
638 for (int x = startX; x <= endX; x++) {
639 View v = mContent.getChildAt(x,y);
640 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen482ed822012-03-02 14:15:13 -0800641 REORDER_ANIMATION_DURATION, delay, true, true)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700642 empty[0] = x;
643 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700644 delay += delayAmount;
645 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700646 }
647 }
648 }
649 } else {
650 wrap = empty[0] == 0;
651 startY = wrap ? empty[1] - 1 : empty[1];
652 for (int y = startY; y >= target[1]; y--) {
653 startX = y == empty[1] ? empty[0] - 1 : mContent.getCountX() - 1;
654 endX = y > target[1] ? 0 : target[0];
655 for (int x = startX; x >= endX; x--) {
656 View v = mContent.getChildAt(x,y);
657 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen482ed822012-03-02 14:15:13 -0800658 REORDER_ANIMATION_DURATION, delay, true, true)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700659 empty[0] = x;
660 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700661 delay += delayAmount;
662 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700663 }
664 }
665 }
666 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700667 }
668
Adam Cohen2374abf2013-04-16 14:56:57 -0700669 public boolean isLayoutRtl() {
670 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
671 }
672
Adam Cohencb3382b2011-05-24 14:07:08 -0700673 public void onDragOver(DragObject d) {
Alan Viverette4cda5b72013-08-28 17:53:41 -0700674 final DragView dragView = d.dragView;
675 final int scrollOffset = mScrollView.getScrollY();
676 final float[] r = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, dragView, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700677 r[0] -= getPaddingLeft();
678 r[1] -= getPaddingTop();
679
Alan Viverette4cda5b72013-08-28 17:53:41 -0700680 final long downTime = SystemClock.uptimeMillis();
681 final MotionEvent translatedEv = MotionEvent.obtain(
682 downTime, downTime, MotionEvent.ACTION_MOVE, d.x, d.y, 0);
683
684 if (!mAutoScrollHelper.isEnabled()) {
685 mAutoScrollHelper.setEnabled(true);
686 }
687
688 final boolean handled = mAutoScrollHelper.onTouch(this, translatedEv);
689 translatedEv.recycle();
690
691 if (handled) {
692 mReorderAlarm.cancelAlarm();
693 } else {
694 mTargetCell = mContent.findNearestArea(
695 (int) r[0], (int) r[1] + scrollOffset, 1, 1, mTargetCell);
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700696 if (isLayoutRtl()) {
697 mTargetCell[0] = mContent.getCountX() - mTargetCell[0] - 1;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700698 }
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700699 if (mTargetCell[0] != mPreviousTargetCell[0]
700 || mTargetCell[1] != mPreviousTargetCell[1]) {
701 mReorderAlarm.cancelAlarm();
702 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
703 mReorderAlarm.setAlarm(REORDER_DELAY);
704 mPreviousTargetCell[0] = mTargetCell[0];
705 mPreviousTargetCell[1] = mTargetCell[1];
706 mDragMode = DRAG_MODE_REORDER;
707 } else {
708 mDragMode = DRAG_MODE_NONE;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700709 }
Adam Cohenbfbfd262011-06-13 16:55:12 -0700710 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700711 }
712
Adam Cohenbfbfd262011-06-13 16:55:12 -0700713 // This is used to compute the visual center of the dragView. The idea is that
714 // the visual center represents the user's interpretation of where the item is, and hence
715 // is the appropriate point to use when determining drop location.
716 private float[] getDragViewVisualCenter(int x, int y, int xOffset, int yOffset,
717 DragView dragView, float[] recycle) {
718 float res[];
719 if (recycle == null) {
720 res = new float[2];
721 } else {
722 res = recycle;
723 }
724
725 // These represent the visual top and left of drag view if a dragRect was provided.
726 // If a dragRect was not provided, then they correspond to the actual view left and
727 // top, as the dragRect is in that case taken to be the entire dragView.
728 // R.dimen.dragViewOffsetY.
729 int left = x - xOffset;
730 int top = y - yOffset;
731
732 // In order to find the visual center, we shift by half the dragRect
733 res[0] = left + dragView.getDragRegion().width() / 2;
734 res[1] = top + dragView.getDragRegion().height() / 2;
735
736 return res;
737 }
738
739 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
740 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700741 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700742 }
743 };
744
Adam Cohen95bb8002011-07-03 23:40:28 -0700745 public void completeDragExit() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700746 mLauncher.closeFolder();
747 mCurrentDragInfo = null;
748 mCurrentDragView = null;
749 mSuppressOnAdd = false;
750 mRearrangeOnClose = true;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800751 mIsExternalDrag = false;
Adam Cohen3e8f8112011-07-02 18:03:00 -0700752 }
753
Adam Cohencb3382b2011-05-24 14:07:08 -0700754 public void onDragExit(DragObject d) {
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700755 // Exiting folder; stop the auto scroller.
Alan Viverette4cda5b72013-08-28 17:53:41 -0700756 mAutoScrollHelper.setEnabled(false);
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700757 // We only close the folder if this is a true drag exit, ie. not because
758 // a drop has occurred above the folder.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700759 if (!d.dragComplete) {
760 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
761 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
762 }
763 mReorderAlarm.cancelAlarm();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700764 mDragMode = DRAG_MODE_NONE;
Adam Cohen2801caf2011-05-13 20:57:39 -0700765 }
766
Michael Jurka1e2f4652013-07-08 18:03:46 -0700767 public void onDropCompleted(final View target, final DragObject d,
768 final boolean isFlingToDelete, final boolean success) {
769 if (mDeferDropAfterUninstall) {
Michael Jurkaf3007582013-08-21 14:33:57 +0200770 Log.d(TAG, "Deferred handling drop because waiting for uninstall.");
Michael Jurka1e2f4652013-07-08 18:03:46 -0700771 mDeferredAction = new Runnable() {
772 public void run() {
773 onDropCompleted(target, d, isFlingToDelete, success);
774 mDeferredAction = null;
775 }
776 };
777 return;
778 }
779
780 boolean beingCalledAfterUninstall = mDeferredAction != null;
781 boolean successfulDrop =
782 success && (!beingCalledAfterUninstall || mUninstallSuccessful);
Winson Chung5f8afe62013-08-12 16:19:28 -0700783
Michael Jurka1e2f4652013-07-08 18:03:46 -0700784 if (successfulDrop) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800785 if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon && target != this) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700786 replaceFolderWithFinalItem();
787 }
788 } else {
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700789 setupContentForNumItems(getItemCount());
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700790 // The drag failed, we need to return the item to the folder
791 mFolderIcon.onDrop(d);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700792 }
793
794 if (target != this) {
795 if (mOnExitAlarm.alarmPending()) {
796 mOnExitAlarm.cancelAlarm();
Michael Jurka54554252013-08-01 12:52:23 +0200797 if (!successfulDrop) {
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700798 mSuppressFolderDeletion = true;
799 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700800 completeDragExit();
801 }
Adam Cohen9c58d822013-12-13 17:18:10 -0800802 }
803
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700804 mDeleteFolderOnDropCompleted = false;
805 mDragInProgress = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700806 mItemAddedBackToSelfViaIcon = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700807 mCurrentDragInfo = null;
808 mCurrentDragView = null;
809 mSuppressOnAdd = false;
Adam Cohen4045eb72011-10-06 11:44:26 -0700810
811 // Reordering may have occured, and we need to save the new item locations. We do this once
812 // at the end to prevent unnecessary database operations.
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700813 updateItemLocationsInDatabaseBatch();
Adam Cohen4045eb72011-10-06 11:44:26 -0700814 }
815
Michael Jurka1e2f4652013-07-08 18:03:46 -0700816 public void deferCompleteDropAfterUninstallActivity() {
817 mDeferDropAfterUninstall = true;
818 }
819
820 public void onUninstallActivityReturned(boolean success) {
821 mDeferDropAfterUninstall = false;
822 mUninstallSuccessful = success;
823 if (mDeferredAction != null) {
824 mDeferredAction.run();
825 }
826 }
827
Winson Chunga48487a2012-03-20 16:19:37 -0700828 @Override
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800829 public float getIntrinsicIconScaleFactor() {
830 return 1f;
831 }
832
833 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800834 public boolean supportsFlingToDelete() {
835 return true;
836 }
837
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000838 @Override
839 public boolean supportsAppInfoDropTarget() {
840 return false;
841 }
842
843 @Override
844 public boolean supportsDeleteDropTarget() {
845 return true;
846 }
847
Winson Chunga48487a2012-03-20 16:19:37 -0700848 public void onFlingToDelete(DragObject d, int x, int y, PointF vec) {
849 // Do nothing
850 }
851
852 @Override
853 public void onFlingToDeleteCompleted() {
854 // Do nothing
855 }
856
Adam Cohen4045eb72011-10-06 11:44:26 -0700857 private void updateItemLocationsInDatabase() {
858 ArrayList<View> list = getItemsInReadingOrder();
859 for (int i = 0; i < list.size(); i++) {
860 View v = list.get(i);
861 ItemInfo info = (ItemInfo) v.getTag();
862 LauncherModel.moveItemInDatabase(mLauncher, info, mInfo.id, 0,
Winson Chung33231f52013-12-09 16:57:45 -0800863 info.cellX, info.cellY);
Adam Cohen4045eb72011-10-06 11:44:26 -0700864 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700865 }
Adam Cohen228da5a2011-07-27 22:23:47 -0700866
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700867 private void updateItemLocationsInDatabaseBatch() {
868 ArrayList<View> list = getItemsInReadingOrder();
869 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
870 for (int i = 0; i < list.size(); i++) {
871 View v = list.get(i);
872 ItemInfo info = (ItemInfo) v.getTag();
873 items.add(info);
874 }
875
876 LauncherModel.moveItemsInDatabase(mLauncher, items, mInfo.id, 0);
877 }
878
Adam Cohene25af792013-06-06 23:08:25 -0700879 public void addItemLocationsInDatabase() {
880 ArrayList<View> list = getItemsInReadingOrder();
881 for (int i = 0; i < list.size(); i++) {
882 View v = list.get(i);
883 ItemInfo info = (ItemInfo) v.getTag();
884 LauncherModel.addItemToDatabase(mLauncher, info, mInfo.id, 0,
885 info.cellX, info.cellY, false);
886 }
887 }
888
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700889 public void notifyDrop() {
890 if (mDragInProgress) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700891 mItemAddedBackToSelfViaIcon = true;
Adam Cohen76078c42011-06-09 15:06:52 -0700892 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700893 }
894
895 public boolean isDropEnabled() {
896 return true;
897 }
898
Adam Cohen4045eb72011-10-06 11:44:26 -0700899 private void setupContentDimensions(int count) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700900 ArrayList<View> list = getItemsInReadingOrder();
901
902 int countX = mContent.getCountX();
903 int countY = mContent.getCountY();
Adam Cohen7c693212011-05-18 15:26:57 -0700904 boolean done = false;
Adam Cohen2801caf2011-05-13 20:57:39 -0700905
Adam Cohen7c693212011-05-18 15:26:57 -0700906 while (!done) {
907 int oldCountX = countX;
908 int oldCountY = countY;
909 if (countX * countY < count) {
910 // Current grid is too small, expand it
Adam Cohen78dc83e2011-11-15 17:10:00 -0800911 if ((countX <= countY || countY == mMaxCountY) && countX < mMaxCountX) {
Adam Cohen7c693212011-05-18 15:26:57 -0700912 countX++;
913 } else if (countY < mMaxCountY) {
914 countY++;
915 }
916 if (countY == 0) countY++;
917 } else if ((countY - 1) * countX >= count && countY >= countX) {
918 countY = Math.max(0, countY - 1);
919 } else if ((countX - 1) * countY >= count) {
920 countX = Math.max(0, countX - 1);
Adam Cohen2801caf2011-05-13 20:57:39 -0700921 }
Adam Cohen7c693212011-05-18 15:26:57 -0700922 done = countX == oldCountX && countY == oldCountY;
Adam Cohen2801caf2011-05-13 20:57:39 -0700923 }
Adam Cohen7c693212011-05-18 15:26:57 -0700924 mContent.setGridSize(countX, countY);
Adam Cohen2801caf2011-05-13 20:57:39 -0700925 arrangeChildren(list);
926 }
927
928 public boolean isFull() {
Adam Cohen78dc83e2011-11-15 17:10:00 -0800929 return getItemCount() >= mMaxNumItems;
Adam Cohen2801caf2011-05-13 20:57:39 -0700930 }
931
932 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700933 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700934
Winson Chung892c74d2013-08-22 16:15:50 -0700935 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700936 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700937 int height = getFolderHeight();
Adam Cohen2801caf2011-05-13 20:57:39 -0700938
Adam Cohen307fe232012-08-16 17:55:58 -0700939 float scale = parent.getDescendantRectRelativeToSelf(mFolderIcon, mTempRect);
Adam Cohen8e776a62011-06-28 18:10:06 -0700940
Winson Chungaf40f202013-09-18 18:26:31 -0700941 LauncherAppState app = LauncherAppState.getInstance();
942 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
943
Adam Cohen307fe232012-08-16 17:55:58 -0700944 int centerX = (int) (mTempRect.left + mTempRect.width() * scale / 2);
945 int centerY = (int) (mTempRect.top + mTempRect.height() * scale / 2);
Adam Cohen2801caf2011-05-13 20:57:39 -0700946 int centeredLeft = centerX - width / 2;
947 int centeredTop = centerY - height / 2;
Winson Chung3057b1c2013-10-10 17:35:35 -0700948 int currentPage = mLauncher.getWorkspace().getNextPage();
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800949 // In case the workspace is scrolling, we need to use the final scroll to compute
950 // the folders bounds.
951 mLauncher.getWorkspace().setFinalScrollForPageChange(currentPage);
Adam Cohen35e7e642011-07-17 14:47:18 -0700952 // We first fetch the currently visible CellLayoutChildren
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800953 CellLayout currentLayout = (CellLayout) mLauncher.getWorkspace().getChildAt(currentPage);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700954 ShortcutAndWidgetContainer boundingLayout = currentLayout.getShortcutsAndWidgets();
Adam Cohen35e7e642011-07-17 14:47:18 -0700955 Rect bounds = new Rect();
956 parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800957 // We reset the workspaces scroll
958 mLauncher.getWorkspace().resetFinalScrollForPageChange(currentPage);
Adam Cohen2801caf2011-05-13 20:57:39 -0700959
Adam Cohen35e7e642011-07-17 14:47:18 -0700960 // We need to bound the folder to the currently visible CellLayoutChildren
961 int left = Math.min(Math.max(bounds.left, centeredLeft),
962 bounds.left + bounds.width() - width);
963 int top = Math.min(Math.max(bounds.top, centeredTop),
964 bounds.top + bounds.height() - height);
Winson Chungaf40f202013-09-18 18:26:31 -0700965 if (grid.isPhone() && (grid.availableWidthPx - width) < grid.iconSizePx) {
966 // Center the folder if it is full (on phones only)
967 left = (grid.availableWidthPx - width) / 2;
968 } else if (width >= bounds.width()) {
969 // If the folder doesn't fit within the bounds, center it about the desired bounds
Adam Cohen35e7e642011-07-17 14:47:18 -0700970 left = bounds.left + (bounds.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700971 }
Adam Cohen35e7e642011-07-17 14:47:18 -0700972 if (height >= bounds.height()) {
973 top = bounds.top + (bounds.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700974 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700975
976 int folderPivotX = width / 2 + (centeredLeft - left);
977 int folderPivotY = height / 2 + (centeredTop - top);
978 setPivotX(folderPivotX);
979 setPivotY(folderPivotY);
Adam Cohen268c4752012-06-06 17:47:33 -0700980 mFolderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700981 (1.0f * folderPivotX / width));
Adam Cohen268c4752012-06-06 17:47:33 -0700982 mFolderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700983 (1.0f * folderPivotY / height));
Adam Cohen3bf84d32012-05-07 20:17:14 -0700984
Adam Cohen662b5982011-12-13 17:45:21 -0800985 lp.width = width;
986 lp.height = height;
987 lp.x = left;
988 lp.y = top;
Adam Cohen2801caf2011-05-13 20:57:39 -0700989 }
990
Adam Cohen268c4752012-06-06 17:47:33 -0700991 float getPivotXForIconAnimation() {
992 return mFolderIconPivotX;
993 }
994 float getPivotYForIconAnimation() {
995 return mFolderIconPivotY;
996 }
997
Adam Cohen2801caf2011-05-13 20:57:39 -0700998 private void setupContentForNumItems(int count) {
Adam Cohen4045eb72011-10-06 11:44:26 -0700999 setupContentDimensions(count);
Adam Cohen2801caf2011-05-13 20:57:39 -07001000
Adam Cohen8e776a62011-06-28 18:10:06 -07001001 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -07001002 if (lp == null) {
Adam Cohen8e776a62011-06-28 18:10:06 -07001003 lp = new DragLayer.LayoutParams(0, 0);
1004 lp.customPosition = true;
Adam Cohen2801caf2011-05-13 20:57:39 -07001005 setLayoutParams(lp);
1006 }
1007 centerAboutIcon();
1008 }
1009
Winson Chung892c74d2013-08-22 16:15:50 -07001010 private int getContentAreaHeight() {
1011 LauncherAppState app = LauncherAppState.getInstance();
1012 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1013 Rect workspacePadding = grid.getWorkspacePadding(grid.isLandscape ?
1014 CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
1015 int maxContentAreaHeight = grid.availableHeightPx -
Winson Chung892c74d2013-08-22 16:15:50 -07001016 workspacePadding.top - workspacePadding.bottom -
Winson Chung892c74d2013-08-22 16:15:50 -07001017 mFolderNameHeight;
Adam Cohen1960ea42013-11-12 11:33:14 +00001018 int height = Math.min(maxContentAreaHeight,
Winson Chung892c74d2013-08-22 16:15:50 -07001019 mContent.getDesiredHeight());
Adam Cohen1960ea42013-11-12 11:33:14 +00001020 return Math.max(height, MIN_CONTENT_DIMEN);
1021 }
1022
1023 private int getContentAreaWidth() {
1024 return Math.max(mContent.getDesiredWidth(), MIN_CONTENT_DIMEN);
Winson Chung892c74d2013-08-22 16:15:50 -07001025 }
1026
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001027 private int getFolderHeight() {
Winson Chung892c74d2013-08-22 16:15:50 -07001028 int height = getPaddingTop() + getPaddingBottom()
1029 + getContentAreaHeight() + mFolderNameHeight;
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001030 return height;
1031 }
Adam Cohen234c4cd2011-07-17 21:03:04 -07001032
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001033 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001034 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
1035 int height = getFolderHeight();
Adam Cohen1960ea42013-11-12 11:33:14 +00001036 int contentAreaWidthSpec = MeasureSpec.makeMeasureSpec(getContentAreaWidth(),
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001037 MeasureSpec.EXACTLY);
Winson Chung892c74d2013-08-22 16:15:50 -07001038 int contentAreaHeightSpec = MeasureSpec.makeMeasureSpec(getContentAreaHeight(),
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001039 MeasureSpec.EXACTLY);
Adam Cohen1960ea42013-11-12 11:33:14 +00001040
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08001041 if (LauncherAppState.isDisableAllApps()) {
Nilesh Agrawal5f7099a2014-01-02 15:54:57 -08001042 // Don't cap the height of the content to allow scrolling.
1043 mContent.setFixedSize(getContentAreaWidth(), mContent.getDesiredHeight());
1044 } else {
1045 mContent.setFixedSize(getContentAreaWidth(), getContentAreaHeight());
1046 }
1047
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001048 mScrollView.measure(contentAreaWidthSpec, contentAreaHeightSpec);
1049 mFolderName.measure(contentAreaWidthSpec,
Adam Cohen234c4cd2011-07-17 21:03:04 -07001050 MeasureSpec.makeMeasureSpec(mFolderNameHeight, MeasureSpec.EXACTLY));
1051 setMeasuredDimension(width, height);
1052 }
1053
Adam Cohen2801caf2011-05-13 20:57:39 -07001054 private void arrangeChildren(ArrayList<View> list) {
1055 int[] vacant = new int[2];
1056 if (list == null) {
1057 list = getItemsInReadingOrder();
1058 }
1059 mContent.removeAllViews();
1060
1061 for (int i = 0; i < list.size(); i++) {
1062 View v = list.get(i);
1063 mContent.getVacantCell(vacant, 1, 1);
1064 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) v.getLayoutParams();
1065 lp.cellX = vacant[0];
1066 lp.cellY = vacant[1];
1067 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohen2792a332011-09-26 21:09:47 -07001068 if (info.cellX != vacant[0] || info.cellY != vacant[1]) {
1069 info.cellX = vacant[0];
1070 info.cellY = vacant[1];
1071 LauncherModel.addOrMoveItemInDatabase(mLauncher, info, mInfo.id, 0,
1072 info.cellX, info.cellY);
1073 }
Adam Cohen2801caf2011-05-13 20:57:39 -07001074 boolean insert = false;
1075 mContent.addViewToCellLayout(v, insert ? 0 : -1, (int)info.id, lp, true);
1076 }
Adam Cohen7c693212011-05-18 15:26:57 -07001077 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -07001078 }
1079
Adam Cohena9cf38f2011-05-02 15:36:58 -07001080 public int getItemCount() {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001081 return mContent.getShortcutsAndWidgets().getChildCount();
Adam Cohena9cf38f2011-05-02 15:36:58 -07001082 }
1083
1084 public View getItemAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001085 return mContent.getShortcutsAndWidgets().getChildAt(index);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001086 }
1087
Adam Cohen2801caf2011-05-13 20:57:39 -07001088 private void onCloseComplete() {
Adam Cohen05e0f402011-08-01 12:12:49 -07001089 DragLayer parent = (DragLayer) getParent();
Michael Jurka5649c282012-06-18 10:33:21 -07001090 if (parent != null) {
1091 parent.removeView(this);
1092 }
Adam Cohen4554ee12011-08-03 16:13:21 -07001093 mDragController.removeDropTarget((DropTarget) this);
Adam Cohen05e0f402011-08-01 12:12:49 -07001094 clearFocus();
Adam Cohenac56cff2011-09-28 20:45:37 -07001095 mFolderIcon.requestFocus();
Adam Cohen05e0f402011-08-01 12:12:49 -07001096
Adam Cohen2801caf2011-05-13 20:57:39 -07001097 if (mRearrangeOnClose) {
1098 setupContentForNumItems(getItemCount());
1099 mRearrangeOnClose = false;
1100 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001101 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001102 if (!mDragInProgress && !mSuppressFolderDeletion) {
1103 replaceFolderWithFinalItem();
1104 } else if (mDragInProgress) {
1105 mDeleteFolderOnDropCompleted = true;
1106 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001107 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001108 mSuppressFolderDeletion = false;
Adam Cohenafb01ee2011-06-23 15:38:03 -07001109 }
1110
1111 private void replaceFolderWithFinalItem() {
Adam Cohenafb01ee2011-06-23 15:38:03 -07001112 // Add the last remaining child to the workspace in place of the folder
Adam Cohenfb91f302012-06-11 15:45:18 -07001113 Runnable onCompleteRunnable = new Runnable() {
1114 @Override
1115 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -07001116 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screenId);
Adam Cohenafb01ee2011-06-23 15:38:03 -07001117
Winson Chung33231f52013-12-09 16:57:45 -08001118 View child = null;
Adam Cohenfb91f302012-06-11 15:45:18 -07001119 // Move the item from the folder to the workspace, in the position of the folder
1120 if (getItemCount() == 1) {
1121 ShortcutInfo finalItem = mInfo.contents.get(0);
Adam Cohenc5e63f32012-07-12 16:16:57 -07001122 child = mLauncher.createShortcut(R.layout.application, cellLayout,
Adam Cohenfb91f302012-06-11 15:45:18 -07001123 finalItem);
1124 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -07001125 mInfo.screenId, mInfo.cellX, mInfo.cellY);
Adam Cohenfb91f302012-06-11 15:45:18 -07001126 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001127 if (getItemCount() <= 1) {
1128 // Remove the folder
1129 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
Dan Sandler0eb687f2014-01-10 13:24:55 -05001130 if (cellLayout != null) {
1131 // b/12446428 -- sometimes the cell layout has already gone away?
1132 cellLayout.removeView(mFolderIcon);
1133 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001134 if (mFolderIcon instanceof DropTarget) {
1135 mDragController.removeDropTarget((DropTarget) mFolderIcon);
1136 }
1137 mLauncher.removeFolder(mInfo);
1138 }
Adam Cohenc5e63f32012-07-12 16:16:57 -07001139 // We add the child after removing the folder to prevent both from existing at
Winson Chung0e6a7132013-08-23 12:55:10 -07001140 // the same time in the CellLayout. We need to add the new item with addInScreenFromBind()
1141 // to ensure that hotseat items are placed correctly.
Adam Cohenc5e63f32012-07-12 16:16:57 -07001142 if (child != null) {
Winson Chung0e6a7132013-08-23 12:55:10 -07001143 mLauncher.getWorkspace().addInScreenFromBind(child, mInfo.container, mInfo.screenId,
Adam Cohenc5e63f32012-07-12 16:16:57 -07001144 mInfo.cellX, mInfo.cellY, mInfo.spanX, mInfo.spanY);
1145 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001146 }
1147 };
1148 View finalChild = getItemAt(0);
1149 if (finalChild != null) {
1150 mFolderIcon.performDestroyAnimation(finalChild, onCompleteRunnable);
Winson Chung33231f52013-12-09 16:57:45 -08001151 } else {
1152 onCompleteRunnable.run();
Adam Cohenafb01ee2011-06-23 15:38:03 -07001153 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001154 mDestroyed = true;
1155 }
1156
1157 boolean isDestroyed() {
1158 return mDestroyed;
Adam Cohen2801caf2011-05-13 20:57:39 -07001159 }
1160
Adam Cohenac56cff2011-09-28 20:45:37 -07001161 // This method keeps track of the last item in the folder for the purposes
1162 // of keyboard focus
1163 private void updateTextViewFocus() {
1164 View lastChild = getItemAt(getItemCount() - 1);
1165 getItemAt(getItemCount() - 1);
1166 if (lastChild != null) {
1167 mFolderName.setNextFocusDownId(lastChild.getId());
1168 mFolderName.setNextFocusRightId(lastChild.getId());
1169 mFolderName.setNextFocusLeftId(lastChild.getId());
1170 mFolderName.setNextFocusUpId(lastChild.getId());
1171 }
1172 }
1173
Adam Cohenbfbfd262011-06-13 16:55:12 -07001174 public void onDrop(DragObject d) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001175 Runnable cleanUpRunnable = null;
1176
Adam Cohen689ff162014-05-08 17:27:56 -07001177 // If we are coming from All Apps space, we defer removing the extra empty screen
1178 // until the folder closes
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001179 if (d.dragSource != mLauncher.getWorkspace() && !(d.dragSource instanceof Folder)) {
1180 cleanUpRunnable = new Runnable() {
1181 @Override
1182 public void run() {
Adam Cohen689ff162014-05-08 17:27:56 -07001183 mLauncher.exitSpringLoadedDragModeDelayed(true,
1184 Launcher.EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
1185 null);
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001186 }
1187 };
Adam Cohenbfbfd262011-06-13 16:55:12 -07001188 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001189
1190 View currentDragView;
1191 ShortcutInfo si = mCurrentDragInfo;
1192 if (mIsExternalDrag) {
1193 si.cellX = mEmptyCell[0];
1194 si.cellY = mEmptyCell[1];
1195 currentDragView = createAndAddShortcut(si);
1196 } else {
1197 currentDragView = mCurrentDragView;
1198 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) currentDragView.getLayoutParams();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001199 si.cellX = lp.cellX = mEmptyCell[0];
1200 si.cellX = lp.cellY = mEmptyCell[1];
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001201 mContent.addViewToCellLayout(currentDragView, -1, (int) si.id, lp, true);
Adam Cohenbfbfd262011-06-13 16:55:12 -07001202 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001203
1204 if (d.dragView.hasDrawn()) {
1205
1206 // Temporarily reset the scale such that the animation target gets calculated correctly.
1207 float scaleX = getScaleX();
1208 float scaleY = getScaleY();
1209 setScaleX(1.0f);
1210 setScaleY(1.0f);
1211 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, currentDragView,
1212 cleanUpRunnable, null);
1213 setScaleX(scaleX);
1214 setScaleY(scaleY);
1215 } else {
1216 d.deferDragViewCleanupPostAnimation = false;
1217 currentDragView.setVisibility(VISIBLE);
1218 }
1219 mItemsInvalidated = true;
1220 setupContentDimensions(getItemCount());
1221
1222 // Actually move the item in the database if it was an external drag.
1223 if (mIsExternalDrag) {
1224 LauncherModel.addOrMoveItemInDatabase(
1225 mLauncher, si, mInfo.id, 0, si.cellX, si.cellY);
1226
1227 // We only need to update the locations if it doesn't get handled in #onDropCompleted.
1228 if (d.dragSource != this) {
1229 updateItemLocationsInDatabaseBatch();
1230 }
1231 mIsExternalDrag = false;
1232 }
1233
1234 // Temporarily suppress the listener, as we did all the work already here.
1235 mSuppressOnAdd = true;
1236 mInfo.add(si);
1237 mSuppressOnAdd = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001238 }
1239
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001240 // This is used so the item doesn't immediately appear in the folder when added. In one case
1241 // we need to create the illusion that the item isn't added back to the folder yet, to
1242 // to correspond to the animation of the icon back into the folder. This is
1243 public void hideItem(ShortcutInfo info) {
1244 View v = getViewForInfo(info);
1245 v.setVisibility(INVISIBLE);
1246 }
1247 public void showItem(ShortcutInfo info) {
1248 View v = getViewForInfo(info);
1249 v.setVisibility(VISIBLE);
1250 }
1251
Adam Cohenbfbfd262011-06-13 16:55:12 -07001252 public void onAdd(ShortcutInfo item) {
1253 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001254 // If the item was dropped onto this open folder, we have done the work associated
1255 // with adding the item to the folder, as indicated by mSuppressOnAdd being set
Adam Cohenbfbfd262011-06-13 16:55:12 -07001256 if (mSuppressOnAdd) return;
1257 if (!findAndSetEmptyCells(item)) {
1258 // The current layout is full, can we expand it?
1259 setupContentForNumItems(getItemCount() + 1);
1260 findAndSetEmptyCells(item);
1261 }
1262 createAndAddShortcut(item);
1263 LauncherModel.addOrMoveItemInDatabase(
1264 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
1265 }
1266
Adam Cohena9cf38f2011-05-02 15:36:58 -07001267 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -07001268 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001269 // If this item is being dragged from this open folder, we have already handled
1270 // the work associated with removing the item, so we don't have to do anything here.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001271 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001272 View v = getViewForInfo(item);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001273 mContent.removeView(v);
Adam Cohen2801caf2011-05-13 20:57:39 -07001274 if (mState == STATE_ANIMATING) {
1275 mRearrangeOnClose = true;
1276 } else {
1277 setupContentForNumItems(getItemCount());
1278 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001279 if (getItemCount() <= 1) {
1280 replaceFolderWithFinalItem();
1281 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001282 }
Adam Cohen7c693212011-05-18 15:26:57 -07001283
Adam Cohendf1e4e82011-06-24 15:57:39 -07001284 private View getViewForInfo(ShortcutInfo item) {
1285 for (int j = 0; j < mContent.getCountY(); j++) {
1286 for (int i = 0; i < mContent.getCountX(); i++) {
1287 View v = mContent.getChildAt(i, j);
1288 if (v.getTag() == item) {
1289 return v;
1290 }
1291 }
1292 }
1293 return null;
1294 }
1295
Adam Cohen76078c42011-06-09 15:06:52 -07001296 public void onItemsChanged() {
Adam Cohenac56cff2011-09-28 20:45:37 -07001297 updateTextViewFocus();
Adam Cohen76078c42011-06-09 15:06:52 -07001298 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001299
Adam Cohen76fc0852011-06-17 13:26:23 -07001300 public void onTitleChanged(CharSequence title) {
1301 }
Adam Cohen76078c42011-06-09 15:06:52 -07001302
Adam Cohen7c693212011-05-18 15:26:57 -07001303 public ArrayList<View> getItemsInReadingOrder() {
1304 if (mItemsInvalidated) {
1305 mItemsInReadingOrder.clear();
1306 for (int j = 0; j < mContent.getCountY(); j++) {
1307 for (int i = 0; i < mContent.getCountX(); i++) {
1308 View v = mContent.getChildAt(i, j);
1309 if (v != null) {
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001310 mItemsInReadingOrder.add(v);
Adam Cohen7c693212011-05-18 15:26:57 -07001311 }
1312 }
1313 }
1314 mItemsInvalidated = false;
1315 }
1316 return mItemsInReadingOrder;
1317 }
Adam Cohen8dfcba42011-07-07 16:38:18 -07001318
1319 public void getLocationInDragLayer(int[] loc) {
1320 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
1321 }
Adam Cohenea0818d2011-09-30 20:06:58 -07001322
1323 public void onFocusChange(View v, boolean hasFocus) {
1324 if (v == mFolderName && hasFocus) {
1325 startEditingFolderName();
1326 }
1327 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001328
1329 @Override
1330 public void getHitRectRelativeToDragLayer(Rect outRect) {
1331 getHitRect(outRect);
1332 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001333}