blob: a5b7a6003f0ed6a2e3303bce605146d02f49754a [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;
Sunny Goyala07c2f52015-06-01 11:00:38 -070032import android.support.v4.view.animation.FastOutSlowInInterpolator;
Adam Cohen7a14d0b2011-06-24 11:36:35 -070033import android.text.InputType;
Adam Cohene601a432011-07-26 21:51:30 -070034import android.text.Selection;
35import android.text.Spannable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.util.AttributeSet;
Sunny Goyala07c2f52015-06-01 11:00:38 -070037import android.util.LayoutDirection;
Adam Cohen3bf84d32012-05-07 20:17:14 -070038import android.util.Log;
Adam Cohen76fc0852011-06-17 13:26:23 -070039import android.view.ActionMode;
40import android.view.KeyEvent;
Adam Cohen76fc0852011-06-17 13:26:23 -070041import android.view.Menu;
42import android.view.MenuItem;
Adam Cohen0c872ba2011-05-05 10:34:16 -070043import android.view.MotionEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080044import android.view.View;
Sunny Goyalc4918352015-03-10 18:15:48 -070045import android.view.ViewGroup;
Adam Cohen3371da02011-10-25 21:38:29 -070046import android.view.accessibility.AccessibilityEvent;
47import android.view.accessibility.AccessibilityManager;
Adam Cohenc4fe9ea2014-08-18 18:54:10 -070048import android.view.animation.AccelerateInterpolator;
Adam Cohen76fc0852011-06-17 13:26:23 -070049import android.view.inputmethod.EditorInfo;
50import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080051import android.widget.LinearLayout;
Adam Cohendf2cc412011-04-27 16:56:57 -070052import android.widget.TextView;
Sunny Goyal83a8f042015-05-19 12:52:12 -070053
Sunny Goyale9b651e2015-04-24 11:44:51 -070054import com.android.launcher3.CellLayout.CellInfo;
Sunny Goyalf4066152015-04-15 09:42:19 -070055import com.android.launcher3.DragController.DragListener;
Daniel Sandler325dc232013-06-05 22:57:57 -040056import com.android.launcher3.FolderInfo.FolderListener;
Sunny Goyalfa401a12015-04-10 13:45:42 -070057import com.android.launcher3.UninstallDropTarget.UninstallSource;
Sunny Goyalbc753352015-03-05 09:40:44 -080058import com.android.launcher3.Workspace.ItemOperator;
Sunny Goyal83a8f042015-05-19 12:52:12 -070059import com.android.launcher3.accessibility.LauncherAccessibilityDelegate.AccessibilityDragSource;
Adam Cohen091440a2015-03-18 14:16:05 -070060import com.android.launcher3.util.Thunk;
Sunny Goyal83a8f042015-05-19 12:52:12 -070061
Adam Cohenc0dcf592011-06-01 15:30:43 -070062import java.util.ArrayList;
Adam Cohen3bf84d32012-05-07 20:17:14 -070063import java.util.Collections;
Adam Cohenc0dcf592011-06-01 15:30:43 -070064
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065/**
66 * Represents a set of icons chosen by the user or generated by the system.
67 */
Adam Cohen8dfcba42011-07-07 16:38:18 -070068public class Folder extends LinearLayout implements DragSource, View.OnClickListener,
Adam Cohenea0818d2011-09-30 20:06:58 -070069 View.OnLongClickListener, DropTarget, FolderListener, TextView.OnEditorActionListener,
Sunny Goyale9b651e2015-04-24 11:44:51 -070070 View.OnFocusChangeListener, DragListener, UninstallSource, AccessibilityDragSource {
Adam Cohendf2cc412011-04-27 16:56:57 -070071 private static final String TAG = "Launcher.Folder";
72
Sunny Goyalc3a609f2015-02-26 17:43:50 -080073 /**
74 * We avoid measuring {@link #mContentWrapper} with a 0 width or height, as this
75 * results in CellLayout being measured as UNSPECIFIED, which it does not support.
76 */
77 private static final int MIN_CONTENT_DIMEN = 5;
Adam Cohen4eac29a2011-07-11 17:53:37 -070078
Adam Cohendf2cc412011-04-27 16:56:57 -070079 static final int STATE_NONE = -1;
80 static final int STATE_SMALL = 0;
81 static final int STATE_ANIMATING = 1;
82 static final int STATE_OPEN = 2;
83
Sunny Goyal48461932015-03-09 17:41:09 -070084 /**
Sunny Goyal48461932015-03-09 17:41:09 -070085 * Time for which the scroll hint is shown before automatically changing page.
86 */
87 public static final int SCROLL_HINT_DURATION = DragController.SCROLL_DELAY;
88
89 /**
90 * Fraction of icon width which behave as scroll region.
91 */
92 private static final float ICON_OVERSCROLL_WIDTH_FACTOR = 0.45f;
93
Sunny Goyala07c2f52015-06-01 11:00:38 -070094 private static final int FOLDER_NAME_ANIMATION_DURATION = 633;
Sunny Goyalb7e15ad2015-05-07 14:51:31 -070095
Adam Cohenf0f4eda2013-06-06 21:27:03 -070096 private static final int REORDER_DELAY = 250;
Adam Cohen5d518fa2013-12-05 14:16:23 -080097 private static final int ON_EXIT_CLOSE_DELAY = 400;
Sunny Goyalc3a609f2015-02-26 17:43:50 -080098 private static final Rect sTempRect = new Rect();
99
100 private static String sDefaultFolderName;
101 private static String sHintText;
102
103 private final Alarm mReorderAlarm = new Alarm();
104 private final Alarm mOnExitAlarm = new Alarm();
Sunny Goyalb8634152015-04-09 14:17:14 -0700105 private final Alarm mOnScrollHintAlarm = new Alarm();
106 @Thunk final Alarm mScrollPauseAlarm = new Alarm();
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800107
Adam Cohen091440a2015-03-18 14:16:05 -0700108 @Thunk final ArrayList<View> mItemsInReadingOrder = new ArrayList<View>();
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800109
110 private final int mExpandDuration;
111 private final int mMaterialExpandDuration;
112 private final int mMaterialExpandStagger;
113
114 private final InputMethodManager mInputMethodManager;
115
116 protected final Launcher mLauncher;
117 protected DragController mDragController;
118 protected FolderInfo mInfo;
119
Adam Cohen091440a2015-03-18 14:16:05 -0700120 @Thunk FolderIcon mFolderIcon;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800121
Sunny Goyalb8634152015-04-09 14:17:14 -0700122 @Thunk FolderPagedView mContent;
Adam Cohen091440a2015-03-18 14:16:05 -0700123 @Thunk View mContentWrapper;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800124 FolderEditText mFolderName;
125
Sunny Goyal290800b2015-03-05 11:33:33 -0800126 private View mFooter;
127 private int mFooterHeight;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800128
129 // Cell ranks used for drag and drop
Adam Cohen091440a2015-03-18 14:16:05 -0700130 @Thunk int mTargetRank, mPrevTargetRank, mEmptyCellRank;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800131
Adam Cohen091440a2015-03-18 14:16:05 -0700132 @Thunk int mState = STATE_NONE;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800133 private boolean mRearrangeOnClose = false;
Adam Cohen7c693212011-05-18 15:26:57 -0700134 boolean mItemsInvalidated = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700135 private ShortcutInfo mCurrentDragInfo;
136 private View mCurrentDragView;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800137 private boolean mIsExternalDrag;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700138 boolean mSuppressOnAdd = false;
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700139 private boolean mDragInProgress = false;
140 private boolean mDeleteFolderOnDropCompleted = false;
141 private boolean mSuppressFolderDeletion = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700142 private boolean mItemAddedBackToSelfViaIcon = false;
Adam Cohen091440a2015-03-18 14:16:05 -0700143 @Thunk float mFolderIconPivotX;
144 @Thunk float mFolderIconPivotY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700145 private boolean mIsEditingName = false;
Adam Cohen1960ea42013-11-12 11:33:14 +0000146
Adam Cohenfb91f302012-06-11 15:45:18 -0700147 private boolean mDestroyed;
148
Adam Cohen091440a2015-03-18 14:16:05 -0700149 @Thunk Runnable mDeferredAction;
Michael Jurka1e2f4652013-07-08 18:03:46 -0700150 private boolean mDeferDropAfterUninstall;
151 private boolean mUninstallSuccessful;
152
Sunny Goyal48461932015-03-09 17:41:09 -0700153 // Folder scrolling
154 private int mScrollAreaOffset;
Sunny Goyal48461932015-03-09 17:41:09 -0700155
Adam Cohen091440a2015-03-18 14:16:05 -0700156 @Thunk int mScrollHintDir = DragController.SCROLL_NONE;
157 @Thunk int mCurrentScrollDir = DragController.SCROLL_NONE;
Sunny Goyal48461932015-03-09 17:41:09 -0700158
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159 /**
160 * Used to inflate the Workspace from XML.
161 *
162 * @param context The application's context.
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800163 * @param attrs The attributes set containing the Workspace's customization values.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164 */
165 public Folder(Context context, AttributeSet attrs) {
166 super(context, attrs);
167 setAlwaysDrawnWithCacheEnabled(false);
Adam Cohen76fc0852011-06-17 13:26:23 -0700168 mInputMethodManager = (InputMethodManager)
Michael Jurka8b805b12012-04-18 14:23:14 -0700169 getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
Adam Cohen76fc0852011-06-17 13:26:23 -0700170
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800171 Resources res = getResources();
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700172 mExpandDuration = res.getInteger(R.integer.config_folderExpandDuration);
173 mMaterialExpandDuration = res.getInteger(R.integer.config_materialFolderExpandDuration);
174 mMaterialExpandStagger = res.getInteger(R.integer.config_materialFolderExpandStagger);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700175
176 if (sDefaultFolderName == null) {
177 sDefaultFolderName = res.getString(R.string.folder_name);
178 }
Adam Cohena65beee2011-06-27 21:32:23 -0700179 if (sHintText == null) {
180 sHintText = res.getString(R.string.folder_hint_text);
181 }
Adam Cohen4eac29a2011-07-11 17:53:37 -0700182 mLauncher = (Launcher) context;
Adam Cohenac56cff2011-09-28 20:45:37 -0700183 // We need this view to be focusable in touch mode so that when text editing of the folder
184 // name is complete, we have something to focus on, thus hiding the cursor and giving
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800185 // reliable behavior when clicking the text field (since it will always gain focus on click).
Adam Cohenac56cff2011-09-28 20:45:37 -0700186 setFocusableInTouchMode(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187 }
188
189 @Override
190 protected void onFinishInflate() {
191 super.onFinishInflate();
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800192 mContentWrapper = findViewById(R.id.folder_content_wrapper);
Sunny Goyalb8634152015-04-09 14:17:14 -0700193 mContent = (FolderPagedView) findViewById(R.id.folder_content);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800194 mContent.setFolder(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700195
Adam Cohenac56cff2011-09-28 20:45:37 -0700196 mFolderName = (FolderEditText) findViewById(R.id.folder_name);
197 mFolderName.setFolder(this);
Adam Cohenea0818d2011-09-30 20:06:58 -0700198 mFolderName.setOnFocusChangeListener(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700199
Adam Cohen76fc0852011-06-17 13:26:23 -0700200 // We disable action mode for now since it messes up the view on phones
201 mFolderName.setCustomSelectionActionModeCallback(mActionModeCallback);
Adam Cohen76fc0852011-06-17 13:26:23 -0700202 mFolderName.setOnEditorActionListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700203 mFolderName.setSelectAllOnFocus(true);
Adam Cohen7a14d0b2011-06-24 11:36:35 -0700204 mFolderName.setInputType(mFolderName.getInputType() |
205 InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800206
Sunny Goyalb8634152015-04-09 14:17:14 -0700207 mFooter = findViewById(R.id.folder_footer);
Sunny Goyalb8634152015-04-09 14:17:14 -0700208
Sunny Goyal290800b2015-03-05 11:33:33 -0800209 // We find out how tall footer wants to be (it is set to wrap_content), so that
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800210 // we can allocate the appropriate amount of space for it.
211 int measureSpec = MeasureSpec.UNSPECIFIED;
Sunny Goyal290800b2015-03-05 11:33:33 -0800212 mFooter.measure(measureSpec, measureSpec);
213 mFooterHeight = mFooter.getMeasuredHeight();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800214 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700215
Adam Cohen76fc0852011-06-17 13:26:23 -0700216 private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
217 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
218 return false;
219 }
220
221 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
222 return false;
223 }
224
225 public void onDestroyActionMode(ActionMode mode) {
226 }
227
228 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
229 return false;
230 }
231 };
232
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800233 public void onClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700234 Object tag = v.getTag();
235 if (tag instanceof ShortcutInfo) {
Adam Cohenb5fe60c2013-06-06 22:03:51 -0700236 mLauncher.onClick(v);
Adam Cohendf2cc412011-04-27 16:56:57 -0700237 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800238 }
239
240 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -0700241 // Return if global dragging is not enabled
242 if (!mLauncher.isDraggingEnabled()) return true;
Sunny Goyale9b651e2015-04-24 11:44:51 -0700243 return beginDrag(v, false);
244 }
Winson Chung36a62fe2012-05-06 18:04:42 -0700245
Sunny Goyale9b651e2015-04-24 11:44:51 -0700246 private boolean beginDrag(View v, boolean accessible) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700247 Object tag = v.getTag();
248 if (tag instanceof ShortcutInfo) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700249 ShortcutInfo item = (ShortcutInfo) tag;
250 if (!v.isInTouchMode()) {
251 return false;
252 }
253
Sunny Goyale9b651e2015-04-24 11:44:51 -0700254 mLauncher.getWorkspace().beginDragShared(v, new Point(), this, accessible);
Adam Cohen76078c42011-06-09 15:06:52 -0700255
256 mCurrentDragInfo = item;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800257 mEmptyCellRank = item.rank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700258 mCurrentDragView = v;
Adam Cohenfc53cd22011-07-20 15:45:11 -0700259
Sunny Goyal290800b2015-03-05 11:33:33 -0800260 mContent.removeItem(mCurrentDragView);
Adam Cohenfc53cd22011-07-20 15:45:11 -0700261 mInfo.remove(mCurrentDragInfo);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700262 mDragInProgress = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700263 mItemAddedBackToSelfViaIcon = false;
Adam Cohendf2cc412011-04-27 16:56:57 -0700264 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800265 return true;
266 }
267
Sunny Goyale9b651e2015-04-24 11:44:51 -0700268 @Override
269 public void startDrag(CellInfo cellInfo, boolean accessible) {
270 beginDrag(cellInfo.cell, accessible);
271 }
272
273 @Override
274 public void enableAccessibleDrag(boolean enable) {
275 mLauncher.getSearchBar().enableAccessibleDrag(enable);
276 for (int i = 0; i < mContent.getChildCount(); i++) {
277 mContent.getPageAt(i).enableAccessibleDrag(enable, CellLayout.FOLDER_ACCESSIBILITY_DRAG);
278 }
Sunny Goyalccc414b2015-04-30 12:28:16 -0700279
280 mFooter.setImportantForAccessibility(enable ? IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS :
281 IMPORTANT_FOR_ACCESSIBILITY_AUTO);
Sunny Goyale9b651e2015-04-24 11:44:51 -0700282 mLauncher.getWorkspace().setAddNewPageOnDrag(!enable);
283 }
284
Adam Cohen76fc0852011-06-17 13:26:23 -0700285 public boolean isEditingName() {
286 return mIsEditingName;
287 }
288
289 public void startEditingFolderName() {
Adam Cohena65beee2011-06-27 21:32:23 -0700290 mFolderName.setHint("");
Adam Cohen76fc0852011-06-17 13:26:23 -0700291 mIsEditingName = true;
292 }
293
294 public void dismissEditingName() {
295 mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
296 doneEditingFolderName(true);
297 }
298
299 public void doneEditingFolderName(boolean commit) {
Adam Cohena65beee2011-06-27 21:32:23 -0700300 mFolderName.setHint(sHintText);
Adam Cohen1df26a32011-08-12 16:15:23 -0700301 // Convert to a string here to ensure that no other state associated with the text field
302 // gets saved.
Winson Chung82b016c2015-05-08 17:00:10 -0700303 CharSequence newTitle = mFolderName.getText();
Adam Cohen3371da02011-10-25 21:38:29 -0700304 mInfo.setTitle(newTitle);
Adam Cohen76fc0852011-06-17 13:26:23 -0700305 LauncherModel.updateItemInDatabase(mLauncher, mInfo);
Adam Cohenac56cff2011-09-28 20:45:37 -0700306
Adam Cohen3371da02011-10-25 21:38:29 -0700307 if (commit) {
308 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Winson Chung82b016c2015-05-08 17:00:10 -0700309 String.format(getContext().getString(R.string.folder_renamed),
310 newTitle.toString()));
Adam Cohen3371da02011-10-25 21:38:29 -0700311 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700312 // In order to clear the focus from the text field, we set the focus on ourself. This
313 // ensures that every time the field is clicked, focus is gained, giving reliable behavior.
314 requestFocus();
315
Adam Cohene601a432011-07-26 21:51:30 -0700316 Selection.setSelection((Spannable) mFolderName.getText(), 0, 0);
Adam Cohen76fc0852011-06-17 13:26:23 -0700317 mIsEditingName = false;
318 }
319
320 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
321 if (actionId == EditorInfo.IME_ACTION_DONE) {
322 dismissEditingName();
323 return true;
324 }
325 return false;
326 }
327
328 public View getEditTextRegion() {
329 return mFolderName;
330 }
331
Adam Cohen0c872ba2011-05-05 10:34:16 -0700332 /**
333 * We need to handle touch events to prevent them from falling through to the workspace below.
334 */
Sunny Goyal70660032015-05-14 00:07:08 -0700335 @SuppressLint("ClickableViewAccessibility")
Adam Cohen0c872ba2011-05-05 10:34:16 -0700336 @Override
337 public boolean onTouchEvent(MotionEvent ev) {
338 return true;
339 }
340
Joe Onorato00acb122009-08-04 16:04:30 -0400341 public void setDragController(DragController dragController) {
342 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800343 }
344
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800345 public void setFolderIcon(FolderIcon icon) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700346 mFolderIcon = icon;
347 }
348
Adam Cohen3371da02011-10-25 21:38:29 -0700349 @Override
350 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
351 // When the folder gets focus, we don't want to announce the list of items.
352 return true;
353 }
354
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800355 /**
356 * @return the FolderInfo object associated with this folder
357 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700358 public FolderInfo getInfo() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800359 return mInfo;
360 }
361
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800362 void bind(FolderInfo info) {
363 mInfo = info;
Adam Cohendf2cc412011-04-27 16:56:57 -0700364 ArrayList<ShortcutInfo> children = info.contents;
Sunny Goyal08f72612015-01-05 13:41:43 -0800365 Collections.sort(children, Utilities.RANK_COMPARATOR);
Sunny Goyal08f72612015-01-05 13:41:43 -0800366
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800367 ArrayList<ShortcutInfo> overflow = mContent.bindItems(children);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700368
Jason Monk4ff73882014-04-24 16:48:07 -0400369 // If our folder has too many items we prune them from the list. This is an issue
Adam Cohenc508b2d2011-06-28 14:41:44 -0700370 // when upgrading from the old Folders implementation which could contain an unlimited
371 // number of items.
372 for (ShortcutInfo item: overflow) {
373 mInfo.remove(item);
374 LauncherModel.deleteItemFromDatabase(mLauncher, item);
375 }
376
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800377 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
378 if (lp == null) {
379 lp = new DragLayer.LayoutParams(0, 0);
380 lp.customPosition = true;
381 setLayoutParams(lp);
382 }
383 centerAboutIcon();
384
Adam Cohen4dbe6d92011-05-18 17:14:15 -0700385 mItemsInvalidated = true;
Adam Cohenac56cff2011-09-28 20:45:37 -0700386 updateTextViewFocus();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700387 mInfo.addListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700388
Adam Cohenafb01ee2011-06-23 15:38:03 -0700389 if (!sDefaultFolderName.contentEquals(mInfo.title)) {
Adam Cohen4ef610f2011-06-21 22:37:36 -0700390 mFolderName.setText(mInfo.title);
391 } else {
392 mFolderName.setText("");
393 }
Winson Chung33231f52013-12-09 16:57:45 -0800394
395 // In case any children didn't come across during loading, clean up the folder accordingly
396 mFolderIcon.post(new Runnable() {
397 public void run() {
398 if (getItemCount() <= 1) {
399 replaceFolderWithFinalItem();
400 }
401 }
402 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700403 }
404
405 /**
406 * Creates a new UserFolder, inflated from R.layout.user_folder.
407 *
408 * @param context The application's context.
409 *
410 * @return A new UserFolder.
411 */
Sunny Goyalc23da842015-05-14 20:44:01 -0700412 @SuppressLint("InflateParams")
Sunny Goyal70660032015-05-14 00:07:08 -0700413 static Folder fromXml(Launcher launcher) {
Sunny Goyalc23da842015-05-14 20:44:01 -0700414 return (Folder) launcher.getLayoutInflater().inflate(R.layout.user_folder, null);
Adam Cohendf2cc412011-04-27 16:56:57 -0700415 }
416
417 /**
418 * This method is intended to make the UserFolder to be visually identical in size and position
419 * to its associated FolderIcon. This allows for a seamless transition into the expanded state.
420 */
421 private void positionAndSizeAsIcon() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700422 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800423 setScaleX(0.8f);
424 setScaleY(0.8f);
425 setAlpha(0f);
Adam Cohendf2cc412011-04-27 16:56:57 -0700426 mState = STATE_SMALL;
427 }
428
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700429 private void prepareReveal() {
430 setScaleX(1f);
431 setScaleY(1f);
432 setAlpha(1f);
433 mState = STATE_SMALL;
434 }
435
Adam Cohendf2cc412011-04-27 16:56:57 -0700436 public void animateOpen() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700437 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen6441de02011-12-14 14:25:32 -0800438
Sunny Goyalb8634152015-04-09 14:17:14 -0700439 mContent.completePendingPageChanges();
Sunny Goyal8167dc22015-04-27 13:44:01 -0700440 if (!mDragInProgress) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700441 // Open on the first page.
442 mContent.snapToPageImmediately(0);
Sunny Goyal48461932015-03-09 17:41:09 -0700443 }
444
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700445 Animator openFolderAnim = null;
446 final Runnable onCompleteRunnable;
Kenny Guyd794a3f2014-09-16 15:17:58 +0100447 if (!Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700448 positionAndSizeAsIcon();
449 centerAboutIcon();
450
451 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1);
452 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
453 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
454 final ObjectAnimator oa =
455 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
456 oa.setDuration(mExpandDuration);
457 openFolderAnim = oa;
458
459 setLayerType(LAYER_TYPE_HARDWARE, null);
460 onCompleteRunnable = new Runnable() {
461 @Override
462 public void run() {
463 setLayerType(LAYER_TYPE_NONE, null);
464 }
465 };
466 } else {
467 prepareReveal();
468 centerAboutIcon();
469
470 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
471 int height = getFolderHeight();
472
473 float transX = - 0.075f * (width / 2 - getPivotX());
474 float transY = - 0.075f * (height / 2 - getPivotY());
475 setTranslationX(transX);
476 setTranslationY(transY);
477 PropertyValuesHolder tx = PropertyValuesHolder.ofFloat("translationX", transX, 0);
478 PropertyValuesHolder ty = PropertyValuesHolder.ofFloat("translationY", transY, 0);
479
Sunny Goyal75deaf32015-05-07 16:13:12 -0700480 Animator drift = LauncherAnimUtils.ofPropertyValuesHolder(this, tx, ty);
481 drift.setDuration(mMaterialExpandDuration);
482 drift.setStartDelay(mMaterialExpandStagger);
483 drift.setInterpolator(new LogDecelerateInterpolator(100, 0));
484
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700485 int rx = (int) Math.max(Math.max(width - getPivotX(), 0), getPivotX());
486 int ry = (int) Math.max(Math.max(height - getPivotY(), 0), getPivotY());
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700487 float radius = (float) Math.hypot(rx, ry);
Sunny Goyal75deaf32015-05-07 16:13:12 -0700488
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700489 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
490 Animator reveal = LauncherAnimUtils.createCircularReveal(this, (int) getPivotX(),
491 (int) getPivotY(), 0, radius);
492 reveal.setDuration(mMaterialExpandDuration);
493 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
494
Sunny Goyalbc753352015-03-05 09:40:44 -0800495 mContentWrapper.setAlpha(0f);
496 Animator iconsAlpha = LauncherAnimUtils.ofFloat(mContentWrapper, "alpha", 0f, 1f);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700497 iconsAlpha.setDuration(mMaterialExpandDuration);
498 iconsAlpha.setStartDelay(mMaterialExpandStagger);
499 iconsAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
500
Sunny Goyal290800b2015-03-05 11:33:33 -0800501 mFooter.setAlpha(0f);
502 Animator textAlpha = LauncherAnimUtils.ofFloat(mFooter, "alpha", 0f, 1f);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700503 textAlpha.setDuration(mMaterialExpandDuration);
504 textAlpha.setStartDelay(mMaterialExpandStagger);
505 textAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
506
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700507 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());
Sunny Goyala07c2f52015-06-01 11:00:38 -0700549 float translation = (footerWidth - textWidth) / 2;
550 mFolderName.setTranslationX(mContent.mIsRtl ? -translation : translation);
Sunny Goyalb7e15ad2015-05-07 14:51:31 -0700551 mContent.setMarkerScale(0);
552
553 // Do not update the flag if we are in drag mode. The flag will be updated, when we
554 // actually drop the icon.
555 final boolean updateAnimationFlag = !mDragInProgress;
556 openFolderAnim.addListener(new AnimatorListenerAdapter() {
557
558 @Override
559 public void onAnimationEnd(Animator animation) {
Sunny Goyala07c2f52015-06-01 11:00:38 -0700560 mFolderName.animate().setDuration(FOLDER_NAME_ANIMATION_DURATION)
561 .translationX(0)
562 .setInterpolator(new FastOutSlowInInterpolator());
Sunny Goyalb7e15ad2015-05-07 14:51:31 -0700563 mContent.animateMarkers();
564
565 if (updateAnimationFlag) {
566 mInfo.setOption(FolderInfo.FLAG_MULTI_PAGE_ANIMATION, true, mLauncher);
567 }
568 }
569 });
570 } else {
571 mFolderName.setTranslationX(0);
572 mContent.setMarkerScale(1);
573 }
574
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700575 openFolderAnim.start();
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800576
577 // Make sure the folder picks up the last drag move even if the finger doesn't move.
578 if (mDragController.isDragging()) {
579 mDragController.forceTouchMove();
580 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700581
Sunny Goyalb8634152015-04-09 14:17:14 -0700582 FolderPagedView pages = (FolderPagedView) mContent;
583 pages.verifyVisibleHighResIcons(pages.getNextPage());
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800584 }
585
586 public void beginExternalDrag(ShortcutInfo item) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800587 mCurrentDragInfo = item;
Sunny Goyal5d85c442015-03-10 13:14:47 -0700588 mEmptyCellRank = mContent.allocateRankForNewItem(item);
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800589 mIsExternalDrag = true;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800590 mDragInProgress = true;
Sunny Goyalb8634152015-04-09 14:17:14 -0700591
Sunny Goyalf4066152015-04-15 09:42:19 -0700592 // Since this folder opened by another controller, it might not get onDrop or
593 // onDropComplete. Perform cleanup once drag-n-drop ends.
594 mDragController.addDragListener(this);
595 }
596
597 @Override
598 public void onDragStart(DragSource source, Object info, int dragAction) { }
599
600 @Override
601 public void onDragEnd() {
602 if (mIsExternalDrag && mDragInProgress) {
603 completeDragExit();
604 }
605 mDragController.removeDragListener(this);
Adam Cohendf2cc412011-04-27 16:56:57 -0700606 }
607
Adam Cohen091440a2015-03-18 14:16:05 -0700608 @Thunk void sendCustomAccessibilityEvent(int type, String text) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700609 AccessibilityManager accessibilityManager = (AccessibilityManager)
610 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
611 if (accessibilityManager.isEnabled()) {
Adam Cohen3371da02011-10-25 21:38:29 -0700612 AccessibilityEvent event = AccessibilityEvent.obtain(type);
613 onInitializeAccessibilityEvent(event);
614 event.getText().add(text);
Michael Jurka8b805b12012-04-18 14:23:14 -0700615 accessibilityManager.sendAccessibilityEvent(event);
Adam Cohen3371da02011-10-25 21:38:29 -0700616 }
617 }
618
Adam Cohendf2cc412011-04-27 16:56:57 -0700619 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700620 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800621 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
622 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
623 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
Michael Jurka032e6ba2013-04-22 15:08:42 +0200624 final ObjectAnimator oa =
Michael Jurka2ecf9952012-06-18 12:52:28 -0700625 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohendf2cc412011-04-27 16:56:57 -0700626
Adam Cohen2801caf2011-05-13 20:57:39 -0700627 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700628 @Override
629 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700630 onCloseComplete();
Michael Jurka0121c3e2012-05-31 08:36:04 -0700631 setLayerType(LAYER_TYPE_NONE, null);
Adam Cohen2801caf2011-05-13 20:57:39 -0700632 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700633 }
634 @Override
635 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700636 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700637 getContext().getString(R.string.folder_closed));
Adam Cohendf2cc412011-04-27 16:56:57 -0700638 mState = STATE_ANIMATING;
639 }
640 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700641 oa.setDuration(mExpandDuration);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700642 setLayerType(LAYER_TYPE_HARDWARE, null);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100643 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700644 }
645
Adam Cohencb3382b2011-05-24 14:07:08 -0700646 public boolean acceptDrop(DragObject d) {
647 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700648 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700649 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
650 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
651 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700652 }
653
Adam Cohencb3382b2011-05-24 14:07:08 -0700654 public void onDragEnter(DragObject d) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800655 mPrevTargetRank = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700656 mOnExitAlarm.cancelAlarm();
Sunny Goyalb8634152015-04-09 14:17:14 -0700657 // Get the area offset such that the folder only closes if half the drag icon width
658 // is outside the folder area
659 mScrollAreaOffset = d.dragView.getDragRegionWidth() / 2 - d.xOffset;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700660 }
661
662 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
663 public void onAlarm(Alarm alarm) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800664 mContent.realTimeReorder(mEmptyCellRank, mTargetRank);
665 mEmptyCellRank = mTargetRank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700666 }
667 };
668
Sunny Goyalc46bfef2015-01-05 12:40:08 -0800669 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohen2374abf2013-04-16 14:56:57 -0700670 public boolean isLayoutRtl() {
671 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
672 }
673
Sunny Goyal48461932015-03-09 17:41:09 -0700674 @Override
Adam Cohencb3382b2011-05-24 14:07:08 -0700675 public void onDragOver(DragObject d) {
Sunny Goyal48461932015-03-09 17:41:09 -0700676 onDragOver(d, REORDER_DELAY);
677 }
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700678
Sunny Goyal48461932015-03-09 17:41:09 -0700679 private int getTargetRank(DragObject d, float[] recycle) {
680 recycle = d.getVisualCenter(recycle);
681 return mContent.findNearestArea(
682 (int) recycle[0] - getPaddingLeft(), (int) recycle[1] - getPaddingTop());
683 }
684
Adam Cohen091440a2015-03-18 14:16:05 -0700685 @Thunk void onDragOver(DragObject d, int reorderDelay) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700686 if (mScrollPauseAlarm.alarmPending()) {
Sunny Goyal48461932015-03-09 17:41:09 -0700687 return;
688 }
689 final float[] r = new float[2];
690 mTargetRank = getTargetRank(d, r);
691
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800692 if (mTargetRank != mPrevTargetRank) {
Alan Viverette4cda5b72013-08-28 17:53:41 -0700693 mReorderAlarm.cancelAlarm();
Sunny Goyalc46bfef2015-01-05 12:40:08 -0800694 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
695 mReorderAlarm.setAlarm(REORDER_DELAY);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800696 mPrevTargetRank = mTargetRank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700697 }
Sunny Goyal48461932015-03-09 17:41:09 -0700698
Sunny Goyal48461932015-03-09 17:41:09 -0700699 float x = r[0];
Sunny Goyalb8634152015-04-09 14:17:14 -0700700 int currentPage = mContent.getNextPage();
Sunny Goyal48461932015-03-09 17:41:09 -0700701
Sunny Goyalb8634152015-04-09 14:17:14 -0700702 float cellOverlap = mContent.getCurrentCellLayout().getCellWidth()
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700703 * ICON_OVERSCROLL_WIDTH_FACTOR;
704 boolean isOutsideLeftEdge = x < cellOverlap;
705 boolean isOutsideRightEdge = x > (getWidth() - cellOverlap);
Sunny Goyal48461932015-03-09 17:41:09 -0700706
Sunny Goyal70660032015-05-14 00:07:08 -0700707 if (currentPage > 0 && (mContent.mIsRtl ? isOutsideRightEdge : isOutsideLeftEdge)) {
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700708 showScrollHint(DragController.SCROLL_LEFT, d);
Sunny Goyalb8634152015-04-09 14:17:14 -0700709 } else if (currentPage < (mContent.getPageCount() - 1)
Sunny Goyal70660032015-05-14 00:07:08 -0700710 && (mContent.mIsRtl ? isOutsideLeftEdge : isOutsideRightEdge)) {
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700711 showScrollHint(DragController.SCROLL_RIGHT, d);
Sunny Goyal48461932015-03-09 17:41:09 -0700712 } else {
713 mOnScrollHintAlarm.cancelAlarm();
714 if (mScrollHintDir != DragController.SCROLL_NONE) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700715 mContent.clearScrollHint();
Sunny Goyal48461932015-03-09 17:41:09 -0700716 mScrollHintDir = DragController.SCROLL_NONE;
717 }
718 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700719 }
720
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700721 private void showScrollHint(int direction, DragObject d) {
722 // Show scroll hint on the right
723 if (mScrollHintDir != direction) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700724 mContent.showScrollHint(direction);
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700725 mScrollHintDir = direction;
726 }
727
728 // Set alarm for when the hint is complete
729 if (!mOnScrollHintAlarm.alarmPending() || mCurrentScrollDir != direction) {
730 mCurrentScrollDir = direction;
731 mOnScrollHintAlarm.cancelAlarm();
732 mOnScrollHintAlarm.setOnAlarmListener(new OnScrollHintListener(d));
733 mOnScrollHintAlarm.setAlarm(SCROLL_HINT_DURATION);
734
735 mReorderAlarm.cancelAlarm();
736 mTargetRank = mEmptyCellRank;
737 }
738 }
739
Adam Cohenbfbfd262011-06-13 16:55:12 -0700740 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
741 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700742 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700743 }
744 };
745
Adam Cohen95bb8002011-07-03 23:40:28 -0700746 public void completeDragExit() {
Sunny Goyalf4066152015-04-15 09:42:19 -0700747 if (mInfo.opened) {
748 mLauncher.closeFolder();
749 mRearrangeOnClose = true;
Sunny Goyal31abc292015-05-01 10:42:32 -0700750 } else if (mState == STATE_ANIMATING) {
751 mRearrangeOnClose = true;
Sunny Goyalf4066152015-04-15 09:42:19 -0700752 } else {
753 rearrangeChildren();
Sunny Goyal31abc292015-05-01 10:42:32 -0700754 clearDragInfo();
Sunny Goyalf4066152015-04-15 09:42:19 -0700755 }
Sunny Goyal31abc292015-05-01 10:42:32 -0700756 }
757
758 private void clearDragInfo() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700759 mCurrentDragInfo = null;
760 mCurrentDragView = null;
761 mSuppressOnAdd = false;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800762 mIsExternalDrag = false;
Adam Cohen3e8f8112011-07-02 18:03:00 -0700763 }
764
Adam Cohencb3382b2011-05-24 14:07:08 -0700765 public void onDragExit(DragObject d) {
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700766 // We only close the folder if this is a true drag exit, ie. not because
767 // a drop has occurred above the folder.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700768 if (!d.dragComplete) {
769 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
770 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
771 }
772 mReorderAlarm.cancelAlarm();
Sunny Goyal48461932015-03-09 17:41:09 -0700773
Sunny Goyalb8634152015-04-09 14:17:14 -0700774 mOnScrollHintAlarm.cancelAlarm();
775 mScrollPauseAlarm.cancelAlarm();
776 if (mScrollHintDir != DragController.SCROLL_NONE) {
777 mContent.clearScrollHint();
778 mScrollHintDir = DragController.SCROLL_NONE;
Sunny Goyal48461932015-03-09 17:41:09 -0700779 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700780 }
781
Sunny Goyale9b651e2015-04-24 11:44:51 -0700782 /**
783 * When performing an accessibility drop, onDrop is sent immediately after onDragEnter. So we
784 * need to complete all transient states based on timers.
785 */
786 @Override
787 public void prepareAccessibilityDrop() {
788 if (mReorderAlarm.alarmPending()) {
789 mReorderAlarm.cancelAlarm();
790 mReorderAlarmListener.onAlarm(mReorderAlarm);
791 }
792 }
793
Michael Jurka1e2f4652013-07-08 18:03:46 -0700794 public void onDropCompleted(final View target, final DragObject d,
795 final boolean isFlingToDelete, final boolean success) {
796 if (mDeferDropAfterUninstall) {
Michael Jurkaf3007582013-08-21 14:33:57 +0200797 Log.d(TAG, "Deferred handling drop because waiting for uninstall.");
Michael Jurka1e2f4652013-07-08 18:03:46 -0700798 mDeferredAction = new Runnable() {
799 public void run() {
800 onDropCompleted(target, d, isFlingToDelete, success);
801 mDeferredAction = null;
802 }
803 };
804 return;
805 }
806
807 boolean beingCalledAfterUninstall = mDeferredAction != null;
808 boolean successfulDrop =
809 success && (!beingCalledAfterUninstall || mUninstallSuccessful);
Winson Chung5f8afe62013-08-12 16:19:28 -0700810
Michael Jurka1e2f4652013-07-08 18:03:46 -0700811 if (successfulDrop) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800812 if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon && target != this) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700813 replaceFolderWithFinalItem();
814 }
815 } else {
816 // The drag failed, we need to return the item to the folder
Sunny Goyalb8c663c2015-04-23 11:43:48 -0700817 ShortcutInfo info = (ShortcutInfo) d.dragInfo;
818 View icon = (mCurrentDragView != null && mCurrentDragView.getTag() == info)
819 ? mCurrentDragView : mContent.createNewView(info);
820 ArrayList<View> views = getItemsInReadingOrder();
821 views.add(info.rank, icon);
822 mContent.arrangeChildren(views, views.size());
823 mItemsInvalidated = true;
824
825 mSuppressOnAdd = true;
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700826 mFolderIcon.onDrop(d);
Sunny Goyalb8c663c2015-04-23 11:43:48 -0700827 mSuppressOnAdd = false;
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700828 }
829
830 if (target != this) {
831 if (mOnExitAlarm.alarmPending()) {
832 mOnExitAlarm.cancelAlarm();
Michael Jurka54554252013-08-01 12:52:23 +0200833 if (!successfulDrop) {
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700834 mSuppressFolderDeletion = true;
835 }
Sunny Goyal5d85c442015-03-10 13:14:47 -0700836 mScrollPauseAlarm.cancelAlarm();
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700837 completeDragExit();
838 }
Adam Cohen9c58d822013-12-13 17:18:10 -0800839 }
840
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700841 mDeleteFolderOnDropCompleted = false;
842 mDragInProgress = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700843 mItemAddedBackToSelfViaIcon = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700844 mCurrentDragInfo = null;
845 mCurrentDragView = null;
846 mSuppressOnAdd = false;
Adam Cohen4045eb72011-10-06 11:44:26 -0700847
848 // Reordering may have occured, and we need to save the new item locations. We do this once
849 // at the end to prevent unnecessary database operations.
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700850 updateItemLocationsInDatabaseBatch();
Sunny Goyalb7e15ad2015-05-07 14:51:31 -0700851
852 // Use the item count to check for multi-page as the folder UI may not have
853 // been refreshed yet.
854 if (getItemCount() <= mContent.itemsPerPage()) {
855 // Show the animation, next time something is added to the folder.
856 mInfo.setOption(FolderInfo.FLAG_MULTI_PAGE_ANIMATION, false, mLauncher);
857 }
858
Adam Cohen4045eb72011-10-06 11:44:26 -0700859 }
860
Sunny Goyalfa401a12015-04-10 13:45:42 -0700861 @Override
Michael Jurka1e2f4652013-07-08 18:03:46 -0700862 public void deferCompleteDropAfterUninstallActivity() {
863 mDeferDropAfterUninstall = true;
864 }
865
Sunny Goyalfa401a12015-04-10 13:45:42 -0700866 @Override
Michael Jurka1e2f4652013-07-08 18:03:46 -0700867 public void onUninstallActivityReturned(boolean success) {
868 mDeferDropAfterUninstall = false;
869 mUninstallSuccessful = success;
870 if (mDeferredAction != null) {
871 mDeferredAction.run();
872 }
873 }
874
Winson Chunga48487a2012-03-20 16:19:37 -0700875 @Override
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800876 public float getIntrinsicIconScaleFactor() {
877 return 1f;
878 }
879
880 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800881 public boolean supportsFlingToDelete() {
882 return true;
883 }
884
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000885 @Override
886 public boolean supportsAppInfoDropTarget() {
887 return false;
888 }
889
890 @Override
891 public boolean supportsDeleteDropTarget() {
892 return true;
893 }
894
Sunny Goyalddec7342015-04-29 18:12:37 -0700895 @Override
896 public void onFlingToDelete(DragObject d, PointF vec) {
Winson Chunga48487a2012-03-20 16:19:37 -0700897 // Do nothing
898 }
899
900 @Override
901 public void onFlingToDeleteCompleted() {
902 // Do nothing
903 }
904
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700905 private void updateItemLocationsInDatabaseBatch() {
906 ArrayList<View> list = getItemsInReadingOrder();
907 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
908 for (int i = 0; i < list.size(); i++) {
909 View v = list.get(i);
910 ItemInfo info = (ItemInfo) v.getTag();
Sunny Goyal08f72612015-01-05 13:41:43 -0800911 info.rank = i;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700912 items.add(info);
913 }
914
915 LauncherModel.moveItemsInDatabase(mLauncher, items, mInfo.id, 0);
916 }
917
Adam Cohene25af792013-06-06 23:08:25 -0700918 public void addItemLocationsInDatabase() {
919 ArrayList<View> list = getItemsInReadingOrder();
920 for (int i = 0; i < list.size(); i++) {
921 View v = list.get(i);
922 ItemInfo info = (ItemInfo) v.getTag();
923 LauncherModel.addItemToDatabase(mLauncher, info, mInfo.id, 0,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700924 info.cellX, info.cellY);
Adam Cohene25af792013-06-06 23:08:25 -0700925 }
926 }
927
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700928 public void notifyDrop() {
929 if (mDragInProgress) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700930 mItemAddedBackToSelfViaIcon = true;
Adam Cohen76078c42011-06-09 15:06:52 -0700931 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700932 }
933
934 public boolean isDropEnabled() {
935 return true;
936 }
937
Adam Cohen2801caf2011-05-13 20:57:39 -0700938 public boolean isFull() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800939 return mContent.isFull();
Adam Cohen2801caf2011-05-13 20:57:39 -0700940 }
941
942 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700943 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700944
Winson Chung892c74d2013-08-22 16:15:50 -0700945 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700946 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700947 int height = getFolderHeight();
Adam Cohen2801caf2011-05-13 20:57:39 -0700948
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800949 float scale = parent.getDescendantRectRelativeToSelf(mFolderIcon, sTempRect);
Adam Cohen8e776a62011-06-28 18:10:06 -0700950
Adam Cohen2e6da152015-05-06 11:42:25 -0700951 DeviceProfile grid = mLauncher.getDeviceProfile();
Winson Chungaf40f202013-09-18 18:26:31 -0700952
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800953 int centerX = (int) (sTempRect.left + sTempRect.width() * scale / 2);
954 int centerY = (int) (sTempRect.top + sTempRect.height() * scale / 2);
Adam Cohen2801caf2011-05-13 20:57:39 -0700955 int centeredLeft = centerX - width / 2;
956 int centeredTop = centerY - height / 2;
Winson Chung3057b1c2013-10-10 17:35:35 -0700957 int currentPage = mLauncher.getWorkspace().getNextPage();
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800958 // In case the workspace is scrolling, we need to use the final scroll to compute
959 // the folders bounds.
960 mLauncher.getWorkspace().setFinalScrollForPageChange(currentPage);
Adam Cohen35e7e642011-07-17 14:47:18 -0700961 // We first fetch the currently visible CellLayoutChildren
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800962 CellLayout currentLayout = (CellLayout) mLauncher.getWorkspace().getChildAt(currentPage);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700963 ShortcutAndWidgetContainer boundingLayout = currentLayout.getShortcutsAndWidgets();
Adam Cohen35e7e642011-07-17 14:47:18 -0700964 Rect bounds = new Rect();
965 parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800966 // We reset the workspaces scroll
967 mLauncher.getWorkspace().resetFinalScrollForPageChange(currentPage);
Adam Cohen2801caf2011-05-13 20:57:39 -0700968
Adam Cohen35e7e642011-07-17 14:47:18 -0700969 // We need to bound the folder to the currently visible CellLayoutChildren
970 int left = Math.min(Math.max(bounds.left, centeredLeft),
971 bounds.left + bounds.width() - width);
972 int top = Math.min(Math.max(bounds.top, centeredTop),
973 bounds.top + bounds.height() - height);
Sunny Goyalc6205602015-05-21 20:46:33 -0700974 if (grid.isPhone && (grid.availableWidthPx - width) < grid.iconSizePx) {
Winson Chungaf40f202013-09-18 18:26:31 -0700975 // Center the folder if it is full (on phones only)
976 left = (grid.availableWidthPx - width) / 2;
977 } else if (width >= bounds.width()) {
978 // If the folder doesn't fit within the bounds, center it about the desired bounds
Adam Cohen35e7e642011-07-17 14:47:18 -0700979 left = bounds.left + (bounds.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700980 }
Adam Cohen35e7e642011-07-17 14:47:18 -0700981 if (height >= bounds.height()) {
982 top = bounds.top + (bounds.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700983 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700984
985 int folderPivotX = width / 2 + (centeredLeft - left);
986 int folderPivotY = height / 2 + (centeredTop - top);
987 setPivotX(folderPivotX);
988 setPivotY(folderPivotY);
Adam Cohen268c4752012-06-06 17:47:33 -0700989 mFolderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700990 (1.0f * folderPivotX / width));
Adam Cohen268c4752012-06-06 17:47:33 -0700991 mFolderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700992 (1.0f * folderPivotY / height));
Adam Cohen3bf84d32012-05-07 20:17:14 -0700993
Adam Cohen662b5982011-12-13 17:45:21 -0800994 lp.width = width;
995 lp.height = height;
996 lp.x = left;
997 lp.y = top;
Adam Cohen2801caf2011-05-13 20:57:39 -0700998 }
999
Adam Cohen268c4752012-06-06 17:47:33 -07001000 float getPivotXForIconAnimation() {
1001 return mFolderIconPivotX;
1002 }
1003 float getPivotYForIconAnimation() {
1004 return mFolderIconPivotY;
1005 }
1006
Winson Chung892c74d2013-08-22 16:15:50 -07001007 private int getContentAreaHeight() {
Adam Cohen2e6da152015-05-06 11:42:25 -07001008 DeviceProfile grid = mLauncher.getDeviceProfile();
Sunny Goyalc6205602015-05-21 20:46:33 -07001009 Rect workspacePadding = grid.getWorkspacePadding(mContent.mIsRtl);
Winson Chung892c74d2013-08-22 16:15:50 -07001010 int maxContentAreaHeight = grid.availableHeightPx -
Winson Chung892c74d2013-08-22 16:15:50 -07001011 workspacePadding.top - workspacePadding.bottom -
Sunny Goyal290800b2015-03-05 11:33:33 -08001012 mFooterHeight;
Adam Cohen1960ea42013-11-12 11:33:14 +00001013 int height = Math.min(maxContentAreaHeight,
Winson Chung892c74d2013-08-22 16:15:50 -07001014 mContent.getDesiredHeight());
Adam Cohen1960ea42013-11-12 11:33:14 +00001015 return Math.max(height, MIN_CONTENT_DIMEN);
1016 }
1017
1018 private int getContentAreaWidth() {
1019 return Math.max(mContent.getDesiredWidth(), MIN_CONTENT_DIMEN);
Winson Chung892c74d2013-08-22 16:15:50 -07001020 }
1021
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001022 private int getFolderHeight() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001023 return getFolderHeight(getContentAreaHeight());
1024 }
1025
1026 private int getFolderHeight(int contentAreaHeight) {
Sunny Goyal290800b2015-03-05 11:33:33 -08001027 return getPaddingTop() + getPaddingBottom() + contentAreaHeight + mFooterHeight;
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001028 }
Adam Cohen234c4cd2011-07-17 21:03:04 -07001029
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001030 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001031 int contentWidth = getContentAreaWidth();
1032 int contentHeight = getContentAreaHeight();
Adam Cohen1960ea42013-11-12 11:33:14 +00001033
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001034 int contentAreaWidthSpec = MeasureSpec.makeMeasureSpec(contentWidth, MeasureSpec.EXACTLY);
1035 int contentAreaHeightSpec = MeasureSpec.makeMeasureSpec(contentHeight, MeasureSpec.EXACTLY);
1036
1037 mContent.setFixedSize(contentWidth, contentHeight);
1038 mContentWrapper.measure(contentAreaWidthSpec, contentAreaHeightSpec);
Sunny Goyala07c2f52015-06-01 11:00:38 -07001039
1040 if (mContent.getChildCount() > 0) {
1041 int cellIconGap = (mContent.getPageAt(0).getCellWidth()
1042 - mLauncher.getDeviceProfile().iconSizePx) / 2;
1043 mFooter.setPadding(mContent.getPaddingLeft() + cellIconGap,
1044 mFooter.getPaddingTop(),
1045 mContent.getPaddingRight() + cellIconGap,
1046 mFooter.getPaddingBottom());
1047 }
Sunny Goyal290800b2015-03-05 11:33:33 -08001048 mFooter.measure(contentAreaWidthSpec,
1049 MeasureSpec.makeMeasureSpec(mFooterHeight, MeasureSpec.EXACTLY));
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001050
1051 int folderWidth = getPaddingLeft() + getPaddingRight() + contentWidth;
1052 int folderHeight = getFolderHeight(contentHeight);
1053 setMeasuredDimension(folderWidth, folderHeight);
Adam Cohen234c4cd2011-07-17 21:03:04 -07001054 }
1055
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001056 /**
1057 * Rearranges the children based on their rank.
1058 */
1059 public void rearrangeChildren() {
1060 rearrangeChildren(-1);
1061 }
Adam Cohen2801caf2011-05-13 20:57:39 -07001062
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001063 /**
1064 * Rearranges the children based on their rank.
1065 * @param itemCount if greater than the total children count, empty spaces are left at the end,
1066 * otherwise it is ignored.
1067 */
1068 public void rearrangeChildren(int itemCount) {
1069 ArrayList<View> views = getItemsInReadingOrder();
1070 mContent.arrangeChildren(views, Math.max(itemCount, views.size()));
Adam Cohen7c693212011-05-18 15:26:57 -07001071 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -07001072 }
1073
Sunny Goyalc4918352015-03-10 18:15:48 -07001074 // TODO remove this once GSA code fix is submitted
1075 public ViewGroup getContent() {
1076 return (ViewGroup) mContent;
1077 }
1078
Adam Cohena9cf38f2011-05-02 15:36:58 -07001079 public int getItemCount() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001080 return mContent.getItemCount();
Adam Cohena9cf38f2011-05-02 15:36:58 -07001081 }
1082
Adam Cohen091440a2015-03-18 14:16:05 -07001083 @Thunk void onCloseComplete() {
Adam Cohen05e0f402011-08-01 12:12:49 -07001084 DragLayer parent = (DragLayer) getParent();
Michael Jurka5649c282012-06-18 10:33:21 -07001085 if (parent != null) {
1086 parent.removeView(this);
1087 }
Adam Cohen4554ee12011-08-03 16:13:21 -07001088 mDragController.removeDropTarget((DropTarget) this);
Adam Cohen05e0f402011-08-01 12:12:49 -07001089 clearFocus();
Adam Cohenac56cff2011-09-28 20:45:37 -07001090 mFolderIcon.requestFocus();
Adam Cohen05e0f402011-08-01 12:12:49 -07001091
Adam Cohen2801caf2011-05-13 20:57:39 -07001092 if (mRearrangeOnClose) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001093 rearrangeChildren();
Adam Cohen2801caf2011-05-13 20:57:39 -07001094 mRearrangeOnClose = false;
1095 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001096 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001097 if (!mDragInProgress && !mSuppressFolderDeletion) {
1098 replaceFolderWithFinalItem();
1099 } else if (mDragInProgress) {
1100 mDeleteFolderOnDropCompleted = true;
1101 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001102 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001103 mSuppressFolderDeletion = false;
Sunny Goyal31abc292015-05-01 10:42:32 -07001104 clearDragInfo();
Adam Cohenafb01ee2011-06-23 15:38:03 -07001105 }
1106
Adam Cohen091440a2015-03-18 14:16:05 -07001107 @Thunk void replaceFolderWithFinalItem() {
Adam Cohenafb01ee2011-06-23 15:38:03 -07001108 // Add the last remaining child to the workspace in place of the folder
Adam Cohenfb91f302012-06-11 15:45:18 -07001109 Runnable onCompleteRunnable = new Runnable() {
1110 @Override
1111 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -07001112 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screenId);
Adam Cohenafb01ee2011-06-23 15:38:03 -07001113
Winson Chung33231f52013-12-09 16:57:45 -08001114 View child = null;
Adam Cohenfb91f302012-06-11 15:45:18 -07001115 // Move the item from the folder to the workspace, in the position of the folder
1116 if (getItemCount() == 1) {
1117 ShortcutInfo finalItem = mInfo.contents.get(0);
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001118 child = mLauncher.createShortcut(cellLayout, finalItem);
Adam Cohenfb91f302012-06-11 15:45:18 -07001119 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -07001120 mInfo.screenId, mInfo.cellX, mInfo.cellY);
Adam Cohenfb91f302012-06-11 15:45:18 -07001121 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001122 if (getItemCount() <= 1) {
1123 // Remove the folder
1124 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
Dan Sandler0eb687f2014-01-10 13:24:55 -05001125 if (cellLayout != null) {
1126 // b/12446428 -- sometimes the cell layout has already gone away?
1127 cellLayout.removeView(mFolderIcon);
1128 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001129 if (mFolderIcon instanceof DropTarget) {
1130 mDragController.removeDropTarget((DropTarget) mFolderIcon);
1131 }
1132 mLauncher.removeFolder(mInfo);
1133 }
Adam Cohenc5e63f32012-07-12 16:16:57 -07001134 // We add the child after removing the folder to prevent both from existing at
Winson Chung0e6a7132013-08-23 12:55:10 -07001135 // the same time in the CellLayout. We need to add the new item with addInScreenFromBind()
1136 // to ensure that hotseat items are placed correctly.
Adam Cohenc5e63f32012-07-12 16:16:57 -07001137 if (child != null) {
Winson Chung0e6a7132013-08-23 12:55:10 -07001138 mLauncher.getWorkspace().addInScreenFromBind(child, mInfo.container, mInfo.screenId,
Adam Cohenc5e63f32012-07-12 16:16:57 -07001139 mInfo.cellX, mInfo.cellY, mInfo.spanX, mInfo.spanY);
1140 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001141 }
1142 };
Sunny Goyalbc753352015-03-05 09:40:44 -08001143 View finalChild = mContent.getLastItem();
Adam Cohenfb91f302012-06-11 15:45:18 -07001144 if (finalChild != null) {
1145 mFolderIcon.performDestroyAnimation(finalChild, onCompleteRunnable);
Winson Chung33231f52013-12-09 16:57:45 -08001146 } else {
1147 onCompleteRunnable.run();
Adam Cohenafb01ee2011-06-23 15:38:03 -07001148 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001149 mDestroyed = true;
1150 }
1151
1152 boolean isDestroyed() {
1153 return mDestroyed;
Adam Cohen2801caf2011-05-13 20:57:39 -07001154 }
1155
Adam Cohenac56cff2011-09-28 20:45:37 -07001156 // This method keeps track of the last item in the folder for the purposes
1157 // of keyboard focus
Sunny Goyal290800b2015-03-05 11:33:33 -08001158 public void updateTextViewFocus() {
Sunny Goyalbc753352015-03-05 09:40:44 -08001159 View lastChild = mContent.getLastItem();
Adam Cohenac56cff2011-09-28 20:45:37 -07001160 if (lastChild != null) {
1161 mFolderName.setNextFocusDownId(lastChild.getId());
1162 mFolderName.setNextFocusRightId(lastChild.getId());
1163 mFolderName.setNextFocusLeftId(lastChild.getId());
1164 mFolderName.setNextFocusUpId(lastChild.getId());
1165 }
1166 }
1167
Adam Cohenbfbfd262011-06-13 16:55:12 -07001168 public void onDrop(DragObject d) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001169 Runnable cleanUpRunnable = null;
1170
Adam Cohen689ff162014-05-08 17:27:56 -07001171 // If we are coming from All Apps space, we defer removing the extra empty screen
1172 // until the folder closes
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001173 if (d.dragSource != mLauncher.getWorkspace() && !(d.dragSource instanceof Folder)) {
1174 cleanUpRunnable = new Runnable() {
1175 @Override
1176 public void run() {
Adam Cohen689ff162014-05-08 17:27:56 -07001177 mLauncher.exitSpringLoadedDragModeDelayed(true,
1178 Launcher.EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
1179 null);
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001180 }
1181 };
Adam Cohenbfbfd262011-06-13 16:55:12 -07001182 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001183
Sunny Goyalb8634152015-04-09 14:17:14 -07001184 // If the icon was dropped while the page was being scrolled, we need to compute
1185 // the target location again such that the icon is placed of the final page.
1186 if (!mContent.rankOnCurrentPage(mEmptyCellRank)) {
1187 // Reorder again.
1188 mTargetRank = getTargetRank(d, null);
Sunny Goyal48461932015-03-09 17:41:09 -07001189
Sunny Goyalb8634152015-04-09 14:17:14 -07001190 // Rearrange items immediately.
1191 mReorderAlarmListener.onAlarm(mReorderAlarm);
Sunny Goyal48461932015-03-09 17:41:09 -07001192
Sunny Goyalb8634152015-04-09 14:17:14 -07001193 mOnScrollHintAlarm.cancelAlarm();
1194 mScrollPauseAlarm.cancelAlarm();
Sunny Goyal48461932015-03-09 17:41:09 -07001195 }
Sunny Goyalb8634152015-04-09 14:17:14 -07001196 mContent.completePendingPageChanges();
Sunny Goyal48461932015-03-09 17:41:09 -07001197
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001198 View currentDragView;
1199 ShortcutInfo si = mCurrentDragInfo;
1200 if (mIsExternalDrag) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001201 currentDragView = mContent.createAndAddViewForRank(si, mEmptyCellRank);
Sunny Goyal95abbb32014-08-04 10:53:22 -07001202 // Actually move the item in the database if it was an external drag. Call this
1203 // before creating the view, so that ShortcutInfo is updated appropriately.
1204 LauncherModel.addOrMoveItemInDatabase(
1205 mLauncher, si, mInfo.id, 0, si.cellX, si.cellY);
1206
1207 // We only need to update the locations if it doesn't get handled in #onDropCompleted.
1208 if (d.dragSource != this) {
1209 updateItemLocationsInDatabaseBatch();
1210 }
1211 mIsExternalDrag = false;
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001212 } else {
1213 currentDragView = mCurrentDragView;
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001214 mContent.addViewForRank(currentDragView, si, mEmptyCellRank);
Adam Cohenbfbfd262011-06-13 16:55:12 -07001215 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001216
1217 if (d.dragView.hasDrawn()) {
1218
1219 // Temporarily reset the scale such that the animation target gets calculated correctly.
1220 float scaleX = getScaleX();
1221 float scaleY = getScaleY();
1222 setScaleX(1.0f);
1223 setScaleY(1.0f);
1224 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, currentDragView,
1225 cleanUpRunnable, null);
1226 setScaleX(scaleX);
1227 setScaleY(scaleY);
1228 } else {
1229 d.deferDragViewCleanupPostAnimation = false;
1230 currentDragView.setVisibility(VISIBLE);
1231 }
1232 mItemsInvalidated = true;
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001233 rearrangeChildren();
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001234
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001235 // Temporarily suppress the listener, as we did all the work already here.
1236 mSuppressOnAdd = true;
1237 mInfo.add(si);
1238 mSuppressOnAdd = false;
Sunny Goyal4b020172014-08-28 14:51:14 -07001239 // Clear the drag info, as it is no longer being dragged.
1240 mCurrentDragInfo = null;
Sunny Goyalf4066152015-04-15 09:42:19 -07001241 mDragInProgress = false;
Sunny Goyalb7e15ad2015-05-07 14:51:31 -07001242
1243 if (mContent.getPageCount() > 1) {
1244 // The animation has already been shown while opening the folder.
1245 mInfo.setOption(FolderInfo.FLAG_MULTI_PAGE_ANIMATION, true, mLauncher);
1246 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001247 }
1248
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001249 // This is used so the item doesn't immediately appear in the folder when added. In one case
1250 // we need to create the illusion that the item isn't added back to the folder yet, to
1251 // to correspond to the animation of the icon back into the folder. This is
1252 public void hideItem(ShortcutInfo info) {
1253 View v = getViewForInfo(info);
1254 v.setVisibility(INVISIBLE);
1255 }
1256 public void showItem(ShortcutInfo info) {
1257 View v = getViewForInfo(info);
1258 v.setVisibility(VISIBLE);
1259 }
1260
Sunny Goyalb7e15ad2015-05-07 14:51:31 -07001261 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -07001262 public void onAdd(ShortcutInfo item) {
Adam Cohen05e0f402011-08-01 12:12:49 -07001263 // If the item was dropped onto this open folder, we have done the work associated
1264 // with adding the item to the folder, as indicated by mSuppressOnAdd being set
Adam Cohenbfbfd262011-06-13 16:55:12 -07001265 if (mSuppressOnAdd) return;
Sunny Goyal5d85c442015-03-10 13:14:47 -07001266 mContent.createAndAddViewForRank(item, mContent.allocateRankForNewItem(item));
Sunny Goyal2e688a82015-03-18 10:23:39 -07001267 mItemsInvalidated = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001268 LauncherModel.addOrMoveItemInDatabase(
1269 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
1270 }
1271
Adam Cohena9cf38f2011-05-02 15:36:58 -07001272 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -07001273 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001274 // If this item is being dragged from this open folder, we have already handled
1275 // the work associated with removing the item, so we don't have to do anything here.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001276 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001277 View v = getViewForInfo(item);
Sunny Goyal290800b2015-03-05 11:33:33 -08001278 mContent.removeItem(v);
Adam Cohen2801caf2011-05-13 20:57:39 -07001279 if (mState == STATE_ANIMATING) {
1280 mRearrangeOnClose = true;
1281 } else {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001282 rearrangeChildren();
Adam Cohen2801caf2011-05-13 20:57:39 -07001283 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001284 if (getItemCount() <= 1) {
1285 replaceFolderWithFinalItem();
1286 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001287 }
Adam Cohen7c693212011-05-18 15:26:57 -07001288
Sunny Goyalbc753352015-03-05 09:40:44 -08001289 private View getViewForInfo(final ShortcutInfo item) {
1290 return mContent.iterateOverItems(new ItemOperator() {
1291
1292 @Override
1293 public boolean evaluate(ItemInfo info, View view, View parent) {
1294 return info == item;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001295 }
Sunny Goyalbc753352015-03-05 09:40:44 -08001296 });
Adam Cohendf1e4e82011-06-24 15:57:39 -07001297 }
1298
Adam Cohen76078c42011-06-09 15:06:52 -07001299 public void onItemsChanged() {
Adam Cohenac56cff2011-09-28 20:45:37 -07001300 updateTextViewFocus();
Adam Cohen76078c42011-06-09 15:06:52 -07001301 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001302
Adam Cohen76fc0852011-06-17 13:26:23 -07001303 public void onTitleChanged(CharSequence title) {
1304 }
Adam Cohen76078c42011-06-09 15:06:52 -07001305
Adam Cohen7c693212011-05-18 15:26:57 -07001306 public ArrayList<View> getItemsInReadingOrder() {
1307 if (mItemsInvalidated) {
1308 mItemsInReadingOrder.clear();
Sunny Goyalbc753352015-03-05 09:40:44 -08001309 mContent.iterateOverItems(new ItemOperator() {
1310
1311 @Override
1312 public boolean evaluate(ItemInfo info, View view, View parent) {
1313 mItemsInReadingOrder.add(view);
1314 return false;
Adam Cohen7c693212011-05-18 15:26:57 -07001315 }
Sunny Goyalbc753352015-03-05 09:40:44 -08001316 });
Adam Cohen7c693212011-05-18 15:26:57 -07001317 mItemsInvalidated = false;
1318 }
1319 return mItemsInReadingOrder;
1320 }
Adam Cohen8dfcba42011-07-07 16:38:18 -07001321
1322 public void getLocationInDragLayer(int[] loc) {
1323 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
1324 }
Adam Cohenea0818d2011-09-30 20:06:58 -07001325
1326 public void onFocusChange(View v, boolean hasFocus) {
1327 if (v == mFolderName && hasFocus) {
1328 startEditingFolderName();
1329 }
1330 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001331
1332 @Override
1333 public void getHitRectRelativeToDragLayer(Rect outRect) {
1334 getHitRect(outRect);
Sunny Goyal48461932015-03-09 17:41:09 -07001335 outRect.left -= mScrollAreaOffset;
1336 outRect.right += mScrollAreaOffset;
1337 }
1338
1339 private class OnScrollHintListener implements OnAlarmListener {
1340
1341 private final DragObject mDragObject;
1342
1343 OnScrollHintListener(DragObject object) {
1344 mDragObject = object;
1345 }
1346
1347 /**
1348 * Scroll hint has been shown long enough. Now scroll to appropriate page.
1349 */
1350 @Override
1351 public void onAlarm(Alarm alarm) {
1352 if (mCurrentScrollDir == DragController.SCROLL_LEFT) {
Sunny Goyalb8634152015-04-09 14:17:14 -07001353 mContent.scrollLeft();
Sunny Goyal48461932015-03-09 17:41:09 -07001354 mScrollHintDir = DragController.SCROLL_NONE;
1355 } else if (mCurrentScrollDir == DragController.SCROLL_RIGHT) {
Sunny Goyalb8634152015-04-09 14:17:14 -07001356 mContent.scrollRight();
Sunny Goyal48461932015-03-09 17:41:09 -07001357 mScrollHintDir = DragController.SCROLL_NONE;
1358 } else {
1359 // This should not happen
1360 return;
1361 }
1362 mCurrentScrollDir = DragController.SCROLL_NONE;
1363
1364 // Pause drag event until the scrolling is finished
1365 mScrollPauseAlarm.setOnAlarmListener(new OnScrollFinishedListener(mDragObject));
1366 mScrollPauseAlarm.setAlarm(DragController.RESCROLL_DELAY);
1367 }
1368 }
1369
1370 private class OnScrollFinishedListener implements OnAlarmListener {
1371
1372 private final DragObject mDragObject;
1373
1374 OnScrollFinishedListener(DragObject object) {
1375 mDragObject = object;
1376 }
1377
1378 /**
1379 * Page scroll is complete.
1380 */
1381 @Override
1382 public void onAlarm(Alarm alarm) {
1383 // Reorder immediately on page change.
1384 onDragOver(mDragObject, 1);
1385 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001386 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001387}