blob: 786d4a25709ef5e992046a4b1f5394e11100aeda [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 Cohenc4fe9ea2014-08-18 18:54:10 -070021import android.animation.AnimatorSet;
Adam Cohendf2cc412011-04-27 16:56:57 -070022import android.animation.ObjectAnimator;
23import android.animation.PropertyValuesHolder;
Sunny Goyalc46bfef2015-01-05 12:40:08 -080024import android.annotation.TargetApi;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080025import android.content.Context;
Adam Cohen76fc0852011-06-17 13:26:23 -070026import android.content.res.Resources;
Winson Chung043f2af2012-03-01 16:09:54 -080027import android.graphics.PointF;
Romain Guyfb5411e2010-02-24 10:04:17 -080028import android.graphics.Rect;
Sunny Goyalc46bfef2015-01-05 12:40:08 -080029import android.os.Build;
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 Cohenc4fe9ea2014-08-18 18:54:10 -070044import android.view.animation.AccelerateInterpolator;
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.FolderInfo.FolderListener;
Romain Guyedcce092010-03-04 13:03:17 -080052
Adam Cohenc0dcf592011-06-01 15:30:43 -070053import java.util.ArrayList;
Adam Cohen3bf84d32012-05-07 20:17:14 -070054import java.util.Collections;
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;
Adam Cohenc4fe9ea2014-08-18 18:54:10 -070074 private int mMaterialExpandDuration;
75 private int mMaterialExpandStagger;
Adam Cohendf2cc412011-04-27 16:56:57 -070076 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 Cohen5d518fa2013-12-05 14:16:23 -080083 private static final int ON_EXIT_CLOSE_DELAY = 400;
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 Cohen78dc83e2011-11-15 17:10:00 -080088 private int mMaxNumItems;
Adam Cohen7c693212011-05-18 15:26:57 -070089 private ArrayList<View> mItemsInReadingOrder = new ArrayList<View>();
90 boolean mItemsInvalidated = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -070091 private ShortcutInfo mCurrentDragInfo;
92 private View mCurrentDragView;
Jorim Jaggi55bd9722014-01-16 15:30:42 -080093 private boolean mIsExternalDrag;
Adam Cohenbfbfd262011-06-13 16:55:12 -070094 boolean mSuppressOnAdd = false;
95 private int[] mTargetCell = new int[2];
96 private int[] mPreviousTargetCell = new int[2];
97 private int[] mEmptyCell = new int[2];
98 private Alarm mReorderAlarm = new Alarm();
99 private Alarm mOnExitAlarm = new Alarm();
Adam Cohen76fc0852011-06-17 13:26:23 -0700100 private int mFolderNameHeight;
Adam Cohen8e776a62011-06-28 18:10:06 -0700101 private Rect mTempRect = new Rect();
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700102 private boolean mDragInProgress = false;
103 private boolean mDeleteFolderOnDropCompleted = false;
104 private boolean mSuppressFolderDeletion = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700105 private boolean mItemAddedBackToSelfViaIcon = false;
Adam Cohenac56cff2011-09-28 20:45:37 -0700106 FolderEditText mFolderName;
Adam Cohen268c4752012-06-06 17:47:33 -0700107 private float mFolderIconPivotX;
108 private float mFolderIconPivotY;
Adam Cohen228da5a2011-07-27 22:23:47 -0700109
Adam Cohen76fc0852011-06-17 13:26:23 -0700110 private boolean mIsEditingName = false;
111 private InputMethodManager mInputMethodManager;
Adam Cohendf2cc412011-04-27 16:56:57 -0700112
Adam Cohena65beee2011-06-27 21:32:23 -0700113 private static String sDefaultFolderName;
114 private static String sHintText;
115
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700116 private FocusIndicatorView mFocusIndicatorHandler;
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
Michael Jurka1e2f4652013-07-08 18:03:46 -0700125 private Runnable mDeferredAction;
126 private boolean mDeferDropAfterUninstall;
127 private boolean mUninstallSuccessful;
128
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129 /**
130 * Used to inflate the Workspace from XML.
131 *
132 * @param context The application's context.
133 * @param attrs The attribtues set containing the Workspace's customization values.
134 */
135 public Folder(Context context, AttributeSet attrs) {
136 super(context, attrs);
Winson Chung5f8afe62013-08-12 16:19:28 -0700137
138 LauncherAppState app = LauncherAppState.getInstance();
139 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140 setAlwaysDrawnWithCacheEnabled(false);
Adam Cohendf2cc412011-04-27 16:56:57 -0700141 mInflater = LayoutInflater.from(context);
Winson Chung5f8afe62013-08-12 16:19:28 -0700142 mIconCache = app.getIconCache();
Adam Cohen78dc83e2011-11-15 17:10:00 -0800143
144 Resources res = getResources();
Michael Jurka414300a2013-08-27 15:42:35 +0200145 mMaxCountX = (int) grid.numColumns;
Nilesh Agrawal5f7099a2014-01-02 15:54:57 -0800146 // Allow scrolling folders when DISABLE_ALL_APPS is true.
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800147 if (LauncherAppState.isDisableAllApps()) {
Nilesh Agrawal5f7099a2014-01-02 15:54:57 -0800148 mMaxCountY = mMaxNumItems = Integer.MAX_VALUE;
149 } else {
150 mMaxCountY = (int) grid.numRows;
151 mMaxNumItems = mMaxCountX * mMaxCountY;
152 }
Adam Cohen76fc0852011-06-17 13:26:23 -0700153
154 mInputMethodManager = (InputMethodManager)
Michael Jurka8b805b12012-04-18 14:23:14 -0700155 getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
Adam Cohen76fc0852011-06-17 13:26:23 -0700156
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700157 mExpandDuration = res.getInteger(R.integer.config_folderExpandDuration);
158 mMaterialExpandDuration = res.getInteger(R.integer.config_materialFolderExpandDuration);
159 mMaterialExpandStagger = res.getInteger(R.integer.config_materialFolderExpandStagger);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700160
161 if (sDefaultFolderName == null) {
162 sDefaultFolderName = res.getString(R.string.folder_name);
163 }
Adam Cohena65beee2011-06-27 21:32:23 -0700164 if (sHintText == null) {
165 sHintText = res.getString(R.string.folder_hint_text);
166 }
Adam Cohen4eac29a2011-07-11 17:53:37 -0700167 mLauncher = (Launcher) context;
Adam Cohenac56cff2011-09-28 20:45:37 -0700168 // We need this view to be focusable in touch mode so that when text editing of the folder
169 // name is complete, we have something to focus on, thus hiding the cursor and giving
170 // reliable behvior when clicking the text field (since it will always gain focus on click).
171 setFocusableInTouchMode(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172 }
173
174 @Override
175 protected void onFinishInflate() {
176 super.onFinishInflate();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700177 mScrollView = (ScrollView) findViewById(R.id.scroll_view);
Adam Cohendf2cc412011-04-27 16:56:57 -0700178 mContent = (CellLayout) findViewById(R.id.folder_content);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700179
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700180 mFocusIndicatorHandler = new FocusIndicatorView(getContext());
181 mContent.addView(mFocusIndicatorHandler, 0);
182 mFocusIndicatorHandler.getLayoutParams().height = FocusIndicatorView.DEFAULT_LAYOUT_SIZE;
183 mFocusIndicatorHandler.getLayoutParams().width = FocusIndicatorView.DEFAULT_LAYOUT_SIZE;
184
Winson Chung5f8afe62013-08-12 16:19:28 -0700185 LauncherAppState app = LauncherAppState.getInstance();
186 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
187
Winson Chung5f8afe62013-08-12 16:19:28 -0700188 mContent.setCellDimensions(grid.folderCellWidthPx, grid.folderCellHeightPx);
Adam Cohen2801caf2011-05-13 20:57:39 -0700189 mContent.setGridSize(0, 0);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700190 mContent.getShortcutsAndWidgets().setMotionEventSplittingEnabled(false);
Adam Cohen2374abf2013-04-16 14:56:57 -0700191 mContent.setInvertIfRtl(true);
Adam Cohenac56cff2011-09-28 20:45:37 -0700192 mFolderName = (FolderEditText) findViewById(R.id.folder_name);
193 mFolderName.setFolder(this);
Adam Cohenea0818d2011-09-30 20:06:58 -0700194 mFolderName.setOnFocusChangeListener(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700195
196 // We find out how tall the text view wants to be (it is set to wrap_content), so that
197 // we can allocate the appropriate amount of space for it.
198 int measureSpec = MeasureSpec.UNSPECIFIED;
199 mFolderName.measure(measureSpec, measureSpec);
200 mFolderNameHeight = mFolderName.getMeasuredHeight();
201
202 // We disable action mode for now since it messes up the view on phones
203 mFolderName.setCustomSelectionActionModeCallback(mActionModeCallback);
Adam Cohen76fc0852011-06-17 13:26:23 -0700204 mFolderName.setOnEditorActionListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700205 mFolderName.setSelectAllOnFocus(true);
Adam Cohen7a14d0b2011-06-24 11:36:35 -0700206 mFolderName.setInputType(mFolderName.getInputType() |
207 InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800208 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700209
Adam Cohen76fc0852011-06-17 13:26:23 -0700210 private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
211 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
212 return false;
213 }
214
215 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
216 return false;
217 }
218
219 public void onDestroyActionMode(ActionMode mode) {
220 }
221
222 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
223 return false;
224 }
225 };
226
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800227 public void onClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700228 Object tag = v.getTag();
229 if (tag instanceof ShortcutInfo) {
Adam Cohenb5fe60c2013-06-06 22:03:51 -0700230 mLauncher.onClick(v);
Adam Cohendf2cc412011-04-27 16:56:57 -0700231 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800232 }
233
234 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -0700235 // Return if global dragging is not enabled
236 if (!mLauncher.isDraggingEnabled()) return true;
237
Adam Cohendf2cc412011-04-27 16:56:57 -0700238 Object tag = v.getTag();
239 if (tag instanceof ShortcutInfo) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700240 ShortcutInfo item = (ShortcutInfo) tag;
241 if (!v.isInTouchMode()) {
242 return false;
243 }
244
Adam Cohenac8c8762011-07-13 11:15:27 -0700245 mLauncher.getWorkspace().beginDragShared(v, this);
Adam Cohen76078c42011-06-09 15:06:52 -0700246
247 mCurrentDragInfo = item;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700248 mEmptyCell[0] = item.cellX;
249 mEmptyCell[1] = item.cellY;
250 mCurrentDragView = v;
Adam Cohenfc53cd22011-07-20 15:45:11 -0700251
252 mContent.removeView(mCurrentDragView);
253 mInfo.remove(mCurrentDragInfo);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700254 mDragInProgress = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700255 mItemAddedBackToSelfViaIcon = false;
Adam Cohendf2cc412011-04-27 16:56:57 -0700256 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800257 return true;
258 }
259
Adam Cohen76fc0852011-06-17 13:26:23 -0700260 public boolean isEditingName() {
261 return mIsEditingName;
262 }
263
264 public void startEditingFolderName() {
Adam Cohena65beee2011-06-27 21:32:23 -0700265 mFolderName.setHint("");
Adam Cohen76fc0852011-06-17 13:26:23 -0700266 mIsEditingName = true;
267 }
268
269 public void dismissEditingName() {
270 mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
271 doneEditingFolderName(true);
272 }
273
274 public void doneEditingFolderName(boolean commit) {
Adam Cohena65beee2011-06-27 21:32:23 -0700275 mFolderName.setHint(sHintText);
Adam Cohen1df26a32011-08-12 16:15:23 -0700276 // Convert to a string here to ensure that no other state associated with the text field
277 // gets saved.
Adam Cohen3371da02011-10-25 21:38:29 -0700278 String newTitle = mFolderName.getText().toString();
279 mInfo.setTitle(newTitle);
Adam Cohen76fc0852011-06-17 13:26:23 -0700280 LauncherModel.updateItemInDatabase(mLauncher, mInfo);
Adam Cohenac56cff2011-09-28 20:45:37 -0700281
Adam Cohen3371da02011-10-25 21:38:29 -0700282 if (commit) {
283 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700284 String.format(getContext().getString(R.string.folder_renamed), newTitle));
Adam Cohen3371da02011-10-25 21:38:29 -0700285 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700286 // In order to clear the focus from the text field, we set the focus on ourself. This
287 // ensures that every time the field is clicked, focus is gained, giving reliable behavior.
288 requestFocus();
289
Adam Cohene601a432011-07-26 21:51:30 -0700290 Selection.setSelection((Spannable) mFolderName.getText(), 0, 0);
Adam Cohen76fc0852011-06-17 13:26:23 -0700291 mIsEditingName = false;
292 }
293
294 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
295 if (actionId == EditorInfo.IME_ACTION_DONE) {
296 dismissEditingName();
297 return true;
298 }
299 return false;
300 }
301
302 public View getEditTextRegion() {
303 return mFolderName;
304 }
305
Anjali Koppalf05545f2014-03-10 19:18:43 -0700306 public CellLayout getContent() {
307 return mContent;
308 }
309
Adam Cohen0c872ba2011-05-05 10:34:16 -0700310 /**
311 * We need to handle touch events to prevent them from falling through to the workspace below.
312 */
313 @Override
314 public boolean onTouchEvent(MotionEvent ev) {
315 return true;
316 }
317
Joe Onorato00acb122009-08-04 16:04:30 -0400318 public void setDragController(DragController dragController) {
319 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800320 }
321
Adam Cohen2801caf2011-05-13 20:57:39 -0700322 void setFolderIcon(FolderIcon icon) {
323 mFolderIcon = icon;
324 }
325
Adam Cohen3371da02011-10-25 21:38:29 -0700326 @Override
327 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
328 // When the folder gets focus, we don't want to announce the list of items.
329 return true;
330 }
331
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800332 /**
333 * @return the FolderInfo object associated with this folder
334 */
335 FolderInfo getInfo() {
336 return mInfo;
337 }
338
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800339 void bind(FolderInfo info) {
340 mInfo = info;
Adam Cohendf2cc412011-04-27 16:56:57 -0700341 ArrayList<ShortcutInfo> children = info.contents;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700342 ArrayList<ShortcutInfo> overflow = new ArrayList<ShortcutInfo>();
Sunny Goyal08f72612015-01-05 13:41:43 -0800343
344 final int totalChildren = children.size();
345 setupContentForNumItems(totalChildren);
346
347 // Arrange children in the grid based on the rank.
348 Collections.sort(children, Utilities.RANK_COMPARATOR);
349 final int countX = mContent.getCountX();
350
351 int visibleChildren = 0;
Adam Cohendf2cc412011-04-27 16:56:57 -0700352 for (int i = 0; i < children.size(); i++) {
Sunny Goyal08f72612015-01-05 13:41:43 -0800353 ShortcutInfo child = children.get(i);
354 child.cellX = i % countX;
355 child.cellY = i / countX;
356
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800357 if (createAndAddShortcut(child) == null) {
Adam Cohenc508b2d2011-06-28 14:41:44 -0700358 overflow.add(child);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700359 } else {
Sunny Goyal08f72612015-01-05 13:41:43 -0800360 visibleChildren++;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700361 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700362 }
Adam Cohenc508b2d2011-06-28 14:41:44 -0700363
Adam Cohen0057bbc2011-08-12 18:30:51 -0700364 // We rearrange the items in case there are any empty gaps
Sunny Goyal08f72612015-01-05 13:41:43 -0800365 setupContentForNumItems(visibleChildren);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700366
Jason Monk4ff73882014-04-24 16:48:07 -0400367 // If our folder has too many items we prune them from the list. This is an issue
Adam Cohenc508b2d2011-06-28 14:41:44 -0700368 // when upgrading from the old Folders implementation which could contain an unlimited
369 // number of items.
370 for (ShortcutInfo item: overflow) {
371 mInfo.remove(item);
372 LauncherModel.deleteItemFromDatabase(mLauncher, item);
373 }
374
Adam Cohen4dbe6d92011-05-18 17:14:15 -0700375 mItemsInvalidated = true;
Adam Cohenac56cff2011-09-28 20:45:37 -0700376 updateTextViewFocus();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700377 mInfo.addListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700378
Adam Cohenafb01ee2011-06-23 15:38:03 -0700379 if (!sDefaultFolderName.contentEquals(mInfo.title)) {
Adam Cohen4ef610f2011-06-21 22:37:36 -0700380 mFolderName.setText(mInfo.title);
381 } else {
382 mFolderName.setText("");
383 }
Winson Chung33231f52013-12-09 16:57:45 -0800384
385 // In case any children didn't come across during loading, clean up the folder accordingly
386 mFolderIcon.post(new Runnable() {
387 public void run() {
388 if (getItemCount() <= 1) {
389 replaceFolderWithFinalItem();
390 }
391 }
392 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700393 }
394
395 /**
396 * Creates a new UserFolder, inflated from R.layout.user_folder.
397 *
398 * @param context The application's context.
399 *
400 * @return A new UserFolder.
401 */
402 static Folder fromXml(Context context) {
403 return (Folder) LayoutInflater.from(context).inflate(R.layout.user_folder, null);
404 }
405
406 /**
407 * This method is intended to make the UserFolder to be visually identical in size and position
408 * to its associated FolderIcon. This allows for a seamless transition into the expanded state.
409 */
410 private void positionAndSizeAsIcon() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700411 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800412 setScaleX(0.8f);
413 setScaleY(0.8f);
414 setAlpha(0f);
Adam Cohendf2cc412011-04-27 16:56:57 -0700415 mState = STATE_SMALL;
416 }
417
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700418 private void prepareReveal() {
419 setScaleX(1f);
420 setScaleY(1f);
421 setAlpha(1f);
422 mState = STATE_SMALL;
423 }
424
Adam Cohendf2cc412011-04-27 16:56:57 -0700425 public void animateOpen() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700426 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen6441de02011-12-14 14:25:32 -0800427
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700428 Animator openFolderAnim = null;
429 final Runnable onCompleteRunnable;
Kenny Guyd794a3f2014-09-16 15:17:58 +0100430 if (!Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700431 positionAndSizeAsIcon();
432 centerAboutIcon();
433
434 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1);
435 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
436 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
437 final ObjectAnimator oa =
438 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
439 oa.setDuration(mExpandDuration);
440 openFolderAnim = oa;
441
442 setLayerType(LAYER_TYPE_HARDWARE, null);
443 onCompleteRunnable = new Runnable() {
444 @Override
445 public void run() {
446 setLayerType(LAYER_TYPE_NONE, null);
447 }
448 };
449 } else {
450 prepareReveal();
451 centerAboutIcon();
452
453 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
454 int height = getFolderHeight();
455
456 float transX = - 0.075f * (width / 2 - getPivotX());
457 float transY = - 0.075f * (height / 2 - getPivotY());
458 setTranslationX(transX);
459 setTranslationY(transY);
460 PropertyValuesHolder tx = PropertyValuesHolder.ofFloat("translationX", transX, 0);
461 PropertyValuesHolder ty = PropertyValuesHolder.ofFloat("translationY", transY, 0);
462
463 int rx = (int) Math.max(Math.max(width - getPivotX(), 0), getPivotX());
464 int ry = (int) Math.max(Math.max(height - getPivotY(), 0), getPivotY());
465 float radius = (float) Math.sqrt(rx * rx + ry * ry);
466 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
467 Animator reveal = LauncherAnimUtils.createCircularReveal(this, (int) getPivotX(),
468 (int) getPivotY(), 0, radius);
469 reveal.setDuration(mMaterialExpandDuration);
470 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
471
472 mContent.setAlpha(0f);
473 Animator iconsAlpha = LauncherAnimUtils.ofFloat(mContent, "alpha", 0f, 1f);
474 iconsAlpha.setDuration(mMaterialExpandDuration);
475 iconsAlpha.setStartDelay(mMaterialExpandStagger);
476 iconsAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
477
478 mFolderName.setAlpha(0f);
479 Animator textAlpha = LauncherAnimUtils.ofFloat(mFolderName, "alpha", 0f, 1f);
480 textAlpha.setDuration(mMaterialExpandDuration);
481 textAlpha.setStartDelay(mMaterialExpandStagger);
482 textAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
483
484 Animator drift = LauncherAnimUtils.ofPropertyValuesHolder(this, tx, ty);
485 drift.setDuration(mMaterialExpandDuration);
486 drift.setStartDelay(mMaterialExpandStagger);
487 drift.setInterpolator(new LogDecelerateInterpolator(60, 0));
488
489 anim.play(drift);
490 anim.play(iconsAlpha);
491 anim.play(textAlpha);
492 anim.play(reveal);
493
494 openFolderAnim = anim;
495
496 mContent.setLayerType(LAYER_TYPE_HARDWARE, null);
497 onCompleteRunnable = new Runnable() {
498 @Override
499 public void run() {
500 mContent.setLayerType(LAYER_TYPE_NONE, null);
501 }
502 };
503 }
504 openFolderAnim.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700505 @Override
506 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700507 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700508 String.format(getContext().getString(R.string.folder_opened),
Adam Cohen3371da02011-10-25 21:38:29 -0700509 mContent.getCountX(), mContent.getCountY()));
Adam Cohendf2cc412011-04-27 16:56:57 -0700510 mState = STATE_ANIMATING;
511 }
512 @Override
513 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700514 mState = STATE_OPEN;
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700515
516 if (onCompleteRunnable != null) {
517 onCompleteRunnable.run();
518 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800519
Adam Cohenac56cff2011-09-28 20:45:37 -0700520 setFocusOnFirstChild();
Adam Cohendf2cc412011-04-27 16:56:57 -0700521 }
522 });
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700523 openFolderAnim.start();
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800524
525 // Make sure the folder picks up the last drag move even if the finger doesn't move.
526 if (mDragController.isDragging()) {
527 mDragController.forceTouchMove();
528 }
529 }
530
531 public void beginExternalDrag(ShortcutInfo item) {
532 setupContentForNumItems(getItemCount() + 1);
533 findAndSetEmptyCells(item);
534
535 mCurrentDragInfo = item;
536 mEmptyCell[0] = item.cellX;
537 mEmptyCell[1] = item.cellY;
538 mIsExternalDrag = true;
539
540 mDragInProgress = true;
Adam Cohendf2cc412011-04-27 16:56:57 -0700541 }
542
Adam Cohen3371da02011-10-25 21:38:29 -0700543 private void sendCustomAccessibilityEvent(int type, String text) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700544 AccessibilityManager accessibilityManager = (AccessibilityManager)
545 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
546 if (accessibilityManager.isEnabled()) {
Adam Cohen3371da02011-10-25 21:38:29 -0700547 AccessibilityEvent event = AccessibilityEvent.obtain(type);
548 onInitializeAccessibilityEvent(event);
549 event.getText().add(text);
Michael Jurka8b805b12012-04-18 14:23:14 -0700550 accessibilityManager.sendAccessibilityEvent(event);
Adam Cohen3371da02011-10-25 21:38:29 -0700551 }
552 }
553
Adam Cohenac56cff2011-09-28 20:45:37 -0700554 private void setFocusOnFirstChild() {
555 View firstChild = mContent.getChildAt(0, 0);
556 if (firstChild != null) {
557 firstChild.requestFocus();
558 }
559 }
560
Adam Cohendf2cc412011-04-27 16:56:57 -0700561 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700562 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800563 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
564 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
565 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
Michael Jurka032e6ba2013-04-22 15:08:42 +0200566 final ObjectAnimator oa =
Michael Jurka2ecf9952012-06-18 12:52:28 -0700567 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohendf2cc412011-04-27 16:56:57 -0700568
Adam Cohen2801caf2011-05-13 20:57:39 -0700569 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700570 @Override
571 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700572 onCloseComplete();
Michael Jurka0121c3e2012-05-31 08:36:04 -0700573 setLayerType(LAYER_TYPE_NONE, null);
Adam Cohen2801caf2011-05-13 20:57:39 -0700574 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700575 }
576 @Override
577 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700578 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700579 getContext().getString(R.string.folder_closed));
Adam Cohendf2cc412011-04-27 16:56:57 -0700580 mState = STATE_ANIMATING;
581 }
582 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700583 oa.setDuration(mExpandDuration);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700584 setLayerType(LAYER_TYPE_HARDWARE, null);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100585 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700586 }
587
Adam Cohencb3382b2011-05-24 14:07:08 -0700588 public boolean acceptDrop(DragObject d) {
589 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700590 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700591 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
592 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
593 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700594 }
595
Adam Cohendf2cc412011-04-27 16:56:57 -0700596 protected boolean findAndSetEmptyCells(ShortcutInfo item) {
597 int[] emptyCell = new int[2];
598 if (mContent.findCellForSpan(emptyCell, item.spanX, item.spanY)) {
599 item.cellX = emptyCell[0];
600 item.cellY = emptyCell[1];
Adam Cohendf2cc412011-04-27 16:56:57 -0700601 return true;
602 } else {
603 return false;
604 }
605 }
606
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800607 protected View createAndAddShortcut(ShortcutInfo item) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700608 final BubbleTextView textView =
609 (BubbleTextView) mInflater.inflate(R.layout.folder_application, this, false);
610 textView.applyFromShortcutInfo(item, mIconCache, false);
611
Adam Cohendf2cc412011-04-27 16:56:57 -0700612 textView.setOnClickListener(this);
613 textView.setOnLongClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700614 textView.setOnFocusChangeListener(mFocusIndicatorHandler);
Adam Cohendf2cc412011-04-27 16:56:57 -0700615
Adam Cohenc508b2d2011-06-28 14:41:44 -0700616 // We need to check here to verify that the given item's location isn't already occupied
Adam Cohen3bf84d32012-05-07 20:17:14 -0700617 // by another item.
Adam Cohen0057bbc2011-08-12 18:30:51 -0700618 if (mContent.getChildAt(item.cellX, item.cellY) != null || item.cellX < 0 || item.cellY < 0
619 || item.cellX >= mContent.getCountX() || item.cellY >= mContent.getCountY()) {
Jason Monk4ff73882014-04-24 16:48:07 -0400620 // This shouldn't happen, log it.
Adam Cohen3bf84d32012-05-07 20:17:14 -0700621 Log.e(TAG, "Folder order not properly persisted during bind");
Adam Cohenc508b2d2011-06-28 14:41:44 -0700622 if (!findAndSetEmptyCells(item)) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800623 return null;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700624 }
625 }
626
Adam Cohendf2cc412011-04-27 16:56:57 -0700627 CellLayout.LayoutParams lp =
628 new CellLayout.LayoutParams(item.cellX, item.cellY, item.spanX, item.spanY);
629 boolean insert = false;
Adam Cohenac56cff2011-09-28 20:45:37 -0700630 textView.setOnKeyListener(new FolderKeyEventListener());
Adam Cohendf2cc412011-04-27 16:56:57 -0700631 mContent.addViewToCellLayout(textView, insert ? 0 : -1, (int)item.id, lp, true);
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800632 return textView;
Adam Cohendf2cc412011-04-27 16:56:57 -0700633 }
634
Adam Cohencb3382b2011-05-24 14:07:08 -0700635 public void onDragEnter(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700636 mPreviousTargetCell[0] = -1;
637 mPreviousTargetCell[1] = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700638 mOnExitAlarm.cancelAlarm();
639 }
640
641 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
642 public void onAlarm(Alarm alarm) {
643 realTimeReorder(mEmptyCell, mTargetCell);
644 }
645 };
646
647 boolean readingOrderGreaterThan(int[] v1, int[] v2) {
648 if (v1[1] > v2[1] || (v1[1] == v2[1] && v1[0] > v2[0])) {
649 return true;
650 } else {
651 return false;
652 }
653 }
654
655 private void realTimeReorder(int[] empty, int[] target) {
656 boolean wrap;
657 int startX;
658 int endX;
659 int startY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700660 int delay = 0;
661 float delayAmount = 30;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700662 if (readingOrderGreaterThan(target, empty)) {
663 wrap = empty[0] >= mContent.getCountX() - 1;
664 startY = wrap ? empty[1] + 1 : empty[1];
665 for (int y = startY; y <= target[1]; y++) {
666 startX = y == empty[1] ? empty[0] + 1 : 0;
667 endX = y < target[1] ? mContent.getCountX() - 1 : target[0];
668 for (int x = startX; x <= endX; x++) {
669 View v = mContent.getChildAt(x,y);
670 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen482ed822012-03-02 14:15:13 -0800671 REORDER_ANIMATION_DURATION, delay, true, true)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700672 empty[0] = x;
673 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700674 delay += delayAmount;
675 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700676 }
677 }
678 }
679 } else {
680 wrap = empty[0] == 0;
681 startY = wrap ? empty[1] - 1 : empty[1];
682 for (int y = startY; y >= target[1]; y--) {
683 startX = y == empty[1] ? empty[0] - 1 : mContent.getCountX() - 1;
684 endX = y > target[1] ? 0 : target[0];
685 for (int x = startX; x >= endX; x--) {
686 View v = mContent.getChildAt(x,y);
687 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen482ed822012-03-02 14:15:13 -0800688 REORDER_ANIMATION_DURATION, delay, true, true)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700689 empty[0] = x;
690 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700691 delay += delayAmount;
692 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700693 }
694 }
695 }
696 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700697 }
698
Sunny Goyalc46bfef2015-01-05 12:40:08 -0800699 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohen2374abf2013-04-16 14:56:57 -0700700 public boolean isLayoutRtl() {
701 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
702 }
703
Adam Cohencb3382b2011-05-24 14:07:08 -0700704 public void onDragOver(DragObject d) {
Alan Viverette4cda5b72013-08-28 17:53:41 -0700705 final int scrollOffset = mScrollView.getScrollY();
Sunny Goyal1587d532015-01-29 09:57:16 -0800706 final float[] r = d.getVisualCenter(null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700707 r[0] -= getPaddingLeft();
708 r[1] -= getPaddingTop();
709
Sunny Goyalc46bfef2015-01-05 12:40:08 -0800710 mTargetCell = mContent.findNearestArea(
711 (int) r[0], (int) r[1] + scrollOffset, 1, 1, mTargetCell);
712 if (isLayoutRtl()) {
713 mTargetCell[0] = mContent.getCountX() - mTargetCell[0] - 1;
714 }
715 if (mTargetCell[0] != mPreviousTargetCell[0]
716 || mTargetCell[1] != mPreviousTargetCell[1]) {
Alan Viverette4cda5b72013-08-28 17:53:41 -0700717 mReorderAlarm.cancelAlarm();
Sunny Goyalc46bfef2015-01-05 12:40:08 -0800718 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
719 mReorderAlarm.setAlarm(REORDER_DELAY);
720 mPreviousTargetCell[0] = mTargetCell[0];
721 mPreviousTargetCell[1] = mTargetCell[1];
Adam Cohenbfbfd262011-06-13 16:55:12 -0700722 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700723 }
724
Adam Cohenbfbfd262011-06-13 16:55:12 -0700725 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
726 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700727 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700728 }
729 };
730
Adam Cohen95bb8002011-07-03 23:40:28 -0700731 public void completeDragExit() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700732 mLauncher.closeFolder();
733 mCurrentDragInfo = null;
734 mCurrentDragView = null;
735 mSuppressOnAdd = false;
736 mRearrangeOnClose = true;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800737 mIsExternalDrag = false;
Adam Cohen3e8f8112011-07-02 18:03:00 -0700738 }
739
Adam Cohencb3382b2011-05-24 14:07:08 -0700740 public void onDragExit(DragObject d) {
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700741 // We only close the folder if this is a true drag exit, ie. not because
742 // a drop has occurred above the folder.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700743 if (!d.dragComplete) {
744 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
745 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
746 }
747 mReorderAlarm.cancelAlarm();
Adam Cohen2801caf2011-05-13 20:57:39 -0700748 }
749
Michael Jurka1e2f4652013-07-08 18:03:46 -0700750 public void onDropCompleted(final View target, final DragObject d,
751 final boolean isFlingToDelete, final boolean success) {
752 if (mDeferDropAfterUninstall) {
Michael Jurkaf3007582013-08-21 14:33:57 +0200753 Log.d(TAG, "Deferred handling drop because waiting for uninstall.");
Michael Jurka1e2f4652013-07-08 18:03:46 -0700754 mDeferredAction = new Runnable() {
755 public void run() {
756 onDropCompleted(target, d, isFlingToDelete, success);
757 mDeferredAction = null;
758 }
759 };
760 return;
761 }
762
763 boolean beingCalledAfterUninstall = mDeferredAction != null;
764 boolean successfulDrop =
765 success && (!beingCalledAfterUninstall || mUninstallSuccessful);
Winson Chung5f8afe62013-08-12 16:19:28 -0700766
Michael Jurka1e2f4652013-07-08 18:03:46 -0700767 if (successfulDrop) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800768 if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon && target != this) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700769 replaceFolderWithFinalItem();
770 }
771 } else {
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700772 setupContentForNumItems(getItemCount());
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700773 // The drag failed, we need to return the item to the folder
774 mFolderIcon.onDrop(d);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700775 }
776
777 if (target != this) {
778 if (mOnExitAlarm.alarmPending()) {
779 mOnExitAlarm.cancelAlarm();
Michael Jurka54554252013-08-01 12:52:23 +0200780 if (!successfulDrop) {
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700781 mSuppressFolderDeletion = true;
782 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700783 completeDragExit();
784 }
Adam Cohen9c58d822013-12-13 17:18:10 -0800785 }
786
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700787 mDeleteFolderOnDropCompleted = false;
788 mDragInProgress = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700789 mItemAddedBackToSelfViaIcon = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700790 mCurrentDragInfo = null;
791 mCurrentDragView = null;
792 mSuppressOnAdd = false;
Adam Cohen4045eb72011-10-06 11:44:26 -0700793
794 // Reordering may have occured, and we need to save the new item locations. We do this once
795 // at the end to prevent unnecessary database operations.
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700796 updateItemLocationsInDatabaseBatch();
Adam Cohen4045eb72011-10-06 11:44:26 -0700797 }
798
Michael Jurka1e2f4652013-07-08 18:03:46 -0700799 public void deferCompleteDropAfterUninstallActivity() {
800 mDeferDropAfterUninstall = true;
801 }
802
803 public void onUninstallActivityReturned(boolean success) {
804 mDeferDropAfterUninstall = false;
805 mUninstallSuccessful = success;
806 if (mDeferredAction != null) {
807 mDeferredAction.run();
808 }
809 }
810
Winson Chunga48487a2012-03-20 16:19:37 -0700811 @Override
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800812 public float getIntrinsicIconScaleFactor() {
813 return 1f;
814 }
815
816 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800817 public boolean supportsFlingToDelete() {
818 return true;
819 }
820
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000821 @Override
822 public boolean supportsAppInfoDropTarget() {
823 return false;
824 }
825
826 @Override
827 public boolean supportsDeleteDropTarget() {
828 return true;
829 }
830
Winson Chunga48487a2012-03-20 16:19:37 -0700831 public void onFlingToDelete(DragObject d, int x, int y, PointF vec) {
832 // Do nothing
833 }
834
835 @Override
836 public void onFlingToDeleteCompleted() {
837 // Do nothing
838 }
839
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700840 private void updateItemLocationsInDatabaseBatch() {
841 ArrayList<View> list = getItemsInReadingOrder();
842 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
843 for (int i = 0; i < list.size(); i++) {
844 View v = list.get(i);
845 ItemInfo info = (ItemInfo) v.getTag();
Sunny Goyal08f72612015-01-05 13:41:43 -0800846 info.rank = i;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700847 items.add(info);
848 }
849
850 LauncherModel.moveItemsInDatabase(mLauncher, items, mInfo.id, 0);
851 }
852
Adam Cohene25af792013-06-06 23:08:25 -0700853 public void addItemLocationsInDatabase() {
854 ArrayList<View> list = getItemsInReadingOrder();
855 for (int i = 0; i < list.size(); i++) {
856 View v = list.get(i);
857 ItemInfo info = (ItemInfo) v.getTag();
858 LauncherModel.addItemToDatabase(mLauncher, info, mInfo.id, 0,
859 info.cellX, info.cellY, false);
860 }
861 }
862
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700863 public void notifyDrop() {
864 if (mDragInProgress) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700865 mItemAddedBackToSelfViaIcon = true;
Adam Cohen76078c42011-06-09 15:06:52 -0700866 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700867 }
868
869 public boolean isDropEnabled() {
870 return true;
871 }
872
Adam Cohen4045eb72011-10-06 11:44:26 -0700873 private void setupContentDimensions(int count) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700874 ArrayList<View> list = getItemsInReadingOrder();
875
876 int countX = mContent.getCountX();
877 int countY = mContent.getCountY();
Adam Cohen7c693212011-05-18 15:26:57 -0700878 boolean done = false;
Adam Cohen2801caf2011-05-13 20:57:39 -0700879
Adam Cohen7c693212011-05-18 15:26:57 -0700880 while (!done) {
881 int oldCountX = countX;
882 int oldCountY = countY;
883 if (countX * countY < count) {
884 // Current grid is too small, expand it
Adam Cohen78dc83e2011-11-15 17:10:00 -0800885 if ((countX <= countY || countY == mMaxCountY) && countX < mMaxCountX) {
Adam Cohen7c693212011-05-18 15:26:57 -0700886 countX++;
887 } else if (countY < mMaxCountY) {
888 countY++;
889 }
890 if (countY == 0) countY++;
891 } else if ((countY - 1) * countX >= count && countY >= countX) {
892 countY = Math.max(0, countY - 1);
893 } else if ((countX - 1) * countY >= count) {
894 countX = Math.max(0, countX - 1);
Adam Cohen2801caf2011-05-13 20:57:39 -0700895 }
Adam Cohen7c693212011-05-18 15:26:57 -0700896 done = countX == oldCountX && countY == oldCountY;
Adam Cohen2801caf2011-05-13 20:57:39 -0700897 }
Adam Cohen7c693212011-05-18 15:26:57 -0700898 mContent.setGridSize(countX, countY);
Adam Cohen2801caf2011-05-13 20:57:39 -0700899 arrangeChildren(list);
900 }
901
902 public boolean isFull() {
Adam Cohen78dc83e2011-11-15 17:10:00 -0800903 return getItemCount() >= mMaxNumItems;
Adam Cohen2801caf2011-05-13 20:57:39 -0700904 }
905
906 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700907 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700908
Winson Chung892c74d2013-08-22 16:15:50 -0700909 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700910 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700911 int height = getFolderHeight();
Adam Cohen2801caf2011-05-13 20:57:39 -0700912
Adam Cohen307fe232012-08-16 17:55:58 -0700913 float scale = parent.getDescendantRectRelativeToSelf(mFolderIcon, mTempRect);
Adam Cohen8e776a62011-06-28 18:10:06 -0700914
Winson Chungaf40f202013-09-18 18:26:31 -0700915 LauncherAppState app = LauncherAppState.getInstance();
916 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
917
Adam Cohen307fe232012-08-16 17:55:58 -0700918 int centerX = (int) (mTempRect.left + mTempRect.width() * scale / 2);
919 int centerY = (int) (mTempRect.top + mTempRect.height() * scale / 2);
Adam Cohen2801caf2011-05-13 20:57:39 -0700920 int centeredLeft = centerX - width / 2;
921 int centeredTop = centerY - height / 2;
Winson Chung3057b1c2013-10-10 17:35:35 -0700922 int currentPage = mLauncher.getWorkspace().getNextPage();
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800923 // In case the workspace is scrolling, we need to use the final scroll to compute
924 // the folders bounds.
925 mLauncher.getWorkspace().setFinalScrollForPageChange(currentPage);
Adam Cohen35e7e642011-07-17 14:47:18 -0700926 // We first fetch the currently visible CellLayoutChildren
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800927 CellLayout currentLayout = (CellLayout) mLauncher.getWorkspace().getChildAt(currentPage);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700928 ShortcutAndWidgetContainer boundingLayout = currentLayout.getShortcutsAndWidgets();
Adam Cohen35e7e642011-07-17 14:47:18 -0700929 Rect bounds = new Rect();
930 parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800931 // We reset the workspaces scroll
932 mLauncher.getWorkspace().resetFinalScrollForPageChange(currentPage);
Adam Cohen2801caf2011-05-13 20:57:39 -0700933
Adam Cohen35e7e642011-07-17 14:47:18 -0700934 // We need to bound the folder to the currently visible CellLayoutChildren
935 int left = Math.min(Math.max(bounds.left, centeredLeft),
936 bounds.left + bounds.width() - width);
937 int top = Math.min(Math.max(bounds.top, centeredTop),
938 bounds.top + bounds.height() - height);
Winson Chungaf40f202013-09-18 18:26:31 -0700939 if (grid.isPhone() && (grid.availableWidthPx - width) < grid.iconSizePx) {
940 // Center the folder if it is full (on phones only)
941 left = (grid.availableWidthPx - width) / 2;
942 } else if (width >= bounds.width()) {
943 // If the folder doesn't fit within the bounds, center it about the desired bounds
Adam Cohen35e7e642011-07-17 14:47:18 -0700944 left = bounds.left + (bounds.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700945 }
Adam Cohen35e7e642011-07-17 14:47:18 -0700946 if (height >= bounds.height()) {
947 top = bounds.top + (bounds.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700948 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700949
950 int folderPivotX = width / 2 + (centeredLeft - left);
951 int folderPivotY = height / 2 + (centeredTop - top);
952 setPivotX(folderPivotX);
953 setPivotY(folderPivotY);
Adam Cohen268c4752012-06-06 17:47:33 -0700954 mFolderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700955 (1.0f * folderPivotX / width));
Adam Cohen268c4752012-06-06 17:47:33 -0700956 mFolderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700957 (1.0f * folderPivotY / height));
Adam Cohen3bf84d32012-05-07 20:17:14 -0700958
Adam Cohen662b5982011-12-13 17:45:21 -0800959 lp.width = width;
960 lp.height = height;
961 lp.x = left;
962 lp.y = top;
Adam Cohen2801caf2011-05-13 20:57:39 -0700963 }
964
Adam Cohen268c4752012-06-06 17:47:33 -0700965 float getPivotXForIconAnimation() {
966 return mFolderIconPivotX;
967 }
968 float getPivotYForIconAnimation() {
969 return mFolderIconPivotY;
970 }
971
Adam Cohen2801caf2011-05-13 20:57:39 -0700972 private void setupContentForNumItems(int count) {
Adam Cohen4045eb72011-10-06 11:44:26 -0700973 setupContentDimensions(count);
Adam Cohen2801caf2011-05-13 20:57:39 -0700974
Adam Cohen8e776a62011-06-28 18:10:06 -0700975 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700976 if (lp == null) {
Adam Cohen8e776a62011-06-28 18:10:06 -0700977 lp = new DragLayer.LayoutParams(0, 0);
978 lp.customPosition = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700979 setLayoutParams(lp);
980 }
981 centerAboutIcon();
982 }
983
Winson Chung892c74d2013-08-22 16:15:50 -0700984 private int getContentAreaHeight() {
985 LauncherAppState app = LauncherAppState.getInstance();
986 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
987 Rect workspacePadding = grid.getWorkspacePadding(grid.isLandscape ?
988 CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
989 int maxContentAreaHeight = grid.availableHeightPx -
Winson Chung892c74d2013-08-22 16:15:50 -0700990 workspacePadding.top - workspacePadding.bottom -
Winson Chung892c74d2013-08-22 16:15:50 -0700991 mFolderNameHeight;
Adam Cohen1960ea42013-11-12 11:33:14 +0000992 int height = Math.min(maxContentAreaHeight,
Winson Chung892c74d2013-08-22 16:15:50 -0700993 mContent.getDesiredHeight());
Adam Cohen1960ea42013-11-12 11:33:14 +0000994 return Math.max(height, MIN_CONTENT_DIMEN);
995 }
996
997 private int getContentAreaWidth() {
998 return Math.max(mContent.getDesiredWidth(), MIN_CONTENT_DIMEN);
Winson Chung892c74d2013-08-22 16:15:50 -0700999 }
1000
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001001 private int getFolderHeight() {
Winson Chung892c74d2013-08-22 16:15:50 -07001002 int height = getPaddingTop() + getPaddingBottom()
1003 + getContentAreaHeight() + mFolderNameHeight;
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001004 return height;
1005 }
Adam Cohen234c4cd2011-07-17 21:03:04 -07001006
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001007 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001008 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
1009 int height = getFolderHeight();
Adam Cohen1960ea42013-11-12 11:33:14 +00001010 int contentAreaWidthSpec = MeasureSpec.makeMeasureSpec(getContentAreaWidth(),
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001011 MeasureSpec.EXACTLY);
Winson Chung892c74d2013-08-22 16:15:50 -07001012 int contentAreaHeightSpec = MeasureSpec.makeMeasureSpec(getContentAreaHeight(),
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001013 MeasureSpec.EXACTLY);
Adam Cohen1960ea42013-11-12 11:33:14 +00001014
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08001015 if (LauncherAppState.isDisableAllApps()) {
Nilesh Agrawal5f7099a2014-01-02 15:54:57 -08001016 // Don't cap the height of the content to allow scrolling.
1017 mContent.setFixedSize(getContentAreaWidth(), mContent.getDesiredHeight());
1018 } else {
1019 mContent.setFixedSize(getContentAreaWidth(), getContentAreaHeight());
1020 }
1021
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001022 mScrollView.measure(contentAreaWidthSpec, contentAreaHeightSpec);
1023 mFolderName.measure(contentAreaWidthSpec,
Adam Cohen234c4cd2011-07-17 21:03:04 -07001024 MeasureSpec.makeMeasureSpec(mFolderNameHeight, MeasureSpec.EXACTLY));
1025 setMeasuredDimension(width, height);
1026 }
1027
Adam Cohen2801caf2011-05-13 20:57:39 -07001028 private void arrangeChildren(ArrayList<View> list) {
1029 int[] vacant = new int[2];
1030 if (list == null) {
1031 list = getItemsInReadingOrder();
1032 }
1033 mContent.removeAllViews();
1034
1035 for (int i = 0; i < list.size(); i++) {
1036 View v = list.get(i);
1037 mContent.getVacantCell(vacant, 1, 1);
1038 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) v.getLayoutParams();
1039 lp.cellX = vacant[0];
1040 lp.cellY = vacant[1];
1041 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohen2792a332011-09-26 21:09:47 -07001042 if (info.cellX != vacant[0] || info.cellY != vacant[1]) {
1043 info.cellX = vacant[0];
1044 info.cellY = vacant[1];
1045 LauncherModel.addOrMoveItemInDatabase(mLauncher, info, mInfo.id, 0,
1046 info.cellX, info.cellY);
1047 }
Adam Cohen2801caf2011-05-13 20:57:39 -07001048 boolean insert = false;
1049 mContent.addViewToCellLayout(v, insert ? 0 : -1, (int)info.id, lp, true);
1050 }
Adam Cohen7c693212011-05-18 15:26:57 -07001051 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -07001052 }
1053
Adam Cohena9cf38f2011-05-02 15:36:58 -07001054 public int getItemCount() {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001055 return mContent.getShortcutsAndWidgets().getChildCount();
Adam Cohena9cf38f2011-05-02 15:36:58 -07001056 }
1057
1058 public View getItemAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001059 return mContent.getShortcutsAndWidgets().getChildAt(index);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001060 }
1061
Adam Cohen2801caf2011-05-13 20:57:39 -07001062 private void onCloseComplete() {
Adam Cohen05e0f402011-08-01 12:12:49 -07001063 DragLayer parent = (DragLayer) getParent();
Michael Jurka5649c282012-06-18 10:33:21 -07001064 if (parent != null) {
1065 parent.removeView(this);
1066 }
Adam Cohen4554ee12011-08-03 16:13:21 -07001067 mDragController.removeDropTarget((DropTarget) this);
Adam Cohen05e0f402011-08-01 12:12:49 -07001068 clearFocus();
Adam Cohenac56cff2011-09-28 20:45:37 -07001069 mFolderIcon.requestFocus();
Adam Cohen05e0f402011-08-01 12:12:49 -07001070
Adam Cohen2801caf2011-05-13 20:57:39 -07001071 if (mRearrangeOnClose) {
1072 setupContentForNumItems(getItemCount());
1073 mRearrangeOnClose = false;
1074 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001075 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001076 if (!mDragInProgress && !mSuppressFolderDeletion) {
1077 replaceFolderWithFinalItem();
1078 } else if (mDragInProgress) {
1079 mDeleteFolderOnDropCompleted = true;
1080 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001081 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001082 mSuppressFolderDeletion = false;
Adam Cohenafb01ee2011-06-23 15:38:03 -07001083 }
1084
1085 private void replaceFolderWithFinalItem() {
Adam Cohenafb01ee2011-06-23 15:38:03 -07001086 // Add the last remaining child to the workspace in place of the folder
Adam Cohenfb91f302012-06-11 15:45:18 -07001087 Runnable onCompleteRunnable = new Runnable() {
1088 @Override
1089 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -07001090 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screenId);
Adam Cohenafb01ee2011-06-23 15:38:03 -07001091
Winson Chung33231f52013-12-09 16:57:45 -08001092 View child = null;
Adam Cohenfb91f302012-06-11 15:45:18 -07001093 // Move the item from the folder to the workspace, in the position of the folder
1094 if (getItemCount() == 1) {
1095 ShortcutInfo finalItem = mInfo.contents.get(0);
Adam Cohenc5e63f32012-07-12 16:16:57 -07001096 child = mLauncher.createShortcut(R.layout.application, cellLayout,
Adam Cohenfb91f302012-06-11 15:45:18 -07001097 finalItem);
1098 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -07001099 mInfo.screenId, mInfo.cellX, mInfo.cellY);
Adam Cohenfb91f302012-06-11 15:45:18 -07001100 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001101 if (getItemCount() <= 1) {
1102 // Remove the folder
1103 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
Dan Sandler0eb687f2014-01-10 13:24:55 -05001104 if (cellLayout != null) {
1105 // b/12446428 -- sometimes the cell layout has already gone away?
1106 cellLayout.removeView(mFolderIcon);
1107 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001108 if (mFolderIcon instanceof DropTarget) {
1109 mDragController.removeDropTarget((DropTarget) mFolderIcon);
1110 }
1111 mLauncher.removeFolder(mInfo);
1112 }
Adam Cohenc5e63f32012-07-12 16:16:57 -07001113 // We add the child after removing the folder to prevent both from existing at
Winson Chung0e6a7132013-08-23 12:55:10 -07001114 // the same time in the CellLayout. We need to add the new item with addInScreenFromBind()
1115 // to ensure that hotseat items are placed correctly.
Adam Cohenc5e63f32012-07-12 16:16:57 -07001116 if (child != null) {
Winson Chung0e6a7132013-08-23 12:55:10 -07001117 mLauncher.getWorkspace().addInScreenFromBind(child, mInfo.container, mInfo.screenId,
Adam Cohenc5e63f32012-07-12 16:16:57 -07001118 mInfo.cellX, mInfo.cellY, mInfo.spanX, mInfo.spanY);
1119 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001120 }
1121 };
1122 View finalChild = getItemAt(0);
1123 if (finalChild != null) {
1124 mFolderIcon.performDestroyAnimation(finalChild, onCompleteRunnable);
Winson Chung33231f52013-12-09 16:57:45 -08001125 } else {
1126 onCompleteRunnable.run();
Adam Cohenafb01ee2011-06-23 15:38:03 -07001127 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001128 mDestroyed = true;
1129 }
1130
1131 boolean isDestroyed() {
1132 return mDestroyed;
Adam Cohen2801caf2011-05-13 20:57:39 -07001133 }
1134
Adam Cohenac56cff2011-09-28 20:45:37 -07001135 // This method keeps track of the last item in the folder for the purposes
1136 // of keyboard focus
1137 private void updateTextViewFocus() {
1138 View lastChild = getItemAt(getItemCount() - 1);
1139 getItemAt(getItemCount() - 1);
1140 if (lastChild != null) {
1141 mFolderName.setNextFocusDownId(lastChild.getId());
1142 mFolderName.setNextFocusRightId(lastChild.getId());
1143 mFolderName.setNextFocusLeftId(lastChild.getId());
1144 mFolderName.setNextFocusUpId(lastChild.getId());
1145 }
1146 }
1147
Adam Cohenbfbfd262011-06-13 16:55:12 -07001148 public void onDrop(DragObject d) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001149 Runnable cleanUpRunnable = null;
1150
Adam Cohen689ff162014-05-08 17:27:56 -07001151 // If we are coming from All Apps space, we defer removing the extra empty screen
1152 // until the folder closes
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001153 if (d.dragSource != mLauncher.getWorkspace() && !(d.dragSource instanceof Folder)) {
1154 cleanUpRunnable = new Runnable() {
1155 @Override
1156 public void run() {
Adam Cohen689ff162014-05-08 17:27:56 -07001157 mLauncher.exitSpringLoadedDragModeDelayed(true,
1158 Launcher.EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
1159 null);
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001160 }
1161 };
Adam Cohenbfbfd262011-06-13 16:55:12 -07001162 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001163
1164 View currentDragView;
1165 ShortcutInfo si = mCurrentDragInfo;
1166 if (mIsExternalDrag) {
1167 si.cellX = mEmptyCell[0];
1168 si.cellY = mEmptyCell[1];
Sunny Goyal95abbb32014-08-04 10:53:22 -07001169
1170 // Actually move the item in the database if it was an external drag. Call this
1171 // before creating the view, so that ShortcutInfo is updated appropriately.
1172 LauncherModel.addOrMoveItemInDatabase(
1173 mLauncher, si, mInfo.id, 0, si.cellX, si.cellY);
1174
1175 // We only need to update the locations if it doesn't get handled in #onDropCompleted.
1176 if (d.dragSource != this) {
1177 updateItemLocationsInDatabaseBatch();
1178 }
1179 mIsExternalDrag = false;
1180
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001181 currentDragView = createAndAddShortcut(si);
1182 } else {
1183 currentDragView = mCurrentDragView;
1184 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) currentDragView.getLayoutParams();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001185 si.cellX = lp.cellX = mEmptyCell[0];
1186 si.cellX = lp.cellY = mEmptyCell[1];
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001187 mContent.addViewToCellLayout(currentDragView, -1, (int) si.id, lp, true);
Adam Cohenbfbfd262011-06-13 16:55:12 -07001188 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001189
1190 if (d.dragView.hasDrawn()) {
1191
1192 // Temporarily reset the scale such that the animation target gets calculated correctly.
1193 float scaleX = getScaleX();
1194 float scaleY = getScaleY();
1195 setScaleX(1.0f);
1196 setScaleY(1.0f);
1197 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, currentDragView,
1198 cleanUpRunnable, null);
1199 setScaleX(scaleX);
1200 setScaleY(scaleY);
1201 } else {
1202 d.deferDragViewCleanupPostAnimation = false;
1203 currentDragView.setVisibility(VISIBLE);
1204 }
1205 mItemsInvalidated = true;
1206 setupContentDimensions(getItemCount());
1207
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001208 // Temporarily suppress the listener, as we did all the work already here.
1209 mSuppressOnAdd = true;
1210 mInfo.add(si);
1211 mSuppressOnAdd = false;
Sunny Goyal4b020172014-08-28 14:51:14 -07001212 // Clear the drag info, as it is no longer being dragged.
1213 mCurrentDragInfo = null;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001214 }
1215
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001216 // This is used so the item doesn't immediately appear in the folder when added. In one case
1217 // we need to create the illusion that the item isn't added back to the folder yet, to
1218 // to correspond to the animation of the icon back into the folder. This is
1219 public void hideItem(ShortcutInfo info) {
1220 View v = getViewForInfo(info);
1221 v.setVisibility(INVISIBLE);
1222 }
1223 public void showItem(ShortcutInfo info) {
1224 View v = getViewForInfo(info);
1225 v.setVisibility(VISIBLE);
1226 }
1227
Adam Cohenbfbfd262011-06-13 16:55:12 -07001228 public void onAdd(ShortcutInfo item) {
1229 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001230 // If the item was dropped onto this open folder, we have done the work associated
1231 // with adding the item to the folder, as indicated by mSuppressOnAdd being set
Adam Cohenbfbfd262011-06-13 16:55:12 -07001232 if (mSuppressOnAdd) return;
1233 if (!findAndSetEmptyCells(item)) {
1234 // The current layout is full, can we expand it?
1235 setupContentForNumItems(getItemCount() + 1);
1236 findAndSetEmptyCells(item);
1237 }
1238 createAndAddShortcut(item);
1239 LauncherModel.addOrMoveItemInDatabase(
1240 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
1241 }
1242
Adam Cohena9cf38f2011-05-02 15:36:58 -07001243 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -07001244 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001245 // If this item is being dragged from this open folder, we have already handled
1246 // the work associated with removing the item, so we don't have to do anything here.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001247 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001248 View v = getViewForInfo(item);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001249 mContent.removeView(v);
Adam Cohen2801caf2011-05-13 20:57:39 -07001250 if (mState == STATE_ANIMATING) {
1251 mRearrangeOnClose = true;
1252 } else {
1253 setupContentForNumItems(getItemCount());
1254 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001255 if (getItemCount() <= 1) {
1256 replaceFolderWithFinalItem();
1257 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001258 }
Adam Cohen7c693212011-05-18 15:26:57 -07001259
Adam Cohendf1e4e82011-06-24 15:57:39 -07001260 private View getViewForInfo(ShortcutInfo item) {
1261 for (int j = 0; j < mContent.getCountY(); j++) {
1262 for (int i = 0; i < mContent.getCountX(); i++) {
1263 View v = mContent.getChildAt(i, j);
1264 if (v.getTag() == item) {
1265 return v;
1266 }
1267 }
1268 }
1269 return null;
1270 }
1271
Adam Cohen76078c42011-06-09 15:06:52 -07001272 public void onItemsChanged() {
Adam Cohenac56cff2011-09-28 20:45:37 -07001273 updateTextViewFocus();
Adam Cohen76078c42011-06-09 15:06:52 -07001274 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001275
Adam Cohen76fc0852011-06-17 13:26:23 -07001276 public void onTitleChanged(CharSequence title) {
1277 }
Adam Cohen76078c42011-06-09 15:06:52 -07001278
Adam Cohen7c693212011-05-18 15:26:57 -07001279 public ArrayList<View> getItemsInReadingOrder() {
1280 if (mItemsInvalidated) {
1281 mItemsInReadingOrder.clear();
1282 for (int j = 0; j < mContent.getCountY(); j++) {
1283 for (int i = 0; i < mContent.getCountX(); i++) {
1284 View v = mContent.getChildAt(i, j);
1285 if (v != null) {
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001286 mItemsInReadingOrder.add(v);
Adam Cohen7c693212011-05-18 15:26:57 -07001287 }
1288 }
1289 }
1290 mItemsInvalidated = false;
1291 }
1292 return mItemsInReadingOrder;
1293 }
Adam Cohen8dfcba42011-07-07 16:38:18 -07001294
1295 public void getLocationInDragLayer(int[] loc) {
1296 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
1297 }
Adam Cohenea0818d2011-09-30 20:06:58 -07001298
1299 public void onFocusChange(View v, boolean hasFocus) {
1300 if (v == mFolderName && hasFocus) {
1301 startEditingFolderName();
1302 }
1303 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001304
1305 @Override
1306 public void getHitRectRelativeToDragLayer(Rect outRect) {
1307 getHitRect(outRect);
1308 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001309}