blob: 72dc1e94a546144ca0fe9542921030b39a7a84b5 [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 Goyal70660032015-05-14 00:07:08 -070024import android.annotation.SuppressLint;
Sunny Goyalc46bfef2015-01-05 12:40:08 -080025import android.annotation.TargetApi;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080026import android.content.Context;
Adam Cohen76fc0852011-06-17 13:26:23 -070027import android.content.res.Resources;
Winson Chungb745afb2015-03-02 11:51:23 -080028import android.graphics.Point;
Winson Chung043f2af2012-03-01 16:09:54 -080029import android.graphics.PointF;
Romain Guyfb5411e2010-02-24 10:04:17 -080030import android.graphics.Rect;
Sunny Goyalc46bfef2015-01-05 12:40:08 -080031import android.os.Build;
Adam Cohen7a14d0b2011-06-24 11:36:35 -070032import android.text.InputType;
Adam Cohene601a432011-07-26 21:51:30 -070033import android.text.Selection;
34import android.text.Spannable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.util.AttributeSet;
Adam Cohen3bf84d32012-05-07 20:17:14 -070036import android.util.Log;
Adam Cohen76fc0852011-06-17 13:26:23 -070037import android.view.ActionMode;
38import android.view.KeyEvent;
Adam Cohendf2cc412011-04-27 16:56:57 -070039import android.view.LayoutInflater;
Adam Cohen76fc0852011-06-17 13:26:23 -070040import android.view.Menu;
41import android.view.MenuItem;
Adam Cohen0c872ba2011-05-05 10:34:16 -070042import android.view.MotionEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.view.View;
Sunny Goyalc4918352015-03-10 18:15:48 -070044import android.view.ViewGroup;
Adam Cohen3371da02011-10-25 21:38:29 -070045import android.view.accessibility.AccessibilityEvent;
46import android.view.accessibility.AccessibilityManager;
Adam Cohenc4fe9ea2014-08-18 18:54:10 -070047import android.view.animation.AccelerateInterpolator;
Adam Cohen76fc0852011-06-17 13:26:23 -070048import android.view.inputmethod.EditorInfo;
49import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.widget.LinearLayout;
Adam Cohendf2cc412011-04-27 16:56:57 -070051import android.widget.TextView;
Sunny Goyal83a8f042015-05-19 12:52:12 -070052
Sunny Goyale9b651e2015-04-24 11:44:51 -070053import com.android.launcher3.CellLayout.CellInfo;
Sunny Goyalf4066152015-04-15 09:42:19 -070054import com.android.launcher3.DragController.DragListener;
Daniel Sandler325dc232013-06-05 22:57:57 -040055import com.android.launcher3.FolderInfo.FolderListener;
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;
Sunny Goyal83a8f042015-05-19 12:52:12 -070058import com.android.launcher3.accessibility.LauncherAccessibilityDelegate.AccessibilityDragSource;
Adam Cohen091440a2015-03-18 14:16:05 -070059import com.android.launcher3.util.Thunk;
Sunny Goyal83a8f042015-05-19 12:52:12 -070060
Adam Cohenc0dcf592011-06-01 15:30:43 -070061import java.util.ArrayList;
Adam Cohen3bf84d32012-05-07 20:17:14 -070062import java.util.Collections;
Adam Cohenc0dcf592011-06-01 15:30:43 -070063
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064/**
65 * Represents a set of icons chosen by the user or generated by the system.
66 */
Adam Cohen8dfcba42011-07-07 16:38:18 -070067public class Folder extends LinearLayout implements DragSource, View.OnClickListener,
Adam Cohenea0818d2011-09-30 20:06:58 -070068 View.OnLongClickListener, DropTarget, FolderListener, TextView.OnEditorActionListener,
Sunny Goyale9b651e2015-04-24 11:44:51 -070069 View.OnFocusChangeListener, DragListener, UninstallSource, AccessibilityDragSource {
Adam Cohendf2cc412011-04-27 16:56:57 -070070 private static final String TAG = "Launcher.Folder";
71
Sunny Goyalc3a609f2015-02-26 17:43:50 -080072 /**
73 * We avoid measuring {@link #mContentWrapper} with a 0 width or height, as this
74 * results in CellLayout being measured as UNSPECIFIED, which it does not support.
75 */
76 private static final int MIN_CONTENT_DIMEN = 5;
Adam Cohen4eac29a2011-07-11 17:53:37 -070077
Adam Cohendf2cc412011-04-27 16:56:57 -070078 static final int STATE_NONE = -1;
79 static final int STATE_SMALL = 0;
80 static final int STATE_ANIMATING = 1;
81 static final int STATE_OPEN = 2;
82
Sunny Goyal48461932015-03-09 17:41:09 -070083 /**
Sunny Goyal48461932015-03-09 17:41:09 -070084 * Time for which the scroll hint is shown before automatically changing page.
85 */
86 public static final int SCROLL_HINT_DURATION = DragController.SCROLL_DELAY;
87
88 /**
89 * Fraction of icon width which behave as scroll region.
90 */
91 private static final float ICON_OVERSCROLL_WIDTH_FACTOR = 0.45f;
92
Sunny Goyalb7e15ad2015-05-07 14:51:31 -070093 public static final int FOOTER_ANIMATION_DURATION = 200;
94
Adam Cohenf0f4eda2013-06-06 21:27:03 -070095 private static final int REORDER_DELAY = 250;
Adam Cohen5d518fa2013-12-05 14:16:23 -080096 private static final int ON_EXIT_CLOSE_DELAY = 400;
Sunny Goyalc3a609f2015-02-26 17:43:50 -080097 private static final Rect sTempRect = new Rect();
98
99 private static String sDefaultFolderName;
100 private static String sHintText;
101
102 private final Alarm mReorderAlarm = new Alarm();
103 private final Alarm mOnExitAlarm = new Alarm();
Sunny Goyalb8634152015-04-09 14:17:14 -0700104 private final Alarm mOnScrollHintAlarm = new Alarm();
105 @Thunk final Alarm mScrollPauseAlarm = new Alarm();
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800106
Adam Cohen091440a2015-03-18 14:16:05 -0700107 @Thunk final ArrayList<View> mItemsInReadingOrder = new ArrayList<View>();
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800108
109 private final int mExpandDuration;
110 private final int mMaterialExpandDuration;
111 private final int mMaterialExpandStagger;
112
113 private final InputMethodManager mInputMethodManager;
114
115 protected final Launcher mLauncher;
116 protected DragController mDragController;
117 protected FolderInfo mInfo;
118
Adam Cohen091440a2015-03-18 14:16:05 -0700119 @Thunk FolderIcon mFolderIcon;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800120
Sunny Goyalb8634152015-04-09 14:17:14 -0700121 @Thunk FolderPagedView mContent;
Adam Cohen091440a2015-03-18 14:16:05 -0700122 @Thunk View mContentWrapper;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800123 FolderEditText mFolderName;
124
Sunny Goyal290800b2015-03-05 11:33:33 -0800125 private View mFooter;
126 private int mFooterHeight;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800127
128 // Cell ranks used for drag and drop
Adam Cohen091440a2015-03-18 14:16:05 -0700129 @Thunk int mTargetRank, mPrevTargetRank, mEmptyCellRank;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800130
Adam Cohen091440a2015-03-18 14:16:05 -0700131 @Thunk int mState = STATE_NONE;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800132 private boolean mRearrangeOnClose = false;
Adam Cohen7c693212011-05-18 15:26:57 -0700133 boolean mItemsInvalidated = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700134 private ShortcutInfo mCurrentDragInfo;
135 private View mCurrentDragView;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800136 private boolean mIsExternalDrag;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700137 boolean mSuppressOnAdd = false;
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700138 private boolean mDragInProgress = false;
139 private boolean mDeleteFolderOnDropCompleted = false;
140 private boolean mSuppressFolderDeletion = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700141 private boolean mItemAddedBackToSelfViaIcon = false;
Adam Cohen091440a2015-03-18 14:16:05 -0700142 @Thunk float mFolderIconPivotX;
143 @Thunk float mFolderIconPivotY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700144 private boolean mIsEditingName = false;
Adam Cohen1960ea42013-11-12 11:33:14 +0000145
Adam Cohenfb91f302012-06-11 15:45:18 -0700146 private boolean mDestroyed;
147
Adam Cohen091440a2015-03-18 14:16:05 -0700148 @Thunk Runnable mDeferredAction;
Michael Jurka1e2f4652013-07-08 18:03:46 -0700149 private boolean mDeferDropAfterUninstall;
150 private boolean mUninstallSuccessful;
151
Sunny Goyal48461932015-03-09 17:41:09 -0700152 // Folder scrolling
153 private int mScrollAreaOffset;
Sunny Goyal48461932015-03-09 17:41:09 -0700154
Adam Cohen091440a2015-03-18 14:16:05 -0700155 @Thunk int mScrollHintDir = DragController.SCROLL_NONE;
156 @Thunk int mCurrentScrollDir = DragController.SCROLL_NONE;
Sunny Goyal48461932015-03-09 17:41:09 -0700157
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158 /**
159 * Used to inflate the Workspace from XML.
160 *
161 * @param context The application's context.
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800162 * @param attrs The attributes set containing the Workspace's customization values.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163 */
164 public Folder(Context context, AttributeSet attrs) {
165 super(context, attrs);
166 setAlwaysDrawnWithCacheEnabled(false);
Adam Cohen76fc0852011-06-17 13:26:23 -0700167 mInputMethodManager = (InputMethodManager)
Michael Jurka8b805b12012-04-18 14:23:14 -0700168 getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
Adam Cohen76fc0852011-06-17 13:26:23 -0700169
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800170 Resources res = getResources();
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700171 mExpandDuration = res.getInteger(R.integer.config_folderExpandDuration);
172 mMaterialExpandDuration = res.getInteger(R.integer.config_materialFolderExpandDuration);
173 mMaterialExpandStagger = res.getInteger(R.integer.config_materialFolderExpandStagger);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700174
175 if (sDefaultFolderName == null) {
176 sDefaultFolderName = res.getString(R.string.folder_name);
177 }
Adam Cohena65beee2011-06-27 21:32:23 -0700178 if (sHintText == null) {
179 sHintText = res.getString(R.string.folder_hint_text);
180 }
Adam Cohen4eac29a2011-07-11 17:53:37 -0700181 mLauncher = (Launcher) context;
Adam Cohenac56cff2011-09-28 20:45:37 -0700182 // We need this view to be focusable in touch mode so that when text editing of the folder
183 // name is complete, we have something to focus on, thus hiding the cursor and giving
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800184 // reliable behavior when clicking the text field (since it will always gain focus on click).
Adam Cohenac56cff2011-09-28 20:45:37 -0700185 setFocusableInTouchMode(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186 }
187
188 @Override
189 protected void onFinishInflate() {
190 super.onFinishInflate();
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800191 mContentWrapper = findViewById(R.id.folder_content_wrapper);
Sunny Goyalb8634152015-04-09 14:17:14 -0700192 mContent = (FolderPagedView) findViewById(R.id.folder_content);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800193 mContent.setFolder(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700194
Adam Cohenac56cff2011-09-28 20:45:37 -0700195 mFolderName = (FolderEditText) findViewById(R.id.folder_name);
196 mFolderName.setFolder(this);
Adam Cohenea0818d2011-09-30 20:06:58 -0700197 mFolderName.setOnFocusChangeListener(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700198
Adam Cohen76fc0852011-06-17 13:26:23 -0700199 // We disable action mode for now since it messes up the view on phones
200 mFolderName.setCustomSelectionActionModeCallback(mActionModeCallback);
Adam Cohen76fc0852011-06-17 13:26:23 -0700201 mFolderName.setOnEditorActionListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700202 mFolderName.setSelectAllOnFocus(true);
Adam Cohen7a14d0b2011-06-24 11:36:35 -0700203 mFolderName.setInputType(mFolderName.getInputType() |
204 InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800205
Sunny Goyalb8634152015-04-09 14:17:14 -0700206 mFooter = findViewById(R.id.folder_footer);
Sunny Goyalb8634152015-04-09 14:17:14 -0700207
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 }
Sunny Goyalccc414b2015-04-30 12:28:16 -0700278
279 mFooter.setImportantForAccessibility(enable ? IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS :
280 IMPORTANT_FOR_ACCESSIBILITY_AUTO);
Sunny Goyale9b651e2015-04-24 11:44:51 -0700281 mLauncher.getWorkspace().setAddNewPageOnDrag(!enable);
282 }
283
Adam Cohen76fc0852011-06-17 13:26:23 -0700284 public boolean isEditingName() {
285 return mIsEditingName;
286 }
287
288 public void startEditingFolderName() {
Adam Cohena65beee2011-06-27 21:32:23 -0700289 mFolderName.setHint("");
Adam Cohen76fc0852011-06-17 13:26:23 -0700290 mIsEditingName = true;
291 }
292
293 public void dismissEditingName() {
294 mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
295 doneEditingFolderName(true);
296 }
297
298 public void doneEditingFolderName(boolean commit) {
Adam Cohena65beee2011-06-27 21:32:23 -0700299 mFolderName.setHint(sHintText);
Adam Cohen1df26a32011-08-12 16:15:23 -0700300 // Convert to a string here to ensure that no other state associated with the text field
301 // gets saved.
Winson Chung82b016c2015-05-08 17:00:10 -0700302 CharSequence newTitle = mFolderName.getText();
Adam Cohen3371da02011-10-25 21:38:29 -0700303 mInfo.setTitle(newTitle);
Adam Cohen76fc0852011-06-17 13:26:23 -0700304 LauncherModel.updateItemInDatabase(mLauncher, mInfo);
Adam Cohenac56cff2011-09-28 20:45:37 -0700305
Adam Cohen3371da02011-10-25 21:38:29 -0700306 if (commit) {
307 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Winson Chung82b016c2015-05-08 17:00:10 -0700308 String.format(getContext().getString(R.string.folder_renamed),
309 newTitle.toString()));
Adam Cohen3371da02011-10-25 21:38:29 -0700310 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700311 // In order to clear the focus from the text field, we set the focus on ourself. This
312 // ensures that every time the field is clicked, focus is gained, giving reliable behavior.
313 requestFocus();
314
Adam Cohene601a432011-07-26 21:51:30 -0700315 Selection.setSelection((Spannable) mFolderName.getText(), 0, 0);
Adam Cohen76fc0852011-06-17 13:26:23 -0700316 mIsEditingName = false;
317 }
318
319 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
320 if (actionId == EditorInfo.IME_ACTION_DONE) {
321 dismissEditingName();
322 return true;
323 }
324 return false;
325 }
326
327 public View getEditTextRegion() {
328 return mFolderName;
329 }
330
Adam Cohen0c872ba2011-05-05 10:34:16 -0700331 /**
332 * We need to handle touch events to prevent them from falling through to the workspace below.
333 */
Sunny Goyal70660032015-05-14 00:07:08 -0700334 @SuppressLint("ClickableViewAccessibility")
Adam Cohen0c872ba2011-05-05 10:34:16 -0700335 @Override
336 public boolean onTouchEvent(MotionEvent ev) {
337 return true;
338 }
339
Joe Onorato00acb122009-08-04 16:04:30 -0400340 public void setDragController(DragController dragController) {
341 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800342 }
343
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800344 public void setFolderIcon(FolderIcon icon) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700345 mFolderIcon = icon;
346 }
347
Adam Cohen3371da02011-10-25 21:38:29 -0700348 @Override
349 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
350 // When the folder gets focus, we don't want to announce the list of items.
351 return true;
352 }
353
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800354 /**
355 * @return the FolderInfo object associated with this folder
356 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700357 public FolderInfo getInfo() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800358 return mInfo;
359 }
360
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800361 void bind(FolderInfo info) {
362 mInfo = info;
Adam Cohendf2cc412011-04-27 16:56:57 -0700363 ArrayList<ShortcutInfo> children = info.contents;
Sunny Goyal08f72612015-01-05 13:41:43 -0800364 Collections.sort(children, Utilities.RANK_COMPARATOR);
Sunny Goyal08f72612015-01-05 13:41:43 -0800365
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800366 ArrayList<ShortcutInfo> overflow = mContent.bindItems(children);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700367
Jason Monk4ff73882014-04-24 16:48:07 -0400368 // If our folder has too many items we prune them from the list. This is an issue
Adam Cohenc508b2d2011-06-28 14:41:44 -0700369 // when upgrading from the old Folders implementation which could contain an unlimited
370 // number of items.
371 for (ShortcutInfo item: overflow) {
372 mInfo.remove(item);
373 LauncherModel.deleteItemFromDatabase(mLauncher, item);
374 }
375
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800376 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
377 if (lp == null) {
378 lp = new DragLayer.LayoutParams(0, 0);
379 lp.customPosition = true;
380 setLayoutParams(lp);
381 }
382 centerAboutIcon();
383
Adam Cohen4dbe6d92011-05-18 17:14:15 -0700384 mItemsInvalidated = true;
Adam Cohenac56cff2011-09-28 20:45:37 -0700385 updateTextViewFocus();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700386 mInfo.addListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700387
Adam Cohenafb01ee2011-06-23 15:38:03 -0700388 if (!sDefaultFolderName.contentEquals(mInfo.title)) {
Adam Cohen4ef610f2011-06-21 22:37:36 -0700389 mFolderName.setText(mInfo.title);
390 } else {
391 mFolderName.setText("");
392 }
Winson Chung33231f52013-12-09 16:57:45 -0800393
394 // In case any children didn't come across during loading, clean up the folder accordingly
395 mFolderIcon.post(new Runnable() {
396 public void run() {
397 if (getItemCount() <= 1) {
398 replaceFolderWithFinalItem();
399 }
400 }
401 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700402 }
403
404 /**
405 * Creates a new UserFolder, inflated from R.layout.user_folder.
406 *
407 * @param context The application's context.
408 *
409 * @return A new UserFolder.
410 */
Sunny Goyalc23da842015-05-14 20:44:01 -0700411 @SuppressLint("InflateParams")
Sunny Goyal70660032015-05-14 00:07:08 -0700412 static Folder fromXml(Launcher launcher) {
Sunny Goyalc23da842015-05-14 20:44:01 -0700413 return (Folder) launcher.getLayoutInflater().inflate(R.layout.user_folder, null);
Adam Cohendf2cc412011-04-27 16:56:57 -0700414 }
415
416 /**
417 * This method is intended to make the UserFolder to be visually identical in size and position
418 * to its associated FolderIcon. This allows for a seamless transition into the expanded state.
419 */
420 private void positionAndSizeAsIcon() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700421 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800422 setScaleX(0.8f);
423 setScaleY(0.8f);
424 setAlpha(0f);
Adam Cohendf2cc412011-04-27 16:56:57 -0700425 mState = STATE_SMALL;
426 }
427
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700428 private void prepareReveal() {
429 setScaleX(1f);
430 setScaleY(1f);
431 setAlpha(1f);
432 mState = STATE_SMALL;
433 }
434
Adam Cohendf2cc412011-04-27 16:56:57 -0700435 public void animateOpen() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700436 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen6441de02011-12-14 14:25:32 -0800437
Sunny Goyalb8634152015-04-09 14:17:14 -0700438 mContent.completePendingPageChanges();
Sunny Goyal8167dc22015-04-27 13:44:01 -0700439 if (!mDragInProgress) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700440 // Open on the first page.
441 mContent.snapToPageImmediately(0);
Sunny Goyal48461932015-03-09 17:41:09 -0700442 }
443
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700444 Animator openFolderAnim = null;
445 final Runnable onCompleteRunnable;
Kenny Guyd794a3f2014-09-16 15:17:58 +0100446 if (!Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700447 positionAndSizeAsIcon();
448 centerAboutIcon();
449
450 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1);
451 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
452 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
453 final ObjectAnimator oa =
454 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
455 oa.setDuration(mExpandDuration);
456 openFolderAnim = oa;
457
458 setLayerType(LAYER_TYPE_HARDWARE, null);
459 onCompleteRunnable = new Runnable() {
460 @Override
461 public void run() {
462 setLayerType(LAYER_TYPE_NONE, null);
463 }
464 };
465 } else {
466 prepareReveal();
467 centerAboutIcon();
468
469 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
470 int height = getFolderHeight();
471
472 float transX = - 0.075f * (width / 2 - getPivotX());
473 float transY = - 0.075f * (height / 2 - getPivotY());
474 setTranslationX(transX);
475 setTranslationY(transY);
476 PropertyValuesHolder tx = PropertyValuesHolder.ofFloat("translationX", transX, 0);
477 PropertyValuesHolder ty = PropertyValuesHolder.ofFloat("translationY", transY, 0);
478
Sunny Goyal75deaf32015-05-07 16:13:12 -0700479 Animator drift = LauncherAnimUtils.ofPropertyValuesHolder(this, tx, ty);
480 drift.setDuration(mMaterialExpandDuration);
481 drift.setStartDelay(mMaterialExpandStagger);
482 drift.setInterpolator(new LogDecelerateInterpolator(100, 0));
483
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700484 int rx = (int) Math.max(Math.max(width - getPivotX(), 0), getPivotX());
485 int ry = (int) Math.max(Math.max(height - getPivotY(), 0), getPivotY());
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700486 float radius = (float) Math.hypot(rx, ry);
Sunny Goyal75deaf32015-05-07 16:13:12 -0700487
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700488 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
489 Animator reveal = LauncherAnimUtils.createCircularReveal(this, (int) getPivotX(),
490 (int) getPivotY(), 0, radius);
491 reveal.setDuration(mMaterialExpandDuration);
492 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
493
Sunny Goyalbc753352015-03-05 09:40:44 -0800494 mContentWrapper.setAlpha(0f);
495 Animator iconsAlpha = LauncherAnimUtils.ofFloat(mContentWrapper, "alpha", 0f, 1f);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700496 iconsAlpha.setDuration(mMaterialExpandDuration);
497 iconsAlpha.setStartDelay(mMaterialExpandStagger);
498 iconsAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
499
Sunny Goyal290800b2015-03-05 11:33:33 -0800500 mFooter.setAlpha(0f);
501 Animator textAlpha = LauncherAnimUtils.ofFloat(mFooter, "alpha", 0f, 1f);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700502 textAlpha.setDuration(mMaterialExpandDuration);
503 textAlpha.setStartDelay(mMaterialExpandStagger);
504 textAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
505
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700506
507 anim.play(drift);
508 anim.play(iconsAlpha);
509 anim.play(textAlpha);
510 anim.play(reveal);
511
512 openFolderAnim = anim;
513
Sunny Goyalbc753352015-03-05 09:40:44 -0800514 mContentWrapper.setLayerType(LAYER_TYPE_HARDWARE, null);
Sunny Goyal75deaf32015-05-07 16:13:12 -0700515 mFooter.setLayerType(LAYER_TYPE_HARDWARE, null);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700516 onCompleteRunnable = new Runnable() {
517 @Override
518 public void run() {
Sunny Goyalbc753352015-03-05 09:40:44 -0800519 mContentWrapper.setLayerType(LAYER_TYPE_NONE, null);
Sunny Goyal75deaf32015-05-07 16:13:12 -0700520 mContentWrapper.setLayerType(LAYER_TYPE_NONE, null);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700521 }
522 };
523 }
524 openFolderAnim.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700525 @Override
526 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700527 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Sunny Goyalbc753352015-03-05 09:40:44 -0800528 mContent.getAccessibilityDescription());
Adam Cohendf2cc412011-04-27 16:56:57 -0700529 mState = STATE_ANIMATING;
530 }
531 @Override
532 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700533 mState = STATE_OPEN;
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700534
535 if (onCompleteRunnable != null) {
536 onCompleteRunnable.run();
537 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800538
Sunny Goyalbc753352015-03-05 09:40:44 -0800539 mContent.setFocusOnFirstChild();
Adam Cohendf2cc412011-04-27 16:56:57 -0700540 }
541 });
Sunny Goyalb7e15ad2015-05-07 14:51:31 -0700542
543 // Footer animation
544 if (mContent.getPageCount() > 1 && !mInfo.hasOption(FolderInfo.FLAG_MULTI_PAGE_ANIMATION)) {
545 int footerWidth = mContent.getDesiredWidth()
546 - mFooter.getPaddingLeft() - mFooter.getPaddingRight();
547
548 float textWidth = mFolderName.getPaint().measureText(mFolderName.getText().toString());
549 mFolderName.setTranslationX((footerWidth - textWidth) / 2);
550 mContent.setMarkerScale(0);
551
552 // Do not update the flag if we are in drag mode. The flag will be updated, when we
553 // actually drop the icon.
554 final boolean updateAnimationFlag = !mDragInProgress;
555 openFolderAnim.addListener(new AnimatorListenerAdapter() {
556
557 @Override
558 public void onAnimationEnd(Animator animation) {
559 mFolderName.animate().setDuration(FOOTER_ANIMATION_DURATION).translationX(0);
560 mContent.animateMarkers();
561
562 if (updateAnimationFlag) {
563 mInfo.setOption(FolderInfo.FLAG_MULTI_PAGE_ANIMATION, true, mLauncher);
564 }
565 }
566 });
567 } else {
568 mFolderName.setTranslationX(0);
569 mContent.setMarkerScale(1);
570 }
571
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700572 openFolderAnim.start();
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800573
574 // Make sure the folder picks up the last drag move even if the finger doesn't move.
575 if (mDragController.isDragging()) {
576 mDragController.forceTouchMove();
577 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700578
Sunny Goyalb8634152015-04-09 14:17:14 -0700579 FolderPagedView pages = (FolderPagedView) mContent;
580 pages.verifyVisibleHighResIcons(pages.getNextPage());
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800581 }
582
583 public void beginExternalDrag(ShortcutInfo item) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800584 mCurrentDragInfo = item;
Sunny Goyal5d85c442015-03-10 13:14:47 -0700585 mEmptyCellRank = mContent.allocateRankForNewItem(item);
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800586 mIsExternalDrag = true;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800587 mDragInProgress = true;
Sunny Goyalb8634152015-04-09 14:17:14 -0700588
Sunny Goyalf4066152015-04-15 09:42:19 -0700589 // Since this folder opened by another controller, it might not get onDrop or
590 // onDropComplete. Perform cleanup once drag-n-drop ends.
591 mDragController.addDragListener(this);
592 }
593
594 @Override
595 public void onDragStart(DragSource source, Object info, int dragAction) { }
596
597 @Override
598 public void onDragEnd() {
599 if (mIsExternalDrag && mDragInProgress) {
600 completeDragExit();
601 }
602 mDragController.removeDragListener(this);
Adam Cohendf2cc412011-04-27 16:56:57 -0700603 }
604
Adam Cohen091440a2015-03-18 14:16:05 -0700605 @Thunk void sendCustomAccessibilityEvent(int type, String text) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700606 AccessibilityManager accessibilityManager = (AccessibilityManager)
607 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
608 if (accessibilityManager.isEnabled()) {
Adam Cohen3371da02011-10-25 21:38:29 -0700609 AccessibilityEvent event = AccessibilityEvent.obtain(type);
610 onInitializeAccessibilityEvent(event);
611 event.getText().add(text);
Michael Jurka8b805b12012-04-18 14:23:14 -0700612 accessibilityManager.sendAccessibilityEvent(event);
Adam Cohen3371da02011-10-25 21:38:29 -0700613 }
614 }
615
Adam Cohendf2cc412011-04-27 16:56:57 -0700616 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700617 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800618 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
619 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
620 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
Michael Jurka032e6ba2013-04-22 15:08:42 +0200621 final ObjectAnimator oa =
Michael Jurka2ecf9952012-06-18 12:52:28 -0700622 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohendf2cc412011-04-27 16:56:57 -0700623
Adam Cohen2801caf2011-05-13 20:57:39 -0700624 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700625 @Override
626 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700627 onCloseComplete();
Michael Jurka0121c3e2012-05-31 08:36:04 -0700628 setLayerType(LAYER_TYPE_NONE, null);
Adam Cohen2801caf2011-05-13 20:57:39 -0700629 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700630 }
631 @Override
632 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700633 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700634 getContext().getString(R.string.folder_closed));
Adam Cohendf2cc412011-04-27 16:56:57 -0700635 mState = STATE_ANIMATING;
636 }
637 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700638 oa.setDuration(mExpandDuration);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700639 setLayerType(LAYER_TYPE_HARDWARE, null);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100640 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700641 }
642
Adam Cohencb3382b2011-05-24 14:07:08 -0700643 public boolean acceptDrop(DragObject d) {
644 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700645 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700646 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
647 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
648 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700649 }
650
Adam Cohencb3382b2011-05-24 14:07:08 -0700651 public void onDragEnter(DragObject d) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800652 mPrevTargetRank = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700653 mOnExitAlarm.cancelAlarm();
Sunny Goyalb8634152015-04-09 14:17:14 -0700654 // Get the area offset such that the folder only closes if half the drag icon width
655 // is outside the folder area
656 mScrollAreaOffset = d.dragView.getDragRegionWidth() / 2 - d.xOffset;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700657 }
658
659 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
660 public void onAlarm(Alarm alarm) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800661 mContent.realTimeReorder(mEmptyCellRank, mTargetRank);
662 mEmptyCellRank = mTargetRank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700663 }
664 };
665
Sunny Goyalc46bfef2015-01-05 12:40:08 -0800666 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohen2374abf2013-04-16 14:56:57 -0700667 public boolean isLayoutRtl() {
668 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
669 }
670
Sunny Goyal48461932015-03-09 17:41:09 -0700671 @Override
Adam Cohencb3382b2011-05-24 14:07:08 -0700672 public void onDragOver(DragObject d) {
Sunny Goyal48461932015-03-09 17:41:09 -0700673 onDragOver(d, REORDER_DELAY);
674 }
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700675
Sunny Goyal48461932015-03-09 17:41:09 -0700676 private int getTargetRank(DragObject d, float[] recycle) {
677 recycle = d.getVisualCenter(recycle);
678 return mContent.findNearestArea(
679 (int) recycle[0] - getPaddingLeft(), (int) recycle[1] - getPaddingTop());
680 }
681
Adam Cohen091440a2015-03-18 14:16:05 -0700682 @Thunk void onDragOver(DragObject d, int reorderDelay) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700683 if (mScrollPauseAlarm.alarmPending()) {
Sunny Goyal48461932015-03-09 17:41:09 -0700684 return;
685 }
686 final float[] r = new float[2];
687 mTargetRank = getTargetRank(d, r);
688
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800689 if (mTargetRank != mPrevTargetRank) {
Alan Viverette4cda5b72013-08-28 17:53:41 -0700690 mReorderAlarm.cancelAlarm();
Sunny Goyalc46bfef2015-01-05 12:40:08 -0800691 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
692 mReorderAlarm.setAlarm(REORDER_DELAY);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800693 mPrevTargetRank = mTargetRank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700694 }
Sunny Goyal48461932015-03-09 17:41:09 -0700695
Sunny Goyal48461932015-03-09 17:41:09 -0700696 float x = r[0];
Sunny Goyalb8634152015-04-09 14:17:14 -0700697 int currentPage = mContent.getNextPage();
Sunny Goyal48461932015-03-09 17:41:09 -0700698
Sunny Goyalb8634152015-04-09 14:17:14 -0700699 float cellOverlap = mContent.getCurrentCellLayout().getCellWidth()
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700700 * ICON_OVERSCROLL_WIDTH_FACTOR;
701 boolean isOutsideLeftEdge = x < cellOverlap;
702 boolean isOutsideRightEdge = x > (getWidth() - cellOverlap);
Sunny Goyal48461932015-03-09 17:41:09 -0700703
Sunny Goyal70660032015-05-14 00:07:08 -0700704 if (currentPage > 0 && (mContent.mIsRtl ? isOutsideRightEdge : isOutsideLeftEdge)) {
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700705 showScrollHint(DragController.SCROLL_LEFT, d);
Sunny Goyalb8634152015-04-09 14:17:14 -0700706 } else if (currentPage < (mContent.getPageCount() - 1)
Sunny Goyal70660032015-05-14 00:07:08 -0700707 && (mContent.mIsRtl ? isOutsideLeftEdge : isOutsideRightEdge)) {
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700708 showScrollHint(DragController.SCROLL_RIGHT, d);
Sunny Goyal48461932015-03-09 17:41:09 -0700709 } else {
710 mOnScrollHintAlarm.cancelAlarm();
711 if (mScrollHintDir != DragController.SCROLL_NONE) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700712 mContent.clearScrollHint();
Sunny Goyal48461932015-03-09 17:41:09 -0700713 mScrollHintDir = DragController.SCROLL_NONE;
714 }
715 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700716 }
717
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700718 private void showScrollHint(int direction, DragObject d) {
719 // Show scroll hint on the right
720 if (mScrollHintDir != direction) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700721 mContent.showScrollHint(direction);
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700722 mScrollHintDir = direction;
723 }
724
725 // Set alarm for when the hint is complete
726 if (!mOnScrollHintAlarm.alarmPending() || mCurrentScrollDir != direction) {
727 mCurrentScrollDir = direction;
728 mOnScrollHintAlarm.cancelAlarm();
729 mOnScrollHintAlarm.setOnAlarmListener(new OnScrollHintListener(d));
730 mOnScrollHintAlarm.setAlarm(SCROLL_HINT_DURATION);
731
732 mReorderAlarm.cancelAlarm();
733 mTargetRank = mEmptyCellRank;
734 }
735 }
736
Adam Cohenbfbfd262011-06-13 16:55:12 -0700737 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
738 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700739 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700740 }
741 };
742
Adam Cohen95bb8002011-07-03 23:40:28 -0700743 public void completeDragExit() {
Sunny Goyalf4066152015-04-15 09:42:19 -0700744 if (mInfo.opened) {
745 mLauncher.closeFolder();
746 mRearrangeOnClose = true;
Sunny Goyal31abc292015-05-01 10:42:32 -0700747 } else if (mState == STATE_ANIMATING) {
748 mRearrangeOnClose = true;
Sunny Goyalf4066152015-04-15 09:42:19 -0700749 } else {
750 rearrangeChildren();
Sunny Goyal31abc292015-05-01 10:42:32 -0700751 clearDragInfo();
Sunny Goyalf4066152015-04-15 09:42:19 -0700752 }
Sunny Goyal31abc292015-05-01 10:42:32 -0700753 }
754
755 private void clearDragInfo() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700756 mCurrentDragInfo = null;
757 mCurrentDragView = null;
758 mSuppressOnAdd = false;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800759 mIsExternalDrag = false;
Adam Cohen3e8f8112011-07-02 18:03:00 -0700760 }
761
Adam Cohencb3382b2011-05-24 14:07:08 -0700762 public void onDragExit(DragObject d) {
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700763 // We only close the folder if this is a true drag exit, ie. not because
764 // a drop has occurred above the folder.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700765 if (!d.dragComplete) {
766 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
767 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
768 }
769 mReorderAlarm.cancelAlarm();
Sunny Goyal48461932015-03-09 17:41:09 -0700770
Sunny Goyalb8634152015-04-09 14:17:14 -0700771 mOnScrollHintAlarm.cancelAlarm();
772 mScrollPauseAlarm.cancelAlarm();
773 if (mScrollHintDir != DragController.SCROLL_NONE) {
774 mContent.clearScrollHint();
775 mScrollHintDir = DragController.SCROLL_NONE;
Sunny Goyal48461932015-03-09 17:41:09 -0700776 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700777 }
778
Sunny Goyale9b651e2015-04-24 11:44:51 -0700779 /**
780 * When performing an accessibility drop, onDrop is sent immediately after onDragEnter. So we
781 * need to complete all transient states based on timers.
782 */
783 @Override
784 public void prepareAccessibilityDrop() {
785 if (mReorderAlarm.alarmPending()) {
786 mReorderAlarm.cancelAlarm();
787 mReorderAlarmListener.onAlarm(mReorderAlarm);
788 }
789 }
790
Michael Jurka1e2f4652013-07-08 18:03:46 -0700791 public void onDropCompleted(final View target, final DragObject d,
792 final boolean isFlingToDelete, final boolean success) {
793 if (mDeferDropAfterUninstall) {
Michael Jurkaf3007582013-08-21 14:33:57 +0200794 Log.d(TAG, "Deferred handling drop because waiting for uninstall.");
Michael Jurka1e2f4652013-07-08 18:03:46 -0700795 mDeferredAction = new Runnable() {
796 public void run() {
797 onDropCompleted(target, d, isFlingToDelete, success);
798 mDeferredAction = null;
799 }
800 };
801 return;
802 }
803
804 boolean beingCalledAfterUninstall = mDeferredAction != null;
805 boolean successfulDrop =
806 success && (!beingCalledAfterUninstall || mUninstallSuccessful);
Winson Chung5f8afe62013-08-12 16:19:28 -0700807
Michael Jurka1e2f4652013-07-08 18:03:46 -0700808 if (successfulDrop) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800809 if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon && target != this) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700810 replaceFolderWithFinalItem();
811 }
812 } else {
813 // The drag failed, we need to return the item to the folder
Sunny Goyalb8c663c2015-04-23 11:43:48 -0700814 ShortcutInfo info = (ShortcutInfo) d.dragInfo;
815 View icon = (mCurrentDragView != null && mCurrentDragView.getTag() == info)
816 ? mCurrentDragView : mContent.createNewView(info);
817 ArrayList<View> views = getItemsInReadingOrder();
818 views.add(info.rank, icon);
819 mContent.arrangeChildren(views, views.size());
820 mItemsInvalidated = true;
821
822 mSuppressOnAdd = true;
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700823 mFolderIcon.onDrop(d);
Sunny Goyalb8c663c2015-04-23 11:43:48 -0700824 mSuppressOnAdd = false;
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700825 }
826
827 if (target != this) {
828 if (mOnExitAlarm.alarmPending()) {
829 mOnExitAlarm.cancelAlarm();
Michael Jurka54554252013-08-01 12:52:23 +0200830 if (!successfulDrop) {
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700831 mSuppressFolderDeletion = true;
832 }
Sunny Goyal5d85c442015-03-10 13:14:47 -0700833 mScrollPauseAlarm.cancelAlarm();
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700834 completeDragExit();
835 }
Adam Cohen9c58d822013-12-13 17:18:10 -0800836 }
837
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700838 mDeleteFolderOnDropCompleted = false;
839 mDragInProgress = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700840 mItemAddedBackToSelfViaIcon = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700841 mCurrentDragInfo = null;
842 mCurrentDragView = null;
843 mSuppressOnAdd = false;
Adam Cohen4045eb72011-10-06 11:44:26 -0700844
845 // Reordering may have occured, and we need to save the new item locations. We do this once
846 // at the end to prevent unnecessary database operations.
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700847 updateItemLocationsInDatabaseBatch();
Sunny Goyalb7e15ad2015-05-07 14:51:31 -0700848
849 // Use the item count to check for multi-page as the folder UI may not have
850 // been refreshed yet.
851 if (getItemCount() <= mContent.itemsPerPage()) {
852 // Show the animation, next time something is added to the folder.
853 mInfo.setOption(FolderInfo.FLAG_MULTI_PAGE_ANIMATION, false, mLauncher);
854 }
855
Adam Cohen4045eb72011-10-06 11:44:26 -0700856 }
857
Sunny Goyalfa401a12015-04-10 13:45:42 -0700858 @Override
Michael Jurka1e2f4652013-07-08 18:03:46 -0700859 public void deferCompleteDropAfterUninstallActivity() {
860 mDeferDropAfterUninstall = true;
861 }
862
Sunny Goyalfa401a12015-04-10 13:45:42 -0700863 @Override
Michael Jurka1e2f4652013-07-08 18:03:46 -0700864 public void onUninstallActivityReturned(boolean success) {
865 mDeferDropAfterUninstall = false;
866 mUninstallSuccessful = success;
867 if (mDeferredAction != null) {
868 mDeferredAction.run();
869 }
870 }
871
Winson Chunga48487a2012-03-20 16:19:37 -0700872 @Override
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800873 public float getIntrinsicIconScaleFactor() {
874 return 1f;
875 }
876
877 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800878 public boolean supportsFlingToDelete() {
879 return true;
880 }
881
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000882 @Override
883 public boolean supportsAppInfoDropTarget() {
884 return false;
885 }
886
887 @Override
888 public boolean supportsDeleteDropTarget() {
889 return true;
890 }
891
Sunny Goyalddec7342015-04-29 18:12:37 -0700892 @Override
893 public void onFlingToDelete(DragObject d, PointF vec) {
Winson Chunga48487a2012-03-20 16:19:37 -0700894 // Do nothing
895 }
896
897 @Override
898 public void onFlingToDeleteCompleted() {
899 // Do nothing
900 }
901
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700902 private void updateItemLocationsInDatabaseBatch() {
903 ArrayList<View> list = getItemsInReadingOrder();
904 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
905 for (int i = 0; i < list.size(); i++) {
906 View v = list.get(i);
907 ItemInfo info = (ItemInfo) v.getTag();
Sunny Goyal08f72612015-01-05 13:41:43 -0800908 info.rank = i;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700909 items.add(info);
910 }
911
912 LauncherModel.moveItemsInDatabase(mLauncher, items, mInfo.id, 0);
913 }
914
Adam Cohene25af792013-06-06 23:08:25 -0700915 public void addItemLocationsInDatabase() {
916 ArrayList<View> list = getItemsInReadingOrder();
917 for (int i = 0; i < list.size(); i++) {
918 View v = list.get(i);
919 ItemInfo info = (ItemInfo) v.getTag();
920 LauncherModel.addItemToDatabase(mLauncher, info, mInfo.id, 0,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700921 info.cellX, info.cellY);
Adam Cohene25af792013-06-06 23:08:25 -0700922 }
923 }
924
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700925 public void notifyDrop() {
926 if (mDragInProgress) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700927 mItemAddedBackToSelfViaIcon = true;
Adam Cohen76078c42011-06-09 15:06:52 -0700928 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700929 }
930
931 public boolean isDropEnabled() {
932 return true;
933 }
934
Adam Cohen2801caf2011-05-13 20:57:39 -0700935 public boolean isFull() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800936 return mContent.isFull();
Adam Cohen2801caf2011-05-13 20:57:39 -0700937 }
938
939 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700940 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700941
Winson Chung892c74d2013-08-22 16:15:50 -0700942 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700943 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700944 int height = getFolderHeight();
Adam Cohen2801caf2011-05-13 20:57:39 -0700945
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800946 float scale = parent.getDescendantRectRelativeToSelf(mFolderIcon, sTempRect);
Adam Cohen8e776a62011-06-28 18:10:06 -0700947
Adam Cohen2e6da152015-05-06 11:42:25 -0700948 DeviceProfile grid = mLauncher.getDeviceProfile();
Winson Chungaf40f202013-09-18 18:26:31 -0700949
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800950 int centerX = (int) (sTempRect.left + sTempRect.width() * scale / 2);
951 int centerY = (int) (sTempRect.top + sTempRect.height() * scale / 2);
Adam Cohen2801caf2011-05-13 20:57:39 -0700952 int centeredLeft = centerX - width / 2;
953 int centeredTop = centerY - height / 2;
Winson Chung3057b1c2013-10-10 17:35:35 -0700954 int currentPage = mLauncher.getWorkspace().getNextPage();
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800955 // In case the workspace is scrolling, we need to use the final scroll to compute
956 // the folders bounds.
957 mLauncher.getWorkspace().setFinalScrollForPageChange(currentPage);
Adam Cohen35e7e642011-07-17 14:47:18 -0700958 // We first fetch the currently visible CellLayoutChildren
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800959 CellLayout currentLayout = (CellLayout) mLauncher.getWorkspace().getChildAt(currentPage);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700960 ShortcutAndWidgetContainer boundingLayout = currentLayout.getShortcutsAndWidgets();
Adam Cohen35e7e642011-07-17 14:47:18 -0700961 Rect bounds = new Rect();
962 parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800963 // We reset the workspaces scroll
964 mLauncher.getWorkspace().resetFinalScrollForPageChange(currentPage);
Adam Cohen2801caf2011-05-13 20:57:39 -0700965
Adam Cohen35e7e642011-07-17 14:47:18 -0700966 // We need to bound the folder to the currently visible CellLayoutChildren
967 int left = Math.min(Math.max(bounds.left, centeredLeft),
968 bounds.left + bounds.width() - width);
969 int top = Math.min(Math.max(bounds.top, centeredTop),
970 bounds.top + bounds.height() - height);
Winson Chungaf40f202013-09-18 18:26:31 -0700971 if (grid.isPhone() && (grid.availableWidthPx - width) < grid.iconSizePx) {
972 // Center the folder if it is full (on phones only)
973 left = (grid.availableWidthPx - width) / 2;
974 } else if (width >= bounds.width()) {
975 // If the folder doesn't fit within the bounds, center it about the desired bounds
Adam Cohen35e7e642011-07-17 14:47:18 -0700976 left = bounds.left + (bounds.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700977 }
Adam Cohen35e7e642011-07-17 14:47:18 -0700978 if (height >= bounds.height()) {
979 top = bounds.top + (bounds.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700980 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700981
982 int folderPivotX = width / 2 + (centeredLeft - left);
983 int folderPivotY = height / 2 + (centeredTop - top);
984 setPivotX(folderPivotX);
985 setPivotY(folderPivotY);
Adam Cohen268c4752012-06-06 17:47:33 -0700986 mFolderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700987 (1.0f * folderPivotX / width));
Adam Cohen268c4752012-06-06 17:47:33 -0700988 mFolderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700989 (1.0f * folderPivotY / height));
Adam Cohen3bf84d32012-05-07 20:17:14 -0700990
Adam Cohen662b5982011-12-13 17:45:21 -0800991 lp.width = width;
992 lp.height = height;
993 lp.x = left;
994 lp.y = top;
Adam Cohen2801caf2011-05-13 20:57:39 -0700995 }
996
Adam Cohen268c4752012-06-06 17:47:33 -0700997 float getPivotXForIconAnimation() {
998 return mFolderIconPivotX;
999 }
1000 float getPivotYForIconAnimation() {
1001 return mFolderIconPivotY;
1002 }
1003
Winson Chung892c74d2013-08-22 16:15:50 -07001004 private int getContentAreaHeight() {
Adam Cohen2e6da152015-05-06 11:42:25 -07001005 DeviceProfile grid = mLauncher.getDeviceProfile();
Winson Chung892c74d2013-08-22 16:15:50 -07001006 Rect workspacePadding = grid.getWorkspacePadding(grid.isLandscape ?
1007 CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
1008 int maxContentAreaHeight = grid.availableHeightPx -
Winson Chung892c74d2013-08-22 16:15:50 -07001009 workspacePadding.top - workspacePadding.bottom -
Sunny Goyal290800b2015-03-05 11:33:33 -08001010 mFooterHeight;
Adam Cohen1960ea42013-11-12 11:33:14 +00001011 int height = Math.min(maxContentAreaHeight,
Winson Chung892c74d2013-08-22 16:15:50 -07001012 mContent.getDesiredHeight());
Adam Cohen1960ea42013-11-12 11:33:14 +00001013 return Math.max(height, MIN_CONTENT_DIMEN);
1014 }
1015
1016 private int getContentAreaWidth() {
1017 return Math.max(mContent.getDesiredWidth(), MIN_CONTENT_DIMEN);
Winson Chung892c74d2013-08-22 16:15:50 -07001018 }
1019
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001020 private int getFolderHeight() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001021 return getFolderHeight(getContentAreaHeight());
1022 }
1023
1024 private int getFolderHeight(int contentAreaHeight) {
Sunny Goyal290800b2015-03-05 11:33:33 -08001025 return getPaddingTop() + getPaddingBottom() + contentAreaHeight + mFooterHeight;
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001026 }
Adam Cohen234c4cd2011-07-17 21:03:04 -07001027
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001028 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001029 int contentWidth = getContentAreaWidth();
1030 int contentHeight = getContentAreaHeight();
Adam Cohen1960ea42013-11-12 11:33:14 +00001031
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001032 int contentAreaWidthSpec = MeasureSpec.makeMeasureSpec(contentWidth, MeasureSpec.EXACTLY);
1033 int contentAreaHeightSpec = MeasureSpec.makeMeasureSpec(contentHeight, MeasureSpec.EXACTLY);
1034
1035 mContent.setFixedSize(contentWidth, contentHeight);
1036 mContentWrapper.measure(contentAreaWidthSpec, contentAreaHeightSpec);
Sunny Goyal290800b2015-03-05 11:33:33 -08001037 mFooter.measure(contentAreaWidthSpec,
1038 MeasureSpec.makeMeasureSpec(mFooterHeight, MeasureSpec.EXACTLY));
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001039
1040 int folderWidth = getPaddingLeft() + getPaddingRight() + contentWidth;
1041 int folderHeight = getFolderHeight(contentHeight);
1042 setMeasuredDimension(folderWidth, folderHeight);
Adam Cohen234c4cd2011-07-17 21:03:04 -07001043 }
1044
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001045 /**
1046 * Rearranges the children based on their rank.
1047 */
1048 public void rearrangeChildren() {
1049 rearrangeChildren(-1);
1050 }
Adam Cohen2801caf2011-05-13 20:57:39 -07001051
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001052 /**
1053 * Rearranges the children based on their rank.
1054 * @param itemCount if greater than the total children count, empty spaces are left at the end,
1055 * otherwise it is ignored.
1056 */
1057 public void rearrangeChildren(int itemCount) {
1058 ArrayList<View> views = getItemsInReadingOrder();
1059 mContent.arrangeChildren(views, Math.max(itemCount, views.size()));
Adam Cohen7c693212011-05-18 15:26:57 -07001060 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -07001061 }
1062
Sunny Goyalc4918352015-03-10 18:15:48 -07001063 // TODO remove this once GSA code fix is submitted
1064 public ViewGroup getContent() {
1065 return (ViewGroup) mContent;
1066 }
1067
Adam Cohena9cf38f2011-05-02 15:36:58 -07001068 public int getItemCount() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001069 return mContent.getItemCount();
Adam Cohena9cf38f2011-05-02 15:36:58 -07001070 }
1071
Adam Cohen091440a2015-03-18 14:16:05 -07001072 @Thunk void onCloseComplete() {
Adam Cohen05e0f402011-08-01 12:12:49 -07001073 DragLayer parent = (DragLayer) getParent();
Michael Jurka5649c282012-06-18 10:33:21 -07001074 if (parent != null) {
1075 parent.removeView(this);
1076 }
Adam Cohen4554ee12011-08-03 16:13:21 -07001077 mDragController.removeDropTarget((DropTarget) this);
Adam Cohen05e0f402011-08-01 12:12:49 -07001078 clearFocus();
Adam Cohenac56cff2011-09-28 20:45:37 -07001079 mFolderIcon.requestFocus();
Adam Cohen05e0f402011-08-01 12:12:49 -07001080
Adam Cohen2801caf2011-05-13 20:57:39 -07001081 if (mRearrangeOnClose) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001082 rearrangeChildren();
Adam Cohen2801caf2011-05-13 20:57:39 -07001083 mRearrangeOnClose = false;
1084 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001085 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001086 if (!mDragInProgress && !mSuppressFolderDeletion) {
1087 replaceFolderWithFinalItem();
1088 } else if (mDragInProgress) {
1089 mDeleteFolderOnDropCompleted = true;
1090 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001091 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001092 mSuppressFolderDeletion = false;
Sunny Goyal31abc292015-05-01 10:42:32 -07001093 clearDragInfo();
Adam Cohenafb01ee2011-06-23 15:38:03 -07001094 }
1095
Adam Cohen091440a2015-03-18 14:16:05 -07001096 @Thunk void replaceFolderWithFinalItem() {
Adam Cohenafb01ee2011-06-23 15:38:03 -07001097 // Add the last remaining child to the workspace in place of the folder
Adam Cohenfb91f302012-06-11 15:45:18 -07001098 Runnable onCompleteRunnable = new Runnable() {
1099 @Override
1100 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -07001101 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screenId);
Adam Cohenafb01ee2011-06-23 15:38:03 -07001102
Winson Chung33231f52013-12-09 16:57:45 -08001103 View child = null;
Adam Cohenfb91f302012-06-11 15:45:18 -07001104 // Move the item from the folder to the workspace, in the position of the folder
1105 if (getItemCount() == 1) {
1106 ShortcutInfo finalItem = mInfo.contents.get(0);
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001107 child = mLauncher.createShortcut(cellLayout, finalItem);
Adam Cohenfb91f302012-06-11 15:45:18 -07001108 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -07001109 mInfo.screenId, mInfo.cellX, mInfo.cellY);
Adam Cohenfb91f302012-06-11 15:45:18 -07001110 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001111 if (getItemCount() <= 1) {
1112 // Remove the folder
1113 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
Dan Sandler0eb687f2014-01-10 13:24:55 -05001114 if (cellLayout != null) {
1115 // b/12446428 -- sometimes the cell layout has already gone away?
1116 cellLayout.removeView(mFolderIcon);
1117 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001118 if (mFolderIcon instanceof DropTarget) {
1119 mDragController.removeDropTarget((DropTarget) mFolderIcon);
1120 }
1121 mLauncher.removeFolder(mInfo);
1122 }
Adam Cohenc5e63f32012-07-12 16:16:57 -07001123 // We add the child after removing the folder to prevent both from existing at
Winson Chung0e6a7132013-08-23 12:55:10 -07001124 // the same time in the CellLayout. We need to add the new item with addInScreenFromBind()
1125 // to ensure that hotseat items are placed correctly.
Adam Cohenc5e63f32012-07-12 16:16:57 -07001126 if (child != null) {
Winson Chung0e6a7132013-08-23 12:55:10 -07001127 mLauncher.getWorkspace().addInScreenFromBind(child, mInfo.container, mInfo.screenId,
Adam Cohenc5e63f32012-07-12 16:16:57 -07001128 mInfo.cellX, mInfo.cellY, mInfo.spanX, mInfo.spanY);
1129 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001130 }
1131 };
Sunny Goyalbc753352015-03-05 09:40:44 -08001132 View finalChild = mContent.getLastItem();
Adam Cohenfb91f302012-06-11 15:45:18 -07001133 if (finalChild != null) {
1134 mFolderIcon.performDestroyAnimation(finalChild, onCompleteRunnable);
Winson Chung33231f52013-12-09 16:57:45 -08001135 } else {
1136 onCompleteRunnable.run();
Adam Cohenafb01ee2011-06-23 15:38:03 -07001137 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001138 mDestroyed = true;
1139 }
1140
1141 boolean isDestroyed() {
1142 return mDestroyed;
Adam Cohen2801caf2011-05-13 20:57:39 -07001143 }
1144
Adam Cohenac56cff2011-09-28 20:45:37 -07001145 // This method keeps track of the last item in the folder for the purposes
1146 // of keyboard focus
Sunny Goyal290800b2015-03-05 11:33:33 -08001147 public void updateTextViewFocus() {
Sunny Goyalbc753352015-03-05 09:40:44 -08001148 View lastChild = mContent.getLastItem();
Adam Cohenac56cff2011-09-28 20:45:37 -07001149 if (lastChild != null) {
1150 mFolderName.setNextFocusDownId(lastChild.getId());
1151 mFolderName.setNextFocusRightId(lastChild.getId());
1152 mFolderName.setNextFocusLeftId(lastChild.getId());
1153 mFolderName.setNextFocusUpId(lastChild.getId());
1154 }
1155 }
1156
Adam Cohenbfbfd262011-06-13 16:55:12 -07001157 public void onDrop(DragObject d) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001158 Runnable cleanUpRunnable = null;
1159
Adam Cohen689ff162014-05-08 17:27:56 -07001160 // If we are coming from All Apps space, we defer removing the extra empty screen
1161 // until the folder closes
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001162 if (d.dragSource != mLauncher.getWorkspace() && !(d.dragSource instanceof Folder)) {
1163 cleanUpRunnable = new Runnable() {
1164 @Override
1165 public void run() {
Adam Cohen689ff162014-05-08 17:27:56 -07001166 mLauncher.exitSpringLoadedDragModeDelayed(true,
1167 Launcher.EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
1168 null);
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001169 }
1170 };
Adam Cohenbfbfd262011-06-13 16:55:12 -07001171 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001172
Sunny Goyalb8634152015-04-09 14:17:14 -07001173 // If the icon was dropped while the page was being scrolled, we need to compute
1174 // the target location again such that the icon is placed of the final page.
1175 if (!mContent.rankOnCurrentPage(mEmptyCellRank)) {
1176 // Reorder again.
1177 mTargetRank = getTargetRank(d, null);
Sunny Goyal48461932015-03-09 17:41:09 -07001178
Sunny Goyalb8634152015-04-09 14:17:14 -07001179 // Rearrange items immediately.
1180 mReorderAlarmListener.onAlarm(mReorderAlarm);
Sunny Goyal48461932015-03-09 17:41:09 -07001181
Sunny Goyalb8634152015-04-09 14:17:14 -07001182 mOnScrollHintAlarm.cancelAlarm();
1183 mScrollPauseAlarm.cancelAlarm();
Sunny Goyal48461932015-03-09 17:41:09 -07001184 }
Sunny Goyalb8634152015-04-09 14:17:14 -07001185 mContent.completePendingPageChanges();
Sunny Goyal48461932015-03-09 17:41:09 -07001186
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001187 View currentDragView;
1188 ShortcutInfo si = mCurrentDragInfo;
1189 if (mIsExternalDrag) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001190 currentDragView = mContent.createAndAddViewForRank(si, mEmptyCellRank);
Sunny Goyal95abbb32014-08-04 10:53:22 -07001191 // Actually move the item in the database if it was an external drag. Call this
1192 // before creating the view, so that ShortcutInfo is updated appropriately.
1193 LauncherModel.addOrMoveItemInDatabase(
1194 mLauncher, si, mInfo.id, 0, si.cellX, si.cellY);
1195
1196 // We only need to update the locations if it doesn't get handled in #onDropCompleted.
1197 if (d.dragSource != this) {
1198 updateItemLocationsInDatabaseBatch();
1199 }
1200 mIsExternalDrag = false;
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001201 } else {
1202 currentDragView = mCurrentDragView;
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001203 mContent.addViewForRank(currentDragView, si, mEmptyCellRank);
Adam Cohenbfbfd262011-06-13 16:55:12 -07001204 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001205
1206 if (d.dragView.hasDrawn()) {
1207
1208 // Temporarily reset the scale such that the animation target gets calculated correctly.
1209 float scaleX = getScaleX();
1210 float scaleY = getScaleY();
1211 setScaleX(1.0f);
1212 setScaleY(1.0f);
1213 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, currentDragView,
1214 cleanUpRunnable, null);
1215 setScaleX(scaleX);
1216 setScaleY(scaleY);
1217 } else {
1218 d.deferDragViewCleanupPostAnimation = false;
1219 currentDragView.setVisibility(VISIBLE);
1220 }
1221 mItemsInvalidated = true;
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001222 rearrangeChildren();
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001223
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001224 // Temporarily suppress the listener, as we did all the work already here.
1225 mSuppressOnAdd = true;
1226 mInfo.add(si);
1227 mSuppressOnAdd = false;
Sunny Goyal4b020172014-08-28 14:51:14 -07001228 // Clear the drag info, as it is no longer being dragged.
1229 mCurrentDragInfo = null;
Sunny Goyalf4066152015-04-15 09:42:19 -07001230 mDragInProgress = false;
Sunny Goyalb7e15ad2015-05-07 14:51:31 -07001231
1232 if (mContent.getPageCount() > 1) {
1233 // The animation has already been shown while opening the folder.
1234 mInfo.setOption(FolderInfo.FLAG_MULTI_PAGE_ANIMATION, true, mLauncher);
1235 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001236 }
1237
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001238 // This is used so the item doesn't immediately appear in the folder when added. In one case
1239 // we need to create the illusion that the item isn't added back to the folder yet, to
1240 // to correspond to the animation of the icon back into the folder. This is
1241 public void hideItem(ShortcutInfo info) {
1242 View v = getViewForInfo(info);
1243 v.setVisibility(INVISIBLE);
1244 }
1245 public void showItem(ShortcutInfo info) {
1246 View v = getViewForInfo(info);
1247 v.setVisibility(VISIBLE);
1248 }
1249
Sunny Goyalb7e15ad2015-05-07 14:51:31 -07001250 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -07001251 public void onAdd(ShortcutInfo item) {
Adam Cohen05e0f402011-08-01 12:12:49 -07001252 // If the item was dropped onto this open folder, we have done the work associated
1253 // with adding the item to the folder, as indicated by mSuppressOnAdd being set
Adam Cohenbfbfd262011-06-13 16:55:12 -07001254 if (mSuppressOnAdd) return;
Sunny Goyal5d85c442015-03-10 13:14:47 -07001255 mContent.createAndAddViewForRank(item, mContent.allocateRankForNewItem(item));
Sunny Goyal2e688a82015-03-18 10:23:39 -07001256 mItemsInvalidated = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001257 LauncherModel.addOrMoveItemInDatabase(
1258 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
1259 }
1260
Adam Cohena9cf38f2011-05-02 15:36:58 -07001261 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -07001262 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001263 // If this item is being dragged from this open folder, we have already handled
1264 // the work associated with removing the item, so we don't have to do anything here.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001265 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001266 View v = getViewForInfo(item);
Sunny Goyal290800b2015-03-05 11:33:33 -08001267 mContent.removeItem(v);
Adam Cohen2801caf2011-05-13 20:57:39 -07001268 if (mState == STATE_ANIMATING) {
1269 mRearrangeOnClose = true;
1270 } else {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001271 rearrangeChildren();
Adam Cohen2801caf2011-05-13 20:57:39 -07001272 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001273 if (getItemCount() <= 1) {
1274 replaceFolderWithFinalItem();
1275 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001276 }
Adam Cohen7c693212011-05-18 15:26:57 -07001277
Sunny Goyalbc753352015-03-05 09:40:44 -08001278 private View getViewForInfo(final ShortcutInfo item) {
1279 return mContent.iterateOverItems(new ItemOperator() {
1280
1281 @Override
1282 public boolean evaluate(ItemInfo info, View view, View parent) {
1283 return info == item;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001284 }
Sunny Goyalbc753352015-03-05 09:40:44 -08001285 });
Adam Cohendf1e4e82011-06-24 15:57:39 -07001286 }
1287
Adam Cohen76078c42011-06-09 15:06:52 -07001288 public void onItemsChanged() {
Adam Cohenac56cff2011-09-28 20:45:37 -07001289 updateTextViewFocus();
Adam Cohen76078c42011-06-09 15:06:52 -07001290 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001291
Adam Cohen76fc0852011-06-17 13:26:23 -07001292 public void onTitleChanged(CharSequence title) {
1293 }
Adam Cohen76078c42011-06-09 15:06:52 -07001294
Adam Cohen7c693212011-05-18 15:26:57 -07001295 public ArrayList<View> getItemsInReadingOrder() {
1296 if (mItemsInvalidated) {
1297 mItemsInReadingOrder.clear();
Sunny Goyalbc753352015-03-05 09:40:44 -08001298 mContent.iterateOverItems(new ItemOperator() {
1299
1300 @Override
1301 public boolean evaluate(ItemInfo info, View view, View parent) {
1302 mItemsInReadingOrder.add(view);
1303 return false;
Adam Cohen7c693212011-05-18 15:26:57 -07001304 }
Sunny Goyalbc753352015-03-05 09:40:44 -08001305 });
Adam Cohen7c693212011-05-18 15:26:57 -07001306 mItemsInvalidated = false;
1307 }
1308 return mItemsInReadingOrder;
1309 }
Adam Cohen8dfcba42011-07-07 16:38:18 -07001310
1311 public void getLocationInDragLayer(int[] loc) {
1312 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
1313 }
Adam Cohenea0818d2011-09-30 20:06:58 -07001314
1315 public void onFocusChange(View v, boolean hasFocus) {
1316 if (v == mFolderName && hasFocus) {
1317 startEditingFolderName();
1318 }
1319 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001320
1321 @Override
1322 public void getHitRectRelativeToDragLayer(Rect outRect) {
1323 getHitRect(outRect);
Sunny Goyal48461932015-03-09 17:41:09 -07001324 outRect.left -= mScrollAreaOffset;
1325 outRect.right += mScrollAreaOffset;
1326 }
1327
1328 private class OnScrollHintListener implements OnAlarmListener {
1329
1330 private final DragObject mDragObject;
1331
1332 OnScrollHintListener(DragObject object) {
1333 mDragObject = object;
1334 }
1335
1336 /**
1337 * Scroll hint has been shown long enough. Now scroll to appropriate page.
1338 */
1339 @Override
1340 public void onAlarm(Alarm alarm) {
1341 if (mCurrentScrollDir == DragController.SCROLL_LEFT) {
Sunny Goyalb8634152015-04-09 14:17:14 -07001342 mContent.scrollLeft();
Sunny Goyal48461932015-03-09 17:41:09 -07001343 mScrollHintDir = DragController.SCROLL_NONE;
1344 } else if (mCurrentScrollDir == DragController.SCROLL_RIGHT) {
Sunny Goyalb8634152015-04-09 14:17:14 -07001345 mContent.scrollRight();
Sunny Goyal48461932015-03-09 17:41:09 -07001346 mScrollHintDir = DragController.SCROLL_NONE;
1347 } else {
1348 // This should not happen
1349 return;
1350 }
1351 mCurrentScrollDir = DragController.SCROLL_NONE;
1352
1353 // Pause drag event until the scrolling is finished
1354 mScrollPauseAlarm.setOnAlarmListener(new OnScrollFinishedListener(mDragObject));
1355 mScrollPauseAlarm.setAlarm(DragController.RESCROLL_DELAY);
1356 }
1357 }
1358
1359 private class OnScrollFinishedListener implements OnAlarmListener {
1360
1361 private final DragObject mDragObject;
1362
1363 OnScrollFinishedListener(DragObject object) {
1364 mDragObject = object;
1365 }
1366
1367 /**
1368 * Page scroll is complete.
1369 */
1370 @Override
1371 public void onAlarm(Alarm alarm) {
1372 // Reorder immediately on page change.
1373 onDragOver(mDragObject, 1);
1374 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001375 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001376}