blob: 2fb8f9256fc23c0c7d664df6311d9b5592764c56 [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;
Winson Chung892c74d2013-08-22 16:15:50 -070034import android.util.DisplayMetrics;
Adam Cohen3bf84d32012-05-07 20:17:14 -070035import android.util.Log;
Winson Chung892c74d2013-08-22 16:15:50 -070036import android.util.TypedValue;
Adam Cohen76fc0852011-06-17 13:26:23 -070037import android.view.ActionMode;
38import android.view.KeyEvent;
Adam Cohendf2cc412011-04-27 16:56:57 -070039import android.view.LayoutInflater;
Adam Cohen76fc0852011-06-17 13:26:23 -070040import android.view.Menu;
41import android.view.MenuItem;
Adam Cohen0c872ba2011-05-05 10:34:16 -070042import android.view.MotionEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.view.View;
Adam Cohen3371da02011-10-25 21:38:29 -070044import android.view.accessibility.AccessibilityEvent;
45import android.view.accessibility.AccessibilityManager;
Mindy DelliCarpini2e7471c2013-06-27 08:45:11 -070046import android.view.animation.AccelerateInterpolator;
47import android.view.animation.Interpolator;
Adam Cohen76fc0852011-06-17 13:26:23 -070048import android.view.inputmethod.EditorInfo;
49import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.widget.LinearLayout;
Adam Cohenf0f4eda2013-06-06 21:27:03 -070051import android.widget.ScrollView;
Adam Cohendf2cc412011-04-27 16:56:57 -070052import android.widget.TextView;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053
Daniel Sandler325dc232013-06-05 22:57:57 -040054import com.android.launcher3.FolderInfo.FolderListener;
Romain Guyedcce092010-03-04 13:03:17 -080055
Adam Cohenc0dcf592011-06-01 15:30:43 -070056import java.util.ArrayList;
Adam Cohen3bf84d32012-05-07 20:17:14 -070057import java.util.Collections;
58import java.util.Comparator;
Adam Cohenc0dcf592011-06-01 15:30:43 -070059
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060/**
61 * Represents a set of icons chosen by the user or generated by the system.
62 */
Adam Cohen8dfcba42011-07-07 16:38:18 -070063public class Folder extends LinearLayout implements DragSource, View.OnClickListener,
Adam Cohenea0818d2011-09-30 20:06:58 -070064 View.OnLongClickListener, DropTarget, FolderListener, TextView.OnEditorActionListener,
65 View.OnFocusChangeListener {
Adam Cohendf2cc412011-04-27 16:56:57 -070066 private static final String TAG = "Launcher.Folder";
67
Adam Cohen4eac29a2011-07-11 17:53:37 -070068 protected DragController mDragController;
69 protected Launcher mLauncher;
70 protected FolderInfo mInfo;
71
Adam Cohendf2cc412011-04-27 16:56:57 -070072 static final int STATE_NONE = -1;
73 static final int STATE_SMALL = 0;
74 static final int STATE_ANIMATING = 1;
75 static final int STATE_OPEN = 2;
76
77 private int mExpandDuration;
78 protected CellLayout mContent;
Adam Cohenf0f4eda2013-06-06 21:27:03 -070079 private ScrollView mScrollView;
Adam Cohendf2cc412011-04-27 16:56:57 -070080 private final LayoutInflater mInflater;
81 private final IconCache mIconCache;
82 private int mState = STATE_NONE;
Adam Cohenbfbfd262011-06-13 16:55:12 -070083 private static final int REORDER_ANIMATION_DURATION = 230;
Adam Cohenf0f4eda2013-06-06 21:27:03 -070084 private static final int REORDER_DELAY = 250;
Adam Cohenbfbfd262011-06-13 16:55:12 -070085 private static final int ON_EXIT_CLOSE_DELAY = 800;
Adam Cohen2801caf2011-05-13 20:57:39 -070086 private boolean mRearrangeOnClose = false;
87 private FolderIcon mFolderIcon;
88 private int mMaxCountX;
89 private int mMaxCountY;
Adam Cohen78dc83e2011-11-15 17:10:00 -080090 private int mMaxNumItems;
Adam Cohen7c693212011-05-18 15:26:57 -070091 private ArrayList<View> mItemsInReadingOrder = new ArrayList<View>();
Adam Cohenbadf71e2011-05-26 19:08:29 -070092 private Drawable mIconDrawable;
Adam Cohen7c693212011-05-18 15:26:57 -070093 boolean mItemsInvalidated = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -070094 private ShortcutInfo mCurrentDragInfo;
95 private View mCurrentDragView;
96 boolean mSuppressOnAdd = false;
97 private int[] mTargetCell = new int[2];
98 private int[] mPreviousTargetCell = new int[2];
99 private int[] mEmptyCell = new int[2];
100 private Alarm mReorderAlarm = new Alarm();
101 private Alarm mOnExitAlarm = new Alarm();
Adam Cohen76fc0852011-06-17 13:26:23 -0700102 private int mFolderNameHeight;
Adam Cohen8e776a62011-06-28 18:10:06 -0700103 private Rect mTempRect = new Rect();
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700104 private boolean mDragInProgress = false;
105 private boolean mDeleteFolderOnDropCompleted = false;
106 private boolean mSuppressFolderDeletion = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700107 private boolean mItemAddedBackToSelfViaIcon = false;
Adam Cohenac56cff2011-09-28 20:45:37 -0700108 FolderEditText mFolderName;
Adam Cohen268c4752012-06-06 17:47:33 -0700109 private float mFolderIconPivotX;
110 private float mFolderIconPivotY;
Adam Cohen228da5a2011-07-27 22:23:47 -0700111
Adam Cohen76fc0852011-06-17 13:26:23 -0700112 private boolean mIsEditingName = false;
113 private InputMethodManager mInputMethodManager;
Adam Cohendf2cc412011-04-27 16:56:57 -0700114
Adam Cohena65beee2011-06-27 21:32:23 -0700115 private static String sDefaultFolderName;
116 private static String sHintText;
117
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700118 private int DRAG_MODE_NONE = 0;
119 private int DRAG_MODE_REORDER = 1;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700120 private int mDragMode = DRAG_MODE_NONE;
121
Adam Cohen1960ea42013-11-12 11:33:14 +0000122 // We avoid measuring the scroll view with a 0 width or height, as this
123 // results in CellLayout being measured as UNSPECIFIED, which it does
124 // not support.
125 private static final int MIN_CONTENT_DIMEN = 5;
126
Adam Cohenfb91f302012-06-11 15:45:18 -0700127 private boolean mDestroyed;
128
Alan Viverette4cda5b72013-08-28 17:53:41 -0700129 private AutoScrollHelper mAutoScrollHelper;
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700130
Michael Jurka1e2f4652013-07-08 18:03:46 -0700131 private Runnable mDeferredAction;
132 private boolean mDeferDropAfterUninstall;
133 private boolean mUninstallSuccessful;
134
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135 /**
136 * Used to inflate the Workspace from XML.
137 *
138 * @param context The application's context.
139 * @param attrs The attribtues set containing the Workspace's customization values.
140 */
141 public Folder(Context context, AttributeSet attrs) {
142 super(context, attrs);
Winson Chung5f8afe62013-08-12 16:19:28 -0700143
144 LauncherAppState app = LauncherAppState.getInstance();
145 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146 setAlwaysDrawnWithCacheEnabled(false);
Adam Cohendf2cc412011-04-27 16:56:57 -0700147 mInflater = LayoutInflater.from(context);
Winson Chung5f8afe62013-08-12 16:19:28 -0700148 mIconCache = app.getIconCache();
Adam Cohen78dc83e2011-11-15 17:10:00 -0800149
150 Resources res = getResources();
Michael Jurka414300a2013-08-27 15:42:35 +0200151 mMaxCountX = (int) grid.numColumns;
Adam Cohen477828c2013-09-20 12:05:49 -0700152 mMaxCountY = (int) grid.numRows;
153 mMaxNumItems = mMaxCountX * mMaxCountY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700154
155 mInputMethodManager = (InputMethodManager)
Michael Jurka8b805b12012-04-18 14:23:14 -0700156 getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
Adam Cohen76fc0852011-06-17 13:26:23 -0700157
Adam Cohen76fc0852011-06-17 13:26:23 -0700158 mExpandDuration = res.getInteger(R.integer.config_folderAnimDuration);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700159
160 if (sDefaultFolderName == null) {
161 sDefaultFolderName = res.getString(R.string.folder_name);
162 }
Adam Cohena65beee2011-06-27 21:32:23 -0700163 if (sHintText == null) {
164 sHintText = res.getString(R.string.folder_hint_text);
165 }
Adam Cohen4eac29a2011-07-11 17:53:37 -0700166 mLauncher = (Launcher) context;
Adam Cohenac56cff2011-09-28 20:45:37 -0700167 // We need this view to be focusable in touch mode so that when text editing of the folder
168 // name is complete, we have something to focus on, thus hiding the cursor and giving
169 // reliable behvior when clicking the text field (since it will always gain focus on click).
170 setFocusableInTouchMode(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171 }
172
173 @Override
174 protected void onFinishInflate() {
175 super.onFinishInflate();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700176 mScrollView = (ScrollView) findViewById(R.id.scroll_view);
Adam Cohendf2cc412011-04-27 16:56:57 -0700177 mContent = (CellLayout) findViewById(R.id.folder_content);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700178
Winson Chung5f8afe62013-08-12 16:19:28 -0700179 LauncherAppState app = LauncherAppState.getInstance();
180 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
181
Winson Chung5f8afe62013-08-12 16:19:28 -0700182 mContent.setCellDimensions(grid.folderCellWidthPx, grid.folderCellHeightPx);
Adam Cohen2801caf2011-05-13 20:57:39 -0700183 mContent.setGridSize(0, 0);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700184 mContent.getShortcutsAndWidgets().setMotionEventSplittingEnabled(false);
Adam Cohen2374abf2013-04-16 14:56:57 -0700185 mContent.setInvertIfRtl(true);
Adam Cohenac56cff2011-09-28 20:45:37 -0700186 mFolderName = (FolderEditText) findViewById(R.id.folder_name);
187 mFolderName.setFolder(this);
Adam Cohenea0818d2011-09-30 20:06:58 -0700188 mFolderName.setOnFocusChangeListener(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700189
190 // We find out how tall the text view wants to be (it is set to wrap_content), so that
191 // we can allocate the appropriate amount of space for it.
192 int measureSpec = MeasureSpec.UNSPECIFIED;
193 mFolderName.measure(measureSpec, measureSpec);
194 mFolderNameHeight = mFolderName.getMeasuredHeight();
195
196 // We disable action mode for now since it messes up the view on phones
197 mFolderName.setCustomSelectionActionModeCallback(mActionModeCallback);
Adam Cohen76fc0852011-06-17 13:26:23 -0700198 mFolderName.setOnEditorActionListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700199 mFolderName.setSelectAllOnFocus(true);
Adam Cohen7a14d0b2011-06-24 11:36:35 -0700200 mFolderName.setInputType(mFolderName.getInputType() |
201 InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
Alan Viverette4cda5b72013-08-28 17:53:41 -0700202 mAutoScrollHelper = new FolderAutoScrollHelper(mScrollView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800203 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700204
Adam Cohen76fc0852011-06-17 13:26:23 -0700205 private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
206 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
207 return false;
208 }
209
210 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
211 return false;
212 }
213
214 public void onDestroyActionMode(ActionMode mode) {
215 }
216
217 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
218 return false;
219 }
220 };
221
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800222 public void onClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700223 Object tag = v.getTag();
224 if (tag instanceof ShortcutInfo) {
Adam Cohenb5fe60c2013-06-06 22:03:51 -0700225 mLauncher.onClick(v);
Adam Cohendf2cc412011-04-27 16:56:57 -0700226 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800227 }
228
229 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -0700230 // Return if global dragging is not enabled
231 if (!mLauncher.isDraggingEnabled()) return true;
232
Adam Cohendf2cc412011-04-27 16:56:57 -0700233 Object tag = v.getTag();
234 if (tag instanceof ShortcutInfo) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700235 ShortcutInfo item = (ShortcutInfo) tag;
236 if (!v.isInTouchMode()) {
237 return false;
238 }
239
Winson Chung7d7541e2011-09-16 20:14:36 -0700240 mLauncher.dismissFolderCling(null);
241
Adam Cohendf2cc412011-04-27 16:56:57 -0700242 mLauncher.getWorkspace().onDragStartedWithItem(v);
Adam Cohenac8c8762011-07-13 11:15:27 -0700243 mLauncher.getWorkspace().beginDragShared(v, this);
Adam Cohenbadf71e2011-05-26 19:08:29 -0700244 mIconDrawable = ((TextView) v).getCompoundDrawables()[1];
Adam Cohen76078c42011-06-09 15:06:52 -0700245
246 mCurrentDragInfo = item;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700247 mEmptyCell[0] = item.cellX;
248 mEmptyCell[1] = item.cellY;
249 mCurrentDragView = v;
Adam Cohenfc53cd22011-07-20 15:45:11 -0700250
251 mContent.removeView(mCurrentDragView);
252 mInfo.remove(mCurrentDragInfo);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700253 mDragInProgress = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700254 mItemAddedBackToSelfViaIcon = false;
Adam Cohendf2cc412011-04-27 16:56:57 -0700255 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800256 return true;
257 }
258
Adam Cohen76fc0852011-06-17 13:26:23 -0700259 public boolean isEditingName() {
260 return mIsEditingName;
261 }
262
263 public void startEditingFolderName() {
Adam Cohena65beee2011-06-27 21:32:23 -0700264 mFolderName.setHint("");
Adam Cohen76fc0852011-06-17 13:26:23 -0700265 mIsEditingName = true;
266 }
267
268 public void dismissEditingName() {
269 mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
270 doneEditingFolderName(true);
271 }
272
273 public void doneEditingFolderName(boolean commit) {
Adam Cohena65beee2011-06-27 21:32:23 -0700274 mFolderName.setHint(sHintText);
Adam Cohen1df26a32011-08-12 16:15:23 -0700275 // Convert to a string here to ensure that no other state associated with the text field
276 // gets saved.
Adam Cohen3371da02011-10-25 21:38:29 -0700277 String newTitle = mFolderName.getText().toString();
278 mInfo.setTitle(newTitle);
Adam Cohen76fc0852011-06-17 13:26:23 -0700279 LauncherModel.updateItemInDatabase(mLauncher, mInfo);
Adam Cohenac56cff2011-09-28 20:45:37 -0700280
Adam Cohen3371da02011-10-25 21:38:29 -0700281 if (commit) {
282 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700283 String.format(getContext().getString(R.string.folder_renamed), newTitle));
Adam Cohen3371da02011-10-25 21:38:29 -0700284 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700285 // In order to clear the focus from the text field, we set the focus on ourself. This
286 // ensures that every time the field is clicked, focus is gained, giving reliable behavior.
287 requestFocus();
288
Adam Cohene601a432011-07-26 21:51:30 -0700289 Selection.setSelection((Spannable) mFolderName.getText(), 0, 0);
Adam Cohen76fc0852011-06-17 13:26:23 -0700290 mIsEditingName = false;
291 }
292
293 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
294 if (actionId == EditorInfo.IME_ACTION_DONE) {
295 dismissEditingName();
296 return true;
297 }
298 return false;
299 }
300
301 public View getEditTextRegion() {
302 return mFolderName;
303 }
304
Adam Cohenbadf71e2011-05-26 19:08:29 -0700305 public Drawable getDragDrawable() {
306 return mIconDrawable;
307 }
308
Adam Cohen0c872ba2011-05-05 10:34:16 -0700309 /**
310 * We need to handle touch events to prevent them from falling through to the workspace below.
311 */
312 @Override
313 public boolean onTouchEvent(MotionEvent ev) {
314 return true;
315 }
316
Joe Onorato00acb122009-08-04 16:04:30 -0400317 public void setDragController(DragController dragController) {
318 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800319 }
320
Adam Cohen2801caf2011-05-13 20:57:39 -0700321 void setFolderIcon(FolderIcon icon) {
322 mFolderIcon = icon;
323 }
324
Adam Cohen3371da02011-10-25 21:38:29 -0700325 @Override
326 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
327 // When the folder gets focus, we don't want to announce the list of items.
328 return true;
329 }
330
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800331 /**
332 * @return the FolderInfo object associated with this folder
333 */
334 FolderInfo getInfo() {
335 return mInfo;
336 }
337
Adam Cohen3bf84d32012-05-07 20:17:14 -0700338 private class GridComparator implements Comparator<ShortcutInfo> {
339 int mNumCols;
340 public GridComparator(int numCols) {
341 mNumCols = numCols;
342 }
343
344 @Override
345 public int compare(ShortcutInfo lhs, ShortcutInfo rhs) {
346 int lhIndex = lhs.cellY * mNumCols + lhs.cellX;
347 int rhIndex = rhs.cellY * mNumCols + rhs.cellX;
348 return (lhIndex - rhIndex);
349 }
Adam Cohen3bf84d32012-05-07 20:17:14 -0700350 }
351
352 private void placeInReadingOrder(ArrayList<ShortcutInfo> items) {
353 int maxX = 0;
354 int count = items.size();
355 for (int i = 0; i < count; i++) {
356 ShortcutInfo item = items.get(i);
357 if (item.cellX > maxX) {
358 maxX = item.cellX;
359 }
360 }
Adam Cohen691a5792012-05-11 14:27:30 -0700361
362 GridComparator gridComparator = new GridComparator(maxX + 1);
Adam Cohen3bf84d32012-05-07 20:17:14 -0700363 Collections.sort(items, gridComparator);
364 final int countX = mContent.getCountX();
365 for (int i = 0; i < count; i++) {
366 int x = i % countX;
367 int y = i / countX;
368 ShortcutInfo item = items.get(i);
369 item.cellX = x;
370 item.cellY = y;
371 }
372 }
373
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800374 void bind(FolderInfo info) {
375 mInfo = info;
Adam Cohendf2cc412011-04-27 16:56:57 -0700376 ArrayList<ShortcutInfo> children = info.contents;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700377 ArrayList<ShortcutInfo> overflow = new ArrayList<ShortcutInfo>();
Adam Cohen7c693212011-05-18 15:26:57 -0700378 setupContentForNumItems(children.size());
Adam Cohen3bf84d32012-05-07 20:17:14 -0700379 placeInReadingOrder(children);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700380 int count = 0;
Adam Cohendf2cc412011-04-27 16:56:57 -0700381 for (int i = 0; i < children.size(); i++) {
382 ShortcutInfo child = (ShortcutInfo) children.get(i);
Adam Cohenc508b2d2011-06-28 14:41:44 -0700383 if (!createAndAddShortcut(child)) {
384 overflow.add(child);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700385 } else {
386 count++;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700387 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700388 }
Adam Cohenc508b2d2011-06-28 14:41:44 -0700389
Adam Cohen0057bbc2011-08-12 18:30:51 -0700390 // We rearrange the items in case there are any empty gaps
391 setupContentForNumItems(count);
392
Adam Cohenc508b2d2011-06-28 14:41:44 -0700393 // If our folder has too many items we prune them from the list. This is an issue
394 // when upgrading from the old Folders implementation which could contain an unlimited
395 // number of items.
396 for (ShortcutInfo item: overflow) {
397 mInfo.remove(item);
398 LauncherModel.deleteItemFromDatabase(mLauncher, item);
399 }
400
Adam Cohen4dbe6d92011-05-18 17:14:15 -0700401 mItemsInvalidated = true;
Adam Cohenac56cff2011-09-28 20:45:37 -0700402 updateTextViewFocus();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700403 mInfo.addListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700404
Adam Cohenafb01ee2011-06-23 15:38:03 -0700405 if (!sDefaultFolderName.contentEquals(mInfo.title)) {
Adam Cohen4ef610f2011-06-21 22:37:36 -0700406 mFolderName.setText(mInfo.title);
407 } else {
408 mFolderName.setText("");
409 }
Adam Cohen691a5792012-05-11 14:27:30 -0700410 updateItemLocationsInDatabase();
Winson Chung68b66782013-12-09 16:57:45 -0800411
412 // In case any children didn't come across during loading, clean up the folder accordingly
413 mFolderIcon.post(new Runnable() {
414 public void run() {
415 if (getItemCount() <= 1) {
416 replaceFolderWithFinalItem();
417 }
418 }
419 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700420 }
421
422 /**
423 * Creates a new UserFolder, inflated from R.layout.user_folder.
424 *
425 * @param context The application's context.
426 *
427 * @return A new UserFolder.
428 */
429 static Folder fromXml(Context context) {
430 return (Folder) LayoutInflater.from(context).inflate(R.layout.user_folder, null);
431 }
432
433 /**
434 * This method is intended to make the UserFolder to be visually identical in size and position
435 * to its associated FolderIcon. This allows for a seamless transition into the expanded state.
436 */
437 private void positionAndSizeAsIcon() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700438 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800439 setScaleX(0.8f);
440 setScaleY(0.8f);
441 setAlpha(0f);
Adam Cohendf2cc412011-04-27 16:56:57 -0700442 mState = STATE_SMALL;
443 }
444
445 public void animateOpen() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700446 positionAndSizeAsIcon();
447
Adam Cohen8e776a62011-06-28 18:10:06 -0700448 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen2801caf2011-05-13 20:57:39 -0700449 centerAboutIcon();
Adam Cohen662b5982011-12-13 17:45:21 -0800450 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1);
451 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
452 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
Michael Jurka032e6ba2013-04-22 15:08:42 +0200453 final ObjectAnimator oa =
Michael Jurka2ecf9952012-06-18 12:52:28 -0700454 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohen6441de02011-12-14 14:25:32 -0800455
Adam Cohen2801caf2011-05-13 20:57:39 -0700456 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700457 @Override
458 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700459 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700460 String.format(getContext().getString(R.string.folder_opened),
Adam Cohen3371da02011-10-25 21:38:29 -0700461 mContent.getCountX(), mContent.getCountY()));
Adam Cohendf2cc412011-04-27 16:56:57 -0700462 mState = STATE_ANIMATING;
463 }
464 @Override
465 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700466 mState = STATE_OPEN;
Michael Jurka0121c3e2012-05-31 08:36:04 -0700467 setLayerType(LAYER_TYPE_NONE, null);
Winson Chung7a74ac92011-09-20 17:43:51 -0700468 Cling cling = mLauncher.showFirstRunFoldersCling();
469 if (cling != null) {
Winson Chungfa545132013-09-26 17:45:32 -0700470 cling.bringScrimToFront();
Winson Chungaf40f202013-09-18 18:26:31 -0700471 bringToFront();
Winson Chungfa545132013-09-26 17:45:32 -0700472 cling.bringToFront();
Winson Chung7a74ac92011-09-20 17:43:51 -0700473 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700474 setFocusOnFirstChild();
Adam Cohendf2cc412011-04-27 16:56:57 -0700475 }
476 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700477 oa.setDuration(mExpandDuration);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700478 setLayerType(LAYER_TYPE_HARDWARE, null);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100479 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700480 }
481
Adam Cohen3371da02011-10-25 21:38:29 -0700482 private void sendCustomAccessibilityEvent(int type, String text) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700483 AccessibilityManager accessibilityManager = (AccessibilityManager)
484 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
485 if (accessibilityManager.isEnabled()) {
Adam Cohen3371da02011-10-25 21:38:29 -0700486 AccessibilityEvent event = AccessibilityEvent.obtain(type);
487 onInitializeAccessibilityEvent(event);
488 event.getText().add(text);
Michael Jurka8b805b12012-04-18 14:23:14 -0700489 accessibilityManager.sendAccessibilityEvent(event);
Adam Cohen3371da02011-10-25 21:38:29 -0700490 }
491 }
492
Adam Cohenac56cff2011-09-28 20:45:37 -0700493 private void setFocusOnFirstChild() {
494 View firstChild = mContent.getChildAt(0, 0);
495 if (firstChild != null) {
496 firstChild.requestFocus();
497 }
498 }
499
Adam Cohendf2cc412011-04-27 16:56:57 -0700500 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700501 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800502 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
503 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
504 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
Michael Jurka032e6ba2013-04-22 15:08:42 +0200505 final ObjectAnimator oa =
Michael Jurka2ecf9952012-06-18 12:52:28 -0700506 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohendf2cc412011-04-27 16:56:57 -0700507
Adam Cohen2801caf2011-05-13 20:57:39 -0700508 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700509 @Override
510 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700511 onCloseComplete();
Michael Jurka0121c3e2012-05-31 08:36:04 -0700512 setLayerType(LAYER_TYPE_NONE, null);
Adam Cohen2801caf2011-05-13 20:57:39 -0700513 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700514 }
515 @Override
516 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700517 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700518 getContext().getString(R.string.folder_closed));
Adam Cohendf2cc412011-04-27 16:56:57 -0700519 mState = STATE_ANIMATING;
520 }
521 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700522 oa.setDuration(mExpandDuration);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700523 setLayerType(LAYER_TYPE_HARDWARE, null);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100524 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700525 }
526
Adam Cohencb3382b2011-05-24 14:07:08 -0700527 public boolean acceptDrop(DragObject d) {
528 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700529 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700530 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
531 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
532 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700533 }
534
Adam Cohendf2cc412011-04-27 16:56:57 -0700535 protected boolean findAndSetEmptyCells(ShortcutInfo item) {
536 int[] emptyCell = new int[2];
537 if (mContent.findCellForSpan(emptyCell, item.spanX, item.spanY)) {
538 item.cellX = emptyCell[0];
539 item.cellY = emptyCell[1];
Adam Cohendf2cc412011-04-27 16:56:57 -0700540 return true;
541 } else {
542 return false;
543 }
544 }
545
Adam Cohenc508b2d2011-06-28 14:41:44 -0700546 protected boolean createAndAddShortcut(ShortcutInfo item) {
Adam Cohen477828c2013-09-20 12:05:49 -0700547 final BubbleTextView textView =
548 (BubbleTextView) mInflater.inflate(R.layout.application, this, false);
Winson Chung54000492013-10-14 16:29:29 -0700549 textView.setCompoundDrawables(null,
Winson Chung0dbd7342013-10-13 22:46:20 -0700550 Utilities.createIconDrawable(item.getIcon(mIconCache)), null, null);
Adam Cohendf2cc412011-04-27 16:56:57 -0700551 textView.setText(item.title);
552 textView.setTag(item);
Adam Cohen477828c2013-09-20 12:05:49 -0700553 textView.setTextColor(getResources().getColor(R.color.folder_items_text_color));
554 textView.setShadowsEnabled(false);
Adam Cohen7397e622013-10-24 12:11:34 -0700555 textView.setGlowColor(getResources().getColor(R.color.folder_items_glow_color));
Adam Cohendf2cc412011-04-27 16:56:57 -0700556
557 textView.setOnClickListener(this);
558 textView.setOnLongClickListener(this);
559
Adam Cohenc508b2d2011-06-28 14:41:44 -0700560 // We need to check here to verify that the given item's location isn't already occupied
Adam Cohen3bf84d32012-05-07 20:17:14 -0700561 // by another item.
Adam Cohen0057bbc2011-08-12 18:30:51 -0700562 if (mContent.getChildAt(item.cellX, item.cellY) != null || item.cellX < 0 || item.cellY < 0
563 || item.cellX >= mContent.getCountX() || item.cellY >= mContent.getCountY()) {
Adam Cohen3bf84d32012-05-07 20:17:14 -0700564 // This shouldn't happen, log it.
565 Log.e(TAG, "Folder order not properly persisted during bind");
Adam Cohenc508b2d2011-06-28 14:41:44 -0700566 if (!findAndSetEmptyCells(item)) {
567 return false;
568 }
569 }
570
Adam Cohendf2cc412011-04-27 16:56:57 -0700571 CellLayout.LayoutParams lp =
572 new CellLayout.LayoutParams(item.cellX, item.cellY, item.spanX, item.spanY);
573 boolean insert = false;
Adam Cohenac56cff2011-09-28 20:45:37 -0700574 textView.setOnKeyListener(new FolderKeyEventListener());
Adam Cohendf2cc412011-04-27 16:56:57 -0700575 mContent.addViewToCellLayout(textView, insert ? 0 : -1, (int)item.id, lp, true);
Adam Cohenc508b2d2011-06-28 14:41:44 -0700576 return true;
Adam Cohendf2cc412011-04-27 16:56:57 -0700577 }
578
Adam Cohencb3382b2011-05-24 14:07:08 -0700579 public void onDragEnter(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700580 mPreviousTargetCell[0] = -1;
581 mPreviousTargetCell[1] = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700582 mOnExitAlarm.cancelAlarm();
583 }
584
585 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
586 public void onAlarm(Alarm alarm) {
587 realTimeReorder(mEmptyCell, mTargetCell);
588 }
589 };
590
591 boolean readingOrderGreaterThan(int[] v1, int[] v2) {
592 if (v1[1] > v2[1] || (v1[1] == v2[1] && v1[0] > v2[0])) {
593 return true;
594 } else {
595 return false;
596 }
597 }
598
599 private void realTimeReorder(int[] empty, int[] target) {
600 boolean wrap;
601 int startX;
602 int endX;
603 int startY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700604 int delay = 0;
605 float delayAmount = 30;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700606 if (readingOrderGreaterThan(target, empty)) {
607 wrap = empty[0] >= mContent.getCountX() - 1;
608 startY = wrap ? empty[1] + 1 : empty[1];
609 for (int y = startY; y <= target[1]; y++) {
610 startX = y == empty[1] ? empty[0] + 1 : 0;
611 endX = y < target[1] ? mContent.getCountX() - 1 : target[0];
612 for (int x = startX; x <= endX; x++) {
613 View v = mContent.getChildAt(x,y);
614 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen482ed822012-03-02 14:15:13 -0800615 REORDER_ANIMATION_DURATION, delay, true, true)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700616 empty[0] = x;
617 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700618 delay += delayAmount;
619 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700620 }
621 }
622 }
623 } else {
624 wrap = empty[0] == 0;
625 startY = wrap ? empty[1] - 1 : empty[1];
626 for (int y = startY; y >= target[1]; y--) {
627 startX = y == empty[1] ? empty[0] - 1 : mContent.getCountX() - 1;
628 endX = y > target[1] ? 0 : target[0];
629 for (int x = startX; x >= endX; x--) {
630 View v = mContent.getChildAt(x,y);
631 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen482ed822012-03-02 14:15:13 -0800632 REORDER_ANIMATION_DURATION, delay, true, true)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700633 empty[0] = x;
634 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700635 delay += delayAmount;
636 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700637 }
638 }
639 }
640 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700641 }
642
Adam Cohen2374abf2013-04-16 14:56:57 -0700643 public boolean isLayoutRtl() {
644 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
645 }
646
Adam Cohencb3382b2011-05-24 14:07:08 -0700647 public void onDragOver(DragObject d) {
Alan Viverette4cda5b72013-08-28 17:53:41 -0700648 final DragView dragView = d.dragView;
649 final int scrollOffset = mScrollView.getScrollY();
650 final float[] r = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, dragView, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700651 r[0] -= getPaddingLeft();
652 r[1] -= getPaddingTop();
653
Alan Viverette4cda5b72013-08-28 17:53:41 -0700654 final long downTime = SystemClock.uptimeMillis();
655 final MotionEvent translatedEv = MotionEvent.obtain(
656 downTime, downTime, MotionEvent.ACTION_MOVE, d.x, d.y, 0);
657
658 if (!mAutoScrollHelper.isEnabled()) {
659 mAutoScrollHelper.setEnabled(true);
660 }
661
662 final boolean handled = mAutoScrollHelper.onTouch(this, translatedEv);
663 translatedEv.recycle();
664
665 if (handled) {
666 mReorderAlarm.cancelAlarm();
667 } else {
668 mTargetCell = mContent.findNearestArea(
669 (int) r[0], (int) r[1] + scrollOffset, 1, 1, mTargetCell);
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700670 if (isLayoutRtl()) {
671 mTargetCell[0] = mContent.getCountX() - mTargetCell[0] - 1;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700672 }
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700673 if (mTargetCell[0] != mPreviousTargetCell[0]
674 || mTargetCell[1] != mPreviousTargetCell[1]) {
675 mReorderAlarm.cancelAlarm();
676 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
677 mReorderAlarm.setAlarm(REORDER_DELAY);
678 mPreviousTargetCell[0] = mTargetCell[0];
679 mPreviousTargetCell[1] = mTargetCell[1];
680 mDragMode = DRAG_MODE_REORDER;
681 } else {
682 mDragMode = DRAG_MODE_NONE;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700683 }
Adam Cohenbfbfd262011-06-13 16:55:12 -0700684 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700685 }
686
Adam Cohenbfbfd262011-06-13 16:55:12 -0700687 // This is used to compute the visual center of the dragView. The idea is that
688 // the visual center represents the user's interpretation of where the item is, and hence
689 // is the appropriate point to use when determining drop location.
690 private float[] getDragViewVisualCenter(int x, int y, int xOffset, int yOffset,
691 DragView dragView, float[] recycle) {
692 float res[];
693 if (recycle == null) {
694 res = new float[2];
695 } else {
696 res = recycle;
697 }
698
699 // These represent the visual top and left of drag view if a dragRect was provided.
700 // If a dragRect was not provided, then they correspond to the actual view left and
701 // top, as the dragRect is in that case taken to be the entire dragView.
702 // R.dimen.dragViewOffsetY.
703 int left = x - xOffset;
704 int top = y - yOffset;
705
706 // In order to find the visual center, we shift by half the dragRect
707 res[0] = left + dragView.getDragRegion().width() / 2;
708 res[1] = top + dragView.getDragRegion().height() / 2;
709
710 return res;
711 }
712
713 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
714 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700715 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700716 }
717 };
718
Adam Cohen95bb8002011-07-03 23:40:28 -0700719 public void completeDragExit() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700720 mLauncher.closeFolder();
721 mCurrentDragInfo = null;
722 mCurrentDragView = null;
723 mSuppressOnAdd = false;
724 mRearrangeOnClose = true;
725 }
726
Adam Cohencb3382b2011-05-24 14:07:08 -0700727 public void onDragExit(DragObject d) {
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700728 // Exiting folder; stop the auto scroller.
Alan Viverette4cda5b72013-08-28 17:53:41 -0700729 mAutoScrollHelper.setEnabled(false);
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700730 // We only close the folder if this is a true drag exit, ie. not because
731 // a drop has occurred above the folder.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700732 if (!d.dragComplete) {
733 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
734 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
735 }
736 mReorderAlarm.cancelAlarm();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700737 mDragMode = DRAG_MODE_NONE;
Adam Cohen2801caf2011-05-13 20:57:39 -0700738 }
739
Michael Jurka1e2f4652013-07-08 18:03:46 -0700740 public void onDropCompleted(final View target, final DragObject d,
741 final boolean isFlingToDelete, final boolean success) {
742 if (mDeferDropAfterUninstall) {
Michael Jurkaf3007582013-08-21 14:33:57 +0200743 Log.d(TAG, "Deferred handling drop because waiting for uninstall.");
Michael Jurka1e2f4652013-07-08 18:03:46 -0700744 mDeferredAction = new Runnable() {
745 public void run() {
746 onDropCompleted(target, d, isFlingToDelete, success);
747 mDeferredAction = null;
748 }
749 };
750 return;
751 }
752
753 boolean beingCalledAfterUninstall = mDeferredAction != null;
754 boolean successfulDrop =
755 success && (!beingCalledAfterUninstall || mUninstallSuccessful);
Winson Chung5f8afe62013-08-12 16:19:28 -0700756
Michael Jurka1e2f4652013-07-08 18:03:46 -0700757 if (successfulDrop) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700758 if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700759 replaceFolderWithFinalItem();
760 }
761 } else {
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700762 setupContentForNumItems(getItemCount());
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700763 // The drag failed, we need to return the item to the folder
764 mFolderIcon.onDrop(d);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700765 }
766
767 if (target != this) {
768 if (mOnExitAlarm.alarmPending()) {
769 mOnExitAlarm.cancelAlarm();
Michael Jurka54554252013-08-01 12:52:23 +0200770 if (!successfulDrop) {
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700771 mSuppressFolderDeletion = true;
772 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700773 completeDragExit();
774 }
Adam Cohenf0f2ffa2013-11-12 11:44:58 +0000775 } else {
776 mLauncher.getWorkspace().removeExtraEmptyScreen(true, null);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700777 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700778
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700779 mDeleteFolderOnDropCompleted = false;
780 mDragInProgress = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700781 mItemAddedBackToSelfViaIcon = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700782 mCurrentDragInfo = null;
783 mCurrentDragView = null;
784 mSuppressOnAdd = false;
Adam Cohen4045eb72011-10-06 11:44:26 -0700785
786 // Reordering may have occured, and we need to save the new item locations. We do this once
787 // at the end to prevent unnecessary database operations.
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700788 updateItemLocationsInDatabaseBatch();
Adam Cohen4045eb72011-10-06 11:44:26 -0700789 }
790
Michael Jurka1e2f4652013-07-08 18:03:46 -0700791 public void deferCompleteDropAfterUninstallActivity() {
792 mDeferDropAfterUninstall = true;
793 }
794
795 public void onUninstallActivityReturned(boolean success) {
796 mDeferDropAfterUninstall = false;
797 mUninstallSuccessful = success;
798 if (mDeferredAction != null) {
799 mDeferredAction.run();
800 }
801 }
802
Winson Chunga48487a2012-03-20 16:19:37 -0700803 @Override
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800804 public float getIntrinsicIconScaleFactor() {
805 return 1f;
806 }
807
808 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800809 public boolean supportsFlingToDelete() {
810 return true;
811 }
812
Winson Chunga48487a2012-03-20 16:19:37 -0700813 public void onFlingToDelete(DragObject d, int x, int y, PointF vec) {
814 // Do nothing
815 }
816
817 @Override
818 public void onFlingToDeleteCompleted() {
819 // Do nothing
820 }
821
Adam Cohen4045eb72011-10-06 11:44:26 -0700822 private void updateItemLocationsInDatabase() {
823 ArrayList<View> list = getItemsInReadingOrder();
824 for (int i = 0; i < list.size(); i++) {
825 View v = list.get(i);
826 ItemInfo info = (ItemInfo) v.getTag();
827 LauncherModel.moveItemInDatabase(mLauncher, info, mInfo.id, 0,
Winson Chung68b66782013-12-09 16:57:45 -0800828 info.cellX, info.cellY);
Adam Cohen4045eb72011-10-06 11:44:26 -0700829 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700830 }
Adam Cohen228da5a2011-07-27 22:23:47 -0700831
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700832 private void updateItemLocationsInDatabaseBatch() {
833 ArrayList<View> list = getItemsInReadingOrder();
834 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
835 for (int i = 0; i < list.size(); i++) {
836 View v = list.get(i);
837 ItemInfo info = (ItemInfo) v.getTag();
838 items.add(info);
839 }
840
841 LauncherModel.moveItemsInDatabase(mLauncher, items, mInfo.id, 0);
842 }
843
Adam Cohene25af792013-06-06 23:08:25 -0700844 public void addItemLocationsInDatabase() {
845 ArrayList<View> list = getItemsInReadingOrder();
846 for (int i = 0; i < list.size(); i++) {
847 View v = list.get(i);
848 ItemInfo info = (ItemInfo) v.getTag();
849 LauncherModel.addItemToDatabase(mLauncher, info, mInfo.id, 0,
850 info.cellX, info.cellY, false);
851 }
852 }
853
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700854 public void notifyDrop() {
855 if (mDragInProgress) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700856 mItemAddedBackToSelfViaIcon = true;
Adam Cohen76078c42011-06-09 15:06:52 -0700857 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700858 }
859
860 public boolean isDropEnabled() {
861 return true;
862 }
863
Adam Cohen4045eb72011-10-06 11:44:26 -0700864 private void setupContentDimensions(int count) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700865 ArrayList<View> list = getItemsInReadingOrder();
866
867 int countX = mContent.getCountX();
868 int countY = mContent.getCountY();
Adam Cohen7c693212011-05-18 15:26:57 -0700869 boolean done = false;
Adam Cohen2801caf2011-05-13 20:57:39 -0700870
Adam Cohen7c693212011-05-18 15:26:57 -0700871 while (!done) {
872 int oldCountX = countX;
873 int oldCountY = countY;
874 if (countX * countY < count) {
875 // Current grid is too small, expand it
Adam Cohen78dc83e2011-11-15 17:10:00 -0800876 if ((countX <= countY || countY == mMaxCountY) && countX < mMaxCountX) {
Adam Cohen7c693212011-05-18 15:26:57 -0700877 countX++;
878 } else if (countY < mMaxCountY) {
879 countY++;
880 }
881 if (countY == 0) countY++;
882 } else if ((countY - 1) * countX >= count && countY >= countX) {
883 countY = Math.max(0, countY - 1);
884 } else if ((countX - 1) * countY >= count) {
885 countX = Math.max(0, countX - 1);
Adam Cohen2801caf2011-05-13 20:57:39 -0700886 }
Adam Cohen7c693212011-05-18 15:26:57 -0700887 done = countX == oldCountX && countY == oldCountY;
Adam Cohen2801caf2011-05-13 20:57:39 -0700888 }
Adam Cohen7c693212011-05-18 15:26:57 -0700889 mContent.setGridSize(countX, countY);
Adam Cohen2801caf2011-05-13 20:57:39 -0700890 arrangeChildren(list);
891 }
892
893 public boolean isFull() {
Adam Cohen78dc83e2011-11-15 17:10:00 -0800894 return getItemCount() >= mMaxNumItems;
Adam Cohen2801caf2011-05-13 20:57:39 -0700895 }
896
897 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700898 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700899
Winson Chung892c74d2013-08-22 16:15:50 -0700900 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700901 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700902 int height = getFolderHeight();
Adam Cohen2801caf2011-05-13 20:57:39 -0700903
Adam Cohen307fe232012-08-16 17:55:58 -0700904 float scale = parent.getDescendantRectRelativeToSelf(mFolderIcon, mTempRect);
Adam Cohen8e776a62011-06-28 18:10:06 -0700905
Winson Chungaf40f202013-09-18 18:26:31 -0700906 LauncherAppState app = LauncherAppState.getInstance();
907 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
908
Adam Cohen307fe232012-08-16 17:55:58 -0700909 int centerX = (int) (mTempRect.left + mTempRect.width() * scale / 2);
910 int centerY = (int) (mTempRect.top + mTempRect.height() * scale / 2);
Adam Cohen2801caf2011-05-13 20:57:39 -0700911 int centeredLeft = centerX - width / 2;
912 int centeredTop = centerY - height / 2;
Winson Chung3057b1c2013-10-10 17:35:35 -0700913 int currentPage = mLauncher.getWorkspace().getNextPage();
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800914 // In case the workspace is scrolling, we need to use the final scroll to compute
915 // the folders bounds.
916 mLauncher.getWorkspace().setFinalScrollForPageChange(currentPage);
Adam Cohen35e7e642011-07-17 14:47:18 -0700917 // We first fetch the currently visible CellLayoutChildren
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800918 CellLayout currentLayout = (CellLayout) mLauncher.getWorkspace().getChildAt(currentPage);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700919 ShortcutAndWidgetContainer boundingLayout = currentLayout.getShortcutsAndWidgets();
Adam Cohen35e7e642011-07-17 14:47:18 -0700920 Rect bounds = new Rect();
921 parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800922 // We reset the workspaces scroll
923 mLauncher.getWorkspace().resetFinalScrollForPageChange(currentPage);
Adam Cohen2801caf2011-05-13 20:57:39 -0700924
Adam Cohen35e7e642011-07-17 14:47:18 -0700925 // We need to bound the folder to the currently visible CellLayoutChildren
926 int left = Math.min(Math.max(bounds.left, centeredLeft),
927 bounds.left + bounds.width() - width);
928 int top = Math.min(Math.max(bounds.top, centeredTop),
929 bounds.top + bounds.height() - height);
Winson Chungaf40f202013-09-18 18:26:31 -0700930 if (grid.isPhone() && (grid.availableWidthPx - width) < grid.iconSizePx) {
931 // Center the folder if it is full (on phones only)
932 left = (grid.availableWidthPx - width) / 2;
933 } else if (width >= bounds.width()) {
934 // If the folder doesn't fit within the bounds, center it about the desired bounds
Adam Cohen35e7e642011-07-17 14:47:18 -0700935 left = bounds.left + (bounds.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700936 }
Adam Cohen35e7e642011-07-17 14:47:18 -0700937 if (height >= bounds.height()) {
938 top = bounds.top + (bounds.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700939 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700940
941 int folderPivotX = width / 2 + (centeredLeft - left);
942 int folderPivotY = height / 2 + (centeredTop - top);
943 setPivotX(folderPivotX);
944 setPivotY(folderPivotY);
Adam Cohen268c4752012-06-06 17:47:33 -0700945 mFolderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700946 (1.0f * folderPivotX / width));
Adam Cohen268c4752012-06-06 17:47:33 -0700947 mFolderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700948 (1.0f * folderPivotY / height));
Adam Cohen3bf84d32012-05-07 20:17:14 -0700949
Adam Cohen662b5982011-12-13 17:45:21 -0800950 lp.width = width;
951 lp.height = height;
952 lp.x = left;
953 lp.y = top;
Adam Cohen2801caf2011-05-13 20:57:39 -0700954 }
955
Adam Cohen268c4752012-06-06 17:47:33 -0700956 float getPivotXForIconAnimation() {
957 return mFolderIconPivotX;
958 }
959 float getPivotYForIconAnimation() {
960 return mFolderIconPivotY;
961 }
962
Adam Cohen2801caf2011-05-13 20:57:39 -0700963 private void setupContentForNumItems(int count) {
Adam Cohen4045eb72011-10-06 11:44:26 -0700964 setupContentDimensions(count);
Adam Cohen2801caf2011-05-13 20:57:39 -0700965
Adam Cohen8e776a62011-06-28 18:10:06 -0700966 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700967 if (lp == null) {
Adam Cohen8e776a62011-06-28 18:10:06 -0700968 lp = new DragLayer.LayoutParams(0, 0);
969 lp.customPosition = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700970 setLayoutParams(lp);
971 }
972 centerAboutIcon();
973 }
974
Winson Chung892c74d2013-08-22 16:15:50 -0700975 private int getContentAreaHeight() {
976 LauncherAppState app = LauncherAppState.getInstance();
977 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
978 Rect workspacePadding = grid.getWorkspacePadding(grid.isLandscape ?
979 CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
980 int maxContentAreaHeight = grid.availableHeightPx -
Winson Chung892c74d2013-08-22 16:15:50 -0700981 workspacePadding.top - workspacePadding.bottom -
Winson Chung892c74d2013-08-22 16:15:50 -0700982 mFolderNameHeight;
Adam Cohen1960ea42013-11-12 11:33:14 +0000983 int height = Math.min(maxContentAreaHeight,
Winson Chung892c74d2013-08-22 16:15:50 -0700984 mContent.getDesiredHeight());
Adam Cohen1960ea42013-11-12 11:33:14 +0000985 return Math.max(height, MIN_CONTENT_DIMEN);
986 }
987
988 private int getContentAreaWidth() {
989 return Math.max(mContent.getDesiredWidth(), MIN_CONTENT_DIMEN);
Winson Chung892c74d2013-08-22 16:15:50 -0700990 }
991
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700992 private int getFolderHeight() {
Winson Chung892c74d2013-08-22 16:15:50 -0700993 int height = getPaddingTop() + getPaddingBottom()
994 + getContentAreaHeight() + mFolderNameHeight;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700995 return height;
996 }
Adam Cohen234c4cd2011-07-17 21:03:04 -0700997
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700998 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700999 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
1000 int height = getFolderHeight();
Adam Cohen1960ea42013-11-12 11:33:14 +00001001 int contentAreaWidthSpec = MeasureSpec.makeMeasureSpec(getContentAreaWidth(),
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001002 MeasureSpec.EXACTLY);
Winson Chung892c74d2013-08-22 16:15:50 -07001003 int contentAreaHeightSpec = MeasureSpec.makeMeasureSpec(getContentAreaHeight(),
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001004 MeasureSpec.EXACTLY);
Adam Cohen1960ea42013-11-12 11:33:14 +00001005
1006 mContent.setFixedSize(getContentAreaWidth(), getContentAreaHeight());
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001007 mScrollView.measure(contentAreaWidthSpec, contentAreaHeightSpec);
1008 mFolderName.measure(contentAreaWidthSpec,
Adam Cohen234c4cd2011-07-17 21:03:04 -07001009 MeasureSpec.makeMeasureSpec(mFolderNameHeight, MeasureSpec.EXACTLY));
1010 setMeasuredDimension(width, height);
1011 }
1012
Adam Cohen2801caf2011-05-13 20:57:39 -07001013 private void arrangeChildren(ArrayList<View> list) {
1014 int[] vacant = new int[2];
1015 if (list == null) {
1016 list = getItemsInReadingOrder();
1017 }
1018 mContent.removeAllViews();
1019
1020 for (int i = 0; i < list.size(); i++) {
1021 View v = list.get(i);
1022 mContent.getVacantCell(vacant, 1, 1);
1023 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) v.getLayoutParams();
1024 lp.cellX = vacant[0];
1025 lp.cellY = vacant[1];
1026 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohen2792a332011-09-26 21:09:47 -07001027 if (info.cellX != vacant[0] || info.cellY != vacant[1]) {
1028 info.cellX = vacant[0];
1029 info.cellY = vacant[1];
1030 LauncherModel.addOrMoveItemInDatabase(mLauncher, info, mInfo.id, 0,
1031 info.cellX, info.cellY);
1032 }
Adam Cohen2801caf2011-05-13 20:57:39 -07001033 boolean insert = false;
1034 mContent.addViewToCellLayout(v, insert ? 0 : -1, (int)info.id, lp, true);
1035 }
Adam Cohen7c693212011-05-18 15:26:57 -07001036 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -07001037 }
1038
Adam Cohena9cf38f2011-05-02 15:36:58 -07001039 public int getItemCount() {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001040 return mContent.getShortcutsAndWidgets().getChildCount();
Adam Cohena9cf38f2011-05-02 15:36:58 -07001041 }
1042
1043 public View getItemAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001044 return mContent.getShortcutsAndWidgets().getChildAt(index);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001045 }
1046
Adam Cohen2801caf2011-05-13 20:57:39 -07001047 private void onCloseComplete() {
Adam Cohen05e0f402011-08-01 12:12:49 -07001048 DragLayer parent = (DragLayer) getParent();
Michael Jurka5649c282012-06-18 10:33:21 -07001049 if (parent != null) {
1050 parent.removeView(this);
1051 }
Adam Cohen4554ee12011-08-03 16:13:21 -07001052 mDragController.removeDropTarget((DropTarget) this);
Adam Cohen05e0f402011-08-01 12:12:49 -07001053 clearFocus();
Adam Cohenac56cff2011-09-28 20:45:37 -07001054 mFolderIcon.requestFocus();
Adam Cohen05e0f402011-08-01 12:12:49 -07001055
Adam Cohen2801caf2011-05-13 20:57:39 -07001056 if (mRearrangeOnClose) {
1057 setupContentForNumItems(getItemCount());
1058 mRearrangeOnClose = false;
1059 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001060 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001061 if (!mDragInProgress && !mSuppressFolderDeletion) {
1062 replaceFolderWithFinalItem();
1063 } else if (mDragInProgress) {
1064 mDeleteFolderOnDropCompleted = true;
1065 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001066 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001067 mSuppressFolderDeletion = false;
Adam Cohenafb01ee2011-06-23 15:38:03 -07001068 }
1069
1070 private void replaceFolderWithFinalItem() {
Adam Cohenafb01ee2011-06-23 15:38:03 -07001071 // Add the last remaining child to the workspace in place of the folder
Adam Cohenfb91f302012-06-11 15:45:18 -07001072 Runnable onCompleteRunnable = new Runnable() {
1073 @Override
1074 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -07001075 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screenId);
Adam Cohenafb01ee2011-06-23 15:38:03 -07001076
Winson Chung68b66782013-12-09 16:57:45 -08001077 View child = null;
Adam Cohenfb91f302012-06-11 15:45:18 -07001078 // Move the item from the folder to the workspace, in the position of the folder
1079 if (getItemCount() == 1) {
1080 ShortcutInfo finalItem = mInfo.contents.get(0);
Adam Cohenc5e63f32012-07-12 16:16:57 -07001081 child = mLauncher.createShortcut(R.layout.application, cellLayout,
Adam Cohenfb91f302012-06-11 15:45:18 -07001082 finalItem);
1083 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -07001084 mInfo.screenId, mInfo.cellX, mInfo.cellY);
Adam Cohenfb91f302012-06-11 15:45:18 -07001085 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001086 if (getItemCount() <= 1) {
1087 // Remove the folder
1088 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
1089 cellLayout.removeView(mFolderIcon);
1090 if (mFolderIcon instanceof DropTarget) {
1091 mDragController.removeDropTarget((DropTarget) mFolderIcon);
1092 }
1093 mLauncher.removeFolder(mInfo);
1094 }
Adam Cohenc5e63f32012-07-12 16:16:57 -07001095 // We add the child after removing the folder to prevent both from existing at
Winson Chung0e6a7132013-08-23 12:55:10 -07001096 // the same time in the CellLayout. We need to add the new item with addInScreenFromBind()
1097 // to ensure that hotseat items are placed correctly.
Adam Cohenc5e63f32012-07-12 16:16:57 -07001098 if (child != null) {
Winson Chung0e6a7132013-08-23 12:55:10 -07001099 mLauncher.getWorkspace().addInScreenFromBind(child, mInfo.container, mInfo.screenId,
Adam Cohenc5e63f32012-07-12 16:16:57 -07001100 mInfo.cellX, mInfo.cellY, mInfo.spanX, mInfo.spanY);
1101 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001102 }
1103 };
1104 View finalChild = getItemAt(0);
1105 if (finalChild != null) {
1106 mFolderIcon.performDestroyAnimation(finalChild, onCompleteRunnable);
Winson Chung68b66782013-12-09 16:57:45 -08001107 } else {
1108 onCompleteRunnable.run();
Adam Cohenafb01ee2011-06-23 15:38:03 -07001109 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001110 mDestroyed = true;
1111 }
1112
1113 boolean isDestroyed() {
1114 return mDestroyed;
Adam Cohen2801caf2011-05-13 20:57:39 -07001115 }
1116
Adam Cohenac56cff2011-09-28 20:45:37 -07001117 // This method keeps track of the last item in the folder for the purposes
1118 // of keyboard focus
1119 private void updateTextViewFocus() {
1120 View lastChild = getItemAt(getItemCount() - 1);
1121 getItemAt(getItemCount() - 1);
1122 if (lastChild != null) {
1123 mFolderName.setNextFocusDownId(lastChild.getId());
1124 mFolderName.setNextFocusRightId(lastChild.getId());
1125 mFolderName.setNextFocusLeftId(lastChild.getId());
1126 mFolderName.setNextFocusUpId(lastChild.getId());
1127 }
1128 }
1129
Adam Cohenbfbfd262011-06-13 16:55:12 -07001130 public void onDrop(DragObject d) {
1131 ShortcutInfo item;
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001132 if (d.dragInfo instanceof AppInfo) {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001133 // Came from all apps -- make a copy
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001134 item = ((AppInfo) d.dragInfo).makeShortcut();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001135 item.spanX = 1;
1136 item.spanY = 1;
1137 } else {
1138 item = (ShortcutInfo) d.dragInfo;
1139 }
Adam Cohen05e0f402011-08-01 12:12:49 -07001140 // Dragged from self onto self, currently this is the only path possible, however
1141 // we keep this as a distinct code path.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001142 if (item == mCurrentDragInfo) {
1143 ShortcutInfo si = (ShortcutInfo) mCurrentDragView.getTag();
1144 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) mCurrentDragView.getLayoutParams();
1145 si.cellX = lp.cellX = mEmptyCell[0];
1146 si.cellX = lp.cellY = mEmptyCell[1];
1147 mContent.addViewToCellLayout(mCurrentDragView, -1, (int)item.id, lp, true);
Adam Cohenfc53cd22011-07-20 15:45:11 -07001148 if (d.dragView.hasDrawn()) {
1149 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, mCurrentDragView);
1150 } else {
Winson Chung2c4cf412012-05-08 14:03:21 -07001151 d.deferDragViewCleanupPostAnimation = false;
Adam Cohenfc53cd22011-07-20 15:45:11 -07001152 mCurrentDragView.setVisibility(VISIBLE);
1153 }
Adam Cohene9166b22011-07-08 17:11:11 -07001154 mItemsInvalidated = true;
Adam Cohen4045eb72011-10-06 11:44:26 -07001155 setupContentDimensions(getItemCount());
Adam Cohen716b51e2011-06-30 12:09:54 -07001156 mSuppressOnAdd = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001157 }
1158 mInfo.add(item);
1159 }
1160
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001161 // This is used so the item doesn't immediately appear in the folder when added. In one case
1162 // we need to create the illusion that the item isn't added back to the folder yet, to
1163 // to correspond to the animation of the icon back into the folder. This is
1164 public void hideItem(ShortcutInfo info) {
1165 View v = getViewForInfo(info);
1166 v.setVisibility(INVISIBLE);
1167 }
1168 public void showItem(ShortcutInfo info) {
1169 View v = getViewForInfo(info);
1170 v.setVisibility(VISIBLE);
1171 }
1172
Adam Cohenbfbfd262011-06-13 16:55:12 -07001173 public void onAdd(ShortcutInfo item) {
1174 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001175 // If the item was dropped onto this open folder, we have done the work associated
1176 // with adding the item to the folder, as indicated by mSuppressOnAdd being set
Adam Cohenbfbfd262011-06-13 16:55:12 -07001177 if (mSuppressOnAdd) return;
1178 if (!findAndSetEmptyCells(item)) {
1179 // The current layout is full, can we expand it?
1180 setupContentForNumItems(getItemCount() + 1);
1181 findAndSetEmptyCells(item);
1182 }
1183 createAndAddShortcut(item);
1184 LauncherModel.addOrMoveItemInDatabase(
1185 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
1186 }
1187
Adam Cohena9cf38f2011-05-02 15:36:58 -07001188 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -07001189 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001190 // If this item is being dragged from this open folder, we have already handled
1191 // the work associated with removing the item, so we don't have to do anything here.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001192 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001193 View v = getViewForInfo(item);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001194 mContent.removeView(v);
Adam Cohen2801caf2011-05-13 20:57:39 -07001195 if (mState == STATE_ANIMATING) {
1196 mRearrangeOnClose = true;
1197 } else {
1198 setupContentForNumItems(getItemCount());
1199 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001200 if (getItemCount() <= 1) {
1201 replaceFolderWithFinalItem();
1202 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001203 }
Adam Cohen7c693212011-05-18 15:26:57 -07001204
Adam Cohendf1e4e82011-06-24 15:57:39 -07001205 private View getViewForInfo(ShortcutInfo item) {
1206 for (int j = 0; j < mContent.getCountY(); j++) {
1207 for (int i = 0; i < mContent.getCountX(); i++) {
1208 View v = mContent.getChildAt(i, j);
1209 if (v.getTag() == item) {
1210 return v;
1211 }
1212 }
1213 }
1214 return null;
1215 }
1216
Adam Cohen76078c42011-06-09 15:06:52 -07001217 public void onItemsChanged() {
Adam Cohenac56cff2011-09-28 20:45:37 -07001218 updateTextViewFocus();
Adam Cohen76078c42011-06-09 15:06:52 -07001219 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001220
Adam Cohen76fc0852011-06-17 13:26:23 -07001221 public void onTitleChanged(CharSequence title) {
1222 }
Adam Cohen76078c42011-06-09 15:06:52 -07001223
Adam Cohen7c693212011-05-18 15:26:57 -07001224 public ArrayList<View> getItemsInReadingOrder() {
1225 if (mItemsInvalidated) {
1226 mItemsInReadingOrder.clear();
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 != null) {
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001231 mItemsInReadingOrder.add(v);
Adam Cohen7c693212011-05-18 15:26:57 -07001232 }
1233 }
1234 }
1235 mItemsInvalidated = false;
1236 }
1237 return mItemsInReadingOrder;
1238 }
Adam Cohen8dfcba42011-07-07 16:38:18 -07001239
1240 public void getLocationInDragLayer(int[] loc) {
1241 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
1242 }
Adam Cohenea0818d2011-09-30 20:06:58 -07001243
1244 public void onFocusChange(View v, boolean hasFocus) {
1245 if (v == mFolderName && hasFocus) {
1246 startEditingFolderName();
1247 }
1248 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001249
1250 @Override
1251 public void getHitRectRelativeToDragLayer(Rect outRect) {
1252 getHitRect(outRect);
1253 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001254}