blob: fcfb717ab6422bf5a6b7fdc1caed5de2c8d9770f [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 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;
Sunny Goyalc1cd23b2015-06-01 17:15:34 -070047import android.view.animation.AnimationUtils;
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 Goyala07c2f52015-06-01 11:00:38 -070093 private static final int FOLDER_NAME_ANIMATION_DURATION = 633;
Sunny Goyalb7e15ad2015-05-07 14:51:31 -070094
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 anim.play(drift);
507 anim.play(iconsAlpha);
508 anim.play(textAlpha);
509 anim.play(reveal);
510
511 openFolderAnim = anim;
512
Sunny Goyalbc753352015-03-05 09:40:44 -0800513 mContentWrapper.setLayerType(LAYER_TYPE_HARDWARE, null);
Sunny Goyal75deaf32015-05-07 16:13:12 -0700514 mFooter.setLayerType(LAYER_TYPE_HARDWARE, null);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700515 onCompleteRunnable = new Runnable() {
516 @Override
517 public void run() {
Sunny Goyalbc753352015-03-05 09:40:44 -0800518 mContentWrapper.setLayerType(LAYER_TYPE_NONE, null);
Sunny Goyal75deaf32015-05-07 16:13:12 -0700519 mContentWrapper.setLayerType(LAYER_TYPE_NONE, null);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700520 }
521 };
522 }
523 openFolderAnim.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700524 @Override
525 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700526 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Sunny Goyalbc753352015-03-05 09:40:44 -0800527 mContent.getAccessibilityDescription());
Adam Cohendf2cc412011-04-27 16:56:57 -0700528 mState = STATE_ANIMATING;
529 }
530 @Override
531 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700532 mState = STATE_OPEN;
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700533
534 if (onCompleteRunnable != null) {
535 onCompleteRunnable.run();
536 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800537
Sunny Goyalbc753352015-03-05 09:40:44 -0800538 mContent.setFocusOnFirstChild();
Adam Cohendf2cc412011-04-27 16:56:57 -0700539 }
540 });
Sunny Goyalb7e15ad2015-05-07 14:51:31 -0700541
542 // Footer animation
543 if (mContent.getPageCount() > 1 && !mInfo.hasOption(FolderInfo.FLAG_MULTI_PAGE_ANIMATION)) {
544 int footerWidth = mContent.getDesiredWidth()
545 - mFooter.getPaddingLeft() - mFooter.getPaddingRight();
546
547 float textWidth = mFolderName.getPaint().measureText(mFolderName.getText().toString());
Sunny Goyala07c2f52015-06-01 11:00:38 -0700548 float translation = (footerWidth - textWidth) / 2;
549 mFolderName.setTranslationX(mContent.mIsRtl ? -translation : translation);
Sunny Goyalb7e15ad2015-05-07 14:51:31 -0700550 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) {
Sunny Goyala07c2f52015-06-01 11:00:38 -0700559 mFolderName.animate().setDuration(FOLDER_NAME_ANIMATION_DURATION)
560 .translationX(0)
Sunny Goyalc1cd23b2015-06-01 17:15:34 -0700561 .setInterpolator(Utilities.isLmpOrAbove() ?
562 AnimationUtils.loadInterpolator(mLauncher,
563 android.R.interpolator.fast_out_slow_in)
564 : new LogDecelerateInterpolator(100, 0));
Sunny Goyalb7e15ad2015-05-07 14:51:31 -0700565 mContent.animateMarkers();
566
567 if (updateAnimationFlag) {
568 mInfo.setOption(FolderInfo.FLAG_MULTI_PAGE_ANIMATION, true, mLauncher);
569 }
570 }
571 });
572 } else {
573 mFolderName.setTranslationX(0);
574 mContent.setMarkerScale(1);
575 }
576
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700577 openFolderAnim.start();
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800578
579 // Make sure the folder picks up the last drag move even if the finger doesn't move.
580 if (mDragController.isDragging()) {
581 mDragController.forceTouchMove();
582 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700583
Sunny Goyalb8634152015-04-09 14:17:14 -0700584 FolderPagedView pages = (FolderPagedView) mContent;
585 pages.verifyVisibleHighResIcons(pages.getNextPage());
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800586 }
587
588 public void beginExternalDrag(ShortcutInfo item) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800589 mCurrentDragInfo = item;
Sunny Goyal5d85c442015-03-10 13:14:47 -0700590 mEmptyCellRank = mContent.allocateRankForNewItem(item);
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800591 mIsExternalDrag = true;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800592 mDragInProgress = true;
Sunny Goyalb8634152015-04-09 14:17:14 -0700593
Sunny Goyalf4066152015-04-15 09:42:19 -0700594 // Since this folder opened by another controller, it might not get onDrop or
595 // onDropComplete. Perform cleanup once drag-n-drop ends.
596 mDragController.addDragListener(this);
597 }
598
599 @Override
600 public void onDragStart(DragSource source, Object info, int dragAction) { }
601
602 @Override
603 public void onDragEnd() {
604 if (mIsExternalDrag && mDragInProgress) {
605 completeDragExit();
606 }
607 mDragController.removeDragListener(this);
Adam Cohendf2cc412011-04-27 16:56:57 -0700608 }
609
Adam Cohen091440a2015-03-18 14:16:05 -0700610 @Thunk void sendCustomAccessibilityEvent(int type, String text) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700611 AccessibilityManager accessibilityManager = (AccessibilityManager)
612 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
613 if (accessibilityManager.isEnabled()) {
Adam Cohen3371da02011-10-25 21:38:29 -0700614 AccessibilityEvent event = AccessibilityEvent.obtain(type);
615 onInitializeAccessibilityEvent(event);
616 event.getText().add(text);
Michael Jurka8b805b12012-04-18 14:23:14 -0700617 accessibilityManager.sendAccessibilityEvent(event);
Adam Cohen3371da02011-10-25 21:38:29 -0700618 }
619 }
620
Adam Cohendf2cc412011-04-27 16:56:57 -0700621 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700622 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800623 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
624 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
625 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
Michael Jurka032e6ba2013-04-22 15:08:42 +0200626 final ObjectAnimator oa =
Michael Jurka2ecf9952012-06-18 12:52:28 -0700627 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohendf2cc412011-04-27 16:56:57 -0700628
Adam Cohen2801caf2011-05-13 20:57:39 -0700629 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700630 @Override
631 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700632 onCloseComplete();
Michael Jurka0121c3e2012-05-31 08:36:04 -0700633 setLayerType(LAYER_TYPE_NONE, null);
Adam Cohen2801caf2011-05-13 20:57:39 -0700634 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700635 }
636 @Override
637 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700638 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700639 getContext().getString(R.string.folder_closed));
Adam Cohendf2cc412011-04-27 16:56:57 -0700640 mState = STATE_ANIMATING;
641 }
642 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700643 oa.setDuration(mExpandDuration);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700644 setLayerType(LAYER_TYPE_HARDWARE, null);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100645 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700646 }
647
Adam Cohencb3382b2011-05-24 14:07:08 -0700648 public boolean acceptDrop(DragObject d) {
649 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700650 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700651 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
652 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
653 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700654 }
655
Adam Cohencb3382b2011-05-24 14:07:08 -0700656 public void onDragEnter(DragObject d) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800657 mPrevTargetRank = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700658 mOnExitAlarm.cancelAlarm();
Sunny Goyalb8634152015-04-09 14:17:14 -0700659 // Get the area offset such that the folder only closes if half the drag icon width
660 // is outside the folder area
661 mScrollAreaOffset = d.dragView.getDragRegionWidth() / 2 - d.xOffset;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700662 }
663
664 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
665 public void onAlarm(Alarm alarm) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800666 mContent.realTimeReorder(mEmptyCellRank, mTargetRank);
667 mEmptyCellRank = mTargetRank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700668 }
669 };
670
Sunny Goyalc46bfef2015-01-05 12:40:08 -0800671 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohen2374abf2013-04-16 14:56:57 -0700672 public boolean isLayoutRtl() {
673 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
674 }
675
Sunny Goyal48461932015-03-09 17:41:09 -0700676 @Override
Adam Cohencb3382b2011-05-24 14:07:08 -0700677 public void onDragOver(DragObject d) {
Sunny Goyal48461932015-03-09 17:41:09 -0700678 onDragOver(d, REORDER_DELAY);
679 }
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700680
Sunny Goyal48461932015-03-09 17:41:09 -0700681 private int getTargetRank(DragObject d, float[] recycle) {
682 recycle = d.getVisualCenter(recycle);
683 return mContent.findNearestArea(
684 (int) recycle[0] - getPaddingLeft(), (int) recycle[1] - getPaddingTop());
685 }
686
Adam Cohen091440a2015-03-18 14:16:05 -0700687 @Thunk void onDragOver(DragObject d, int reorderDelay) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700688 if (mScrollPauseAlarm.alarmPending()) {
Sunny Goyal48461932015-03-09 17:41:09 -0700689 return;
690 }
691 final float[] r = new float[2];
692 mTargetRank = getTargetRank(d, r);
693
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800694 if (mTargetRank != mPrevTargetRank) {
Alan Viverette4cda5b72013-08-28 17:53:41 -0700695 mReorderAlarm.cancelAlarm();
Sunny Goyalc46bfef2015-01-05 12:40:08 -0800696 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
697 mReorderAlarm.setAlarm(REORDER_DELAY);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800698 mPrevTargetRank = mTargetRank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700699 }
Sunny Goyal48461932015-03-09 17:41:09 -0700700
Sunny Goyal48461932015-03-09 17:41:09 -0700701 float x = r[0];
Sunny Goyalb8634152015-04-09 14:17:14 -0700702 int currentPage = mContent.getNextPage();
Sunny Goyal48461932015-03-09 17:41:09 -0700703
Sunny Goyalb8634152015-04-09 14:17:14 -0700704 float cellOverlap = mContent.getCurrentCellLayout().getCellWidth()
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700705 * ICON_OVERSCROLL_WIDTH_FACTOR;
706 boolean isOutsideLeftEdge = x < cellOverlap;
707 boolean isOutsideRightEdge = x > (getWidth() - cellOverlap);
Sunny Goyal48461932015-03-09 17:41:09 -0700708
Sunny Goyal70660032015-05-14 00:07:08 -0700709 if (currentPage > 0 && (mContent.mIsRtl ? isOutsideRightEdge : isOutsideLeftEdge)) {
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700710 showScrollHint(DragController.SCROLL_LEFT, d);
Sunny Goyalb8634152015-04-09 14:17:14 -0700711 } else if (currentPage < (mContent.getPageCount() - 1)
Sunny Goyal70660032015-05-14 00:07:08 -0700712 && (mContent.mIsRtl ? isOutsideLeftEdge : isOutsideRightEdge)) {
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700713 showScrollHint(DragController.SCROLL_RIGHT, d);
Sunny Goyal48461932015-03-09 17:41:09 -0700714 } else {
715 mOnScrollHintAlarm.cancelAlarm();
716 if (mScrollHintDir != DragController.SCROLL_NONE) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700717 mContent.clearScrollHint();
Sunny Goyal48461932015-03-09 17:41:09 -0700718 mScrollHintDir = DragController.SCROLL_NONE;
719 }
720 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700721 }
722
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700723 private void showScrollHint(int direction, DragObject d) {
724 // Show scroll hint on the right
725 if (mScrollHintDir != direction) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700726 mContent.showScrollHint(direction);
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700727 mScrollHintDir = direction;
728 }
729
730 // Set alarm for when the hint is complete
731 if (!mOnScrollHintAlarm.alarmPending() || mCurrentScrollDir != direction) {
732 mCurrentScrollDir = direction;
733 mOnScrollHintAlarm.cancelAlarm();
734 mOnScrollHintAlarm.setOnAlarmListener(new OnScrollHintListener(d));
735 mOnScrollHintAlarm.setAlarm(SCROLL_HINT_DURATION);
736
737 mReorderAlarm.cancelAlarm();
738 mTargetRank = mEmptyCellRank;
739 }
740 }
741
Adam Cohenbfbfd262011-06-13 16:55:12 -0700742 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
743 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700744 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700745 }
746 };
747
Adam Cohen95bb8002011-07-03 23:40:28 -0700748 public void completeDragExit() {
Sunny Goyalf4066152015-04-15 09:42:19 -0700749 if (mInfo.opened) {
750 mLauncher.closeFolder();
751 mRearrangeOnClose = true;
Sunny Goyal31abc292015-05-01 10:42:32 -0700752 } else if (mState == STATE_ANIMATING) {
753 mRearrangeOnClose = true;
Sunny Goyalf4066152015-04-15 09:42:19 -0700754 } else {
755 rearrangeChildren();
Sunny Goyal31abc292015-05-01 10:42:32 -0700756 clearDragInfo();
Sunny Goyalf4066152015-04-15 09:42:19 -0700757 }
Sunny Goyal31abc292015-05-01 10:42:32 -0700758 }
759
760 private void clearDragInfo() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700761 mCurrentDragInfo = null;
762 mCurrentDragView = null;
763 mSuppressOnAdd = false;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800764 mIsExternalDrag = false;
Adam Cohen3e8f8112011-07-02 18:03:00 -0700765 }
766
Adam Cohencb3382b2011-05-24 14:07:08 -0700767 public void onDragExit(DragObject d) {
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700768 // We only close the folder if this is a true drag exit, ie. not because
769 // a drop has occurred above the folder.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700770 if (!d.dragComplete) {
771 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
772 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
773 }
774 mReorderAlarm.cancelAlarm();
Sunny Goyal48461932015-03-09 17:41:09 -0700775
Sunny Goyalb8634152015-04-09 14:17:14 -0700776 mOnScrollHintAlarm.cancelAlarm();
777 mScrollPauseAlarm.cancelAlarm();
778 if (mScrollHintDir != DragController.SCROLL_NONE) {
779 mContent.clearScrollHint();
780 mScrollHintDir = DragController.SCROLL_NONE;
Sunny Goyal48461932015-03-09 17:41:09 -0700781 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700782 }
783
Sunny Goyale9b651e2015-04-24 11:44:51 -0700784 /**
785 * When performing an accessibility drop, onDrop is sent immediately after onDragEnter. So we
786 * need to complete all transient states based on timers.
787 */
788 @Override
789 public void prepareAccessibilityDrop() {
790 if (mReorderAlarm.alarmPending()) {
791 mReorderAlarm.cancelAlarm();
792 mReorderAlarmListener.onAlarm(mReorderAlarm);
793 }
794 }
795
Michael Jurka1e2f4652013-07-08 18:03:46 -0700796 public void onDropCompleted(final View target, final DragObject d,
797 final boolean isFlingToDelete, final boolean success) {
798 if (mDeferDropAfterUninstall) {
Michael Jurkaf3007582013-08-21 14:33:57 +0200799 Log.d(TAG, "Deferred handling drop because waiting for uninstall.");
Michael Jurka1e2f4652013-07-08 18:03:46 -0700800 mDeferredAction = new Runnable() {
801 public void run() {
802 onDropCompleted(target, d, isFlingToDelete, success);
803 mDeferredAction = null;
804 }
805 };
806 return;
807 }
808
809 boolean beingCalledAfterUninstall = mDeferredAction != null;
810 boolean successfulDrop =
811 success && (!beingCalledAfterUninstall || mUninstallSuccessful);
Winson Chung5f8afe62013-08-12 16:19:28 -0700812
Michael Jurka1e2f4652013-07-08 18:03:46 -0700813 if (successfulDrop) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800814 if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon && target != this) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700815 replaceFolderWithFinalItem();
816 }
817 } else {
818 // The drag failed, we need to return the item to the folder
Sunny Goyalb8c663c2015-04-23 11:43:48 -0700819 ShortcutInfo info = (ShortcutInfo) d.dragInfo;
820 View icon = (mCurrentDragView != null && mCurrentDragView.getTag() == info)
821 ? mCurrentDragView : mContent.createNewView(info);
822 ArrayList<View> views = getItemsInReadingOrder();
823 views.add(info.rank, icon);
824 mContent.arrangeChildren(views, views.size());
825 mItemsInvalidated = true;
826
827 mSuppressOnAdd = true;
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700828 mFolderIcon.onDrop(d);
Sunny Goyalb8c663c2015-04-23 11:43:48 -0700829 mSuppressOnAdd = false;
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700830 }
831
832 if (target != this) {
833 if (mOnExitAlarm.alarmPending()) {
834 mOnExitAlarm.cancelAlarm();
Michael Jurka54554252013-08-01 12:52:23 +0200835 if (!successfulDrop) {
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700836 mSuppressFolderDeletion = true;
837 }
Sunny Goyal5d85c442015-03-10 13:14:47 -0700838 mScrollPauseAlarm.cancelAlarm();
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700839 completeDragExit();
840 }
Adam Cohen9c58d822013-12-13 17:18:10 -0800841 }
842
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700843 mDeleteFolderOnDropCompleted = false;
844 mDragInProgress = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700845 mItemAddedBackToSelfViaIcon = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700846 mCurrentDragInfo = null;
847 mCurrentDragView = null;
848 mSuppressOnAdd = false;
Adam Cohen4045eb72011-10-06 11:44:26 -0700849
850 // Reordering may have occured, and we need to save the new item locations. We do this once
851 // at the end to prevent unnecessary database operations.
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700852 updateItemLocationsInDatabaseBatch();
Sunny Goyalb7e15ad2015-05-07 14:51:31 -0700853
854 // Use the item count to check for multi-page as the folder UI may not have
855 // been refreshed yet.
856 if (getItemCount() <= mContent.itemsPerPage()) {
857 // Show the animation, next time something is added to the folder.
858 mInfo.setOption(FolderInfo.FLAG_MULTI_PAGE_ANIMATION, false, mLauncher);
859 }
860
Adam Cohen4045eb72011-10-06 11:44:26 -0700861 }
862
Sunny Goyalfa401a12015-04-10 13:45:42 -0700863 @Override
Michael Jurka1e2f4652013-07-08 18:03:46 -0700864 public void deferCompleteDropAfterUninstallActivity() {
865 mDeferDropAfterUninstall = true;
866 }
867
Sunny Goyalfa401a12015-04-10 13:45:42 -0700868 @Override
Michael Jurka1e2f4652013-07-08 18:03:46 -0700869 public void onUninstallActivityReturned(boolean success) {
870 mDeferDropAfterUninstall = false;
871 mUninstallSuccessful = success;
872 if (mDeferredAction != null) {
873 mDeferredAction.run();
874 }
875 }
876
Winson Chunga48487a2012-03-20 16:19:37 -0700877 @Override
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800878 public float getIntrinsicIconScaleFactor() {
879 return 1f;
880 }
881
882 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800883 public boolean supportsFlingToDelete() {
884 return true;
885 }
886
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000887 @Override
888 public boolean supportsAppInfoDropTarget() {
889 return false;
890 }
891
892 @Override
893 public boolean supportsDeleteDropTarget() {
894 return true;
895 }
896
Sunny Goyalddec7342015-04-29 18:12:37 -0700897 @Override
898 public void onFlingToDelete(DragObject d, PointF vec) {
Winson Chunga48487a2012-03-20 16:19:37 -0700899 // Do nothing
900 }
901
902 @Override
903 public void onFlingToDeleteCompleted() {
904 // Do nothing
905 }
906
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700907 private void updateItemLocationsInDatabaseBatch() {
908 ArrayList<View> list = getItemsInReadingOrder();
909 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
910 for (int i = 0; i < list.size(); i++) {
911 View v = list.get(i);
912 ItemInfo info = (ItemInfo) v.getTag();
Sunny Goyal08f72612015-01-05 13:41:43 -0800913 info.rank = i;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700914 items.add(info);
915 }
916
917 LauncherModel.moveItemsInDatabase(mLauncher, items, mInfo.id, 0);
918 }
919
Adam Cohene25af792013-06-06 23:08:25 -0700920 public void addItemLocationsInDatabase() {
921 ArrayList<View> list = getItemsInReadingOrder();
922 for (int i = 0; i < list.size(); i++) {
923 View v = list.get(i);
924 ItemInfo info = (ItemInfo) v.getTag();
925 LauncherModel.addItemToDatabase(mLauncher, info, mInfo.id, 0,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700926 info.cellX, info.cellY);
Adam Cohene25af792013-06-06 23:08:25 -0700927 }
928 }
929
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700930 public void notifyDrop() {
931 if (mDragInProgress) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700932 mItemAddedBackToSelfViaIcon = true;
Adam Cohen76078c42011-06-09 15:06:52 -0700933 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700934 }
935
936 public boolean isDropEnabled() {
937 return true;
938 }
939
Adam Cohen2801caf2011-05-13 20:57:39 -0700940 public boolean isFull() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800941 return mContent.isFull();
Adam Cohen2801caf2011-05-13 20:57:39 -0700942 }
943
944 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700945 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700946
Winson Chung892c74d2013-08-22 16:15:50 -0700947 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700948 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700949 int height = getFolderHeight();
Adam Cohen2801caf2011-05-13 20:57:39 -0700950
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800951 float scale = parent.getDescendantRectRelativeToSelf(mFolderIcon, sTempRect);
Adam Cohen8e776a62011-06-28 18:10:06 -0700952
Adam Cohen2e6da152015-05-06 11:42:25 -0700953 DeviceProfile grid = mLauncher.getDeviceProfile();
Winson Chungaf40f202013-09-18 18:26:31 -0700954
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800955 int centerX = (int) (sTempRect.left + sTempRect.width() * scale / 2);
956 int centerY = (int) (sTempRect.top + sTempRect.height() * scale / 2);
Adam Cohen2801caf2011-05-13 20:57:39 -0700957 int centeredLeft = centerX - width / 2;
958 int centeredTop = centerY - height / 2;
Winson Chung3057b1c2013-10-10 17:35:35 -0700959 int currentPage = mLauncher.getWorkspace().getNextPage();
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800960 // In case the workspace is scrolling, we need to use the final scroll to compute
961 // the folders bounds.
962 mLauncher.getWorkspace().setFinalScrollForPageChange(currentPage);
Adam Cohen35e7e642011-07-17 14:47:18 -0700963 // We first fetch the currently visible CellLayoutChildren
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800964 CellLayout currentLayout = (CellLayout) mLauncher.getWorkspace().getChildAt(currentPage);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700965 ShortcutAndWidgetContainer boundingLayout = currentLayout.getShortcutsAndWidgets();
Adam Cohen35e7e642011-07-17 14:47:18 -0700966 Rect bounds = new Rect();
967 parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800968 // We reset the workspaces scroll
969 mLauncher.getWorkspace().resetFinalScrollForPageChange(currentPage);
Adam Cohen2801caf2011-05-13 20:57:39 -0700970
Adam Cohen35e7e642011-07-17 14:47:18 -0700971 // We need to bound the folder to the currently visible CellLayoutChildren
972 int left = Math.min(Math.max(bounds.left, centeredLeft),
973 bounds.left + bounds.width() - width);
974 int top = Math.min(Math.max(bounds.top, centeredTop),
975 bounds.top + bounds.height() - height);
Sunny Goyalc6205602015-05-21 20:46:33 -0700976 if (grid.isPhone && (grid.availableWidthPx - width) < grid.iconSizePx) {
Winson Chungaf40f202013-09-18 18:26:31 -0700977 // Center the folder if it is full (on phones only)
978 left = (grid.availableWidthPx - width) / 2;
979 } else if (width >= bounds.width()) {
980 // If the folder doesn't fit within the bounds, center it about the desired bounds
Adam Cohen35e7e642011-07-17 14:47:18 -0700981 left = bounds.left + (bounds.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700982 }
Adam Cohen35e7e642011-07-17 14:47:18 -0700983 if (height >= bounds.height()) {
984 top = bounds.top + (bounds.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700985 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700986
987 int folderPivotX = width / 2 + (centeredLeft - left);
988 int folderPivotY = height / 2 + (centeredTop - top);
989 setPivotX(folderPivotX);
990 setPivotY(folderPivotY);
Adam Cohen268c4752012-06-06 17:47:33 -0700991 mFolderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700992 (1.0f * folderPivotX / width));
Adam Cohen268c4752012-06-06 17:47:33 -0700993 mFolderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700994 (1.0f * folderPivotY / height));
Adam Cohen3bf84d32012-05-07 20:17:14 -0700995
Adam Cohen662b5982011-12-13 17:45:21 -0800996 lp.width = width;
997 lp.height = height;
998 lp.x = left;
999 lp.y = top;
Adam Cohen2801caf2011-05-13 20:57:39 -07001000 }
1001
Adam Cohen268c4752012-06-06 17:47:33 -07001002 float getPivotXForIconAnimation() {
1003 return mFolderIconPivotX;
1004 }
1005 float getPivotYForIconAnimation() {
1006 return mFolderIconPivotY;
1007 }
1008
Winson Chung892c74d2013-08-22 16:15:50 -07001009 private int getContentAreaHeight() {
Adam Cohen2e6da152015-05-06 11:42:25 -07001010 DeviceProfile grid = mLauncher.getDeviceProfile();
Sunny Goyalc6205602015-05-21 20:46:33 -07001011 Rect workspacePadding = grid.getWorkspacePadding(mContent.mIsRtl);
Winson Chung892c74d2013-08-22 16:15:50 -07001012 int maxContentAreaHeight = grid.availableHeightPx -
Winson Chung892c74d2013-08-22 16:15:50 -07001013 workspacePadding.top - workspacePadding.bottom -
Sunny Goyal290800b2015-03-05 11:33:33 -08001014 mFooterHeight;
Adam Cohen1960ea42013-11-12 11:33:14 +00001015 int height = Math.min(maxContentAreaHeight,
Winson Chung892c74d2013-08-22 16:15:50 -07001016 mContent.getDesiredHeight());
Adam Cohen1960ea42013-11-12 11:33:14 +00001017 return Math.max(height, MIN_CONTENT_DIMEN);
1018 }
1019
1020 private int getContentAreaWidth() {
1021 return Math.max(mContent.getDesiredWidth(), MIN_CONTENT_DIMEN);
Winson Chung892c74d2013-08-22 16:15:50 -07001022 }
1023
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001024 private int getFolderHeight() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001025 return getFolderHeight(getContentAreaHeight());
1026 }
1027
1028 private int getFolderHeight(int contentAreaHeight) {
Sunny Goyal290800b2015-03-05 11:33:33 -08001029 return getPaddingTop() + getPaddingBottom() + contentAreaHeight + mFooterHeight;
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001030 }
Adam Cohen234c4cd2011-07-17 21:03:04 -07001031
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001032 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001033 int contentWidth = getContentAreaWidth();
1034 int contentHeight = getContentAreaHeight();
Adam Cohen1960ea42013-11-12 11:33:14 +00001035
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001036 int contentAreaWidthSpec = MeasureSpec.makeMeasureSpec(contentWidth, MeasureSpec.EXACTLY);
1037 int contentAreaHeightSpec = MeasureSpec.makeMeasureSpec(contentHeight, MeasureSpec.EXACTLY);
1038
1039 mContent.setFixedSize(contentWidth, contentHeight);
1040 mContentWrapper.measure(contentAreaWidthSpec, contentAreaHeightSpec);
Sunny Goyala07c2f52015-06-01 11:00:38 -07001041
1042 if (mContent.getChildCount() > 0) {
1043 int cellIconGap = (mContent.getPageAt(0).getCellWidth()
1044 - mLauncher.getDeviceProfile().iconSizePx) / 2;
1045 mFooter.setPadding(mContent.getPaddingLeft() + cellIconGap,
1046 mFooter.getPaddingTop(),
1047 mContent.getPaddingRight() + cellIconGap,
1048 mFooter.getPaddingBottom());
1049 }
Sunny Goyal290800b2015-03-05 11:33:33 -08001050 mFooter.measure(contentAreaWidthSpec,
1051 MeasureSpec.makeMeasureSpec(mFooterHeight, MeasureSpec.EXACTLY));
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001052
1053 int folderWidth = getPaddingLeft() + getPaddingRight() + contentWidth;
1054 int folderHeight = getFolderHeight(contentHeight);
1055 setMeasuredDimension(folderWidth, folderHeight);
Adam Cohen234c4cd2011-07-17 21:03:04 -07001056 }
1057
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001058 /**
1059 * Rearranges the children based on their rank.
1060 */
1061 public void rearrangeChildren() {
1062 rearrangeChildren(-1);
1063 }
Adam Cohen2801caf2011-05-13 20:57:39 -07001064
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001065 /**
1066 * Rearranges the children based on their rank.
1067 * @param itemCount if greater than the total children count, empty spaces are left at the end,
1068 * otherwise it is ignored.
1069 */
1070 public void rearrangeChildren(int itemCount) {
1071 ArrayList<View> views = getItemsInReadingOrder();
1072 mContent.arrangeChildren(views, Math.max(itemCount, views.size()));
Adam Cohen7c693212011-05-18 15:26:57 -07001073 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -07001074 }
1075
Sunny Goyalc4918352015-03-10 18:15:48 -07001076 // TODO remove this once GSA code fix is submitted
1077 public ViewGroup getContent() {
1078 return (ViewGroup) mContent;
1079 }
1080
Adam Cohena9cf38f2011-05-02 15:36:58 -07001081 public int getItemCount() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001082 return mContent.getItemCount();
Adam Cohena9cf38f2011-05-02 15:36:58 -07001083 }
1084
Adam Cohen091440a2015-03-18 14:16:05 -07001085 @Thunk void onCloseComplete() {
Adam Cohen05e0f402011-08-01 12:12:49 -07001086 DragLayer parent = (DragLayer) getParent();
Michael Jurka5649c282012-06-18 10:33:21 -07001087 if (parent != null) {
1088 parent.removeView(this);
1089 }
Adam Cohen4554ee12011-08-03 16:13:21 -07001090 mDragController.removeDropTarget((DropTarget) this);
Adam Cohen05e0f402011-08-01 12:12:49 -07001091 clearFocus();
Adam Cohenac56cff2011-09-28 20:45:37 -07001092 mFolderIcon.requestFocus();
Adam Cohen05e0f402011-08-01 12:12:49 -07001093
Adam Cohen2801caf2011-05-13 20:57:39 -07001094 if (mRearrangeOnClose) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001095 rearrangeChildren();
Adam Cohen2801caf2011-05-13 20:57:39 -07001096 mRearrangeOnClose = false;
1097 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001098 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001099 if (!mDragInProgress && !mSuppressFolderDeletion) {
1100 replaceFolderWithFinalItem();
1101 } else if (mDragInProgress) {
1102 mDeleteFolderOnDropCompleted = true;
1103 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001104 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001105 mSuppressFolderDeletion = false;
Sunny Goyal31abc292015-05-01 10:42:32 -07001106 clearDragInfo();
Adam Cohenafb01ee2011-06-23 15:38:03 -07001107 }
1108
Adam Cohen091440a2015-03-18 14:16:05 -07001109 @Thunk void replaceFolderWithFinalItem() {
Adam Cohenafb01ee2011-06-23 15:38:03 -07001110 // Add the last remaining child to the workspace in place of the folder
Adam Cohenfb91f302012-06-11 15:45:18 -07001111 Runnable onCompleteRunnable = new Runnable() {
1112 @Override
1113 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -07001114 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screenId);
Adam Cohenafb01ee2011-06-23 15:38:03 -07001115
Winson Chung33231f52013-12-09 16:57:45 -08001116 View child = null;
Adam Cohenfb91f302012-06-11 15:45:18 -07001117 // Move the item from the folder to the workspace, in the position of the folder
1118 if (getItemCount() == 1) {
1119 ShortcutInfo finalItem = mInfo.contents.get(0);
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001120 child = mLauncher.createShortcut(cellLayout, finalItem);
Adam Cohenfb91f302012-06-11 15:45:18 -07001121 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -07001122 mInfo.screenId, mInfo.cellX, mInfo.cellY);
Adam Cohenfb91f302012-06-11 15:45:18 -07001123 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001124 if (getItemCount() <= 1) {
1125 // Remove the folder
1126 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
Dan Sandler0eb687f2014-01-10 13:24:55 -05001127 if (cellLayout != null) {
1128 // b/12446428 -- sometimes the cell layout has already gone away?
1129 cellLayout.removeView(mFolderIcon);
1130 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001131 if (mFolderIcon instanceof DropTarget) {
1132 mDragController.removeDropTarget((DropTarget) mFolderIcon);
1133 }
1134 mLauncher.removeFolder(mInfo);
1135 }
Adam Cohenc5e63f32012-07-12 16:16:57 -07001136 // We add the child after removing the folder to prevent both from existing at
Winson Chung0e6a7132013-08-23 12:55:10 -07001137 // the same time in the CellLayout. We need to add the new item with addInScreenFromBind()
1138 // to ensure that hotseat items are placed correctly.
Adam Cohenc5e63f32012-07-12 16:16:57 -07001139 if (child != null) {
Winson Chung0e6a7132013-08-23 12:55:10 -07001140 mLauncher.getWorkspace().addInScreenFromBind(child, mInfo.container, mInfo.screenId,
Adam Cohenc5e63f32012-07-12 16:16:57 -07001141 mInfo.cellX, mInfo.cellY, mInfo.spanX, mInfo.spanY);
1142 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001143 }
1144 };
Sunny Goyalbc753352015-03-05 09:40:44 -08001145 View finalChild = mContent.getLastItem();
Adam Cohenfb91f302012-06-11 15:45:18 -07001146 if (finalChild != null) {
1147 mFolderIcon.performDestroyAnimation(finalChild, onCompleteRunnable);
Winson Chung33231f52013-12-09 16:57:45 -08001148 } else {
1149 onCompleteRunnable.run();
Adam Cohenafb01ee2011-06-23 15:38:03 -07001150 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001151 mDestroyed = true;
1152 }
1153
1154 boolean isDestroyed() {
1155 return mDestroyed;
Adam Cohen2801caf2011-05-13 20:57:39 -07001156 }
1157
Adam Cohenac56cff2011-09-28 20:45:37 -07001158 // This method keeps track of the last item in the folder for the purposes
1159 // of keyboard focus
Sunny Goyal290800b2015-03-05 11:33:33 -08001160 public void updateTextViewFocus() {
Sunny Goyalbc753352015-03-05 09:40:44 -08001161 View lastChild = mContent.getLastItem();
Adam Cohenac56cff2011-09-28 20:45:37 -07001162 if (lastChild != null) {
1163 mFolderName.setNextFocusDownId(lastChild.getId());
1164 mFolderName.setNextFocusRightId(lastChild.getId());
1165 mFolderName.setNextFocusLeftId(lastChild.getId());
1166 mFolderName.setNextFocusUpId(lastChild.getId());
1167 }
1168 }
1169
Adam Cohenbfbfd262011-06-13 16:55:12 -07001170 public void onDrop(DragObject d) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001171 Runnable cleanUpRunnable = null;
1172
Adam Cohen689ff162014-05-08 17:27:56 -07001173 // If we are coming from All Apps space, we defer removing the extra empty screen
1174 // until the folder closes
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001175 if (d.dragSource != mLauncher.getWorkspace() && !(d.dragSource instanceof Folder)) {
1176 cleanUpRunnable = new Runnable() {
1177 @Override
1178 public void run() {
Adam Cohen689ff162014-05-08 17:27:56 -07001179 mLauncher.exitSpringLoadedDragModeDelayed(true,
1180 Launcher.EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
1181 null);
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001182 }
1183 };
Adam Cohenbfbfd262011-06-13 16:55:12 -07001184 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001185
Sunny Goyalb8634152015-04-09 14:17:14 -07001186 // If the icon was dropped while the page was being scrolled, we need to compute
1187 // the target location again such that the icon is placed of the final page.
1188 if (!mContent.rankOnCurrentPage(mEmptyCellRank)) {
1189 // Reorder again.
1190 mTargetRank = getTargetRank(d, null);
Sunny Goyal48461932015-03-09 17:41:09 -07001191
Sunny Goyalb8634152015-04-09 14:17:14 -07001192 // Rearrange items immediately.
1193 mReorderAlarmListener.onAlarm(mReorderAlarm);
Sunny Goyal48461932015-03-09 17:41:09 -07001194
Sunny Goyalb8634152015-04-09 14:17:14 -07001195 mOnScrollHintAlarm.cancelAlarm();
1196 mScrollPauseAlarm.cancelAlarm();
Sunny Goyal48461932015-03-09 17:41:09 -07001197 }
Sunny Goyalb8634152015-04-09 14:17:14 -07001198 mContent.completePendingPageChanges();
Sunny Goyal48461932015-03-09 17:41:09 -07001199
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001200 View currentDragView;
1201 ShortcutInfo si = mCurrentDragInfo;
1202 if (mIsExternalDrag) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001203 currentDragView = mContent.createAndAddViewForRank(si, mEmptyCellRank);
Sunny Goyal95abbb32014-08-04 10:53:22 -07001204 // Actually move the item in the database if it was an external drag. Call this
1205 // before creating the view, so that ShortcutInfo is updated appropriately.
1206 LauncherModel.addOrMoveItemInDatabase(
1207 mLauncher, si, mInfo.id, 0, si.cellX, si.cellY);
1208
1209 // We only need to update the locations if it doesn't get handled in #onDropCompleted.
1210 if (d.dragSource != this) {
1211 updateItemLocationsInDatabaseBatch();
1212 }
1213 mIsExternalDrag = false;
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001214 } else {
1215 currentDragView = mCurrentDragView;
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001216 mContent.addViewForRank(currentDragView, si, mEmptyCellRank);
Adam Cohenbfbfd262011-06-13 16:55:12 -07001217 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001218
1219 if (d.dragView.hasDrawn()) {
1220
1221 // Temporarily reset the scale such that the animation target gets calculated correctly.
1222 float scaleX = getScaleX();
1223 float scaleY = getScaleY();
1224 setScaleX(1.0f);
1225 setScaleY(1.0f);
1226 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, currentDragView,
1227 cleanUpRunnable, null);
1228 setScaleX(scaleX);
1229 setScaleY(scaleY);
1230 } else {
1231 d.deferDragViewCleanupPostAnimation = false;
1232 currentDragView.setVisibility(VISIBLE);
1233 }
1234 mItemsInvalidated = true;
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001235 rearrangeChildren();
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001236
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001237 // Temporarily suppress the listener, as we did all the work already here.
1238 mSuppressOnAdd = true;
1239 mInfo.add(si);
1240 mSuppressOnAdd = false;
Sunny Goyal4b020172014-08-28 14:51:14 -07001241 // Clear the drag info, as it is no longer being dragged.
1242 mCurrentDragInfo = null;
Sunny Goyalf4066152015-04-15 09:42:19 -07001243 mDragInProgress = false;
Sunny Goyalb7e15ad2015-05-07 14:51:31 -07001244
1245 if (mContent.getPageCount() > 1) {
1246 // The animation has already been shown while opening the folder.
1247 mInfo.setOption(FolderInfo.FLAG_MULTI_PAGE_ANIMATION, true, mLauncher);
1248 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001249 }
1250
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001251 // This is used so the item doesn't immediately appear in the folder when added. In one case
1252 // we need to create the illusion that the item isn't added back to the folder yet, to
1253 // to correspond to the animation of the icon back into the folder. This is
1254 public void hideItem(ShortcutInfo info) {
1255 View v = getViewForInfo(info);
1256 v.setVisibility(INVISIBLE);
1257 }
1258 public void showItem(ShortcutInfo info) {
1259 View v = getViewForInfo(info);
1260 v.setVisibility(VISIBLE);
1261 }
1262
Sunny Goyalb7e15ad2015-05-07 14:51:31 -07001263 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -07001264 public void onAdd(ShortcutInfo item) {
Adam Cohen05e0f402011-08-01 12:12:49 -07001265 // If the item was dropped onto this open folder, we have done the work associated
1266 // with adding the item to the folder, as indicated by mSuppressOnAdd being set
Adam Cohenbfbfd262011-06-13 16:55:12 -07001267 if (mSuppressOnAdd) return;
Sunny Goyal5d85c442015-03-10 13:14:47 -07001268 mContent.createAndAddViewForRank(item, mContent.allocateRankForNewItem(item));
Sunny Goyal2e688a82015-03-18 10:23:39 -07001269 mItemsInvalidated = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001270 LauncherModel.addOrMoveItemInDatabase(
1271 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
1272 }
1273
Adam Cohena9cf38f2011-05-02 15:36:58 -07001274 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -07001275 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001276 // If this item is being dragged from this open folder, we have already handled
1277 // the work associated with removing the item, so we don't have to do anything here.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001278 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001279 View v = getViewForInfo(item);
Sunny Goyal290800b2015-03-05 11:33:33 -08001280 mContent.removeItem(v);
Adam Cohen2801caf2011-05-13 20:57:39 -07001281 if (mState == STATE_ANIMATING) {
1282 mRearrangeOnClose = true;
1283 } else {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001284 rearrangeChildren();
Adam Cohen2801caf2011-05-13 20:57:39 -07001285 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001286 if (getItemCount() <= 1) {
1287 replaceFolderWithFinalItem();
1288 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001289 }
Adam Cohen7c693212011-05-18 15:26:57 -07001290
Sunny Goyalbc753352015-03-05 09:40:44 -08001291 private View getViewForInfo(final ShortcutInfo item) {
1292 return mContent.iterateOverItems(new ItemOperator() {
1293
1294 @Override
1295 public boolean evaluate(ItemInfo info, View view, View parent) {
1296 return info == item;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001297 }
Sunny Goyalbc753352015-03-05 09:40:44 -08001298 });
Adam Cohendf1e4e82011-06-24 15:57:39 -07001299 }
1300
Adam Cohen76078c42011-06-09 15:06:52 -07001301 public void onItemsChanged() {
Adam Cohenac56cff2011-09-28 20:45:37 -07001302 updateTextViewFocus();
Adam Cohen76078c42011-06-09 15:06:52 -07001303 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001304
Adam Cohen76fc0852011-06-17 13:26:23 -07001305 public void onTitleChanged(CharSequence title) {
1306 }
Adam Cohen76078c42011-06-09 15:06:52 -07001307
Adam Cohen7c693212011-05-18 15:26:57 -07001308 public ArrayList<View> getItemsInReadingOrder() {
1309 if (mItemsInvalidated) {
1310 mItemsInReadingOrder.clear();
Sunny Goyalbc753352015-03-05 09:40:44 -08001311 mContent.iterateOverItems(new ItemOperator() {
1312
1313 @Override
1314 public boolean evaluate(ItemInfo info, View view, View parent) {
1315 mItemsInReadingOrder.add(view);
1316 return false;
Adam Cohen7c693212011-05-18 15:26:57 -07001317 }
Sunny Goyalbc753352015-03-05 09:40:44 -08001318 });
Adam Cohen7c693212011-05-18 15:26:57 -07001319 mItemsInvalidated = false;
1320 }
1321 return mItemsInReadingOrder;
1322 }
Adam Cohen8dfcba42011-07-07 16:38:18 -07001323
1324 public void getLocationInDragLayer(int[] loc) {
1325 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
1326 }
Adam Cohenea0818d2011-09-30 20:06:58 -07001327
1328 public void onFocusChange(View v, boolean hasFocus) {
1329 if (v == mFolderName && hasFocus) {
1330 startEditingFolderName();
1331 }
1332 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001333
1334 @Override
1335 public void getHitRectRelativeToDragLayer(Rect outRect) {
1336 getHitRect(outRect);
Sunny Goyal48461932015-03-09 17:41:09 -07001337 outRect.left -= mScrollAreaOffset;
1338 outRect.right += mScrollAreaOffset;
1339 }
1340
1341 private class OnScrollHintListener implements OnAlarmListener {
1342
1343 private final DragObject mDragObject;
1344
1345 OnScrollHintListener(DragObject object) {
1346 mDragObject = object;
1347 }
1348
1349 /**
1350 * Scroll hint has been shown long enough. Now scroll to appropriate page.
1351 */
1352 @Override
1353 public void onAlarm(Alarm alarm) {
1354 if (mCurrentScrollDir == DragController.SCROLL_LEFT) {
Sunny Goyalb8634152015-04-09 14:17:14 -07001355 mContent.scrollLeft();
Sunny Goyal48461932015-03-09 17:41:09 -07001356 mScrollHintDir = DragController.SCROLL_NONE;
1357 } else if (mCurrentScrollDir == DragController.SCROLL_RIGHT) {
Sunny Goyalb8634152015-04-09 14:17:14 -07001358 mContent.scrollRight();
Sunny Goyal48461932015-03-09 17:41:09 -07001359 mScrollHintDir = DragController.SCROLL_NONE;
1360 } else {
1361 // This should not happen
1362 return;
1363 }
1364 mCurrentScrollDir = DragController.SCROLL_NONE;
1365
1366 // Pause drag event until the scrolling is finished
1367 mScrollPauseAlarm.setOnAlarmListener(new OnScrollFinishedListener(mDragObject));
1368 mScrollPauseAlarm.setAlarm(DragController.RESCROLL_DELAY);
1369 }
1370 }
1371
1372 private class OnScrollFinishedListener implements OnAlarmListener {
1373
1374 private final DragObject mDragObject;
1375
1376 OnScrollFinishedListener(DragObject object) {
1377 mDragObject = object;
1378 }
1379
1380 /**
1381 * Page scroll is complete.
1382 */
1383 @Override
1384 public void onAlarm(Alarm alarm) {
1385 // Reorder immediately on page change.
1386 onDragOver(mDragObject, 1);
1387 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001388 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001389}