blob: f26d01f5d006ef127fdb5cf3be65814b30227245 [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 Cohenfb91f302012-06-11 15:45:18 -0700122 private boolean mDestroyed;
123
Alan Viverette4cda5b72013-08-28 17:53:41 -0700124 private AutoScrollHelper mAutoScrollHelper;
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700125
Michael Jurka1e2f4652013-07-08 18:03:46 -0700126 private Runnable mDeferredAction;
127 private boolean mDeferDropAfterUninstall;
128 private boolean mUninstallSuccessful;
129
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130 /**
131 * Used to inflate the Workspace from XML.
132 *
133 * @param context The application's context.
134 * @param attrs The attribtues set containing the Workspace's customization values.
135 */
136 public Folder(Context context, AttributeSet attrs) {
137 super(context, attrs);
Winson Chung5f8afe62013-08-12 16:19:28 -0700138
139 LauncherAppState app = LauncherAppState.getInstance();
140 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141 setAlwaysDrawnWithCacheEnabled(false);
Adam Cohendf2cc412011-04-27 16:56:57 -0700142 mInflater = LayoutInflater.from(context);
Winson Chung5f8afe62013-08-12 16:19:28 -0700143 mIconCache = app.getIconCache();
Adam Cohen78dc83e2011-11-15 17:10:00 -0800144
145 Resources res = getResources();
Michael Jurka414300a2013-08-27 15:42:35 +0200146 mMaxCountX = (int) grid.numColumns;
Adam Cohen477828c2013-09-20 12:05:49 -0700147 mMaxCountY = (int) grid.numRows;
148 mMaxNumItems = mMaxCountX * mMaxCountY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700149
150 mInputMethodManager = (InputMethodManager)
Michael Jurka8b805b12012-04-18 14:23:14 -0700151 getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
Adam Cohen76fc0852011-06-17 13:26:23 -0700152
Adam Cohen76fc0852011-06-17 13:26:23 -0700153 mExpandDuration = res.getInteger(R.integer.config_folderAnimDuration);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700154
155 if (sDefaultFolderName == null) {
156 sDefaultFolderName = res.getString(R.string.folder_name);
157 }
Adam Cohena65beee2011-06-27 21:32:23 -0700158 if (sHintText == null) {
159 sHintText = res.getString(R.string.folder_hint_text);
160 }
Adam Cohen4eac29a2011-07-11 17:53:37 -0700161 mLauncher = (Launcher) context;
Adam Cohenac56cff2011-09-28 20:45:37 -0700162 // We need this view to be focusable in touch mode so that when text editing of the folder
163 // name is complete, we have something to focus on, thus hiding the cursor and giving
164 // reliable behvior when clicking the text field (since it will always gain focus on click).
165 setFocusableInTouchMode(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166 }
167
168 @Override
169 protected void onFinishInflate() {
170 super.onFinishInflate();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700171 mScrollView = (ScrollView) findViewById(R.id.scroll_view);
Adam Cohendf2cc412011-04-27 16:56:57 -0700172 mContent = (CellLayout) findViewById(R.id.folder_content);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700173
Winson Chung5f8afe62013-08-12 16:19:28 -0700174 LauncherAppState app = LauncherAppState.getInstance();
175 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
176
Winson Chung5f8afe62013-08-12 16:19:28 -0700177 mContent.setCellDimensions(grid.folderCellWidthPx, grid.folderCellHeightPx);
Adam Cohen2801caf2011-05-13 20:57:39 -0700178 mContent.setGridSize(0, 0);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700179 mContent.getShortcutsAndWidgets().setMotionEventSplittingEnabled(false);
Adam Cohen2374abf2013-04-16 14:56:57 -0700180 mContent.setInvertIfRtl(true);
Adam Cohenac56cff2011-09-28 20:45:37 -0700181 mFolderName = (FolderEditText) findViewById(R.id.folder_name);
182 mFolderName.setFolder(this);
Adam Cohenea0818d2011-09-30 20:06:58 -0700183 mFolderName.setOnFocusChangeListener(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700184
185 // We find out how tall the text view wants to be (it is set to wrap_content), so that
186 // we can allocate the appropriate amount of space for it.
187 int measureSpec = MeasureSpec.UNSPECIFIED;
188 mFolderName.measure(measureSpec, measureSpec);
189 mFolderNameHeight = mFolderName.getMeasuredHeight();
190
191 // We disable action mode for now since it messes up the view on phones
192 mFolderName.setCustomSelectionActionModeCallback(mActionModeCallback);
Adam Cohen76fc0852011-06-17 13:26:23 -0700193 mFolderName.setOnEditorActionListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700194 mFolderName.setSelectAllOnFocus(true);
Adam Cohen7a14d0b2011-06-24 11:36:35 -0700195 mFolderName.setInputType(mFolderName.getInputType() |
196 InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
Alan Viverette4cda5b72013-08-28 17:53:41 -0700197 mAutoScrollHelper = new FolderAutoScrollHelper(mScrollView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800198 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700199
Adam Cohen76fc0852011-06-17 13:26:23 -0700200 private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
201 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
202 return false;
203 }
204
205 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
206 return false;
207 }
208
209 public void onDestroyActionMode(ActionMode mode) {
210 }
211
212 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
213 return false;
214 }
215 };
216
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800217 public void onClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700218 Object tag = v.getTag();
219 if (tag instanceof ShortcutInfo) {
Adam Cohenb5fe60c2013-06-06 22:03:51 -0700220 mLauncher.onClick(v);
Adam Cohendf2cc412011-04-27 16:56:57 -0700221 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800222 }
223
224 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -0700225 // Return if global dragging is not enabled
226 if (!mLauncher.isDraggingEnabled()) return true;
227
Adam Cohendf2cc412011-04-27 16:56:57 -0700228 Object tag = v.getTag();
229 if (tag instanceof ShortcutInfo) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700230 ShortcutInfo item = (ShortcutInfo) tag;
231 if (!v.isInTouchMode()) {
232 return false;
233 }
234
Winson Chung7d7541e2011-09-16 20:14:36 -0700235 mLauncher.dismissFolderCling(null);
236
Adam Cohendf2cc412011-04-27 16:56:57 -0700237 mLauncher.getWorkspace().onDragStartedWithItem(v);
Adam Cohenac8c8762011-07-13 11:15:27 -0700238 mLauncher.getWorkspace().beginDragShared(v, this);
Adam Cohenbadf71e2011-05-26 19:08:29 -0700239 mIconDrawable = ((TextView) v).getCompoundDrawables()[1];
Adam Cohen76078c42011-06-09 15:06:52 -0700240
241 mCurrentDragInfo = item;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700242 mEmptyCell[0] = item.cellX;
243 mEmptyCell[1] = item.cellY;
244 mCurrentDragView = v;
Adam Cohenfc53cd22011-07-20 15:45:11 -0700245
246 mContent.removeView(mCurrentDragView);
247 mInfo.remove(mCurrentDragInfo);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700248 mDragInProgress = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700249 mItemAddedBackToSelfViaIcon = false;
Adam Cohendf2cc412011-04-27 16:56:57 -0700250 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800251 return true;
252 }
253
Adam Cohen76fc0852011-06-17 13:26:23 -0700254 public boolean isEditingName() {
255 return mIsEditingName;
256 }
257
258 public void startEditingFolderName() {
Adam Cohena65beee2011-06-27 21:32:23 -0700259 mFolderName.setHint("");
Adam Cohen76fc0852011-06-17 13:26:23 -0700260 mIsEditingName = true;
261 }
262
263 public void dismissEditingName() {
264 mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
265 doneEditingFolderName(true);
266 }
267
268 public void doneEditingFolderName(boolean commit) {
Adam Cohena65beee2011-06-27 21:32:23 -0700269 mFolderName.setHint(sHintText);
Adam Cohen1df26a32011-08-12 16:15:23 -0700270 // Convert to a string here to ensure that no other state associated with the text field
271 // gets saved.
Adam Cohen3371da02011-10-25 21:38:29 -0700272 String newTitle = mFolderName.getText().toString();
273 mInfo.setTitle(newTitle);
Adam Cohen76fc0852011-06-17 13:26:23 -0700274 LauncherModel.updateItemInDatabase(mLauncher, mInfo);
Adam Cohenac56cff2011-09-28 20:45:37 -0700275
Adam Cohen3371da02011-10-25 21:38:29 -0700276 if (commit) {
277 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700278 String.format(getContext().getString(R.string.folder_renamed), newTitle));
Adam Cohen3371da02011-10-25 21:38:29 -0700279 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700280 // In order to clear the focus from the text field, we set the focus on ourself. This
281 // ensures that every time the field is clicked, focus is gained, giving reliable behavior.
282 requestFocus();
283
Adam Cohene601a432011-07-26 21:51:30 -0700284 Selection.setSelection((Spannable) mFolderName.getText(), 0, 0);
Adam Cohen76fc0852011-06-17 13:26:23 -0700285 mIsEditingName = false;
286 }
287
288 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
289 if (actionId == EditorInfo.IME_ACTION_DONE) {
290 dismissEditingName();
291 return true;
292 }
293 return false;
294 }
295
296 public View getEditTextRegion() {
297 return mFolderName;
298 }
299
Adam Cohenbadf71e2011-05-26 19:08:29 -0700300 public Drawable getDragDrawable() {
301 return mIconDrawable;
302 }
303
Adam Cohen0c872ba2011-05-05 10:34:16 -0700304 /**
305 * We need to handle touch events to prevent them from falling through to the workspace below.
306 */
307 @Override
308 public boolean onTouchEvent(MotionEvent ev) {
309 return true;
310 }
311
Joe Onorato00acb122009-08-04 16:04:30 -0400312 public void setDragController(DragController dragController) {
313 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800314 }
315
Adam Cohen2801caf2011-05-13 20:57:39 -0700316 void setFolderIcon(FolderIcon icon) {
317 mFolderIcon = icon;
318 }
319
Adam Cohen3371da02011-10-25 21:38:29 -0700320 @Override
321 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
322 // When the folder gets focus, we don't want to announce the list of items.
323 return true;
324 }
325
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800326 /**
327 * @return the FolderInfo object associated with this folder
328 */
329 FolderInfo getInfo() {
330 return mInfo;
331 }
332
Adam Cohen3bf84d32012-05-07 20:17:14 -0700333 private class GridComparator implements Comparator<ShortcutInfo> {
334 int mNumCols;
335 public GridComparator(int numCols) {
336 mNumCols = numCols;
337 }
338
339 @Override
340 public int compare(ShortcutInfo lhs, ShortcutInfo rhs) {
341 int lhIndex = lhs.cellY * mNumCols + lhs.cellX;
342 int rhIndex = rhs.cellY * mNumCols + rhs.cellX;
343 return (lhIndex - rhIndex);
344 }
Adam Cohen3bf84d32012-05-07 20:17:14 -0700345 }
346
347 private void placeInReadingOrder(ArrayList<ShortcutInfo> items) {
348 int maxX = 0;
349 int count = items.size();
350 for (int i = 0; i < count; i++) {
351 ShortcutInfo item = items.get(i);
352 if (item.cellX > maxX) {
353 maxX = item.cellX;
354 }
355 }
Adam Cohen691a5792012-05-11 14:27:30 -0700356
357 GridComparator gridComparator = new GridComparator(maxX + 1);
Adam Cohen3bf84d32012-05-07 20:17:14 -0700358 Collections.sort(items, gridComparator);
359 final int countX = mContent.getCountX();
360 for (int i = 0; i < count; i++) {
361 int x = i % countX;
362 int y = i / countX;
363 ShortcutInfo item = items.get(i);
364 item.cellX = x;
365 item.cellY = y;
366 }
367 }
368
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800369 void bind(FolderInfo info) {
370 mInfo = info;
Adam Cohendf2cc412011-04-27 16:56:57 -0700371 ArrayList<ShortcutInfo> children = info.contents;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700372 ArrayList<ShortcutInfo> overflow = new ArrayList<ShortcutInfo>();
Adam Cohen7c693212011-05-18 15:26:57 -0700373 setupContentForNumItems(children.size());
Adam Cohen3bf84d32012-05-07 20:17:14 -0700374 placeInReadingOrder(children);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700375 int count = 0;
Adam Cohendf2cc412011-04-27 16:56:57 -0700376 for (int i = 0; i < children.size(); i++) {
377 ShortcutInfo child = (ShortcutInfo) children.get(i);
Adam Cohenc508b2d2011-06-28 14:41:44 -0700378 if (!createAndAddShortcut(child)) {
379 overflow.add(child);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700380 } else {
381 count++;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700382 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700383 }
Adam Cohenc508b2d2011-06-28 14:41:44 -0700384
Adam Cohen0057bbc2011-08-12 18:30:51 -0700385 // We rearrange the items in case there are any empty gaps
386 setupContentForNumItems(count);
387
Adam Cohenc508b2d2011-06-28 14:41:44 -0700388 // If our folder has too many items we prune them from the list. This is an issue
389 // when upgrading from the old Folders implementation which could contain an unlimited
390 // number of items.
391 for (ShortcutInfo item: overflow) {
392 mInfo.remove(item);
393 LauncherModel.deleteItemFromDatabase(mLauncher, item);
394 }
395
Adam Cohen4dbe6d92011-05-18 17:14:15 -0700396 mItemsInvalidated = true;
Adam Cohenac56cff2011-09-28 20:45:37 -0700397 updateTextViewFocus();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700398 mInfo.addListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700399
Adam Cohenafb01ee2011-06-23 15:38:03 -0700400 if (!sDefaultFolderName.contentEquals(mInfo.title)) {
Adam Cohen4ef610f2011-06-21 22:37:36 -0700401 mFolderName.setText(mInfo.title);
402 } else {
403 mFolderName.setText("");
404 }
Adam Cohen691a5792012-05-11 14:27:30 -0700405 updateItemLocationsInDatabase();
Adam Cohendf2cc412011-04-27 16:56:57 -0700406 }
407
408 /**
409 * Creates a new UserFolder, inflated from R.layout.user_folder.
410 *
411 * @param context The application's context.
412 *
413 * @return A new UserFolder.
414 */
415 static Folder fromXml(Context context) {
416 return (Folder) LayoutInflater.from(context).inflate(R.layout.user_folder, null);
417 }
418
419 /**
420 * This method is intended to make the UserFolder to be visually identical in size and position
421 * to its associated FolderIcon. This allows for a seamless transition into the expanded state.
422 */
423 private void positionAndSizeAsIcon() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700424 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800425 setScaleX(0.8f);
426 setScaleY(0.8f);
427 setAlpha(0f);
Adam Cohendf2cc412011-04-27 16:56:57 -0700428 mState = STATE_SMALL;
429 }
430
431 public void animateOpen() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700432 positionAndSizeAsIcon();
433
Adam Cohen8e776a62011-06-28 18:10:06 -0700434 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen2801caf2011-05-13 20:57:39 -0700435 centerAboutIcon();
Adam Cohen662b5982011-12-13 17:45:21 -0800436 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1);
437 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
438 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
Michael Jurka032e6ba2013-04-22 15:08:42 +0200439 final ObjectAnimator oa =
Michael Jurka2ecf9952012-06-18 12:52:28 -0700440 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohen6441de02011-12-14 14:25:32 -0800441
Adam Cohen2801caf2011-05-13 20:57:39 -0700442 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700443 @Override
444 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700445 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700446 String.format(getContext().getString(R.string.folder_opened),
Adam Cohen3371da02011-10-25 21:38:29 -0700447 mContent.getCountX(), mContent.getCountY()));
Adam Cohendf2cc412011-04-27 16:56:57 -0700448 mState = STATE_ANIMATING;
449 }
450 @Override
451 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700452 mState = STATE_OPEN;
Michael Jurka0121c3e2012-05-31 08:36:04 -0700453 setLayerType(LAYER_TYPE_NONE, null);
Winson Chung7a74ac92011-09-20 17:43:51 -0700454 Cling cling = mLauncher.showFirstRunFoldersCling();
455 if (cling != null) {
Winson Chungfa545132013-09-26 17:45:32 -0700456 cling.bringScrimToFront();
Winson Chungaf40f202013-09-18 18:26:31 -0700457 bringToFront();
Winson Chungfa545132013-09-26 17:45:32 -0700458 cling.bringToFront();
Winson Chung7a74ac92011-09-20 17:43:51 -0700459 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700460 setFocusOnFirstChild();
Adam Cohendf2cc412011-04-27 16:56:57 -0700461 }
462 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700463 oa.setDuration(mExpandDuration);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700464 setLayerType(LAYER_TYPE_HARDWARE, null);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100465 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700466 }
467
Adam Cohen3371da02011-10-25 21:38:29 -0700468 private void sendCustomAccessibilityEvent(int type, String text) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700469 AccessibilityManager accessibilityManager = (AccessibilityManager)
470 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
471 if (accessibilityManager.isEnabled()) {
Adam Cohen3371da02011-10-25 21:38:29 -0700472 AccessibilityEvent event = AccessibilityEvent.obtain(type);
473 onInitializeAccessibilityEvent(event);
474 event.getText().add(text);
Michael Jurka8b805b12012-04-18 14:23:14 -0700475 accessibilityManager.sendAccessibilityEvent(event);
Adam Cohen3371da02011-10-25 21:38:29 -0700476 }
477 }
478
Adam Cohenac56cff2011-09-28 20:45:37 -0700479 private void setFocusOnFirstChild() {
480 View firstChild = mContent.getChildAt(0, 0);
481 if (firstChild != null) {
482 firstChild.requestFocus();
483 }
484 }
485
Adam Cohendf2cc412011-04-27 16:56:57 -0700486 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700487 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800488 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
489 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
490 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
Michael Jurka032e6ba2013-04-22 15:08:42 +0200491 final ObjectAnimator oa =
Michael Jurka2ecf9952012-06-18 12:52:28 -0700492 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohendf2cc412011-04-27 16:56:57 -0700493
Adam Cohen2801caf2011-05-13 20:57:39 -0700494 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700495 @Override
496 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700497 onCloseComplete();
Michael Jurka0121c3e2012-05-31 08:36:04 -0700498 setLayerType(LAYER_TYPE_NONE, null);
Adam Cohen2801caf2011-05-13 20:57:39 -0700499 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700500 }
501 @Override
502 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700503 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700504 getContext().getString(R.string.folder_closed));
Adam Cohendf2cc412011-04-27 16:56:57 -0700505 mState = STATE_ANIMATING;
506 }
507 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700508 oa.setDuration(mExpandDuration);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700509 setLayerType(LAYER_TYPE_HARDWARE, null);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100510 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700511 }
512
Adam Cohencb3382b2011-05-24 14:07:08 -0700513 public boolean acceptDrop(DragObject d) {
514 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700515 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700516 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
517 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
518 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700519 }
520
Adam Cohendf2cc412011-04-27 16:56:57 -0700521 protected boolean findAndSetEmptyCells(ShortcutInfo item) {
522 int[] emptyCell = new int[2];
523 if (mContent.findCellForSpan(emptyCell, item.spanX, item.spanY)) {
524 item.cellX = emptyCell[0];
525 item.cellY = emptyCell[1];
Adam Cohendf2cc412011-04-27 16:56:57 -0700526 return true;
527 } else {
528 return false;
529 }
530 }
531
Adam Cohenc508b2d2011-06-28 14:41:44 -0700532 protected boolean createAndAddShortcut(ShortcutInfo item) {
Adam Cohen477828c2013-09-20 12:05:49 -0700533 final BubbleTextView textView =
534 (BubbleTextView) mInflater.inflate(R.layout.application, this, false);
Winson Chung54000492013-10-14 16:29:29 -0700535 textView.setCompoundDrawables(null,
Winson Chung0dbd7342013-10-13 22:46:20 -0700536 Utilities.createIconDrawable(item.getIcon(mIconCache)), null, null);
Adam Cohendf2cc412011-04-27 16:56:57 -0700537 textView.setText(item.title);
538 textView.setTag(item);
Adam Cohen477828c2013-09-20 12:05:49 -0700539 textView.setTextColor(getResources().getColor(R.color.folder_items_text_color));
540 textView.setShadowsEnabled(false);
Adam Cohen7397e622013-10-24 12:11:34 -0700541 textView.setGlowColor(getResources().getColor(R.color.folder_items_glow_color));
Adam Cohendf2cc412011-04-27 16:56:57 -0700542
543 textView.setOnClickListener(this);
544 textView.setOnLongClickListener(this);
545
Adam Cohenc508b2d2011-06-28 14:41:44 -0700546 // We need to check here to verify that the given item's location isn't already occupied
Adam Cohen3bf84d32012-05-07 20:17:14 -0700547 // by another item.
Adam Cohen0057bbc2011-08-12 18:30:51 -0700548 if (mContent.getChildAt(item.cellX, item.cellY) != null || item.cellX < 0 || item.cellY < 0
549 || item.cellX >= mContent.getCountX() || item.cellY >= mContent.getCountY()) {
Adam Cohen3bf84d32012-05-07 20:17:14 -0700550 // This shouldn't happen, log it.
551 Log.e(TAG, "Folder order not properly persisted during bind");
Adam Cohenc508b2d2011-06-28 14:41:44 -0700552 if (!findAndSetEmptyCells(item)) {
553 return false;
554 }
555 }
556
Adam Cohendf2cc412011-04-27 16:56:57 -0700557 CellLayout.LayoutParams lp =
558 new CellLayout.LayoutParams(item.cellX, item.cellY, item.spanX, item.spanY);
559 boolean insert = false;
Adam Cohenac56cff2011-09-28 20:45:37 -0700560 textView.setOnKeyListener(new FolderKeyEventListener());
Adam Cohendf2cc412011-04-27 16:56:57 -0700561 mContent.addViewToCellLayout(textView, insert ? 0 : -1, (int)item.id, lp, true);
Adam Cohenc508b2d2011-06-28 14:41:44 -0700562 return true;
Adam Cohendf2cc412011-04-27 16:56:57 -0700563 }
564
Adam Cohencb3382b2011-05-24 14:07:08 -0700565 public void onDragEnter(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700566 mPreviousTargetCell[0] = -1;
567 mPreviousTargetCell[1] = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700568 mOnExitAlarm.cancelAlarm();
569 }
570
571 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
572 public void onAlarm(Alarm alarm) {
573 realTimeReorder(mEmptyCell, mTargetCell);
574 }
575 };
576
577 boolean readingOrderGreaterThan(int[] v1, int[] v2) {
578 if (v1[1] > v2[1] || (v1[1] == v2[1] && v1[0] > v2[0])) {
579 return true;
580 } else {
581 return false;
582 }
583 }
584
585 private void realTimeReorder(int[] empty, int[] target) {
586 boolean wrap;
587 int startX;
588 int endX;
589 int startY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700590 int delay = 0;
591 float delayAmount = 30;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700592 if (readingOrderGreaterThan(target, empty)) {
593 wrap = empty[0] >= mContent.getCountX() - 1;
594 startY = wrap ? empty[1] + 1 : empty[1];
595 for (int y = startY; y <= target[1]; y++) {
596 startX = y == empty[1] ? empty[0] + 1 : 0;
597 endX = y < target[1] ? mContent.getCountX() - 1 : target[0];
598 for (int x = startX; x <= endX; x++) {
599 View v = mContent.getChildAt(x,y);
600 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen482ed822012-03-02 14:15:13 -0800601 REORDER_ANIMATION_DURATION, delay, true, true)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700602 empty[0] = x;
603 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700604 delay += delayAmount;
605 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700606 }
607 }
608 }
609 } else {
610 wrap = empty[0] == 0;
611 startY = wrap ? empty[1] - 1 : empty[1];
612 for (int y = startY; y >= target[1]; y--) {
613 startX = y == empty[1] ? empty[0] - 1 : mContent.getCountX() - 1;
614 endX = y > target[1] ? 0 : target[0];
615 for (int x = startX; x >= endX; x--) {
616 View v = mContent.getChildAt(x,y);
617 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen482ed822012-03-02 14:15:13 -0800618 REORDER_ANIMATION_DURATION, delay, true, true)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700619 empty[0] = x;
620 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700621 delay += delayAmount;
622 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700623 }
624 }
625 }
626 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700627 }
628
Adam Cohen2374abf2013-04-16 14:56:57 -0700629 public boolean isLayoutRtl() {
630 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
631 }
632
Adam Cohencb3382b2011-05-24 14:07:08 -0700633 public void onDragOver(DragObject d) {
Alan Viverette4cda5b72013-08-28 17:53:41 -0700634 final DragView dragView = d.dragView;
635 final int scrollOffset = mScrollView.getScrollY();
636 final float[] r = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, dragView, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700637 r[0] -= getPaddingLeft();
638 r[1] -= getPaddingTop();
639
Alan Viverette4cda5b72013-08-28 17:53:41 -0700640 final long downTime = SystemClock.uptimeMillis();
641 final MotionEvent translatedEv = MotionEvent.obtain(
642 downTime, downTime, MotionEvent.ACTION_MOVE, d.x, d.y, 0);
643
644 if (!mAutoScrollHelper.isEnabled()) {
645 mAutoScrollHelper.setEnabled(true);
646 }
647
648 final boolean handled = mAutoScrollHelper.onTouch(this, translatedEv);
649 translatedEv.recycle();
650
651 if (handled) {
652 mReorderAlarm.cancelAlarm();
653 } else {
654 mTargetCell = mContent.findNearestArea(
655 (int) r[0], (int) r[1] + scrollOffset, 1, 1, mTargetCell);
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700656 if (isLayoutRtl()) {
657 mTargetCell[0] = mContent.getCountX() - mTargetCell[0] - 1;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700658 }
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700659 if (mTargetCell[0] != mPreviousTargetCell[0]
660 || mTargetCell[1] != mPreviousTargetCell[1]) {
661 mReorderAlarm.cancelAlarm();
662 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
663 mReorderAlarm.setAlarm(REORDER_DELAY);
664 mPreviousTargetCell[0] = mTargetCell[0];
665 mPreviousTargetCell[1] = mTargetCell[1];
666 mDragMode = DRAG_MODE_REORDER;
667 } else {
668 mDragMode = DRAG_MODE_NONE;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700669 }
Adam Cohenbfbfd262011-06-13 16:55:12 -0700670 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700671 }
672
Adam Cohenbfbfd262011-06-13 16:55:12 -0700673 // This is used to compute the visual center of the dragView. The idea is that
674 // the visual center represents the user's interpretation of where the item is, and hence
675 // is the appropriate point to use when determining drop location.
676 private float[] getDragViewVisualCenter(int x, int y, int xOffset, int yOffset,
677 DragView dragView, float[] recycle) {
678 float res[];
679 if (recycle == null) {
680 res = new float[2];
681 } else {
682 res = recycle;
683 }
684
685 // These represent the visual top and left of drag view if a dragRect was provided.
686 // If a dragRect was not provided, then they correspond to the actual view left and
687 // top, as the dragRect is in that case taken to be the entire dragView.
688 // R.dimen.dragViewOffsetY.
689 int left = x - xOffset;
690 int top = y - yOffset;
691
692 // In order to find the visual center, we shift by half the dragRect
693 res[0] = left + dragView.getDragRegion().width() / 2;
694 res[1] = top + dragView.getDragRegion().height() / 2;
695
696 return res;
697 }
698
699 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
700 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700701 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700702 }
703 };
704
Adam Cohen95bb8002011-07-03 23:40:28 -0700705 public void completeDragExit() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700706 mLauncher.closeFolder();
707 mCurrentDragInfo = null;
708 mCurrentDragView = null;
709 mSuppressOnAdd = false;
710 mRearrangeOnClose = true;
711 }
712
Adam Cohencb3382b2011-05-24 14:07:08 -0700713 public void onDragExit(DragObject d) {
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700714 // Exiting folder; stop the auto scroller.
Alan Viverette4cda5b72013-08-28 17:53:41 -0700715 mAutoScrollHelper.setEnabled(false);
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700716 // We only close the folder if this is a true drag exit, ie. not because
717 // a drop has occurred above the folder.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700718 if (!d.dragComplete) {
719 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
720 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
721 }
722 mReorderAlarm.cancelAlarm();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700723 mDragMode = DRAG_MODE_NONE;
Adam Cohen2801caf2011-05-13 20:57:39 -0700724 }
725
Michael Jurka1e2f4652013-07-08 18:03:46 -0700726 public void onDropCompleted(final View target, final DragObject d,
727 final boolean isFlingToDelete, final boolean success) {
728 if (mDeferDropAfterUninstall) {
Michael Jurkaf3007582013-08-21 14:33:57 +0200729 Log.d(TAG, "Deferred handling drop because waiting for uninstall.");
Michael Jurka1e2f4652013-07-08 18:03:46 -0700730 mDeferredAction = new Runnable() {
731 public void run() {
732 onDropCompleted(target, d, isFlingToDelete, success);
733 mDeferredAction = null;
734 }
735 };
736 return;
737 }
738
739 boolean beingCalledAfterUninstall = mDeferredAction != null;
740 boolean successfulDrop =
741 success && (!beingCalledAfterUninstall || mUninstallSuccessful);
Winson Chung5f8afe62013-08-12 16:19:28 -0700742
Michael Jurka1e2f4652013-07-08 18:03:46 -0700743 if (successfulDrop) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700744 if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700745 replaceFolderWithFinalItem();
746 }
747 } else {
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700748 setupContentForNumItems(getItemCount());
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700749 // The drag failed, we need to return the item to the folder
750 mFolderIcon.onDrop(d);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700751 }
752
753 if (target != this) {
754 if (mOnExitAlarm.alarmPending()) {
755 mOnExitAlarm.cancelAlarm();
Michael Jurka54554252013-08-01 12:52:23 +0200756 if (!successfulDrop) {
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700757 mSuppressFolderDeletion = true;
758 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700759 completeDragExit();
760 }
Adam Cohenf0f2ffa2013-11-12 11:44:58 +0000761 } else {
762 mLauncher.getWorkspace().removeExtraEmptyScreen(true, null);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700763 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700764
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700765 mDeleteFolderOnDropCompleted = false;
766 mDragInProgress = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700767 mItemAddedBackToSelfViaIcon = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700768 mCurrentDragInfo = null;
769 mCurrentDragView = null;
770 mSuppressOnAdd = false;
Adam Cohen4045eb72011-10-06 11:44:26 -0700771
772 // Reordering may have occured, and we need to save the new item locations. We do this once
773 // at the end to prevent unnecessary database operations.
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700774 updateItemLocationsInDatabaseBatch();
Adam Cohen4045eb72011-10-06 11:44:26 -0700775 }
776
Michael Jurka1e2f4652013-07-08 18:03:46 -0700777 public void deferCompleteDropAfterUninstallActivity() {
778 mDeferDropAfterUninstall = true;
779 }
780
781 public void onUninstallActivityReturned(boolean success) {
782 mDeferDropAfterUninstall = false;
783 mUninstallSuccessful = success;
784 if (mDeferredAction != null) {
785 mDeferredAction.run();
786 }
787 }
788
Winson Chunga48487a2012-03-20 16:19:37 -0700789 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800790 public boolean supportsFlingToDelete() {
791 return true;
792 }
793
Winson Chunga48487a2012-03-20 16:19:37 -0700794 public void onFlingToDelete(DragObject d, int x, int y, PointF vec) {
795 // Do nothing
796 }
797
798 @Override
799 public void onFlingToDeleteCompleted() {
800 // Do nothing
801 }
802
Adam Cohen4045eb72011-10-06 11:44:26 -0700803 private void updateItemLocationsInDatabase() {
804 ArrayList<View> list = getItemsInReadingOrder();
805 for (int i = 0; i < list.size(); i++) {
806 View v = list.get(i);
807 ItemInfo info = (ItemInfo) v.getTag();
808 LauncherModel.moveItemInDatabase(mLauncher, info, mInfo.id, 0,
809 info.cellX, info.cellY);
810 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700811 }
Adam Cohen228da5a2011-07-27 22:23:47 -0700812
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700813 private void updateItemLocationsInDatabaseBatch() {
814 ArrayList<View> list = getItemsInReadingOrder();
815 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
816 for (int i = 0; i < list.size(); i++) {
817 View v = list.get(i);
818 ItemInfo info = (ItemInfo) v.getTag();
819 items.add(info);
820 }
821
822 LauncherModel.moveItemsInDatabase(mLauncher, items, mInfo.id, 0);
823 }
824
Adam Cohene25af792013-06-06 23:08:25 -0700825 public void addItemLocationsInDatabase() {
826 ArrayList<View> list = getItemsInReadingOrder();
827 for (int i = 0; i < list.size(); i++) {
828 View v = list.get(i);
829 ItemInfo info = (ItemInfo) v.getTag();
830 LauncherModel.addItemToDatabase(mLauncher, info, mInfo.id, 0,
831 info.cellX, info.cellY, false);
832 }
833 }
834
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700835 public void notifyDrop() {
836 if (mDragInProgress) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700837 mItemAddedBackToSelfViaIcon = true;
Adam Cohen76078c42011-06-09 15:06:52 -0700838 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700839 }
840
841 public boolean isDropEnabled() {
842 return true;
843 }
844
Adam Cohen4045eb72011-10-06 11:44:26 -0700845 private void setupContentDimensions(int count) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700846 ArrayList<View> list = getItemsInReadingOrder();
847
848 int countX = mContent.getCountX();
849 int countY = mContent.getCountY();
Adam Cohen7c693212011-05-18 15:26:57 -0700850 boolean done = false;
Adam Cohen2801caf2011-05-13 20:57:39 -0700851
Adam Cohen7c693212011-05-18 15:26:57 -0700852 while (!done) {
853 int oldCountX = countX;
854 int oldCountY = countY;
855 if (countX * countY < count) {
856 // Current grid is too small, expand it
Adam Cohen78dc83e2011-11-15 17:10:00 -0800857 if ((countX <= countY || countY == mMaxCountY) && countX < mMaxCountX) {
Adam Cohen7c693212011-05-18 15:26:57 -0700858 countX++;
859 } else if (countY < mMaxCountY) {
860 countY++;
861 }
862 if (countY == 0) countY++;
863 } else if ((countY - 1) * countX >= count && countY >= countX) {
864 countY = Math.max(0, countY - 1);
865 } else if ((countX - 1) * countY >= count) {
866 countX = Math.max(0, countX - 1);
Adam Cohen2801caf2011-05-13 20:57:39 -0700867 }
Adam Cohen7c693212011-05-18 15:26:57 -0700868 done = countX == oldCountX && countY == oldCountY;
Adam Cohen2801caf2011-05-13 20:57:39 -0700869 }
Adam Cohen7c693212011-05-18 15:26:57 -0700870 mContent.setGridSize(countX, countY);
Adam Cohen2801caf2011-05-13 20:57:39 -0700871 arrangeChildren(list);
872 }
873
874 public boolean isFull() {
Adam Cohen78dc83e2011-11-15 17:10:00 -0800875 return getItemCount() >= mMaxNumItems;
Adam Cohen2801caf2011-05-13 20:57:39 -0700876 }
877
878 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700879 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700880
Winson Chung892c74d2013-08-22 16:15:50 -0700881 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700882 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700883 int height = getFolderHeight();
Adam Cohen2801caf2011-05-13 20:57:39 -0700884
Adam Cohen307fe232012-08-16 17:55:58 -0700885 float scale = parent.getDescendantRectRelativeToSelf(mFolderIcon, mTempRect);
Adam Cohen8e776a62011-06-28 18:10:06 -0700886
Winson Chungaf40f202013-09-18 18:26:31 -0700887 LauncherAppState app = LauncherAppState.getInstance();
888 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
889
Adam Cohen307fe232012-08-16 17:55:58 -0700890 int centerX = (int) (mTempRect.left + mTempRect.width() * scale / 2);
891 int centerY = (int) (mTempRect.top + mTempRect.height() * scale / 2);
Adam Cohen2801caf2011-05-13 20:57:39 -0700892 int centeredLeft = centerX - width / 2;
893 int centeredTop = centerY - height / 2;
Winson Chung3057b1c2013-10-10 17:35:35 -0700894 int currentPage = mLauncher.getWorkspace().getNextPage();
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800895 // In case the workspace is scrolling, we need to use the final scroll to compute
896 // the folders bounds.
897 mLauncher.getWorkspace().setFinalScrollForPageChange(currentPage);
Adam Cohen35e7e642011-07-17 14:47:18 -0700898 // We first fetch the currently visible CellLayoutChildren
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800899 CellLayout currentLayout = (CellLayout) mLauncher.getWorkspace().getChildAt(currentPage);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700900 ShortcutAndWidgetContainer boundingLayout = currentLayout.getShortcutsAndWidgets();
Adam Cohen35e7e642011-07-17 14:47:18 -0700901 Rect bounds = new Rect();
902 parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800903 // We reset the workspaces scroll
904 mLauncher.getWorkspace().resetFinalScrollForPageChange(currentPage);
Adam Cohen2801caf2011-05-13 20:57:39 -0700905
Adam Cohen35e7e642011-07-17 14:47:18 -0700906 // We need to bound the folder to the currently visible CellLayoutChildren
907 int left = Math.min(Math.max(bounds.left, centeredLeft),
908 bounds.left + bounds.width() - width);
909 int top = Math.min(Math.max(bounds.top, centeredTop),
910 bounds.top + bounds.height() - height);
Winson Chungaf40f202013-09-18 18:26:31 -0700911 if (grid.isPhone() && (grid.availableWidthPx - width) < grid.iconSizePx) {
912 // Center the folder if it is full (on phones only)
913 left = (grid.availableWidthPx - width) / 2;
914 } else if (width >= bounds.width()) {
915 // If the folder doesn't fit within the bounds, center it about the desired bounds
Adam Cohen35e7e642011-07-17 14:47:18 -0700916 left = bounds.left + (bounds.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700917 }
Adam Cohen35e7e642011-07-17 14:47:18 -0700918 if (height >= bounds.height()) {
919 top = bounds.top + (bounds.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700920 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700921
922 int folderPivotX = width / 2 + (centeredLeft - left);
923 int folderPivotY = height / 2 + (centeredTop - top);
924 setPivotX(folderPivotX);
925 setPivotY(folderPivotY);
Adam Cohen268c4752012-06-06 17:47:33 -0700926 mFolderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700927 (1.0f * folderPivotX / width));
Adam Cohen268c4752012-06-06 17:47:33 -0700928 mFolderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700929 (1.0f * folderPivotY / height));
Adam Cohen3bf84d32012-05-07 20:17:14 -0700930
Adam Cohen662b5982011-12-13 17:45:21 -0800931 lp.width = width;
932 lp.height = height;
933 lp.x = left;
934 lp.y = top;
Adam Cohen2801caf2011-05-13 20:57:39 -0700935 }
936
Adam Cohen268c4752012-06-06 17:47:33 -0700937 float getPivotXForIconAnimation() {
938 return mFolderIconPivotX;
939 }
940 float getPivotYForIconAnimation() {
941 return mFolderIconPivotY;
942 }
943
Adam Cohen2801caf2011-05-13 20:57:39 -0700944 private void setupContentForNumItems(int count) {
Adam Cohen4045eb72011-10-06 11:44:26 -0700945 setupContentDimensions(count);
Adam Cohen2801caf2011-05-13 20:57:39 -0700946
Adam Cohen8e776a62011-06-28 18:10:06 -0700947 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700948 if (lp == null) {
Adam Cohen8e776a62011-06-28 18:10:06 -0700949 lp = new DragLayer.LayoutParams(0, 0);
950 lp.customPosition = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700951 setLayoutParams(lp);
952 }
953 centerAboutIcon();
954 }
955
Winson Chung892c74d2013-08-22 16:15:50 -0700956 private int getContentAreaHeight() {
957 LauncherAppState app = LauncherAppState.getInstance();
958 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
959 Rect workspacePadding = grid.getWorkspacePadding(grid.isLandscape ?
960 CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
961 int maxContentAreaHeight = grid.availableHeightPx -
Winson Chung892c74d2013-08-22 16:15:50 -0700962 workspacePadding.top - workspacePadding.bottom -
Winson Chung892c74d2013-08-22 16:15:50 -0700963 mFolderNameHeight;
964 return Math.min(maxContentAreaHeight,
965 mContent.getDesiredHeight());
966 }
967
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700968 private int getFolderHeight() {
Winson Chung892c74d2013-08-22 16:15:50 -0700969 int height = getPaddingTop() + getPaddingBottom()
970 + getContentAreaHeight() + mFolderNameHeight;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700971 return height;
972 }
Adam Cohen234c4cd2011-07-17 21:03:04 -0700973
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700974 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700975 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
976 int height = getFolderHeight();
977 int contentAreaWidthSpec = MeasureSpec.makeMeasureSpec(mContent.getDesiredWidth(),
978 MeasureSpec.EXACTLY);
Winson Chung892c74d2013-08-22 16:15:50 -0700979 int contentAreaHeightSpec = MeasureSpec.makeMeasureSpec(getContentAreaHeight(),
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700980 MeasureSpec.EXACTLY);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700981 mContent.setFixedSize(mContent.getDesiredWidth(), mContent.getDesiredHeight());
982 mScrollView.measure(contentAreaWidthSpec, contentAreaHeightSpec);
983 mFolderName.measure(contentAreaWidthSpec,
Adam Cohen234c4cd2011-07-17 21:03:04 -0700984 MeasureSpec.makeMeasureSpec(mFolderNameHeight, MeasureSpec.EXACTLY));
985 setMeasuredDimension(width, height);
986 }
987
Adam Cohen2801caf2011-05-13 20:57:39 -0700988 private void arrangeChildren(ArrayList<View> list) {
989 int[] vacant = new int[2];
990 if (list == null) {
991 list = getItemsInReadingOrder();
992 }
993 mContent.removeAllViews();
994
995 for (int i = 0; i < list.size(); i++) {
996 View v = list.get(i);
997 mContent.getVacantCell(vacant, 1, 1);
998 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) v.getLayoutParams();
999 lp.cellX = vacant[0];
1000 lp.cellY = vacant[1];
1001 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohen2792a332011-09-26 21:09:47 -07001002 if (info.cellX != vacant[0] || info.cellY != vacant[1]) {
1003 info.cellX = vacant[0];
1004 info.cellY = vacant[1];
1005 LauncherModel.addOrMoveItemInDatabase(mLauncher, info, mInfo.id, 0,
1006 info.cellX, info.cellY);
1007 }
Adam Cohen2801caf2011-05-13 20:57:39 -07001008 boolean insert = false;
1009 mContent.addViewToCellLayout(v, insert ? 0 : -1, (int)info.id, lp, true);
1010 }
Adam Cohen7c693212011-05-18 15:26:57 -07001011 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -07001012 }
1013
Adam Cohena9cf38f2011-05-02 15:36:58 -07001014 public int getItemCount() {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001015 return mContent.getShortcutsAndWidgets().getChildCount();
Adam Cohena9cf38f2011-05-02 15:36:58 -07001016 }
1017
1018 public View getItemAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001019 return mContent.getShortcutsAndWidgets().getChildAt(index);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001020 }
1021
Adam Cohen2801caf2011-05-13 20:57:39 -07001022 private void onCloseComplete() {
Adam Cohen05e0f402011-08-01 12:12:49 -07001023 DragLayer parent = (DragLayer) getParent();
Michael Jurka5649c282012-06-18 10:33:21 -07001024 if (parent != null) {
1025 parent.removeView(this);
1026 }
Adam Cohen4554ee12011-08-03 16:13:21 -07001027 mDragController.removeDropTarget((DropTarget) this);
Adam Cohen05e0f402011-08-01 12:12:49 -07001028 clearFocus();
Adam Cohenac56cff2011-09-28 20:45:37 -07001029 mFolderIcon.requestFocus();
Adam Cohen05e0f402011-08-01 12:12:49 -07001030
Adam Cohen2801caf2011-05-13 20:57:39 -07001031 if (mRearrangeOnClose) {
1032 setupContentForNumItems(getItemCount());
1033 mRearrangeOnClose = false;
1034 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001035 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001036 if (!mDragInProgress && !mSuppressFolderDeletion) {
1037 replaceFolderWithFinalItem();
1038 } else if (mDragInProgress) {
1039 mDeleteFolderOnDropCompleted = true;
1040 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001041 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001042 mSuppressFolderDeletion = false;
Adam Cohenafb01ee2011-06-23 15:38:03 -07001043 }
1044
1045 private void replaceFolderWithFinalItem() {
Adam Cohenafb01ee2011-06-23 15:38:03 -07001046 // Add the last remaining child to the workspace in place of the folder
Adam Cohenfb91f302012-06-11 15:45:18 -07001047 Runnable onCompleteRunnable = new Runnable() {
1048 @Override
1049 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -07001050 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screenId);
Adam Cohenafb01ee2011-06-23 15:38:03 -07001051
Adam Cohenc5e63f32012-07-12 16:16:57 -07001052 View child = null;
Adam Cohenfb91f302012-06-11 15:45:18 -07001053 // Move the item from the folder to the workspace, in the position of the folder
1054 if (getItemCount() == 1) {
1055 ShortcutInfo finalItem = mInfo.contents.get(0);
Adam Cohenc5e63f32012-07-12 16:16:57 -07001056 child = mLauncher.createShortcut(R.layout.application, cellLayout,
Adam Cohenfb91f302012-06-11 15:45:18 -07001057 finalItem);
1058 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -07001059 mInfo.screenId, mInfo.cellX, mInfo.cellY);
Adam Cohenfb91f302012-06-11 15:45:18 -07001060 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001061 if (getItemCount() <= 1) {
1062 // Remove the folder
1063 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
1064 cellLayout.removeView(mFolderIcon);
1065 if (mFolderIcon instanceof DropTarget) {
1066 mDragController.removeDropTarget((DropTarget) mFolderIcon);
1067 }
1068 mLauncher.removeFolder(mInfo);
1069 }
Adam Cohenc5e63f32012-07-12 16:16:57 -07001070 // We add the child after removing the folder to prevent both from existing at
Winson Chung0e6a7132013-08-23 12:55:10 -07001071 // the same time in the CellLayout. We need to add the new item with addInScreenFromBind()
1072 // to ensure that hotseat items are placed correctly.
Adam Cohenc5e63f32012-07-12 16:16:57 -07001073 if (child != null) {
Winson Chung0e6a7132013-08-23 12:55:10 -07001074 mLauncher.getWorkspace().addInScreenFromBind(child, mInfo.container, mInfo.screenId,
Adam Cohenc5e63f32012-07-12 16:16:57 -07001075 mInfo.cellX, mInfo.cellY, mInfo.spanX, mInfo.spanY);
1076 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001077 }
1078 };
1079 View finalChild = getItemAt(0);
1080 if (finalChild != null) {
1081 mFolderIcon.performDestroyAnimation(finalChild, onCompleteRunnable);
Adam Cohenafb01ee2011-06-23 15:38:03 -07001082 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001083 mDestroyed = true;
1084 }
1085
1086 boolean isDestroyed() {
1087 return mDestroyed;
Adam Cohen2801caf2011-05-13 20:57:39 -07001088 }
1089
Adam Cohenac56cff2011-09-28 20:45:37 -07001090 // This method keeps track of the last item in the folder for the purposes
1091 // of keyboard focus
1092 private void updateTextViewFocus() {
1093 View lastChild = getItemAt(getItemCount() - 1);
1094 getItemAt(getItemCount() - 1);
1095 if (lastChild != null) {
1096 mFolderName.setNextFocusDownId(lastChild.getId());
1097 mFolderName.setNextFocusRightId(lastChild.getId());
1098 mFolderName.setNextFocusLeftId(lastChild.getId());
1099 mFolderName.setNextFocusUpId(lastChild.getId());
1100 }
1101 }
1102
Adam Cohenbfbfd262011-06-13 16:55:12 -07001103 public void onDrop(DragObject d) {
1104 ShortcutInfo item;
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001105 if (d.dragInfo instanceof AppInfo) {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001106 // Came from all apps -- make a copy
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001107 item = ((AppInfo) d.dragInfo).makeShortcut();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001108 item.spanX = 1;
1109 item.spanY = 1;
1110 } else {
1111 item = (ShortcutInfo) d.dragInfo;
1112 }
Adam Cohen05e0f402011-08-01 12:12:49 -07001113 // Dragged from self onto self, currently this is the only path possible, however
1114 // we keep this as a distinct code path.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001115 if (item == mCurrentDragInfo) {
1116 ShortcutInfo si = (ShortcutInfo) mCurrentDragView.getTag();
1117 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) mCurrentDragView.getLayoutParams();
1118 si.cellX = lp.cellX = mEmptyCell[0];
1119 si.cellX = lp.cellY = mEmptyCell[1];
1120 mContent.addViewToCellLayout(mCurrentDragView, -1, (int)item.id, lp, true);
Adam Cohenfc53cd22011-07-20 15:45:11 -07001121 if (d.dragView.hasDrawn()) {
1122 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, mCurrentDragView);
1123 } else {
Winson Chung2c4cf412012-05-08 14:03:21 -07001124 d.deferDragViewCleanupPostAnimation = false;
Adam Cohenfc53cd22011-07-20 15:45:11 -07001125 mCurrentDragView.setVisibility(VISIBLE);
1126 }
Adam Cohene9166b22011-07-08 17:11:11 -07001127 mItemsInvalidated = true;
Adam Cohen4045eb72011-10-06 11:44:26 -07001128 setupContentDimensions(getItemCount());
Adam Cohen716b51e2011-06-30 12:09:54 -07001129 mSuppressOnAdd = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001130 }
1131 mInfo.add(item);
1132 }
1133
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001134 // This is used so the item doesn't immediately appear in the folder when added. In one case
1135 // we need to create the illusion that the item isn't added back to the folder yet, to
1136 // to correspond to the animation of the icon back into the folder. This is
1137 public void hideItem(ShortcutInfo info) {
1138 View v = getViewForInfo(info);
1139 v.setVisibility(INVISIBLE);
1140 }
1141 public void showItem(ShortcutInfo info) {
1142 View v = getViewForInfo(info);
1143 v.setVisibility(VISIBLE);
1144 }
1145
Adam Cohenbfbfd262011-06-13 16:55:12 -07001146 public void onAdd(ShortcutInfo item) {
1147 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001148 // If the item was dropped onto this open folder, we have done the work associated
1149 // with adding the item to the folder, as indicated by mSuppressOnAdd being set
Adam Cohenbfbfd262011-06-13 16:55:12 -07001150 if (mSuppressOnAdd) return;
1151 if (!findAndSetEmptyCells(item)) {
1152 // The current layout is full, can we expand it?
1153 setupContentForNumItems(getItemCount() + 1);
1154 findAndSetEmptyCells(item);
1155 }
1156 createAndAddShortcut(item);
1157 LauncherModel.addOrMoveItemInDatabase(
1158 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
1159 }
1160
Adam Cohena9cf38f2011-05-02 15:36:58 -07001161 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -07001162 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001163 // If this item is being dragged from this open folder, we have already handled
1164 // the work associated with removing the item, so we don't have to do anything here.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001165 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001166 View v = getViewForInfo(item);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001167 mContent.removeView(v);
Adam Cohen2801caf2011-05-13 20:57:39 -07001168 if (mState == STATE_ANIMATING) {
1169 mRearrangeOnClose = true;
1170 } else {
1171 setupContentForNumItems(getItemCount());
1172 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001173 if (getItemCount() <= 1) {
1174 replaceFolderWithFinalItem();
1175 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001176 }
Adam Cohen7c693212011-05-18 15:26:57 -07001177
Adam Cohendf1e4e82011-06-24 15:57:39 -07001178 private View getViewForInfo(ShortcutInfo item) {
1179 for (int j = 0; j < mContent.getCountY(); j++) {
1180 for (int i = 0; i < mContent.getCountX(); i++) {
1181 View v = mContent.getChildAt(i, j);
1182 if (v.getTag() == item) {
1183 return v;
1184 }
1185 }
1186 }
1187 return null;
1188 }
1189
Adam Cohen76078c42011-06-09 15:06:52 -07001190 public void onItemsChanged() {
Adam Cohenac56cff2011-09-28 20:45:37 -07001191 updateTextViewFocus();
Adam Cohen76078c42011-06-09 15:06:52 -07001192 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001193
Adam Cohen76fc0852011-06-17 13:26:23 -07001194 public void onTitleChanged(CharSequence title) {
1195 }
Adam Cohen76078c42011-06-09 15:06:52 -07001196
Adam Cohen7c693212011-05-18 15:26:57 -07001197 public ArrayList<View> getItemsInReadingOrder() {
1198 if (mItemsInvalidated) {
1199 mItemsInReadingOrder.clear();
1200 for (int j = 0; j < mContent.getCountY(); j++) {
1201 for (int i = 0; i < mContent.getCountX(); i++) {
1202 View v = mContent.getChildAt(i, j);
1203 if (v != null) {
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001204 mItemsInReadingOrder.add(v);
Adam Cohen7c693212011-05-18 15:26:57 -07001205 }
1206 }
1207 }
1208 mItemsInvalidated = false;
1209 }
1210 return mItemsInReadingOrder;
1211 }
Adam Cohen8dfcba42011-07-07 16:38:18 -07001212
1213 public void getLocationInDragLayer(int[] loc) {
1214 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
1215 }
Adam Cohenea0818d2011-09-30 20:06:58 -07001216
1217 public void onFocusChange(View v, boolean hasFocus) {
1218 if (v == mFolderName && hasFocus) {
1219 startEditingFolderName();
1220 }
1221 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001222
1223 @Override
1224 public void getHitRectRelativeToDragLayer(Rect outRect) {
1225 getHitRect(outRect);
1226 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001227}