blob: 41815b60370fed3c0b639cb126fed2da54de03a3 [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;
Adam Cohenbadf71e2011-05-26 19:08:29 -070027import android.graphics.drawable.Drawable;
Alan Viverette4cda5b72013-08-28 17:53:41 -070028import android.os.SystemClock;
Bjorn Bringertb66773c2013-09-25 16:12:24 +010029import android.support.v4.widget.AutoScrollHelper;
Adam Cohen7a14d0b2011-06-24 11:36:35 -070030import android.text.InputType;
Adam Cohene601a432011-07-26 21:51:30 -070031import android.text.Selection;
32import android.text.Spannable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.util.AttributeSet;
Adam Cohen3bf84d32012-05-07 20:17:14 -070034import android.util.Log;
Adam Cohen76fc0852011-06-17 13:26:23 -070035import android.view.ActionMode;
36import android.view.KeyEvent;
Adam Cohendf2cc412011-04-27 16:56:57 -070037import android.view.LayoutInflater;
Adam Cohen76fc0852011-06-17 13:26:23 -070038import android.view.Menu;
39import android.view.MenuItem;
Adam Cohen0c872ba2011-05-05 10:34:16 -070040import android.view.MotionEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.view.View;
Adam Cohen3371da02011-10-25 21:38:29 -070042import android.view.accessibility.AccessibilityEvent;
43import android.view.accessibility.AccessibilityManager;
Adam Cohen76fc0852011-06-17 13:26:23 -070044import android.view.inputmethod.EditorInfo;
45import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.widget.LinearLayout;
Adam Cohenf0f4eda2013-06-06 21:27:03 -070047import android.widget.ScrollView;
Adam Cohendf2cc412011-04-27 16:56:57 -070048import android.widget.TextView;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049
Daniel Sandler325dc232013-06-05 22:57:57 -040050import com.android.launcher3.FolderInfo.FolderListener;
Romain Guyedcce092010-03-04 13:03:17 -080051
Adam Cohenc0dcf592011-06-01 15:30:43 -070052import java.util.ArrayList;
Adam Cohen3bf84d32012-05-07 20:17:14 -070053import java.util.Collections;
54import java.util.Comparator;
Adam Cohenc0dcf592011-06-01 15:30:43 -070055
The Android Open Source Project31dd5032009-03-03 19:32:27 -080056/**
57 * Represents a set of icons chosen by the user or generated by the system.
58 */
Adam Cohen8dfcba42011-07-07 16:38:18 -070059public class Folder extends LinearLayout implements DragSource, View.OnClickListener,
Adam Cohenea0818d2011-09-30 20:06:58 -070060 View.OnLongClickListener, DropTarget, FolderListener, TextView.OnEditorActionListener,
61 View.OnFocusChangeListener {
Adam Cohendf2cc412011-04-27 16:56:57 -070062 private static final String TAG = "Launcher.Folder";
63
Adam Cohen4eac29a2011-07-11 17:53:37 -070064 protected DragController mDragController;
65 protected Launcher mLauncher;
66 protected FolderInfo mInfo;
67
Adam Cohendf2cc412011-04-27 16:56:57 -070068 static final int STATE_NONE = -1;
69 static final int STATE_SMALL = 0;
70 static final int STATE_ANIMATING = 1;
71 static final int STATE_OPEN = 2;
72
73 private int mExpandDuration;
74 protected CellLayout mContent;
Adam Cohenf0f4eda2013-06-06 21:27:03 -070075 private ScrollView mScrollView;
Adam Cohendf2cc412011-04-27 16:56:57 -070076 private final LayoutInflater mInflater;
77 private final IconCache mIconCache;
78 private int mState = STATE_NONE;
Adam Cohenbfbfd262011-06-13 16:55:12 -070079 private static final int REORDER_ANIMATION_DURATION = 230;
Adam Cohenf0f4eda2013-06-06 21:27:03 -070080 private static final int REORDER_DELAY = 250;
Adam Cohen5d518fa2013-12-05 14:16:23 -080081 private static final int ON_EXIT_CLOSE_DELAY = 400;
Adam Cohen2801caf2011-05-13 20:57:39 -070082 private boolean mRearrangeOnClose = false;
83 private FolderIcon mFolderIcon;
84 private int mMaxCountX;
85 private int mMaxCountY;
Adam Cohen78dc83e2011-11-15 17:10:00 -080086 private int mMaxNumItems;
Adam Cohen7c693212011-05-18 15:26:57 -070087 private ArrayList<View> mItemsInReadingOrder = new ArrayList<View>();
Adam Cohenbadf71e2011-05-26 19:08:29 -070088 private Drawable mIconDrawable;
Adam Cohen7c693212011-05-18 15:26:57 -070089 boolean mItemsInvalidated = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -070090 private ShortcutInfo mCurrentDragInfo;
91 private View mCurrentDragView;
92 boolean mSuppressOnAdd = false;
93 private int[] mTargetCell = new int[2];
94 private int[] mPreviousTargetCell = new int[2];
95 private int[] mEmptyCell = new int[2];
96 private Alarm mReorderAlarm = new Alarm();
97 private Alarm mOnExitAlarm = new Alarm();
Adam Cohen76fc0852011-06-17 13:26:23 -070098 private int mFolderNameHeight;
Adam Cohen8e776a62011-06-28 18:10:06 -070099 private Rect mTempRect = new Rect();
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700100 private boolean mDragInProgress = false;
101 private boolean mDeleteFolderOnDropCompleted = false;
102 private boolean mSuppressFolderDeletion = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700103 private boolean mItemAddedBackToSelfViaIcon = false;
Adam Cohenac56cff2011-09-28 20:45:37 -0700104 FolderEditText mFolderName;
Adam Cohen268c4752012-06-06 17:47:33 -0700105 private float mFolderIconPivotX;
106 private float mFolderIconPivotY;
Adam Cohen228da5a2011-07-27 22:23:47 -0700107
Adam Cohen76fc0852011-06-17 13:26:23 -0700108 private boolean mIsEditingName = false;
109 private InputMethodManager mInputMethodManager;
Adam Cohendf2cc412011-04-27 16:56:57 -0700110
Adam Cohena65beee2011-06-27 21:32:23 -0700111 private static String sDefaultFolderName;
112 private static String sHintText;
113
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700114 private int DRAG_MODE_NONE = 0;
115 private int DRAG_MODE_REORDER = 1;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700116 private int mDragMode = DRAG_MODE_NONE;
117
Adam Cohen1960ea42013-11-12 11:33:14 +0000118 // We avoid measuring the scroll view with a 0 width or height, as this
119 // results in CellLayout being measured as UNSPECIFIED, which it does
120 // not support.
121 private static final int MIN_CONTENT_DIMEN = 5;
122
Adam Cohenfb91f302012-06-11 15:45:18 -0700123 private boolean mDestroyed;
124
Alan Viverette4cda5b72013-08-28 17:53:41 -0700125 private AutoScrollHelper mAutoScrollHelper;
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700126
Michael Jurka1e2f4652013-07-08 18:03:46 -0700127 private Runnable mDeferredAction;
128 private boolean mDeferDropAfterUninstall;
129 private boolean mUninstallSuccessful;
130
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131 /**
132 * Used to inflate the Workspace from XML.
133 *
134 * @param context The application's context.
135 * @param attrs The attribtues set containing the Workspace's customization values.
136 */
137 public Folder(Context context, AttributeSet attrs) {
138 super(context, attrs);
Winson Chung5f8afe62013-08-12 16:19:28 -0700139
140 LauncherAppState app = LauncherAppState.getInstance();
141 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800142 setAlwaysDrawnWithCacheEnabled(false);
Adam Cohendf2cc412011-04-27 16:56:57 -0700143 mInflater = LayoutInflater.from(context);
Winson Chung5f8afe62013-08-12 16:19:28 -0700144 mIconCache = app.getIconCache();
Adam Cohen78dc83e2011-11-15 17:10:00 -0800145
146 Resources res = getResources();
Michael Jurka414300a2013-08-27 15:42:35 +0200147 mMaxCountX = (int) grid.numColumns;
Nilesh Agrawal5f7099a2014-01-02 15:54:57 -0800148 // Allow scrolling folders when DISABLE_ALL_APPS is true.
149 if (AppsCustomizePagedView.DISABLE_ALL_APPS) {
150 mMaxCountY = mMaxNumItems = Integer.MAX_VALUE;
151 } else {
152 mMaxCountY = (int) grid.numRows;
153 mMaxNumItems = mMaxCountX * mMaxCountY;
154 }
Adam Cohen76fc0852011-06-17 13:26:23 -0700155
156 mInputMethodManager = (InputMethodManager)
Michael Jurka8b805b12012-04-18 14:23:14 -0700157 getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
Adam Cohen76fc0852011-06-17 13:26:23 -0700158
Adam Cohen76fc0852011-06-17 13:26:23 -0700159 mExpandDuration = res.getInteger(R.integer.config_folderAnimDuration);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700160
161 if (sDefaultFolderName == null) {
162 sDefaultFolderName = res.getString(R.string.folder_name);
163 }
Adam Cohena65beee2011-06-27 21:32:23 -0700164 if (sHintText == null) {
165 sHintText = res.getString(R.string.folder_hint_text);
166 }
Adam Cohen4eac29a2011-07-11 17:53:37 -0700167 mLauncher = (Launcher) context;
Adam Cohenac56cff2011-09-28 20:45:37 -0700168 // We need this view to be focusable in touch mode so that when text editing of the folder
169 // name is complete, we have something to focus on, thus hiding the cursor and giving
170 // reliable behvior when clicking the text field (since it will always gain focus on click).
171 setFocusableInTouchMode(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172 }
173
174 @Override
175 protected void onFinishInflate() {
176 super.onFinishInflate();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700177 mScrollView = (ScrollView) findViewById(R.id.scroll_view);
Adam Cohendf2cc412011-04-27 16:56:57 -0700178 mContent = (CellLayout) findViewById(R.id.folder_content);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700179
Winson Chung5f8afe62013-08-12 16:19:28 -0700180 LauncherAppState app = LauncherAppState.getInstance();
181 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
182
Winson Chung5f8afe62013-08-12 16:19:28 -0700183 mContent.setCellDimensions(grid.folderCellWidthPx, grid.folderCellHeightPx);
Adam Cohen2801caf2011-05-13 20:57:39 -0700184 mContent.setGridSize(0, 0);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700185 mContent.getShortcutsAndWidgets().setMotionEventSplittingEnabled(false);
Adam Cohen2374abf2013-04-16 14:56:57 -0700186 mContent.setInvertIfRtl(true);
Adam Cohenac56cff2011-09-28 20:45:37 -0700187 mFolderName = (FolderEditText) findViewById(R.id.folder_name);
188 mFolderName.setFolder(this);
Adam Cohenea0818d2011-09-30 20:06:58 -0700189 mFolderName.setOnFocusChangeListener(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700190
191 // We find out how tall the text view wants to be (it is set to wrap_content), so that
192 // we can allocate the appropriate amount of space for it.
193 int measureSpec = MeasureSpec.UNSPECIFIED;
194 mFolderName.measure(measureSpec, measureSpec);
195 mFolderNameHeight = mFolderName.getMeasuredHeight();
196
197 // We disable action mode for now since it messes up the view on phones
198 mFolderName.setCustomSelectionActionModeCallback(mActionModeCallback);
Adam Cohen76fc0852011-06-17 13:26:23 -0700199 mFolderName.setOnEditorActionListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700200 mFolderName.setSelectAllOnFocus(true);
Adam Cohen7a14d0b2011-06-24 11:36:35 -0700201 mFolderName.setInputType(mFolderName.getInputType() |
202 InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
Alan Viverette4cda5b72013-08-28 17:53:41 -0700203 mAutoScrollHelper = new FolderAutoScrollHelper(mScrollView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800204 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700205
Adam Cohen76fc0852011-06-17 13:26:23 -0700206 private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
207 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
208 return false;
209 }
210
211 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
212 return false;
213 }
214
215 public void onDestroyActionMode(ActionMode mode) {
216 }
217
218 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
219 return false;
220 }
221 };
222
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800223 public void onClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700224 Object tag = v.getTag();
225 if (tag instanceof ShortcutInfo) {
Adam Cohenb5fe60c2013-06-06 22:03:51 -0700226 mLauncher.onClick(v);
Adam Cohendf2cc412011-04-27 16:56:57 -0700227 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800228 }
229
230 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -0700231 // Return if global dragging is not enabled
232 if (!mLauncher.isDraggingEnabled()) return true;
233
Adam Cohendf2cc412011-04-27 16:56:57 -0700234 Object tag = v.getTag();
235 if (tag instanceof ShortcutInfo) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700236 ShortcutInfo item = (ShortcutInfo) tag;
237 if (!v.isInTouchMode()) {
238 return false;
239 }
240
Winson Chunga6945242014-01-08 14:04:34 -0800241 mLauncher.getLauncherClings().dismissFolderCling(null);
Winson Chung7d7541e2011-09-16 20:14:36 -0700242
Adam Cohendf2cc412011-04-27 16:56:57 -0700243 mLauncher.getWorkspace().onDragStartedWithItem(v);
Adam Cohenac8c8762011-07-13 11:15:27 -0700244 mLauncher.getWorkspace().beginDragShared(v, this);
Adam Cohenbadf71e2011-05-26 19:08:29 -0700245 mIconDrawable = ((TextView) v).getCompoundDrawables()[1];
Adam Cohen76078c42011-06-09 15:06:52 -0700246
247 mCurrentDragInfo = item;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700248 mEmptyCell[0] = item.cellX;
249 mEmptyCell[1] = item.cellY;
250 mCurrentDragView = v;
Adam Cohenfc53cd22011-07-20 15:45:11 -0700251
252 mContent.removeView(mCurrentDragView);
253 mInfo.remove(mCurrentDragInfo);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700254 mDragInProgress = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700255 mItemAddedBackToSelfViaIcon = false;
Adam Cohendf2cc412011-04-27 16:56:57 -0700256 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800257 return true;
258 }
259
Adam Cohen76fc0852011-06-17 13:26:23 -0700260 public boolean isEditingName() {
261 return mIsEditingName;
262 }
263
264 public void startEditingFolderName() {
Adam Cohena65beee2011-06-27 21:32:23 -0700265 mFolderName.setHint("");
Adam Cohen76fc0852011-06-17 13:26:23 -0700266 mIsEditingName = true;
267 }
268
269 public void dismissEditingName() {
270 mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
271 doneEditingFolderName(true);
272 }
273
274 public void doneEditingFolderName(boolean commit) {
Adam Cohena65beee2011-06-27 21:32:23 -0700275 mFolderName.setHint(sHintText);
Adam Cohen1df26a32011-08-12 16:15:23 -0700276 // Convert to a string here to ensure that no other state associated with the text field
277 // gets saved.
Adam Cohen3371da02011-10-25 21:38:29 -0700278 String newTitle = mFolderName.getText().toString();
279 mInfo.setTitle(newTitle);
Adam Cohen76fc0852011-06-17 13:26:23 -0700280 LauncherModel.updateItemInDatabase(mLauncher, mInfo);
Adam Cohenac56cff2011-09-28 20:45:37 -0700281
Adam Cohen3371da02011-10-25 21:38:29 -0700282 if (commit) {
283 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700284 String.format(getContext().getString(R.string.folder_renamed), newTitle));
Adam Cohen3371da02011-10-25 21:38:29 -0700285 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700286 // In order to clear the focus from the text field, we set the focus on ourself. This
287 // ensures that every time the field is clicked, focus is gained, giving reliable behavior.
288 requestFocus();
289
Adam Cohene601a432011-07-26 21:51:30 -0700290 Selection.setSelection((Spannable) mFolderName.getText(), 0, 0);
Adam Cohen76fc0852011-06-17 13:26:23 -0700291 mIsEditingName = false;
292 }
293
294 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
295 if (actionId == EditorInfo.IME_ACTION_DONE) {
296 dismissEditingName();
297 return true;
298 }
299 return false;
300 }
301
302 public View getEditTextRegion() {
303 return mFolderName;
304 }
305
Adam Cohenbadf71e2011-05-26 19:08:29 -0700306 public Drawable getDragDrawable() {
307 return mIconDrawable;
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);
Adam Cohenc508b2d2011-06-28 14:41:44 -0700384 if (!createAndAddShortcut(child)) {
385 overflow.add(child);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700386 } else {
387 count++;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700388 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700389 }
Adam Cohenc508b2d2011-06-28 14:41:44 -0700390
Adam Cohen0057bbc2011-08-12 18:30:51 -0700391 // We rearrange the items in case there are any empty gaps
392 setupContentForNumItems(count);
393
Adam Cohenc508b2d2011-06-28 14:41:44 -0700394 // If our folder has too many items we prune them from the list. This is an issue
395 // when upgrading from the old Folders implementation which could contain an unlimited
396 // number of items.
397 for (ShortcutInfo item: overflow) {
398 mInfo.remove(item);
399 LauncherModel.deleteItemFromDatabase(mLauncher, item);
400 }
401
Adam Cohen4dbe6d92011-05-18 17:14:15 -0700402 mItemsInvalidated = true;
Adam Cohenac56cff2011-09-28 20:45:37 -0700403 updateTextViewFocus();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700404 mInfo.addListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700405
Adam Cohenafb01ee2011-06-23 15:38:03 -0700406 if (!sDefaultFolderName.contentEquals(mInfo.title)) {
Adam Cohen4ef610f2011-06-21 22:37:36 -0700407 mFolderName.setText(mInfo.title);
408 } else {
409 mFolderName.setText("");
410 }
Adam Cohen691a5792012-05-11 14:27:30 -0700411 updateItemLocationsInDatabase();
Winson Chung33231f52013-12-09 16:57:45 -0800412
413 // In case any children didn't come across during loading, clean up the folder accordingly
414 mFolderIcon.post(new Runnable() {
415 public void run() {
416 if (getItemCount() <= 1) {
417 replaceFolderWithFinalItem();
418 }
419 }
420 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700421 }
422
423 /**
424 * Creates a new UserFolder, inflated from R.layout.user_folder.
425 *
426 * @param context The application's context.
427 *
428 * @return A new UserFolder.
429 */
430 static Folder fromXml(Context context) {
431 return (Folder) LayoutInflater.from(context).inflate(R.layout.user_folder, null);
432 }
433
434 /**
435 * This method is intended to make the UserFolder to be visually identical in size and position
436 * to its associated FolderIcon. This allows for a seamless transition into the expanded state.
437 */
438 private void positionAndSizeAsIcon() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700439 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800440 setScaleX(0.8f);
441 setScaleY(0.8f);
442 setAlpha(0f);
Adam Cohendf2cc412011-04-27 16:56:57 -0700443 mState = STATE_SMALL;
444 }
445
446 public void animateOpen() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700447 positionAndSizeAsIcon();
448
Adam Cohen8e776a62011-06-28 18:10:06 -0700449 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen2801caf2011-05-13 20:57:39 -0700450 centerAboutIcon();
Adam Cohen662b5982011-12-13 17:45:21 -0800451 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1);
452 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
453 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
Michael Jurka032e6ba2013-04-22 15:08:42 +0200454 final ObjectAnimator oa =
Michael Jurka2ecf9952012-06-18 12:52:28 -0700455 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohen6441de02011-12-14 14:25:32 -0800456
Adam Cohen2801caf2011-05-13 20:57:39 -0700457 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700458 @Override
459 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700460 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700461 String.format(getContext().getString(R.string.folder_opened),
Adam Cohen3371da02011-10-25 21:38:29 -0700462 mContent.getCountX(), mContent.getCountY()));
Adam Cohendf2cc412011-04-27 16:56:57 -0700463 mState = STATE_ANIMATING;
464 }
465 @Override
466 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700467 mState = STATE_OPEN;
Michael Jurka0121c3e2012-05-31 08:36:04 -0700468 setLayerType(LAYER_TYPE_NONE, null);
Winson Chunga6945242014-01-08 14:04:34 -0800469 Cling cling = mLauncher.getLauncherClings().showFoldersCling();
Winson Chung7a74ac92011-09-20 17:43:51 -0700470 if (cling != null) {
Winson Chungfa545132013-09-26 17:45:32 -0700471 cling.bringScrimToFront();
Winson Chungaf40f202013-09-18 18:26:31 -0700472 bringToFront();
Winson Chungfa545132013-09-26 17:45:32 -0700473 cling.bringToFront();
Winson Chung7a74ac92011-09-20 17:43:51 -0700474 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700475 setFocusOnFirstChild();
Adam Cohendf2cc412011-04-27 16:56:57 -0700476 }
477 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700478 oa.setDuration(mExpandDuration);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700479 setLayerType(LAYER_TYPE_HARDWARE, null);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100480 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700481 }
482
Adam Cohen3371da02011-10-25 21:38:29 -0700483 private void sendCustomAccessibilityEvent(int type, String text) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700484 AccessibilityManager accessibilityManager = (AccessibilityManager)
485 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
486 if (accessibilityManager.isEnabled()) {
Adam Cohen3371da02011-10-25 21:38:29 -0700487 AccessibilityEvent event = AccessibilityEvent.obtain(type);
488 onInitializeAccessibilityEvent(event);
489 event.getText().add(text);
Michael Jurka8b805b12012-04-18 14:23:14 -0700490 accessibilityManager.sendAccessibilityEvent(event);
Adam Cohen3371da02011-10-25 21:38:29 -0700491 }
492 }
493
Adam Cohenac56cff2011-09-28 20:45:37 -0700494 private void setFocusOnFirstChild() {
495 View firstChild = mContent.getChildAt(0, 0);
496 if (firstChild != null) {
497 firstChild.requestFocus();
498 }
499 }
500
Adam Cohendf2cc412011-04-27 16:56:57 -0700501 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700502 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800503 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
504 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
505 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
Michael Jurka032e6ba2013-04-22 15:08:42 +0200506 final ObjectAnimator oa =
Michael Jurka2ecf9952012-06-18 12:52:28 -0700507 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohendf2cc412011-04-27 16:56:57 -0700508
Adam Cohen2801caf2011-05-13 20:57:39 -0700509 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700510 @Override
511 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700512 onCloseComplete();
Michael Jurka0121c3e2012-05-31 08:36:04 -0700513 setLayerType(LAYER_TYPE_NONE, null);
Adam Cohen2801caf2011-05-13 20:57:39 -0700514 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700515 }
516 @Override
517 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700518 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700519 getContext().getString(R.string.folder_closed));
Adam Cohendf2cc412011-04-27 16:56:57 -0700520 mState = STATE_ANIMATING;
521 }
522 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700523 oa.setDuration(mExpandDuration);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700524 setLayerType(LAYER_TYPE_HARDWARE, null);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100525 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700526 }
527
Adam Cohencb3382b2011-05-24 14:07:08 -0700528 public boolean acceptDrop(DragObject d) {
529 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700530 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700531 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
532 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
533 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700534 }
535
Adam Cohendf2cc412011-04-27 16:56:57 -0700536 protected boolean findAndSetEmptyCells(ShortcutInfo item) {
537 int[] emptyCell = new int[2];
538 if (mContent.findCellForSpan(emptyCell, item.spanX, item.spanY)) {
539 item.cellX = emptyCell[0];
540 item.cellY = emptyCell[1];
Adam Cohendf2cc412011-04-27 16:56:57 -0700541 return true;
542 } else {
543 return false;
544 }
545 }
546
Adam Cohenc508b2d2011-06-28 14:41:44 -0700547 protected boolean createAndAddShortcut(ShortcutInfo item) {
Adam Cohen477828c2013-09-20 12:05:49 -0700548 final BubbleTextView textView =
549 (BubbleTextView) mInflater.inflate(R.layout.application, this, false);
Winson Chung54000492013-10-14 16:29:29 -0700550 textView.setCompoundDrawables(null,
Winson Chung0dbd7342013-10-13 22:46:20 -0700551 Utilities.createIconDrawable(item.getIcon(mIconCache)), null, null);
Adam Cohendf2cc412011-04-27 16:56:57 -0700552 textView.setText(item.title);
553 textView.setTag(item);
Adam Cohen477828c2013-09-20 12:05:49 -0700554 textView.setTextColor(getResources().getColor(R.color.folder_items_text_color));
555 textView.setShadowsEnabled(false);
Adam Cohen7397e622013-10-24 12:11:34 -0700556 textView.setGlowColor(getResources().getColor(R.color.folder_items_glow_color));
Adam Cohendf2cc412011-04-27 16:56:57 -0700557
558 textView.setOnClickListener(this);
559 textView.setOnLongClickListener(this);
560
Adam Cohenc508b2d2011-06-28 14:41:44 -0700561 // We need to check here to verify that the given item's location isn't already occupied
Adam Cohen3bf84d32012-05-07 20:17:14 -0700562 // by another item.
Adam Cohen0057bbc2011-08-12 18:30:51 -0700563 if (mContent.getChildAt(item.cellX, item.cellY) != null || item.cellX < 0 || item.cellY < 0
564 || item.cellX >= mContent.getCountX() || item.cellY >= mContent.getCountY()) {
Adam Cohen3bf84d32012-05-07 20:17:14 -0700565 // This shouldn't happen, log it.
566 Log.e(TAG, "Folder order not properly persisted during bind");
Adam Cohenc508b2d2011-06-28 14:41:44 -0700567 if (!findAndSetEmptyCells(item)) {
568 return false;
569 }
570 }
571
Adam Cohendf2cc412011-04-27 16:56:57 -0700572 CellLayout.LayoutParams lp =
573 new CellLayout.LayoutParams(item.cellX, item.cellY, item.spanX, item.spanY);
574 boolean insert = false;
Adam Cohenac56cff2011-09-28 20:45:37 -0700575 textView.setOnKeyListener(new FolderKeyEventListener());
Adam Cohendf2cc412011-04-27 16:56:57 -0700576 mContent.addViewToCellLayout(textView, insert ? 0 : -1, (int)item.id, lp, true);
Adam Cohenc508b2d2011-06-28 14:41:44 -0700577 return true;
Adam Cohendf2cc412011-04-27 16:56:57 -0700578 }
579
Adam Cohencb3382b2011-05-24 14:07:08 -0700580 public void onDragEnter(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700581 mPreviousTargetCell[0] = -1;
582 mPreviousTargetCell[1] = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700583 mOnExitAlarm.cancelAlarm();
584 }
585
586 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
587 public void onAlarm(Alarm alarm) {
588 realTimeReorder(mEmptyCell, mTargetCell);
589 }
590 };
591
592 boolean readingOrderGreaterThan(int[] v1, int[] v2) {
593 if (v1[1] > v2[1] || (v1[1] == v2[1] && v1[0] > v2[0])) {
594 return true;
595 } else {
596 return false;
597 }
598 }
599
600 private void realTimeReorder(int[] empty, int[] target) {
601 boolean wrap;
602 int startX;
603 int endX;
604 int startY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700605 int delay = 0;
606 float delayAmount = 30;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700607 if (readingOrderGreaterThan(target, empty)) {
608 wrap = empty[0] >= mContent.getCountX() - 1;
609 startY = wrap ? empty[1] + 1 : empty[1];
610 for (int y = startY; y <= target[1]; y++) {
611 startX = y == empty[1] ? empty[0] + 1 : 0;
612 endX = y < target[1] ? mContent.getCountX() - 1 : target[0];
613 for (int x = startX; x <= endX; x++) {
614 View v = mContent.getChildAt(x,y);
615 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen482ed822012-03-02 14:15:13 -0800616 REORDER_ANIMATION_DURATION, delay, true, true)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700617 empty[0] = x;
618 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700619 delay += delayAmount;
620 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700621 }
622 }
623 }
624 } else {
625 wrap = empty[0] == 0;
626 startY = wrap ? empty[1] - 1 : empty[1];
627 for (int y = startY; y >= target[1]; y--) {
628 startX = y == empty[1] ? empty[0] - 1 : mContent.getCountX() - 1;
629 endX = y > target[1] ? 0 : target[0];
630 for (int x = startX; x >= endX; x--) {
631 View v = mContent.getChildAt(x,y);
632 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen482ed822012-03-02 14:15:13 -0800633 REORDER_ANIMATION_DURATION, delay, true, true)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700634 empty[0] = x;
635 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700636 delay += delayAmount;
637 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700638 }
639 }
640 }
641 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700642 }
643
Adam Cohen2374abf2013-04-16 14:56:57 -0700644 public boolean isLayoutRtl() {
645 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
646 }
647
Adam Cohencb3382b2011-05-24 14:07:08 -0700648 public void onDragOver(DragObject d) {
Alan Viverette4cda5b72013-08-28 17:53:41 -0700649 final DragView dragView = d.dragView;
650 final int scrollOffset = mScrollView.getScrollY();
651 final float[] r = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, dragView, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700652 r[0] -= getPaddingLeft();
653 r[1] -= getPaddingTop();
654
Alan Viverette4cda5b72013-08-28 17:53:41 -0700655 final long downTime = SystemClock.uptimeMillis();
656 final MotionEvent translatedEv = MotionEvent.obtain(
657 downTime, downTime, MotionEvent.ACTION_MOVE, d.x, d.y, 0);
658
659 if (!mAutoScrollHelper.isEnabled()) {
660 mAutoScrollHelper.setEnabled(true);
661 }
662
663 final boolean handled = mAutoScrollHelper.onTouch(this, translatedEv);
664 translatedEv.recycle();
665
666 if (handled) {
667 mReorderAlarm.cancelAlarm();
668 } else {
669 mTargetCell = mContent.findNearestArea(
670 (int) r[0], (int) r[1] + scrollOffset, 1, 1, mTargetCell);
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700671 if (isLayoutRtl()) {
672 mTargetCell[0] = mContent.getCountX() - mTargetCell[0] - 1;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700673 }
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700674 if (mTargetCell[0] != mPreviousTargetCell[0]
675 || mTargetCell[1] != mPreviousTargetCell[1]) {
676 mReorderAlarm.cancelAlarm();
677 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
678 mReorderAlarm.setAlarm(REORDER_DELAY);
679 mPreviousTargetCell[0] = mTargetCell[0];
680 mPreviousTargetCell[1] = mTargetCell[1];
681 mDragMode = DRAG_MODE_REORDER;
682 } else {
683 mDragMode = DRAG_MODE_NONE;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700684 }
Adam Cohenbfbfd262011-06-13 16:55:12 -0700685 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700686 }
687
Adam Cohenbfbfd262011-06-13 16:55:12 -0700688 // This is used to compute the visual center of the dragView. The idea is that
689 // the visual center represents the user's interpretation of where the item is, and hence
690 // is the appropriate point to use when determining drop location.
691 private float[] getDragViewVisualCenter(int x, int y, int xOffset, int yOffset,
692 DragView dragView, float[] recycle) {
693 float res[];
694 if (recycle == null) {
695 res = new float[2];
696 } else {
697 res = recycle;
698 }
699
700 // These represent the visual top and left of drag view if a dragRect was provided.
701 // If a dragRect was not provided, then they correspond to the actual view left and
702 // top, as the dragRect is in that case taken to be the entire dragView.
703 // R.dimen.dragViewOffsetY.
704 int left = x - xOffset;
705 int top = y - yOffset;
706
707 // In order to find the visual center, we shift by half the dragRect
708 res[0] = left + dragView.getDragRegion().width() / 2;
709 res[1] = top + dragView.getDragRegion().height() / 2;
710
711 return res;
712 }
713
714 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
715 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700716 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700717 }
718 };
719
Adam Cohen95bb8002011-07-03 23:40:28 -0700720 public void completeDragExit() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700721 mLauncher.closeFolder();
722 mCurrentDragInfo = null;
723 mCurrentDragView = null;
724 mSuppressOnAdd = false;
725 mRearrangeOnClose = true;
726 }
727
Adam Cohencb3382b2011-05-24 14:07:08 -0700728 public void onDragExit(DragObject d) {
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700729 // Exiting folder; stop the auto scroller.
Alan Viverette4cda5b72013-08-28 17:53:41 -0700730 mAutoScrollHelper.setEnabled(false);
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700731 // We only close the folder if this is a true drag exit, ie. not because
732 // a drop has occurred above the folder.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700733 if (!d.dragComplete) {
734 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
735 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
736 }
737 mReorderAlarm.cancelAlarm();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700738 mDragMode = DRAG_MODE_NONE;
Adam Cohen2801caf2011-05-13 20:57:39 -0700739 }
740
Michael Jurka1e2f4652013-07-08 18:03:46 -0700741 public void onDropCompleted(final View target, final DragObject d,
742 final boolean isFlingToDelete, final boolean success) {
743 if (mDeferDropAfterUninstall) {
Michael Jurkaf3007582013-08-21 14:33:57 +0200744 Log.d(TAG, "Deferred handling drop because waiting for uninstall.");
Michael Jurka1e2f4652013-07-08 18:03:46 -0700745 mDeferredAction = new Runnable() {
746 public void run() {
747 onDropCompleted(target, d, isFlingToDelete, success);
748 mDeferredAction = null;
749 }
750 };
751 return;
752 }
753
754 boolean beingCalledAfterUninstall = mDeferredAction != null;
755 boolean successfulDrop =
756 success && (!beingCalledAfterUninstall || mUninstallSuccessful);
Winson Chung5f8afe62013-08-12 16:19:28 -0700757
Michael Jurka1e2f4652013-07-08 18:03:46 -0700758 if (successfulDrop) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700759 if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700760 replaceFolderWithFinalItem();
761 }
762 } else {
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700763 setupContentForNumItems(getItemCount());
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700764 // The drag failed, we need to return the item to the folder
765 mFolderIcon.onDrop(d);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700766 }
767
768 if (target != this) {
769 if (mOnExitAlarm.alarmPending()) {
770 mOnExitAlarm.cancelAlarm();
Michael Jurka54554252013-08-01 12:52:23 +0200771 if (!successfulDrop) {
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700772 mSuppressFolderDeletion = true;
773 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700774 completeDragExit();
775 }
Adam Cohen9c58d822013-12-13 17:18:10 -0800776 }
777
778 // This is kind of hacky, but in general, dropping on the workspace handles removing
779 // the extra screen, but dropping elsewhere (back to self, or onto delete) doesn't.
780 if (target != mLauncher.getWorkspace()) {
Adam Cohenf0f2ffa2013-11-12 11:44:58 +0000781 mLauncher.getWorkspace().removeExtraEmptyScreen(true, null);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700782 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700783
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700784 mDeleteFolderOnDropCompleted = false;
785 mDragInProgress = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700786 mItemAddedBackToSelfViaIcon = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700787 mCurrentDragInfo = null;
788 mCurrentDragView = null;
789 mSuppressOnAdd = false;
Adam Cohen4045eb72011-10-06 11:44:26 -0700790
791 // Reordering may have occured, and we need to save the new item locations. We do this once
792 // at the end to prevent unnecessary database operations.
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700793 updateItemLocationsInDatabaseBatch();
Adam Cohen4045eb72011-10-06 11:44:26 -0700794 }
795
Michael Jurka1e2f4652013-07-08 18:03:46 -0700796 public void deferCompleteDropAfterUninstallActivity() {
797 mDeferDropAfterUninstall = true;
798 }
799
800 public void onUninstallActivityReturned(boolean success) {
801 mDeferDropAfterUninstall = false;
802 mUninstallSuccessful = success;
803 if (mDeferredAction != null) {
804 mDeferredAction.run();
805 }
806 }
807
Winson Chunga48487a2012-03-20 16:19:37 -0700808 @Override
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800809 public float getIntrinsicIconScaleFactor() {
810 return 1f;
811 }
812
813 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800814 public boolean supportsFlingToDelete() {
815 return true;
816 }
817
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000818 @Override
819 public boolean supportsAppInfoDropTarget() {
820 return false;
821 }
822
823 @Override
824 public boolean supportsDeleteDropTarget() {
825 return true;
826 }
827
Winson Chunga48487a2012-03-20 16:19:37 -0700828 public void onFlingToDelete(DragObject d, int x, int y, PointF vec) {
829 // Do nothing
830 }
831
832 @Override
833 public void onFlingToDeleteCompleted() {
834 // Do nothing
835 }
836
Adam Cohen4045eb72011-10-06 11:44:26 -0700837 private void updateItemLocationsInDatabase() {
838 ArrayList<View> list = getItemsInReadingOrder();
839 for (int i = 0; i < list.size(); i++) {
840 View v = list.get(i);
841 ItemInfo info = (ItemInfo) v.getTag();
842 LauncherModel.moveItemInDatabase(mLauncher, info, mInfo.id, 0,
Winson Chung33231f52013-12-09 16:57:45 -0800843 info.cellX, info.cellY);
Adam Cohen4045eb72011-10-06 11:44:26 -0700844 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700845 }
Adam Cohen228da5a2011-07-27 22:23:47 -0700846
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700847 private void updateItemLocationsInDatabaseBatch() {
848 ArrayList<View> list = getItemsInReadingOrder();
849 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
850 for (int i = 0; i < list.size(); i++) {
851 View v = list.get(i);
852 ItemInfo info = (ItemInfo) v.getTag();
853 items.add(info);
854 }
855
856 LauncherModel.moveItemsInDatabase(mLauncher, items, mInfo.id, 0);
857 }
858
Adam Cohene25af792013-06-06 23:08:25 -0700859 public void addItemLocationsInDatabase() {
860 ArrayList<View> list = getItemsInReadingOrder();
861 for (int i = 0; i < list.size(); i++) {
862 View v = list.get(i);
863 ItemInfo info = (ItemInfo) v.getTag();
864 LauncherModel.addItemToDatabase(mLauncher, info, mInfo.id, 0,
865 info.cellX, info.cellY, false);
866 }
867 }
868
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700869 public void notifyDrop() {
870 if (mDragInProgress) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700871 mItemAddedBackToSelfViaIcon = true;
Adam Cohen76078c42011-06-09 15:06:52 -0700872 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700873 }
874
875 public boolean isDropEnabled() {
876 return true;
877 }
878
Adam Cohen4045eb72011-10-06 11:44:26 -0700879 private void setupContentDimensions(int count) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700880 ArrayList<View> list = getItemsInReadingOrder();
881
882 int countX = mContent.getCountX();
883 int countY = mContent.getCountY();
Adam Cohen7c693212011-05-18 15:26:57 -0700884 boolean done = false;
Adam Cohen2801caf2011-05-13 20:57:39 -0700885
Adam Cohen7c693212011-05-18 15:26:57 -0700886 while (!done) {
887 int oldCountX = countX;
888 int oldCountY = countY;
889 if (countX * countY < count) {
890 // Current grid is too small, expand it
Adam Cohen78dc83e2011-11-15 17:10:00 -0800891 if ((countX <= countY || countY == mMaxCountY) && countX < mMaxCountX) {
Adam Cohen7c693212011-05-18 15:26:57 -0700892 countX++;
893 } else if (countY < mMaxCountY) {
894 countY++;
895 }
896 if (countY == 0) countY++;
897 } else if ((countY - 1) * countX >= count && countY >= countX) {
898 countY = Math.max(0, countY - 1);
899 } else if ((countX - 1) * countY >= count) {
900 countX = Math.max(0, countX - 1);
Adam Cohen2801caf2011-05-13 20:57:39 -0700901 }
Adam Cohen7c693212011-05-18 15:26:57 -0700902 done = countX == oldCountX && countY == oldCountY;
Adam Cohen2801caf2011-05-13 20:57:39 -0700903 }
Adam Cohen7c693212011-05-18 15:26:57 -0700904 mContent.setGridSize(countX, countY);
Adam Cohen2801caf2011-05-13 20:57:39 -0700905 arrangeChildren(list);
906 }
907
908 public boolean isFull() {
Adam Cohen78dc83e2011-11-15 17:10:00 -0800909 return getItemCount() >= mMaxNumItems;
Adam Cohen2801caf2011-05-13 20:57:39 -0700910 }
911
912 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700913 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700914
Winson Chung892c74d2013-08-22 16:15:50 -0700915 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700916 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700917 int height = getFolderHeight();
Adam Cohen2801caf2011-05-13 20:57:39 -0700918
Adam Cohen307fe232012-08-16 17:55:58 -0700919 float scale = parent.getDescendantRectRelativeToSelf(mFolderIcon, mTempRect);
Adam Cohen8e776a62011-06-28 18:10:06 -0700920
Winson Chungaf40f202013-09-18 18:26:31 -0700921 LauncherAppState app = LauncherAppState.getInstance();
922 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
923
Adam Cohen307fe232012-08-16 17:55:58 -0700924 int centerX = (int) (mTempRect.left + mTempRect.width() * scale / 2);
925 int centerY = (int) (mTempRect.top + mTempRect.height() * scale / 2);
Adam Cohen2801caf2011-05-13 20:57:39 -0700926 int centeredLeft = centerX - width / 2;
927 int centeredTop = centerY - height / 2;
Winson Chung3057b1c2013-10-10 17:35:35 -0700928 int currentPage = mLauncher.getWorkspace().getNextPage();
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800929 // In case the workspace is scrolling, we need to use the final scroll to compute
930 // the folders bounds.
931 mLauncher.getWorkspace().setFinalScrollForPageChange(currentPage);
Adam Cohen35e7e642011-07-17 14:47:18 -0700932 // We first fetch the currently visible CellLayoutChildren
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800933 CellLayout currentLayout = (CellLayout) mLauncher.getWorkspace().getChildAt(currentPage);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700934 ShortcutAndWidgetContainer boundingLayout = currentLayout.getShortcutsAndWidgets();
Adam Cohen35e7e642011-07-17 14:47:18 -0700935 Rect bounds = new Rect();
936 parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800937 // We reset the workspaces scroll
938 mLauncher.getWorkspace().resetFinalScrollForPageChange(currentPage);
Adam Cohen2801caf2011-05-13 20:57:39 -0700939
Adam Cohen35e7e642011-07-17 14:47:18 -0700940 // We need to bound the folder to the currently visible CellLayoutChildren
941 int left = Math.min(Math.max(bounds.left, centeredLeft),
942 bounds.left + bounds.width() - width);
943 int top = Math.min(Math.max(bounds.top, centeredTop),
944 bounds.top + bounds.height() - height);
Winson Chungaf40f202013-09-18 18:26:31 -0700945 if (grid.isPhone() && (grid.availableWidthPx - width) < grid.iconSizePx) {
946 // Center the folder if it is full (on phones only)
947 left = (grid.availableWidthPx - width) / 2;
948 } else if (width >= bounds.width()) {
949 // If the folder doesn't fit within the bounds, center it about the desired bounds
Adam Cohen35e7e642011-07-17 14:47:18 -0700950 left = bounds.left + (bounds.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700951 }
Adam Cohen35e7e642011-07-17 14:47:18 -0700952 if (height >= bounds.height()) {
953 top = bounds.top + (bounds.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700954 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700955
956 int folderPivotX = width / 2 + (centeredLeft - left);
957 int folderPivotY = height / 2 + (centeredTop - top);
958 setPivotX(folderPivotX);
959 setPivotY(folderPivotY);
Adam Cohen268c4752012-06-06 17:47:33 -0700960 mFolderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700961 (1.0f * folderPivotX / width));
Adam Cohen268c4752012-06-06 17:47:33 -0700962 mFolderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700963 (1.0f * folderPivotY / height));
Adam Cohen3bf84d32012-05-07 20:17:14 -0700964
Adam Cohen662b5982011-12-13 17:45:21 -0800965 lp.width = width;
966 lp.height = height;
967 lp.x = left;
968 lp.y = top;
Adam Cohen2801caf2011-05-13 20:57:39 -0700969 }
970
Adam Cohen268c4752012-06-06 17:47:33 -0700971 float getPivotXForIconAnimation() {
972 return mFolderIconPivotX;
973 }
974 float getPivotYForIconAnimation() {
975 return mFolderIconPivotY;
976 }
977
Adam Cohen2801caf2011-05-13 20:57:39 -0700978 private void setupContentForNumItems(int count) {
Adam Cohen4045eb72011-10-06 11:44:26 -0700979 setupContentDimensions(count);
Adam Cohen2801caf2011-05-13 20:57:39 -0700980
Adam Cohen8e776a62011-06-28 18:10:06 -0700981 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700982 if (lp == null) {
Adam Cohen8e776a62011-06-28 18:10:06 -0700983 lp = new DragLayer.LayoutParams(0, 0);
984 lp.customPosition = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700985 setLayoutParams(lp);
986 }
987 centerAboutIcon();
988 }
989
Winson Chung892c74d2013-08-22 16:15:50 -0700990 private int getContentAreaHeight() {
991 LauncherAppState app = LauncherAppState.getInstance();
992 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
993 Rect workspacePadding = grid.getWorkspacePadding(grid.isLandscape ?
994 CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
995 int maxContentAreaHeight = grid.availableHeightPx -
Winson Chung892c74d2013-08-22 16:15:50 -0700996 workspacePadding.top - workspacePadding.bottom -
Winson Chung892c74d2013-08-22 16:15:50 -0700997 mFolderNameHeight;
Adam Cohen1960ea42013-11-12 11:33:14 +0000998 int height = Math.min(maxContentAreaHeight,
Winson Chung892c74d2013-08-22 16:15:50 -0700999 mContent.getDesiredHeight());
Adam Cohen1960ea42013-11-12 11:33:14 +00001000 return Math.max(height, MIN_CONTENT_DIMEN);
1001 }
1002
1003 private int getContentAreaWidth() {
1004 return Math.max(mContent.getDesiredWidth(), MIN_CONTENT_DIMEN);
Winson Chung892c74d2013-08-22 16:15:50 -07001005 }
1006
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001007 private int getFolderHeight() {
Winson Chung892c74d2013-08-22 16:15:50 -07001008 int height = getPaddingTop() + getPaddingBottom()
1009 + getContentAreaHeight() + mFolderNameHeight;
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001010 return height;
1011 }
Adam Cohen234c4cd2011-07-17 21:03:04 -07001012
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001013 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001014 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
1015 int height = getFolderHeight();
Adam Cohen1960ea42013-11-12 11:33:14 +00001016 int contentAreaWidthSpec = MeasureSpec.makeMeasureSpec(getContentAreaWidth(),
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001017 MeasureSpec.EXACTLY);
Winson Chung892c74d2013-08-22 16:15:50 -07001018 int contentAreaHeightSpec = MeasureSpec.makeMeasureSpec(getContentAreaHeight(),
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001019 MeasureSpec.EXACTLY);
Adam Cohen1960ea42013-11-12 11:33:14 +00001020
Nilesh Agrawal5f7099a2014-01-02 15:54:57 -08001021 if (AppsCustomizePagedView.DISABLE_ALL_APPS) {
1022 // Don't cap the height of the content to allow scrolling.
1023 mContent.setFixedSize(getContentAreaWidth(), mContent.getDesiredHeight());
1024 } else {
1025 mContent.setFixedSize(getContentAreaWidth(), getContentAreaHeight());
1026 }
1027
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001028 mScrollView.measure(contentAreaWidthSpec, contentAreaHeightSpec);
1029 mFolderName.measure(contentAreaWidthSpec,
Adam Cohen234c4cd2011-07-17 21:03:04 -07001030 MeasureSpec.makeMeasureSpec(mFolderNameHeight, MeasureSpec.EXACTLY));
1031 setMeasuredDimension(width, height);
1032 }
1033
Adam Cohen2801caf2011-05-13 20:57:39 -07001034 private void arrangeChildren(ArrayList<View> list) {
1035 int[] vacant = new int[2];
1036 if (list == null) {
1037 list = getItemsInReadingOrder();
1038 }
1039 mContent.removeAllViews();
1040
1041 for (int i = 0; i < list.size(); i++) {
1042 View v = list.get(i);
1043 mContent.getVacantCell(vacant, 1, 1);
1044 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) v.getLayoutParams();
1045 lp.cellX = vacant[0];
1046 lp.cellY = vacant[1];
1047 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohen2792a332011-09-26 21:09:47 -07001048 if (info.cellX != vacant[0] || info.cellY != vacant[1]) {
1049 info.cellX = vacant[0];
1050 info.cellY = vacant[1];
1051 LauncherModel.addOrMoveItemInDatabase(mLauncher, info, mInfo.id, 0,
1052 info.cellX, info.cellY);
1053 }
Adam Cohen2801caf2011-05-13 20:57:39 -07001054 boolean insert = false;
1055 mContent.addViewToCellLayout(v, insert ? 0 : -1, (int)info.id, lp, true);
1056 }
Adam Cohen7c693212011-05-18 15:26:57 -07001057 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -07001058 }
1059
Adam Cohena9cf38f2011-05-02 15:36:58 -07001060 public int getItemCount() {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001061 return mContent.getShortcutsAndWidgets().getChildCount();
Adam Cohena9cf38f2011-05-02 15:36:58 -07001062 }
1063
1064 public View getItemAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001065 return mContent.getShortcutsAndWidgets().getChildAt(index);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001066 }
1067
Adam Cohen2801caf2011-05-13 20:57:39 -07001068 private void onCloseComplete() {
Adam Cohen05e0f402011-08-01 12:12:49 -07001069 DragLayer parent = (DragLayer) getParent();
Michael Jurka5649c282012-06-18 10:33:21 -07001070 if (parent != null) {
1071 parent.removeView(this);
1072 }
Adam Cohen4554ee12011-08-03 16:13:21 -07001073 mDragController.removeDropTarget((DropTarget) this);
Adam Cohen05e0f402011-08-01 12:12:49 -07001074 clearFocus();
Adam Cohenac56cff2011-09-28 20:45:37 -07001075 mFolderIcon.requestFocus();
Adam Cohen05e0f402011-08-01 12:12:49 -07001076
Adam Cohen2801caf2011-05-13 20:57:39 -07001077 if (mRearrangeOnClose) {
1078 setupContentForNumItems(getItemCount());
1079 mRearrangeOnClose = false;
1080 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001081 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001082 if (!mDragInProgress && !mSuppressFolderDeletion) {
1083 replaceFolderWithFinalItem();
1084 } else if (mDragInProgress) {
1085 mDeleteFolderOnDropCompleted = true;
1086 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001087 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001088 mSuppressFolderDeletion = false;
Adam Cohenafb01ee2011-06-23 15:38:03 -07001089 }
1090
1091 private void replaceFolderWithFinalItem() {
Adam Cohenafb01ee2011-06-23 15:38:03 -07001092 // Add the last remaining child to the workspace in place of the folder
Adam Cohenfb91f302012-06-11 15:45:18 -07001093 Runnable onCompleteRunnable = new Runnable() {
1094 @Override
1095 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -07001096 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screenId);
Adam Cohenafb01ee2011-06-23 15:38:03 -07001097
Winson Chung33231f52013-12-09 16:57:45 -08001098 View child = null;
Adam Cohenfb91f302012-06-11 15:45:18 -07001099 // Move the item from the folder to the workspace, in the position of the folder
1100 if (getItemCount() == 1) {
1101 ShortcutInfo finalItem = mInfo.contents.get(0);
Adam Cohenc5e63f32012-07-12 16:16:57 -07001102 child = mLauncher.createShortcut(R.layout.application, cellLayout,
Adam Cohenfb91f302012-06-11 15:45:18 -07001103 finalItem);
1104 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -07001105 mInfo.screenId, mInfo.cellX, mInfo.cellY);
Adam Cohenfb91f302012-06-11 15:45:18 -07001106 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001107 if (getItemCount() <= 1) {
1108 // Remove the folder
1109 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
1110 cellLayout.removeView(mFolderIcon);
1111 if (mFolderIcon instanceof DropTarget) {
1112 mDragController.removeDropTarget((DropTarget) mFolderIcon);
1113 }
1114 mLauncher.removeFolder(mInfo);
1115 }
Adam Cohenc5e63f32012-07-12 16:16:57 -07001116 // We add the child after removing the folder to prevent both from existing at
Winson Chung0e6a7132013-08-23 12:55:10 -07001117 // the same time in the CellLayout. We need to add the new item with addInScreenFromBind()
1118 // to ensure that hotseat items are placed correctly.
Adam Cohenc5e63f32012-07-12 16:16:57 -07001119 if (child != null) {
Winson Chung0e6a7132013-08-23 12:55:10 -07001120 mLauncher.getWorkspace().addInScreenFromBind(child, mInfo.container, mInfo.screenId,
Adam Cohenc5e63f32012-07-12 16:16:57 -07001121 mInfo.cellX, mInfo.cellY, mInfo.spanX, mInfo.spanY);
1122 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001123 }
1124 };
1125 View finalChild = getItemAt(0);
1126 if (finalChild != null) {
1127 mFolderIcon.performDestroyAnimation(finalChild, onCompleteRunnable);
Winson Chung33231f52013-12-09 16:57:45 -08001128 } else {
1129 onCompleteRunnable.run();
Adam Cohenafb01ee2011-06-23 15:38:03 -07001130 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001131 mDestroyed = true;
1132 }
1133
1134 boolean isDestroyed() {
1135 return mDestroyed;
Adam Cohen2801caf2011-05-13 20:57:39 -07001136 }
1137
Adam Cohenac56cff2011-09-28 20:45:37 -07001138 // This method keeps track of the last item in the folder for the purposes
1139 // of keyboard focus
1140 private void updateTextViewFocus() {
1141 View lastChild = getItemAt(getItemCount() - 1);
1142 getItemAt(getItemCount() - 1);
1143 if (lastChild != null) {
1144 mFolderName.setNextFocusDownId(lastChild.getId());
1145 mFolderName.setNextFocusRightId(lastChild.getId());
1146 mFolderName.setNextFocusLeftId(lastChild.getId());
1147 mFolderName.setNextFocusUpId(lastChild.getId());
1148 }
1149 }
1150
Adam Cohenbfbfd262011-06-13 16:55:12 -07001151 public void onDrop(DragObject d) {
1152 ShortcutInfo item;
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001153 if (d.dragInfo instanceof AppInfo) {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001154 // Came from all apps -- make a copy
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001155 item = ((AppInfo) d.dragInfo).makeShortcut();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001156 item.spanX = 1;
1157 item.spanY = 1;
1158 } else {
1159 item = (ShortcutInfo) d.dragInfo;
1160 }
Adam Cohen05e0f402011-08-01 12:12:49 -07001161 // Dragged from self onto self, currently this is the only path possible, however
1162 // we keep this as a distinct code path.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001163 if (item == mCurrentDragInfo) {
1164 ShortcutInfo si = (ShortcutInfo) mCurrentDragView.getTag();
1165 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) mCurrentDragView.getLayoutParams();
1166 si.cellX = lp.cellX = mEmptyCell[0];
1167 si.cellX = lp.cellY = mEmptyCell[1];
1168 mContent.addViewToCellLayout(mCurrentDragView, -1, (int)item.id, lp, true);
Adam Cohenfc53cd22011-07-20 15:45:11 -07001169 if (d.dragView.hasDrawn()) {
1170 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, mCurrentDragView);
1171 } else {
Winson Chung2c4cf412012-05-08 14:03:21 -07001172 d.deferDragViewCleanupPostAnimation = false;
Adam Cohenfc53cd22011-07-20 15:45:11 -07001173 mCurrentDragView.setVisibility(VISIBLE);
1174 }
Adam Cohene9166b22011-07-08 17:11:11 -07001175 mItemsInvalidated = true;
Adam Cohen4045eb72011-10-06 11:44:26 -07001176 setupContentDimensions(getItemCount());
Adam Cohen716b51e2011-06-30 12:09:54 -07001177 mSuppressOnAdd = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001178 }
1179 mInfo.add(item);
1180 }
1181
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001182 // This is used so the item doesn't immediately appear in the folder when added. In one case
1183 // we need to create the illusion that the item isn't added back to the folder yet, to
1184 // to correspond to the animation of the icon back into the folder. This is
1185 public void hideItem(ShortcutInfo info) {
1186 View v = getViewForInfo(info);
1187 v.setVisibility(INVISIBLE);
1188 }
1189 public void showItem(ShortcutInfo info) {
1190 View v = getViewForInfo(info);
1191 v.setVisibility(VISIBLE);
1192 }
1193
Adam Cohenbfbfd262011-06-13 16:55:12 -07001194 public void onAdd(ShortcutInfo item) {
1195 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001196 // If the item was dropped onto this open folder, we have done the work associated
1197 // with adding the item to the folder, as indicated by mSuppressOnAdd being set
Adam Cohenbfbfd262011-06-13 16:55:12 -07001198 if (mSuppressOnAdd) return;
1199 if (!findAndSetEmptyCells(item)) {
1200 // The current layout is full, can we expand it?
1201 setupContentForNumItems(getItemCount() + 1);
1202 findAndSetEmptyCells(item);
1203 }
1204 createAndAddShortcut(item);
1205 LauncherModel.addOrMoveItemInDatabase(
1206 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
1207 }
1208
Adam Cohena9cf38f2011-05-02 15:36:58 -07001209 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -07001210 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001211 // If this item is being dragged from this open folder, we have already handled
1212 // the work associated with removing the item, so we don't have to do anything here.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001213 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001214 View v = getViewForInfo(item);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001215 mContent.removeView(v);
Adam Cohen2801caf2011-05-13 20:57:39 -07001216 if (mState == STATE_ANIMATING) {
1217 mRearrangeOnClose = true;
1218 } else {
1219 setupContentForNumItems(getItemCount());
1220 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001221 if (getItemCount() <= 1) {
1222 replaceFolderWithFinalItem();
1223 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001224 }
Adam Cohen7c693212011-05-18 15:26:57 -07001225
Adam Cohendf1e4e82011-06-24 15:57:39 -07001226 private View getViewForInfo(ShortcutInfo item) {
1227 for (int j = 0; j < mContent.getCountY(); j++) {
1228 for (int i = 0; i < mContent.getCountX(); i++) {
1229 View v = mContent.getChildAt(i, j);
1230 if (v.getTag() == item) {
1231 return v;
1232 }
1233 }
1234 }
1235 return null;
1236 }
1237
Adam Cohen76078c42011-06-09 15:06:52 -07001238 public void onItemsChanged() {
Adam Cohenac56cff2011-09-28 20:45:37 -07001239 updateTextViewFocus();
Adam Cohen76078c42011-06-09 15:06:52 -07001240 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001241
Adam Cohen76fc0852011-06-17 13:26:23 -07001242 public void onTitleChanged(CharSequence title) {
1243 }
Adam Cohen76078c42011-06-09 15:06:52 -07001244
Adam Cohen7c693212011-05-18 15:26:57 -07001245 public ArrayList<View> getItemsInReadingOrder() {
1246 if (mItemsInvalidated) {
1247 mItemsInReadingOrder.clear();
1248 for (int j = 0; j < mContent.getCountY(); j++) {
1249 for (int i = 0; i < mContent.getCountX(); i++) {
1250 View v = mContent.getChildAt(i, j);
1251 if (v != null) {
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001252 mItemsInReadingOrder.add(v);
Adam Cohen7c693212011-05-18 15:26:57 -07001253 }
1254 }
1255 }
1256 mItemsInvalidated = false;
1257 }
1258 return mItemsInReadingOrder;
1259 }
Adam Cohen8dfcba42011-07-07 16:38:18 -07001260
1261 public void getLocationInDragLayer(int[] loc) {
1262 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
1263 }
Adam Cohenea0818d2011-09-30 20:06:58 -07001264
1265 public void onFocusChange(View v, boolean hasFocus) {
1266 if (v == mFolderName && hasFocus) {
1267 startEditingFolderName();
1268 }
1269 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001270
1271 @Override
1272 public void getHitRectRelativeToDragLayer(Rect outRect) {
1273 getHitRect(outRect);
1274 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001275}