blob: 1fca7ce689d59fbb44a51b916e12568dbd7d8518 [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 Goyalf4066152015-04-15 09:42:19 -070052import com.android.launcher3.DragController.DragListener;
Daniel Sandler325dc232013-06-05 22:57:57 -040053import com.android.launcher3.FolderInfo.FolderListener;
Sunny Goyalfa401a12015-04-10 13:45:42 -070054import com.android.launcher3.UninstallDropTarget.UninstallSource;
Sunny Goyalbc753352015-03-05 09:40:44 -080055import com.android.launcher3.Workspace.ItemOperator;
Adam Cohen091440a2015-03-18 14:16:05 -070056import com.android.launcher3.util.Thunk;
Romain Guyedcce092010-03-04 13:03:17 -080057
Adam Cohenc0dcf592011-06-01 15:30:43 -070058import java.util.ArrayList;
Adam Cohen3bf84d32012-05-07 20:17:14 -070059import java.util.Collections;
Adam Cohenc0dcf592011-06-01 15:30:43 -070060
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061/**
62 * Represents a set of icons chosen by the user or generated by the system.
63 */
Adam Cohen8dfcba42011-07-07 16:38:18 -070064public class Folder extends LinearLayout implements DragSource, View.OnClickListener,
Adam Cohenea0818d2011-09-30 20:06:58 -070065 View.OnLongClickListener, DropTarget, FolderListener, TextView.OnEditorActionListener,
Sunny Goyalfa401a12015-04-10 13:45:42 -070066 View.OnFocusChangeListener, DragListener, UninstallSource {
Adam Cohendf2cc412011-04-27 16:56:57 -070067 private static final String TAG = "Launcher.Folder";
68
Sunny Goyalc3a609f2015-02-26 17:43:50 -080069 /**
70 * We avoid measuring {@link #mContentWrapper} with a 0 width or height, as this
71 * results in CellLayout being measured as UNSPECIFIED, which it does not support.
72 */
73 private static final int MIN_CONTENT_DIMEN = 5;
Adam Cohen4eac29a2011-07-11 17:53:37 -070074
Adam Cohendf2cc412011-04-27 16:56:57 -070075 static final int STATE_NONE = -1;
76 static final int STATE_SMALL = 0;
77 static final int STATE_ANIMATING = 1;
78 static final int STATE_OPEN = 2;
79
Sunny Goyal48461932015-03-09 17:41:09 -070080 /**
Sunny Goyal48461932015-03-09 17:41:09 -070081 * Time for which the scroll hint is shown before automatically changing page.
82 */
83 public static final int SCROLL_HINT_DURATION = DragController.SCROLL_DELAY;
84
85 /**
Sunny Goyal5d85c442015-03-10 13:14:47 -070086 * Time in milliseconds for which an icon sticks to the target position
87 * in case of a sorted folder.
88 */
89 private static final int SORTED_STICKY_REORDER_DELAY = 1500;
90
91 /**
Sunny Goyal48461932015-03-09 17:41:09 -070092 * Fraction of icon width which behave as scroll region.
93 */
94 private static final float ICON_OVERSCROLL_WIDTH_FACTOR = 0.45f;
95
Adam Cohenf0f4eda2013-06-06 21:27:03 -070096 private static final int REORDER_DELAY = 250;
Adam Cohen5d518fa2013-12-05 14:16:23 -080097 private static final int ON_EXIT_CLOSE_DELAY = 400;
Sunny Goyalc3a609f2015-02-26 17:43:50 -080098 private static final Rect sTempRect = new Rect();
99
100 private static String sDefaultFolderName;
101 private static String sHintText;
102
103 private final Alarm mReorderAlarm = new Alarm();
104 private final Alarm mOnExitAlarm = new Alarm();
Sunny Goyalb8634152015-04-09 14:17:14 -0700105 private final Alarm mOnScrollHintAlarm = new Alarm();
106 @Thunk final Alarm mScrollPauseAlarm = new Alarm();
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800107
Adam Cohen091440a2015-03-18 14:16:05 -0700108 @Thunk final ArrayList<View> mItemsInReadingOrder = new ArrayList<View>();
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800109
110 private final int mExpandDuration;
111 private final int mMaterialExpandDuration;
112 private final int mMaterialExpandStagger;
113
114 private final InputMethodManager mInputMethodManager;
115
116 protected final Launcher mLauncher;
117 protected DragController mDragController;
118 protected FolderInfo mInfo;
119
Adam Cohen091440a2015-03-18 14:16:05 -0700120 @Thunk FolderIcon mFolderIcon;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800121
Sunny Goyalb8634152015-04-09 14:17:14 -0700122 @Thunk FolderPagedView mContent;
Adam Cohen091440a2015-03-18 14:16:05 -0700123 @Thunk View mContentWrapper;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800124 FolderEditText mFolderName;
125
Sunny Goyal290800b2015-03-05 11:33:33 -0800126 private View mFooter;
127 private int mFooterHeight;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800128
129 // Cell ranks used for drag and drop
Adam Cohen091440a2015-03-18 14:16:05 -0700130 @Thunk int mTargetRank, mPrevTargetRank, mEmptyCellRank;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800131
Adam Cohen091440a2015-03-18 14:16:05 -0700132 @Thunk int mState = STATE_NONE;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800133 private boolean mRearrangeOnClose = false;
Adam Cohen7c693212011-05-18 15:26:57 -0700134 boolean mItemsInvalidated = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700135 private ShortcutInfo mCurrentDragInfo;
136 private View mCurrentDragView;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800137 private boolean mIsExternalDrag;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700138 boolean mSuppressOnAdd = false;
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700139 private boolean mDragInProgress = false;
140 private boolean mDeleteFolderOnDropCompleted = false;
141 private boolean mSuppressFolderDeletion = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700142 private boolean mItemAddedBackToSelfViaIcon = false;
Adam Cohen091440a2015-03-18 14:16:05 -0700143 @Thunk float mFolderIconPivotX;
144 @Thunk float mFolderIconPivotY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700145 private boolean mIsEditingName = false;
Adam Cohen1960ea42013-11-12 11:33:14 +0000146
Adam Cohenfb91f302012-06-11 15:45:18 -0700147 private boolean mDestroyed;
148
Adam Cohen091440a2015-03-18 14:16:05 -0700149 @Thunk Runnable mDeferredAction;
Michael Jurka1e2f4652013-07-08 18:03:46 -0700150 private boolean mDeferDropAfterUninstall;
151 private boolean mUninstallSuccessful;
152
Sunny Goyal48461932015-03-09 17:41:09 -0700153 // Folder scrolling
154 private int mScrollAreaOffset;
Sunny Goyal48461932015-03-09 17:41:09 -0700155
Adam Cohen091440a2015-03-18 14:16:05 -0700156 @Thunk int mScrollHintDir = DragController.SCROLL_NONE;
157 @Thunk int mCurrentScrollDir = DragController.SCROLL_NONE;
Sunny Goyal48461932015-03-09 17:41:09 -0700158
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159 /**
160 * Used to inflate the Workspace from XML.
161 *
162 * @param context The application's context.
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800163 * @param attrs The attributes set containing the Workspace's customization values.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164 */
165 public Folder(Context context, AttributeSet attrs) {
166 super(context, attrs);
167 setAlwaysDrawnWithCacheEnabled(false);
Adam Cohen76fc0852011-06-17 13:26:23 -0700168 mInputMethodManager = (InputMethodManager)
Michael Jurka8b805b12012-04-18 14:23:14 -0700169 getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
Adam Cohen76fc0852011-06-17 13:26:23 -0700170
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800171 Resources res = getResources();
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700172 mExpandDuration = res.getInteger(R.integer.config_folderExpandDuration);
173 mMaterialExpandDuration = res.getInteger(R.integer.config_materialFolderExpandDuration);
174 mMaterialExpandStagger = res.getInteger(R.integer.config_materialFolderExpandStagger);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700175
176 if (sDefaultFolderName == null) {
177 sDefaultFolderName = res.getString(R.string.folder_name);
178 }
Adam Cohena65beee2011-06-27 21:32:23 -0700179 if (sHintText == null) {
180 sHintText = res.getString(R.string.folder_hint_text);
181 }
Adam Cohen4eac29a2011-07-11 17:53:37 -0700182 mLauncher = (Launcher) context;
Adam Cohenac56cff2011-09-28 20:45:37 -0700183 // We need this view to be focusable in touch mode so that when text editing of the folder
184 // name is complete, we have something to focus on, thus hiding the cursor and giving
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800185 // reliable behavior when clicking the text field (since it will always gain focus on click).
Adam Cohenac56cff2011-09-28 20:45:37 -0700186 setFocusableInTouchMode(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187 }
188
189 @Override
190 protected void onFinishInflate() {
191 super.onFinishInflate();
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800192 mContentWrapper = findViewById(R.id.folder_content_wrapper);
Sunny Goyalb8634152015-04-09 14:17:14 -0700193 mContent = (FolderPagedView) findViewById(R.id.folder_content);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800194 mContent.setFolder(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700195
Adam Cohenac56cff2011-09-28 20:45:37 -0700196 mFolderName = (FolderEditText) findViewById(R.id.folder_name);
197 mFolderName.setFolder(this);
Adam Cohenea0818d2011-09-30 20:06:58 -0700198 mFolderName.setOnFocusChangeListener(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700199
Adam Cohen76fc0852011-06-17 13:26:23 -0700200 // We disable action mode for now since it messes up the view on phones
201 mFolderName.setCustomSelectionActionModeCallback(mActionModeCallback);
Adam Cohen76fc0852011-06-17 13:26:23 -0700202 mFolderName.setOnEditorActionListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700203 mFolderName.setSelectAllOnFocus(true);
Adam Cohen7a14d0b2011-06-24 11:36:35 -0700204 mFolderName.setInputType(mFolderName.getInputType() |
205 InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800206
Sunny Goyalb8634152015-04-09 14:17:14 -0700207 mFooter = findViewById(R.id.folder_footer);
208 updateFooterHeight();
209 }
210
211 public void updateFooterHeight() {
Sunny Goyal290800b2015-03-05 11:33:33 -0800212 // We find out how tall footer wants to be (it is set to wrap_content), so that
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800213 // we can allocate the appropriate amount of space for it.
214 int measureSpec = MeasureSpec.UNSPECIFIED;
Sunny Goyal290800b2015-03-05 11:33:33 -0800215 mFooter.measure(measureSpec, measureSpec);
216 mFooterHeight = mFooter.getMeasuredHeight();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800217 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700218
Adam Cohen76fc0852011-06-17 13:26:23 -0700219 private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
220 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
221 return false;
222 }
223
224 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
225 return false;
226 }
227
228 public void onDestroyActionMode(ActionMode mode) {
229 }
230
231 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
232 return false;
233 }
234 };
235
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800236 public void onClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700237 Object tag = v.getTag();
238 if (tag instanceof ShortcutInfo) {
Adam Cohenb5fe60c2013-06-06 22:03:51 -0700239 mLauncher.onClick(v);
Adam Cohendf2cc412011-04-27 16:56:57 -0700240 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800241 }
242
243 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -0700244 // Return if global dragging is not enabled
245 if (!mLauncher.isDraggingEnabled()) return true;
246
Adam Cohendf2cc412011-04-27 16:56:57 -0700247 Object tag = v.getTag();
248 if (tag instanceof ShortcutInfo) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700249 ShortcutInfo item = (ShortcutInfo) tag;
250 if (!v.isInTouchMode()) {
251 return false;
252 }
253
Winson Chungb745afb2015-03-02 11:51:23 -0800254 mLauncher.getWorkspace().beginDragShared(v, new Point(), this, false);
Adam Cohen76078c42011-06-09 15:06:52 -0700255
256 mCurrentDragInfo = item;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800257 mEmptyCellRank = item.rank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700258 mCurrentDragView = v;
Adam Cohenfc53cd22011-07-20 15:45:11 -0700259
Sunny Goyal290800b2015-03-05 11:33:33 -0800260 mContent.removeItem(mCurrentDragView);
Adam Cohenfc53cd22011-07-20 15:45:11 -0700261 mInfo.remove(mCurrentDragInfo);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700262 mDragInProgress = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700263 mItemAddedBackToSelfViaIcon = false;
Adam Cohendf2cc412011-04-27 16:56:57 -0700264 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800265 return true;
266 }
267
Adam Cohen76fc0852011-06-17 13:26:23 -0700268 public boolean isEditingName() {
269 return mIsEditingName;
270 }
271
272 public void startEditingFolderName() {
Adam Cohena65beee2011-06-27 21:32:23 -0700273 mFolderName.setHint("");
Adam Cohen76fc0852011-06-17 13:26:23 -0700274 mIsEditingName = true;
275 }
276
277 public void dismissEditingName() {
278 mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
279 doneEditingFolderName(true);
280 }
281
282 public void doneEditingFolderName(boolean commit) {
Adam Cohena65beee2011-06-27 21:32:23 -0700283 mFolderName.setHint(sHintText);
Adam Cohen1df26a32011-08-12 16:15:23 -0700284 // Convert to a string here to ensure that no other state associated with the text field
285 // gets saved.
Adam Cohen3371da02011-10-25 21:38:29 -0700286 String newTitle = mFolderName.getText().toString();
287 mInfo.setTitle(newTitle);
Adam Cohen76fc0852011-06-17 13:26:23 -0700288 LauncherModel.updateItemInDatabase(mLauncher, mInfo);
Adam Cohenac56cff2011-09-28 20:45:37 -0700289
Adam Cohen3371da02011-10-25 21:38:29 -0700290 if (commit) {
291 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700292 String.format(getContext().getString(R.string.folder_renamed), newTitle));
Adam Cohen3371da02011-10-25 21:38:29 -0700293 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700294 // In order to clear the focus from the text field, we set the focus on ourself. This
295 // ensures that every time the field is clicked, focus is gained, giving reliable behavior.
296 requestFocus();
297
Adam Cohene601a432011-07-26 21:51:30 -0700298 Selection.setSelection((Spannable) mFolderName.getText(), 0, 0);
Adam Cohen76fc0852011-06-17 13:26:23 -0700299 mIsEditingName = false;
300 }
301
302 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
303 if (actionId == EditorInfo.IME_ACTION_DONE) {
304 dismissEditingName();
305 return true;
306 }
307 return false;
308 }
309
310 public View getEditTextRegion() {
311 return mFolderName;
312 }
313
Adam Cohen0c872ba2011-05-05 10:34:16 -0700314 /**
315 * We need to handle touch events to prevent them from falling through to the workspace below.
316 */
317 @Override
318 public boolean onTouchEvent(MotionEvent ev) {
319 return true;
320 }
321
Joe Onorato00acb122009-08-04 16:04:30 -0400322 public void setDragController(DragController dragController) {
323 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800324 }
325
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800326 public void setFolderIcon(FolderIcon icon) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700327 mFolderIcon = icon;
328 }
329
Adam Cohen3371da02011-10-25 21:38:29 -0700330 @Override
331 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
332 // When the folder gets focus, we don't want to announce the list of items.
333 return true;
334 }
335
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800336 /**
337 * @return the FolderInfo object associated with this folder
338 */
339 FolderInfo getInfo() {
340 return mInfo;
341 }
342
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800343 void bind(FolderInfo info) {
344 mInfo = info;
Adam Cohendf2cc412011-04-27 16:56:57 -0700345 ArrayList<ShortcutInfo> children = info.contents;
Sunny Goyal08f72612015-01-05 13:41:43 -0800346 Collections.sort(children, Utilities.RANK_COMPARATOR);
Sunny Goyal08f72612015-01-05 13:41:43 -0800347
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800348 ArrayList<ShortcutInfo> overflow = mContent.bindItems(children);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700349
Jason Monk4ff73882014-04-24 16:48:07 -0400350 // If our folder has too many items we prune them from the list. This is an issue
Adam Cohenc508b2d2011-06-28 14:41:44 -0700351 // when upgrading from the old Folders implementation which could contain an unlimited
352 // number of items.
353 for (ShortcutInfo item: overflow) {
354 mInfo.remove(item);
355 LauncherModel.deleteItemFromDatabase(mLauncher, item);
356 }
357
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800358 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
359 if (lp == null) {
360 lp = new DragLayer.LayoutParams(0, 0);
361 lp.customPosition = true;
362 setLayoutParams(lp);
363 }
364 centerAboutIcon();
365
Adam Cohen4dbe6d92011-05-18 17:14:15 -0700366 mItemsInvalidated = true;
Adam Cohenac56cff2011-09-28 20:45:37 -0700367 updateTextViewFocus();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700368 mInfo.addListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700369
Adam Cohenafb01ee2011-06-23 15:38:03 -0700370 if (!sDefaultFolderName.contentEquals(mInfo.title)) {
Adam Cohen4ef610f2011-06-21 22:37:36 -0700371 mFolderName.setText(mInfo.title);
372 } else {
373 mFolderName.setText("");
374 }
Winson Chung33231f52013-12-09 16:57:45 -0800375
376 // In case any children didn't come across during loading, clean up the folder accordingly
377 mFolderIcon.post(new Runnable() {
378 public void run() {
379 if (getItemCount() <= 1) {
380 replaceFolderWithFinalItem();
381 }
382 }
383 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700384 }
385
386 /**
387 * Creates a new UserFolder, inflated from R.layout.user_folder.
388 *
389 * @param context The application's context.
390 *
391 * @return A new UserFolder.
392 */
393 static Folder fromXml(Context context) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700394 return (Folder) LayoutInflater.from(context).inflate(R.layout.user_folder, null);
Adam Cohendf2cc412011-04-27 16:56:57 -0700395 }
396
397 /**
398 * This method is intended to make the UserFolder to be visually identical in size and position
399 * to its associated FolderIcon. This allows for a seamless transition into the expanded state.
400 */
401 private void positionAndSizeAsIcon() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700402 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800403 setScaleX(0.8f);
404 setScaleY(0.8f);
405 setAlpha(0f);
Adam Cohendf2cc412011-04-27 16:56:57 -0700406 mState = STATE_SMALL;
407 }
408
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700409 private void prepareReveal() {
410 setScaleX(1f);
411 setScaleY(1f);
412 setAlpha(1f);
413 mState = STATE_SMALL;
414 }
415
Adam Cohendf2cc412011-04-27 16:56:57 -0700416 public void animateOpen() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700417 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen6441de02011-12-14 14:25:32 -0800418
Sunny Goyalb8634152015-04-09 14:17:14 -0700419 mContent.completePendingPageChanges();
420 if (!(mDragInProgress && mContent.mIsSorted)) {
421 // Open on the first page.
422 mContent.snapToPageImmediately(0);
Sunny Goyal48461932015-03-09 17:41:09 -0700423 }
424
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700425 Animator openFolderAnim = null;
426 final Runnable onCompleteRunnable;
Kenny Guyd794a3f2014-09-16 15:17:58 +0100427 if (!Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700428 positionAndSizeAsIcon();
429 centerAboutIcon();
430
431 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1);
432 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
433 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
434 final ObjectAnimator oa =
435 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
436 oa.setDuration(mExpandDuration);
437 openFolderAnim = oa;
438
439 setLayerType(LAYER_TYPE_HARDWARE, null);
440 onCompleteRunnable = new Runnable() {
441 @Override
442 public void run() {
443 setLayerType(LAYER_TYPE_NONE, null);
444 }
445 };
446 } else {
447 prepareReveal();
448 centerAboutIcon();
449
450 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
451 int height = getFolderHeight();
452
453 float transX = - 0.075f * (width / 2 - getPivotX());
454 float transY = - 0.075f * (height / 2 - getPivotY());
455 setTranslationX(transX);
456 setTranslationY(transY);
457 PropertyValuesHolder tx = PropertyValuesHolder.ofFloat("translationX", transX, 0);
458 PropertyValuesHolder ty = PropertyValuesHolder.ofFloat("translationY", transY, 0);
459
460 int rx = (int) Math.max(Math.max(width - getPivotX(), 0), getPivotX());
461 int ry = (int) Math.max(Math.max(height - getPivotY(), 0), getPivotY());
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700462 float radius = (float) Math.hypot(rx, ry);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700463 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
464 Animator reveal = LauncherAnimUtils.createCircularReveal(this, (int) getPivotX(),
465 (int) getPivotY(), 0, radius);
466 reveal.setDuration(mMaterialExpandDuration);
467 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
468
Sunny Goyalbc753352015-03-05 09:40:44 -0800469 mContentWrapper.setAlpha(0f);
470 Animator iconsAlpha = LauncherAnimUtils.ofFloat(mContentWrapper, "alpha", 0f, 1f);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700471 iconsAlpha.setDuration(mMaterialExpandDuration);
472 iconsAlpha.setStartDelay(mMaterialExpandStagger);
473 iconsAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
474
Sunny Goyal290800b2015-03-05 11:33:33 -0800475 mFooter.setAlpha(0f);
476 Animator textAlpha = LauncherAnimUtils.ofFloat(mFooter, "alpha", 0f, 1f);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700477 textAlpha.setDuration(mMaterialExpandDuration);
478 textAlpha.setStartDelay(mMaterialExpandStagger);
479 textAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
480
481 Animator drift = LauncherAnimUtils.ofPropertyValuesHolder(this, tx, ty);
482 drift.setDuration(mMaterialExpandDuration);
483 drift.setStartDelay(mMaterialExpandStagger);
484 drift.setInterpolator(new LogDecelerateInterpolator(60, 0));
485
486 anim.play(drift);
487 anim.play(iconsAlpha);
488 anim.play(textAlpha);
489 anim.play(reveal);
490
491 openFolderAnim = anim;
492
Sunny Goyalbc753352015-03-05 09:40:44 -0800493 mContentWrapper.setLayerType(LAYER_TYPE_HARDWARE, null);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700494 onCompleteRunnable = new Runnable() {
495 @Override
496 public void run() {
Sunny Goyalbc753352015-03-05 09:40:44 -0800497 mContentWrapper.setLayerType(LAYER_TYPE_NONE, null);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700498 }
499 };
500 }
501 openFolderAnim.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700502 @Override
503 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700504 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Sunny Goyalbc753352015-03-05 09:40:44 -0800505 mContent.getAccessibilityDescription());
Adam Cohendf2cc412011-04-27 16:56:57 -0700506 mState = STATE_ANIMATING;
507 }
508 @Override
509 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700510 mState = STATE_OPEN;
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700511
512 if (onCompleteRunnable != null) {
513 onCompleteRunnable.run();
514 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800515
Sunny Goyalbc753352015-03-05 09:40:44 -0800516 mContent.setFocusOnFirstChild();
Adam Cohendf2cc412011-04-27 16:56:57 -0700517 }
518 });
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700519 openFolderAnim.start();
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800520
521 // Make sure the folder picks up the last drag move even if the finger doesn't move.
522 if (mDragController.isDragging()) {
523 mDragController.forceTouchMove();
524 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700525
Sunny Goyalb8634152015-04-09 14:17:14 -0700526 FolderPagedView pages = (FolderPagedView) mContent;
527 pages.verifyVisibleHighResIcons(pages.getNextPage());
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800528 }
529
530 public void beginExternalDrag(ShortcutInfo item) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800531 mCurrentDragInfo = item;
Sunny Goyal5d85c442015-03-10 13:14:47 -0700532 mEmptyCellRank = mContent.allocateRankForNewItem(item);
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800533 mIsExternalDrag = true;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800534 mDragInProgress = true;
Sunny Goyalb8634152015-04-09 14:17:14 -0700535
536 if (mContent.mIsSorted) {
Sunny Goyal5d85c442015-03-10 13:14:47 -0700537 mScrollPauseAlarm.setOnAlarmListener(null);
538 mScrollPauseAlarm.cancelAlarm();
539 mScrollPauseAlarm.setAlarm(SORTED_STICKY_REORDER_DELAY);
540 }
541
Sunny Goyalf4066152015-04-15 09:42:19 -0700542 // Since this folder opened by another controller, it might not get onDrop or
543 // onDropComplete. Perform cleanup once drag-n-drop ends.
544 mDragController.addDragListener(this);
545 }
546
547 @Override
548 public void onDragStart(DragSource source, Object info, int dragAction) { }
549
550 @Override
551 public void onDragEnd() {
552 if (mIsExternalDrag && mDragInProgress) {
553 completeDragExit();
554 }
555 mDragController.removeDragListener(this);
Adam Cohendf2cc412011-04-27 16:56:57 -0700556 }
557
Adam Cohen091440a2015-03-18 14:16:05 -0700558 @Thunk void sendCustomAccessibilityEvent(int type, String text) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700559 AccessibilityManager accessibilityManager = (AccessibilityManager)
560 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
561 if (accessibilityManager.isEnabled()) {
Adam Cohen3371da02011-10-25 21:38:29 -0700562 AccessibilityEvent event = AccessibilityEvent.obtain(type);
563 onInitializeAccessibilityEvent(event);
564 event.getText().add(text);
Michael Jurka8b805b12012-04-18 14:23:14 -0700565 accessibilityManager.sendAccessibilityEvent(event);
Adam Cohen3371da02011-10-25 21:38:29 -0700566 }
567 }
568
Adam Cohendf2cc412011-04-27 16:56:57 -0700569 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700570 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800571 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
572 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
573 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
Michael Jurka032e6ba2013-04-22 15:08:42 +0200574 final ObjectAnimator oa =
Michael Jurka2ecf9952012-06-18 12:52:28 -0700575 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohendf2cc412011-04-27 16:56:57 -0700576
Adam Cohen2801caf2011-05-13 20:57:39 -0700577 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700578 @Override
579 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700580 onCloseComplete();
Michael Jurka0121c3e2012-05-31 08:36:04 -0700581 setLayerType(LAYER_TYPE_NONE, null);
Adam Cohen2801caf2011-05-13 20:57:39 -0700582 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700583 }
584 @Override
585 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700586 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700587 getContext().getString(R.string.folder_closed));
Adam Cohendf2cc412011-04-27 16:56:57 -0700588 mState = STATE_ANIMATING;
589 }
590 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700591 oa.setDuration(mExpandDuration);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700592 setLayerType(LAYER_TYPE_HARDWARE, null);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100593 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700594 }
595
Adam Cohencb3382b2011-05-24 14:07:08 -0700596 public boolean acceptDrop(DragObject d) {
597 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700598 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700599 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
600 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
601 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700602 }
603
Adam Cohencb3382b2011-05-24 14:07:08 -0700604 public void onDragEnter(DragObject d) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800605 mPrevTargetRank = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700606 mOnExitAlarm.cancelAlarm();
Sunny Goyalb8634152015-04-09 14:17:14 -0700607 // Get the area offset such that the folder only closes if half the drag icon width
608 // is outside the folder area
609 mScrollAreaOffset = d.dragView.getDragRegionWidth() / 2 - d.xOffset;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700610 }
611
612 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
613 public void onAlarm(Alarm alarm) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800614 mContent.realTimeReorder(mEmptyCellRank, mTargetRank);
615 mEmptyCellRank = mTargetRank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700616 }
617 };
618
Sunny Goyalc46bfef2015-01-05 12:40:08 -0800619 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohen2374abf2013-04-16 14:56:57 -0700620 public boolean isLayoutRtl() {
621 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
622 }
623
Sunny Goyal48461932015-03-09 17:41:09 -0700624 @Override
Adam Cohencb3382b2011-05-24 14:07:08 -0700625 public void onDragOver(DragObject d) {
Sunny Goyal48461932015-03-09 17:41:09 -0700626 onDragOver(d, REORDER_DELAY);
627 }
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700628
Sunny Goyal48461932015-03-09 17:41:09 -0700629 private int getTargetRank(DragObject d, float[] recycle) {
630 recycle = d.getVisualCenter(recycle);
631 return mContent.findNearestArea(
632 (int) recycle[0] - getPaddingLeft(), (int) recycle[1] - getPaddingTop());
633 }
634
Adam Cohen091440a2015-03-18 14:16:05 -0700635 @Thunk void onDragOver(DragObject d, int reorderDelay) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700636 if (mScrollPauseAlarm.alarmPending()) {
Sunny Goyal48461932015-03-09 17:41:09 -0700637 return;
638 }
639 final float[] r = new float[2];
640 mTargetRank = getTargetRank(d, r);
641
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800642 if (mTargetRank != mPrevTargetRank) {
Alan Viverette4cda5b72013-08-28 17:53:41 -0700643 mReorderAlarm.cancelAlarm();
Sunny Goyalc46bfef2015-01-05 12:40:08 -0800644 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
645 mReorderAlarm.setAlarm(REORDER_DELAY);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800646 mPrevTargetRank = mTargetRank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700647 }
Sunny Goyal48461932015-03-09 17:41:09 -0700648
Sunny Goyal48461932015-03-09 17:41:09 -0700649 float x = r[0];
Sunny Goyalb8634152015-04-09 14:17:14 -0700650 int currentPage = mContent.getNextPage();
Sunny Goyal48461932015-03-09 17:41:09 -0700651
Sunny Goyalb8634152015-04-09 14:17:14 -0700652 float cellOverlap = mContent.getCurrentCellLayout().getCellWidth()
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700653 * ICON_OVERSCROLL_WIDTH_FACTOR;
654 boolean isOutsideLeftEdge = x < cellOverlap;
655 boolean isOutsideRightEdge = x > (getWidth() - cellOverlap);
Sunny Goyal48461932015-03-09 17:41:09 -0700656
Sunny Goyalb8634152015-04-09 14:17:14 -0700657 if (currentPage > 0 && (mContent.rtlLayout ? isOutsideRightEdge : isOutsideLeftEdge)) {
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700658 showScrollHint(DragController.SCROLL_LEFT, d);
Sunny Goyalb8634152015-04-09 14:17:14 -0700659 } else if (currentPage < (mContent.getPageCount() - 1)
660 && (mContent.rtlLayout ? isOutsideLeftEdge : isOutsideRightEdge)) {
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700661 showScrollHint(DragController.SCROLL_RIGHT, d);
Sunny Goyal48461932015-03-09 17:41:09 -0700662 } else {
663 mOnScrollHintAlarm.cancelAlarm();
664 if (mScrollHintDir != DragController.SCROLL_NONE) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700665 mContent.clearScrollHint();
Sunny Goyal48461932015-03-09 17:41:09 -0700666 mScrollHintDir = DragController.SCROLL_NONE;
667 }
668 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700669 }
670
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700671 private void showScrollHint(int direction, DragObject d) {
672 // Show scroll hint on the right
673 if (mScrollHintDir != direction) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700674 mContent.showScrollHint(direction);
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700675 mScrollHintDir = direction;
676 }
677
678 // Set alarm for when the hint is complete
679 if (!mOnScrollHintAlarm.alarmPending() || mCurrentScrollDir != direction) {
680 mCurrentScrollDir = direction;
681 mOnScrollHintAlarm.cancelAlarm();
682 mOnScrollHintAlarm.setOnAlarmListener(new OnScrollHintListener(d));
683 mOnScrollHintAlarm.setAlarm(SCROLL_HINT_DURATION);
684
685 mReorderAlarm.cancelAlarm();
686 mTargetRank = mEmptyCellRank;
687 }
688 }
689
Adam Cohenbfbfd262011-06-13 16:55:12 -0700690 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
691 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700692 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700693 }
694 };
695
Adam Cohen95bb8002011-07-03 23:40:28 -0700696 public void completeDragExit() {
Sunny Goyalf4066152015-04-15 09:42:19 -0700697 if (mInfo.opened) {
698 mLauncher.closeFolder();
699 mRearrangeOnClose = true;
700 } else {
701 rearrangeChildren();
702 }
Adam Cohen3e8f8112011-07-02 18:03:00 -0700703 mCurrentDragInfo = null;
704 mCurrentDragView = null;
705 mSuppressOnAdd = false;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800706 mIsExternalDrag = false;
Adam Cohen3e8f8112011-07-02 18:03:00 -0700707 }
708
Adam Cohencb3382b2011-05-24 14:07:08 -0700709 public void onDragExit(DragObject d) {
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700710 // We only close the folder if this is a true drag exit, ie. not because
711 // a drop has occurred above the folder.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700712 if (!d.dragComplete) {
713 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
714 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
715 }
716 mReorderAlarm.cancelAlarm();
Sunny Goyal48461932015-03-09 17:41:09 -0700717
Sunny Goyalb8634152015-04-09 14:17:14 -0700718 mOnScrollHintAlarm.cancelAlarm();
719 mScrollPauseAlarm.cancelAlarm();
720 if (mScrollHintDir != DragController.SCROLL_NONE) {
721 mContent.clearScrollHint();
722 mScrollHintDir = DragController.SCROLL_NONE;
Sunny Goyal48461932015-03-09 17:41:09 -0700723 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700724 }
725
Michael Jurka1e2f4652013-07-08 18:03:46 -0700726 public void onDropCompleted(final View target, final DragObject d,
727 final boolean isFlingToDelete, final boolean success) {
728 if (mDeferDropAfterUninstall) {
Michael Jurkaf3007582013-08-21 14:33:57 +0200729 Log.d(TAG, "Deferred handling drop because waiting for uninstall.");
Michael Jurka1e2f4652013-07-08 18:03:46 -0700730 mDeferredAction = new Runnable() {
731 public void run() {
732 onDropCompleted(target, d, isFlingToDelete, success);
733 mDeferredAction = null;
734 }
735 };
736 return;
737 }
738
739 boolean beingCalledAfterUninstall = mDeferredAction != null;
740 boolean successfulDrop =
741 success && (!beingCalledAfterUninstall || mUninstallSuccessful);
Winson Chung5f8afe62013-08-12 16:19:28 -0700742
Michael Jurka1e2f4652013-07-08 18:03:46 -0700743 if (successfulDrop) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800744 if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon && target != this) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700745 replaceFolderWithFinalItem();
746 }
747 } else {
748 // The drag failed, we need to return the item to the folder
Sunny Goyalb8c663c2015-04-23 11:43:48 -0700749 ShortcutInfo info = (ShortcutInfo) d.dragInfo;
750 View icon = (mCurrentDragView != null && mCurrentDragView.getTag() == info)
751 ? mCurrentDragView : mContent.createNewView(info);
752 ArrayList<View> views = getItemsInReadingOrder();
753 views.add(info.rank, icon);
754 mContent.arrangeChildren(views, views.size());
755 mItemsInvalidated = true;
756
757 mSuppressOnAdd = true;
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700758 mFolderIcon.onDrop(d);
Sunny Goyalb8c663c2015-04-23 11:43:48 -0700759 mSuppressOnAdd = false;
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700760 }
761
762 if (target != this) {
763 if (mOnExitAlarm.alarmPending()) {
764 mOnExitAlarm.cancelAlarm();
Michael Jurka54554252013-08-01 12:52:23 +0200765 if (!successfulDrop) {
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700766 mSuppressFolderDeletion = true;
767 }
Sunny Goyal5d85c442015-03-10 13:14:47 -0700768 mScrollPauseAlarm.cancelAlarm();
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700769 completeDragExit();
770 }
Adam Cohen9c58d822013-12-13 17:18:10 -0800771 }
772
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700773 mDeleteFolderOnDropCompleted = false;
774 mDragInProgress = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700775 mItemAddedBackToSelfViaIcon = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700776 mCurrentDragInfo = null;
777 mCurrentDragView = null;
778 mSuppressOnAdd = false;
Adam Cohen4045eb72011-10-06 11:44:26 -0700779
780 // Reordering may have occured, and we need to save the new item locations. We do this once
781 // at the end to prevent unnecessary database operations.
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700782 updateItemLocationsInDatabaseBatch();
Adam Cohen4045eb72011-10-06 11:44:26 -0700783 }
784
Sunny Goyalfa401a12015-04-10 13:45:42 -0700785 @Override
Michael Jurka1e2f4652013-07-08 18:03:46 -0700786 public void deferCompleteDropAfterUninstallActivity() {
787 mDeferDropAfterUninstall = true;
788 }
789
Sunny Goyalfa401a12015-04-10 13:45:42 -0700790 @Override
Michael Jurka1e2f4652013-07-08 18:03:46 -0700791 public void onUninstallActivityReturned(boolean success) {
792 mDeferDropAfterUninstall = false;
793 mUninstallSuccessful = success;
794 if (mDeferredAction != null) {
795 mDeferredAction.run();
796 }
797 }
798
Winson Chunga48487a2012-03-20 16:19:37 -0700799 @Override
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800800 public float getIntrinsicIconScaleFactor() {
801 return 1f;
802 }
803
804 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800805 public boolean supportsFlingToDelete() {
806 return true;
807 }
808
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000809 @Override
810 public boolean supportsAppInfoDropTarget() {
811 return false;
812 }
813
814 @Override
815 public boolean supportsDeleteDropTarget() {
816 return true;
817 }
818
Winson Chunga48487a2012-03-20 16:19:37 -0700819 public void onFlingToDelete(DragObject d, int x, int y, PointF vec) {
820 // Do nothing
821 }
822
823 @Override
824 public void onFlingToDeleteCompleted() {
825 // Do nothing
826 }
827
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700828 private void updateItemLocationsInDatabaseBatch() {
829 ArrayList<View> list = getItemsInReadingOrder();
830 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
831 for (int i = 0; i < list.size(); i++) {
832 View v = list.get(i);
833 ItemInfo info = (ItemInfo) v.getTag();
Sunny Goyal08f72612015-01-05 13:41:43 -0800834 info.rank = i;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700835 items.add(info);
836 }
837
838 LauncherModel.moveItemsInDatabase(mLauncher, items, mInfo.id, 0);
839 }
840
Adam Cohene25af792013-06-06 23:08:25 -0700841 public void addItemLocationsInDatabase() {
842 ArrayList<View> list = getItemsInReadingOrder();
843 for (int i = 0; i < list.size(); i++) {
844 View v = list.get(i);
845 ItemInfo info = (ItemInfo) v.getTag();
846 LauncherModel.addItemToDatabase(mLauncher, info, mInfo.id, 0,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700847 info.cellX, info.cellY);
Adam Cohene25af792013-06-06 23:08:25 -0700848 }
849 }
850
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700851 public void notifyDrop() {
852 if (mDragInProgress) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700853 mItemAddedBackToSelfViaIcon = true;
Adam Cohen76078c42011-06-09 15:06:52 -0700854 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700855 }
856
857 public boolean isDropEnabled() {
858 return true;
859 }
860
Adam Cohen2801caf2011-05-13 20:57:39 -0700861 public boolean isFull() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800862 return mContent.isFull();
Adam Cohen2801caf2011-05-13 20:57:39 -0700863 }
864
865 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700866 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700867
Winson Chung892c74d2013-08-22 16:15:50 -0700868 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700869 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700870 int height = getFolderHeight();
Adam Cohen2801caf2011-05-13 20:57:39 -0700871
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800872 float scale = parent.getDescendantRectRelativeToSelf(mFolderIcon, sTempRect);
Adam Cohen8e776a62011-06-28 18:10:06 -0700873
Winson Chungaf40f202013-09-18 18:26:31 -0700874 LauncherAppState app = LauncherAppState.getInstance();
875 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
876
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800877 int centerX = (int) (sTempRect.left + sTempRect.width() * scale / 2);
878 int centerY = (int) (sTempRect.top + sTempRect.height() * scale / 2);
Adam Cohen2801caf2011-05-13 20:57:39 -0700879 int centeredLeft = centerX - width / 2;
880 int centeredTop = centerY - height / 2;
Winson Chung3057b1c2013-10-10 17:35:35 -0700881 int currentPage = mLauncher.getWorkspace().getNextPage();
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800882 // In case the workspace is scrolling, we need to use the final scroll to compute
883 // the folders bounds.
884 mLauncher.getWorkspace().setFinalScrollForPageChange(currentPage);
Adam Cohen35e7e642011-07-17 14:47:18 -0700885 // We first fetch the currently visible CellLayoutChildren
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800886 CellLayout currentLayout = (CellLayout) mLauncher.getWorkspace().getChildAt(currentPage);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700887 ShortcutAndWidgetContainer boundingLayout = currentLayout.getShortcutsAndWidgets();
Adam Cohen35e7e642011-07-17 14:47:18 -0700888 Rect bounds = new Rect();
889 parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800890 // We reset the workspaces scroll
891 mLauncher.getWorkspace().resetFinalScrollForPageChange(currentPage);
Adam Cohen2801caf2011-05-13 20:57:39 -0700892
Adam Cohen35e7e642011-07-17 14:47:18 -0700893 // We need to bound the folder to the currently visible CellLayoutChildren
894 int left = Math.min(Math.max(bounds.left, centeredLeft),
895 bounds.left + bounds.width() - width);
896 int top = Math.min(Math.max(bounds.top, centeredTop),
897 bounds.top + bounds.height() - height);
Winson Chungaf40f202013-09-18 18:26:31 -0700898 if (grid.isPhone() && (grid.availableWidthPx - width) < grid.iconSizePx) {
899 // Center the folder if it is full (on phones only)
900 left = (grid.availableWidthPx - width) / 2;
901 } else if (width >= bounds.width()) {
902 // If the folder doesn't fit within the bounds, center it about the desired bounds
Adam Cohen35e7e642011-07-17 14:47:18 -0700903 left = bounds.left + (bounds.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700904 }
Adam Cohen35e7e642011-07-17 14:47:18 -0700905 if (height >= bounds.height()) {
906 top = bounds.top + (bounds.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700907 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700908
909 int folderPivotX = width / 2 + (centeredLeft - left);
910 int folderPivotY = height / 2 + (centeredTop - top);
911 setPivotX(folderPivotX);
912 setPivotY(folderPivotY);
Adam Cohen268c4752012-06-06 17:47:33 -0700913 mFolderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700914 (1.0f * folderPivotX / width));
Adam Cohen268c4752012-06-06 17:47:33 -0700915 mFolderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700916 (1.0f * folderPivotY / height));
Adam Cohen3bf84d32012-05-07 20:17:14 -0700917
Adam Cohen662b5982011-12-13 17:45:21 -0800918 lp.width = width;
919 lp.height = height;
920 lp.x = left;
921 lp.y = top;
Adam Cohen2801caf2011-05-13 20:57:39 -0700922 }
923
Adam Cohen268c4752012-06-06 17:47:33 -0700924 float getPivotXForIconAnimation() {
925 return mFolderIconPivotX;
926 }
927 float getPivotYForIconAnimation() {
928 return mFolderIconPivotY;
929 }
930
Winson Chung892c74d2013-08-22 16:15:50 -0700931 private int getContentAreaHeight() {
932 LauncherAppState app = LauncherAppState.getInstance();
933 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
934 Rect workspacePadding = grid.getWorkspacePadding(grid.isLandscape ?
935 CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
936 int maxContentAreaHeight = grid.availableHeightPx -
Winson Chung892c74d2013-08-22 16:15:50 -0700937 workspacePadding.top - workspacePadding.bottom -
Sunny Goyal290800b2015-03-05 11:33:33 -0800938 mFooterHeight;
Adam Cohen1960ea42013-11-12 11:33:14 +0000939 int height = Math.min(maxContentAreaHeight,
Winson Chung892c74d2013-08-22 16:15:50 -0700940 mContent.getDesiredHeight());
Adam Cohen1960ea42013-11-12 11:33:14 +0000941 return Math.max(height, MIN_CONTENT_DIMEN);
942 }
943
944 private int getContentAreaWidth() {
945 return Math.max(mContent.getDesiredWidth(), MIN_CONTENT_DIMEN);
Winson Chung892c74d2013-08-22 16:15:50 -0700946 }
947
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700948 private int getFolderHeight() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800949 return getFolderHeight(getContentAreaHeight());
950 }
951
952 private int getFolderHeight(int contentAreaHeight) {
Sunny Goyal290800b2015-03-05 11:33:33 -0800953 return getPaddingTop() + getPaddingBottom() + contentAreaHeight + mFooterHeight;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700954 }
Adam Cohen234c4cd2011-07-17 21:03:04 -0700955
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700956 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800957 int contentWidth = getContentAreaWidth();
958 int contentHeight = getContentAreaHeight();
Adam Cohen1960ea42013-11-12 11:33:14 +0000959
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800960 int contentAreaWidthSpec = MeasureSpec.makeMeasureSpec(contentWidth, MeasureSpec.EXACTLY);
961 int contentAreaHeightSpec = MeasureSpec.makeMeasureSpec(contentHeight, MeasureSpec.EXACTLY);
962
963 mContent.setFixedSize(contentWidth, contentHeight);
964 mContentWrapper.measure(contentAreaWidthSpec, contentAreaHeightSpec);
Sunny Goyal290800b2015-03-05 11:33:33 -0800965 mFooter.measure(contentAreaWidthSpec,
966 MeasureSpec.makeMeasureSpec(mFooterHeight, MeasureSpec.EXACTLY));
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800967
968 int folderWidth = getPaddingLeft() + getPaddingRight() + contentWidth;
969 int folderHeight = getFolderHeight(contentHeight);
970 setMeasuredDimension(folderWidth, folderHeight);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700971 }
972
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800973 /**
974 * Rearranges the children based on their rank.
975 */
976 public void rearrangeChildren() {
977 rearrangeChildren(-1);
978 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700979
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800980 /**
981 * Rearranges the children based on their rank.
982 * @param itemCount if greater than the total children count, empty spaces are left at the end,
983 * otherwise it is ignored.
984 */
985 public void rearrangeChildren(int itemCount) {
986 ArrayList<View> views = getItemsInReadingOrder();
987 mContent.arrangeChildren(views, Math.max(itemCount, views.size()));
Adam Cohen7c693212011-05-18 15:26:57 -0700988 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700989 }
990
Sunny Goyalc4918352015-03-10 18:15:48 -0700991 // TODO remove this once GSA code fix is submitted
992 public ViewGroup getContent() {
993 return (ViewGroup) mContent;
994 }
995
Adam Cohena9cf38f2011-05-02 15:36:58 -0700996 public int getItemCount() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800997 return mContent.getItemCount();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700998 }
999
Adam Cohen091440a2015-03-18 14:16:05 -07001000 @Thunk void onCloseComplete() {
Adam Cohen05e0f402011-08-01 12:12:49 -07001001 DragLayer parent = (DragLayer) getParent();
Michael Jurka5649c282012-06-18 10:33:21 -07001002 if (parent != null) {
1003 parent.removeView(this);
1004 }
Adam Cohen4554ee12011-08-03 16:13:21 -07001005 mDragController.removeDropTarget((DropTarget) this);
Adam Cohen05e0f402011-08-01 12:12:49 -07001006 clearFocus();
Adam Cohenac56cff2011-09-28 20:45:37 -07001007 mFolderIcon.requestFocus();
Adam Cohen05e0f402011-08-01 12:12:49 -07001008
Adam Cohen2801caf2011-05-13 20:57:39 -07001009 if (mRearrangeOnClose) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001010 rearrangeChildren();
Adam Cohen2801caf2011-05-13 20:57:39 -07001011 mRearrangeOnClose = false;
1012 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001013 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001014 if (!mDragInProgress && !mSuppressFolderDeletion) {
1015 replaceFolderWithFinalItem();
1016 } else if (mDragInProgress) {
1017 mDeleteFolderOnDropCompleted = true;
1018 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001019 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001020 mSuppressFolderDeletion = false;
Adam Cohenafb01ee2011-06-23 15:38:03 -07001021 }
1022
Adam Cohen091440a2015-03-18 14:16:05 -07001023 @Thunk void replaceFolderWithFinalItem() {
Adam Cohenafb01ee2011-06-23 15:38:03 -07001024 // Add the last remaining child to the workspace in place of the folder
Adam Cohenfb91f302012-06-11 15:45:18 -07001025 Runnable onCompleteRunnable = new Runnable() {
1026 @Override
1027 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -07001028 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screenId);
Adam Cohenafb01ee2011-06-23 15:38:03 -07001029
Winson Chung33231f52013-12-09 16:57:45 -08001030 View child = null;
Adam Cohenfb91f302012-06-11 15:45:18 -07001031 // Move the item from the folder to the workspace, in the position of the folder
1032 if (getItemCount() == 1) {
1033 ShortcutInfo finalItem = mInfo.contents.get(0);
Adam Cohenc5e63f32012-07-12 16:16:57 -07001034 child = mLauncher.createShortcut(R.layout.application, cellLayout,
Adam Cohenfb91f302012-06-11 15:45:18 -07001035 finalItem);
1036 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -07001037 mInfo.screenId, mInfo.cellX, mInfo.cellY);
Adam Cohenfb91f302012-06-11 15:45:18 -07001038 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001039 if (getItemCount() <= 1) {
1040 // Remove the folder
1041 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
Dan Sandler0eb687f2014-01-10 13:24:55 -05001042 if (cellLayout != null) {
1043 // b/12446428 -- sometimes the cell layout has already gone away?
1044 cellLayout.removeView(mFolderIcon);
1045 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001046 if (mFolderIcon instanceof DropTarget) {
1047 mDragController.removeDropTarget((DropTarget) mFolderIcon);
1048 }
1049 mLauncher.removeFolder(mInfo);
1050 }
Adam Cohenc5e63f32012-07-12 16:16:57 -07001051 // We add the child after removing the folder to prevent both from existing at
Winson Chung0e6a7132013-08-23 12:55:10 -07001052 // the same time in the CellLayout. We need to add the new item with addInScreenFromBind()
1053 // to ensure that hotseat items are placed correctly.
Adam Cohenc5e63f32012-07-12 16:16:57 -07001054 if (child != null) {
Winson Chung0e6a7132013-08-23 12:55:10 -07001055 mLauncher.getWorkspace().addInScreenFromBind(child, mInfo.container, mInfo.screenId,
Adam Cohenc5e63f32012-07-12 16:16:57 -07001056 mInfo.cellX, mInfo.cellY, mInfo.spanX, mInfo.spanY);
1057 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001058 }
1059 };
Sunny Goyalbc753352015-03-05 09:40:44 -08001060 View finalChild = mContent.getLastItem();
Adam Cohenfb91f302012-06-11 15:45:18 -07001061 if (finalChild != null) {
1062 mFolderIcon.performDestroyAnimation(finalChild, onCompleteRunnable);
Winson Chung33231f52013-12-09 16:57:45 -08001063 } else {
1064 onCompleteRunnable.run();
Adam Cohenafb01ee2011-06-23 15:38:03 -07001065 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001066 mDestroyed = true;
1067 }
1068
1069 boolean isDestroyed() {
1070 return mDestroyed;
Adam Cohen2801caf2011-05-13 20:57:39 -07001071 }
1072
Adam Cohenac56cff2011-09-28 20:45:37 -07001073 // This method keeps track of the last item in the folder for the purposes
1074 // of keyboard focus
Sunny Goyal290800b2015-03-05 11:33:33 -08001075 public void updateTextViewFocus() {
Sunny Goyalbc753352015-03-05 09:40:44 -08001076 View lastChild = mContent.getLastItem();
Adam Cohenac56cff2011-09-28 20:45:37 -07001077 if (lastChild != null) {
1078 mFolderName.setNextFocusDownId(lastChild.getId());
1079 mFolderName.setNextFocusRightId(lastChild.getId());
1080 mFolderName.setNextFocusLeftId(lastChild.getId());
1081 mFolderName.setNextFocusUpId(lastChild.getId());
1082 }
1083 }
1084
Adam Cohenbfbfd262011-06-13 16:55:12 -07001085 public void onDrop(DragObject d) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001086 Runnable cleanUpRunnable = null;
1087
Adam Cohen689ff162014-05-08 17:27:56 -07001088 // If we are coming from All Apps space, we defer removing the extra empty screen
1089 // until the folder closes
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001090 if (d.dragSource != mLauncher.getWorkspace() && !(d.dragSource instanceof Folder)) {
1091 cleanUpRunnable = new Runnable() {
1092 @Override
1093 public void run() {
Adam Cohen689ff162014-05-08 17:27:56 -07001094 mLauncher.exitSpringLoadedDragModeDelayed(true,
1095 Launcher.EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
1096 null);
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001097 }
1098 };
Adam Cohenbfbfd262011-06-13 16:55:12 -07001099 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001100
Sunny Goyalb8634152015-04-09 14:17:14 -07001101 // If the icon was dropped while the page was being scrolled, we need to compute
1102 // the target location again such that the icon is placed of the final page.
1103 if (!mContent.rankOnCurrentPage(mEmptyCellRank)) {
1104 // Reorder again.
1105 mTargetRank = getTargetRank(d, null);
Sunny Goyal48461932015-03-09 17:41:09 -07001106
Sunny Goyalb8634152015-04-09 14:17:14 -07001107 // Rearrange items immediately.
1108 mReorderAlarmListener.onAlarm(mReorderAlarm);
Sunny Goyal48461932015-03-09 17:41:09 -07001109
Sunny Goyalb8634152015-04-09 14:17:14 -07001110 mOnScrollHintAlarm.cancelAlarm();
1111 mScrollPauseAlarm.cancelAlarm();
Sunny Goyal48461932015-03-09 17:41:09 -07001112 }
Sunny Goyalb8634152015-04-09 14:17:14 -07001113 mContent.completePendingPageChanges();
Sunny Goyal48461932015-03-09 17:41:09 -07001114
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001115 View currentDragView;
1116 ShortcutInfo si = mCurrentDragInfo;
1117 if (mIsExternalDrag) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001118 currentDragView = mContent.createAndAddViewForRank(si, mEmptyCellRank);
Sunny Goyal95abbb32014-08-04 10:53:22 -07001119 // Actually move the item in the database if it was an external drag. Call this
1120 // before creating the view, so that ShortcutInfo is updated appropriately.
1121 LauncherModel.addOrMoveItemInDatabase(
1122 mLauncher, si, mInfo.id, 0, si.cellX, si.cellY);
1123
1124 // We only need to update the locations if it doesn't get handled in #onDropCompleted.
1125 if (d.dragSource != this) {
1126 updateItemLocationsInDatabaseBatch();
1127 }
1128 mIsExternalDrag = false;
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001129 } else {
1130 currentDragView = mCurrentDragView;
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001131 mContent.addViewForRank(currentDragView, si, mEmptyCellRank);
Adam Cohenbfbfd262011-06-13 16:55:12 -07001132 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001133
1134 if (d.dragView.hasDrawn()) {
1135
1136 // Temporarily reset the scale such that the animation target gets calculated correctly.
1137 float scaleX = getScaleX();
1138 float scaleY = getScaleY();
1139 setScaleX(1.0f);
1140 setScaleY(1.0f);
1141 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, currentDragView,
1142 cleanUpRunnable, null);
1143 setScaleX(scaleX);
1144 setScaleY(scaleY);
1145 } else {
1146 d.deferDragViewCleanupPostAnimation = false;
1147 currentDragView.setVisibility(VISIBLE);
1148 }
1149 mItemsInvalidated = true;
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001150 rearrangeChildren();
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001151
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001152 // Temporarily suppress the listener, as we did all the work already here.
1153 mSuppressOnAdd = true;
1154 mInfo.add(si);
1155 mSuppressOnAdd = false;
Sunny Goyal4b020172014-08-28 14:51:14 -07001156 // Clear the drag info, as it is no longer being dragged.
1157 mCurrentDragInfo = null;
Sunny Goyalf4066152015-04-15 09:42:19 -07001158 mDragInProgress = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001159 }
1160
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001161 // This is used so the item doesn't immediately appear in the folder when added. In one case
1162 // we need to create the illusion that the item isn't added back to the folder yet, to
1163 // to correspond to the animation of the icon back into the folder. This is
1164 public void hideItem(ShortcutInfo info) {
1165 View v = getViewForInfo(info);
1166 v.setVisibility(INVISIBLE);
1167 }
1168 public void showItem(ShortcutInfo info) {
1169 View v = getViewForInfo(info);
1170 v.setVisibility(VISIBLE);
1171 }
1172
Adam Cohenbfbfd262011-06-13 16:55:12 -07001173 public void onAdd(ShortcutInfo item) {
Adam Cohen05e0f402011-08-01 12:12:49 -07001174 // If the item was dropped onto this open folder, we have done the work associated
1175 // with adding the item to the folder, as indicated by mSuppressOnAdd being set
Adam Cohenbfbfd262011-06-13 16:55:12 -07001176 if (mSuppressOnAdd) return;
Sunny Goyal5d85c442015-03-10 13:14:47 -07001177 mContent.createAndAddViewForRank(item, mContent.allocateRankForNewItem(item));
Sunny Goyal2e688a82015-03-18 10:23:39 -07001178 mItemsInvalidated = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001179 LauncherModel.addOrMoveItemInDatabase(
1180 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
1181 }
1182
Adam Cohena9cf38f2011-05-02 15:36:58 -07001183 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -07001184 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001185 // If this item is being dragged from this open folder, we have already handled
1186 // the work associated with removing the item, so we don't have to do anything here.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001187 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001188 View v = getViewForInfo(item);
Sunny Goyal290800b2015-03-05 11:33:33 -08001189 mContent.removeItem(v);
Adam Cohen2801caf2011-05-13 20:57:39 -07001190 if (mState == STATE_ANIMATING) {
1191 mRearrangeOnClose = true;
1192 } else {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001193 rearrangeChildren();
Adam Cohen2801caf2011-05-13 20:57:39 -07001194 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001195 if (getItemCount() <= 1) {
1196 replaceFolderWithFinalItem();
1197 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001198 }
Adam Cohen7c693212011-05-18 15:26:57 -07001199
Sunny Goyalbc753352015-03-05 09:40:44 -08001200 private View getViewForInfo(final ShortcutInfo item) {
1201 return mContent.iterateOverItems(new ItemOperator() {
1202
1203 @Override
1204 public boolean evaluate(ItemInfo info, View view, View parent) {
1205 return info == item;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001206 }
Sunny Goyalbc753352015-03-05 09:40:44 -08001207 });
Adam Cohendf1e4e82011-06-24 15:57:39 -07001208 }
1209
Adam Cohen76078c42011-06-09 15:06:52 -07001210 public void onItemsChanged() {
Adam Cohenac56cff2011-09-28 20:45:37 -07001211 updateTextViewFocus();
Adam Cohen76078c42011-06-09 15:06:52 -07001212 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001213
Adam Cohen76fc0852011-06-17 13:26:23 -07001214 public void onTitleChanged(CharSequence title) {
1215 }
Adam Cohen76078c42011-06-09 15:06:52 -07001216
Adam Cohen7c693212011-05-18 15:26:57 -07001217 public ArrayList<View> getItemsInReadingOrder() {
1218 if (mItemsInvalidated) {
1219 mItemsInReadingOrder.clear();
Sunny Goyalbc753352015-03-05 09:40:44 -08001220 mContent.iterateOverItems(new ItemOperator() {
1221
1222 @Override
1223 public boolean evaluate(ItemInfo info, View view, View parent) {
1224 mItemsInReadingOrder.add(view);
1225 return false;
Adam Cohen7c693212011-05-18 15:26:57 -07001226 }
Sunny Goyalbc753352015-03-05 09:40:44 -08001227 });
Adam Cohen7c693212011-05-18 15:26:57 -07001228 mItemsInvalidated = false;
1229 }
1230 return mItemsInReadingOrder;
1231 }
Adam Cohen8dfcba42011-07-07 16:38:18 -07001232
1233 public void getLocationInDragLayer(int[] loc) {
1234 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
1235 }
Adam Cohenea0818d2011-09-30 20:06:58 -07001236
1237 public void onFocusChange(View v, boolean hasFocus) {
1238 if (v == mFolderName && hasFocus) {
1239 startEditingFolderName();
1240 }
1241 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001242
1243 @Override
1244 public void getHitRectRelativeToDragLayer(Rect outRect) {
1245 getHitRect(outRect);
Sunny Goyal48461932015-03-09 17:41:09 -07001246 outRect.left -= mScrollAreaOffset;
1247 outRect.right += mScrollAreaOffset;
1248 }
1249
1250 private class OnScrollHintListener implements OnAlarmListener {
1251
1252 private final DragObject mDragObject;
1253
1254 OnScrollHintListener(DragObject object) {
1255 mDragObject = object;
1256 }
1257
1258 /**
1259 * Scroll hint has been shown long enough. Now scroll to appropriate page.
1260 */
1261 @Override
1262 public void onAlarm(Alarm alarm) {
1263 if (mCurrentScrollDir == DragController.SCROLL_LEFT) {
Sunny Goyalb8634152015-04-09 14:17:14 -07001264 mContent.scrollLeft();
Sunny Goyal48461932015-03-09 17:41:09 -07001265 mScrollHintDir = DragController.SCROLL_NONE;
1266 } else if (mCurrentScrollDir == DragController.SCROLL_RIGHT) {
Sunny Goyalb8634152015-04-09 14:17:14 -07001267 mContent.scrollRight();
Sunny Goyal48461932015-03-09 17:41:09 -07001268 mScrollHintDir = DragController.SCROLL_NONE;
1269 } else {
1270 // This should not happen
1271 return;
1272 }
1273 mCurrentScrollDir = DragController.SCROLL_NONE;
1274
1275 // Pause drag event until the scrolling is finished
1276 mScrollPauseAlarm.setOnAlarmListener(new OnScrollFinishedListener(mDragObject));
1277 mScrollPauseAlarm.setAlarm(DragController.RESCROLL_DELAY);
1278 }
1279 }
1280
1281 private class OnScrollFinishedListener implements OnAlarmListener {
1282
1283 private final DragObject mDragObject;
1284
1285 OnScrollFinishedListener(DragObject object) {
1286 mDragObject = object;
1287 }
1288
1289 /**
1290 * Page scroll is complete.
1291 */
1292 @Override
1293 public void onAlarm(Alarm alarm) {
1294 // Reorder immediately on page change.
1295 onDragOver(mDragObject, 1);
1296 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001297 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001298}