blob: f292821f48156e60d37d7095f9c1b30080550b39 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Adam Cohendf2cc412011-04-27 16:56:57 -070019import android.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
Adam Cohendf2cc412011-04-27 16:56:57 -070021import android.animation.ObjectAnimator;
22import android.animation.PropertyValuesHolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080023import android.content.Context;
Adam Cohen76fc0852011-06-17 13:26:23 -070024import android.content.res.Resources;
Winson Chung043f2af2012-03-01 16:09:54 -080025import android.graphics.PointF;
Romain Guyfb5411e2010-02-24 10:04:17 -080026import android.graphics.Rect;
Adam Cohenbadf71e2011-05-26 19:08:29 -070027import android.graphics.drawable.Drawable;
Alan Viverette4cda5b72013-08-28 17:53:41 -070028import android.os.SystemClock;
Bjorn Bringertb66773c2013-09-25 16:12:24 +010029import android.support.v4.widget.AutoScrollHelper;
Adam Cohen7a14d0b2011-06-24 11:36:35 -070030import android.text.InputType;
Adam Cohene601a432011-07-26 21:51:30 -070031import android.text.Selection;
32import android.text.Spannable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.util.AttributeSet;
Adam Cohen3bf84d32012-05-07 20:17:14 -070034import android.util.Log;
Adam Cohen76fc0852011-06-17 13:26:23 -070035import android.view.ActionMode;
36import android.view.KeyEvent;
Adam Cohendf2cc412011-04-27 16:56:57 -070037import android.view.LayoutInflater;
Adam Cohen76fc0852011-06-17 13:26:23 -070038import android.view.Menu;
39import android.view.MenuItem;
Adam Cohen0c872ba2011-05-05 10:34:16 -070040import android.view.MotionEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.view.View;
Adam Cohen3371da02011-10-25 21:38:29 -070042import android.view.accessibility.AccessibilityEvent;
43import android.view.accessibility.AccessibilityManager;
Adam Cohen76fc0852011-06-17 13:26:23 -070044import android.view.inputmethod.EditorInfo;
45import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.widget.LinearLayout;
Adam Cohenf0f4eda2013-06-06 21:27:03 -070047import android.widget.ScrollView;
Adam Cohendf2cc412011-04-27 16:56:57 -070048import android.widget.TextView;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049
Daniel Sandler325dc232013-06-05 22:57:57 -040050import com.android.launcher3.FolderInfo.FolderListener;
Romain Guyedcce092010-03-04 13:03:17 -080051
Adam Cohenc0dcf592011-06-01 15:30:43 -070052import java.util.ArrayList;
Adam Cohen3bf84d32012-05-07 20:17:14 -070053import java.util.Collections;
54import java.util.Comparator;
Adam Cohenc0dcf592011-06-01 15:30:43 -070055
The Android Open Source Project31dd5032009-03-03 19:32:27 -080056/**
57 * Represents a set of icons chosen by the user or generated by the system.
58 */
Adam Cohen8dfcba42011-07-07 16:38:18 -070059public class Folder extends LinearLayout implements DragSource, View.OnClickListener,
Adam Cohenea0818d2011-09-30 20:06:58 -070060 View.OnLongClickListener, DropTarget, FolderListener, TextView.OnEditorActionListener,
61 View.OnFocusChangeListener {
Adam Cohendf2cc412011-04-27 16:56:57 -070062 private static final String TAG = "Launcher.Folder";
63
Adam Cohen4eac29a2011-07-11 17:53:37 -070064 protected DragController mDragController;
65 protected Launcher mLauncher;
66 protected FolderInfo mInfo;
67
Adam Cohendf2cc412011-04-27 16:56:57 -070068 static final int STATE_NONE = -1;
69 static final int STATE_SMALL = 0;
70 static final int STATE_ANIMATING = 1;
71 static final int STATE_OPEN = 2;
72
73 private int mExpandDuration;
74 protected CellLayout mContent;
Adam Cohenf0f4eda2013-06-06 21:27:03 -070075 private ScrollView mScrollView;
Adam Cohendf2cc412011-04-27 16:56:57 -070076 private final LayoutInflater mInflater;
77 private final IconCache mIconCache;
78 private int mState = STATE_NONE;
Adam Cohenbfbfd262011-06-13 16:55:12 -070079 private static final int REORDER_ANIMATION_DURATION = 230;
Adam Cohenf0f4eda2013-06-06 21:27:03 -070080 private static final int REORDER_DELAY = 250;
Adam Cohen5d518fa2013-12-05 14:16:23 -080081 private static final int ON_EXIT_CLOSE_DELAY = 400;
Adam Cohen2801caf2011-05-13 20:57:39 -070082 private boolean mRearrangeOnClose = false;
83 private FolderIcon mFolderIcon;
84 private int mMaxCountX;
85 private int mMaxCountY;
Adam Cohen78dc83e2011-11-15 17:10:00 -080086 private int mMaxNumItems;
Adam Cohen7c693212011-05-18 15:26:57 -070087 private ArrayList<View> mItemsInReadingOrder = new ArrayList<View>();
Adam Cohenbadf71e2011-05-26 19:08:29 -070088 private Drawable mIconDrawable;
Adam Cohen7c693212011-05-18 15:26:57 -070089 boolean mItemsInvalidated = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -070090 private ShortcutInfo mCurrentDragInfo;
91 private View mCurrentDragView;
92 boolean mSuppressOnAdd = false;
93 private int[] mTargetCell = new int[2];
94 private int[] mPreviousTargetCell = new int[2];
95 private int[] mEmptyCell = new int[2];
96 private Alarm mReorderAlarm = new Alarm();
97 private Alarm mOnExitAlarm = new Alarm();
Adam Cohen76fc0852011-06-17 13:26:23 -070098 private int mFolderNameHeight;
Adam Cohen8e776a62011-06-28 18:10:06 -070099 private Rect mTempRect = new Rect();
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700100 private boolean mDragInProgress = false;
101 private boolean mDeleteFolderOnDropCompleted = false;
102 private boolean mSuppressFolderDeletion = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700103 private boolean mItemAddedBackToSelfViaIcon = false;
Adam Cohenac56cff2011-09-28 20:45:37 -0700104 FolderEditText mFolderName;
Adam Cohen268c4752012-06-06 17:47:33 -0700105 private float mFolderIconPivotX;
106 private float mFolderIconPivotY;
Adam Cohen228da5a2011-07-27 22:23:47 -0700107
Adam Cohen76fc0852011-06-17 13:26:23 -0700108 private boolean mIsEditingName = false;
109 private InputMethodManager mInputMethodManager;
Adam Cohendf2cc412011-04-27 16:56:57 -0700110
Adam Cohena65beee2011-06-27 21:32:23 -0700111 private static String sDefaultFolderName;
112 private static String sHintText;
113
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700114 private int DRAG_MODE_NONE = 0;
115 private int DRAG_MODE_REORDER = 1;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700116 private int mDragMode = DRAG_MODE_NONE;
117
Adam Cohen1960ea42013-11-12 11:33:14 +0000118 // We avoid measuring the scroll view with a 0 width or height, as this
119 // results in CellLayout being measured as UNSPECIFIED, which it does
120 // not support.
121 private static final int MIN_CONTENT_DIMEN = 5;
122
Adam Cohenfb91f302012-06-11 15:45:18 -0700123 private boolean mDestroyed;
124
Alan Viverette4cda5b72013-08-28 17:53:41 -0700125 private AutoScrollHelper mAutoScrollHelper;
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700126
Michael Jurka1e2f4652013-07-08 18:03:46 -0700127 private Runnable mDeferredAction;
128 private boolean mDeferDropAfterUninstall;
129 private boolean mUninstallSuccessful;
130
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131 /**
132 * Used to inflate the Workspace from XML.
133 *
134 * @param context The application's context.
135 * @param attrs The attribtues set containing the Workspace's customization values.
136 */
137 public Folder(Context context, AttributeSet attrs) {
138 super(context, attrs);
Winson Chung5f8afe62013-08-12 16:19:28 -0700139
140 LauncherAppState app = LauncherAppState.getInstance();
141 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800142 setAlwaysDrawnWithCacheEnabled(false);
Adam Cohendf2cc412011-04-27 16:56:57 -0700143 mInflater = LayoutInflater.from(context);
Winson Chung5f8afe62013-08-12 16:19:28 -0700144 mIconCache = app.getIconCache();
Adam Cohen78dc83e2011-11-15 17:10:00 -0800145
146 Resources res = getResources();
Michael Jurka414300a2013-08-27 15:42:35 +0200147 mMaxCountX = (int) grid.numColumns;
Adam Cohen477828c2013-09-20 12:05:49 -0700148 mMaxCountY = (int) grid.numRows;
149 mMaxNumItems = mMaxCountX * mMaxCountY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700150
151 mInputMethodManager = (InputMethodManager)
Michael Jurka8b805b12012-04-18 14:23:14 -0700152 getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
Adam Cohen76fc0852011-06-17 13:26:23 -0700153
Adam Cohen76fc0852011-06-17 13:26:23 -0700154 mExpandDuration = res.getInteger(R.integer.config_folderAnimDuration);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700155
156 if (sDefaultFolderName == null) {
157 sDefaultFolderName = res.getString(R.string.folder_name);
158 }
Adam Cohena65beee2011-06-27 21:32:23 -0700159 if (sHintText == null) {
160 sHintText = res.getString(R.string.folder_hint_text);
161 }
Adam Cohen4eac29a2011-07-11 17:53:37 -0700162 mLauncher = (Launcher) context;
Adam Cohenac56cff2011-09-28 20:45:37 -0700163 // We need this view to be focusable in touch mode so that when text editing of the folder
164 // name is complete, we have something to focus on, thus hiding the cursor and giving
165 // reliable behvior when clicking the text field (since it will always gain focus on click).
166 setFocusableInTouchMode(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167 }
168
169 @Override
170 protected void onFinishInflate() {
171 super.onFinishInflate();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700172 mScrollView = (ScrollView) findViewById(R.id.scroll_view);
Adam Cohendf2cc412011-04-27 16:56:57 -0700173 mContent = (CellLayout) findViewById(R.id.folder_content);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700174
Winson Chung5f8afe62013-08-12 16:19:28 -0700175 LauncherAppState app = LauncherAppState.getInstance();
176 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
177
Winson Chung5f8afe62013-08-12 16:19:28 -0700178 mContent.setCellDimensions(grid.folderCellWidthPx, grid.folderCellHeightPx);
Adam Cohen2801caf2011-05-13 20:57:39 -0700179 mContent.setGridSize(0, 0);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700180 mContent.getShortcutsAndWidgets().setMotionEventSplittingEnabled(false);
Adam Cohen2374abf2013-04-16 14:56:57 -0700181 mContent.setInvertIfRtl(true);
Adam Cohenac56cff2011-09-28 20:45:37 -0700182 mFolderName = (FolderEditText) findViewById(R.id.folder_name);
183 mFolderName.setFolder(this);
Adam Cohenea0818d2011-09-30 20:06:58 -0700184 mFolderName.setOnFocusChangeListener(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700185
186 // We find out how tall the text view wants to be (it is set to wrap_content), so that
187 // we can allocate the appropriate amount of space for it.
188 int measureSpec = MeasureSpec.UNSPECIFIED;
189 mFolderName.measure(measureSpec, measureSpec);
190 mFolderNameHeight = mFolderName.getMeasuredHeight();
191
192 // We disable action mode for now since it messes up the view on phones
193 mFolderName.setCustomSelectionActionModeCallback(mActionModeCallback);
Adam Cohen76fc0852011-06-17 13:26:23 -0700194 mFolderName.setOnEditorActionListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700195 mFolderName.setSelectAllOnFocus(true);
Adam Cohen7a14d0b2011-06-24 11:36:35 -0700196 mFolderName.setInputType(mFolderName.getInputType() |
197 InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
Alan Viverette4cda5b72013-08-28 17:53:41 -0700198 mAutoScrollHelper = new FolderAutoScrollHelper(mScrollView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800199 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700200
Adam Cohen76fc0852011-06-17 13:26:23 -0700201 private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
202 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
203 return false;
204 }
205
206 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
207 return false;
208 }
209
210 public void onDestroyActionMode(ActionMode mode) {
211 }
212
213 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
214 return false;
215 }
216 };
217
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800218 public void onClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700219 Object tag = v.getTag();
220 if (tag instanceof ShortcutInfo) {
Adam Cohenb5fe60c2013-06-06 22:03:51 -0700221 mLauncher.onClick(v);
Adam Cohendf2cc412011-04-27 16:56:57 -0700222 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800223 }
224
225 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -0700226 // Return if global dragging is not enabled
227 if (!mLauncher.isDraggingEnabled()) return true;
228
Adam Cohendf2cc412011-04-27 16:56:57 -0700229 Object tag = v.getTag();
230 if (tag instanceof ShortcutInfo) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700231 ShortcutInfo item = (ShortcutInfo) tag;
232 if (!v.isInTouchMode()) {
233 return false;
234 }
235
Winson Chung7d7541e2011-09-16 20:14:36 -0700236 mLauncher.dismissFolderCling(null);
237
Adam Cohendf2cc412011-04-27 16:56:57 -0700238 mLauncher.getWorkspace().onDragStartedWithItem(v);
Adam Cohenac8c8762011-07-13 11:15:27 -0700239 mLauncher.getWorkspace().beginDragShared(v, this);
Adam Cohenbadf71e2011-05-26 19:08:29 -0700240 mIconDrawable = ((TextView) v).getCompoundDrawables()[1];
Adam Cohen76078c42011-06-09 15:06:52 -0700241
242 mCurrentDragInfo = item;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700243 mEmptyCell[0] = item.cellX;
244 mEmptyCell[1] = item.cellY;
245 mCurrentDragView = v;
Adam Cohenfc53cd22011-07-20 15:45:11 -0700246
247 mContent.removeView(mCurrentDragView);
248 mInfo.remove(mCurrentDragInfo);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700249 mDragInProgress = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700250 mItemAddedBackToSelfViaIcon = false;
Adam Cohendf2cc412011-04-27 16:56:57 -0700251 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800252 return true;
253 }
254
Adam Cohen76fc0852011-06-17 13:26:23 -0700255 public boolean isEditingName() {
256 return mIsEditingName;
257 }
258
259 public void startEditingFolderName() {
Adam Cohena65beee2011-06-27 21:32:23 -0700260 mFolderName.setHint("");
Adam Cohen76fc0852011-06-17 13:26:23 -0700261 mIsEditingName = true;
262 }
263
264 public void dismissEditingName() {
265 mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
266 doneEditingFolderName(true);
267 }
268
269 public void doneEditingFolderName(boolean commit) {
Adam Cohena65beee2011-06-27 21:32:23 -0700270 mFolderName.setHint(sHintText);
Adam Cohen1df26a32011-08-12 16:15:23 -0700271 // Convert to a string here to ensure that no other state associated with the text field
272 // gets saved.
Adam Cohen3371da02011-10-25 21:38:29 -0700273 String newTitle = mFolderName.getText().toString();
274 mInfo.setTitle(newTitle);
Adam Cohen76fc0852011-06-17 13:26:23 -0700275 LauncherModel.updateItemInDatabase(mLauncher, mInfo);
Adam Cohenac56cff2011-09-28 20:45:37 -0700276
Adam Cohen3371da02011-10-25 21:38:29 -0700277 if (commit) {
278 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700279 String.format(getContext().getString(R.string.folder_renamed), newTitle));
Adam Cohen3371da02011-10-25 21:38:29 -0700280 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700281 // In order to clear the focus from the text field, we set the focus on ourself. This
282 // ensures that every time the field is clicked, focus is gained, giving reliable behavior.
283 requestFocus();
284
Adam Cohene601a432011-07-26 21:51:30 -0700285 Selection.setSelection((Spannable) mFolderName.getText(), 0, 0);
Adam Cohen76fc0852011-06-17 13:26:23 -0700286 mIsEditingName = false;
287 }
288
289 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
290 if (actionId == EditorInfo.IME_ACTION_DONE) {
291 dismissEditingName();
292 return true;
293 }
294 return false;
295 }
296
297 public View getEditTextRegion() {
298 return mFolderName;
299 }
300
Adam Cohenbadf71e2011-05-26 19:08:29 -0700301 public Drawable getDragDrawable() {
302 return mIconDrawable;
303 }
304
Adam Cohen0c872ba2011-05-05 10:34:16 -0700305 /**
306 * We need to handle touch events to prevent them from falling through to the workspace below.
307 */
308 @Override
309 public boolean onTouchEvent(MotionEvent ev) {
310 return true;
311 }
312
Joe Onorato00acb122009-08-04 16:04:30 -0400313 public void setDragController(DragController dragController) {
314 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800315 }
316
Adam Cohen2801caf2011-05-13 20:57:39 -0700317 void setFolderIcon(FolderIcon icon) {
318 mFolderIcon = icon;
319 }
320
Adam Cohen3371da02011-10-25 21:38:29 -0700321 @Override
322 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
323 // When the folder gets focus, we don't want to announce the list of items.
324 return true;
325 }
326
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800327 /**
328 * @return the FolderInfo object associated with this folder
329 */
330 FolderInfo getInfo() {
331 return mInfo;
332 }
333
Adam Cohen3bf84d32012-05-07 20:17:14 -0700334 private class GridComparator implements Comparator<ShortcutInfo> {
335 int mNumCols;
336 public GridComparator(int numCols) {
337 mNumCols = numCols;
338 }
339
340 @Override
341 public int compare(ShortcutInfo lhs, ShortcutInfo rhs) {
342 int lhIndex = lhs.cellY * mNumCols + lhs.cellX;
343 int rhIndex = rhs.cellY * mNumCols + rhs.cellX;
344 return (lhIndex - rhIndex);
345 }
Adam Cohen3bf84d32012-05-07 20:17:14 -0700346 }
347
348 private void placeInReadingOrder(ArrayList<ShortcutInfo> items) {
349 int maxX = 0;
350 int count = items.size();
351 for (int i = 0; i < count; i++) {
352 ShortcutInfo item = items.get(i);
353 if (item.cellX > maxX) {
354 maxX = item.cellX;
355 }
356 }
Adam Cohen691a5792012-05-11 14:27:30 -0700357
358 GridComparator gridComparator = new GridComparator(maxX + 1);
Adam Cohen3bf84d32012-05-07 20:17:14 -0700359 Collections.sort(items, gridComparator);
360 final int countX = mContent.getCountX();
361 for (int i = 0; i < count; i++) {
362 int x = i % countX;
363 int y = i / countX;
364 ShortcutInfo item = items.get(i);
365 item.cellX = x;
366 item.cellY = y;
367 }
368 }
369
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800370 void bind(FolderInfo info) {
371 mInfo = info;
Adam Cohendf2cc412011-04-27 16:56:57 -0700372 ArrayList<ShortcutInfo> children = info.contents;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700373 ArrayList<ShortcutInfo> overflow = new ArrayList<ShortcutInfo>();
Adam Cohen7c693212011-05-18 15:26:57 -0700374 setupContentForNumItems(children.size());
Adam Cohen3bf84d32012-05-07 20:17:14 -0700375 placeInReadingOrder(children);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700376 int count = 0;
Adam Cohendf2cc412011-04-27 16:56:57 -0700377 for (int i = 0; i < children.size(); i++) {
378 ShortcutInfo child = (ShortcutInfo) children.get(i);
Adam Cohenc508b2d2011-06-28 14:41:44 -0700379 if (!createAndAddShortcut(child)) {
380 overflow.add(child);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700381 } else {
382 count++;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700383 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700384 }
Adam Cohenc508b2d2011-06-28 14:41:44 -0700385
Adam Cohen0057bbc2011-08-12 18:30:51 -0700386 // We rearrange the items in case there are any empty gaps
387 setupContentForNumItems(count);
388
Adam Cohenc508b2d2011-06-28 14:41:44 -0700389 // If our folder has too many items we prune them from the list. This is an issue
390 // when upgrading from the old Folders implementation which could contain an unlimited
391 // number of items.
392 for (ShortcutInfo item: overflow) {
393 mInfo.remove(item);
394 LauncherModel.deleteItemFromDatabase(mLauncher, item);
395 }
396
Adam Cohen4dbe6d92011-05-18 17:14:15 -0700397 mItemsInvalidated = true;
Adam Cohenac56cff2011-09-28 20:45:37 -0700398 updateTextViewFocus();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700399 mInfo.addListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700400
Adam Cohenafb01ee2011-06-23 15:38:03 -0700401 if (!sDefaultFolderName.contentEquals(mInfo.title)) {
Adam Cohen4ef610f2011-06-21 22:37:36 -0700402 mFolderName.setText(mInfo.title);
403 } else {
404 mFolderName.setText("");
405 }
Adam Cohen691a5792012-05-11 14:27:30 -0700406 updateItemLocationsInDatabase();
Winson Chung33231f52013-12-09 16:57:45 -0800407
408 // In case any children didn't come across during loading, clean up the folder accordingly
409 mFolderIcon.post(new Runnable() {
410 public void run() {
411 if (getItemCount() <= 1) {
412 replaceFolderWithFinalItem();
413 }
414 }
415 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700416 }
417
418 /**
419 * Creates a new UserFolder, inflated from R.layout.user_folder.
420 *
421 * @param context The application's context.
422 *
423 * @return A new UserFolder.
424 */
425 static Folder fromXml(Context context) {
426 return (Folder) LayoutInflater.from(context).inflate(R.layout.user_folder, null);
427 }
428
429 /**
430 * This method is intended to make the UserFolder to be visually identical in size and position
431 * to its associated FolderIcon. This allows for a seamless transition into the expanded state.
432 */
433 private void positionAndSizeAsIcon() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700434 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800435 setScaleX(0.8f);
436 setScaleY(0.8f);
437 setAlpha(0f);
Adam Cohendf2cc412011-04-27 16:56:57 -0700438 mState = STATE_SMALL;
439 }
440
441 public void animateOpen() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700442 positionAndSizeAsIcon();
443
Adam Cohen8e776a62011-06-28 18:10:06 -0700444 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen2801caf2011-05-13 20:57:39 -0700445 centerAboutIcon();
Adam Cohen662b5982011-12-13 17:45:21 -0800446 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1);
447 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
448 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
Michael Jurka032e6ba2013-04-22 15:08:42 +0200449 final ObjectAnimator oa =
Michael Jurka2ecf9952012-06-18 12:52:28 -0700450 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohen6441de02011-12-14 14:25:32 -0800451
Adam Cohen2801caf2011-05-13 20:57:39 -0700452 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700453 @Override
454 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700455 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700456 String.format(getContext().getString(R.string.folder_opened),
Adam Cohen3371da02011-10-25 21:38:29 -0700457 mContent.getCountX(), mContent.getCountY()));
Adam Cohendf2cc412011-04-27 16:56:57 -0700458 mState = STATE_ANIMATING;
459 }
460 @Override
461 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700462 mState = STATE_OPEN;
Michael Jurka0121c3e2012-05-31 08:36:04 -0700463 setLayerType(LAYER_TYPE_NONE, null);
Winson Chung7a74ac92011-09-20 17:43:51 -0700464 Cling cling = mLauncher.showFirstRunFoldersCling();
465 if (cling != null) {
Winson Chungfa545132013-09-26 17:45:32 -0700466 cling.bringScrimToFront();
Winson Chungaf40f202013-09-18 18:26:31 -0700467 bringToFront();
Winson Chungfa545132013-09-26 17:45:32 -0700468 cling.bringToFront();
Winson Chung7a74ac92011-09-20 17:43:51 -0700469 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700470 setFocusOnFirstChild();
Adam Cohendf2cc412011-04-27 16:56:57 -0700471 }
472 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700473 oa.setDuration(mExpandDuration);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700474 setLayerType(LAYER_TYPE_HARDWARE, null);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100475 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700476 }
477
Adam Cohen3371da02011-10-25 21:38:29 -0700478 private void sendCustomAccessibilityEvent(int type, String text) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700479 AccessibilityManager accessibilityManager = (AccessibilityManager)
480 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
481 if (accessibilityManager.isEnabled()) {
Adam Cohen3371da02011-10-25 21:38:29 -0700482 AccessibilityEvent event = AccessibilityEvent.obtain(type);
483 onInitializeAccessibilityEvent(event);
484 event.getText().add(text);
Michael Jurka8b805b12012-04-18 14:23:14 -0700485 accessibilityManager.sendAccessibilityEvent(event);
Adam Cohen3371da02011-10-25 21:38:29 -0700486 }
487 }
488
Adam Cohenac56cff2011-09-28 20:45:37 -0700489 private void setFocusOnFirstChild() {
490 View firstChild = mContent.getChildAt(0, 0);
491 if (firstChild != null) {
492 firstChild.requestFocus();
493 }
494 }
495
Adam Cohendf2cc412011-04-27 16:56:57 -0700496 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700497 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800498 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
499 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
500 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
Michael Jurka032e6ba2013-04-22 15:08:42 +0200501 final ObjectAnimator oa =
Michael Jurka2ecf9952012-06-18 12:52:28 -0700502 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohendf2cc412011-04-27 16:56:57 -0700503
Adam Cohen2801caf2011-05-13 20:57:39 -0700504 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700505 @Override
506 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700507 onCloseComplete();
Michael Jurka0121c3e2012-05-31 08:36:04 -0700508 setLayerType(LAYER_TYPE_NONE, null);
Adam Cohen2801caf2011-05-13 20:57:39 -0700509 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700510 }
511 @Override
512 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700513 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700514 getContext().getString(R.string.folder_closed));
Adam Cohendf2cc412011-04-27 16:56:57 -0700515 mState = STATE_ANIMATING;
516 }
517 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700518 oa.setDuration(mExpandDuration);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700519 setLayerType(LAYER_TYPE_HARDWARE, null);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100520 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700521 }
522
Adam Cohencb3382b2011-05-24 14:07:08 -0700523 public boolean acceptDrop(DragObject d) {
524 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700525 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700526 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
527 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
528 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700529 }
530
Adam Cohendf2cc412011-04-27 16:56:57 -0700531 protected boolean findAndSetEmptyCells(ShortcutInfo item) {
532 int[] emptyCell = new int[2];
533 if (mContent.findCellForSpan(emptyCell, item.spanX, item.spanY)) {
534 item.cellX = emptyCell[0];
535 item.cellY = emptyCell[1];
Adam Cohendf2cc412011-04-27 16:56:57 -0700536 return true;
537 } else {
538 return false;
539 }
540 }
541
Adam Cohenc508b2d2011-06-28 14:41:44 -0700542 protected boolean createAndAddShortcut(ShortcutInfo item) {
Adam Cohen477828c2013-09-20 12:05:49 -0700543 final BubbleTextView textView =
544 (BubbleTextView) mInflater.inflate(R.layout.application, this, false);
Winson Chung54000492013-10-14 16:29:29 -0700545 textView.setCompoundDrawables(null,
Winson Chung0dbd7342013-10-13 22:46:20 -0700546 Utilities.createIconDrawable(item.getIcon(mIconCache)), null, null);
Adam Cohendf2cc412011-04-27 16:56:57 -0700547 textView.setText(item.title);
548 textView.setTag(item);
Adam Cohen477828c2013-09-20 12:05:49 -0700549 textView.setTextColor(getResources().getColor(R.color.folder_items_text_color));
550 textView.setShadowsEnabled(false);
Adam Cohen7397e622013-10-24 12:11:34 -0700551 textView.setGlowColor(getResources().getColor(R.color.folder_items_glow_color));
Adam Cohendf2cc412011-04-27 16:56:57 -0700552
553 textView.setOnClickListener(this);
554 textView.setOnLongClickListener(this);
555
Adam Cohenc508b2d2011-06-28 14:41:44 -0700556 // We need to check here to verify that the given item's location isn't already occupied
Adam Cohen3bf84d32012-05-07 20:17:14 -0700557 // by another item.
Adam Cohen0057bbc2011-08-12 18:30:51 -0700558 if (mContent.getChildAt(item.cellX, item.cellY) != null || item.cellX < 0 || item.cellY < 0
559 || item.cellX >= mContent.getCountX() || item.cellY >= mContent.getCountY()) {
Adam Cohen3bf84d32012-05-07 20:17:14 -0700560 // This shouldn't happen, log it.
561 Log.e(TAG, "Folder order not properly persisted during bind");
Adam Cohenc508b2d2011-06-28 14:41:44 -0700562 if (!findAndSetEmptyCells(item)) {
563 return false;
564 }
565 }
566
Adam Cohendf2cc412011-04-27 16:56:57 -0700567 CellLayout.LayoutParams lp =
568 new CellLayout.LayoutParams(item.cellX, item.cellY, item.spanX, item.spanY);
569 boolean insert = false;
Adam Cohenac56cff2011-09-28 20:45:37 -0700570 textView.setOnKeyListener(new FolderKeyEventListener());
Adam Cohendf2cc412011-04-27 16:56:57 -0700571 mContent.addViewToCellLayout(textView, insert ? 0 : -1, (int)item.id, lp, true);
Adam Cohenc508b2d2011-06-28 14:41:44 -0700572 return true;
Adam Cohendf2cc412011-04-27 16:56:57 -0700573 }
574
Adam Cohencb3382b2011-05-24 14:07:08 -0700575 public void onDragEnter(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700576 mPreviousTargetCell[0] = -1;
577 mPreviousTargetCell[1] = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700578 mOnExitAlarm.cancelAlarm();
579 }
580
581 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
582 public void onAlarm(Alarm alarm) {
583 realTimeReorder(mEmptyCell, mTargetCell);
584 }
585 };
586
587 boolean readingOrderGreaterThan(int[] v1, int[] v2) {
588 if (v1[1] > v2[1] || (v1[1] == v2[1] && v1[0] > v2[0])) {
589 return true;
590 } else {
591 return false;
592 }
593 }
594
595 private void realTimeReorder(int[] empty, int[] target) {
596 boolean wrap;
597 int startX;
598 int endX;
599 int startY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700600 int delay = 0;
601 float delayAmount = 30;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700602 if (readingOrderGreaterThan(target, empty)) {
603 wrap = empty[0] >= mContent.getCountX() - 1;
604 startY = wrap ? empty[1] + 1 : empty[1];
605 for (int y = startY; y <= target[1]; y++) {
606 startX = y == empty[1] ? empty[0] + 1 : 0;
607 endX = y < target[1] ? mContent.getCountX() - 1 : target[0];
608 for (int x = startX; x <= endX; x++) {
609 View v = mContent.getChildAt(x,y);
610 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen482ed822012-03-02 14:15:13 -0800611 REORDER_ANIMATION_DURATION, delay, true, true)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700612 empty[0] = x;
613 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700614 delay += delayAmount;
615 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700616 }
617 }
618 }
619 } else {
620 wrap = empty[0] == 0;
621 startY = wrap ? empty[1] - 1 : empty[1];
622 for (int y = startY; y >= target[1]; y--) {
623 startX = y == empty[1] ? empty[0] - 1 : mContent.getCountX() - 1;
624 endX = y > target[1] ? 0 : target[0];
625 for (int x = startX; x >= endX; x--) {
626 View v = mContent.getChildAt(x,y);
627 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen482ed822012-03-02 14:15:13 -0800628 REORDER_ANIMATION_DURATION, delay, true, true)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700629 empty[0] = x;
630 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700631 delay += delayAmount;
632 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700633 }
634 }
635 }
636 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700637 }
638
Adam Cohen2374abf2013-04-16 14:56:57 -0700639 public boolean isLayoutRtl() {
640 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
641 }
642
Adam Cohencb3382b2011-05-24 14:07:08 -0700643 public void onDragOver(DragObject d) {
Alan Viverette4cda5b72013-08-28 17:53:41 -0700644 final DragView dragView = d.dragView;
645 final int scrollOffset = mScrollView.getScrollY();
646 final float[] r = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, dragView, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700647 r[0] -= getPaddingLeft();
648 r[1] -= getPaddingTop();
649
Alan Viverette4cda5b72013-08-28 17:53:41 -0700650 final long downTime = SystemClock.uptimeMillis();
651 final MotionEvent translatedEv = MotionEvent.obtain(
652 downTime, downTime, MotionEvent.ACTION_MOVE, d.x, d.y, 0);
653
654 if (!mAutoScrollHelper.isEnabled()) {
655 mAutoScrollHelper.setEnabled(true);
656 }
657
658 final boolean handled = mAutoScrollHelper.onTouch(this, translatedEv);
659 translatedEv.recycle();
660
661 if (handled) {
662 mReorderAlarm.cancelAlarm();
663 } else {
664 mTargetCell = mContent.findNearestArea(
665 (int) r[0], (int) r[1] + scrollOffset, 1, 1, mTargetCell);
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700666 if (isLayoutRtl()) {
667 mTargetCell[0] = mContent.getCountX() - mTargetCell[0] - 1;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700668 }
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700669 if (mTargetCell[0] != mPreviousTargetCell[0]
670 || mTargetCell[1] != mPreviousTargetCell[1]) {
671 mReorderAlarm.cancelAlarm();
672 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
673 mReorderAlarm.setAlarm(REORDER_DELAY);
674 mPreviousTargetCell[0] = mTargetCell[0];
675 mPreviousTargetCell[1] = mTargetCell[1];
676 mDragMode = DRAG_MODE_REORDER;
677 } else {
678 mDragMode = DRAG_MODE_NONE;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700679 }
Adam Cohenbfbfd262011-06-13 16:55:12 -0700680 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700681 }
682
Adam Cohenbfbfd262011-06-13 16:55:12 -0700683 // This is used to compute the visual center of the dragView. The idea is that
684 // the visual center represents the user's interpretation of where the item is, and hence
685 // is the appropriate point to use when determining drop location.
686 private float[] getDragViewVisualCenter(int x, int y, int xOffset, int yOffset,
687 DragView dragView, float[] recycle) {
688 float res[];
689 if (recycle == null) {
690 res = new float[2];
691 } else {
692 res = recycle;
693 }
694
695 // These represent the visual top and left of drag view if a dragRect was provided.
696 // If a dragRect was not provided, then they correspond to the actual view left and
697 // top, as the dragRect is in that case taken to be the entire dragView.
698 // R.dimen.dragViewOffsetY.
699 int left = x - xOffset;
700 int top = y - yOffset;
701
702 // In order to find the visual center, we shift by half the dragRect
703 res[0] = left + dragView.getDragRegion().width() / 2;
704 res[1] = top + dragView.getDragRegion().height() / 2;
705
706 return res;
707 }
708
709 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
710 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700711 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700712 }
713 };
714
Adam Cohen95bb8002011-07-03 23:40:28 -0700715 public void completeDragExit() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700716 mLauncher.closeFolder();
717 mCurrentDragInfo = null;
718 mCurrentDragView = null;
719 mSuppressOnAdd = false;
720 mRearrangeOnClose = true;
721 }
722
Adam Cohencb3382b2011-05-24 14:07:08 -0700723 public void onDragExit(DragObject d) {
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700724 // Exiting folder; stop the auto scroller.
Alan Viverette4cda5b72013-08-28 17:53:41 -0700725 mAutoScrollHelper.setEnabled(false);
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700726 // We only close the folder if this is a true drag exit, ie. not because
727 // a drop has occurred above the folder.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700728 if (!d.dragComplete) {
729 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
730 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
731 }
732 mReorderAlarm.cancelAlarm();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700733 mDragMode = DRAG_MODE_NONE;
Adam Cohen2801caf2011-05-13 20:57:39 -0700734 }
735
Michael Jurka1e2f4652013-07-08 18:03:46 -0700736 public void onDropCompleted(final View target, final DragObject d,
737 final boolean isFlingToDelete, final boolean success) {
738 if (mDeferDropAfterUninstall) {
Michael Jurkaf3007582013-08-21 14:33:57 +0200739 Log.d(TAG, "Deferred handling drop because waiting for uninstall.");
Michael Jurka1e2f4652013-07-08 18:03:46 -0700740 mDeferredAction = new Runnable() {
741 public void run() {
742 onDropCompleted(target, d, isFlingToDelete, success);
743 mDeferredAction = null;
744 }
745 };
746 return;
747 }
748
749 boolean beingCalledAfterUninstall = mDeferredAction != null;
750 boolean successfulDrop =
751 success && (!beingCalledAfterUninstall || mUninstallSuccessful);
Winson Chung5f8afe62013-08-12 16:19:28 -0700752
Michael Jurka1e2f4652013-07-08 18:03:46 -0700753 if (successfulDrop) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700754 if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700755 replaceFolderWithFinalItem();
756 }
757 } else {
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700758 setupContentForNumItems(getItemCount());
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700759 // The drag failed, we need to return the item to the folder
760 mFolderIcon.onDrop(d);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700761 }
762
763 if (target != this) {
764 if (mOnExitAlarm.alarmPending()) {
765 mOnExitAlarm.cancelAlarm();
Michael Jurka54554252013-08-01 12:52:23 +0200766 if (!successfulDrop) {
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700767 mSuppressFolderDeletion = true;
768 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700769 completeDragExit();
770 }
Adam Cohenf0f2ffa2013-11-12 11:44:58 +0000771 } else {
772 mLauncher.getWorkspace().removeExtraEmptyScreen(true, null);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700773 }
Winson Chung5f8afe62013-08-12 16:19:28 -0700774
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700775 mDeleteFolderOnDropCompleted = false;
776 mDragInProgress = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700777 mItemAddedBackToSelfViaIcon = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700778 mCurrentDragInfo = null;
779 mCurrentDragView = null;
780 mSuppressOnAdd = false;
Adam Cohen4045eb72011-10-06 11:44:26 -0700781
782 // Reordering may have occured, and we need to save the new item locations. We do this once
783 // at the end to prevent unnecessary database operations.
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700784 updateItemLocationsInDatabaseBatch();
Adam Cohen4045eb72011-10-06 11:44:26 -0700785 }
786
Michael Jurka1e2f4652013-07-08 18:03:46 -0700787 public void deferCompleteDropAfterUninstallActivity() {
788 mDeferDropAfterUninstall = true;
789 }
790
791 public void onUninstallActivityReturned(boolean success) {
792 mDeferDropAfterUninstall = false;
793 mUninstallSuccessful = success;
794 if (mDeferredAction != null) {
795 mDeferredAction.run();
796 }
797 }
798
Winson Chunga48487a2012-03-20 16:19:37 -0700799 @Override
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800800 public float getIntrinsicIconScaleFactor() {
801 return 1f;
802 }
803
804 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800805 public boolean supportsFlingToDelete() {
806 return true;
807 }
808
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000809 @Override
810 public boolean supportsAppInfoDropTarget() {
811 return false;
812 }
813
814 @Override
815 public boolean supportsDeleteDropTarget() {
816 return true;
817 }
818
Winson Chunga48487a2012-03-20 16:19:37 -0700819 public void onFlingToDelete(DragObject d, int x, int y, PointF vec) {
820 // Do nothing
821 }
822
823 @Override
824 public void onFlingToDeleteCompleted() {
825 // Do nothing
826 }
827
Adam Cohen4045eb72011-10-06 11:44:26 -0700828 private void updateItemLocationsInDatabase() {
829 ArrayList<View> list = getItemsInReadingOrder();
830 for (int i = 0; i < list.size(); i++) {
831 View v = list.get(i);
832 ItemInfo info = (ItemInfo) v.getTag();
833 LauncherModel.moveItemInDatabase(mLauncher, info, mInfo.id, 0,
Winson Chung33231f52013-12-09 16:57:45 -0800834 info.cellX, info.cellY);
Adam Cohen4045eb72011-10-06 11:44:26 -0700835 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700836 }
Adam Cohen228da5a2011-07-27 22:23:47 -0700837
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700838 private void updateItemLocationsInDatabaseBatch() {
839 ArrayList<View> list = getItemsInReadingOrder();
840 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
841 for (int i = 0; i < list.size(); i++) {
842 View v = list.get(i);
843 ItemInfo info = (ItemInfo) v.getTag();
844 items.add(info);
845 }
846
847 LauncherModel.moveItemsInDatabase(mLauncher, items, mInfo.id, 0);
848 }
849
Adam Cohene25af792013-06-06 23:08:25 -0700850 public void addItemLocationsInDatabase() {
851 ArrayList<View> list = getItemsInReadingOrder();
852 for (int i = 0; i < list.size(); i++) {
853 View v = list.get(i);
854 ItemInfo info = (ItemInfo) v.getTag();
855 LauncherModel.addItemToDatabase(mLauncher, info, mInfo.id, 0,
856 info.cellX, info.cellY, false);
857 }
858 }
859
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700860 public void notifyDrop() {
861 if (mDragInProgress) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700862 mItemAddedBackToSelfViaIcon = true;
Adam Cohen76078c42011-06-09 15:06:52 -0700863 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700864 }
865
866 public boolean isDropEnabled() {
867 return true;
868 }
869
Adam Cohen4045eb72011-10-06 11:44:26 -0700870 private void setupContentDimensions(int count) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700871 ArrayList<View> list = getItemsInReadingOrder();
872
873 int countX = mContent.getCountX();
874 int countY = mContent.getCountY();
Adam Cohen7c693212011-05-18 15:26:57 -0700875 boolean done = false;
Adam Cohen2801caf2011-05-13 20:57:39 -0700876
Adam Cohen7c693212011-05-18 15:26:57 -0700877 while (!done) {
878 int oldCountX = countX;
879 int oldCountY = countY;
880 if (countX * countY < count) {
881 // Current grid is too small, expand it
Adam Cohen78dc83e2011-11-15 17:10:00 -0800882 if ((countX <= countY || countY == mMaxCountY) && countX < mMaxCountX) {
Adam Cohen7c693212011-05-18 15:26:57 -0700883 countX++;
884 } else if (countY < mMaxCountY) {
885 countY++;
886 }
887 if (countY == 0) countY++;
888 } else if ((countY - 1) * countX >= count && countY >= countX) {
889 countY = Math.max(0, countY - 1);
890 } else if ((countX - 1) * countY >= count) {
891 countX = Math.max(0, countX - 1);
Adam Cohen2801caf2011-05-13 20:57:39 -0700892 }
Adam Cohen7c693212011-05-18 15:26:57 -0700893 done = countX == oldCountX && countY == oldCountY;
Adam Cohen2801caf2011-05-13 20:57:39 -0700894 }
Adam Cohen7c693212011-05-18 15:26:57 -0700895 mContent.setGridSize(countX, countY);
Adam Cohen2801caf2011-05-13 20:57:39 -0700896 arrangeChildren(list);
897 }
898
899 public boolean isFull() {
Adam Cohen78dc83e2011-11-15 17:10:00 -0800900 return getItemCount() >= mMaxNumItems;
Adam Cohen2801caf2011-05-13 20:57:39 -0700901 }
902
903 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700904 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700905
Winson Chung892c74d2013-08-22 16:15:50 -0700906 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700907 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700908 int height = getFolderHeight();
Adam Cohen2801caf2011-05-13 20:57:39 -0700909
Adam Cohen307fe232012-08-16 17:55:58 -0700910 float scale = parent.getDescendantRectRelativeToSelf(mFolderIcon, mTempRect);
Adam Cohen8e776a62011-06-28 18:10:06 -0700911
Winson Chungaf40f202013-09-18 18:26:31 -0700912 LauncherAppState app = LauncherAppState.getInstance();
913 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
914
Adam Cohen307fe232012-08-16 17:55:58 -0700915 int centerX = (int) (mTempRect.left + mTempRect.width() * scale / 2);
916 int centerY = (int) (mTempRect.top + mTempRect.height() * scale / 2);
Adam Cohen2801caf2011-05-13 20:57:39 -0700917 int centeredLeft = centerX - width / 2;
918 int centeredTop = centerY - height / 2;
Winson Chung3057b1c2013-10-10 17:35:35 -0700919 int currentPage = mLauncher.getWorkspace().getNextPage();
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800920 // In case the workspace is scrolling, we need to use the final scroll to compute
921 // the folders bounds.
922 mLauncher.getWorkspace().setFinalScrollForPageChange(currentPage);
Adam Cohen35e7e642011-07-17 14:47:18 -0700923 // We first fetch the currently visible CellLayoutChildren
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800924 CellLayout currentLayout = (CellLayout) mLauncher.getWorkspace().getChildAt(currentPage);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700925 ShortcutAndWidgetContainer boundingLayout = currentLayout.getShortcutsAndWidgets();
Adam Cohen35e7e642011-07-17 14:47:18 -0700926 Rect bounds = new Rect();
927 parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800928 // We reset the workspaces scroll
929 mLauncher.getWorkspace().resetFinalScrollForPageChange(currentPage);
Adam Cohen2801caf2011-05-13 20:57:39 -0700930
Adam Cohen35e7e642011-07-17 14:47:18 -0700931 // We need to bound the folder to the currently visible CellLayoutChildren
932 int left = Math.min(Math.max(bounds.left, centeredLeft),
933 bounds.left + bounds.width() - width);
934 int top = Math.min(Math.max(bounds.top, centeredTop),
935 bounds.top + bounds.height() - height);
Winson Chungaf40f202013-09-18 18:26:31 -0700936 if (grid.isPhone() && (grid.availableWidthPx - width) < grid.iconSizePx) {
937 // Center the folder if it is full (on phones only)
938 left = (grid.availableWidthPx - width) / 2;
939 } else if (width >= bounds.width()) {
940 // If the folder doesn't fit within the bounds, center it about the desired bounds
Adam Cohen35e7e642011-07-17 14:47:18 -0700941 left = bounds.left + (bounds.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700942 }
Adam Cohen35e7e642011-07-17 14:47:18 -0700943 if (height >= bounds.height()) {
944 top = bounds.top + (bounds.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700945 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700946
947 int folderPivotX = width / 2 + (centeredLeft - left);
948 int folderPivotY = height / 2 + (centeredTop - top);
949 setPivotX(folderPivotX);
950 setPivotY(folderPivotY);
Adam Cohen268c4752012-06-06 17:47:33 -0700951 mFolderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700952 (1.0f * folderPivotX / width));
Adam Cohen268c4752012-06-06 17:47:33 -0700953 mFolderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700954 (1.0f * folderPivotY / height));
Adam Cohen3bf84d32012-05-07 20:17:14 -0700955
Adam Cohen662b5982011-12-13 17:45:21 -0800956 lp.width = width;
957 lp.height = height;
958 lp.x = left;
959 lp.y = top;
Adam Cohen2801caf2011-05-13 20:57:39 -0700960 }
961
Adam Cohen268c4752012-06-06 17:47:33 -0700962 float getPivotXForIconAnimation() {
963 return mFolderIconPivotX;
964 }
965 float getPivotYForIconAnimation() {
966 return mFolderIconPivotY;
967 }
968
Adam Cohen2801caf2011-05-13 20:57:39 -0700969 private void setupContentForNumItems(int count) {
Adam Cohen4045eb72011-10-06 11:44:26 -0700970 setupContentDimensions(count);
Adam Cohen2801caf2011-05-13 20:57:39 -0700971
Adam Cohen8e776a62011-06-28 18:10:06 -0700972 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700973 if (lp == null) {
Adam Cohen8e776a62011-06-28 18:10:06 -0700974 lp = new DragLayer.LayoutParams(0, 0);
975 lp.customPosition = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700976 setLayoutParams(lp);
977 }
978 centerAboutIcon();
979 }
980
Winson Chung892c74d2013-08-22 16:15:50 -0700981 private int getContentAreaHeight() {
982 LauncherAppState app = LauncherAppState.getInstance();
983 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
984 Rect workspacePadding = grid.getWorkspacePadding(grid.isLandscape ?
985 CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
986 int maxContentAreaHeight = grid.availableHeightPx -
Winson Chung892c74d2013-08-22 16:15:50 -0700987 workspacePadding.top - workspacePadding.bottom -
Winson Chung892c74d2013-08-22 16:15:50 -0700988 mFolderNameHeight;
Adam Cohen1960ea42013-11-12 11:33:14 +0000989 int height = Math.min(maxContentAreaHeight,
Winson Chung892c74d2013-08-22 16:15:50 -0700990 mContent.getDesiredHeight());
Adam Cohen1960ea42013-11-12 11:33:14 +0000991 return Math.max(height, MIN_CONTENT_DIMEN);
992 }
993
994 private int getContentAreaWidth() {
995 return Math.max(mContent.getDesiredWidth(), MIN_CONTENT_DIMEN);
Winson Chung892c74d2013-08-22 16:15:50 -0700996 }
997
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700998 private int getFolderHeight() {
Winson Chung892c74d2013-08-22 16:15:50 -0700999 int height = getPaddingTop() + getPaddingBottom()
1000 + getContentAreaHeight() + mFolderNameHeight;
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001001 return height;
1002 }
Adam Cohen234c4cd2011-07-17 21:03:04 -07001003
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001004 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001005 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
1006 int height = getFolderHeight();
Adam Cohen1960ea42013-11-12 11:33:14 +00001007 int contentAreaWidthSpec = MeasureSpec.makeMeasureSpec(getContentAreaWidth(),
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001008 MeasureSpec.EXACTLY);
Winson Chung892c74d2013-08-22 16:15:50 -07001009 int contentAreaHeightSpec = MeasureSpec.makeMeasureSpec(getContentAreaHeight(),
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001010 MeasureSpec.EXACTLY);
Adam Cohen1960ea42013-11-12 11:33:14 +00001011
1012 mContent.setFixedSize(getContentAreaWidth(), getContentAreaHeight());
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001013 mScrollView.measure(contentAreaWidthSpec, contentAreaHeightSpec);
1014 mFolderName.measure(contentAreaWidthSpec,
Adam Cohen234c4cd2011-07-17 21:03:04 -07001015 MeasureSpec.makeMeasureSpec(mFolderNameHeight, MeasureSpec.EXACTLY));
1016 setMeasuredDimension(width, height);
1017 }
1018
Adam Cohen2801caf2011-05-13 20:57:39 -07001019 private void arrangeChildren(ArrayList<View> list) {
1020 int[] vacant = new int[2];
1021 if (list == null) {
1022 list = getItemsInReadingOrder();
1023 }
1024 mContent.removeAllViews();
1025
1026 for (int i = 0; i < list.size(); i++) {
1027 View v = list.get(i);
1028 mContent.getVacantCell(vacant, 1, 1);
1029 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) v.getLayoutParams();
1030 lp.cellX = vacant[0];
1031 lp.cellY = vacant[1];
1032 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohen2792a332011-09-26 21:09:47 -07001033 if (info.cellX != vacant[0] || info.cellY != vacant[1]) {
1034 info.cellX = vacant[0];
1035 info.cellY = vacant[1];
1036 LauncherModel.addOrMoveItemInDatabase(mLauncher, info, mInfo.id, 0,
1037 info.cellX, info.cellY);
1038 }
Adam Cohen2801caf2011-05-13 20:57:39 -07001039 boolean insert = false;
1040 mContent.addViewToCellLayout(v, insert ? 0 : -1, (int)info.id, lp, true);
1041 }
Adam Cohen7c693212011-05-18 15:26:57 -07001042 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -07001043 }
1044
Adam Cohena9cf38f2011-05-02 15:36:58 -07001045 public int getItemCount() {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001046 return mContent.getShortcutsAndWidgets().getChildCount();
Adam Cohena9cf38f2011-05-02 15:36:58 -07001047 }
1048
1049 public View getItemAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001050 return mContent.getShortcutsAndWidgets().getChildAt(index);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001051 }
1052
Adam Cohen2801caf2011-05-13 20:57:39 -07001053 private void onCloseComplete() {
Adam Cohen05e0f402011-08-01 12:12:49 -07001054 DragLayer parent = (DragLayer) getParent();
Michael Jurka5649c282012-06-18 10:33:21 -07001055 if (parent != null) {
1056 parent.removeView(this);
1057 }
Adam Cohen4554ee12011-08-03 16:13:21 -07001058 mDragController.removeDropTarget((DropTarget) this);
Adam Cohen05e0f402011-08-01 12:12:49 -07001059 clearFocus();
Adam Cohenac56cff2011-09-28 20:45:37 -07001060 mFolderIcon.requestFocus();
Adam Cohen05e0f402011-08-01 12:12:49 -07001061
Adam Cohen2801caf2011-05-13 20:57:39 -07001062 if (mRearrangeOnClose) {
1063 setupContentForNumItems(getItemCount());
1064 mRearrangeOnClose = false;
1065 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001066 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001067 if (!mDragInProgress && !mSuppressFolderDeletion) {
1068 replaceFolderWithFinalItem();
1069 } else if (mDragInProgress) {
1070 mDeleteFolderOnDropCompleted = true;
1071 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001072 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001073 mSuppressFolderDeletion = false;
Adam Cohenafb01ee2011-06-23 15:38:03 -07001074 }
1075
1076 private void replaceFolderWithFinalItem() {
Adam Cohenafb01ee2011-06-23 15:38:03 -07001077 // Add the last remaining child to the workspace in place of the folder
Adam Cohenfb91f302012-06-11 15:45:18 -07001078 Runnable onCompleteRunnable = new Runnable() {
1079 @Override
1080 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -07001081 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screenId);
Adam Cohenafb01ee2011-06-23 15:38:03 -07001082
Winson Chung33231f52013-12-09 16:57:45 -08001083 View child = null;
Adam Cohenfb91f302012-06-11 15:45:18 -07001084 // Move the item from the folder to the workspace, in the position of the folder
1085 if (getItemCount() == 1) {
1086 ShortcutInfo finalItem = mInfo.contents.get(0);
Adam Cohenc5e63f32012-07-12 16:16:57 -07001087 child = mLauncher.createShortcut(R.layout.application, cellLayout,
Adam Cohenfb91f302012-06-11 15:45:18 -07001088 finalItem);
1089 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -07001090 mInfo.screenId, mInfo.cellX, mInfo.cellY);
Adam Cohenfb91f302012-06-11 15:45:18 -07001091 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001092 if (getItemCount() <= 1) {
1093 // Remove the folder
1094 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
1095 cellLayout.removeView(mFolderIcon);
1096 if (mFolderIcon instanceof DropTarget) {
1097 mDragController.removeDropTarget((DropTarget) mFolderIcon);
1098 }
1099 mLauncher.removeFolder(mInfo);
1100 }
Adam Cohenc5e63f32012-07-12 16:16:57 -07001101 // We add the child after removing the folder to prevent both from existing at
Winson Chung0e6a7132013-08-23 12:55:10 -07001102 // the same time in the CellLayout. We need to add the new item with addInScreenFromBind()
1103 // to ensure that hotseat items are placed correctly.
Adam Cohenc5e63f32012-07-12 16:16:57 -07001104 if (child != null) {
Winson Chung0e6a7132013-08-23 12:55:10 -07001105 mLauncher.getWorkspace().addInScreenFromBind(child, mInfo.container, mInfo.screenId,
Adam Cohenc5e63f32012-07-12 16:16:57 -07001106 mInfo.cellX, mInfo.cellY, mInfo.spanX, mInfo.spanY);
1107 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001108 }
1109 };
1110 View finalChild = getItemAt(0);
1111 if (finalChild != null) {
1112 mFolderIcon.performDestroyAnimation(finalChild, onCompleteRunnable);
Winson Chung33231f52013-12-09 16:57:45 -08001113 } else {
1114 onCompleteRunnable.run();
Adam Cohenafb01ee2011-06-23 15:38:03 -07001115 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001116 mDestroyed = true;
1117 }
1118
1119 boolean isDestroyed() {
1120 return mDestroyed;
Adam Cohen2801caf2011-05-13 20:57:39 -07001121 }
1122
Adam Cohenac56cff2011-09-28 20:45:37 -07001123 // This method keeps track of the last item in the folder for the purposes
1124 // of keyboard focus
1125 private void updateTextViewFocus() {
1126 View lastChild = getItemAt(getItemCount() - 1);
1127 getItemAt(getItemCount() - 1);
1128 if (lastChild != null) {
1129 mFolderName.setNextFocusDownId(lastChild.getId());
1130 mFolderName.setNextFocusRightId(lastChild.getId());
1131 mFolderName.setNextFocusLeftId(lastChild.getId());
1132 mFolderName.setNextFocusUpId(lastChild.getId());
1133 }
1134 }
1135
Adam Cohenbfbfd262011-06-13 16:55:12 -07001136 public void onDrop(DragObject d) {
1137 ShortcutInfo item;
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001138 if (d.dragInfo instanceof AppInfo) {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001139 // Came from all apps -- make a copy
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001140 item = ((AppInfo) d.dragInfo).makeShortcut();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001141 item.spanX = 1;
1142 item.spanY = 1;
1143 } else {
1144 item = (ShortcutInfo) d.dragInfo;
1145 }
Adam Cohen05e0f402011-08-01 12:12:49 -07001146 // Dragged from self onto self, currently this is the only path possible, however
1147 // we keep this as a distinct code path.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001148 if (item == mCurrentDragInfo) {
1149 ShortcutInfo si = (ShortcutInfo) mCurrentDragView.getTag();
1150 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) mCurrentDragView.getLayoutParams();
1151 si.cellX = lp.cellX = mEmptyCell[0];
1152 si.cellX = lp.cellY = mEmptyCell[1];
1153 mContent.addViewToCellLayout(mCurrentDragView, -1, (int)item.id, lp, true);
Adam Cohenfc53cd22011-07-20 15:45:11 -07001154 if (d.dragView.hasDrawn()) {
1155 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, mCurrentDragView);
1156 } else {
Winson Chung2c4cf412012-05-08 14:03:21 -07001157 d.deferDragViewCleanupPostAnimation = false;
Adam Cohenfc53cd22011-07-20 15:45:11 -07001158 mCurrentDragView.setVisibility(VISIBLE);
1159 }
Adam Cohene9166b22011-07-08 17:11:11 -07001160 mItemsInvalidated = true;
Adam Cohen4045eb72011-10-06 11:44:26 -07001161 setupContentDimensions(getItemCount());
Adam Cohen716b51e2011-06-30 12:09:54 -07001162 mSuppressOnAdd = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001163 }
1164 mInfo.add(item);
1165 }
1166
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001167 // This is used so the item doesn't immediately appear in the folder when added. In one case
1168 // we need to create the illusion that the item isn't added back to the folder yet, to
1169 // to correspond to the animation of the icon back into the folder. This is
1170 public void hideItem(ShortcutInfo info) {
1171 View v = getViewForInfo(info);
1172 v.setVisibility(INVISIBLE);
1173 }
1174 public void showItem(ShortcutInfo info) {
1175 View v = getViewForInfo(info);
1176 v.setVisibility(VISIBLE);
1177 }
1178
Adam Cohenbfbfd262011-06-13 16:55:12 -07001179 public void onAdd(ShortcutInfo item) {
1180 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001181 // If the item was dropped onto this open folder, we have done the work associated
1182 // with adding the item to the folder, as indicated by mSuppressOnAdd being set
Adam Cohenbfbfd262011-06-13 16:55:12 -07001183 if (mSuppressOnAdd) return;
1184 if (!findAndSetEmptyCells(item)) {
1185 // The current layout is full, can we expand it?
1186 setupContentForNumItems(getItemCount() + 1);
1187 findAndSetEmptyCells(item);
1188 }
1189 createAndAddShortcut(item);
1190 LauncherModel.addOrMoveItemInDatabase(
1191 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
1192 }
1193
Adam Cohena9cf38f2011-05-02 15:36:58 -07001194 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -07001195 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001196 // If this item is being dragged from this open folder, we have already handled
1197 // the work associated with removing the item, so we don't have to do anything here.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001198 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001199 View v = getViewForInfo(item);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001200 mContent.removeView(v);
Adam Cohen2801caf2011-05-13 20:57:39 -07001201 if (mState == STATE_ANIMATING) {
1202 mRearrangeOnClose = true;
1203 } else {
1204 setupContentForNumItems(getItemCount());
1205 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001206 if (getItemCount() <= 1) {
1207 replaceFolderWithFinalItem();
1208 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001209 }
Adam Cohen7c693212011-05-18 15:26:57 -07001210
Adam Cohendf1e4e82011-06-24 15:57:39 -07001211 private View getViewForInfo(ShortcutInfo item) {
1212 for (int j = 0; j < mContent.getCountY(); j++) {
1213 for (int i = 0; i < mContent.getCountX(); i++) {
1214 View v = mContent.getChildAt(i, j);
1215 if (v.getTag() == item) {
1216 return v;
1217 }
1218 }
1219 }
1220 return null;
1221 }
1222
Adam Cohen76078c42011-06-09 15:06:52 -07001223 public void onItemsChanged() {
Adam Cohenac56cff2011-09-28 20:45:37 -07001224 updateTextViewFocus();
Adam Cohen76078c42011-06-09 15:06:52 -07001225 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001226
Adam Cohen76fc0852011-06-17 13:26:23 -07001227 public void onTitleChanged(CharSequence title) {
1228 }
Adam Cohen76078c42011-06-09 15:06:52 -07001229
Adam Cohen7c693212011-05-18 15:26:57 -07001230 public ArrayList<View> getItemsInReadingOrder() {
1231 if (mItemsInvalidated) {
1232 mItemsInReadingOrder.clear();
1233 for (int j = 0; j < mContent.getCountY(); j++) {
1234 for (int i = 0; i < mContent.getCountX(); i++) {
1235 View v = mContent.getChildAt(i, j);
1236 if (v != null) {
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001237 mItemsInReadingOrder.add(v);
Adam Cohen7c693212011-05-18 15:26:57 -07001238 }
1239 }
1240 }
1241 mItemsInvalidated = false;
1242 }
1243 return mItemsInReadingOrder;
1244 }
Adam Cohen8dfcba42011-07-07 16:38:18 -07001245
1246 public void getLocationInDragLayer(int[] loc) {
1247 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
1248 }
Adam Cohenea0818d2011-09-30 20:06:58 -07001249
1250 public void onFocusChange(View v, boolean hasFocus) {
1251 if (v == mFolderName && hasFocus) {
1252 startEditingFolderName();
1253 }
1254 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001255
1256 @Override
1257 public void getHitRectRelativeToDragLayer(Rect outRect) {
1258 getHitRect(outRect);
1259 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001260}