blob: 50e2000926f88cdb4ca30565c57fb76e03482d7b [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();
Adam Cohendf2cc412011-04-27 16:56:57 -0700411 }
412
413 /**
414 * Creates a new UserFolder, inflated from R.layout.user_folder.
415 *
416 * @param context The application's context.
417 *
418 * @return A new UserFolder.
419 */
420 static Folder fromXml(Context context) {
421 return (Folder) LayoutInflater.from(context).inflate(R.layout.user_folder, null);
422 }
423
424 /**
425 * This method is intended to make the UserFolder to be visually identical in size and position
426 * to its associated FolderIcon. This allows for a seamless transition into the expanded state.
427 */
428 private void positionAndSizeAsIcon() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700429 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800430 setScaleX(0.8f);
431 setScaleY(0.8f);
432 setAlpha(0f);
Adam Cohendf2cc412011-04-27 16:56:57 -0700433 mState = STATE_SMALL;
434 }
435
436 public void animateOpen() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700437 positionAndSizeAsIcon();
438
Adam Cohen8e776a62011-06-28 18:10:06 -0700439 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen2801caf2011-05-13 20:57:39 -0700440 centerAboutIcon();
Adam Cohen662b5982011-12-13 17:45:21 -0800441 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1);
442 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
443 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
Michael Jurka032e6ba2013-04-22 15:08:42 +0200444 final ObjectAnimator oa =
Michael Jurka2ecf9952012-06-18 12:52:28 -0700445 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohen6441de02011-12-14 14:25:32 -0800446
Adam Cohen2801caf2011-05-13 20:57:39 -0700447 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700448 @Override
449 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700450 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700451 String.format(getContext().getString(R.string.folder_opened),
Adam Cohen3371da02011-10-25 21:38:29 -0700452 mContent.getCountX(), mContent.getCountY()));
Adam Cohendf2cc412011-04-27 16:56:57 -0700453 mState = STATE_ANIMATING;
454 }
455 @Override
456 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700457 mState = STATE_OPEN;
Michael Jurka0121c3e2012-05-31 08:36:04 -0700458 setLayerType(LAYER_TYPE_NONE, null);
Winson Chung7a74ac92011-09-20 17:43:51 -0700459 Cling cling = mLauncher.showFirstRunFoldersCling();
460 if (cling != null) {
Winson Chungfa545132013-09-26 17:45:32 -0700461 cling.bringScrimToFront();
Winson Chungaf40f202013-09-18 18:26:31 -0700462 bringToFront();
Winson Chungfa545132013-09-26 17:45:32 -0700463 cling.bringToFront();
Winson Chung7a74ac92011-09-20 17:43:51 -0700464 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700465 setFocusOnFirstChild();
Adam Cohendf2cc412011-04-27 16:56:57 -0700466 }
467 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700468 oa.setDuration(mExpandDuration);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700469 setLayerType(LAYER_TYPE_HARDWARE, null);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100470 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700471 }
472
Adam Cohen3371da02011-10-25 21:38:29 -0700473 private void sendCustomAccessibilityEvent(int type, String text) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700474 AccessibilityManager accessibilityManager = (AccessibilityManager)
475 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
476 if (accessibilityManager.isEnabled()) {
Adam Cohen3371da02011-10-25 21:38:29 -0700477 AccessibilityEvent event = AccessibilityEvent.obtain(type);
478 onInitializeAccessibilityEvent(event);
479 event.getText().add(text);
Michael Jurka8b805b12012-04-18 14:23:14 -0700480 accessibilityManager.sendAccessibilityEvent(event);
Adam Cohen3371da02011-10-25 21:38:29 -0700481 }
482 }
483
Adam Cohenac56cff2011-09-28 20:45:37 -0700484 private void setFocusOnFirstChild() {
485 View firstChild = mContent.getChildAt(0, 0);
486 if (firstChild != null) {
487 firstChild.requestFocus();
488 }
489 }
490
Adam Cohendf2cc412011-04-27 16:56:57 -0700491 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700492 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800493 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
494 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
495 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
Michael Jurka032e6ba2013-04-22 15:08:42 +0200496 final ObjectAnimator oa =
Michael Jurka2ecf9952012-06-18 12:52:28 -0700497 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohendf2cc412011-04-27 16:56:57 -0700498
Adam Cohen2801caf2011-05-13 20:57:39 -0700499 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700500 @Override
501 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700502 onCloseComplete();
Michael Jurka0121c3e2012-05-31 08:36:04 -0700503 setLayerType(LAYER_TYPE_NONE, null);
Adam Cohen2801caf2011-05-13 20:57:39 -0700504 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700505 }
506 @Override
507 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700508 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700509 getContext().getString(R.string.folder_closed));
Adam Cohendf2cc412011-04-27 16:56:57 -0700510 mState = STATE_ANIMATING;
511 }
512 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700513 oa.setDuration(mExpandDuration);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700514 setLayerType(LAYER_TYPE_HARDWARE, null);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100515 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700516 }
517
Adam Cohencb3382b2011-05-24 14:07:08 -0700518 public boolean acceptDrop(DragObject d) {
519 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700520 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700521 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
522 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
523 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700524 }
525
Adam Cohendf2cc412011-04-27 16:56:57 -0700526 protected boolean findAndSetEmptyCells(ShortcutInfo item) {
527 int[] emptyCell = new int[2];
528 if (mContent.findCellForSpan(emptyCell, item.spanX, item.spanY)) {
529 item.cellX = emptyCell[0];
530 item.cellY = emptyCell[1];
Adam Cohendf2cc412011-04-27 16:56:57 -0700531 return true;
532 } else {
533 return false;
534 }
535 }
536
Adam Cohenc508b2d2011-06-28 14:41:44 -0700537 protected boolean createAndAddShortcut(ShortcutInfo item) {
Adam Cohen477828c2013-09-20 12:05:49 -0700538 final BubbleTextView textView =
539 (BubbleTextView) mInflater.inflate(R.layout.application, this, false);
Winson Chung54000492013-10-14 16:29:29 -0700540 textView.setCompoundDrawables(null,
Winson Chung0dbd7342013-10-13 22:46:20 -0700541 Utilities.createIconDrawable(item.getIcon(mIconCache)), null, null);
Adam Cohendf2cc412011-04-27 16:56:57 -0700542 textView.setText(item.title);
543 textView.setTag(item);
Adam Cohen477828c2013-09-20 12:05:49 -0700544 textView.setTextColor(getResources().getColor(R.color.folder_items_text_color));
545 textView.setShadowsEnabled(false);
Adam Cohen7397e622013-10-24 12:11:34 -0700546 textView.setGlowColor(getResources().getColor(R.color.folder_items_glow_color));
Adam Cohendf2cc412011-04-27 16:56:57 -0700547
548 textView.setOnClickListener(this);
549 textView.setOnLongClickListener(this);
550
Adam Cohenc508b2d2011-06-28 14:41:44 -0700551 // We need to check here to verify that the given item's location isn't already occupied
Adam Cohen3bf84d32012-05-07 20:17:14 -0700552 // by another item.
Adam Cohen0057bbc2011-08-12 18:30:51 -0700553 if (mContent.getChildAt(item.cellX, item.cellY) != null || item.cellX < 0 || item.cellY < 0
554 || item.cellX >= mContent.getCountX() || item.cellY >= mContent.getCountY()) {
Adam Cohen3bf84d32012-05-07 20:17:14 -0700555 // This shouldn't happen, log it.
556 Log.e(TAG, "Folder order not properly persisted during bind");
Adam Cohenc508b2d2011-06-28 14:41:44 -0700557 if (!findAndSetEmptyCells(item)) {
558 return false;
559 }
560 }
561
Adam Cohendf2cc412011-04-27 16:56:57 -0700562 CellLayout.LayoutParams lp =
563 new CellLayout.LayoutParams(item.cellX, item.cellY, item.spanX, item.spanY);
564 boolean insert = false;
Adam Cohenac56cff2011-09-28 20:45:37 -0700565 textView.setOnKeyListener(new FolderKeyEventListener());
Adam Cohendf2cc412011-04-27 16:56:57 -0700566 mContent.addViewToCellLayout(textView, insert ? 0 : -1, (int)item.id, lp, true);
Adam Cohenc508b2d2011-06-28 14:41:44 -0700567 return true;
Adam Cohendf2cc412011-04-27 16:56:57 -0700568 }
569
Adam Cohencb3382b2011-05-24 14:07:08 -0700570 public void onDragEnter(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700571 mPreviousTargetCell[0] = -1;
572 mPreviousTargetCell[1] = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700573 mOnExitAlarm.cancelAlarm();
574 }
575
576 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
577 public void onAlarm(Alarm alarm) {
578 realTimeReorder(mEmptyCell, mTargetCell);
579 }
580 };
581
582 boolean readingOrderGreaterThan(int[] v1, int[] v2) {
583 if (v1[1] > v2[1] || (v1[1] == v2[1] && v1[0] > v2[0])) {
584 return true;
585 } else {
586 return false;
587 }
588 }
589
590 private void realTimeReorder(int[] empty, int[] target) {
591 boolean wrap;
592 int startX;
593 int endX;
594 int startY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700595 int delay = 0;
596 float delayAmount = 30;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700597 if (readingOrderGreaterThan(target, empty)) {
598 wrap = empty[0] >= mContent.getCountX() - 1;
599 startY = wrap ? empty[1] + 1 : empty[1];
600 for (int y = startY; y <= target[1]; y++) {
601 startX = y == empty[1] ? empty[0] + 1 : 0;
602 endX = y < target[1] ? mContent.getCountX() - 1 : target[0];
603 for (int x = startX; x <= endX; x++) {
604 View v = mContent.getChildAt(x,y);
605 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen482ed822012-03-02 14:15:13 -0800606 REORDER_ANIMATION_DURATION, delay, true, true)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700607 empty[0] = x;
608 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700609 delay += delayAmount;
610 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700611 }
612 }
613 }
614 } else {
615 wrap = empty[0] == 0;
616 startY = wrap ? empty[1] - 1 : empty[1];
617 for (int y = startY; y >= target[1]; y--) {
618 startX = y == empty[1] ? empty[0] - 1 : mContent.getCountX() - 1;
619 endX = y > target[1] ? 0 : target[0];
620 for (int x = startX; x >= endX; x--) {
621 View v = mContent.getChildAt(x,y);
622 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen482ed822012-03-02 14:15:13 -0800623 REORDER_ANIMATION_DURATION, delay, true, true)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700624 empty[0] = x;
625 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700626 delay += delayAmount;
627 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700628 }
629 }
630 }
631 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700632 }
633
Adam Cohen2374abf2013-04-16 14:56:57 -0700634 public boolean isLayoutRtl() {
635 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
636 }
637
Adam Cohencb3382b2011-05-24 14:07:08 -0700638 public void onDragOver(DragObject d) {
Alan Viverette4cda5b72013-08-28 17:53:41 -0700639 final DragView dragView = d.dragView;
640 final int scrollOffset = mScrollView.getScrollY();
641 final float[] r = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, dragView, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700642 r[0] -= getPaddingLeft();
643 r[1] -= getPaddingTop();
644
Alan Viverette4cda5b72013-08-28 17:53:41 -0700645 final long downTime = SystemClock.uptimeMillis();
646 final MotionEvent translatedEv = MotionEvent.obtain(
647 downTime, downTime, MotionEvent.ACTION_MOVE, d.x, d.y, 0);
648
649 if (!mAutoScrollHelper.isEnabled()) {
650 mAutoScrollHelper.setEnabled(true);
651 }
652
653 final boolean handled = mAutoScrollHelper.onTouch(this, translatedEv);
654 translatedEv.recycle();
655
656 if (handled) {
657 mReorderAlarm.cancelAlarm();
658 } else {
659 mTargetCell = mContent.findNearestArea(
660 (int) r[0], (int) r[1] + scrollOffset, 1, 1, mTargetCell);
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700661 if (isLayoutRtl()) {
662 mTargetCell[0] = mContent.getCountX() - mTargetCell[0] - 1;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700663 }
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700664 if (mTargetCell[0] != mPreviousTargetCell[0]
665 || mTargetCell[1] != mPreviousTargetCell[1]) {
666 mReorderAlarm.cancelAlarm();
667 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
668 mReorderAlarm.setAlarm(REORDER_DELAY);
669 mPreviousTargetCell[0] = mTargetCell[0];
670 mPreviousTargetCell[1] = mTargetCell[1];
671 mDragMode = DRAG_MODE_REORDER;
672 } else {
673 mDragMode = DRAG_MODE_NONE;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700674 }
Adam Cohenbfbfd262011-06-13 16:55:12 -0700675 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700676 }
677
Adam Cohenbfbfd262011-06-13 16:55:12 -0700678 // This is used to compute the visual center of the dragView. The idea is that
679 // the visual center represents the user's interpretation of where the item is, and hence
680 // is the appropriate point to use when determining drop location.
681 private float[] getDragViewVisualCenter(int x, int y, int xOffset, int yOffset,
682 DragView dragView, float[] recycle) {
683 float res[];
684 if (recycle == null) {
685 res = new float[2];
686 } else {
687 res = recycle;
688 }
689
690 // These represent the visual top and left of drag view if a dragRect was provided.
691 // If a dragRect was not provided, then they correspond to the actual view left and
692 // top, as the dragRect is in that case taken to be the entire dragView.
693 // R.dimen.dragViewOffsetY.
694 int left = x - xOffset;
695 int top = y - yOffset;
696
697 // In order to find the visual center, we shift by half the dragRect
698 res[0] = left + dragView.getDragRegion().width() / 2;
699 res[1] = top + dragView.getDragRegion().height() / 2;
700
701 return res;
702 }
703
704 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
705 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700706 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700707 }
708 };
709
Adam Cohen95bb8002011-07-03 23:40:28 -0700710 public void completeDragExit() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700711 mLauncher.closeFolder();
712 mCurrentDragInfo = null;
713 mCurrentDragView = null;
714 mSuppressOnAdd = false;
715 mRearrangeOnClose = true;
716 }
717
Adam Cohencb3382b2011-05-24 14:07:08 -0700718 public void onDragExit(DragObject d) {
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700719 // Exiting folder; stop the auto scroller.
Alan Viverette4cda5b72013-08-28 17:53:41 -0700720 mAutoScrollHelper.setEnabled(false);
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700721 // We only close the folder if this is a true drag exit, ie. not because
722 // a drop has occurred above the folder.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700723 if (!d.dragComplete) {
724 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
725 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
726 }
727 mReorderAlarm.cancelAlarm();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700728 mDragMode = DRAG_MODE_NONE;
Adam Cohen2801caf2011-05-13 20:57:39 -0700729 }
730
Michael Jurka1e2f4652013-07-08 18:03:46 -0700731 public void onDropCompleted(final View target, final DragObject d,
732 final boolean isFlingToDelete, final boolean success) {
733 if (mDeferDropAfterUninstall) {
Michael Jurkaf3007582013-08-21 14:33:57 +0200734 Log.d(TAG, "Deferred handling drop because waiting for uninstall.");
Michael Jurka1e2f4652013-07-08 18:03:46 -0700735 mDeferredAction = new Runnable() {
736 public void run() {
737 onDropCompleted(target, d, isFlingToDelete, success);
738 mDeferredAction = null;
739 }
740 };
741 return;
742 }
743
744 boolean beingCalledAfterUninstall = mDeferredAction != null;
745 boolean successfulDrop =
746 success && (!beingCalledAfterUninstall || mUninstallSuccessful);
Winson Chung5f8afe62013-08-12 16:19:28 -0700747
Michael Jurka1e2f4652013-07-08 18:03:46 -0700748 if (successfulDrop) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700749 if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700750 replaceFolderWithFinalItem();
751 }
752 } else {
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700753 setupContentForNumItems(getItemCount());
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700754 // The drag failed, we need to return the item to the folder
755 mFolderIcon.onDrop(d);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700756 }
757
758 if (target != this) {
759 if (mOnExitAlarm.alarmPending()) {
760 mOnExitAlarm.cancelAlarm();
Michael Jurka54554252013-08-01 12:52:23 +0200761 if (!successfulDrop) {
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700762 mSuppressFolderDeletion = true;
763 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700764 completeDragExit();
765 }
Adam Cohenf0f2ffa2013-11-12 11:44:58 +0000766 } else {
767 mLauncher.getWorkspace().removeExtraEmptyScreen(true, null);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700768 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700769
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700770 mDeleteFolderOnDropCompleted = false;
771 mDragInProgress = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700772 mItemAddedBackToSelfViaIcon = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700773 mCurrentDragInfo = null;
774 mCurrentDragView = null;
775 mSuppressOnAdd = false;
Adam Cohen4045eb72011-10-06 11:44:26 -0700776
777 // Reordering may have occured, and we need to save the new item locations. We do this once
778 // at the end to prevent unnecessary database operations.
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700779 updateItemLocationsInDatabaseBatch();
Adam Cohen4045eb72011-10-06 11:44:26 -0700780 }
781
Michael Jurka1e2f4652013-07-08 18:03:46 -0700782 public void deferCompleteDropAfterUninstallActivity() {
783 mDeferDropAfterUninstall = true;
784 }
785
786 public void onUninstallActivityReturned(boolean success) {
787 mDeferDropAfterUninstall = false;
788 mUninstallSuccessful = success;
789 if (mDeferredAction != null) {
790 mDeferredAction.run();
791 }
792 }
793
Winson Chunga48487a2012-03-20 16:19:37 -0700794 @Override
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800795 public float getIntrinsicIconScaleFactor() {
796 return 1f;
797 }
798
799 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800800 public boolean supportsFlingToDelete() {
801 return true;
802 }
803
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000804 @Override
805 public boolean supportsAppInfoDropTarget() {
806 return false;
807 }
808
809 @Override
810 public boolean supportsDeleteDropTarget() {
811 return true;
812 }
813
Winson Chunga48487a2012-03-20 16:19:37 -0700814 public void onFlingToDelete(DragObject d, int x, int y, PointF vec) {
815 // Do nothing
816 }
817
818 @Override
819 public void onFlingToDeleteCompleted() {
820 // Do nothing
821 }
822
Adam Cohen4045eb72011-10-06 11:44:26 -0700823 private void updateItemLocationsInDatabase() {
824 ArrayList<View> list = getItemsInReadingOrder();
825 for (int i = 0; i < list.size(); i++) {
826 View v = list.get(i);
827 ItemInfo info = (ItemInfo) v.getTag();
828 LauncherModel.moveItemInDatabase(mLauncher, info, mInfo.id, 0,
829 info.cellX, info.cellY);
830 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700831 }
Adam Cohen228da5a2011-07-27 22:23:47 -0700832
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700833 private void updateItemLocationsInDatabaseBatch() {
834 ArrayList<View> list = getItemsInReadingOrder();
835 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
836 for (int i = 0; i < list.size(); i++) {
837 View v = list.get(i);
838 ItemInfo info = (ItemInfo) v.getTag();
839 items.add(info);
840 }
841
842 LauncherModel.moveItemsInDatabase(mLauncher, items, mInfo.id, 0);
843 }
844
Adam Cohene25af792013-06-06 23:08:25 -0700845 public void addItemLocationsInDatabase() {
846 ArrayList<View> list = getItemsInReadingOrder();
847 for (int i = 0; i < list.size(); i++) {
848 View v = list.get(i);
849 ItemInfo info = (ItemInfo) v.getTag();
850 LauncherModel.addItemToDatabase(mLauncher, info, mInfo.id, 0,
851 info.cellX, info.cellY, false);
852 }
853 }
854
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700855 public void notifyDrop() {
856 if (mDragInProgress) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700857 mItemAddedBackToSelfViaIcon = true;
Adam Cohen76078c42011-06-09 15:06:52 -0700858 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700859 }
860
861 public boolean isDropEnabled() {
862 return true;
863 }
864
Adam Cohen4045eb72011-10-06 11:44:26 -0700865 private void setupContentDimensions(int count) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700866 ArrayList<View> list = getItemsInReadingOrder();
867
868 int countX = mContent.getCountX();
869 int countY = mContent.getCountY();
Adam Cohen7c693212011-05-18 15:26:57 -0700870 boolean done = false;
Adam Cohen2801caf2011-05-13 20:57:39 -0700871
Adam Cohen7c693212011-05-18 15:26:57 -0700872 while (!done) {
873 int oldCountX = countX;
874 int oldCountY = countY;
875 if (countX * countY < count) {
876 // Current grid is too small, expand it
Adam Cohen78dc83e2011-11-15 17:10:00 -0800877 if ((countX <= countY || countY == mMaxCountY) && countX < mMaxCountX) {
Adam Cohen7c693212011-05-18 15:26:57 -0700878 countX++;
879 } else if (countY < mMaxCountY) {
880 countY++;
881 }
882 if (countY == 0) countY++;
883 } else if ((countY - 1) * countX >= count && countY >= countX) {
884 countY = Math.max(0, countY - 1);
885 } else if ((countX - 1) * countY >= count) {
886 countX = Math.max(0, countX - 1);
Adam Cohen2801caf2011-05-13 20:57:39 -0700887 }
Adam Cohen7c693212011-05-18 15:26:57 -0700888 done = countX == oldCountX && countY == oldCountY;
Adam Cohen2801caf2011-05-13 20:57:39 -0700889 }
Adam Cohen7c693212011-05-18 15:26:57 -0700890 mContent.setGridSize(countX, countY);
Adam Cohen2801caf2011-05-13 20:57:39 -0700891 arrangeChildren(list);
892 }
893
894 public boolean isFull() {
Adam Cohen78dc83e2011-11-15 17:10:00 -0800895 return getItemCount() >= mMaxNumItems;
Adam Cohen2801caf2011-05-13 20:57:39 -0700896 }
897
898 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700899 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700900
Winson Chung892c74d2013-08-22 16:15:50 -0700901 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700902 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700903 int height = getFolderHeight();
Adam Cohen2801caf2011-05-13 20:57:39 -0700904
Adam Cohen307fe232012-08-16 17:55:58 -0700905 float scale = parent.getDescendantRectRelativeToSelf(mFolderIcon, mTempRect);
Adam Cohen8e776a62011-06-28 18:10:06 -0700906
Winson Chungaf40f202013-09-18 18:26:31 -0700907 LauncherAppState app = LauncherAppState.getInstance();
908 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
909
Adam Cohen307fe232012-08-16 17:55:58 -0700910 int centerX = (int) (mTempRect.left + mTempRect.width() * scale / 2);
911 int centerY = (int) (mTempRect.top + mTempRect.height() * scale / 2);
Adam Cohen2801caf2011-05-13 20:57:39 -0700912 int centeredLeft = centerX - width / 2;
913 int centeredTop = centerY - height / 2;
Winson Chung3057b1c2013-10-10 17:35:35 -0700914 int currentPage = mLauncher.getWorkspace().getNextPage();
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800915 // In case the workspace is scrolling, we need to use the final scroll to compute
916 // the folders bounds.
917 mLauncher.getWorkspace().setFinalScrollForPageChange(currentPage);
Adam Cohen35e7e642011-07-17 14:47:18 -0700918 // We first fetch the currently visible CellLayoutChildren
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800919 CellLayout currentLayout = (CellLayout) mLauncher.getWorkspace().getChildAt(currentPage);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700920 ShortcutAndWidgetContainer boundingLayout = currentLayout.getShortcutsAndWidgets();
Adam Cohen35e7e642011-07-17 14:47:18 -0700921 Rect bounds = new Rect();
922 parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800923 // We reset the workspaces scroll
924 mLauncher.getWorkspace().resetFinalScrollForPageChange(currentPage);
Adam Cohen2801caf2011-05-13 20:57:39 -0700925
Adam Cohen35e7e642011-07-17 14:47:18 -0700926 // We need to bound the folder to the currently visible CellLayoutChildren
927 int left = Math.min(Math.max(bounds.left, centeredLeft),
928 bounds.left + bounds.width() - width);
929 int top = Math.min(Math.max(bounds.top, centeredTop),
930 bounds.top + bounds.height() - height);
Winson Chungaf40f202013-09-18 18:26:31 -0700931 if (grid.isPhone() && (grid.availableWidthPx - width) < grid.iconSizePx) {
932 // Center the folder if it is full (on phones only)
933 left = (grid.availableWidthPx - width) / 2;
934 } else if (width >= bounds.width()) {
935 // If the folder doesn't fit within the bounds, center it about the desired bounds
Adam Cohen35e7e642011-07-17 14:47:18 -0700936 left = bounds.left + (bounds.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700937 }
Adam Cohen35e7e642011-07-17 14:47:18 -0700938 if (height >= bounds.height()) {
939 top = bounds.top + (bounds.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700940 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700941
942 int folderPivotX = width / 2 + (centeredLeft - left);
943 int folderPivotY = height / 2 + (centeredTop - top);
944 setPivotX(folderPivotX);
945 setPivotY(folderPivotY);
Adam Cohen268c4752012-06-06 17:47:33 -0700946 mFolderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700947 (1.0f * folderPivotX / width));
Adam Cohen268c4752012-06-06 17:47:33 -0700948 mFolderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700949 (1.0f * folderPivotY / height));
Adam Cohen3bf84d32012-05-07 20:17:14 -0700950
Adam Cohen662b5982011-12-13 17:45:21 -0800951 lp.width = width;
952 lp.height = height;
953 lp.x = left;
954 lp.y = top;
Adam Cohen2801caf2011-05-13 20:57:39 -0700955 }
956
Adam Cohen268c4752012-06-06 17:47:33 -0700957 float getPivotXForIconAnimation() {
958 return mFolderIconPivotX;
959 }
960 float getPivotYForIconAnimation() {
961 return mFolderIconPivotY;
962 }
963
Adam Cohen2801caf2011-05-13 20:57:39 -0700964 private void setupContentForNumItems(int count) {
Adam Cohen4045eb72011-10-06 11:44:26 -0700965 setupContentDimensions(count);
Adam Cohen2801caf2011-05-13 20:57:39 -0700966
Adam Cohen8e776a62011-06-28 18:10:06 -0700967 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700968 if (lp == null) {
Adam Cohen8e776a62011-06-28 18:10:06 -0700969 lp = new DragLayer.LayoutParams(0, 0);
970 lp.customPosition = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700971 setLayoutParams(lp);
972 }
973 centerAboutIcon();
974 }
975
Winson Chung892c74d2013-08-22 16:15:50 -0700976 private int getContentAreaHeight() {
977 LauncherAppState app = LauncherAppState.getInstance();
978 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
979 Rect workspacePadding = grid.getWorkspacePadding(grid.isLandscape ?
980 CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
981 int maxContentAreaHeight = grid.availableHeightPx -
Winson Chung892c74d2013-08-22 16:15:50 -0700982 workspacePadding.top - workspacePadding.bottom -
Winson Chung892c74d2013-08-22 16:15:50 -0700983 mFolderNameHeight;
Adam Cohen1960ea42013-11-12 11:33:14 +0000984 int height = Math.min(maxContentAreaHeight,
Winson Chung892c74d2013-08-22 16:15:50 -0700985 mContent.getDesiredHeight());
Adam Cohen1960ea42013-11-12 11:33:14 +0000986 return Math.max(height, MIN_CONTENT_DIMEN);
987 }
988
989 private int getContentAreaWidth() {
990 return Math.max(mContent.getDesiredWidth(), MIN_CONTENT_DIMEN);
Winson Chung892c74d2013-08-22 16:15:50 -0700991 }
992
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700993 private int getFolderHeight() {
Winson Chung892c74d2013-08-22 16:15:50 -0700994 int height = getPaddingTop() + getPaddingBottom()
995 + getContentAreaHeight() + mFolderNameHeight;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700996 return height;
997 }
Adam Cohen234c4cd2011-07-17 21:03:04 -0700998
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700999 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001000 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
1001 int height = getFolderHeight();
Adam Cohen1960ea42013-11-12 11:33:14 +00001002 int contentAreaWidthSpec = MeasureSpec.makeMeasureSpec(getContentAreaWidth(),
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001003 MeasureSpec.EXACTLY);
Winson Chung892c74d2013-08-22 16:15:50 -07001004 int contentAreaHeightSpec = MeasureSpec.makeMeasureSpec(getContentAreaHeight(),
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001005 MeasureSpec.EXACTLY);
Adam Cohen1960ea42013-11-12 11:33:14 +00001006
1007 mContent.setFixedSize(getContentAreaWidth(), getContentAreaHeight());
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001008 mScrollView.measure(contentAreaWidthSpec, contentAreaHeightSpec);
1009 mFolderName.measure(contentAreaWidthSpec,
Adam Cohen234c4cd2011-07-17 21:03:04 -07001010 MeasureSpec.makeMeasureSpec(mFolderNameHeight, MeasureSpec.EXACTLY));
1011 setMeasuredDimension(width, height);
1012 }
1013
Adam Cohen2801caf2011-05-13 20:57:39 -07001014 private void arrangeChildren(ArrayList<View> list) {
1015 int[] vacant = new int[2];
1016 if (list == null) {
1017 list = getItemsInReadingOrder();
1018 }
1019 mContent.removeAllViews();
1020
1021 for (int i = 0; i < list.size(); i++) {
1022 View v = list.get(i);
1023 mContent.getVacantCell(vacant, 1, 1);
1024 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) v.getLayoutParams();
1025 lp.cellX = vacant[0];
1026 lp.cellY = vacant[1];
1027 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohen2792a332011-09-26 21:09:47 -07001028 if (info.cellX != vacant[0] || info.cellY != vacant[1]) {
1029 info.cellX = vacant[0];
1030 info.cellY = vacant[1];
1031 LauncherModel.addOrMoveItemInDatabase(mLauncher, info, mInfo.id, 0,
1032 info.cellX, info.cellY);
1033 }
Adam Cohen2801caf2011-05-13 20:57:39 -07001034 boolean insert = false;
1035 mContent.addViewToCellLayout(v, insert ? 0 : -1, (int)info.id, lp, true);
1036 }
Adam Cohen7c693212011-05-18 15:26:57 -07001037 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -07001038 }
1039
Adam Cohena9cf38f2011-05-02 15:36:58 -07001040 public int getItemCount() {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001041 return mContent.getShortcutsAndWidgets().getChildCount();
Adam Cohena9cf38f2011-05-02 15:36:58 -07001042 }
1043
1044 public View getItemAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001045 return mContent.getShortcutsAndWidgets().getChildAt(index);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001046 }
1047
Adam Cohen2801caf2011-05-13 20:57:39 -07001048 private void onCloseComplete() {
Adam Cohen05e0f402011-08-01 12:12:49 -07001049 DragLayer parent = (DragLayer) getParent();
Michael Jurka5649c282012-06-18 10:33:21 -07001050 if (parent != null) {
1051 parent.removeView(this);
1052 }
Adam Cohen4554ee12011-08-03 16:13:21 -07001053 mDragController.removeDropTarget((DropTarget) this);
Adam Cohen05e0f402011-08-01 12:12:49 -07001054 clearFocus();
Adam Cohenac56cff2011-09-28 20:45:37 -07001055 mFolderIcon.requestFocus();
Adam Cohen05e0f402011-08-01 12:12:49 -07001056
Adam Cohen2801caf2011-05-13 20:57:39 -07001057 if (mRearrangeOnClose) {
1058 setupContentForNumItems(getItemCount());
1059 mRearrangeOnClose = false;
1060 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001061 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001062 if (!mDragInProgress && !mSuppressFolderDeletion) {
1063 replaceFolderWithFinalItem();
1064 } else if (mDragInProgress) {
1065 mDeleteFolderOnDropCompleted = true;
1066 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001067 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001068 mSuppressFolderDeletion = false;
Adam Cohenafb01ee2011-06-23 15:38:03 -07001069 }
1070
1071 private void replaceFolderWithFinalItem() {
Adam Cohenafb01ee2011-06-23 15:38:03 -07001072 // Add the last remaining child to the workspace in place of the folder
Adam Cohenfb91f302012-06-11 15:45:18 -07001073 Runnable onCompleteRunnable = new Runnable() {
1074 @Override
1075 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -07001076 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screenId);
Adam Cohenafb01ee2011-06-23 15:38:03 -07001077
Adam Cohenc5e63f32012-07-12 16:16:57 -07001078 View child = null;
Adam Cohenfb91f302012-06-11 15:45:18 -07001079 // Move the item from the folder to the workspace, in the position of the folder
1080 if (getItemCount() == 1) {
1081 ShortcutInfo finalItem = mInfo.contents.get(0);
Adam Cohenc5e63f32012-07-12 16:16:57 -07001082 child = mLauncher.createShortcut(R.layout.application, cellLayout,
Adam Cohenfb91f302012-06-11 15:45:18 -07001083 finalItem);
1084 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -07001085 mInfo.screenId, mInfo.cellX, mInfo.cellY);
Adam Cohenfb91f302012-06-11 15:45:18 -07001086 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001087 if (getItemCount() <= 1) {
1088 // Remove the folder
1089 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
1090 cellLayout.removeView(mFolderIcon);
1091 if (mFolderIcon instanceof DropTarget) {
1092 mDragController.removeDropTarget((DropTarget) mFolderIcon);
1093 }
1094 mLauncher.removeFolder(mInfo);
1095 }
Adam Cohenc5e63f32012-07-12 16:16:57 -07001096 // We add the child after removing the folder to prevent both from existing at
Winson Chung0e6a7132013-08-23 12:55:10 -07001097 // the same time in the CellLayout. We need to add the new item with addInScreenFromBind()
1098 // to ensure that hotseat items are placed correctly.
Adam Cohenc5e63f32012-07-12 16:16:57 -07001099 if (child != null) {
Winson Chung0e6a7132013-08-23 12:55:10 -07001100 mLauncher.getWorkspace().addInScreenFromBind(child, mInfo.container, mInfo.screenId,
Adam Cohenc5e63f32012-07-12 16:16:57 -07001101 mInfo.cellX, mInfo.cellY, mInfo.spanX, mInfo.spanY);
1102 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001103 }
1104 };
1105 View finalChild = getItemAt(0);
1106 if (finalChild != null) {
1107 mFolderIcon.performDestroyAnimation(finalChild, onCompleteRunnable);
Adam Cohenafb01ee2011-06-23 15:38:03 -07001108 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001109 mDestroyed = true;
1110 }
1111
1112 boolean isDestroyed() {
1113 return mDestroyed;
Adam Cohen2801caf2011-05-13 20:57:39 -07001114 }
1115
Adam Cohenac56cff2011-09-28 20:45:37 -07001116 // This method keeps track of the last item in the folder for the purposes
1117 // of keyboard focus
1118 private void updateTextViewFocus() {
1119 View lastChild = getItemAt(getItemCount() - 1);
1120 getItemAt(getItemCount() - 1);
1121 if (lastChild != null) {
1122 mFolderName.setNextFocusDownId(lastChild.getId());
1123 mFolderName.setNextFocusRightId(lastChild.getId());
1124 mFolderName.setNextFocusLeftId(lastChild.getId());
1125 mFolderName.setNextFocusUpId(lastChild.getId());
1126 }
1127 }
1128
Adam Cohenbfbfd262011-06-13 16:55:12 -07001129 public void onDrop(DragObject d) {
1130 ShortcutInfo item;
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001131 if (d.dragInfo instanceof AppInfo) {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001132 // Came from all apps -- make a copy
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001133 item = ((AppInfo) d.dragInfo).makeShortcut();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001134 item.spanX = 1;
1135 item.spanY = 1;
1136 } else {
1137 item = (ShortcutInfo) d.dragInfo;
1138 }
Adam Cohen05e0f402011-08-01 12:12:49 -07001139 // Dragged from self onto self, currently this is the only path possible, however
1140 // we keep this as a distinct code path.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001141 if (item == mCurrentDragInfo) {
1142 ShortcutInfo si = (ShortcutInfo) mCurrentDragView.getTag();
1143 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) mCurrentDragView.getLayoutParams();
1144 si.cellX = lp.cellX = mEmptyCell[0];
1145 si.cellX = lp.cellY = mEmptyCell[1];
1146 mContent.addViewToCellLayout(mCurrentDragView, -1, (int)item.id, lp, true);
Adam Cohenfc53cd22011-07-20 15:45:11 -07001147 if (d.dragView.hasDrawn()) {
1148 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, mCurrentDragView);
1149 } else {
Winson Chung2c4cf412012-05-08 14:03:21 -07001150 d.deferDragViewCleanupPostAnimation = false;
Adam Cohenfc53cd22011-07-20 15:45:11 -07001151 mCurrentDragView.setVisibility(VISIBLE);
1152 }
Adam Cohene9166b22011-07-08 17:11:11 -07001153 mItemsInvalidated = true;
Adam Cohen4045eb72011-10-06 11:44:26 -07001154 setupContentDimensions(getItemCount());
Adam Cohen716b51e2011-06-30 12:09:54 -07001155 mSuppressOnAdd = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001156 }
1157 mInfo.add(item);
1158 }
1159
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001160 // This is used so the item doesn't immediately appear in the folder when added. In one case
1161 // we need to create the illusion that the item isn't added back to the folder yet, to
1162 // to correspond to the animation of the icon back into the folder. This is
1163 public void hideItem(ShortcutInfo info) {
1164 View v = getViewForInfo(info);
1165 v.setVisibility(INVISIBLE);
1166 }
1167 public void showItem(ShortcutInfo info) {
1168 View v = getViewForInfo(info);
1169 v.setVisibility(VISIBLE);
1170 }
1171
Adam Cohenbfbfd262011-06-13 16:55:12 -07001172 public void onAdd(ShortcutInfo item) {
1173 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001174 // If the item was dropped onto this open folder, we have done the work associated
1175 // with adding the item to the folder, as indicated by mSuppressOnAdd being set
Adam Cohenbfbfd262011-06-13 16:55:12 -07001176 if (mSuppressOnAdd) return;
1177 if (!findAndSetEmptyCells(item)) {
1178 // The current layout is full, can we expand it?
1179 setupContentForNumItems(getItemCount() + 1);
1180 findAndSetEmptyCells(item);
1181 }
1182 createAndAddShortcut(item);
1183 LauncherModel.addOrMoveItemInDatabase(
1184 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
1185 }
1186
Adam Cohena9cf38f2011-05-02 15:36:58 -07001187 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -07001188 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001189 // If this item is being dragged from this open folder, we have already handled
1190 // the work associated with removing the item, so we don't have to do anything here.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001191 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001192 View v = getViewForInfo(item);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001193 mContent.removeView(v);
Adam Cohen2801caf2011-05-13 20:57:39 -07001194 if (mState == STATE_ANIMATING) {
1195 mRearrangeOnClose = true;
1196 } else {
1197 setupContentForNumItems(getItemCount());
1198 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001199 if (getItemCount() <= 1) {
1200 replaceFolderWithFinalItem();
1201 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001202 }
Adam Cohen7c693212011-05-18 15:26:57 -07001203
Adam Cohendf1e4e82011-06-24 15:57:39 -07001204 private View getViewForInfo(ShortcutInfo item) {
1205 for (int j = 0; j < mContent.getCountY(); j++) {
1206 for (int i = 0; i < mContent.getCountX(); i++) {
1207 View v = mContent.getChildAt(i, j);
1208 if (v.getTag() == item) {
1209 return v;
1210 }
1211 }
1212 }
1213 return null;
1214 }
1215
Adam Cohen76078c42011-06-09 15:06:52 -07001216 public void onItemsChanged() {
Adam Cohenac56cff2011-09-28 20:45:37 -07001217 updateTextViewFocus();
Adam Cohen76078c42011-06-09 15:06:52 -07001218 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001219
Adam Cohen76fc0852011-06-17 13:26:23 -07001220 public void onTitleChanged(CharSequence title) {
1221 }
Adam Cohen76078c42011-06-09 15:06:52 -07001222
Adam Cohen7c693212011-05-18 15:26:57 -07001223 public ArrayList<View> getItemsInReadingOrder() {
1224 if (mItemsInvalidated) {
1225 mItemsInReadingOrder.clear();
1226 for (int j = 0; j < mContent.getCountY(); j++) {
1227 for (int i = 0; i < mContent.getCountX(); i++) {
1228 View v = mContent.getChildAt(i, j);
1229 if (v != null) {
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001230 mItemsInReadingOrder.add(v);
Adam Cohen7c693212011-05-18 15:26:57 -07001231 }
1232 }
1233 }
1234 mItemsInvalidated = false;
1235 }
1236 return mItemsInReadingOrder;
1237 }
Adam Cohen8dfcba42011-07-07 16:38:18 -07001238
1239 public void getLocationInDragLayer(int[] loc) {
1240 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
1241 }
Adam Cohenea0818d2011-09-30 20:06:58 -07001242
1243 public void onFocusChange(View v, boolean hasFocus) {
1244 if (v == mFolderName && hasFocus) {
1245 startEditingFolderName();
1246 }
1247 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001248
1249 @Override
1250 public void getHitRectRelativeToDragLayer(Rect outRect) {
1251 getHitRect(outRect);
1252 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001253}