blob: f2d7a69916c61515378355712b8283fabed80a40 [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 Chungb745afb2015-03-02 11:51:23 -080027import android.graphics.Point;
Winson Chung043f2af2012-03-01 16:09:54 -080028import android.graphics.PointF;
Romain Guyfb5411e2010-02-24 10:04:17 -080029import android.graphics.Rect;
Sunny Goyalc46bfef2015-01-05 12:40:08 -080030import android.os.Build;
Adam Cohen7a14d0b2011-06-24 11:36:35 -070031import android.text.InputType;
Adam Cohene601a432011-07-26 21:51:30 -070032import android.text.Selection;
33import android.text.Spannable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.util.AttributeSet;
Adam Cohen3bf84d32012-05-07 20:17:14 -070035import android.util.Log;
Adam Cohen76fc0852011-06-17 13:26:23 -070036import android.view.ActionMode;
37import android.view.KeyEvent;
Adam Cohendf2cc412011-04-27 16:56:57 -070038import android.view.LayoutInflater;
Adam Cohen76fc0852011-06-17 13:26:23 -070039import android.view.Menu;
40import android.view.MenuItem;
Adam Cohen0c872ba2011-05-05 10:34:16 -070041import android.view.MotionEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.view.View;
Sunny Goyalc4918352015-03-10 18:15:48 -070043import android.view.ViewGroup;
Adam Cohen3371da02011-10-25 21:38:29 -070044import android.view.accessibility.AccessibilityEvent;
45import android.view.accessibility.AccessibilityManager;
Adam Cohenc4fe9ea2014-08-18 18:54:10 -070046import android.view.animation.AccelerateInterpolator;
Adam Cohen76fc0852011-06-17 13:26:23 -070047import android.view.inputmethod.EditorInfo;
48import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.widget.LinearLayout;
Adam Cohendf2cc412011-04-27 16:56:57 -070050import android.widget.TextView;
Sunny Goyal48461932015-03-09 17:41:09 -070051
Sunny Goyale9b651e2015-04-24 11:44:51 -070052import com.android.launcher3.CellLayout.CellInfo;
Sunny Goyalf4066152015-04-15 09:42:19 -070053import com.android.launcher3.DragController.DragListener;
Daniel Sandler325dc232013-06-05 22:57:57 -040054import com.android.launcher3.FolderInfo.FolderListener;
Sunny Goyale9b651e2015-04-24 11:44:51 -070055import com.android.launcher3.LauncherAccessibilityDelegate.AccessibilityDragSource;
Sunny Goyalfa401a12015-04-10 13:45:42 -070056import com.android.launcher3.UninstallDropTarget.UninstallSource;
Sunny Goyalbc753352015-03-05 09:40:44 -080057import com.android.launcher3.Workspace.ItemOperator;
Adam Cohen091440a2015-03-18 14:16:05 -070058import com.android.launcher3.util.Thunk;
Romain Guyedcce092010-03-04 13:03:17 -080059
Adam Cohenc0dcf592011-06-01 15:30:43 -070060import java.util.ArrayList;
Adam Cohen3bf84d32012-05-07 20:17:14 -070061import java.util.Collections;
Adam Cohenc0dcf592011-06-01 15:30:43 -070062
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063/**
64 * Represents a set of icons chosen by the user or generated by the system.
65 */
Adam Cohen8dfcba42011-07-07 16:38:18 -070066public class Folder extends LinearLayout implements DragSource, View.OnClickListener,
Adam Cohenea0818d2011-09-30 20:06:58 -070067 View.OnLongClickListener, DropTarget, FolderListener, TextView.OnEditorActionListener,
Sunny Goyale9b651e2015-04-24 11:44:51 -070068 View.OnFocusChangeListener, DragListener, UninstallSource, AccessibilityDragSource {
Adam Cohendf2cc412011-04-27 16:56:57 -070069 private static final String TAG = "Launcher.Folder";
70
Sunny Goyalc3a609f2015-02-26 17:43:50 -080071 /**
72 * We avoid measuring {@link #mContentWrapper} with a 0 width or height, as this
73 * results in CellLayout being measured as UNSPECIFIED, which it does not support.
74 */
75 private static final int MIN_CONTENT_DIMEN = 5;
Adam Cohen4eac29a2011-07-11 17:53:37 -070076
Adam Cohendf2cc412011-04-27 16:56:57 -070077 static final int STATE_NONE = -1;
78 static final int STATE_SMALL = 0;
79 static final int STATE_ANIMATING = 1;
80 static final int STATE_OPEN = 2;
81
Sunny Goyal48461932015-03-09 17:41:09 -070082 /**
Sunny Goyal48461932015-03-09 17:41:09 -070083 * Time for which the scroll hint is shown before automatically changing page.
84 */
85 public static final int SCROLL_HINT_DURATION = DragController.SCROLL_DELAY;
86
87 /**
88 * Fraction of icon width which behave as scroll region.
89 */
90 private static final float ICON_OVERSCROLL_WIDTH_FACTOR = 0.45f;
91
Sunny Goyalb7e15ad2015-05-07 14:51:31 -070092 public static final int FOOTER_ANIMATION_DURATION = 200;
93
Adam Cohenf0f4eda2013-06-06 21:27:03 -070094 private static final int REORDER_DELAY = 250;
Adam Cohen5d518fa2013-12-05 14:16:23 -080095 private static final int ON_EXIT_CLOSE_DELAY = 400;
Sunny Goyalc3a609f2015-02-26 17:43:50 -080096 private static final Rect sTempRect = new Rect();
97
98 private static String sDefaultFolderName;
99 private static String sHintText;
100
101 private final Alarm mReorderAlarm = new Alarm();
102 private final Alarm mOnExitAlarm = new Alarm();
Sunny Goyalb8634152015-04-09 14:17:14 -0700103 private final Alarm mOnScrollHintAlarm = new Alarm();
104 @Thunk final Alarm mScrollPauseAlarm = new Alarm();
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800105
Adam Cohen091440a2015-03-18 14:16:05 -0700106 @Thunk final ArrayList<View> mItemsInReadingOrder = new ArrayList<View>();
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800107
108 private final int mExpandDuration;
109 private final int mMaterialExpandDuration;
110 private final int mMaterialExpandStagger;
111
112 private final InputMethodManager mInputMethodManager;
113
114 protected final Launcher mLauncher;
115 protected DragController mDragController;
116 protected FolderInfo mInfo;
117
Adam Cohen091440a2015-03-18 14:16:05 -0700118 @Thunk FolderIcon mFolderIcon;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800119
Sunny Goyalb8634152015-04-09 14:17:14 -0700120 @Thunk FolderPagedView mContent;
Adam Cohen091440a2015-03-18 14:16:05 -0700121 @Thunk View mContentWrapper;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800122 FolderEditText mFolderName;
123
Sunny Goyal290800b2015-03-05 11:33:33 -0800124 private View mFooter;
125 private int mFooterHeight;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800126
127 // Cell ranks used for drag and drop
Adam Cohen091440a2015-03-18 14:16:05 -0700128 @Thunk int mTargetRank, mPrevTargetRank, mEmptyCellRank;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800129
Adam Cohen091440a2015-03-18 14:16:05 -0700130 @Thunk int mState = STATE_NONE;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800131 private boolean mRearrangeOnClose = false;
Adam Cohen7c693212011-05-18 15:26:57 -0700132 boolean mItemsInvalidated = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700133 private ShortcutInfo mCurrentDragInfo;
134 private View mCurrentDragView;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800135 private boolean mIsExternalDrag;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700136 boolean mSuppressOnAdd = false;
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700137 private boolean mDragInProgress = false;
138 private boolean mDeleteFolderOnDropCompleted = false;
139 private boolean mSuppressFolderDeletion = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700140 private boolean mItemAddedBackToSelfViaIcon = false;
Adam Cohen091440a2015-03-18 14:16:05 -0700141 @Thunk float mFolderIconPivotX;
142 @Thunk float mFolderIconPivotY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700143 private boolean mIsEditingName = false;
Adam Cohen1960ea42013-11-12 11:33:14 +0000144
Adam Cohenfb91f302012-06-11 15:45:18 -0700145 private boolean mDestroyed;
146
Adam Cohen091440a2015-03-18 14:16:05 -0700147 @Thunk Runnable mDeferredAction;
Michael Jurka1e2f4652013-07-08 18:03:46 -0700148 private boolean mDeferDropAfterUninstall;
149 private boolean mUninstallSuccessful;
150
Sunny Goyal48461932015-03-09 17:41:09 -0700151 // Folder scrolling
152 private int mScrollAreaOffset;
Sunny Goyal48461932015-03-09 17:41:09 -0700153
Adam Cohen091440a2015-03-18 14:16:05 -0700154 @Thunk int mScrollHintDir = DragController.SCROLL_NONE;
155 @Thunk int mCurrentScrollDir = DragController.SCROLL_NONE;
Sunny Goyal48461932015-03-09 17:41:09 -0700156
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157 /**
158 * Used to inflate the Workspace from XML.
159 *
160 * @param context The application's context.
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800161 * @param attrs The attributes set containing the Workspace's customization values.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162 */
163 public Folder(Context context, AttributeSet attrs) {
164 super(context, attrs);
165 setAlwaysDrawnWithCacheEnabled(false);
Adam Cohen76fc0852011-06-17 13:26:23 -0700166 mInputMethodManager = (InputMethodManager)
Michael Jurka8b805b12012-04-18 14:23:14 -0700167 getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
Adam Cohen76fc0852011-06-17 13:26:23 -0700168
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800169 Resources res = getResources();
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700170 mExpandDuration = res.getInteger(R.integer.config_folderExpandDuration);
171 mMaterialExpandDuration = res.getInteger(R.integer.config_materialFolderExpandDuration);
172 mMaterialExpandStagger = res.getInteger(R.integer.config_materialFolderExpandStagger);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700173
174 if (sDefaultFolderName == null) {
175 sDefaultFolderName = res.getString(R.string.folder_name);
176 }
Adam Cohena65beee2011-06-27 21:32:23 -0700177 if (sHintText == null) {
178 sHintText = res.getString(R.string.folder_hint_text);
179 }
Adam Cohen4eac29a2011-07-11 17:53:37 -0700180 mLauncher = (Launcher) context;
Adam Cohenac56cff2011-09-28 20:45:37 -0700181 // We need this view to be focusable in touch mode so that when text editing of the folder
182 // name is complete, we have something to focus on, thus hiding the cursor and giving
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800183 // reliable behavior when clicking the text field (since it will always gain focus on click).
Adam Cohenac56cff2011-09-28 20:45:37 -0700184 setFocusableInTouchMode(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185 }
186
187 @Override
188 protected void onFinishInflate() {
189 super.onFinishInflate();
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800190 mContentWrapper = findViewById(R.id.folder_content_wrapper);
Sunny Goyalb8634152015-04-09 14:17:14 -0700191 mContent = (FolderPagedView) findViewById(R.id.folder_content);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800192 mContent.setFolder(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700193
Adam Cohenac56cff2011-09-28 20:45:37 -0700194 mFolderName = (FolderEditText) findViewById(R.id.folder_name);
195 mFolderName.setFolder(this);
Adam Cohenea0818d2011-09-30 20:06:58 -0700196 mFolderName.setOnFocusChangeListener(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700197
Adam Cohen76fc0852011-06-17 13:26:23 -0700198 // We disable action mode for now since it messes up the view on phones
199 mFolderName.setCustomSelectionActionModeCallback(mActionModeCallback);
Adam Cohen76fc0852011-06-17 13:26:23 -0700200 mFolderName.setOnEditorActionListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700201 mFolderName.setSelectAllOnFocus(true);
Adam Cohen7a14d0b2011-06-24 11:36:35 -0700202 mFolderName.setInputType(mFolderName.getInputType() |
203 InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800204
Sunny Goyalb8634152015-04-09 14:17:14 -0700205 mFooter = findViewById(R.id.folder_footer);
Sunny Goyalb8634152015-04-09 14:17:14 -0700206
Sunny Goyal290800b2015-03-05 11:33:33 -0800207 // We find out how tall footer wants to be (it is set to wrap_content), so that
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800208 // we can allocate the appropriate amount of space for it.
209 int measureSpec = MeasureSpec.UNSPECIFIED;
Sunny Goyal290800b2015-03-05 11:33:33 -0800210 mFooter.measure(measureSpec, measureSpec);
211 mFooterHeight = mFooter.getMeasuredHeight();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800212 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700213
Adam Cohen76fc0852011-06-17 13:26:23 -0700214 private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
215 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
216 return false;
217 }
218
219 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
220 return false;
221 }
222
223 public void onDestroyActionMode(ActionMode mode) {
224 }
225
226 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
227 return false;
228 }
229 };
230
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800231 public void onClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700232 Object tag = v.getTag();
233 if (tag instanceof ShortcutInfo) {
Adam Cohenb5fe60c2013-06-06 22:03:51 -0700234 mLauncher.onClick(v);
Adam Cohendf2cc412011-04-27 16:56:57 -0700235 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800236 }
237
238 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -0700239 // Return if global dragging is not enabled
240 if (!mLauncher.isDraggingEnabled()) return true;
Sunny Goyale9b651e2015-04-24 11:44:51 -0700241 return beginDrag(v, false);
242 }
Winson Chung36a62fe2012-05-06 18:04:42 -0700243
Sunny Goyale9b651e2015-04-24 11:44:51 -0700244 private boolean beginDrag(View v, boolean accessible) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700245 Object tag = v.getTag();
246 if (tag instanceof ShortcutInfo) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700247 ShortcutInfo item = (ShortcutInfo) tag;
248 if (!v.isInTouchMode()) {
249 return false;
250 }
251
Sunny Goyale9b651e2015-04-24 11:44:51 -0700252 mLauncher.getWorkspace().beginDragShared(v, new Point(), this, accessible);
Adam Cohen76078c42011-06-09 15:06:52 -0700253
254 mCurrentDragInfo = item;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800255 mEmptyCellRank = item.rank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700256 mCurrentDragView = v;
Adam Cohenfc53cd22011-07-20 15:45:11 -0700257
Sunny Goyal290800b2015-03-05 11:33:33 -0800258 mContent.removeItem(mCurrentDragView);
Adam Cohenfc53cd22011-07-20 15:45:11 -0700259 mInfo.remove(mCurrentDragInfo);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700260 mDragInProgress = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700261 mItemAddedBackToSelfViaIcon = false;
Adam Cohendf2cc412011-04-27 16:56:57 -0700262 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800263 return true;
264 }
265
Sunny Goyale9b651e2015-04-24 11:44:51 -0700266 @Override
267 public void startDrag(CellInfo cellInfo, boolean accessible) {
268 beginDrag(cellInfo.cell, accessible);
269 }
270
271 @Override
272 public void enableAccessibleDrag(boolean enable) {
273 mLauncher.getSearchBar().enableAccessibleDrag(enable);
274 for (int i = 0; i < mContent.getChildCount(); i++) {
275 mContent.getPageAt(i).enableAccessibleDrag(enable, CellLayout.FOLDER_ACCESSIBILITY_DRAG);
276 }
277 mLauncher.getWorkspace().setAddNewPageOnDrag(!enable);
278 }
279
Adam Cohen76fc0852011-06-17 13:26:23 -0700280 public boolean isEditingName() {
281 return mIsEditingName;
282 }
283
284 public void startEditingFolderName() {
Adam Cohena65beee2011-06-27 21:32:23 -0700285 mFolderName.setHint("");
Adam Cohen76fc0852011-06-17 13:26:23 -0700286 mIsEditingName = true;
287 }
288
289 public void dismissEditingName() {
290 mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
291 doneEditingFolderName(true);
292 }
293
294 public void doneEditingFolderName(boolean commit) {
Adam Cohena65beee2011-06-27 21:32:23 -0700295 mFolderName.setHint(sHintText);
Adam Cohen1df26a32011-08-12 16:15:23 -0700296 // Convert to a string here to ensure that no other state associated with the text field
297 // gets saved.
Adam Cohen3371da02011-10-25 21:38:29 -0700298 String newTitle = mFolderName.getText().toString();
299 mInfo.setTitle(newTitle);
Adam Cohen76fc0852011-06-17 13:26:23 -0700300 LauncherModel.updateItemInDatabase(mLauncher, mInfo);
Adam Cohenac56cff2011-09-28 20:45:37 -0700301
Adam Cohen3371da02011-10-25 21:38:29 -0700302 if (commit) {
303 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700304 String.format(getContext().getString(R.string.folder_renamed), newTitle));
Adam Cohen3371da02011-10-25 21:38:29 -0700305 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700306 // In order to clear the focus from the text field, we set the focus on ourself. This
307 // ensures that every time the field is clicked, focus is gained, giving reliable behavior.
308 requestFocus();
309
Adam Cohene601a432011-07-26 21:51:30 -0700310 Selection.setSelection((Spannable) mFolderName.getText(), 0, 0);
Adam Cohen76fc0852011-06-17 13:26:23 -0700311 mIsEditingName = false;
312 }
313
314 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
315 if (actionId == EditorInfo.IME_ACTION_DONE) {
316 dismissEditingName();
317 return true;
318 }
319 return false;
320 }
321
322 public View getEditTextRegion() {
323 return mFolderName;
324 }
325
Adam Cohen0c872ba2011-05-05 10:34:16 -0700326 /**
327 * We need to handle touch events to prevent them from falling through to the workspace below.
328 */
329 @Override
330 public boolean onTouchEvent(MotionEvent ev) {
331 return true;
332 }
333
Joe Onorato00acb122009-08-04 16:04:30 -0400334 public void setDragController(DragController dragController) {
335 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800336 }
337
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800338 public void setFolderIcon(FolderIcon icon) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700339 mFolderIcon = icon;
340 }
341
Adam Cohen3371da02011-10-25 21:38:29 -0700342 @Override
343 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
344 // When the folder gets focus, we don't want to announce the list of items.
345 return true;
346 }
347
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800348 /**
349 * @return the FolderInfo object associated with this folder
350 */
351 FolderInfo getInfo() {
352 return mInfo;
353 }
354
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800355 void bind(FolderInfo info) {
356 mInfo = info;
Adam Cohendf2cc412011-04-27 16:56:57 -0700357 ArrayList<ShortcutInfo> children = info.contents;
Sunny Goyal08f72612015-01-05 13:41:43 -0800358 Collections.sort(children, Utilities.RANK_COMPARATOR);
Sunny Goyal08f72612015-01-05 13:41:43 -0800359
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800360 ArrayList<ShortcutInfo> overflow = mContent.bindItems(children);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700361
Jason Monk4ff73882014-04-24 16:48:07 -0400362 // If our folder has too many items we prune them from the list. This is an issue
Adam Cohenc508b2d2011-06-28 14:41:44 -0700363 // when upgrading from the old Folders implementation which could contain an unlimited
364 // number of items.
365 for (ShortcutInfo item: overflow) {
366 mInfo.remove(item);
367 LauncherModel.deleteItemFromDatabase(mLauncher, item);
368 }
369
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800370 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
371 if (lp == null) {
372 lp = new DragLayer.LayoutParams(0, 0);
373 lp.customPosition = true;
374 setLayoutParams(lp);
375 }
376 centerAboutIcon();
377
Adam Cohen4dbe6d92011-05-18 17:14:15 -0700378 mItemsInvalidated = true;
Adam Cohenac56cff2011-09-28 20:45:37 -0700379 updateTextViewFocus();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700380 mInfo.addListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700381
Adam Cohenafb01ee2011-06-23 15:38:03 -0700382 if (!sDefaultFolderName.contentEquals(mInfo.title)) {
Adam Cohen4ef610f2011-06-21 22:37:36 -0700383 mFolderName.setText(mInfo.title);
384 } else {
385 mFolderName.setText("");
386 }
Winson Chung33231f52013-12-09 16:57:45 -0800387
388 // In case any children didn't come across during loading, clean up the folder accordingly
389 mFolderIcon.post(new Runnable() {
390 public void run() {
391 if (getItemCount() <= 1) {
392 replaceFolderWithFinalItem();
393 }
394 }
395 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700396 }
397
398 /**
399 * Creates a new UserFolder, inflated from R.layout.user_folder.
400 *
401 * @param context The application's context.
402 *
403 * @return A new UserFolder.
404 */
405 static Folder fromXml(Context context) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700406 return (Folder) LayoutInflater.from(context).inflate(R.layout.user_folder, null);
Adam Cohendf2cc412011-04-27 16:56:57 -0700407 }
408
409 /**
410 * This method is intended to make the UserFolder to be visually identical in size and position
411 * to its associated FolderIcon. This allows for a seamless transition into the expanded state.
412 */
413 private void positionAndSizeAsIcon() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700414 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800415 setScaleX(0.8f);
416 setScaleY(0.8f);
417 setAlpha(0f);
Adam Cohendf2cc412011-04-27 16:56:57 -0700418 mState = STATE_SMALL;
419 }
420
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700421 private void prepareReveal() {
422 setScaleX(1f);
423 setScaleY(1f);
424 setAlpha(1f);
425 mState = STATE_SMALL;
426 }
427
Adam Cohendf2cc412011-04-27 16:56:57 -0700428 public void animateOpen() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700429 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen6441de02011-12-14 14:25:32 -0800430
Sunny Goyalb8634152015-04-09 14:17:14 -0700431 mContent.completePendingPageChanges();
Sunny Goyal8167dc22015-04-27 13:44:01 -0700432 if (!mDragInProgress) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700433 // Open on the first page.
434 mContent.snapToPageImmediately(0);
Sunny Goyal48461932015-03-09 17:41:09 -0700435 }
436
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700437 Animator openFolderAnim = null;
438 final Runnable onCompleteRunnable;
Kenny Guyd794a3f2014-09-16 15:17:58 +0100439 if (!Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700440 positionAndSizeAsIcon();
441 centerAboutIcon();
442
443 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1);
444 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
445 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
446 final ObjectAnimator oa =
447 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
448 oa.setDuration(mExpandDuration);
449 openFolderAnim = oa;
450
451 setLayerType(LAYER_TYPE_HARDWARE, null);
452 onCompleteRunnable = new Runnable() {
453 @Override
454 public void run() {
455 setLayerType(LAYER_TYPE_NONE, null);
456 }
457 };
458 } else {
459 prepareReveal();
460 centerAboutIcon();
461
462 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
463 int height = getFolderHeight();
464
465 float transX = - 0.075f * (width / 2 - getPivotX());
466 float transY = - 0.075f * (height / 2 - getPivotY());
467 setTranslationX(transX);
468 setTranslationY(transY);
469 PropertyValuesHolder tx = PropertyValuesHolder.ofFloat("translationX", transX, 0);
470 PropertyValuesHolder ty = PropertyValuesHolder.ofFloat("translationY", transY, 0);
471
472 int rx = (int) Math.max(Math.max(width - getPivotX(), 0), getPivotX());
473 int ry = (int) Math.max(Math.max(height - getPivotY(), 0), getPivotY());
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700474 float radius = (float) Math.hypot(rx, ry);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700475 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
476 Animator reveal = LauncherAnimUtils.createCircularReveal(this, (int) getPivotX(),
477 (int) getPivotY(), 0, radius);
478 reveal.setDuration(mMaterialExpandDuration);
479 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
480
Sunny Goyalbc753352015-03-05 09:40:44 -0800481 mContentWrapper.setAlpha(0f);
482 Animator iconsAlpha = LauncherAnimUtils.ofFloat(mContentWrapper, "alpha", 0f, 1f);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700483 iconsAlpha.setDuration(mMaterialExpandDuration);
484 iconsAlpha.setStartDelay(mMaterialExpandStagger);
485 iconsAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
486
Sunny Goyal290800b2015-03-05 11:33:33 -0800487 mFooter.setAlpha(0f);
488 Animator textAlpha = LauncherAnimUtils.ofFloat(mFooter, "alpha", 0f, 1f);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700489 textAlpha.setDuration(mMaterialExpandDuration);
490 textAlpha.setStartDelay(mMaterialExpandStagger);
491 textAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
492
493 Animator drift = LauncherAnimUtils.ofPropertyValuesHolder(this, tx, ty);
494 drift.setDuration(mMaterialExpandDuration);
495 drift.setStartDelay(mMaterialExpandStagger);
496 drift.setInterpolator(new LogDecelerateInterpolator(60, 0));
497
498 anim.play(drift);
499 anim.play(iconsAlpha);
500 anim.play(textAlpha);
501 anim.play(reveal);
502
503 openFolderAnim = anim;
504
Sunny Goyalbc753352015-03-05 09:40:44 -0800505 mContentWrapper.setLayerType(LAYER_TYPE_HARDWARE, null);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700506 onCompleteRunnable = new Runnable() {
507 @Override
508 public void run() {
Sunny Goyalbc753352015-03-05 09:40:44 -0800509 mContentWrapper.setLayerType(LAYER_TYPE_NONE, null);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700510 }
511 };
512 }
513 openFolderAnim.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700514 @Override
515 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700516 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Sunny Goyalbc753352015-03-05 09:40:44 -0800517 mContent.getAccessibilityDescription());
Adam Cohendf2cc412011-04-27 16:56:57 -0700518 mState = STATE_ANIMATING;
519 }
520 @Override
521 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700522 mState = STATE_OPEN;
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700523
524 if (onCompleteRunnable != null) {
525 onCompleteRunnable.run();
526 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800527
Sunny Goyalbc753352015-03-05 09:40:44 -0800528 mContent.setFocusOnFirstChild();
Adam Cohendf2cc412011-04-27 16:56:57 -0700529 }
530 });
Sunny Goyalb7e15ad2015-05-07 14:51:31 -0700531
532 // Footer animation
533 if (mContent.getPageCount() > 1 && !mInfo.hasOption(FolderInfo.FLAG_MULTI_PAGE_ANIMATION)) {
534 int footerWidth = mContent.getDesiredWidth()
535 - mFooter.getPaddingLeft() - mFooter.getPaddingRight();
536
537 float textWidth = mFolderName.getPaint().measureText(mFolderName.getText().toString());
538 mFolderName.setTranslationX((footerWidth - textWidth) / 2);
539 mContent.setMarkerScale(0);
540
541 // Do not update the flag if we are in drag mode. The flag will be updated, when we
542 // actually drop the icon.
543 final boolean updateAnimationFlag = !mDragInProgress;
544 openFolderAnim.addListener(new AnimatorListenerAdapter() {
545
546 @Override
547 public void onAnimationEnd(Animator animation) {
548 mFolderName.animate().setDuration(FOOTER_ANIMATION_DURATION).translationX(0);
549 mContent.animateMarkers();
550
551 if (updateAnimationFlag) {
552 mInfo.setOption(FolderInfo.FLAG_MULTI_PAGE_ANIMATION, true, mLauncher);
553 }
554 }
555 });
556 } else {
557 mFolderName.setTranslationX(0);
558 mContent.setMarkerScale(1);
559 }
560
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700561 openFolderAnim.start();
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800562
563 // Make sure the folder picks up the last drag move even if the finger doesn't move.
564 if (mDragController.isDragging()) {
565 mDragController.forceTouchMove();
566 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700567
Sunny Goyalb8634152015-04-09 14:17:14 -0700568 FolderPagedView pages = (FolderPagedView) mContent;
569 pages.verifyVisibleHighResIcons(pages.getNextPage());
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800570 }
571
572 public void beginExternalDrag(ShortcutInfo item) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800573 mCurrentDragInfo = item;
Sunny Goyal5d85c442015-03-10 13:14:47 -0700574 mEmptyCellRank = mContent.allocateRankForNewItem(item);
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800575 mIsExternalDrag = true;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800576 mDragInProgress = true;
Sunny Goyalb8634152015-04-09 14:17:14 -0700577
Sunny Goyalf4066152015-04-15 09:42:19 -0700578 // Since this folder opened by another controller, it might not get onDrop or
579 // onDropComplete. Perform cleanup once drag-n-drop ends.
580 mDragController.addDragListener(this);
581 }
582
583 @Override
584 public void onDragStart(DragSource source, Object info, int dragAction) { }
585
586 @Override
587 public void onDragEnd() {
588 if (mIsExternalDrag && mDragInProgress) {
589 completeDragExit();
590 }
591 mDragController.removeDragListener(this);
Adam Cohendf2cc412011-04-27 16:56:57 -0700592 }
593
Adam Cohen091440a2015-03-18 14:16:05 -0700594 @Thunk void sendCustomAccessibilityEvent(int type, String text) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700595 AccessibilityManager accessibilityManager = (AccessibilityManager)
596 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
597 if (accessibilityManager.isEnabled()) {
Adam Cohen3371da02011-10-25 21:38:29 -0700598 AccessibilityEvent event = AccessibilityEvent.obtain(type);
599 onInitializeAccessibilityEvent(event);
600 event.getText().add(text);
Michael Jurka8b805b12012-04-18 14:23:14 -0700601 accessibilityManager.sendAccessibilityEvent(event);
Adam Cohen3371da02011-10-25 21:38:29 -0700602 }
603 }
604
Adam Cohendf2cc412011-04-27 16:56:57 -0700605 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700606 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800607 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
608 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
609 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
Michael Jurka032e6ba2013-04-22 15:08:42 +0200610 final ObjectAnimator oa =
Michael Jurka2ecf9952012-06-18 12:52:28 -0700611 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohendf2cc412011-04-27 16:56:57 -0700612
Adam Cohen2801caf2011-05-13 20:57:39 -0700613 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700614 @Override
615 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700616 onCloseComplete();
Michael Jurka0121c3e2012-05-31 08:36:04 -0700617 setLayerType(LAYER_TYPE_NONE, null);
Adam Cohen2801caf2011-05-13 20:57:39 -0700618 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700619 }
620 @Override
621 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700622 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700623 getContext().getString(R.string.folder_closed));
Adam Cohendf2cc412011-04-27 16:56:57 -0700624 mState = STATE_ANIMATING;
625 }
626 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700627 oa.setDuration(mExpandDuration);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700628 setLayerType(LAYER_TYPE_HARDWARE, null);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100629 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700630 }
631
Adam Cohencb3382b2011-05-24 14:07:08 -0700632 public boolean acceptDrop(DragObject d) {
633 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700634 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700635 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
636 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
637 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700638 }
639
Adam Cohencb3382b2011-05-24 14:07:08 -0700640 public void onDragEnter(DragObject d) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800641 mPrevTargetRank = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700642 mOnExitAlarm.cancelAlarm();
Sunny Goyalb8634152015-04-09 14:17:14 -0700643 // Get the area offset such that the folder only closes if half the drag icon width
644 // is outside the folder area
645 mScrollAreaOffset = d.dragView.getDragRegionWidth() / 2 - d.xOffset;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700646 }
647
648 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
649 public void onAlarm(Alarm alarm) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800650 mContent.realTimeReorder(mEmptyCellRank, mTargetRank);
651 mEmptyCellRank = mTargetRank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700652 }
653 };
654
Sunny Goyalc46bfef2015-01-05 12:40:08 -0800655 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohen2374abf2013-04-16 14:56:57 -0700656 public boolean isLayoutRtl() {
657 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
658 }
659
Sunny Goyal48461932015-03-09 17:41:09 -0700660 @Override
Adam Cohencb3382b2011-05-24 14:07:08 -0700661 public void onDragOver(DragObject d) {
Sunny Goyal48461932015-03-09 17:41:09 -0700662 onDragOver(d, REORDER_DELAY);
663 }
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700664
Sunny Goyal48461932015-03-09 17:41:09 -0700665 private int getTargetRank(DragObject d, float[] recycle) {
666 recycle = d.getVisualCenter(recycle);
667 return mContent.findNearestArea(
668 (int) recycle[0] - getPaddingLeft(), (int) recycle[1] - getPaddingTop());
669 }
670
Adam Cohen091440a2015-03-18 14:16:05 -0700671 @Thunk void onDragOver(DragObject d, int reorderDelay) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700672 if (mScrollPauseAlarm.alarmPending()) {
Sunny Goyal48461932015-03-09 17:41:09 -0700673 return;
674 }
675 final float[] r = new float[2];
676 mTargetRank = getTargetRank(d, r);
677
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800678 if (mTargetRank != mPrevTargetRank) {
Alan Viverette4cda5b72013-08-28 17:53:41 -0700679 mReorderAlarm.cancelAlarm();
Sunny Goyalc46bfef2015-01-05 12:40:08 -0800680 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
681 mReorderAlarm.setAlarm(REORDER_DELAY);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800682 mPrevTargetRank = mTargetRank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700683 }
Sunny Goyal48461932015-03-09 17:41:09 -0700684
Sunny Goyal48461932015-03-09 17:41:09 -0700685 float x = r[0];
Sunny Goyalb8634152015-04-09 14:17:14 -0700686 int currentPage = mContent.getNextPage();
Sunny Goyal48461932015-03-09 17:41:09 -0700687
Sunny Goyalb8634152015-04-09 14:17:14 -0700688 float cellOverlap = mContent.getCurrentCellLayout().getCellWidth()
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700689 * ICON_OVERSCROLL_WIDTH_FACTOR;
690 boolean isOutsideLeftEdge = x < cellOverlap;
691 boolean isOutsideRightEdge = x > (getWidth() - cellOverlap);
Sunny Goyal48461932015-03-09 17:41:09 -0700692
Sunny Goyalb8634152015-04-09 14:17:14 -0700693 if (currentPage > 0 && (mContent.rtlLayout ? isOutsideRightEdge : isOutsideLeftEdge)) {
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700694 showScrollHint(DragController.SCROLL_LEFT, d);
Sunny Goyalb8634152015-04-09 14:17:14 -0700695 } else if (currentPage < (mContent.getPageCount() - 1)
696 && (mContent.rtlLayout ? isOutsideLeftEdge : isOutsideRightEdge)) {
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700697 showScrollHint(DragController.SCROLL_RIGHT, d);
Sunny Goyal48461932015-03-09 17:41:09 -0700698 } else {
699 mOnScrollHintAlarm.cancelAlarm();
700 if (mScrollHintDir != DragController.SCROLL_NONE) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700701 mContent.clearScrollHint();
Sunny Goyal48461932015-03-09 17:41:09 -0700702 mScrollHintDir = DragController.SCROLL_NONE;
703 }
704 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700705 }
706
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700707 private void showScrollHint(int direction, DragObject d) {
708 // Show scroll hint on the right
709 if (mScrollHintDir != direction) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700710 mContent.showScrollHint(direction);
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700711 mScrollHintDir = direction;
712 }
713
714 // Set alarm for when the hint is complete
715 if (!mOnScrollHintAlarm.alarmPending() || mCurrentScrollDir != direction) {
716 mCurrentScrollDir = direction;
717 mOnScrollHintAlarm.cancelAlarm();
718 mOnScrollHintAlarm.setOnAlarmListener(new OnScrollHintListener(d));
719 mOnScrollHintAlarm.setAlarm(SCROLL_HINT_DURATION);
720
721 mReorderAlarm.cancelAlarm();
722 mTargetRank = mEmptyCellRank;
723 }
724 }
725
Adam Cohenbfbfd262011-06-13 16:55:12 -0700726 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
727 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700728 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700729 }
730 };
731
Adam Cohen95bb8002011-07-03 23:40:28 -0700732 public void completeDragExit() {
Sunny Goyalf4066152015-04-15 09:42:19 -0700733 if (mInfo.opened) {
734 mLauncher.closeFolder();
735 mRearrangeOnClose = true;
Sunny Goyal31abc292015-05-01 10:42:32 -0700736 } else if (mState == STATE_ANIMATING) {
737 mRearrangeOnClose = true;
Sunny Goyalf4066152015-04-15 09:42:19 -0700738 } else {
739 rearrangeChildren();
Sunny Goyal31abc292015-05-01 10:42:32 -0700740 clearDragInfo();
Sunny Goyalf4066152015-04-15 09:42:19 -0700741 }
Sunny Goyal31abc292015-05-01 10:42:32 -0700742 }
743
744 private void clearDragInfo() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700745 mCurrentDragInfo = null;
746 mCurrentDragView = null;
747 mSuppressOnAdd = false;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800748 mIsExternalDrag = false;
Adam Cohen3e8f8112011-07-02 18:03:00 -0700749 }
750
Adam Cohencb3382b2011-05-24 14:07:08 -0700751 public void onDragExit(DragObject d) {
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700752 // We only close the folder if this is a true drag exit, ie. not because
753 // a drop has occurred above the folder.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700754 if (!d.dragComplete) {
755 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
756 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
757 }
758 mReorderAlarm.cancelAlarm();
Sunny Goyal48461932015-03-09 17:41:09 -0700759
Sunny Goyalb8634152015-04-09 14:17:14 -0700760 mOnScrollHintAlarm.cancelAlarm();
761 mScrollPauseAlarm.cancelAlarm();
762 if (mScrollHintDir != DragController.SCROLL_NONE) {
763 mContent.clearScrollHint();
764 mScrollHintDir = DragController.SCROLL_NONE;
Sunny Goyal48461932015-03-09 17:41:09 -0700765 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700766 }
767
Sunny Goyale9b651e2015-04-24 11:44:51 -0700768 /**
769 * When performing an accessibility drop, onDrop is sent immediately after onDragEnter. So we
770 * need to complete all transient states based on timers.
771 */
772 @Override
773 public void prepareAccessibilityDrop() {
774 if (mReorderAlarm.alarmPending()) {
775 mReorderAlarm.cancelAlarm();
776 mReorderAlarmListener.onAlarm(mReorderAlarm);
777 }
778 }
779
Michael Jurka1e2f4652013-07-08 18:03:46 -0700780 public void onDropCompleted(final View target, final DragObject d,
781 final boolean isFlingToDelete, final boolean success) {
782 if (mDeferDropAfterUninstall) {
Michael Jurkaf3007582013-08-21 14:33:57 +0200783 Log.d(TAG, "Deferred handling drop because waiting for uninstall.");
Michael Jurka1e2f4652013-07-08 18:03:46 -0700784 mDeferredAction = new Runnable() {
785 public void run() {
786 onDropCompleted(target, d, isFlingToDelete, success);
787 mDeferredAction = null;
788 }
789 };
790 return;
791 }
792
793 boolean beingCalledAfterUninstall = mDeferredAction != null;
794 boolean successfulDrop =
795 success && (!beingCalledAfterUninstall || mUninstallSuccessful);
Winson Chung5f8afe62013-08-12 16:19:28 -0700796
Michael Jurka1e2f4652013-07-08 18:03:46 -0700797 if (successfulDrop) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800798 if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon && target != this) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700799 replaceFolderWithFinalItem();
800 }
801 } else {
802 // The drag failed, we need to return the item to the folder
Sunny Goyalb8c663c2015-04-23 11:43:48 -0700803 ShortcutInfo info = (ShortcutInfo) d.dragInfo;
804 View icon = (mCurrentDragView != null && mCurrentDragView.getTag() == info)
805 ? mCurrentDragView : mContent.createNewView(info);
806 ArrayList<View> views = getItemsInReadingOrder();
807 views.add(info.rank, icon);
808 mContent.arrangeChildren(views, views.size());
809 mItemsInvalidated = true;
810
811 mSuppressOnAdd = true;
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700812 mFolderIcon.onDrop(d);
Sunny Goyalb8c663c2015-04-23 11:43:48 -0700813 mSuppressOnAdd = false;
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700814 }
815
816 if (target != this) {
817 if (mOnExitAlarm.alarmPending()) {
818 mOnExitAlarm.cancelAlarm();
Michael Jurka54554252013-08-01 12:52:23 +0200819 if (!successfulDrop) {
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700820 mSuppressFolderDeletion = true;
821 }
Sunny Goyal5d85c442015-03-10 13:14:47 -0700822 mScrollPauseAlarm.cancelAlarm();
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700823 completeDragExit();
824 }
Adam Cohen9c58d822013-12-13 17:18:10 -0800825 }
826
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700827 mDeleteFolderOnDropCompleted = false;
828 mDragInProgress = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700829 mItemAddedBackToSelfViaIcon = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700830 mCurrentDragInfo = null;
831 mCurrentDragView = null;
832 mSuppressOnAdd = false;
Adam Cohen4045eb72011-10-06 11:44:26 -0700833
834 // Reordering may have occured, and we need to save the new item locations. We do this once
835 // at the end to prevent unnecessary database operations.
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700836 updateItemLocationsInDatabaseBatch();
Sunny Goyalb7e15ad2015-05-07 14:51:31 -0700837
838 // Use the item count to check for multi-page as the folder UI may not have
839 // been refreshed yet.
840 if (getItemCount() <= mContent.itemsPerPage()) {
841 // Show the animation, next time something is added to the folder.
842 mInfo.setOption(FolderInfo.FLAG_MULTI_PAGE_ANIMATION, false, mLauncher);
843 }
844
Adam Cohen4045eb72011-10-06 11:44:26 -0700845 }
846
Sunny Goyalfa401a12015-04-10 13:45:42 -0700847 @Override
Michael Jurka1e2f4652013-07-08 18:03:46 -0700848 public void deferCompleteDropAfterUninstallActivity() {
849 mDeferDropAfterUninstall = true;
850 }
851
Sunny Goyalfa401a12015-04-10 13:45:42 -0700852 @Override
Michael Jurka1e2f4652013-07-08 18:03:46 -0700853 public void onUninstallActivityReturned(boolean success) {
854 mDeferDropAfterUninstall = false;
855 mUninstallSuccessful = success;
856 if (mDeferredAction != null) {
857 mDeferredAction.run();
858 }
859 }
860
Winson Chunga48487a2012-03-20 16:19:37 -0700861 @Override
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800862 public float getIntrinsicIconScaleFactor() {
863 return 1f;
864 }
865
866 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800867 public boolean supportsFlingToDelete() {
868 return true;
869 }
870
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000871 @Override
872 public boolean supportsAppInfoDropTarget() {
873 return false;
874 }
875
876 @Override
877 public boolean supportsDeleteDropTarget() {
878 return true;
879 }
880
Sunny Goyalddec7342015-04-29 18:12:37 -0700881 @Override
882 public void onFlingToDelete(DragObject d, PointF vec) {
Winson Chunga48487a2012-03-20 16:19:37 -0700883 // Do nothing
884 }
885
886 @Override
887 public void onFlingToDeleteCompleted() {
888 // Do nothing
889 }
890
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700891 private void updateItemLocationsInDatabaseBatch() {
892 ArrayList<View> list = getItemsInReadingOrder();
893 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
894 for (int i = 0; i < list.size(); i++) {
895 View v = list.get(i);
896 ItemInfo info = (ItemInfo) v.getTag();
Sunny Goyal08f72612015-01-05 13:41:43 -0800897 info.rank = i;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700898 items.add(info);
899 }
900
901 LauncherModel.moveItemsInDatabase(mLauncher, items, mInfo.id, 0);
902 }
903
Adam Cohene25af792013-06-06 23:08:25 -0700904 public void addItemLocationsInDatabase() {
905 ArrayList<View> list = getItemsInReadingOrder();
906 for (int i = 0; i < list.size(); i++) {
907 View v = list.get(i);
908 ItemInfo info = (ItemInfo) v.getTag();
909 LauncherModel.addItemToDatabase(mLauncher, info, mInfo.id, 0,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700910 info.cellX, info.cellY);
Adam Cohene25af792013-06-06 23:08:25 -0700911 }
912 }
913
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700914 public void notifyDrop() {
915 if (mDragInProgress) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700916 mItemAddedBackToSelfViaIcon = true;
Adam Cohen76078c42011-06-09 15:06:52 -0700917 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700918 }
919
920 public boolean isDropEnabled() {
921 return true;
922 }
923
Adam Cohen2801caf2011-05-13 20:57:39 -0700924 public boolean isFull() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800925 return mContent.isFull();
Adam Cohen2801caf2011-05-13 20:57:39 -0700926 }
927
928 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700929 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700930
Winson Chung892c74d2013-08-22 16:15:50 -0700931 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700932 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700933 int height = getFolderHeight();
Adam Cohen2801caf2011-05-13 20:57:39 -0700934
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800935 float scale = parent.getDescendantRectRelativeToSelf(mFolderIcon, sTempRect);
Adam Cohen8e776a62011-06-28 18:10:06 -0700936
Winson Chungaf40f202013-09-18 18:26:31 -0700937 LauncherAppState app = LauncherAppState.getInstance();
938 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
939
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800940 int centerX = (int) (sTempRect.left + sTempRect.width() * scale / 2);
941 int centerY = (int) (sTempRect.top + sTempRect.height() * scale / 2);
Adam Cohen2801caf2011-05-13 20:57:39 -0700942 int centeredLeft = centerX - width / 2;
943 int centeredTop = centerY - height / 2;
Winson Chung3057b1c2013-10-10 17:35:35 -0700944 int currentPage = mLauncher.getWorkspace().getNextPage();
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800945 // In case the workspace is scrolling, we need to use the final scroll to compute
946 // the folders bounds.
947 mLauncher.getWorkspace().setFinalScrollForPageChange(currentPage);
Adam Cohen35e7e642011-07-17 14:47:18 -0700948 // We first fetch the currently visible CellLayoutChildren
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800949 CellLayout currentLayout = (CellLayout) mLauncher.getWorkspace().getChildAt(currentPage);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700950 ShortcutAndWidgetContainer boundingLayout = currentLayout.getShortcutsAndWidgets();
Adam Cohen35e7e642011-07-17 14:47:18 -0700951 Rect bounds = new Rect();
952 parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800953 // We reset the workspaces scroll
954 mLauncher.getWorkspace().resetFinalScrollForPageChange(currentPage);
Adam Cohen2801caf2011-05-13 20:57:39 -0700955
Adam Cohen35e7e642011-07-17 14:47:18 -0700956 // We need to bound the folder to the currently visible CellLayoutChildren
957 int left = Math.min(Math.max(bounds.left, centeredLeft),
958 bounds.left + bounds.width() - width);
959 int top = Math.min(Math.max(bounds.top, centeredTop),
960 bounds.top + bounds.height() - height);
Winson Chungaf40f202013-09-18 18:26:31 -0700961 if (grid.isPhone() && (grid.availableWidthPx - width) < grid.iconSizePx) {
962 // Center the folder if it is full (on phones only)
963 left = (grid.availableWidthPx - width) / 2;
964 } else if (width >= bounds.width()) {
965 // If the folder doesn't fit within the bounds, center it about the desired bounds
Adam Cohen35e7e642011-07-17 14:47:18 -0700966 left = bounds.left + (bounds.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700967 }
Adam Cohen35e7e642011-07-17 14:47:18 -0700968 if (height >= bounds.height()) {
969 top = bounds.top + (bounds.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700970 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700971
972 int folderPivotX = width / 2 + (centeredLeft - left);
973 int folderPivotY = height / 2 + (centeredTop - top);
974 setPivotX(folderPivotX);
975 setPivotY(folderPivotY);
Adam Cohen268c4752012-06-06 17:47:33 -0700976 mFolderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700977 (1.0f * folderPivotX / width));
Adam Cohen268c4752012-06-06 17:47:33 -0700978 mFolderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700979 (1.0f * folderPivotY / height));
Adam Cohen3bf84d32012-05-07 20:17:14 -0700980
Adam Cohen662b5982011-12-13 17:45:21 -0800981 lp.width = width;
982 lp.height = height;
983 lp.x = left;
984 lp.y = top;
Adam Cohen2801caf2011-05-13 20:57:39 -0700985 }
986
Adam Cohen268c4752012-06-06 17:47:33 -0700987 float getPivotXForIconAnimation() {
988 return mFolderIconPivotX;
989 }
990 float getPivotYForIconAnimation() {
991 return mFolderIconPivotY;
992 }
993
Winson Chung892c74d2013-08-22 16:15:50 -0700994 private int getContentAreaHeight() {
995 LauncherAppState app = LauncherAppState.getInstance();
996 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
997 Rect workspacePadding = grid.getWorkspacePadding(grid.isLandscape ?
998 CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
999 int maxContentAreaHeight = grid.availableHeightPx -
Winson Chung892c74d2013-08-22 16:15:50 -07001000 workspacePadding.top - workspacePadding.bottom -
Sunny Goyal290800b2015-03-05 11:33:33 -08001001 mFooterHeight;
Adam Cohen1960ea42013-11-12 11:33:14 +00001002 int height = Math.min(maxContentAreaHeight,
Winson Chung892c74d2013-08-22 16:15:50 -07001003 mContent.getDesiredHeight());
Adam Cohen1960ea42013-11-12 11:33:14 +00001004 return Math.max(height, MIN_CONTENT_DIMEN);
1005 }
1006
1007 private int getContentAreaWidth() {
1008 return Math.max(mContent.getDesiredWidth(), MIN_CONTENT_DIMEN);
Winson Chung892c74d2013-08-22 16:15:50 -07001009 }
1010
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001011 private int getFolderHeight() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001012 return getFolderHeight(getContentAreaHeight());
1013 }
1014
1015 private int getFolderHeight(int contentAreaHeight) {
Sunny Goyal290800b2015-03-05 11:33:33 -08001016 return getPaddingTop() + getPaddingBottom() + contentAreaHeight + mFooterHeight;
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001017 }
Adam Cohen234c4cd2011-07-17 21:03:04 -07001018
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001019 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001020 int contentWidth = getContentAreaWidth();
1021 int contentHeight = getContentAreaHeight();
Adam Cohen1960ea42013-11-12 11:33:14 +00001022
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001023 int contentAreaWidthSpec = MeasureSpec.makeMeasureSpec(contentWidth, MeasureSpec.EXACTLY);
1024 int contentAreaHeightSpec = MeasureSpec.makeMeasureSpec(contentHeight, MeasureSpec.EXACTLY);
1025
1026 mContent.setFixedSize(contentWidth, contentHeight);
1027 mContentWrapper.measure(contentAreaWidthSpec, contentAreaHeightSpec);
Sunny Goyal290800b2015-03-05 11:33:33 -08001028 mFooter.measure(contentAreaWidthSpec,
1029 MeasureSpec.makeMeasureSpec(mFooterHeight, MeasureSpec.EXACTLY));
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001030
1031 int folderWidth = getPaddingLeft() + getPaddingRight() + contentWidth;
1032 int folderHeight = getFolderHeight(contentHeight);
1033 setMeasuredDimension(folderWidth, folderHeight);
Adam Cohen234c4cd2011-07-17 21:03:04 -07001034 }
1035
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001036 /**
1037 * Rearranges the children based on their rank.
1038 */
1039 public void rearrangeChildren() {
1040 rearrangeChildren(-1);
1041 }
Adam Cohen2801caf2011-05-13 20:57:39 -07001042
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001043 /**
1044 * Rearranges the children based on their rank.
1045 * @param itemCount if greater than the total children count, empty spaces are left at the end,
1046 * otherwise it is ignored.
1047 */
1048 public void rearrangeChildren(int itemCount) {
1049 ArrayList<View> views = getItemsInReadingOrder();
1050 mContent.arrangeChildren(views, Math.max(itemCount, views.size()));
Adam Cohen7c693212011-05-18 15:26:57 -07001051 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -07001052 }
1053
Sunny Goyalc4918352015-03-10 18:15:48 -07001054 // TODO remove this once GSA code fix is submitted
1055 public ViewGroup getContent() {
1056 return (ViewGroup) mContent;
1057 }
1058
Adam Cohena9cf38f2011-05-02 15:36:58 -07001059 public int getItemCount() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001060 return mContent.getItemCount();
Adam Cohena9cf38f2011-05-02 15:36:58 -07001061 }
1062
Adam Cohen091440a2015-03-18 14:16:05 -07001063 @Thunk void onCloseComplete() {
Adam Cohen05e0f402011-08-01 12:12:49 -07001064 DragLayer parent = (DragLayer) getParent();
Michael Jurka5649c282012-06-18 10:33:21 -07001065 if (parent != null) {
1066 parent.removeView(this);
1067 }
Adam Cohen4554ee12011-08-03 16:13:21 -07001068 mDragController.removeDropTarget((DropTarget) this);
Adam Cohen05e0f402011-08-01 12:12:49 -07001069 clearFocus();
Adam Cohenac56cff2011-09-28 20:45:37 -07001070 mFolderIcon.requestFocus();
Adam Cohen05e0f402011-08-01 12:12:49 -07001071
Adam Cohen2801caf2011-05-13 20:57:39 -07001072 if (mRearrangeOnClose) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001073 rearrangeChildren();
Adam Cohen2801caf2011-05-13 20:57:39 -07001074 mRearrangeOnClose = false;
1075 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001076 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001077 if (!mDragInProgress && !mSuppressFolderDeletion) {
1078 replaceFolderWithFinalItem();
1079 } else if (mDragInProgress) {
1080 mDeleteFolderOnDropCompleted = true;
1081 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001082 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001083 mSuppressFolderDeletion = false;
Sunny Goyal31abc292015-05-01 10:42:32 -07001084 clearDragInfo();
Adam Cohenafb01ee2011-06-23 15:38:03 -07001085 }
1086
Adam Cohen091440a2015-03-18 14:16:05 -07001087 @Thunk void replaceFolderWithFinalItem() {
Adam Cohenafb01ee2011-06-23 15:38:03 -07001088 // Add the last remaining child to the workspace in place of the folder
Adam Cohenfb91f302012-06-11 15:45:18 -07001089 Runnable onCompleteRunnable = new Runnable() {
1090 @Override
1091 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -07001092 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screenId);
Adam Cohenafb01ee2011-06-23 15:38:03 -07001093
Winson Chung33231f52013-12-09 16:57:45 -08001094 View child = null;
Adam Cohenfb91f302012-06-11 15:45:18 -07001095 // Move the item from the folder to the workspace, in the position of the folder
1096 if (getItemCount() == 1) {
1097 ShortcutInfo finalItem = mInfo.contents.get(0);
Adam Cohenc5e63f32012-07-12 16:16:57 -07001098 child = mLauncher.createShortcut(R.layout.application, cellLayout,
Adam Cohenfb91f302012-06-11 15:45:18 -07001099 finalItem);
1100 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -07001101 mInfo.screenId, mInfo.cellX, mInfo.cellY);
Adam Cohenfb91f302012-06-11 15:45:18 -07001102 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001103 if (getItemCount() <= 1) {
1104 // Remove the folder
1105 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
Dan Sandler0eb687f2014-01-10 13:24:55 -05001106 if (cellLayout != null) {
1107 // b/12446428 -- sometimes the cell layout has already gone away?
1108 cellLayout.removeView(mFolderIcon);
1109 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001110 if (mFolderIcon instanceof DropTarget) {
1111 mDragController.removeDropTarget((DropTarget) mFolderIcon);
1112 }
1113 mLauncher.removeFolder(mInfo);
1114 }
Adam Cohenc5e63f32012-07-12 16:16:57 -07001115 // We add the child after removing the folder to prevent both from existing at
Winson Chung0e6a7132013-08-23 12:55:10 -07001116 // the same time in the CellLayout. We need to add the new item with addInScreenFromBind()
1117 // to ensure that hotseat items are placed correctly.
Adam Cohenc5e63f32012-07-12 16:16:57 -07001118 if (child != null) {
Winson Chung0e6a7132013-08-23 12:55:10 -07001119 mLauncher.getWorkspace().addInScreenFromBind(child, mInfo.container, mInfo.screenId,
Adam Cohenc5e63f32012-07-12 16:16:57 -07001120 mInfo.cellX, mInfo.cellY, mInfo.spanX, mInfo.spanY);
1121 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001122 }
1123 };
Sunny Goyalbc753352015-03-05 09:40:44 -08001124 View finalChild = mContent.getLastItem();
Adam Cohenfb91f302012-06-11 15:45:18 -07001125 if (finalChild != null) {
1126 mFolderIcon.performDestroyAnimation(finalChild, onCompleteRunnable);
Winson Chung33231f52013-12-09 16:57:45 -08001127 } else {
1128 onCompleteRunnable.run();
Adam Cohenafb01ee2011-06-23 15:38:03 -07001129 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001130 mDestroyed = true;
1131 }
1132
1133 boolean isDestroyed() {
1134 return mDestroyed;
Adam Cohen2801caf2011-05-13 20:57:39 -07001135 }
1136
Adam Cohenac56cff2011-09-28 20:45:37 -07001137 // This method keeps track of the last item in the folder for the purposes
1138 // of keyboard focus
Sunny Goyal290800b2015-03-05 11:33:33 -08001139 public void updateTextViewFocus() {
Sunny Goyalbc753352015-03-05 09:40:44 -08001140 View lastChild = mContent.getLastItem();
Adam Cohenac56cff2011-09-28 20:45:37 -07001141 if (lastChild != null) {
1142 mFolderName.setNextFocusDownId(lastChild.getId());
1143 mFolderName.setNextFocusRightId(lastChild.getId());
1144 mFolderName.setNextFocusLeftId(lastChild.getId());
1145 mFolderName.setNextFocusUpId(lastChild.getId());
1146 }
1147 }
1148
Adam Cohenbfbfd262011-06-13 16:55:12 -07001149 public void onDrop(DragObject d) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001150 Runnable cleanUpRunnable = null;
1151
Adam Cohen689ff162014-05-08 17:27:56 -07001152 // If we are coming from All Apps space, we defer removing the extra empty screen
1153 // until the folder closes
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001154 if (d.dragSource != mLauncher.getWorkspace() && !(d.dragSource instanceof Folder)) {
1155 cleanUpRunnable = new Runnable() {
1156 @Override
1157 public void run() {
Adam Cohen689ff162014-05-08 17:27:56 -07001158 mLauncher.exitSpringLoadedDragModeDelayed(true,
1159 Launcher.EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
1160 null);
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001161 }
1162 };
Adam Cohenbfbfd262011-06-13 16:55:12 -07001163 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001164
Sunny Goyalb8634152015-04-09 14:17:14 -07001165 // If the icon was dropped while the page was being scrolled, we need to compute
1166 // the target location again such that the icon is placed of the final page.
1167 if (!mContent.rankOnCurrentPage(mEmptyCellRank)) {
1168 // Reorder again.
1169 mTargetRank = getTargetRank(d, null);
Sunny Goyal48461932015-03-09 17:41:09 -07001170
Sunny Goyalb8634152015-04-09 14:17:14 -07001171 // Rearrange items immediately.
1172 mReorderAlarmListener.onAlarm(mReorderAlarm);
Sunny Goyal48461932015-03-09 17:41:09 -07001173
Sunny Goyalb8634152015-04-09 14:17:14 -07001174 mOnScrollHintAlarm.cancelAlarm();
1175 mScrollPauseAlarm.cancelAlarm();
Sunny Goyal48461932015-03-09 17:41:09 -07001176 }
Sunny Goyalb8634152015-04-09 14:17:14 -07001177 mContent.completePendingPageChanges();
Sunny Goyal48461932015-03-09 17:41:09 -07001178
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001179 View currentDragView;
1180 ShortcutInfo si = mCurrentDragInfo;
1181 if (mIsExternalDrag) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001182 currentDragView = mContent.createAndAddViewForRank(si, mEmptyCellRank);
Sunny Goyal95abbb32014-08-04 10:53:22 -07001183 // Actually move the item in the database if it was an external drag. Call this
1184 // before creating the view, so that ShortcutInfo is updated appropriately.
1185 LauncherModel.addOrMoveItemInDatabase(
1186 mLauncher, si, mInfo.id, 0, si.cellX, si.cellY);
1187
1188 // We only need to update the locations if it doesn't get handled in #onDropCompleted.
1189 if (d.dragSource != this) {
1190 updateItemLocationsInDatabaseBatch();
1191 }
1192 mIsExternalDrag = false;
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001193 } else {
1194 currentDragView = mCurrentDragView;
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001195 mContent.addViewForRank(currentDragView, si, mEmptyCellRank);
Adam Cohenbfbfd262011-06-13 16:55:12 -07001196 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001197
1198 if (d.dragView.hasDrawn()) {
1199
1200 // Temporarily reset the scale such that the animation target gets calculated correctly.
1201 float scaleX = getScaleX();
1202 float scaleY = getScaleY();
1203 setScaleX(1.0f);
1204 setScaleY(1.0f);
1205 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, currentDragView,
1206 cleanUpRunnable, null);
1207 setScaleX(scaleX);
1208 setScaleY(scaleY);
1209 } else {
1210 d.deferDragViewCleanupPostAnimation = false;
1211 currentDragView.setVisibility(VISIBLE);
1212 }
1213 mItemsInvalidated = true;
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001214 rearrangeChildren();
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001215
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001216 // Temporarily suppress the listener, as we did all the work already here.
1217 mSuppressOnAdd = true;
1218 mInfo.add(si);
1219 mSuppressOnAdd = false;
Sunny Goyal4b020172014-08-28 14:51:14 -07001220 // Clear the drag info, as it is no longer being dragged.
1221 mCurrentDragInfo = null;
Sunny Goyalf4066152015-04-15 09:42:19 -07001222 mDragInProgress = false;
Sunny Goyalb7e15ad2015-05-07 14:51:31 -07001223
1224 if (mContent.getPageCount() > 1) {
1225 // The animation has already been shown while opening the folder.
1226 mInfo.setOption(FolderInfo.FLAG_MULTI_PAGE_ANIMATION, true, mLauncher);
1227 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001228 }
1229
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001230 // This is used so the item doesn't immediately appear in the folder when added. In one case
1231 // we need to create the illusion that the item isn't added back to the folder yet, to
1232 // to correspond to the animation of the icon back into the folder. This is
1233 public void hideItem(ShortcutInfo info) {
1234 View v = getViewForInfo(info);
1235 v.setVisibility(INVISIBLE);
1236 }
1237 public void showItem(ShortcutInfo info) {
1238 View v = getViewForInfo(info);
1239 v.setVisibility(VISIBLE);
1240 }
1241
Sunny Goyalb7e15ad2015-05-07 14:51:31 -07001242 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -07001243 public void onAdd(ShortcutInfo item) {
Adam Cohen05e0f402011-08-01 12:12:49 -07001244 // If the item was dropped onto this open folder, we have done the work associated
1245 // with adding the item to the folder, as indicated by mSuppressOnAdd being set
Adam Cohenbfbfd262011-06-13 16:55:12 -07001246 if (mSuppressOnAdd) return;
Sunny Goyal5d85c442015-03-10 13:14:47 -07001247 mContent.createAndAddViewForRank(item, mContent.allocateRankForNewItem(item));
Sunny Goyal2e688a82015-03-18 10:23:39 -07001248 mItemsInvalidated = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001249 LauncherModel.addOrMoveItemInDatabase(
1250 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
1251 }
1252
Adam Cohena9cf38f2011-05-02 15:36:58 -07001253 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -07001254 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001255 // If this item is being dragged from this open folder, we have already handled
1256 // the work associated with removing the item, so we don't have to do anything here.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001257 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001258 View v = getViewForInfo(item);
Sunny Goyal290800b2015-03-05 11:33:33 -08001259 mContent.removeItem(v);
Adam Cohen2801caf2011-05-13 20:57:39 -07001260 if (mState == STATE_ANIMATING) {
1261 mRearrangeOnClose = true;
1262 } else {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001263 rearrangeChildren();
Adam Cohen2801caf2011-05-13 20:57:39 -07001264 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001265 if (getItemCount() <= 1) {
1266 replaceFolderWithFinalItem();
1267 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001268 }
Adam Cohen7c693212011-05-18 15:26:57 -07001269
Sunny Goyalbc753352015-03-05 09:40:44 -08001270 private View getViewForInfo(final ShortcutInfo item) {
1271 return mContent.iterateOverItems(new ItemOperator() {
1272
1273 @Override
1274 public boolean evaluate(ItemInfo info, View view, View parent) {
1275 return info == item;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001276 }
Sunny Goyalbc753352015-03-05 09:40:44 -08001277 });
Adam Cohendf1e4e82011-06-24 15:57:39 -07001278 }
1279
Adam Cohen76078c42011-06-09 15:06:52 -07001280 public void onItemsChanged() {
Adam Cohenac56cff2011-09-28 20:45:37 -07001281 updateTextViewFocus();
Adam Cohen76078c42011-06-09 15:06:52 -07001282 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001283
Adam Cohen76fc0852011-06-17 13:26:23 -07001284 public void onTitleChanged(CharSequence title) {
1285 }
Adam Cohen76078c42011-06-09 15:06:52 -07001286
Adam Cohen7c693212011-05-18 15:26:57 -07001287 public ArrayList<View> getItemsInReadingOrder() {
1288 if (mItemsInvalidated) {
1289 mItemsInReadingOrder.clear();
Sunny Goyalbc753352015-03-05 09:40:44 -08001290 mContent.iterateOverItems(new ItemOperator() {
1291
1292 @Override
1293 public boolean evaluate(ItemInfo info, View view, View parent) {
1294 mItemsInReadingOrder.add(view);
1295 return false;
Adam Cohen7c693212011-05-18 15:26:57 -07001296 }
Sunny Goyalbc753352015-03-05 09:40:44 -08001297 });
Adam Cohen7c693212011-05-18 15:26:57 -07001298 mItemsInvalidated = false;
1299 }
1300 return mItemsInReadingOrder;
1301 }
Adam Cohen8dfcba42011-07-07 16:38:18 -07001302
1303 public void getLocationInDragLayer(int[] loc) {
1304 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
1305 }
Adam Cohenea0818d2011-09-30 20:06:58 -07001306
1307 public void onFocusChange(View v, boolean hasFocus) {
1308 if (v == mFolderName && hasFocus) {
1309 startEditingFolderName();
1310 }
1311 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001312
1313 @Override
1314 public void getHitRectRelativeToDragLayer(Rect outRect) {
1315 getHitRect(outRect);
Sunny Goyal48461932015-03-09 17:41:09 -07001316 outRect.left -= mScrollAreaOffset;
1317 outRect.right += mScrollAreaOffset;
1318 }
1319
1320 private class OnScrollHintListener implements OnAlarmListener {
1321
1322 private final DragObject mDragObject;
1323
1324 OnScrollHintListener(DragObject object) {
1325 mDragObject = object;
1326 }
1327
1328 /**
1329 * Scroll hint has been shown long enough. Now scroll to appropriate page.
1330 */
1331 @Override
1332 public void onAlarm(Alarm alarm) {
1333 if (mCurrentScrollDir == DragController.SCROLL_LEFT) {
Sunny Goyalb8634152015-04-09 14:17:14 -07001334 mContent.scrollLeft();
Sunny Goyal48461932015-03-09 17:41:09 -07001335 mScrollHintDir = DragController.SCROLL_NONE;
1336 } else if (mCurrentScrollDir == DragController.SCROLL_RIGHT) {
Sunny Goyalb8634152015-04-09 14:17:14 -07001337 mContent.scrollRight();
Sunny Goyal48461932015-03-09 17:41:09 -07001338 mScrollHintDir = DragController.SCROLL_NONE;
1339 } else {
1340 // This should not happen
1341 return;
1342 }
1343 mCurrentScrollDir = DragController.SCROLL_NONE;
1344
1345 // Pause drag event until the scrolling is finished
1346 mScrollPauseAlarm.setOnAlarmListener(new OnScrollFinishedListener(mDragObject));
1347 mScrollPauseAlarm.setAlarm(DragController.RESCROLL_DELAY);
1348 }
1349 }
1350
1351 private class OnScrollFinishedListener implements OnAlarmListener {
1352
1353 private final DragObject mDragObject;
1354
1355 OnScrollFinishedListener(DragObject object) {
1356 mDragObject = object;
1357 }
1358
1359 /**
1360 * Page scroll is complete.
1361 */
1362 @Override
1363 public void onAlarm(Alarm alarm) {
1364 // Reorder immediately on page change.
1365 onDragOver(mDragObject, 1);
1366 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001367 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001368}