blob: be511fb9b016fec282d3b64af7979cad7f0660cf [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;
Adam Cohenf0f4eda2013-06-06 21:27:03 -070023import android.content.ComponentName;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080024import android.content.Context;
Adam Cohen76fc0852011-06-17 13:26:23 -070025import android.content.res.Resources;
Winson Chung043f2af2012-03-01 16:09:54 -080026import android.graphics.PointF;
Romain Guyfb5411e2010-02-24 10:04:17 -080027import android.graphics.Rect;
Adam Cohenbadf71e2011-05-26 19:08:29 -070028import android.graphics.drawable.Drawable;
Adam Cohen7a14d0b2011-06-24 11:36:35 -070029import android.text.InputType;
Adam Cohene601a432011-07-26 21:51:30 -070030import android.text.Selection;
31import android.text.Spannable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.util.AttributeSet;
Adam Cohen3bf84d32012-05-07 20:17:14 -070033import android.util.Log;
Adam Cohen76fc0852011-06-17 13:26:23 -070034import android.view.ActionMode;
35import android.view.KeyEvent;
Adam Cohendf2cc412011-04-27 16:56:57 -070036import android.view.LayoutInflater;
Adam Cohen76fc0852011-06-17 13:26:23 -070037import android.view.Menu;
38import android.view.MenuItem;
Adam Cohen0c872ba2011-05-05 10:34:16 -070039import android.view.MotionEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.view.View;
Adam Cohen3371da02011-10-25 21:38:29 -070041import android.view.accessibility.AccessibilityEvent;
42import android.view.accessibility.AccessibilityManager;
Mindy DelliCarpini2e7471c2013-06-27 08:45:11 -070043import android.view.animation.AccelerateInterpolator;
44import android.view.animation.Interpolator;
Adam Cohen76fc0852011-06-17 13:26:23 -070045import android.view.inputmethod.EditorInfo;
46import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.widget.LinearLayout;
Adam Cohenf0f4eda2013-06-06 21:27:03 -070048import android.widget.ScrollView;
Adam Cohendf2cc412011-04-27 16:56:57 -070049import android.widget.TextView;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050
Daniel Sandler325dc232013-06-05 22:57:57 -040051import com.android.launcher3.R;
52import com.android.launcher3.FolderInfo.FolderListener;
Romain Guyedcce092010-03-04 13:03:17 -080053
Adam Cohenc0dcf592011-06-01 15:30:43 -070054import java.util.ArrayList;
Adam Cohen3bf84d32012-05-07 20:17:14 -070055import java.util.Collections;
56import java.util.Comparator;
Adam Cohenc0dcf592011-06-01 15:30:43 -070057
The Android Open Source Project31dd5032009-03-03 19:32:27 -080058/**
59 * Represents a set of icons chosen by the user or generated by the system.
60 */
Adam Cohen8dfcba42011-07-07 16:38:18 -070061public class Folder extends LinearLayout implements DragSource, View.OnClickListener,
Adam Cohenea0818d2011-09-30 20:06:58 -070062 View.OnLongClickListener, DropTarget, FolderListener, TextView.OnEditorActionListener,
63 View.OnFocusChangeListener {
Adam Cohendf2cc412011-04-27 16:56:57 -070064 private static final String TAG = "Launcher.Folder";
65
Adam Cohen4eac29a2011-07-11 17:53:37 -070066 protected DragController mDragController;
67 protected Launcher mLauncher;
68 protected FolderInfo mInfo;
69
Adam Cohendf2cc412011-04-27 16:56:57 -070070 static final int STATE_NONE = -1;
71 static final int STATE_SMALL = 0;
72 static final int STATE_ANIMATING = 1;
73 static final int STATE_OPEN = 2;
74
75 private int mExpandDuration;
76 protected CellLayout mContent;
Adam Cohenf0f4eda2013-06-06 21:27:03 -070077 private ScrollView mScrollView;
Adam Cohendf2cc412011-04-27 16:56:57 -070078 private final LayoutInflater mInflater;
79 private final IconCache mIconCache;
80 private int mState = STATE_NONE;
Adam Cohenbfbfd262011-06-13 16:55:12 -070081 private static final int REORDER_ANIMATION_DURATION = 230;
Adam Cohenf0f4eda2013-06-06 21:27:03 -070082 private static final int REORDER_DELAY = 250;
Adam Cohenbfbfd262011-06-13 16:55:12 -070083 private static final int ON_EXIT_CLOSE_DELAY = 800;
Adam Cohen2801caf2011-05-13 20:57:39 -070084 private boolean mRearrangeOnClose = false;
85 private FolderIcon mFolderIcon;
86 private int mMaxCountX;
87 private int mMaxCountY;
Adam Cohenf0f4eda2013-06-06 21:27:03 -070088 private int mMaxVisibleX;
89 private int mMaxVisibleY;
90 private int mMaxContentAreaHeight = 0;
Adam Cohen78dc83e2011-11-15 17:10:00 -080091 private int mMaxNumItems;
Adam Cohen7c693212011-05-18 15:26:57 -070092 private ArrayList<View> mItemsInReadingOrder = new ArrayList<View>();
Adam Cohenbadf71e2011-05-26 19:08:29 -070093 private Drawable mIconDrawable;
Adam Cohen7c693212011-05-18 15:26:57 -070094 boolean mItemsInvalidated = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -070095 private ShortcutInfo mCurrentDragInfo;
96 private View mCurrentDragView;
97 boolean mSuppressOnAdd = false;
98 private int[] mTargetCell = new int[2];
99 private int[] mPreviousTargetCell = new int[2];
100 private int[] mEmptyCell = new int[2];
101 private Alarm mReorderAlarm = new Alarm();
102 private Alarm mOnExitAlarm = new Alarm();
Adam Cohen76fc0852011-06-17 13:26:23 -0700103 private int mFolderNameHeight;
Adam Cohen8e776a62011-06-28 18:10:06 -0700104 private Rect mTempRect = new Rect();
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700105 private boolean mDragInProgress = false;
106 private boolean mDeleteFolderOnDropCompleted = false;
107 private boolean mSuppressFolderDeletion = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700108 private boolean mItemAddedBackToSelfViaIcon = false;
Adam Cohenac56cff2011-09-28 20:45:37 -0700109 FolderEditText mFolderName;
Adam Cohen268c4752012-06-06 17:47:33 -0700110 private float mFolderIconPivotX;
111 private float mFolderIconPivotY;
Mindy DelliCarpini2e7471c2013-06-27 08:45:11 -0700112 private long mDragScrollStart;
113
114 /** Interpolator used to scale velocity with touch position, may be null. */
115 private Interpolator mEdgeInterpolator = new AccelerateInterpolator();
Adam Cohen228da5a2011-07-27 22:23:47 -0700116
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700117 private static final int SCROLL_CUT_OFF_AMOUNT = 60;
Mindy DelliCarpini2e7471c2013-06-27 08:45:11 -0700118
119 private final float mDurationToMax = 300f;
120 private final float mMaxVelocity = 100000f/1000f;
121
122 // Aim for this amount of target area to trigger scrolling
123 private static float sScrollBandFactor = -1f;
124 private static float sScrollBandMaxFactor = -1f;
125
126 // Min size for the target area to trigger scrolling
127 private static int sMinScrollBandHeight = -1;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700128
Adam Cohen76fc0852011-06-17 13:26:23 -0700129 private boolean mIsEditingName = false;
130 private InputMethodManager mInputMethodManager;
Adam Cohendf2cc412011-04-27 16:56:57 -0700131
Adam Cohena65beee2011-06-27 21:32:23 -0700132 private static String sDefaultFolderName;
133 private static String sHintText;
134
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700135 private int DRAG_MODE_NONE = 0;
136 private int DRAG_MODE_REORDER = 1;
137 private int DRAG_MODE_SCROLL_UP = 2;
138 private int DRAG_MODE_SCROLL_DOWN = 3;
139 private int mDragMode = DRAG_MODE_NONE;
140
Adam Cohenfb91f302012-06-11 15:45:18 -0700141 private boolean mDestroyed;
142
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143 /**
144 * Used to inflate the Workspace from XML.
145 *
146 * @param context The application's context.
147 * @param attrs The attribtues set containing the Workspace's customization values.
148 */
149 public Folder(Context context, AttributeSet attrs) {
150 super(context, attrs);
151 setAlwaysDrawnWithCacheEnabled(false);
Adam Cohendf2cc412011-04-27 16:56:57 -0700152 mInflater = LayoutInflater.from(context);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400153 mIconCache = (LauncherAppState.getInstance()).getIconCache();
Adam Cohen78dc83e2011-11-15 17:10:00 -0800154
155 Resources res = getResources();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700156 mMaxCountX = mMaxVisibleX = res.getInteger(R.integer.folder_max_count_x);
157 mMaxCountY = mMaxVisibleY = res.getInteger(R.integer.folder_max_count_y);
Adam Cohen78dc83e2011-11-15 17:10:00 -0800158 mMaxNumItems = res.getInteger(R.integer.folder_max_num_items);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700159
160 if (mMaxCountY == -1) {
161 // -2 indicates unlimited
162 mMaxCountY = Integer.MAX_VALUE;
163 mMaxVisibleX = LauncherModel.getCellCountX() + 1;
164 }
165 if (mMaxNumItems == -1) {
166 // -2 indicates unlimited
167 mMaxNumItems = Integer.MAX_VALUE;
168 mMaxVisibleY = LauncherModel.getCellCountY() + 1;
169 }
170 if (mMaxCountX == 0) {
171 mMaxCountX = mMaxVisibleX = LauncherModel.getCellCountX();
172 mMaxVisibleX++;
173 }
174 if (mMaxCountY == 0) {
175 mMaxCountY = mMaxVisibleY = LauncherModel.getCellCountY();
176 mMaxVisibleY++;
177 }
178 if (mMaxNumItems == 0) {
Adam Cohen78dc83e2011-11-15 17:10:00 -0800179 mMaxNumItems = mMaxCountX * mMaxCountY;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700180 if (mMaxNumItems < 0) {
181 mMaxNumItems = Integer.MAX_VALUE;
182 }
Adam Cohen78dc83e2011-11-15 17:10:00 -0800183 }
Adam Cohen76fc0852011-06-17 13:26:23 -0700184
185 mInputMethodManager = (InputMethodManager)
Michael Jurka8b805b12012-04-18 14:23:14 -0700186 getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
Adam Cohen76fc0852011-06-17 13:26:23 -0700187
Adam Cohen76fc0852011-06-17 13:26:23 -0700188 mExpandDuration = res.getInteger(R.integer.config_folderAnimDuration);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700189
190 if (sDefaultFolderName == null) {
191 sDefaultFolderName = res.getString(R.string.folder_name);
192 }
Adam Cohena65beee2011-06-27 21:32:23 -0700193 if (sHintText == null) {
194 sHintText = res.getString(R.string.folder_hint_text);
195 }
Mindy DelliCarpini2e7471c2013-06-27 08:45:11 -0700196 if (sMinScrollBandHeight == -1) {
197 sMinScrollBandHeight = res.getDimensionPixelSize(R.dimen.min_scroll_band_size);
198 }
199 if (sScrollBandFactor == -1f) {
200 sScrollBandFactor = res.getInteger(R.integer.scroll_band_factor)/100f;
201 }
202 if (sScrollBandMaxFactor == -1f) {
203 sScrollBandMaxFactor = res.getInteger(R.integer.scroll_band_max_factor)/100f;
204 }
Adam Cohen4eac29a2011-07-11 17:53:37 -0700205 mLauncher = (Launcher) context;
Adam Cohenac56cff2011-09-28 20:45:37 -0700206 // We need this view to be focusable in touch mode so that when text editing of the folder
207 // name is complete, we have something to focus on, thus hiding the cursor and giving
208 // reliable behvior when clicking the text field (since it will always gain focus on click).
209 setFocusableInTouchMode(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800210 }
211
212 @Override
213 protected void onFinishInflate() {
214 super.onFinishInflate();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700215 mScrollView = (ScrollView) findViewById(R.id.scroll_view);
Adam Cohendf2cc412011-04-27 16:56:57 -0700216 mContent = (CellLayout) findViewById(R.id.folder_content);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700217
218 // Beyond this height, the area scrolls
219 mContent.setGridSize(mMaxVisibleX, mMaxVisibleY);
220 mMaxContentAreaHeight = mContent.getDesiredHeight() - SCROLL_CUT_OFF_AMOUNT;
221
Adam Cohen2801caf2011-05-13 20:57:39 -0700222 mContent.setGridSize(0, 0);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700223 mContent.getShortcutsAndWidgets().setMotionEventSplittingEnabled(false);
Adam Cohen2374abf2013-04-16 14:56:57 -0700224 mContent.setInvertIfRtl(true);
Adam Cohenac56cff2011-09-28 20:45:37 -0700225 mFolderName = (FolderEditText) findViewById(R.id.folder_name);
226 mFolderName.setFolder(this);
Adam Cohenea0818d2011-09-30 20:06:58 -0700227 mFolderName.setOnFocusChangeListener(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700228
229 // We find out how tall the text view wants to be (it is set to wrap_content), so that
230 // we can allocate the appropriate amount of space for it.
231 int measureSpec = MeasureSpec.UNSPECIFIED;
232 mFolderName.measure(measureSpec, measureSpec);
233 mFolderNameHeight = mFolderName.getMeasuredHeight();
234
235 // We disable action mode for now since it messes up the view on phones
236 mFolderName.setCustomSelectionActionModeCallback(mActionModeCallback);
Adam Cohen76fc0852011-06-17 13:26:23 -0700237 mFolderName.setOnEditorActionListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700238 mFolderName.setSelectAllOnFocus(true);
Adam Cohen7a14d0b2011-06-24 11:36:35 -0700239 mFolderName.setInputType(mFolderName.getInputType() |
240 InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800241 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700242
Adam Cohen76fc0852011-06-17 13:26:23 -0700243 private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
244 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
245 return false;
246 }
247
248 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
249 return false;
250 }
251
252 public void onDestroyActionMode(ActionMode mode) {
253 }
254
255 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
256 return false;
257 }
258 };
259
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800260 public void onClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700261 Object tag = v.getTag();
262 if (tag instanceof ShortcutInfo) {
Adam Cohenb5fe60c2013-06-06 22:03:51 -0700263 mLauncher.onClick(v);
Adam Cohendf2cc412011-04-27 16:56:57 -0700264 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800265 }
266
267 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -0700268 // Return if global dragging is not enabled
269 if (!mLauncher.isDraggingEnabled()) return true;
270
Adam Cohendf2cc412011-04-27 16:56:57 -0700271 Object tag = v.getTag();
272 if (tag instanceof ShortcutInfo) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700273 ShortcutInfo item = (ShortcutInfo) tag;
274 if (!v.isInTouchMode()) {
275 return false;
276 }
277
Winson Chung7d7541e2011-09-16 20:14:36 -0700278 mLauncher.dismissFolderCling(null);
279
Adam Cohendf2cc412011-04-27 16:56:57 -0700280 mLauncher.getWorkspace().onDragStartedWithItem(v);
Adam Cohenac8c8762011-07-13 11:15:27 -0700281 mLauncher.getWorkspace().beginDragShared(v, this);
Adam Cohenbadf71e2011-05-26 19:08:29 -0700282 mIconDrawable = ((TextView) v).getCompoundDrawables()[1];
Adam Cohen76078c42011-06-09 15:06:52 -0700283
284 mCurrentDragInfo = item;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700285 mEmptyCell[0] = item.cellX;
286 mEmptyCell[1] = item.cellY;
287 mCurrentDragView = v;
Adam Cohenfc53cd22011-07-20 15:45:11 -0700288
289 mContent.removeView(mCurrentDragView);
290 mInfo.remove(mCurrentDragInfo);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700291 mDragInProgress = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700292 mItemAddedBackToSelfViaIcon = false;
Adam Cohendf2cc412011-04-27 16:56:57 -0700293 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800294 return true;
295 }
296
Adam Cohen76fc0852011-06-17 13:26:23 -0700297 public boolean isEditingName() {
298 return mIsEditingName;
299 }
300
301 public void startEditingFolderName() {
Adam Cohena65beee2011-06-27 21:32:23 -0700302 mFolderName.setHint("");
Adam Cohen76fc0852011-06-17 13:26:23 -0700303 mIsEditingName = true;
304 }
305
306 public void dismissEditingName() {
307 mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
308 doneEditingFolderName(true);
309 }
310
311 public void doneEditingFolderName(boolean commit) {
Adam Cohena65beee2011-06-27 21:32:23 -0700312 mFolderName.setHint(sHintText);
Adam Cohen1df26a32011-08-12 16:15:23 -0700313 // Convert to a string here to ensure that no other state associated with the text field
314 // gets saved.
Adam Cohen3371da02011-10-25 21:38:29 -0700315 String newTitle = mFolderName.getText().toString();
316 mInfo.setTitle(newTitle);
Adam Cohen76fc0852011-06-17 13:26:23 -0700317 LauncherModel.updateItemInDatabase(mLauncher, mInfo);
Adam Cohenac56cff2011-09-28 20:45:37 -0700318
Adam Cohen3371da02011-10-25 21:38:29 -0700319 if (commit) {
320 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700321 String.format(getContext().getString(R.string.folder_renamed), newTitle));
Adam Cohen3371da02011-10-25 21:38:29 -0700322 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700323 // In order to clear the focus from the text field, we set the focus on ourself. This
324 // ensures that every time the field is clicked, focus is gained, giving reliable behavior.
325 requestFocus();
326
Adam Cohene601a432011-07-26 21:51:30 -0700327 Selection.setSelection((Spannable) mFolderName.getText(), 0, 0);
Adam Cohen76fc0852011-06-17 13:26:23 -0700328 mIsEditingName = false;
329 }
330
331 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
332 if (actionId == EditorInfo.IME_ACTION_DONE) {
333 dismissEditingName();
334 return true;
335 }
336 return false;
337 }
338
339 public View getEditTextRegion() {
340 return mFolderName;
341 }
342
Adam Cohenbadf71e2011-05-26 19:08:29 -0700343 public Drawable getDragDrawable() {
344 return mIconDrawable;
345 }
346
Adam Cohen0c872ba2011-05-05 10:34:16 -0700347 /**
348 * We need to handle touch events to prevent them from falling through to the workspace below.
349 */
350 @Override
351 public boolean onTouchEvent(MotionEvent ev) {
352 return true;
353 }
354
Joe Onorato00acb122009-08-04 16:04:30 -0400355 public void setDragController(DragController dragController) {
356 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800357 }
358
Adam Cohen2801caf2011-05-13 20:57:39 -0700359 void setFolderIcon(FolderIcon icon) {
360 mFolderIcon = icon;
361 }
362
Adam Cohen3371da02011-10-25 21:38:29 -0700363 @Override
364 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
365 // When the folder gets focus, we don't want to announce the list of items.
366 return true;
367 }
368
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800369 /**
370 * @return the FolderInfo object associated with this folder
371 */
372 FolderInfo getInfo() {
373 return mInfo;
374 }
375
Adam Cohen3bf84d32012-05-07 20:17:14 -0700376 private class GridComparator implements Comparator<ShortcutInfo> {
377 int mNumCols;
378 public GridComparator(int numCols) {
379 mNumCols = numCols;
380 }
381
382 @Override
383 public int compare(ShortcutInfo lhs, ShortcutInfo rhs) {
384 int lhIndex = lhs.cellY * mNumCols + lhs.cellX;
385 int rhIndex = rhs.cellY * mNumCols + rhs.cellX;
386 return (lhIndex - rhIndex);
387 }
Adam Cohen3bf84d32012-05-07 20:17:14 -0700388 }
389
390 private void placeInReadingOrder(ArrayList<ShortcutInfo> items) {
391 int maxX = 0;
392 int count = items.size();
393 for (int i = 0; i < count; i++) {
394 ShortcutInfo item = items.get(i);
395 if (item.cellX > maxX) {
396 maxX = item.cellX;
397 }
398 }
Adam Cohen691a5792012-05-11 14:27:30 -0700399
400 GridComparator gridComparator = new GridComparator(maxX + 1);
Adam Cohen3bf84d32012-05-07 20:17:14 -0700401 Collections.sort(items, gridComparator);
402 final int countX = mContent.getCountX();
403 for (int i = 0; i < count; i++) {
404 int x = i % countX;
405 int y = i / countX;
406 ShortcutInfo item = items.get(i);
407 item.cellX = x;
408 item.cellY = y;
409 }
410 }
411
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800412 void bind(FolderInfo info) {
413 mInfo = info;
Adam Cohendf2cc412011-04-27 16:56:57 -0700414 ArrayList<ShortcutInfo> children = info.contents;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700415 ArrayList<ShortcutInfo> overflow = new ArrayList<ShortcutInfo>();
Adam Cohen7c693212011-05-18 15:26:57 -0700416 setupContentForNumItems(children.size());
Adam Cohen3bf84d32012-05-07 20:17:14 -0700417 placeInReadingOrder(children);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700418 int count = 0;
Adam Cohendf2cc412011-04-27 16:56:57 -0700419 for (int i = 0; i < children.size(); i++) {
420 ShortcutInfo child = (ShortcutInfo) children.get(i);
Adam Cohenc508b2d2011-06-28 14:41:44 -0700421 if (!createAndAddShortcut(child)) {
422 overflow.add(child);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700423 } else {
424 count++;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700425 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700426 }
Adam Cohenc508b2d2011-06-28 14:41:44 -0700427
Adam Cohen0057bbc2011-08-12 18:30:51 -0700428 // We rearrange the items in case there are any empty gaps
429 setupContentForNumItems(count);
430
Adam Cohenc508b2d2011-06-28 14:41:44 -0700431 // If our folder has too many items we prune them from the list. This is an issue
432 // when upgrading from the old Folders implementation which could contain an unlimited
433 // number of items.
434 for (ShortcutInfo item: overflow) {
435 mInfo.remove(item);
436 LauncherModel.deleteItemFromDatabase(mLauncher, item);
437 }
438
Adam Cohen4dbe6d92011-05-18 17:14:15 -0700439 mItemsInvalidated = true;
Adam Cohenac56cff2011-09-28 20:45:37 -0700440 updateTextViewFocus();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700441 mInfo.addListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700442
Adam Cohenafb01ee2011-06-23 15:38:03 -0700443 if (!sDefaultFolderName.contentEquals(mInfo.title)) {
Adam Cohen4ef610f2011-06-21 22:37:36 -0700444 mFolderName.setText(mInfo.title);
445 } else {
446 mFolderName.setText("");
447 }
Adam Cohen691a5792012-05-11 14:27:30 -0700448 updateItemLocationsInDatabase();
Adam Cohendf2cc412011-04-27 16:56:57 -0700449 }
450
451 /**
452 * Creates a new UserFolder, inflated from R.layout.user_folder.
453 *
454 * @param context The application's context.
455 *
456 * @return A new UserFolder.
457 */
458 static Folder fromXml(Context context) {
459 return (Folder) LayoutInflater.from(context).inflate(R.layout.user_folder, null);
460 }
461
462 /**
463 * This method is intended to make the UserFolder to be visually identical in size and position
464 * to its associated FolderIcon. This allows for a seamless transition into the expanded state.
465 */
466 private void positionAndSizeAsIcon() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700467 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800468 setScaleX(0.8f);
469 setScaleY(0.8f);
470 setAlpha(0f);
Adam Cohendf2cc412011-04-27 16:56:57 -0700471 mState = STATE_SMALL;
472 }
473
474 public void animateOpen() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700475 positionAndSizeAsIcon();
476
Adam Cohen8e776a62011-06-28 18:10:06 -0700477 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen2801caf2011-05-13 20:57:39 -0700478 centerAboutIcon();
Adam Cohen662b5982011-12-13 17:45:21 -0800479 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1);
480 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
481 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
Michael Jurka032e6ba2013-04-22 15:08:42 +0200482 final ObjectAnimator oa =
Michael Jurka2ecf9952012-06-18 12:52:28 -0700483 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohen6441de02011-12-14 14:25:32 -0800484
Adam Cohen2801caf2011-05-13 20:57:39 -0700485 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700486 @Override
487 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700488 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700489 String.format(getContext().getString(R.string.folder_opened),
Adam Cohen3371da02011-10-25 21:38:29 -0700490 mContent.getCountX(), mContent.getCountY()));
Adam Cohendf2cc412011-04-27 16:56:57 -0700491 mState = STATE_ANIMATING;
492 }
493 @Override
494 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700495 mState = STATE_OPEN;
Michael Jurka0121c3e2012-05-31 08:36:04 -0700496 setLayerType(LAYER_TYPE_NONE, null);
Winson Chung7a74ac92011-09-20 17:43:51 -0700497 Cling cling = mLauncher.showFirstRunFoldersCling();
498 if (cling != null) {
499 cling.bringToFront();
500 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700501 setFocusOnFirstChild();
Adam Cohendf2cc412011-04-27 16:56:57 -0700502 }
503 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700504 oa.setDuration(mExpandDuration);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700505 setLayerType(LAYER_TYPE_HARDWARE, null);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100506 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700507 }
508
Adam Cohen3371da02011-10-25 21:38:29 -0700509 private void sendCustomAccessibilityEvent(int type, String text) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700510 AccessibilityManager accessibilityManager = (AccessibilityManager)
511 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
512 if (accessibilityManager.isEnabled()) {
Adam Cohen3371da02011-10-25 21:38:29 -0700513 AccessibilityEvent event = AccessibilityEvent.obtain(type);
514 onInitializeAccessibilityEvent(event);
515 event.getText().add(text);
Michael Jurka8b805b12012-04-18 14:23:14 -0700516 accessibilityManager.sendAccessibilityEvent(event);
Adam Cohen3371da02011-10-25 21:38:29 -0700517 }
518 }
519
Adam Cohenac56cff2011-09-28 20:45:37 -0700520 private void setFocusOnFirstChild() {
521 View firstChild = mContent.getChildAt(0, 0);
522 if (firstChild != null) {
523 firstChild.requestFocus();
524 }
525 }
526
Adam Cohendf2cc412011-04-27 16:56:57 -0700527 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700528 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800529 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
530 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
531 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
Michael Jurka032e6ba2013-04-22 15:08:42 +0200532 final ObjectAnimator oa =
Michael Jurka2ecf9952012-06-18 12:52:28 -0700533 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohendf2cc412011-04-27 16:56:57 -0700534
Adam Cohen2801caf2011-05-13 20:57:39 -0700535 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700536 @Override
537 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700538 onCloseComplete();
Michael Jurka0121c3e2012-05-31 08:36:04 -0700539 setLayerType(LAYER_TYPE_NONE, null);
Adam Cohen2801caf2011-05-13 20:57:39 -0700540 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700541 }
542 @Override
543 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700544 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700545 getContext().getString(R.string.folder_closed));
Adam Cohendf2cc412011-04-27 16:56:57 -0700546 mState = STATE_ANIMATING;
547 }
548 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700549 oa.setDuration(mExpandDuration);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700550 setLayerType(LAYER_TYPE_HARDWARE, null);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100551 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700552 }
553
554 void notifyDataSetChanged() {
555 // recreate all the children if the data set changes under us. We may want to do this more
556 // intelligently (ie just removing the views that should no longer exist)
557 mContent.removeAllViewsInLayout();
558 bind(mInfo);
559 }
560
Adam Cohencb3382b2011-05-24 14:07:08 -0700561 public boolean acceptDrop(DragObject d) {
562 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700563 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700564 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
565 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
566 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700567 }
568
Adam Cohendf2cc412011-04-27 16:56:57 -0700569 protected boolean findAndSetEmptyCells(ShortcutInfo item) {
570 int[] emptyCell = new int[2];
571 if (mContent.findCellForSpan(emptyCell, item.spanX, item.spanY)) {
572 item.cellX = emptyCell[0];
573 item.cellY = emptyCell[1];
Adam Cohendf2cc412011-04-27 16:56:57 -0700574 return true;
575 } else {
576 return false;
577 }
578 }
579
Adam Cohenc508b2d2011-06-28 14:41:44 -0700580 protected boolean createAndAddShortcut(ShortcutInfo item) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700581 final TextView textView =
Adam Cohene87b9242011-06-29 14:01:26 -0700582 (TextView) mInflater.inflate(R.layout.application, this, false);
Adam Cohendf2cc412011-04-27 16:56:57 -0700583 textView.setCompoundDrawablesWithIntrinsicBounds(null,
584 new FastBitmapDrawable(item.getIcon(mIconCache)), null, null);
585 textView.setText(item.title);
586 textView.setTag(item);
587
588 textView.setOnClickListener(this);
589 textView.setOnLongClickListener(this);
590
Adam Cohenc508b2d2011-06-28 14:41:44 -0700591 // We need to check here to verify that the given item's location isn't already occupied
Adam Cohen3bf84d32012-05-07 20:17:14 -0700592 // by another item.
Adam Cohen0057bbc2011-08-12 18:30:51 -0700593 if (mContent.getChildAt(item.cellX, item.cellY) != null || item.cellX < 0 || item.cellY < 0
594 || item.cellX >= mContent.getCountX() || item.cellY >= mContent.getCountY()) {
Adam Cohen3bf84d32012-05-07 20:17:14 -0700595 // This shouldn't happen, log it.
596 Log.e(TAG, "Folder order not properly persisted during bind");
Adam Cohenc508b2d2011-06-28 14:41:44 -0700597 if (!findAndSetEmptyCells(item)) {
598 return false;
599 }
600 }
601
Adam Cohendf2cc412011-04-27 16:56:57 -0700602 CellLayout.LayoutParams lp =
603 new CellLayout.LayoutParams(item.cellX, item.cellY, item.spanX, item.spanY);
604 boolean insert = false;
Adam Cohenac56cff2011-09-28 20:45:37 -0700605 textView.setOnKeyListener(new FolderKeyEventListener());
Adam Cohendf2cc412011-04-27 16:56:57 -0700606 mContent.addViewToCellLayout(textView, insert ? 0 : -1, (int)item.id, lp, true);
Adam Cohenc508b2d2011-06-28 14:41:44 -0700607 return true;
Adam Cohendf2cc412011-04-27 16:56:57 -0700608 }
609
Adam Cohencb3382b2011-05-24 14:07:08 -0700610 public void onDragEnter(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700611 mPreviousTargetCell[0] = -1;
612 mPreviousTargetCell[1] = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700613 mOnExitAlarm.cancelAlarm();
614 }
615
616 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
617 public void onAlarm(Alarm alarm) {
618 realTimeReorder(mEmptyCell, mTargetCell);
619 }
620 };
621
622 boolean readingOrderGreaterThan(int[] v1, int[] v2) {
623 if (v1[1] > v2[1] || (v1[1] == v2[1] && v1[0] > v2[0])) {
624 return true;
625 } else {
626 return false;
627 }
628 }
629
630 private void realTimeReorder(int[] empty, int[] target) {
631 boolean wrap;
632 int startX;
633 int endX;
634 int startY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700635 int delay = 0;
636 float delayAmount = 30;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700637 if (readingOrderGreaterThan(target, empty)) {
638 wrap = empty[0] >= mContent.getCountX() - 1;
639 startY = wrap ? empty[1] + 1 : empty[1];
640 for (int y = startY; y <= target[1]; y++) {
641 startX = y == empty[1] ? empty[0] + 1 : 0;
642 endX = y < target[1] ? mContent.getCountX() - 1 : target[0];
643 for (int x = startX; x <= endX; x++) {
644 View v = mContent.getChildAt(x,y);
645 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen482ed822012-03-02 14:15:13 -0800646 REORDER_ANIMATION_DURATION, delay, true, true)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700647 empty[0] = x;
648 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700649 delay += delayAmount;
650 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700651 }
652 }
653 }
654 } else {
655 wrap = empty[0] == 0;
656 startY = wrap ? empty[1] - 1 : empty[1];
657 for (int y = startY; y >= target[1]; y--) {
658 startX = y == empty[1] ? empty[0] - 1 : mContent.getCountX() - 1;
659 endX = y > target[1] ? 0 : target[0];
660 for (int x = startX; x >= endX; x--) {
661 View v = mContent.getChildAt(x,y);
662 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen482ed822012-03-02 14:15:13 -0800663 REORDER_ANIMATION_DURATION, delay, true, true)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700664 empty[0] = x;
665 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700666 delay += delayAmount;
667 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700668 }
669 }
670 }
671 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700672 }
673
Adam Cohen2374abf2013-04-16 14:56:57 -0700674 public boolean isLayoutRtl() {
675 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
676 }
677
Adam Cohencb3382b2011-05-24 14:07:08 -0700678 public void onDragOver(DragObject d) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700679 int scrollOffset = mScrollView.getScrollY();
Mindy DelliCarpini2e7471c2013-06-27 08:45:11 -0700680 int height = getMeasuredHeight();
681 int scrollBandHeight = Math.min((int) (height * sScrollBandMaxFactor),
682 (Math.max(sMinScrollBandHeight, (int) (height * sScrollBandFactor))));
Adam Cohenbfbfd262011-06-13 16:55:12 -0700683 float[] r = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, d.dragView, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700684 r[0] -= getPaddingLeft();
685 r[1] -= getPaddingTop();
686
687 mTargetCell = mContent.findNearestArea((int) r[0], (int) r[1] + scrollOffset,
688 1, 1, mTargetCell);
Adam Cohenbfbfd262011-06-13 16:55:12 -0700689
Adam Cohen2374abf2013-04-16 14:56:57 -0700690 if (isLayoutRtl()) {
691 mTargetCell[0] = mContent.getCountX() - mTargetCell[0] - 1;
692 }
693
Mindy DelliCarpini2e7471c2013-06-27 08:45:11 -0700694 if (r[1] < scrollBandHeight && mScrollView.getScrollY() > 0) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700695 // Scroll up
696 if (mDragMode != DRAG_MODE_SCROLL_UP) {
697 mDragMode = DRAG_MODE_SCROLL_UP;
Mindy DelliCarpini2e7471c2013-06-27 08:45:11 -0700698 mDragScrollStart = System.currentTimeMillis();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700699 scrollUp();
700 }
701 mReorderAlarm.cancelAlarm();
Mindy DelliCarpini2e7471c2013-06-27 08:45:11 -0700702 } else if (r[1] > (getFolderHeight() - scrollBandHeight) && mScrollView.getScrollY() <
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700703 (mContent.getMeasuredHeight() - mScrollView.getMeasuredHeight())) {
704 if (mDragMode != DRAG_MODE_SCROLL_DOWN) {
705 mDragMode = DRAG_MODE_SCROLL_DOWN;
Mindy DelliCarpini2e7471c2013-06-27 08:45:11 -0700706 mDragScrollStart = System.currentTimeMillis();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700707 scrollDown();
708 }
709 mReorderAlarm.cancelAlarm();
710 } else if (mTargetCell[0] != mPreviousTargetCell[0] || mTargetCell[1] != mPreviousTargetCell[1]) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700711 mReorderAlarm.cancelAlarm();
712 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700713 mReorderAlarm.setAlarm(REORDER_DELAY);
Adam Cohenbfbfd262011-06-13 16:55:12 -0700714 mPreviousTargetCell[0] = mTargetCell[0];
715 mPreviousTargetCell[1] = mTargetCell[1];
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700716 mDragMode = DRAG_MODE_REORDER;
717 } else {
718 mDragMode = DRAG_MODE_NONE;
719 }
720 }
721
722 Runnable mScrollUpRunnable = new Runnable() {
723 @Override
724 public void run() {
725 scrollUp();
726 }
727 };
728
729 Runnable mScrollDownRunnable = new Runnable() {
730 @Override
731 public void run() {
732 scrollDown();
733 }
734 };
735
Mindy DelliCarpini2e7471c2013-06-27 08:45:11 -0700736 private int getVelocity() {
737 float duration = (System.currentTimeMillis() - mDragScrollStart)/mDurationToMax;
738 return (int) Math.min(mMaxVelocity,
739 (int) (mEdgeInterpolator.getInterpolation(duration) * mMaxVelocity));
740 }
741
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700742 private void scrollUp() {
743 if (mDragMode == DRAG_MODE_SCROLL_UP) {
Mindy DelliCarpini2e7471c2013-06-27 08:45:11 -0700744 mScrollView.setScrollY(mScrollView.getScrollY() - getVelocity());
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700745 invalidate();
746 post(mScrollUpRunnable);
747 }
748 }
749
750 private void scrollDown() {
751 if (mDragMode == DRAG_MODE_SCROLL_DOWN) {
Mindy DelliCarpini2e7471c2013-06-27 08:45:11 -0700752 mScrollView.setScrollY(mScrollView.getScrollY() + getVelocity());
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700753 invalidate();
754 post(mScrollDownRunnable);
Adam Cohenbfbfd262011-06-13 16:55:12 -0700755 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700756 }
757
Adam Cohenbfbfd262011-06-13 16:55:12 -0700758 // This is used to compute the visual center of the dragView. The idea is that
759 // the visual center represents the user's interpretation of where the item is, and hence
760 // is the appropriate point to use when determining drop location.
761 private float[] getDragViewVisualCenter(int x, int y, int xOffset, int yOffset,
762 DragView dragView, float[] recycle) {
763 float res[];
764 if (recycle == null) {
765 res = new float[2];
766 } else {
767 res = recycle;
768 }
769
770 // These represent the visual top and left of drag view if a dragRect was provided.
771 // If a dragRect was not provided, then they correspond to the actual view left and
772 // top, as the dragRect is in that case taken to be the entire dragView.
773 // R.dimen.dragViewOffsetY.
774 int left = x - xOffset;
775 int top = y - yOffset;
776
777 // In order to find the visual center, we shift by half the dragRect
778 res[0] = left + dragView.getDragRegion().width() / 2;
779 res[1] = top + dragView.getDragRegion().height() / 2;
780
781 return res;
782 }
783
784 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
785 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700786 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700787 }
788 };
789
Adam Cohen95bb8002011-07-03 23:40:28 -0700790 public void completeDragExit() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700791 mLauncher.closeFolder();
792 mCurrentDragInfo = null;
793 mCurrentDragView = null;
794 mSuppressOnAdd = false;
795 mRearrangeOnClose = true;
796 }
797
Adam Cohencb3382b2011-05-24 14:07:08 -0700798 public void onDragExit(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700799 // We only close the folder if this is a true drag exit, ie. not because a drop
800 // has occurred above the folder.
801 if (!d.dragComplete) {
802 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
803 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
804 }
805 mReorderAlarm.cancelAlarm();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700806 mDragMode = DRAG_MODE_NONE;
Adam Cohen2801caf2011-05-13 20:57:39 -0700807 }
808
Winson Chunga48487a2012-03-20 16:19:37 -0700809 public void onDropCompleted(View target, DragObject d, boolean isFlingToDelete,
810 boolean success) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700811 if (success) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700812 if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700813 replaceFolderWithFinalItem();
814 }
815 } else {
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700816 setupContentForNumItems(getItemCount());
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700817 // The drag failed, we need to return the item to the folder
818 mFolderIcon.onDrop(d);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700819 }
820
821 if (target != this) {
822 if (mOnExitAlarm.alarmPending()) {
823 mOnExitAlarm.cancelAlarm();
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700824 if (!success) {
825 mSuppressFolderDeletion = true;
826 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700827 completeDragExit();
828 }
829 }
830 mDeleteFolderOnDropCompleted = false;
831 mDragInProgress = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700832 mItemAddedBackToSelfViaIcon = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700833 mCurrentDragInfo = null;
834 mCurrentDragView = null;
835 mSuppressOnAdd = false;
Adam Cohen4045eb72011-10-06 11:44:26 -0700836
837 // Reordering may have occured, and we need to save the new item locations. We do this once
838 // at the end to prevent unnecessary database operations.
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700839 updateItemLocationsInDatabaseBatch();
Adam Cohen4045eb72011-10-06 11:44:26 -0700840 }
841
Winson Chunga48487a2012-03-20 16:19:37 -0700842 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800843 public boolean supportsFlingToDelete() {
844 return true;
845 }
846
Winson Chunga48487a2012-03-20 16:19:37 -0700847 public void onFlingToDelete(DragObject d, int x, int y, PointF vec) {
848 // Do nothing
849 }
850
851 @Override
852 public void onFlingToDeleteCompleted() {
853 // Do nothing
854 }
855
Adam Cohen4045eb72011-10-06 11:44:26 -0700856 private void updateItemLocationsInDatabase() {
857 ArrayList<View> list = getItemsInReadingOrder();
858 for (int i = 0; i < list.size(); i++) {
859 View v = list.get(i);
860 ItemInfo info = (ItemInfo) v.getTag();
861 LauncherModel.moveItemInDatabase(mLauncher, info, mInfo.id, 0,
862 info.cellX, info.cellY);
863 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700864 }
Adam Cohen228da5a2011-07-27 22:23:47 -0700865
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700866 private void updateItemLocationsInDatabaseBatch() {
867 ArrayList<View> list = getItemsInReadingOrder();
868 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
869 for (int i = 0; i < list.size(); i++) {
870 View v = list.get(i);
871 ItemInfo info = (ItemInfo) v.getTag();
872 items.add(info);
873 }
874
875 LauncherModel.moveItemsInDatabase(mLauncher, items, mInfo.id, 0);
876 }
877
Adam Cohene25af792013-06-06 23:08:25 -0700878 public void addItemLocationsInDatabase() {
879 ArrayList<View> list = getItemsInReadingOrder();
880 for (int i = 0; i < list.size(); i++) {
881 View v = list.get(i);
882 ItemInfo info = (ItemInfo) v.getTag();
883 LauncherModel.addItemToDatabase(mLauncher, info, mInfo.id, 0,
884 info.cellX, info.cellY, false);
885 }
886 }
887
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700888 public void notifyDrop() {
889 if (mDragInProgress) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700890 mItemAddedBackToSelfViaIcon = true;
Adam Cohen76078c42011-06-09 15:06:52 -0700891 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700892 }
893
894 public boolean isDropEnabled() {
895 return true;
896 }
897
Adam Cohencb3382b2011-05-24 14:07:08 -0700898 public DropTarget getDropTargetDelegate(DragObject d) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700899 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800900 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700901
Adam Cohen4045eb72011-10-06 11:44:26 -0700902 private void setupContentDimensions(int count) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700903 ArrayList<View> list = getItemsInReadingOrder();
904
905 int countX = mContent.getCountX();
906 int countY = mContent.getCountY();
Adam Cohen7c693212011-05-18 15:26:57 -0700907 boolean done = false;
Adam Cohen2801caf2011-05-13 20:57:39 -0700908
Adam Cohen7c693212011-05-18 15:26:57 -0700909 while (!done) {
910 int oldCountX = countX;
911 int oldCountY = countY;
912 if (countX * countY < count) {
913 // Current grid is too small, expand it
Adam Cohen78dc83e2011-11-15 17:10:00 -0800914 if ((countX <= countY || countY == mMaxCountY) && countX < mMaxCountX) {
Adam Cohen7c693212011-05-18 15:26:57 -0700915 countX++;
916 } else if (countY < mMaxCountY) {
917 countY++;
918 }
919 if (countY == 0) countY++;
920 } else if ((countY - 1) * countX >= count && countY >= countX) {
921 countY = Math.max(0, countY - 1);
922 } else if ((countX - 1) * countY >= count) {
923 countX = Math.max(0, countX - 1);
Adam Cohen2801caf2011-05-13 20:57:39 -0700924 }
Adam Cohen7c693212011-05-18 15:26:57 -0700925 done = countX == oldCountX && countY == oldCountY;
Adam Cohen2801caf2011-05-13 20:57:39 -0700926 }
Adam Cohen7c693212011-05-18 15:26:57 -0700927 mContent.setGridSize(countX, countY);
Adam Cohen2801caf2011-05-13 20:57:39 -0700928 arrangeChildren(list);
929 }
930
931 public boolean isFull() {
Adam Cohen78dc83e2011-11-15 17:10:00 -0800932 return getItemCount() >= mMaxNumItems;
Adam Cohen2801caf2011-05-13 20:57:39 -0700933 }
934
935 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700936 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700937
938 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700939 int height = getFolderHeight();
Adam Cohen8e776a62011-06-28 18:10:06 -0700940 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700941
Adam Cohen307fe232012-08-16 17:55:58 -0700942 float scale = parent.getDescendantRectRelativeToSelf(mFolderIcon, mTempRect);
Adam Cohen8e776a62011-06-28 18:10:06 -0700943
Adam Cohen307fe232012-08-16 17:55:58 -0700944 int centerX = (int) (mTempRect.left + mTempRect.width() * scale / 2);
945 int centerY = (int) (mTempRect.top + mTempRect.height() * scale / 2);
Adam Cohen2801caf2011-05-13 20:57:39 -0700946 int centeredLeft = centerX - width / 2;
947 int centeredTop = centerY - height / 2;
948
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800949 int currentPage = mLauncher.getWorkspace().getCurrentPage();
950 // In case the workspace is scrolling, we need to use the final scroll to compute
951 // the folders bounds.
952 mLauncher.getWorkspace().setFinalScrollForPageChange(currentPage);
Adam Cohen35e7e642011-07-17 14:47:18 -0700953 // We first fetch the currently visible CellLayoutChildren
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800954 CellLayout currentLayout = (CellLayout) mLauncher.getWorkspace().getChildAt(currentPage);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700955 ShortcutAndWidgetContainer boundingLayout = currentLayout.getShortcutsAndWidgets();
Adam Cohen35e7e642011-07-17 14:47:18 -0700956 Rect bounds = new Rect();
957 parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800958 // We reset the workspaces scroll
959 mLauncher.getWorkspace().resetFinalScrollForPageChange(currentPage);
Adam Cohen2801caf2011-05-13 20:57:39 -0700960
Adam Cohen35e7e642011-07-17 14:47:18 -0700961 // We need to bound the folder to the currently visible CellLayoutChildren
962 int left = Math.min(Math.max(bounds.left, centeredLeft),
963 bounds.left + bounds.width() - width);
964 int top = Math.min(Math.max(bounds.top, centeredTop),
965 bounds.top + bounds.height() - height);
966 // If the folder doesn't fit within the bounds, center it about the desired bounds
967 if (width >= bounds.width()) {
968 left = bounds.left + (bounds.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700969 }
Adam Cohen35e7e642011-07-17 14:47:18 -0700970 if (height >= bounds.height()) {
971 top = bounds.top + (bounds.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700972 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700973
974 int folderPivotX = width / 2 + (centeredLeft - left);
975 int folderPivotY = height / 2 + (centeredTop - top);
976 setPivotX(folderPivotX);
977 setPivotY(folderPivotY);
Adam Cohen268c4752012-06-06 17:47:33 -0700978 mFolderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700979 (1.0f * folderPivotX / width));
Adam Cohen268c4752012-06-06 17:47:33 -0700980 mFolderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700981 (1.0f * folderPivotY / height));
Adam Cohen3bf84d32012-05-07 20:17:14 -0700982
Adam Cohen662b5982011-12-13 17:45:21 -0800983 lp.width = width;
984 lp.height = height;
985 lp.x = left;
986 lp.y = top;
Adam Cohen2801caf2011-05-13 20:57:39 -0700987 }
988
Adam Cohen268c4752012-06-06 17:47:33 -0700989 float getPivotXForIconAnimation() {
990 return mFolderIconPivotX;
991 }
992 float getPivotYForIconAnimation() {
993 return mFolderIconPivotY;
994 }
995
Adam Cohen2801caf2011-05-13 20:57:39 -0700996 private void setupContentForNumItems(int count) {
Adam Cohen4045eb72011-10-06 11:44:26 -0700997 setupContentDimensions(count);
Adam Cohen2801caf2011-05-13 20:57:39 -0700998
Adam Cohen8e776a62011-06-28 18:10:06 -0700999 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -07001000 if (lp == null) {
Adam Cohen8e776a62011-06-28 18:10:06 -07001001 lp = new DragLayer.LayoutParams(0, 0);
1002 lp.customPosition = true;
Adam Cohen2801caf2011-05-13 20:57:39 -07001003 setLayoutParams(lp);
1004 }
1005 centerAboutIcon();
1006 }
1007
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001008 private int getFolderHeight() {
1009 int contentAreaHeight = mContent.getDesiredHeight();
1010 if (contentAreaHeight >= mMaxContentAreaHeight) {
1011 // Subtract a bit so the user can see that it's scrollable.
1012 contentAreaHeight = mMaxContentAreaHeight;
1013 }
1014 int height = getPaddingTop() + getPaddingBottom() + contentAreaHeight
Adam Cohenf4bb1cd2011-07-22 14:36:03 -07001015 + mFolderNameHeight;
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001016 return height;
1017 }
Adam Cohen234c4cd2011-07-17 21:03:04 -07001018
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001019 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Adam Cohen234c4cd2011-07-17 21:03:04 -07001020
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001021 int contentAreaHeight = mContent.getDesiredHeight();
1022 if (contentAreaHeight >= mMaxContentAreaHeight) {
1023 // Subtract a bit so the user can see that it's scrollable.
1024 contentAreaHeight = mMaxContentAreaHeight;
1025 }
1026
1027 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
1028 int height = getFolderHeight();
1029 int contentAreaWidthSpec = MeasureSpec.makeMeasureSpec(mContent.getDesiredWidth(),
1030 MeasureSpec.EXACTLY);
1031 int contentAreaHeightSpec = MeasureSpec.makeMeasureSpec(contentAreaHeight,
1032 MeasureSpec.EXACTLY);
1033
1034 mContent.setFixedSize(mContent.getDesiredWidth(), mContent.getDesiredHeight());
1035 mScrollView.measure(contentAreaWidthSpec, contentAreaHeightSpec);
1036 mFolderName.measure(contentAreaWidthSpec,
Adam Cohen234c4cd2011-07-17 21:03:04 -07001037 MeasureSpec.makeMeasureSpec(mFolderNameHeight, MeasureSpec.EXACTLY));
1038 setMeasuredDimension(width, height);
1039 }
1040
Adam Cohen2801caf2011-05-13 20:57:39 -07001041 private void arrangeChildren(ArrayList<View> list) {
1042 int[] vacant = new int[2];
1043 if (list == null) {
1044 list = getItemsInReadingOrder();
1045 }
1046 mContent.removeAllViews();
1047
1048 for (int i = 0; i < list.size(); i++) {
1049 View v = list.get(i);
1050 mContent.getVacantCell(vacant, 1, 1);
1051 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) v.getLayoutParams();
1052 lp.cellX = vacant[0];
1053 lp.cellY = vacant[1];
1054 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohen2792a332011-09-26 21:09:47 -07001055 if (info.cellX != vacant[0] || info.cellY != vacant[1]) {
1056 info.cellX = vacant[0];
1057 info.cellY = vacant[1];
1058 LauncherModel.addOrMoveItemInDatabase(mLauncher, info, mInfo.id, 0,
1059 info.cellX, info.cellY);
1060 }
Adam Cohen2801caf2011-05-13 20:57:39 -07001061 boolean insert = false;
1062 mContent.addViewToCellLayout(v, insert ? 0 : -1, (int)info.id, lp, true);
1063 }
Adam Cohen7c693212011-05-18 15:26:57 -07001064 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -07001065 }
1066
Adam Cohena9cf38f2011-05-02 15:36:58 -07001067 public int getItemCount() {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001068 return mContent.getShortcutsAndWidgets().getChildCount();
Adam Cohena9cf38f2011-05-02 15:36:58 -07001069 }
1070
1071 public View getItemAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001072 return mContent.getShortcutsAndWidgets().getChildAt(index);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001073 }
1074
Adam Cohen2801caf2011-05-13 20:57:39 -07001075 private void onCloseComplete() {
Adam Cohen05e0f402011-08-01 12:12:49 -07001076 DragLayer parent = (DragLayer) getParent();
Michael Jurka5649c282012-06-18 10:33:21 -07001077 if (parent != null) {
1078 parent.removeView(this);
1079 }
Adam Cohen4554ee12011-08-03 16:13:21 -07001080 mDragController.removeDropTarget((DropTarget) this);
Adam Cohen05e0f402011-08-01 12:12:49 -07001081 clearFocus();
Adam Cohenac56cff2011-09-28 20:45:37 -07001082 mFolderIcon.requestFocus();
Adam Cohen05e0f402011-08-01 12:12:49 -07001083
Adam Cohen2801caf2011-05-13 20:57:39 -07001084 if (mRearrangeOnClose) {
1085 setupContentForNumItems(getItemCount());
1086 mRearrangeOnClose = false;
1087 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001088 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001089 if (!mDragInProgress && !mSuppressFolderDeletion) {
1090 replaceFolderWithFinalItem();
1091 } else if (mDragInProgress) {
1092 mDeleteFolderOnDropCompleted = true;
1093 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001094 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001095 mSuppressFolderDeletion = false;
Adam Cohenafb01ee2011-06-23 15:38:03 -07001096 }
1097
1098 private void replaceFolderWithFinalItem() {
Adam Cohenafb01ee2011-06-23 15:38:03 -07001099 // Add the last remaining child to the workspace in place of the folder
Adam Cohenfb91f302012-06-11 15:45:18 -07001100 Runnable onCompleteRunnable = new Runnable() {
1101 @Override
1102 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -07001103 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screenId);
Adam Cohenafb01ee2011-06-23 15:38:03 -07001104
Adam Cohenc5e63f32012-07-12 16:16:57 -07001105 View child = null;
Adam Cohenfb91f302012-06-11 15:45:18 -07001106 // Move the item from the folder to the workspace, in the position of the folder
1107 if (getItemCount() == 1) {
1108 ShortcutInfo finalItem = mInfo.contents.get(0);
Adam Cohenc5e63f32012-07-12 16:16:57 -07001109 child = mLauncher.createShortcut(R.layout.application, cellLayout,
Adam Cohenfb91f302012-06-11 15:45:18 -07001110 finalItem);
1111 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -07001112 mInfo.screenId, mInfo.cellX, mInfo.cellY);
Adam Cohenfb91f302012-06-11 15:45:18 -07001113 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001114 if (getItemCount() <= 1) {
1115 // Remove the folder
1116 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
1117 cellLayout.removeView(mFolderIcon);
1118 if (mFolderIcon instanceof DropTarget) {
1119 mDragController.removeDropTarget((DropTarget) mFolderIcon);
1120 }
1121 mLauncher.removeFolder(mInfo);
1122 }
Adam Cohenc5e63f32012-07-12 16:16:57 -07001123 // We add the child after removing the folder to prevent both from existing at
1124 // the same time in the CellLayout.
1125 if (child != null) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001126 mLauncher.getWorkspace().addInScreen(child, mInfo.container, mInfo.screenId,
Adam Cohenc5e63f32012-07-12 16:16:57 -07001127 mInfo.cellX, mInfo.cellY, mInfo.spanX, mInfo.spanY);
1128 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001129 }
1130 };
1131 View finalChild = getItemAt(0);
1132 if (finalChild != null) {
1133 mFolderIcon.performDestroyAnimation(finalChild, onCompleteRunnable);
Adam Cohenafb01ee2011-06-23 15:38:03 -07001134 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001135 mDestroyed = true;
1136 }
1137
1138 boolean isDestroyed() {
1139 return mDestroyed;
Adam Cohen2801caf2011-05-13 20:57:39 -07001140 }
1141
Adam Cohenac56cff2011-09-28 20:45:37 -07001142 // This method keeps track of the last item in the folder for the purposes
1143 // of keyboard focus
1144 private void updateTextViewFocus() {
1145 View lastChild = getItemAt(getItemCount() - 1);
1146 getItemAt(getItemCount() - 1);
1147 if (lastChild != null) {
1148 mFolderName.setNextFocusDownId(lastChild.getId());
1149 mFolderName.setNextFocusRightId(lastChild.getId());
1150 mFolderName.setNextFocusLeftId(lastChild.getId());
1151 mFolderName.setNextFocusUpId(lastChild.getId());
1152 }
1153 }
1154
Adam Cohenbfbfd262011-06-13 16:55:12 -07001155 public void onDrop(DragObject d) {
1156 ShortcutInfo item;
1157 if (d.dragInfo instanceof ApplicationInfo) {
1158 // Came from all apps -- make a copy
1159 item = ((ApplicationInfo) d.dragInfo).makeShortcut();
1160 item.spanX = 1;
1161 item.spanY = 1;
1162 } else {
1163 item = (ShortcutInfo) d.dragInfo;
1164 }
Adam Cohen05e0f402011-08-01 12:12:49 -07001165 // Dragged from self onto self, currently this is the only path possible, however
1166 // we keep this as a distinct code path.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001167 if (item == mCurrentDragInfo) {
1168 ShortcutInfo si = (ShortcutInfo) mCurrentDragView.getTag();
1169 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) mCurrentDragView.getLayoutParams();
1170 si.cellX = lp.cellX = mEmptyCell[0];
1171 si.cellX = lp.cellY = mEmptyCell[1];
1172 mContent.addViewToCellLayout(mCurrentDragView, -1, (int)item.id, lp, true);
Adam Cohenfc53cd22011-07-20 15:45:11 -07001173 if (d.dragView.hasDrawn()) {
1174 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, mCurrentDragView);
1175 } else {
Winson Chung2c4cf412012-05-08 14:03:21 -07001176 d.deferDragViewCleanupPostAnimation = false;
Adam Cohenfc53cd22011-07-20 15:45:11 -07001177 mCurrentDragView.setVisibility(VISIBLE);
1178 }
Adam Cohene9166b22011-07-08 17:11:11 -07001179 mItemsInvalidated = true;
Adam Cohen4045eb72011-10-06 11:44:26 -07001180 setupContentDimensions(getItemCount());
Adam Cohen716b51e2011-06-30 12:09:54 -07001181 mSuppressOnAdd = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001182 }
1183 mInfo.add(item);
1184 }
1185
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001186 // This is used so the item doesn't immediately appear in the folder when added. In one case
1187 // we need to create the illusion that the item isn't added back to the folder yet, to
1188 // to correspond to the animation of the icon back into the folder. This is
1189 public void hideItem(ShortcutInfo info) {
1190 View v = getViewForInfo(info);
1191 v.setVisibility(INVISIBLE);
1192 }
1193 public void showItem(ShortcutInfo info) {
1194 View v = getViewForInfo(info);
1195 v.setVisibility(VISIBLE);
1196 }
1197
Adam Cohenbfbfd262011-06-13 16:55:12 -07001198 public void onAdd(ShortcutInfo item) {
1199 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001200 // If the item was dropped onto this open folder, we have done the work associated
1201 // with adding the item to the folder, as indicated by mSuppressOnAdd being set
Adam Cohenbfbfd262011-06-13 16:55:12 -07001202 if (mSuppressOnAdd) return;
1203 if (!findAndSetEmptyCells(item)) {
1204 // The current layout is full, can we expand it?
1205 setupContentForNumItems(getItemCount() + 1);
1206 findAndSetEmptyCells(item);
1207 }
1208 createAndAddShortcut(item);
1209 LauncherModel.addOrMoveItemInDatabase(
1210 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
1211 }
1212
Adam Cohena9cf38f2011-05-02 15:36:58 -07001213 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -07001214 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001215 // If this item is being dragged from this open folder, we have already handled
1216 // the work associated with removing the item, so we don't have to do anything here.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001217 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001218 View v = getViewForInfo(item);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001219 mContent.removeView(v);
Adam Cohen2801caf2011-05-13 20:57:39 -07001220 if (mState == STATE_ANIMATING) {
1221 mRearrangeOnClose = true;
1222 } else {
1223 setupContentForNumItems(getItemCount());
1224 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001225 if (getItemCount() <= 1) {
1226 replaceFolderWithFinalItem();
1227 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001228 }
Adam Cohen7c693212011-05-18 15:26:57 -07001229
Adam Cohendf1e4e82011-06-24 15:57:39 -07001230 private View getViewForInfo(ShortcutInfo item) {
1231 for (int j = 0; j < mContent.getCountY(); j++) {
1232 for (int i = 0; i < mContent.getCountX(); i++) {
1233 View v = mContent.getChildAt(i, j);
1234 if (v.getTag() == item) {
1235 return v;
1236 }
1237 }
1238 }
1239 return null;
1240 }
1241
Adam Cohen76078c42011-06-09 15:06:52 -07001242 public void onItemsChanged() {
Adam Cohenac56cff2011-09-28 20:45:37 -07001243 updateTextViewFocus();
Adam Cohen76078c42011-06-09 15:06:52 -07001244 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001245
Adam Cohen76fc0852011-06-17 13:26:23 -07001246 public void onTitleChanged(CharSequence title) {
1247 }
Adam Cohen76078c42011-06-09 15:06:52 -07001248
Adam Cohen7c693212011-05-18 15:26:57 -07001249 public ArrayList<View> getItemsInReadingOrder() {
1250 if (mItemsInvalidated) {
1251 mItemsInReadingOrder.clear();
1252 for (int j = 0; j < mContent.getCountY(); j++) {
1253 for (int i = 0; i < mContent.getCountX(); i++) {
1254 View v = mContent.getChildAt(i, j);
1255 if (v != null) {
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001256 mItemsInReadingOrder.add(v);
Adam Cohen7c693212011-05-18 15:26:57 -07001257 }
1258 }
1259 }
1260 mItemsInvalidated = false;
1261 }
1262 return mItemsInReadingOrder;
1263 }
Adam Cohen8dfcba42011-07-07 16:38:18 -07001264
1265 public void getLocationInDragLayer(int[] loc) {
1266 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
1267 }
Adam Cohenea0818d2011-09-30 20:06:58 -07001268
1269 public void onFocusChange(View v, boolean hasFocus) {
1270 if (v == mFolderName && hasFocus) {
1271 startEditingFolderName();
1272 }
1273 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001274}