blob: 7a3ae399505595a5c078f636f8af431b0613a6d3 [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
Adam Cohenf0f4eda2013-06-06 21:27:03 -070092 private static final int REORDER_DELAY = 250;
Adam Cohen5d518fa2013-12-05 14:16:23 -080093 private static final int ON_EXIT_CLOSE_DELAY = 400;
Sunny Goyalc3a609f2015-02-26 17:43:50 -080094 private static final Rect sTempRect = new Rect();
95
96 private static String sDefaultFolderName;
97 private static String sHintText;
98
99 private final Alarm mReorderAlarm = new Alarm();
100 private final Alarm mOnExitAlarm = new Alarm();
Sunny Goyalb8634152015-04-09 14:17:14 -0700101 private final Alarm mOnScrollHintAlarm = new Alarm();
102 @Thunk final Alarm mScrollPauseAlarm = new Alarm();
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800103
Adam Cohen091440a2015-03-18 14:16:05 -0700104 @Thunk final ArrayList<View> mItemsInReadingOrder = new ArrayList<View>();
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800105
106 private final int mExpandDuration;
107 private final int mMaterialExpandDuration;
108 private final int mMaterialExpandStagger;
109
110 private final InputMethodManager mInputMethodManager;
111
112 protected final Launcher mLauncher;
113 protected DragController mDragController;
114 protected FolderInfo mInfo;
115
Adam Cohen091440a2015-03-18 14:16:05 -0700116 @Thunk FolderIcon mFolderIcon;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800117
Sunny Goyalb8634152015-04-09 14:17:14 -0700118 @Thunk FolderPagedView mContent;
Adam Cohen091440a2015-03-18 14:16:05 -0700119 @Thunk View mContentWrapper;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800120 FolderEditText mFolderName;
121
Sunny Goyal290800b2015-03-05 11:33:33 -0800122 private View mFooter;
123 private int mFooterHeight;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800124
125 // Cell ranks used for drag and drop
Adam Cohen091440a2015-03-18 14:16:05 -0700126 @Thunk int mTargetRank, mPrevTargetRank, mEmptyCellRank;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800127
Adam Cohen091440a2015-03-18 14:16:05 -0700128 @Thunk int mState = STATE_NONE;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800129 private boolean mRearrangeOnClose = false;
Adam Cohen7c693212011-05-18 15:26:57 -0700130 boolean mItemsInvalidated = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700131 private ShortcutInfo mCurrentDragInfo;
132 private View mCurrentDragView;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800133 private boolean mIsExternalDrag;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700134 boolean mSuppressOnAdd = false;
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700135 private boolean mDragInProgress = false;
136 private boolean mDeleteFolderOnDropCompleted = false;
137 private boolean mSuppressFolderDeletion = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700138 private boolean mItemAddedBackToSelfViaIcon = false;
Adam Cohen091440a2015-03-18 14:16:05 -0700139 @Thunk float mFolderIconPivotX;
140 @Thunk float mFolderIconPivotY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700141 private boolean mIsEditingName = false;
Adam Cohen1960ea42013-11-12 11:33:14 +0000142
Adam Cohenfb91f302012-06-11 15:45:18 -0700143 private boolean mDestroyed;
144
Adam Cohen091440a2015-03-18 14:16:05 -0700145 @Thunk Runnable mDeferredAction;
Michael Jurka1e2f4652013-07-08 18:03:46 -0700146 private boolean mDeferDropAfterUninstall;
147 private boolean mUninstallSuccessful;
148
Sunny Goyal48461932015-03-09 17:41:09 -0700149 // Folder scrolling
150 private int mScrollAreaOffset;
Sunny Goyal48461932015-03-09 17:41:09 -0700151
Adam Cohen091440a2015-03-18 14:16:05 -0700152 @Thunk int mScrollHintDir = DragController.SCROLL_NONE;
153 @Thunk int mCurrentScrollDir = DragController.SCROLL_NONE;
Sunny Goyal48461932015-03-09 17:41:09 -0700154
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155 /**
156 * Used to inflate the Workspace from XML.
157 *
158 * @param context The application's context.
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800159 * @param attrs The attributes set containing the Workspace's customization values.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160 */
161 public Folder(Context context, AttributeSet attrs) {
162 super(context, attrs);
163 setAlwaysDrawnWithCacheEnabled(false);
Adam Cohen76fc0852011-06-17 13:26:23 -0700164 mInputMethodManager = (InputMethodManager)
Michael Jurka8b805b12012-04-18 14:23:14 -0700165 getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
Adam Cohen76fc0852011-06-17 13:26:23 -0700166
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800167 Resources res = getResources();
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700168 mExpandDuration = res.getInteger(R.integer.config_folderExpandDuration);
169 mMaterialExpandDuration = res.getInteger(R.integer.config_materialFolderExpandDuration);
170 mMaterialExpandStagger = res.getInteger(R.integer.config_materialFolderExpandStagger);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700171
172 if (sDefaultFolderName == null) {
173 sDefaultFolderName = res.getString(R.string.folder_name);
174 }
Adam Cohena65beee2011-06-27 21:32:23 -0700175 if (sHintText == null) {
176 sHintText = res.getString(R.string.folder_hint_text);
177 }
Adam Cohen4eac29a2011-07-11 17:53:37 -0700178 mLauncher = (Launcher) context;
Adam Cohenac56cff2011-09-28 20:45:37 -0700179 // We need this view to be focusable in touch mode so that when text editing of the folder
180 // name is complete, we have something to focus on, thus hiding the cursor and giving
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800181 // reliable behavior when clicking the text field (since it will always gain focus on click).
Adam Cohenac56cff2011-09-28 20:45:37 -0700182 setFocusableInTouchMode(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 }
184
185 @Override
186 protected void onFinishInflate() {
187 super.onFinishInflate();
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800188 mContentWrapper = findViewById(R.id.folder_content_wrapper);
Sunny Goyalb8634152015-04-09 14:17:14 -0700189 mContent = (FolderPagedView) findViewById(R.id.folder_content);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800190 mContent.setFolder(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700191
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
Adam Cohen76fc0852011-06-17 13:26:23 -0700196 // We disable action mode for now since it messes up the view on phones
197 mFolderName.setCustomSelectionActionModeCallback(mActionModeCallback);
Adam Cohen76fc0852011-06-17 13:26:23 -0700198 mFolderName.setOnEditorActionListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700199 mFolderName.setSelectAllOnFocus(true);
Adam Cohen7a14d0b2011-06-24 11:36:35 -0700200 mFolderName.setInputType(mFolderName.getInputType() |
201 InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800202
Sunny Goyalb8634152015-04-09 14:17:14 -0700203 mFooter = findViewById(R.id.folder_footer);
204 updateFooterHeight();
205 }
206
207 public void updateFooterHeight() {
Sunny Goyal290800b2015-03-05 11:33:33 -0800208 // We find out how tall footer wants to be (it is set to wrap_content), so that
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800209 // we can allocate the appropriate amount of space for it.
210 int measureSpec = MeasureSpec.UNSPECIFIED;
Sunny Goyal290800b2015-03-05 11:33:33 -0800211 mFooter.measure(measureSpec, measureSpec);
212 mFooterHeight = mFooter.getMeasuredHeight();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800213 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700214
Adam Cohen76fc0852011-06-17 13:26:23 -0700215 private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
216 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
217 return false;
218 }
219
220 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
221 return false;
222 }
223
224 public void onDestroyActionMode(ActionMode mode) {
225 }
226
227 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
228 return false;
229 }
230 };
231
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800232 public void onClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700233 Object tag = v.getTag();
234 if (tag instanceof ShortcutInfo) {
Adam Cohenb5fe60c2013-06-06 22:03:51 -0700235 mLauncher.onClick(v);
Adam Cohendf2cc412011-04-27 16:56:57 -0700236 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800237 }
238
239 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -0700240 // Return if global dragging is not enabled
241 if (!mLauncher.isDraggingEnabled()) return true;
Sunny Goyale9b651e2015-04-24 11:44:51 -0700242 return beginDrag(v, false);
243 }
Winson Chung36a62fe2012-05-06 18:04:42 -0700244
Sunny Goyale9b651e2015-04-24 11:44:51 -0700245 private boolean beginDrag(View v, boolean accessible) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700246 Object tag = v.getTag();
247 if (tag instanceof ShortcutInfo) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700248 ShortcutInfo item = (ShortcutInfo) tag;
249 if (!v.isInTouchMode()) {
250 return false;
251 }
252
Sunny Goyale9b651e2015-04-24 11:44:51 -0700253 mLauncher.getWorkspace().beginDragShared(v, new Point(), this, accessible);
Adam Cohen76078c42011-06-09 15:06:52 -0700254
255 mCurrentDragInfo = item;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800256 mEmptyCellRank = item.rank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700257 mCurrentDragView = v;
Adam Cohenfc53cd22011-07-20 15:45:11 -0700258
Sunny Goyal290800b2015-03-05 11:33:33 -0800259 mContent.removeItem(mCurrentDragView);
Adam Cohenfc53cd22011-07-20 15:45:11 -0700260 mInfo.remove(mCurrentDragInfo);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700261 mDragInProgress = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700262 mItemAddedBackToSelfViaIcon = false;
Adam Cohendf2cc412011-04-27 16:56:57 -0700263 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800264 return true;
265 }
266
Sunny Goyale9b651e2015-04-24 11:44:51 -0700267 @Override
268 public void startDrag(CellInfo cellInfo, boolean accessible) {
269 beginDrag(cellInfo.cell, accessible);
270 }
271
272 @Override
273 public void enableAccessibleDrag(boolean enable) {
274 mLauncher.getSearchBar().enableAccessibleDrag(enable);
275 for (int i = 0; i < mContent.getChildCount(); i++) {
276 mContent.getPageAt(i).enableAccessibleDrag(enable, CellLayout.FOLDER_ACCESSIBILITY_DRAG);
277 }
278 mLauncher.getWorkspace().setAddNewPageOnDrag(!enable);
279 }
280
Adam Cohen76fc0852011-06-17 13:26:23 -0700281 public boolean isEditingName() {
282 return mIsEditingName;
283 }
284
285 public void startEditingFolderName() {
Adam Cohena65beee2011-06-27 21:32:23 -0700286 mFolderName.setHint("");
Adam Cohen76fc0852011-06-17 13:26:23 -0700287 mIsEditingName = true;
288 }
289
290 public void dismissEditingName() {
291 mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
292 doneEditingFolderName(true);
293 }
294
295 public void doneEditingFolderName(boolean commit) {
Adam Cohena65beee2011-06-27 21:32:23 -0700296 mFolderName.setHint(sHintText);
Adam Cohen1df26a32011-08-12 16:15:23 -0700297 // Convert to a string here to ensure that no other state associated with the text field
298 // gets saved.
Winson Chung82b016c2015-05-08 17:00:10 -0700299 CharSequence newTitle = mFolderName.getText();
Adam Cohen3371da02011-10-25 21:38:29 -0700300 mInfo.setTitle(newTitle);
Adam Cohen76fc0852011-06-17 13:26:23 -0700301 LauncherModel.updateItemInDatabase(mLauncher, mInfo);
Adam Cohenac56cff2011-09-28 20:45:37 -0700302
Adam Cohen3371da02011-10-25 21:38:29 -0700303 if (commit) {
304 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Winson Chung82b016c2015-05-08 17:00:10 -0700305 String.format(getContext().getString(R.string.folder_renamed),
306 newTitle.toString()));
Adam Cohen3371da02011-10-25 21:38:29 -0700307 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700308 // In order to clear the focus from the text field, we set the focus on ourself. This
309 // ensures that every time the field is clicked, focus is gained, giving reliable behavior.
310 requestFocus();
311
Adam Cohene601a432011-07-26 21:51:30 -0700312 Selection.setSelection((Spannable) mFolderName.getText(), 0, 0);
Adam Cohen76fc0852011-06-17 13:26:23 -0700313 mIsEditingName = false;
314 }
315
316 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
317 if (actionId == EditorInfo.IME_ACTION_DONE) {
318 dismissEditingName();
319 return true;
320 }
321 return false;
322 }
323
324 public View getEditTextRegion() {
325 return mFolderName;
326 }
327
Adam Cohen0c872ba2011-05-05 10:34:16 -0700328 /**
329 * We need to handle touch events to prevent them from falling through to the workspace below.
330 */
331 @Override
332 public boolean onTouchEvent(MotionEvent ev) {
333 return true;
334 }
335
Joe Onorato00acb122009-08-04 16:04:30 -0400336 public void setDragController(DragController dragController) {
337 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800338 }
339
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800340 public void setFolderIcon(FolderIcon icon) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700341 mFolderIcon = icon;
342 }
343
Adam Cohen3371da02011-10-25 21:38:29 -0700344 @Override
345 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
346 // When the folder gets focus, we don't want to announce the list of items.
347 return true;
348 }
349
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800350 /**
351 * @return the FolderInfo object associated with this folder
352 */
353 FolderInfo getInfo() {
354 return mInfo;
355 }
356
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800357 void bind(FolderInfo info) {
358 mInfo = info;
Adam Cohendf2cc412011-04-27 16:56:57 -0700359 ArrayList<ShortcutInfo> children = info.contents;
Sunny Goyal08f72612015-01-05 13:41:43 -0800360 Collections.sort(children, Utilities.RANK_COMPARATOR);
Sunny Goyal08f72612015-01-05 13:41:43 -0800361
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800362 ArrayList<ShortcutInfo> overflow = mContent.bindItems(children);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700363
Jason Monk4ff73882014-04-24 16:48:07 -0400364 // If our folder has too many items we prune them from the list. This is an issue
Adam Cohenc508b2d2011-06-28 14:41:44 -0700365 // when upgrading from the old Folders implementation which could contain an unlimited
366 // number of items.
367 for (ShortcutInfo item: overflow) {
368 mInfo.remove(item);
369 LauncherModel.deleteItemFromDatabase(mLauncher, item);
370 }
371
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800372 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
373 if (lp == null) {
374 lp = new DragLayer.LayoutParams(0, 0);
375 lp.customPosition = true;
376 setLayoutParams(lp);
377 }
378 centerAboutIcon();
379
Adam Cohen4dbe6d92011-05-18 17:14:15 -0700380 mItemsInvalidated = true;
Adam Cohenac56cff2011-09-28 20:45:37 -0700381 updateTextViewFocus();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700382 mInfo.addListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700383
Adam Cohenafb01ee2011-06-23 15:38:03 -0700384 if (!sDefaultFolderName.contentEquals(mInfo.title)) {
Adam Cohen4ef610f2011-06-21 22:37:36 -0700385 mFolderName.setText(mInfo.title);
386 } else {
387 mFolderName.setText("");
388 }
Winson Chung33231f52013-12-09 16:57:45 -0800389
390 // In case any children didn't come across during loading, clean up the folder accordingly
391 mFolderIcon.post(new Runnable() {
392 public void run() {
393 if (getItemCount() <= 1) {
394 replaceFolderWithFinalItem();
395 }
396 }
397 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700398 }
399
400 /**
401 * Creates a new UserFolder, inflated from R.layout.user_folder.
402 *
403 * @param context The application's context.
404 *
405 * @return A new UserFolder.
406 */
407 static Folder fromXml(Context context) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700408 return (Folder) LayoutInflater.from(context).inflate(R.layout.user_folder, null);
Adam Cohendf2cc412011-04-27 16:56:57 -0700409 }
410
411 /**
412 * This method is intended to make the UserFolder to be visually identical in size and position
413 * to its associated FolderIcon. This allows for a seamless transition into the expanded state.
414 */
415 private void positionAndSizeAsIcon() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700416 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800417 setScaleX(0.8f);
418 setScaleY(0.8f);
419 setAlpha(0f);
Adam Cohendf2cc412011-04-27 16:56:57 -0700420 mState = STATE_SMALL;
421 }
422
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700423 private void prepareReveal() {
424 setScaleX(1f);
425 setScaleY(1f);
426 setAlpha(1f);
427 mState = STATE_SMALL;
428 }
429
Adam Cohendf2cc412011-04-27 16:56:57 -0700430 public void animateOpen() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700431 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen6441de02011-12-14 14:25:32 -0800432
Sunny Goyalb8634152015-04-09 14:17:14 -0700433 mContent.completePendingPageChanges();
Sunny Goyal8167dc22015-04-27 13:44:01 -0700434 if (!mDragInProgress) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700435 // Open on the first page.
436 mContent.snapToPageImmediately(0);
Sunny Goyal48461932015-03-09 17:41:09 -0700437 }
438
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700439 Animator openFolderAnim = null;
440 final Runnable onCompleteRunnable;
Kenny Guyd794a3f2014-09-16 15:17:58 +0100441 if (!Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700442 positionAndSizeAsIcon();
443 centerAboutIcon();
444
445 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1);
446 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
447 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
448 final ObjectAnimator oa =
449 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
450 oa.setDuration(mExpandDuration);
451 openFolderAnim = oa;
452
453 setLayerType(LAYER_TYPE_HARDWARE, null);
454 onCompleteRunnable = new Runnable() {
455 @Override
456 public void run() {
457 setLayerType(LAYER_TYPE_NONE, null);
458 }
459 };
460 } else {
461 prepareReveal();
462 centerAboutIcon();
463
464 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
465 int height = getFolderHeight();
466
467 float transX = - 0.075f * (width / 2 - getPivotX());
468 float transY = - 0.075f * (height / 2 - getPivotY());
469 setTranslationX(transX);
470 setTranslationY(transY);
471 PropertyValuesHolder tx = PropertyValuesHolder.ofFloat("translationX", transX, 0);
472 PropertyValuesHolder ty = PropertyValuesHolder.ofFloat("translationY", transY, 0);
473
474 int rx = (int) Math.max(Math.max(width - getPivotX(), 0), getPivotX());
475 int ry = (int) Math.max(Math.max(height - getPivotY(), 0), getPivotY());
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700476 float radius = (float) Math.hypot(rx, ry);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700477 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
478 Animator reveal = LauncherAnimUtils.createCircularReveal(this, (int) getPivotX(),
479 (int) getPivotY(), 0, radius);
480 reveal.setDuration(mMaterialExpandDuration);
481 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
482
Sunny Goyalbc753352015-03-05 09:40:44 -0800483 mContentWrapper.setAlpha(0f);
484 Animator iconsAlpha = LauncherAnimUtils.ofFloat(mContentWrapper, "alpha", 0f, 1f);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700485 iconsAlpha.setDuration(mMaterialExpandDuration);
486 iconsAlpha.setStartDelay(mMaterialExpandStagger);
487 iconsAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
488
Sunny Goyal290800b2015-03-05 11:33:33 -0800489 mFooter.setAlpha(0f);
490 Animator textAlpha = LauncherAnimUtils.ofFloat(mFooter, "alpha", 0f, 1f);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700491 textAlpha.setDuration(mMaterialExpandDuration);
492 textAlpha.setStartDelay(mMaterialExpandStagger);
493 textAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
494
495 Animator drift = LauncherAnimUtils.ofPropertyValuesHolder(this, tx, ty);
496 drift.setDuration(mMaterialExpandDuration);
497 drift.setStartDelay(mMaterialExpandStagger);
498 drift.setInterpolator(new LogDecelerateInterpolator(60, 0));
499
500 anim.play(drift);
501 anim.play(iconsAlpha);
502 anim.play(textAlpha);
503 anim.play(reveal);
504
505 openFolderAnim = anim;
506
Sunny Goyalbc753352015-03-05 09:40:44 -0800507 mContentWrapper.setLayerType(LAYER_TYPE_HARDWARE, null);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700508 onCompleteRunnable = new Runnable() {
509 @Override
510 public void run() {
Sunny Goyalbc753352015-03-05 09:40:44 -0800511 mContentWrapper.setLayerType(LAYER_TYPE_NONE, null);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700512 }
513 };
514 }
515 openFolderAnim.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700516 @Override
517 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700518 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Sunny Goyalbc753352015-03-05 09:40:44 -0800519 mContent.getAccessibilityDescription());
Adam Cohendf2cc412011-04-27 16:56:57 -0700520 mState = STATE_ANIMATING;
521 }
522 @Override
523 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700524 mState = STATE_OPEN;
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700525
526 if (onCompleteRunnable != null) {
527 onCompleteRunnable.run();
528 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800529
Sunny Goyalbc753352015-03-05 09:40:44 -0800530 mContent.setFocusOnFirstChild();
Adam Cohendf2cc412011-04-27 16:56:57 -0700531 }
532 });
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700533 openFolderAnim.start();
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800534
535 // Make sure the folder picks up the last drag move even if the finger doesn't move.
536 if (mDragController.isDragging()) {
537 mDragController.forceTouchMove();
538 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700539
Sunny Goyalb8634152015-04-09 14:17:14 -0700540 FolderPagedView pages = (FolderPagedView) mContent;
541 pages.verifyVisibleHighResIcons(pages.getNextPage());
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800542 }
543
544 public void beginExternalDrag(ShortcutInfo item) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800545 mCurrentDragInfo = item;
Sunny Goyal5d85c442015-03-10 13:14:47 -0700546 mEmptyCellRank = mContent.allocateRankForNewItem(item);
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800547 mIsExternalDrag = true;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800548 mDragInProgress = true;
Sunny Goyalb8634152015-04-09 14:17:14 -0700549
Sunny Goyalf4066152015-04-15 09:42:19 -0700550 // Since this folder opened by another controller, it might not get onDrop or
551 // onDropComplete. Perform cleanup once drag-n-drop ends.
552 mDragController.addDragListener(this);
553 }
554
555 @Override
556 public void onDragStart(DragSource source, Object info, int dragAction) { }
557
558 @Override
559 public void onDragEnd() {
560 if (mIsExternalDrag && mDragInProgress) {
561 completeDragExit();
562 }
563 mDragController.removeDragListener(this);
Adam Cohendf2cc412011-04-27 16:56:57 -0700564 }
565
Adam Cohen091440a2015-03-18 14:16:05 -0700566 @Thunk void sendCustomAccessibilityEvent(int type, String text) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700567 AccessibilityManager accessibilityManager = (AccessibilityManager)
568 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
569 if (accessibilityManager.isEnabled()) {
Adam Cohen3371da02011-10-25 21:38:29 -0700570 AccessibilityEvent event = AccessibilityEvent.obtain(type);
571 onInitializeAccessibilityEvent(event);
572 event.getText().add(text);
Michael Jurka8b805b12012-04-18 14:23:14 -0700573 accessibilityManager.sendAccessibilityEvent(event);
Adam Cohen3371da02011-10-25 21:38:29 -0700574 }
575 }
576
Adam Cohendf2cc412011-04-27 16:56:57 -0700577 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700578 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800579 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
580 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
581 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
Michael Jurka032e6ba2013-04-22 15:08:42 +0200582 final ObjectAnimator oa =
Michael Jurka2ecf9952012-06-18 12:52:28 -0700583 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohendf2cc412011-04-27 16:56:57 -0700584
Adam Cohen2801caf2011-05-13 20:57:39 -0700585 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700586 @Override
587 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700588 onCloseComplete();
Michael Jurka0121c3e2012-05-31 08:36:04 -0700589 setLayerType(LAYER_TYPE_NONE, null);
Adam Cohen2801caf2011-05-13 20:57:39 -0700590 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700591 }
592 @Override
593 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700594 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700595 getContext().getString(R.string.folder_closed));
Adam Cohendf2cc412011-04-27 16:56:57 -0700596 mState = STATE_ANIMATING;
597 }
598 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700599 oa.setDuration(mExpandDuration);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700600 setLayerType(LAYER_TYPE_HARDWARE, null);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100601 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700602 }
603
Adam Cohencb3382b2011-05-24 14:07:08 -0700604 public boolean acceptDrop(DragObject d) {
605 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700606 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700607 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
608 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
609 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700610 }
611
Adam Cohencb3382b2011-05-24 14:07:08 -0700612 public void onDragEnter(DragObject d) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800613 mPrevTargetRank = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700614 mOnExitAlarm.cancelAlarm();
Sunny Goyalb8634152015-04-09 14:17:14 -0700615 // Get the area offset such that the folder only closes if half the drag icon width
616 // is outside the folder area
617 mScrollAreaOffset = d.dragView.getDragRegionWidth() / 2 - d.xOffset;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700618 }
619
620 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
621 public void onAlarm(Alarm alarm) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800622 mContent.realTimeReorder(mEmptyCellRank, mTargetRank);
623 mEmptyCellRank = mTargetRank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700624 }
625 };
626
Sunny Goyalc46bfef2015-01-05 12:40:08 -0800627 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohen2374abf2013-04-16 14:56:57 -0700628 public boolean isLayoutRtl() {
629 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
630 }
631
Sunny Goyal48461932015-03-09 17:41:09 -0700632 @Override
Adam Cohencb3382b2011-05-24 14:07:08 -0700633 public void onDragOver(DragObject d) {
Sunny Goyal48461932015-03-09 17:41:09 -0700634 onDragOver(d, REORDER_DELAY);
635 }
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700636
Sunny Goyal48461932015-03-09 17:41:09 -0700637 private int getTargetRank(DragObject d, float[] recycle) {
638 recycle = d.getVisualCenter(recycle);
639 return mContent.findNearestArea(
640 (int) recycle[0] - getPaddingLeft(), (int) recycle[1] - getPaddingTop());
641 }
642
Adam Cohen091440a2015-03-18 14:16:05 -0700643 @Thunk void onDragOver(DragObject d, int reorderDelay) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700644 if (mScrollPauseAlarm.alarmPending()) {
Sunny Goyal48461932015-03-09 17:41:09 -0700645 return;
646 }
647 final float[] r = new float[2];
648 mTargetRank = getTargetRank(d, r);
649
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800650 if (mTargetRank != mPrevTargetRank) {
Alan Viverette4cda5b72013-08-28 17:53:41 -0700651 mReorderAlarm.cancelAlarm();
Sunny Goyalc46bfef2015-01-05 12:40:08 -0800652 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
653 mReorderAlarm.setAlarm(REORDER_DELAY);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800654 mPrevTargetRank = mTargetRank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700655 }
Sunny Goyal48461932015-03-09 17:41:09 -0700656
Sunny Goyal48461932015-03-09 17:41:09 -0700657 float x = r[0];
Sunny Goyalb8634152015-04-09 14:17:14 -0700658 int currentPage = mContent.getNextPage();
Sunny Goyal48461932015-03-09 17:41:09 -0700659
Sunny Goyalb8634152015-04-09 14:17:14 -0700660 float cellOverlap = mContent.getCurrentCellLayout().getCellWidth()
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700661 * ICON_OVERSCROLL_WIDTH_FACTOR;
662 boolean isOutsideLeftEdge = x < cellOverlap;
663 boolean isOutsideRightEdge = x > (getWidth() - cellOverlap);
Sunny Goyal48461932015-03-09 17:41:09 -0700664
Sunny Goyalb8634152015-04-09 14:17:14 -0700665 if (currentPage > 0 && (mContent.rtlLayout ? isOutsideRightEdge : isOutsideLeftEdge)) {
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700666 showScrollHint(DragController.SCROLL_LEFT, d);
Sunny Goyalb8634152015-04-09 14:17:14 -0700667 } else if (currentPage < (mContent.getPageCount() - 1)
668 && (mContent.rtlLayout ? isOutsideLeftEdge : isOutsideRightEdge)) {
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700669 showScrollHint(DragController.SCROLL_RIGHT, d);
Sunny Goyal48461932015-03-09 17:41:09 -0700670 } else {
671 mOnScrollHintAlarm.cancelAlarm();
672 if (mScrollHintDir != DragController.SCROLL_NONE) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700673 mContent.clearScrollHint();
Sunny Goyal48461932015-03-09 17:41:09 -0700674 mScrollHintDir = DragController.SCROLL_NONE;
675 }
676 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700677 }
678
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700679 private void showScrollHint(int direction, DragObject d) {
680 // Show scroll hint on the right
681 if (mScrollHintDir != direction) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700682 mContent.showScrollHint(direction);
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700683 mScrollHintDir = direction;
684 }
685
686 // Set alarm for when the hint is complete
687 if (!mOnScrollHintAlarm.alarmPending() || mCurrentScrollDir != direction) {
688 mCurrentScrollDir = direction;
689 mOnScrollHintAlarm.cancelAlarm();
690 mOnScrollHintAlarm.setOnAlarmListener(new OnScrollHintListener(d));
691 mOnScrollHintAlarm.setAlarm(SCROLL_HINT_DURATION);
692
693 mReorderAlarm.cancelAlarm();
694 mTargetRank = mEmptyCellRank;
695 }
696 }
697
Adam Cohenbfbfd262011-06-13 16:55:12 -0700698 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
699 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700700 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700701 }
702 };
703
Adam Cohen95bb8002011-07-03 23:40:28 -0700704 public void completeDragExit() {
Sunny Goyalf4066152015-04-15 09:42:19 -0700705 if (mInfo.opened) {
706 mLauncher.closeFolder();
707 mRearrangeOnClose = true;
Sunny Goyal31abc292015-05-01 10:42:32 -0700708 } else if (mState == STATE_ANIMATING) {
709 mRearrangeOnClose = true;
Sunny Goyalf4066152015-04-15 09:42:19 -0700710 } else {
711 rearrangeChildren();
Sunny Goyal31abc292015-05-01 10:42:32 -0700712 clearDragInfo();
Sunny Goyalf4066152015-04-15 09:42:19 -0700713 }
Sunny Goyal31abc292015-05-01 10:42:32 -0700714 }
715
716 private void clearDragInfo() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700717 mCurrentDragInfo = null;
718 mCurrentDragView = null;
719 mSuppressOnAdd = false;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800720 mIsExternalDrag = false;
Adam Cohen3e8f8112011-07-02 18:03:00 -0700721 }
722
Adam Cohencb3382b2011-05-24 14:07:08 -0700723 public void onDragExit(DragObject d) {
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700724 // We only close the folder if this is a true drag exit, ie. not because
725 // a drop has occurred above the folder.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700726 if (!d.dragComplete) {
727 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
728 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
729 }
730 mReorderAlarm.cancelAlarm();
Sunny Goyal48461932015-03-09 17:41:09 -0700731
Sunny Goyalb8634152015-04-09 14:17:14 -0700732 mOnScrollHintAlarm.cancelAlarm();
733 mScrollPauseAlarm.cancelAlarm();
734 if (mScrollHintDir != DragController.SCROLL_NONE) {
735 mContent.clearScrollHint();
736 mScrollHintDir = DragController.SCROLL_NONE;
Sunny Goyal48461932015-03-09 17:41:09 -0700737 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700738 }
739
Sunny Goyale9b651e2015-04-24 11:44:51 -0700740 /**
741 * When performing an accessibility drop, onDrop is sent immediately after onDragEnter. So we
742 * need to complete all transient states based on timers.
743 */
744 @Override
745 public void prepareAccessibilityDrop() {
746 if (mReorderAlarm.alarmPending()) {
747 mReorderAlarm.cancelAlarm();
748 mReorderAlarmListener.onAlarm(mReorderAlarm);
749 }
750 }
751
Michael Jurka1e2f4652013-07-08 18:03:46 -0700752 public void onDropCompleted(final View target, final DragObject d,
753 final boolean isFlingToDelete, final boolean success) {
754 if (mDeferDropAfterUninstall) {
Michael Jurkaf3007582013-08-21 14:33:57 +0200755 Log.d(TAG, "Deferred handling drop because waiting for uninstall.");
Michael Jurka1e2f4652013-07-08 18:03:46 -0700756 mDeferredAction = new Runnable() {
757 public void run() {
758 onDropCompleted(target, d, isFlingToDelete, success);
759 mDeferredAction = null;
760 }
761 };
762 return;
763 }
764
765 boolean beingCalledAfterUninstall = mDeferredAction != null;
766 boolean successfulDrop =
767 success && (!beingCalledAfterUninstall || mUninstallSuccessful);
Winson Chung5f8afe62013-08-12 16:19:28 -0700768
Michael Jurka1e2f4652013-07-08 18:03:46 -0700769 if (successfulDrop) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800770 if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon && target != this) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700771 replaceFolderWithFinalItem();
772 }
773 } else {
774 // The drag failed, we need to return the item to the folder
Sunny Goyalb8c663c2015-04-23 11:43:48 -0700775 ShortcutInfo info = (ShortcutInfo) d.dragInfo;
776 View icon = (mCurrentDragView != null && mCurrentDragView.getTag() == info)
777 ? mCurrentDragView : mContent.createNewView(info);
778 ArrayList<View> views = getItemsInReadingOrder();
779 views.add(info.rank, icon);
780 mContent.arrangeChildren(views, views.size());
781 mItemsInvalidated = true;
782
783 mSuppressOnAdd = true;
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700784 mFolderIcon.onDrop(d);
Sunny Goyalb8c663c2015-04-23 11:43:48 -0700785 mSuppressOnAdd = false;
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700786 }
787
788 if (target != this) {
789 if (mOnExitAlarm.alarmPending()) {
790 mOnExitAlarm.cancelAlarm();
Michael Jurka54554252013-08-01 12:52:23 +0200791 if (!successfulDrop) {
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700792 mSuppressFolderDeletion = true;
793 }
Sunny Goyal5d85c442015-03-10 13:14:47 -0700794 mScrollPauseAlarm.cancelAlarm();
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700795 completeDragExit();
796 }
Adam Cohen9c58d822013-12-13 17:18:10 -0800797 }
798
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700799 mDeleteFolderOnDropCompleted = false;
800 mDragInProgress = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700801 mItemAddedBackToSelfViaIcon = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700802 mCurrentDragInfo = null;
803 mCurrentDragView = null;
804 mSuppressOnAdd = false;
Adam Cohen4045eb72011-10-06 11:44:26 -0700805
806 // Reordering may have occured, and we need to save the new item locations. We do this once
807 // at the end to prevent unnecessary database operations.
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700808 updateItemLocationsInDatabaseBatch();
Adam Cohen4045eb72011-10-06 11:44:26 -0700809 }
810
Sunny Goyalfa401a12015-04-10 13:45:42 -0700811 @Override
Michael Jurka1e2f4652013-07-08 18:03:46 -0700812 public void deferCompleteDropAfterUninstallActivity() {
813 mDeferDropAfterUninstall = true;
814 }
815
Sunny Goyalfa401a12015-04-10 13:45:42 -0700816 @Override
Michael Jurka1e2f4652013-07-08 18:03:46 -0700817 public void onUninstallActivityReturned(boolean success) {
818 mDeferDropAfterUninstall = false;
819 mUninstallSuccessful = success;
820 if (mDeferredAction != null) {
821 mDeferredAction.run();
822 }
823 }
824
Winson Chunga48487a2012-03-20 16:19:37 -0700825 @Override
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800826 public float getIntrinsicIconScaleFactor() {
827 return 1f;
828 }
829
830 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800831 public boolean supportsFlingToDelete() {
832 return true;
833 }
834
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000835 @Override
836 public boolean supportsAppInfoDropTarget() {
837 return false;
838 }
839
840 @Override
841 public boolean supportsDeleteDropTarget() {
842 return true;
843 }
844
Sunny Goyalddec7342015-04-29 18:12:37 -0700845 @Override
846 public void onFlingToDelete(DragObject d, PointF vec) {
Winson Chunga48487a2012-03-20 16:19:37 -0700847 // Do nothing
848 }
849
850 @Override
851 public void onFlingToDeleteCompleted() {
852 // Do nothing
853 }
854
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700855 private void updateItemLocationsInDatabaseBatch() {
856 ArrayList<View> list = getItemsInReadingOrder();
857 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
858 for (int i = 0; i < list.size(); i++) {
859 View v = list.get(i);
860 ItemInfo info = (ItemInfo) v.getTag();
Sunny Goyal08f72612015-01-05 13:41:43 -0800861 info.rank = i;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700862 items.add(info);
863 }
864
865 LauncherModel.moveItemsInDatabase(mLauncher, items, mInfo.id, 0);
866 }
867
Adam Cohene25af792013-06-06 23:08:25 -0700868 public void addItemLocationsInDatabase() {
869 ArrayList<View> list = getItemsInReadingOrder();
870 for (int i = 0; i < list.size(); i++) {
871 View v = list.get(i);
872 ItemInfo info = (ItemInfo) v.getTag();
873 LauncherModel.addItemToDatabase(mLauncher, info, mInfo.id, 0,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700874 info.cellX, info.cellY);
Adam Cohene25af792013-06-06 23:08:25 -0700875 }
876 }
877
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700878 public void notifyDrop() {
879 if (mDragInProgress) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700880 mItemAddedBackToSelfViaIcon = true;
Adam Cohen76078c42011-06-09 15:06:52 -0700881 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700882 }
883
884 public boolean isDropEnabled() {
885 return true;
886 }
887
Adam Cohen2801caf2011-05-13 20:57:39 -0700888 public boolean isFull() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800889 return mContent.isFull();
Adam Cohen2801caf2011-05-13 20:57:39 -0700890 }
891
892 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700893 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700894
Winson Chung892c74d2013-08-22 16:15:50 -0700895 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700896 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700897 int height = getFolderHeight();
Adam Cohen2801caf2011-05-13 20:57:39 -0700898
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800899 float scale = parent.getDescendantRectRelativeToSelf(mFolderIcon, sTempRect);
Adam Cohen8e776a62011-06-28 18:10:06 -0700900
Winson Chungaf40f202013-09-18 18:26:31 -0700901 LauncherAppState app = LauncherAppState.getInstance();
902 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
903
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800904 int centerX = (int) (sTempRect.left + sTempRect.width() * scale / 2);
905 int centerY = (int) (sTempRect.top + sTempRect.height() * scale / 2);
Adam Cohen2801caf2011-05-13 20:57:39 -0700906 int centeredLeft = centerX - width / 2;
907 int centeredTop = centerY - height / 2;
Winson Chung3057b1c2013-10-10 17:35:35 -0700908 int currentPage = mLauncher.getWorkspace().getNextPage();
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800909 // In case the workspace is scrolling, we need to use the final scroll to compute
910 // the folders bounds.
911 mLauncher.getWorkspace().setFinalScrollForPageChange(currentPage);
Adam Cohen35e7e642011-07-17 14:47:18 -0700912 // We first fetch the currently visible CellLayoutChildren
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800913 CellLayout currentLayout = (CellLayout) mLauncher.getWorkspace().getChildAt(currentPage);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700914 ShortcutAndWidgetContainer boundingLayout = currentLayout.getShortcutsAndWidgets();
Adam Cohen35e7e642011-07-17 14:47:18 -0700915 Rect bounds = new Rect();
916 parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800917 // We reset the workspaces scroll
918 mLauncher.getWorkspace().resetFinalScrollForPageChange(currentPage);
Adam Cohen2801caf2011-05-13 20:57:39 -0700919
Adam Cohen35e7e642011-07-17 14:47:18 -0700920 // We need to bound the folder to the currently visible CellLayoutChildren
921 int left = Math.min(Math.max(bounds.left, centeredLeft),
922 bounds.left + bounds.width() - width);
923 int top = Math.min(Math.max(bounds.top, centeredTop),
924 bounds.top + bounds.height() - height);
Winson Chungaf40f202013-09-18 18:26:31 -0700925 if (grid.isPhone() && (grid.availableWidthPx - width) < grid.iconSizePx) {
926 // Center the folder if it is full (on phones only)
927 left = (grid.availableWidthPx - width) / 2;
928 } else if (width >= bounds.width()) {
929 // If the folder doesn't fit within the bounds, center it about the desired bounds
Adam Cohen35e7e642011-07-17 14:47:18 -0700930 left = bounds.left + (bounds.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700931 }
Adam Cohen35e7e642011-07-17 14:47:18 -0700932 if (height >= bounds.height()) {
933 top = bounds.top + (bounds.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700934 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700935
936 int folderPivotX = width / 2 + (centeredLeft - left);
937 int folderPivotY = height / 2 + (centeredTop - top);
938 setPivotX(folderPivotX);
939 setPivotY(folderPivotY);
Adam Cohen268c4752012-06-06 17:47:33 -0700940 mFolderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700941 (1.0f * folderPivotX / width));
Adam Cohen268c4752012-06-06 17:47:33 -0700942 mFolderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700943 (1.0f * folderPivotY / height));
Adam Cohen3bf84d32012-05-07 20:17:14 -0700944
Adam Cohen662b5982011-12-13 17:45:21 -0800945 lp.width = width;
946 lp.height = height;
947 lp.x = left;
948 lp.y = top;
Adam Cohen2801caf2011-05-13 20:57:39 -0700949 }
950
Adam Cohen268c4752012-06-06 17:47:33 -0700951 float getPivotXForIconAnimation() {
952 return mFolderIconPivotX;
953 }
954 float getPivotYForIconAnimation() {
955 return mFolderIconPivotY;
956 }
957
Winson Chung892c74d2013-08-22 16:15:50 -0700958 private int getContentAreaHeight() {
959 LauncherAppState app = LauncherAppState.getInstance();
960 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
961 Rect workspacePadding = grid.getWorkspacePadding(grid.isLandscape ?
962 CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
963 int maxContentAreaHeight = grid.availableHeightPx -
Winson Chung892c74d2013-08-22 16:15:50 -0700964 workspacePadding.top - workspacePadding.bottom -
Sunny Goyal290800b2015-03-05 11:33:33 -0800965 mFooterHeight;
Adam Cohen1960ea42013-11-12 11:33:14 +0000966 int height = Math.min(maxContentAreaHeight,
Winson Chung892c74d2013-08-22 16:15:50 -0700967 mContent.getDesiredHeight());
Adam Cohen1960ea42013-11-12 11:33:14 +0000968 return Math.max(height, MIN_CONTENT_DIMEN);
969 }
970
971 private int getContentAreaWidth() {
972 return Math.max(mContent.getDesiredWidth(), MIN_CONTENT_DIMEN);
Winson Chung892c74d2013-08-22 16:15:50 -0700973 }
974
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700975 private int getFolderHeight() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800976 return getFolderHeight(getContentAreaHeight());
977 }
978
979 private int getFolderHeight(int contentAreaHeight) {
Sunny Goyal290800b2015-03-05 11:33:33 -0800980 return getPaddingTop() + getPaddingBottom() + contentAreaHeight + mFooterHeight;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700981 }
Adam Cohen234c4cd2011-07-17 21:03:04 -0700982
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700983 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800984 int contentWidth = getContentAreaWidth();
985 int contentHeight = getContentAreaHeight();
Adam Cohen1960ea42013-11-12 11:33:14 +0000986
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800987 int contentAreaWidthSpec = MeasureSpec.makeMeasureSpec(contentWidth, MeasureSpec.EXACTLY);
988 int contentAreaHeightSpec = MeasureSpec.makeMeasureSpec(contentHeight, MeasureSpec.EXACTLY);
989
990 mContent.setFixedSize(contentWidth, contentHeight);
991 mContentWrapper.measure(contentAreaWidthSpec, contentAreaHeightSpec);
Sunny Goyal290800b2015-03-05 11:33:33 -0800992 mFooter.measure(contentAreaWidthSpec,
993 MeasureSpec.makeMeasureSpec(mFooterHeight, MeasureSpec.EXACTLY));
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800994
995 int folderWidth = getPaddingLeft() + getPaddingRight() + contentWidth;
996 int folderHeight = getFolderHeight(contentHeight);
997 setMeasuredDimension(folderWidth, folderHeight);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700998 }
999
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001000 /**
1001 * Rearranges the children based on their rank.
1002 */
1003 public void rearrangeChildren() {
1004 rearrangeChildren(-1);
1005 }
Adam Cohen2801caf2011-05-13 20:57:39 -07001006
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001007 /**
1008 * Rearranges the children based on their rank.
1009 * @param itemCount if greater than the total children count, empty spaces are left at the end,
1010 * otherwise it is ignored.
1011 */
1012 public void rearrangeChildren(int itemCount) {
1013 ArrayList<View> views = getItemsInReadingOrder();
1014 mContent.arrangeChildren(views, Math.max(itemCount, views.size()));
Adam Cohen7c693212011-05-18 15:26:57 -07001015 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -07001016 }
1017
Sunny Goyalc4918352015-03-10 18:15:48 -07001018 // TODO remove this once GSA code fix is submitted
1019 public ViewGroup getContent() {
1020 return (ViewGroup) mContent;
1021 }
1022
Adam Cohena9cf38f2011-05-02 15:36:58 -07001023 public int getItemCount() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001024 return mContent.getItemCount();
Adam Cohena9cf38f2011-05-02 15:36:58 -07001025 }
1026
Adam Cohen091440a2015-03-18 14:16:05 -07001027 @Thunk void onCloseComplete() {
Adam Cohen05e0f402011-08-01 12:12:49 -07001028 DragLayer parent = (DragLayer) getParent();
Michael Jurka5649c282012-06-18 10:33:21 -07001029 if (parent != null) {
1030 parent.removeView(this);
1031 }
Adam Cohen4554ee12011-08-03 16:13:21 -07001032 mDragController.removeDropTarget((DropTarget) this);
Adam Cohen05e0f402011-08-01 12:12:49 -07001033 clearFocus();
Adam Cohenac56cff2011-09-28 20:45:37 -07001034 mFolderIcon.requestFocus();
Adam Cohen05e0f402011-08-01 12:12:49 -07001035
Adam Cohen2801caf2011-05-13 20:57:39 -07001036 if (mRearrangeOnClose) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001037 rearrangeChildren();
Adam Cohen2801caf2011-05-13 20:57:39 -07001038 mRearrangeOnClose = false;
1039 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001040 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001041 if (!mDragInProgress && !mSuppressFolderDeletion) {
1042 replaceFolderWithFinalItem();
1043 } else if (mDragInProgress) {
1044 mDeleteFolderOnDropCompleted = true;
1045 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001046 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001047 mSuppressFolderDeletion = false;
Sunny Goyal31abc292015-05-01 10:42:32 -07001048 clearDragInfo();
Adam Cohenafb01ee2011-06-23 15:38:03 -07001049 }
1050
Adam Cohen091440a2015-03-18 14:16:05 -07001051 @Thunk void replaceFolderWithFinalItem() {
Adam Cohenafb01ee2011-06-23 15:38:03 -07001052 // Add the last remaining child to the workspace in place of the folder
Adam Cohenfb91f302012-06-11 15:45:18 -07001053 Runnable onCompleteRunnable = new Runnable() {
1054 @Override
1055 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -07001056 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screenId);
Adam Cohenafb01ee2011-06-23 15:38:03 -07001057
Winson Chung33231f52013-12-09 16:57:45 -08001058 View child = null;
Adam Cohenfb91f302012-06-11 15:45:18 -07001059 // Move the item from the folder to the workspace, in the position of the folder
1060 if (getItemCount() == 1) {
1061 ShortcutInfo finalItem = mInfo.contents.get(0);
Adam Cohenc5e63f32012-07-12 16:16:57 -07001062 child = mLauncher.createShortcut(R.layout.application, cellLayout,
Adam Cohenfb91f302012-06-11 15:45:18 -07001063 finalItem);
1064 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -07001065 mInfo.screenId, mInfo.cellX, mInfo.cellY);
Adam Cohenfb91f302012-06-11 15:45:18 -07001066 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001067 if (getItemCount() <= 1) {
1068 // Remove the folder
1069 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
Dan Sandler0eb687f2014-01-10 13:24:55 -05001070 if (cellLayout != null) {
1071 // b/12446428 -- sometimes the cell layout has already gone away?
1072 cellLayout.removeView(mFolderIcon);
1073 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001074 if (mFolderIcon instanceof DropTarget) {
1075 mDragController.removeDropTarget((DropTarget) mFolderIcon);
1076 }
1077 mLauncher.removeFolder(mInfo);
1078 }
Adam Cohenc5e63f32012-07-12 16:16:57 -07001079 // We add the child after removing the folder to prevent both from existing at
Winson Chung0e6a7132013-08-23 12:55:10 -07001080 // the same time in the CellLayout. We need to add the new item with addInScreenFromBind()
1081 // to ensure that hotseat items are placed correctly.
Adam Cohenc5e63f32012-07-12 16:16:57 -07001082 if (child != null) {
Winson Chung0e6a7132013-08-23 12:55:10 -07001083 mLauncher.getWorkspace().addInScreenFromBind(child, mInfo.container, mInfo.screenId,
Adam Cohenc5e63f32012-07-12 16:16:57 -07001084 mInfo.cellX, mInfo.cellY, mInfo.spanX, mInfo.spanY);
1085 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001086 }
1087 };
Sunny Goyalbc753352015-03-05 09:40:44 -08001088 View finalChild = mContent.getLastItem();
Adam Cohenfb91f302012-06-11 15:45:18 -07001089 if (finalChild != null) {
1090 mFolderIcon.performDestroyAnimation(finalChild, onCompleteRunnable);
Winson Chung33231f52013-12-09 16:57:45 -08001091 } else {
1092 onCompleteRunnable.run();
Adam Cohenafb01ee2011-06-23 15:38:03 -07001093 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001094 mDestroyed = true;
1095 }
1096
1097 boolean isDestroyed() {
1098 return mDestroyed;
Adam Cohen2801caf2011-05-13 20:57:39 -07001099 }
1100
Adam Cohenac56cff2011-09-28 20:45:37 -07001101 // This method keeps track of the last item in the folder for the purposes
1102 // of keyboard focus
Sunny Goyal290800b2015-03-05 11:33:33 -08001103 public void updateTextViewFocus() {
Sunny Goyalbc753352015-03-05 09:40:44 -08001104 View lastChild = mContent.getLastItem();
Adam Cohenac56cff2011-09-28 20:45:37 -07001105 if (lastChild != null) {
1106 mFolderName.setNextFocusDownId(lastChild.getId());
1107 mFolderName.setNextFocusRightId(lastChild.getId());
1108 mFolderName.setNextFocusLeftId(lastChild.getId());
1109 mFolderName.setNextFocusUpId(lastChild.getId());
1110 }
1111 }
1112
Adam Cohenbfbfd262011-06-13 16:55:12 -07001113 public void onDrop(DragObject d) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001114 Runnable cleanUpRunnable = null;
1115
Adam Cohen689ff162014-05-08 17:27:56 -07001116 // If we are coming from All Apps space, we defer removing the extra empty screen
1117 // until the folder closes
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001118 if (d.dragSource != mLauncher.getWorkspace() && !(d.dragSource instanceof Folder)) {
1119 cleanUpRunnable = new Runnable() {
1120 @Override
1121 public void run() {
Adam Cohen689ff162014-05-08 17:27:56 -07001122 mLauncher.exitSpringLoadedDragModeDelayed(true,
1123 Launcher.EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
1124 null);
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001125 }
1126 };
Adam Cohenbfbfd262011-06-13 16:55:12 -07001127 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001128
Sunny Goyalb8634152015-04-09 14:17:14 -07001129 // If the icon was dropped while the page was being scrolled, we need to compute
1130 // the target location again such that the icon is placed of the final page.
1131 if (!mContent.rankOnCurrentPage(mEmptyCellRank)) {
1132 // Reorder again.
1133 mTargetRank = getTargetRank(d, null);
Sunny Goyal48461932015-03-09 17:41:09 -07001134
Sunny Goyalb8634152015-04-09 14:17:14 -07001135 // Rearrange items immediately.
1136 mReorderAlarmListener.onAlarm(mReorderAlarm);
Sunny Goyal48461932015-03-09 17:41:09 -07001137
Sunny Goyalb8634152015-04-09 14:17:14 -07001138 mOnScrollHintAlarm.cancelAlarm();
1139 mScrollPauseAlarm.cancelAlarm();
Sunny Goyal48461932015-03-09 17:41:09 -07001140 }
Sunny Goyalb8634152015-04-09 14:17:14 -07001141 mContent.completePendingPageChanges();
Sunny Goyal48461932015-03-09 17:41:09 -07001142
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001143 View currentDragView;
1144 ShortcutInfo si = mCurrentDragInfo;
1145 if (mIsExternalDrag) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001146 currentDragView = mContent.createAndAddViewForRank(si, mEmptyCellRank);
Sunny Goyal95abbb32014-08-04 10:53:22 -07001147 // Actually move the item in the database if it was an external drag. Call this
1148 // before creating the view, so that ShortcutInfo is updated appropriately.
1149 LauncherModel.addOrMoveItemInDatabase(
1150 mLauncher, si, mInfo.id, 0, si.cellX, si.cellY);
1151
1152 // We only need to update the locations if it doesn't get handled in #onDropCompleted.
1153 if (d.dragSource != this) {
1154 updateItemLocationsInDatabaseBatch();
1155 }
1156 mIsExternalDrag = false;
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001157 } else {
1158 currentDragView = mCurrentDragView;
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001159 mContent.addViewForRank(currentDragView, si, mEmptyCellRank);
Adam Cohenbfbfd262011-06-13 16:55:12 -07001160 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001161
1162 if (d.dragView.hasDrawn()) {
1163
1164 // Temporarily reset the scale such that the animation target gets calculated correctly.
1165 float scaleX = getScaleX();
1166 float scaleY = getScaleY();
1167 setScaleX(1.0f);
1168 setScaleY(1.0f);
1169 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, currentDragView,
1170 cleanUpRunnable, null);
1171 setScaleX(scaleX);
1172 setScaleY(scaleY);
1173 } else {
1174 d.deferDragViewCleanupPostAnimation = false;
1175 currentDragView.setVisibility(VISIBLE);
1176 }
1177 mItemsInvalidated = true;
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001178 rearrangeChildren();
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001179
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001180 // Temporarily suppress the listener, as we did all the work already here.
1181 mSuppressOnAdd = true;
1182 mInfo.add(si);
1183 mSuppressOnAdd = false;
Sunny Goyal4b020172014-08-28 14:51:14 -07001184 // Clear the drag info, as it is no longer being dragged.
1185 mCurrentDragInfo = null;
Sunny Goyalf4066152015-04-15 09:42:19 -07001186 mDragInProgress = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001187 }
1188
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001189 // This is used so the item doesn't immediately appear in the folder when added. In one case
1190 // we need to create the illusion that the item isn't added back to the folder yet, to
1191 // to correspond to the animation of the icon back into the folder. This is
1192 public void hideItem(ShortcutInfo info) {
1193 View v = getViewForInfo(info);
1194 v.setVisibility(INVISIBLE);
1195 }
1196 public void showItem(ShortcutInfo info) {
1197 View v = getViewForInfo(info);
1198 v.setVisibility(VISIBLE);
1199 }
1200
Adam Cohenbfbfd262011-06-13 16:55:12 -07001201 public void onAdd(ShortcutInfo item) {
Adam Cohen05e0f402011-08-01 12:12:49 -07001202 // If the item was dropped onto this open folder, we have done the work associated
1203 // with adding the item to the folder, as indicated by mSuppressOnAdd being set
Adam Cohenbfbfd262011-06-13 16:55:12 -07001204 if (mSuppressOnAdd) return;
Sunny Goyal5d85c442015-03-10 13:14:47 -07001205 mContent.createAndAddViewForRank(item, mContent.allocateRankForNewItem(item));
Sunny Goyal2e688a82015-03-18 10:23:39 -07001206 mItemsInvalidated = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001207 LauncherModel.addOrMoveItemInDatabase(
1208 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
1209 }
1210
Adam Cohena9cf38f2011-05-02 15:36:58 -07001211 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -07001212 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001213 // If this item is being dragged from this open folder, we have already handled
1214 // the work associated with removing the item, so we don't have to do anything here.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001215 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001216 View v = getViewForInfo(item);
Sunny Goyal290800b2015-03-05 11:33:33 -08001217 mContent.removeItem(v);
Adam Cohen2801caf2011-05-13 20:57:39 -07001218 if (mState == STATE_ANIMATING) {
1219 mRearrangeOnClose = true;
1220 } else {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001221 rearrangeChildren();
Adam Cohen2801caf2011-05-13 20:57:39 -07001222 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001223 if (getItemCount() <= 1) {
1224 replaceFolderWithFinalItem();
1225 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001226 }
Adam Cohen7c693212011-05-18 15:26:57 -07001227
Sunny Goyalbc753352015-03-05 09:40:44 -08001228 private View getViewForInfo(final ShortcutInfo item) {
1229 return mContent.iterateOverItems(new ItemOperator() {
1230
1231 @Override
1232 public boolean evaluate(ItemInfo info, View view, View parent) {
1233 return info == item;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001234 }
Sunny Goyalbc753352015-03-05 09:40:44 -08001235 });
Adam Cohendf1e4e82011-06-24 15:57:39 -07001236 }
1237
Adam Cohen76078c42011-06-09 15:06:52 -07001238 public void onItemsChanged() {
Adam Cohenac56cff2011-09-28 20:45:37 -07001239 updateTextViewFocus();
Adam Cohen76078c42011-06-09 15:06:52 -07001240 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001241
Adam Cohen76fc0852011-06-17 13:26:23 -07001242 public void onTitleChanged(CharSequence title) {
1243 }
Adam Cohen76078c42011-06-09 15:06:52 -07001244
Adam Cohen7c693212011-05-18 15:26:57 -07001245 public ArrayList<View> getItemsInReadingOrder() {
1246 if (mItemsInvalidated) {
1247 mItemsInReadingOrder.clear();
Sunny Goyalbc753352015-03-05 09:40:44 -08001248 mContent.iterateOverItems(new ItemOperator() {
1249
1250 @Override
1251 public boolean evaluate(ItemInfo info, View view, View parent) {
1252 mItemsInReadingOrder.add(view);
1253 return false;
Adam Cohen7c693212011-05-18 15:26:57 -07001254 }
Sunny Goyalbc753352015-03-05 09:40:44 -08001255 });
Adam Cohen7c693212011-05-18 15:26:57 -07001256 mItemsInvalidated = false;
1257 }
1258 return mItemsInReadingOrder;
1259 }
Adam Cohen8dfcba42011-07-07 16:38:18 -07001260
1261 public void getLocationInDragLayer(int[] loc) {
1262 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
1263 }
Adam Cohenea0818d2011-09-30 20:06:58 -07001264
1265 public void onFocusChange(View v, boolean hasFocus) {
1266 if (v == mFolderName && hasFocus) {
1267 startEditingFolderName();
1268 }
1269 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001270
1271 @Override
1272 public void getHitRectRelativeToDragLayer(Rect outRect) {
1273 getHitRect(outRect);
Sunny Goyal48461932015-03-09 17:41:09 -07001274 outRect.left -= mScrollAreaOffset;
1275 outRect.right += mScrollAreaOffset;
1276 }
1277
1278 private class OnScrollHintListener implements OnAlarmListener {
1279
1280 private final DragObject mDragObject;
1281
1282 OnScrollHintListener(DragObject object) {
1283 mDragObject = object;
1284 }
1285
1286 /**
1287 * Scroll hint has been shown long enough. Now scroll to appropriate page.
1288 */
1289 @Override
1290 public void onAlarm(Alarm alarm) {
1291 if (mCurrentScrollDir == DragController.SCROLL_LEFT) {
Sunny Goyalb8634152015-04-09 14:17:14 -07001292 mContent.scrollLeft();
Sunny Goyal48461932015-03-09 17:41:09 -07001293 mScrollHintDir = DragController.SCROLL_NONE;
1294 } else if (mCurrentScrollDir == DragController.SCROLL_RIGHT) {
Sunny Goyalb8634152015-04-09 14:17:14 -07001295 mContent.scrollRight();
Sunny Goyal48461932015-03-09 17:41:09 -07001296 mScrollHintDir = DragController.SCROLL_NONE;
1297 } else {
1298 // This should not happen
1299 return;
1300 }
1301 mCurrentScrollDir = DragController.SCROLL_NONE;
1302
1303 // Pause drag event until the scrolling is finished
1304 mScrollPauseAlarm.setOnAlarmListener(new OnScrollFinishedListener(mDragObject));
1305 mScrollPauseAlarm.setAlarm(DragController.RESCROLL_DELAY);
1306 }
1307 }
1308
1309 private class OnScrollFinishedListener implements OnAlarmListener {
1310
1311 private final DragObject mDragObject;
1312
1313 OnScrollFinishedListener(DragObject object) {
1314 mDragObject = object;
1315 }
1316
1317 /**
1318 * Page scroll is complete.
1319 */
1320 @Override
1321 public void onAlarm(Alarm alarm) {
1322 // Reorder immediately on page change.
1323 onDragOver(mDragObject, 1);
1324 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001325 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001326}