blob: ec4ea044c683b7183c1b5a157069df60f7201698 [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;
Adam Cohen76fc0852011-06-17 13:26:23 -070047import android.view.inputmethod.EditorInfo;
48import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.widget.LinearLayout;
Adam Cohendf2cc412011-04-27 16:56:57 -070050import android.widget.TextView;
Sunny Goyal83a8f042015-05-19 12:52:12 -070051
Sunny Goyale9b651e2015-04-24 11:44:51 -070052import com.android.launcher3.CellLayout.CellInfo;
Sunny Goyalf4066152015-04-15 09:42:19 -070053import com.android.launcher3.DragController.DragListener;
Daniel Sandler325dc232013-06-05 22:57:57 -040054import com.android.launcher3.FolderInfo.FolderListener;
Sunny Goyalfa401a12015-04-10 13:45:42 -070055import com.android.launcher3.UninstallDropTarget.UninstallSource;
Sunny Goyalbc753352015-03-05 09:40:44 -080056import com.android.launcher3.Workspace.ItemOperator;
Sunny Goyal83a8f042015-05-19 12:52:12 -070057import com.android.launcher3.accessibility.LauncherAccessibilityDelegate.AccessibilityDragSource;
Adam Cohen091440a2015-03-18 14:16:05 -070058import com.android.launcher3.util.Thunk;
Sunny Goyal83a8f042015-05-19 12:52:12 -070059
Adam Cohenc0dcf592011-06-01 15:30:43 -070060import java.util.ArrayList;
Adam Cohen3bf84d32012-05-07 20:17:14 -070061import java.util.Collections;
Adam Cohenc0dcf592011-06-01 15:30:43 -070062
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063/**
64 * Represents a set of icons chosen by the user or generated by the system.
65 */
Adam Cohen8dfcba42011-07-07 16:38:18 -070066public class Folder extends LinearLayout implements DragSource, View.OnClickListener,
Adam Cohenea0818d2011-09-30 20:06:58 -070067 View.OnLongClickListener, DropTarget, FolderListener, TextView.OnEditorActionListener,
Sunny Goyale9b651e2015-04-24 11:44:51 -070068 View.OnFocusChangeListener, DragListener, UninstallSource, AccessibilityDragSource {
Adam Cohendf2cc412011-04-27 16:56:57 -070069 private static final String TAG = "Launcher.Folder";
70
Sunny Goyalc3a609f2015-02-26 17:43:50 -080071 /**
72 * We avoid measuring {@link #mContentWrapper} with a 0 width or height, as this
73 * results in CellLayout being measured as UNSPECIFIED, which it does not support.
74 */
75 private static final int MIN_CONTENT_DIMEN = 5;
Adam Cohen4eac29a2011-07-11 17:53:37 -070076
Adam Cohendf2cc412011-04-27 16:56:57 -070077 static final int STATE_NONE = -1;
78 static final int STATE_SMALL = 0;
79 static final int STATE_ANIMATING = 1;
80 static final int STATE_OPEN = 2;
81
Sunny Goyal48461932015-03-09 17:41:09 -070082 /**
Sunny Goyal48461932015-03-09 17:41:09 -070083 * Time for which the scroll hint is shown before automatically changing page.
84 */
85 public static final int SCROLL_HINT_DURATION = DragController.SCROLL_DELAY;
86
87 /**
88 * Fraction of icon width which behave as scroll region.
89 */
90 private static final float ICON_OVERSCROLL_WIDTH_FACTOR = 0.45f;
91
Sunny Goyalb7e15ad2015-05-07 14:51:31 -070092 public static final int FOOTER_ANIMATION_DURATION = 200;
93
Adam Cohenf0f4eda2013-06-06 21:27:03 -070094 private static final int REORDER_DELAY = 250;
Adam Cohen5d518fa2013-12-05 14:16:23 -080095 private static final int ON_EXIT_CLOSE_DELAY = 400;
Sunny Goyalc3a609f2015-02-26 17:43:50 -080096 private static final Rect sTempRect = new Rect();
97
98 private static String sDefaultFolderName;
99 private static String sHintText;
100
101 private final Alarm mReorderAlarm = new Alarm();
102 private final Alarm mOnExitAlarm = new Alarm();
Sunny Goyalb8634152015-04-09 14:17:14 -0700103 private final Alarm mOnScrollHintAlarm = new Alarm();
104 @Thunk final Alarm mScrollPauseAlarm = new Alarm();
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800105
Adam Cohen091440a2015-03-18 14:16:05 -0700106 @Thunk final ArrayList<View> mItemsInReadingOrder = new ArrayList<View>();
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800107
108 private final int mExpandDuration;
109 private final int mMaterialExpandDuration;
110 private final int mMaterialExpandStagger;
111
112 private final InputMethodManager mInputMethodManager;
113
114 protected final Launcher mLauncher;
115 protected DragController mDragController;
116 protected FolderInfo mInfo;
117
Adam Cohen091440a2015-03-18 14:16:05 -0700118 @Thunk FolderIcon mFolderIcon;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800119
Sunny Goyalb8634152015-04-09 14:17:14 -0700120 @Thunk FolderPagedView mContent;
Adam Cohen091440a2015-03-18 14:16:05 -0700121 @Thunk View mContentWrapper;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800122 FolderEditText mFolderName;
123
Sunny Goyal290800b2015-03-05 11:33:33 -0800124 private View mFooter;
125 private int mFooterHeight;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800126
127 // Cell ranks used for drag and drop
Adam Cohen091440a2015-03-18 14:16:05 -0700128 @Thunk int mTargetRank, mPrevTargetRank, mEmptyCellRank;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800129
Adam Cohen091440a2015-03-18 14:16:05 -0700130 @Thunk int mState = STATE_NONE;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800131 private boolean mRearrangeOnClose = false;
Adam Cohen7c693212011-05-18 15:26:57 -0700132 boolean mItemsInvalidated = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700133 private ShortcutInfo mCurrentDragInfo;
134 private View mCurrentDragView;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800135 private boolean mIsExternalDrag;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700136 boolean mSuppressOnAdd = false;
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700137 private boolean mDragInProgress = false;
138 private boolean mDeleteFolderOnDropCompleted = false;
139 private boolean mSuppressFolderDeletion = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700140 private boolean mItemAddedBackToSelfViaIcon = false;
Adam Cohen091440a2015-03-18 14:16:05 -0700141 @Thunk float mFolderIconPivotX;
142 @Thunk float mFolderIconPivotY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700143 private boolean mIsEditingName = false;
Adam Cohen1960ea42013-11-12 11:33:14 +0000144
Adam Cohenfb91f302012-06-11 15:45:18 -0700145 private boolean mDestroyed;
146
Adam Cohen091440a2015-03-18 14:16:05 -0700147 @Thunk Runnable mDeferredAction;
Michael Jurka1e2f4652013-07-08 18:03:46 -0700148 private boolean mDeferDropAfterUninstall;
149 private boolean mUninstallSuccessful;
150
Sunny Goyal48461932015-03-09 17:41:09 -0700151 // Folder scrolling
152 private int mScrollAreaOffset;
Sunny Goyal48461932015-03-09 17:41:09 -0700153
Adam Cohen091440a2015-03-18 14:16:05 -0700154 @Thunk int mScrollHintDir = DragController.SCROLL_NONE;
155 @Thunk int mCurrentScrollDir = DragController.SCROLL_NONE;
Sunny Goyal48461932015-03-09 17:41:09 -0700156
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157 /**
158 * Used to inflate the Workspace from XML.
159 *
160 * @param context The application's context.
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800161 * @param attrs The attributes set containing the Workspace's customization values.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162 */
163 public Folder(Context context, AttributeSet attrs) {
164 super(context, attrs);
165 setAlwaysDrawnWithCacheEnabled(false);
Adam Cohen76fc0852011-06-17 13:26:23 -0700166 mInputMethodManager = (InputMethodManager)
Michael Jurka8b805b12012-04-18 14:23:14 -0700167 getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
Adam Cohen76fc0852011-06-17 13:26:23 -0700168
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800169 Resources res = getResources();
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700170 mExpandDuration = res.getInteger(R.integer.config_folderExpandDuration);
171 mMaterialExpandDuration = res.getInteger(R.integer.config_materialFolderExpandDuration);
172 mMaterialExpandStagger = res.getInteger(R.integer.config_materialFolderExpandStagger);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700173
174 if (sDefaultFolderName == null) {
175 sDefaultFolderName = res.getString(R.string.folder_name);
176 }
Adam Cohena65beee2011-06-27 21:32:23 -0700177 if (sHintText == null) {
178 sHintText = res.getString(R.string.folder_hint_text);
179 }
Adam Cohen4eac29a2011-07-11 17:53:37 -0700180 mLauncher = (Launcher) context;
Adam Cohenac56cff2011-09-28 20:45:37 -0700181 // We need this view to be focusable in touch mode so that when text editing of the folder
182 // name is complete, we have something to focus on, thus hiding the cursor and giving
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800183 // reliable behavior when clicking the text field (since it will always gain focus on click).
Adam Cohenac56cff2011-09-28 20:45:37 -0700184 setFocusableInTouchMode(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185 }
186
187 @Override
188 protected void onFinishInflate() {
189 super.onFinishInflate();
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800190 mContentWrapper = findViewById(R.id.folder_content_wrapper);
Sunny Goyalb8634152015-04-09 14:17:14 -0700191 mContent = (FolderPagedView) findViewById(R.id.folder_content);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800192 mContent.setFolder(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700193
Adam Cohenac56cff2011-09-28 20:45:37 -0700194 mFolderName = (FolderEditText) findViewById(R.id.folder_name);
195 mFolderName.setFolder(this);
Adam Cohenea0818d2011-09-30 20:06:58 -0700196 mFolderName.setOnFocusChangeListener(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700197
Adam Cohen76fc0852011-06-17 13:26:23 -0700198 // We disable action mode for now since it messes up the view on phones
199 mFolderName.setCustomSelectionActionModeCallback(mActionModeCallback);
Adam Cohen76fc0852011-06-17 13:26:23 -0700200 mFolderName.setOnEditorActionListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700201 mFolderName.setSelectAllOnFocus(true);
Adam Cohen7a14d0b2011-06-24 11:36:35 -0700202 mFolderName.setInputType(mFolderName.getInputType() |
203 InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800204
Sunny Goyalb8634152015-04-09 14:17:14 -0700205 mFooter = findViewById(R.id.folder_footer);
Sunny Goyalb8634152015-04-09 14:17:14 -0700206
Sunny Goyal290800b2015-03-05 11:33:33 -0800207 // We find out how tall footer wants to be (it is set to wrap_content), so that
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800208 // we can allocate the appropriate amount of space for it.
209 int measureSpec = MeasureSpec.UNSPECIFIED;
Sunny Goyal290800b2015-03-05 11:33:33 -0800210 mFooter.measure(measureSpec, measureSpec);
211 mFooterHeight = mFooter.getMeasuredHeight();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800212 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700213
Adam Cohen76fc0852011-06-17 13:26:23 -0700214 private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
215 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
216 return false;
217 }
218
219 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
220 return false;
221 }
222
223 public void onDestroyActionMode(ActionMode mode) {
224 }
225
226 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
227 return false;
228 }
229 };
230
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800231 public void onClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700232 Object tag = v.getTag();
233 if (tag instanceof ShortcutInfo) {
Adam Cohenb5fe60c2013-06-06 22:03:51 -0700234 mLauncher.onClick(v);
Adam Cohendf2cc412011-04-27 16:56:57 -0700235 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800236 }
237
238 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -0700239 // Return if global dragging is not enabled
240 if (!mLauncher.isDraggingEnabled()) return true;
Sunny Goyale9b651e2015-04-24 11:44:51 -0700241 return beginDrag(v, false);
242 }
Winson Chung36a62fe2012-05-06 18:04:42 -0700243
Sunny Goyale9b651e2015-04-24 11:44:51 -0700244 private boolean beginDrag(View v, boolean accessible) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700245 Object tag = v.getTag();
246 if (tag instanceof ShortcutInfo) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700247 ShortcutInfo item = (ShortcutInfo) tag;
248 if (!v.isInTouchMode()) {
249 return false;
250 }
251
Sunny Goyale9b651e2015-04-24 11:44:51 -0700252 mLauncher.getWorkspace().beginDragShared(v, new Point(), this, accessible);
Adam Cohen76078c42011-06-09 15:06:52 -0700253
254 mCurrentDragInfo = item;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800255 mEmptyCellRank = item.rank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700256 mCurrentDragView = v;
Adam Cohenfc53cd22011-07-20 15:45:11 -0700257
Sunny Goyal290800b2015-03-05 11:33:33 -0800258 mContent.removeItem(mCurrentDragView);
Adam Cohenfc53cd22011-07-20 15:45:11 -0700259 mInfo.remove(mCurrentDragInfo);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700260 mDragInProgress = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700261 mItemAddedBackToSelfViaIcon = false;
Adam Cohendf2cc412011-04-27 16:56:57 -0700262 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800263 return true;
264 }
265
Sunny Goyale9b651e2015-04-24 11:44:51 -0700266 @Override
267 public void startDrag(CellInfo cellInfo, boolean accessible) {
268 beginDrag(cellInfo.cell, accessible);
269 }
270
271 @Override
272 public void enableAccessibleDrag(boolean enable) {
273 mLauncher.getSearchBar().enableAccessibleDrag(enable);
274 for (int i = 0; i < mContent.getChildCount(); i++) {
275 mContent.getPageAt(i).enableAccessibleDrag(enable, CellLayout.FOLDER_ACCESSIBILITY_DRAG);
276 }
Sunny Goyalccc414b2015-04-30 12:28:16 -0700277
278 mFooter.setImportantForAccessibility(enable ? IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS :
279 IMPORTANT_FOR_ACCESSIBILITY_AUTO);
Sunny Goyale9b651e2015-04-24 11:44:51 -0700280 mLauncher.getWorkspace().setAddNewPageOnDrag(!enable);
281 }
282
Adam Cohen76fc0852011-06-17 13:26:23 -0700283 public boolean isEditingName() {
284 return mIsEditingName;
285 }
286
287 public void startEditingFolderName() {
Adam Cohena65beee2011-06-27 21:32:23 -0700288 mFolderName.setHint("");
Adam Cohen76fc0852011-06-17 13:26:23 -0700289 mIsEditingName = true;
290 }
291
292 public void dismissEditingName() {
293 mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
294 doneEditingFolderName(true);
295 }
296
297 public void doneEditingFolderName(boolean commit) {
Adam Cohena65beee2011-06-27 21:32:23 -0700298 mFolderName.setHint(sHintText);
Adam Cohen1df26a32011-08-12 16:15:23 -0700299 // Convert to a string here to ensure that no other state associated with the text field
300 // gets saved.
Winson Chung82b016c2015-05-08 17:00:10 -0700301 CharSequence newTitle = mFolderName.getText();
Adam Cohen3371da02011-10-25 21:38:29 -0700302 mInfo.setTitle(newTitle);
Adam Cohen76fc0852011-06-17 13:26:23 -0700303 LauncherModel.updateItemInDatabase(mLauncher, mInfo);
Adam Cohenac56cff2011-09-28 20:45:37 -0700304
Adam Cohen3371da02011-10-25 21:38:29 -0700305 if (commit) {
306 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Winson Chung82b016c2015-05-08 17:00:10 -0700307 String.format(getContext().getString(R.string.folder_renamed),
308 newTitle.toString()));
Adam Cohen3371da02011-10-25 21:38:29 -0700309 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700310 // In order to clear the focus from the text field, we set the focus on ourself. This
311 // ensures that every time the field is clicked, focus is gained, giving reliable behavior.
312 requestFocus();
313
Adam Cohene601a432011-07-26 21:51:30 -0700314 Selection.setSelection((Spannable) mFolderName.getText(), 0, 0);
Adam Cohen76fc0852011-06-17 13:26:23 -0700315 mIsEditingName = false;
316 }
317
318 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
319 if (actionId == EditorInfo.IME_ACTION_DONE) {
320 dismissEditingName();
321 return true;
322 }
323 return false;
324 }
325
326 public View getEditTextRegion() {
327 return mFolderName;
328 }
329
Adam Cohen0c872ba2011-05-05 10:34:16 -0700330 /**
331 * We need to handle touch events to prevent them from falling through to the workspace below.
332 */
Sunny Goyal70660032015-05-14 00:07:08 -0700333 @SuppressLint("ClickableViewAccessibility")
Adam Cohen0c872ba2011-05-05 10:34:16 -0700334 @Override
335 public boolean onTouchEvent(MotionEvent ev) {
336 return true;
337 }
338
Joe Onorato00acb122009-08-04 16:04:30 -0400339 public void setDragController(DragController dragController) {
340 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800341 }
342
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800343 public void setFolderIcon(FolderIcon icon) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700344 mFolderIcon = icon;
345 }
346
Adam Cohen3371da02011-10-25 21:38:29 -0700347 @Override
348 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
349 // When the folder gets focus, we don't want to announce the list of items.
350 return true;
351 }
352
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800353 /**
354 * @return the FolderInfo object associated with this folder
355 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700356 public FolderInfo getInfo() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800357 return mInfo;
358 }
359
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800360 void bind(FolderInfo info) {
361 mInfo = info;
Adam Cohendf2cc412011-04-27 16:56:57 -0700362 ArrayList<ShortcutInfo> children = info.contents;
Sunny Goyal08f72612015-01-05 13:41:43 -0800363 Collections.sort(children, Utilities.RANK_COMPARATOR);
Sunny Goyal08f72612015-01-05 13:41:43 -0800364
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800365 ArrayList<ShortcutInfo> overflow = mContent.bindItems(children);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700366
Jason Monk4ff73882014-04-24 16:48:07 -0400367 // If our folder has too many items we prune them from the list. This is an issue
Adam Cohenc508b2d2011-06-28 14:41:44 -0700368 // when upgrading from the old Folders implementation which could contain an unlimited
369 // number of items.
370 for (ShortcutInfo item: overflow) {
371 mInfo.remove(item);
372 LauncherModel.deleteItemFromDatabase(mLauncher, item);
373 }
374
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800375 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
376 if (lp == null) {
377 lp = new DragLayer.LayoutParams(0, 0);
378 lp.customPosition = true;
379 setLayoutParams(lp);
380 }
381 centerAboutIcon();
382
Adam Cohen4dbe6d92011-05-18 17:14:15 -0700383 mItemsInvalidated = true;
Adam Cohenac56cff2011-09-28 20:45:37 -0700384 updateTextViewFocus();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700385 mInfo.addListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700386
Adam Cohenafb01ee2011-06-23 15:38:03 -0700387 if (!sDefaultFolderName.contentEquals(mInfo.title)) {
Adam Cohen4ef610f2011-06-21 22:37:36 -0700388 mFolderName.setText(mInfo.title);
389 } else {
390 mFolderName.setText("");
391 }
Winson Chung33231f52013-12-09 16:57:45 -0800392
393 // In case any children didn't come across during loading, clean up the folder accordingly
394 mFolderIcon.post(new Runnable() {
395 public void run() {
396 if (getItemCount() <= 1) {
397 replaceFolderWithFinalItem();
398 }
399 }
400 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700401 }
402
403 /**
404 * Creates a new UserFolder, inflated from R.layout.user_folder.
405 *
406 * @param context The application's context.
407 *
408 * @return A new UserFolder.
409 */
Sunny Goyalc23da842015-05-14 20:44:01 -0700410 @SuppressLint("InflateParams")
Sunny Goyal70660032015-05-14 00:07:08 -0700411 static Folder fromXml(Launcher launcher) {
Sunny Goyalc23da842015-05-14 20:44:01 -0700412 return (Folder) launcher.getLayoutInflater().inflate(R.layout.user_folder, null);
Adam Cohendf2cc412011-04-27 16:56:57 -0700413 }
414
415 /**
416 * This method is intended to make the UserFolder to be visually identical in size and position
417 * to its associated FolderIcon. This allows for a seamless transition into the expanded state.
418 */
419 private void positionAndSizeAsIcon() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700420 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800421 setScaleX(0.8f);
422 setScaleY(0.8f);
423 setAlpha(0f);
Adam Cohendf2cc412011-04-27 16:56:57 -0700424 mState = STATE_SMALL;
425 }
426
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700427 private void prepareReveal() {
428 setScaleX(1f);
429 setScaleY(1f);
430 setAlpha(1f);
431 mState = STATE_SMALL;
432 }
433
Adam Cohendf2cc412011-04-27 16:56:57 -0700434 public void animateOpen() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700435 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen6441de02011-12-14 14:25:32 -0800436
Sunny Goyalb8634152015-04-09 14:17:14 -0700437 mContent.completePendingPageChanges();
Sunny Goyal8167dc22015-04-27 13:44:01 -0700438 if (!mDragInProgress) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700439 // Open on the first page.
440 mContent.snapToPageImmediately(0);
Sunny Goyal48461932015-03-09 17:41:09 -0700441 }
442
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700443 Animator openFolderAnim = null;
444 final Runnable onCompleteRunnable;
Kenny Guyd794a3f2014-09-16 15:17:58 +0100445 if (!Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700446 positionAndSizeAsIcon();
447 centerAboutIcon();
448
449 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1);
450 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
451 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
452 final ObjectAnimator oa =
453 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
454 oa.setDuration(mExpandDuration);
455 openFolderAnim = oa;
456
457 setLayerType(LAYER_TYPE_HARDWARE, null);
458 onCompleteRunnable = new Runnable() {
459 @Override
460 public void run() {
461 setLayerType(LAYER_TYPE_NONE, null);
462 }
463 };
464 } else {
465 prepareReveal();
466 centerAboutIcon();
467
468 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
469 int height = getFolderHeight();
470
471 float transX = - 0.075f * (width / 2 - getPivotX());
472 float transY = - 0.075f * (height / 2 - getPivotY());
473 setTranslationX(transX);
474 setTranslationY(transY);
475 PropertyValuesHolder tx = PropertyValuesHolder.ofFloat("translationX", transX, 0);
476 PropertyValuesHolder ty = PropertyValuesHolder.ofFloat("translationY", transY, 0);
477
Sunny Goyal75deaf32015-05-07 16:13:12 -0700478 Animator drift = LauncherAnimUtils.ofPropertyValuesHolder(this, tx, ty);
479 drift.setDuration(mMaterialExpandDuration);
480 drift.setStartDelay(mMaterialExpandStagger);
481 drift.setInterpolator(new LogDecelerateInterpolator(100, 0));
482
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700483 int rx = (int) Math.max(Math.max(width - getPivotX(), 0), getPivotX());
484 int ry = (int) Math.max(Math.max(height - getPivotY(), 0), getPivotY());
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700485 float radius = (float) Math.hypot(rx, ry);
Sunny Goyal75deaf32015-05-07 16:13:12 -0700486
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700487 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
488 Animator reveal = LauncherAnimUtils.createCircularReveal(this, (int) getPivotX(),
489 (int) getPivotY(), 0, radius);
490 reveal.setDuration(mMaterialExpandDuration);
491 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
492
Sunny Goyalbc753352015-03-05 09:40:44 -0800493 mContentWrapper.setAlpha(0f);
494 Animator iconsAlpha = LauncherAnimUtils.ofFloat(mContentWrapper, "alpha", 0f, 1f);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700495 iconsAlpha.setDuration(mMaterialExpandDuration);
496 iconsAlpha.setStartDelay(mMaterialExpandStagger);
497 iconsAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
498
Sunny Goyal290800b2015-03-05 11:33:33 -0800499 mFooter.setAlpha(0f);
500 Animator textAlpha = LauncherAnimUtils.ofFloat(mFooter, "alpha", 0f, 1f);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700501 textAlpha.setDuration(mMaterialExpandDuration);
502 textAlpha.setStartDelay(mMaterialExpandStagger);
503 textAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
504
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700505
506 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());
548 mFolderName.setTranslationX((footerWidth - textWidth) / 2);
549 mContent.setMarkerScale(0);
550
551 // Do not update the flag if we are in drag mode. The flag will be updated, when we
552 // actually drop the icon.
553 final boolean updateAnimationFlag = !mDragInProgress;
554 openFolderAnim.addListener(new AnimatorListenerAdapter() {
555
556 @Override
557 public void onAnimationEnd(Animator animation) {
558 mFolderName.animate().setDuration(FOOTER_ANIMATION_DURATION).translationX(0);
559 mContent.animateMarkers();
560
561 if (updateAnimationFlag) {
562 mInfo.setOption(FolderInfo.FLAG_MULTI_PAGE_ANIMATION, true, mLauncher);
563 }
564 }
565 });
566 } else {
567 mFolderName.setTranslationX(0);
568 mContent.setMarkerScale(1);
569 }
570
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700571 openFolderAnim.start();
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800572
573 // Make sure the folder picks up the last drag move even if the finger doesn't move.
574 if (mDragController.isDragging()) {
575 mDragController.forceTouchMove();
576 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700577
Sunny Goyalb8634152015-04-09 14:17:14 -0700578 FolderPagedView pages = (FolderPagedView) mContent;
579 pages.verifyVisibleHighResIcons(pages.getNextPage());
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800580 }
581
582 public void beginExternalDrag(ShortcutInfo item) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800583 mCurrentDragInfo = item;
Sunny Goyal5d85c442015-03-10 13:14:47 -0700584 mEmptyCellRank = mContent.allocateRankForNewItem(item);
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800585 mIsExternalDrag = true;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800586 mDragInProgress = true;
Sunny Goyalb8634152015-04-09 14:17:14 -0700587
Sunny Goyalf4066152015-04-15 09:42:19 -0700588 // Since this folder opened by another controller, it might not get onDrop or
589 // onDropComplete. Perform cleanup once drag-n-drop ends.
590 mDragController.addDragListener(this);
591 }
592
593 @Override
594 public void onDragStart(DragSource source, Object info, int dragAction) { }
595
596 @Override
597 public void onDragEnd() {
598 if (mIsExternalDrag && mDragInProgress) {
599 completeDragExit();
600 }
601 mDragController.removeDragListener(this);
Adam Cohendf2cc412011-04-27 16:56:57 -0700602 }
603
Adam Cohen091440a2015-03-18 14:16:05 -0700604 @Thunk void sendCustomAccessibilityEvent(int type, String text) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700605 AccessibilityManager accessibilityManager = (AccessibilityManager)
606 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
607 if (accessibilityManager.isEnabled()) {
Adam Cohen3371da02011-10-25 21:38:29 -0700608 AccessibilityEvent event = AccessibilityEvent.obtain(type);
609 onInitializeAccessibilityEvent(event);
610 event.getText().add(text);
Michael Jurka8b805b12012-04-18 14:23:14 -0700611 accessibilityManager.sendAccessibilityEvent(event);
Adam Cohen3371da02011-10-25 21:38:29 -0700612 }
613 }
614
Adam Cohendf2cc412011-04-27 16:56:57 -0700615 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700616 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800617 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
618 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
619 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
Michael Jurka032e6ba2013-04-22 15:08:42 +0200620 final ObjectAnimator oa =
Michael Jurka2ecf9952012-06-18 12:52:28 -0700621 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohendf2cc412011-04-27 16:56:57 -0700622
Adam Cohen2801caf2011-05-13 20:57:39 -0700623 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700624 @Override
625 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700626 onCloseComplete();
Michael Jurka0121c3e2012-05-31 08:36:04 -0700627 setLayerType(LAYER_TYPE_NONE, null);
Adam Cohen2801caf2011-05-13 20:57:39 -0700628 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700629 }
630 @Override
631 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700632 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700633 getContext().getString(R.string.folder_closed));
Adam Cohendf2cc412011-04-27 16:56:57 -0700634 mState = STATE_ANIMATING;
635 }
636 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700637 oa.setDuration(mExpandDuration);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700638 setLayerType(LAYER_TYPE_HARDWARE, null);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100639 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700640 }
641
Adam Cohencb3382b2011-05-24 14:07:08 -0700642 public boolean acceptDrop(DragObject d) {
643 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700644 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700645 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
646 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
647 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700648 }
649
Adam Cohencb3382b2011-05-24 14:07:08 -0700650 public void onDragEnter(DragObject d) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800651 mPrevTargetRank = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700652 mOnExitAlarm.cancelAlarm();
Sunny Goyalb8634152015-04-09 14:17:14 -0700653 // Get the area offset such that the folder only closes if half the drag icon width
654 // is outside the folder area
655 mScrollAreaOffset = d.dragView.getDragRegionWidth() / 2 - d.xOffset;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700656 }
657
658 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
659 public void onAlarm(Alarm alarm) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800660 mContent.realTimeReorder(mEmptyCellRank, mTargetRank);
661 mEmptyCellRank = mTargetRank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700662 }
663 };
664
Sunny Goyalc46bfef2015-01-05 12:40:08 -0800665 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohen2374abf2013-04-16 14:56:57 -0700666 public boolean isLayoutRtl() {
667 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
668 }
669
Sunny Goyal48461932015-03-09 17:41:09 -0700670 @Override
Adam Cohencb3382b2011-05-24 14:07:08 -0700671 public void onDragOver(DragObject d) {
Sunny Goyal48461932015-03-09 17:41:09 -0700672 onDragOver(d, REORDER_DELAY);
673 }
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700674
Sunny Goyal48461932015-03-09 17:41:09 -0700675 private int getTargetRank(DragObject d, float[] recycle) {
676 recycle = d.getVisualCenter(recycle);
677 return mContent.findNearestArea(
678 (int) recycle[0] - getPaddingLeft(), (int) recycle[1] - getPaddingTop());
679 }
680
Adam Cohen091440a2015-03-18 14:16:05 -0700681 @Thunk void onDragOver(DragObject d, int reorderDelay) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700682 if (mScrollPauseAlarm.alarmPending()) {
Sunny Goyal48461932015-03-09 17:41:09 -0700683 return;
684 }
685 final float[] r = new float[2];
686 mTargetRank = getTargetRank(d, r);
687
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800688 if (mTargetRank != mPrevTargetRank) {
Alan Viverette4cda5b72013-08-28 17:53:41 -0700689 mReorderAlarm.cancelAlarm();
Sunny Goyalc46bfef2015-01-05 12:40:08 -0800690 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
691 mReorderAlarm.setAlarm(REORDER_DELAY);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800692 mPrevTargetRank = mTargetRank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700693 }
Sunny Goyal48461932015-03-09 17:41:09 -0700694
Sunny Goyal48461932015-03-09 17:41:09 -0700695 float x = r[0];
Sunny Goyalb8634152015-04-09 14:17:14 -0700696 int currentPage = mContent.getNextPage();
Sunny Goyal48461932015-03-09 17:41:09 -0700697
Sunny Goyalb8634152015-04-09 14:17:14 -0700698 float cellOverlap = mContent.getCurrentCellLayout().getCellWidth()
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700699 * ICON_OVERSCROLL_WIDTH_FACTOR;
700 boolean isOutsideLeftEdge = x < cellOverlap;
701 boolean isOutsideRightEdge = x > (getWidth() - cellOverlap);
Sunny Goyal48461932015-03-09 17:41:09 -0700702
Sunny Goyal70660032015-05-14 00:07:08 -0700703 if (currentPage > 0 && (mContent.mIsRtl ? isOutsideRightEdge : isOutsideLeftEdge)) {
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700704 showScrollHint(DragController.SCROLL_LEFT, d);
Sunny Goyalb8634152015-04-09 14:17:14 -0700705 } else if (currentPage < (mContent.getPageCount() - 1)
Sunny Goyal70660032015-05-14 00:07:08 -0700706 && (mContent.mIsRtl ? isOutsideLeftEdge : isOutsideRightEdge)) {
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700707 showScrollHint(DragController.SCROLL_RIGHT, d);
Sunny Goyal48461932015-03-09 17:41:09 -0700708 } else {
709 mOnScrollHintAlarm.cancelAlarm();
710 if (mScrollHintDir != DragController.SCROLL_NONE) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700711 mContent.clearScrollHint();
Sunny Goyal48461932015-03-09 17:41:09 -0700712 mScrollHintDir = DragController.SCROLL_NONE;
713 }
714 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700715 }
716
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700717 private void showScrollHint(int direction, DragObject d) {
718 // Show scroll hint on the right
719 if (mScrollHintDir != direction) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700720 mContent.showScrollHint(direction);
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700721 mScrollHintDir = direction;
722 }
723
724 // Set alarm for when the hint is complete
725 if (!mOnScrollHintAlarm.alarmPending() || mCurrentScrollDir != direction) {
726 mCurrentScrollDir = direction;
727 mOnScrollHintAlarm.cancelAlarm();
728 mOnScrollHintAlarm.setOnAlarmListener(new OnScrollHintListener(d));
729 mOnScrollHintAlarm.setAlarm(SCROLL_HINT_DURATION);
730
731 mReorderAlarm.cancelAlarm();
732 mTargetRank = mEmptyCellRank;
733 }
734 }
735
Adam Cohenbfbfd262011-06-13 16:55:12 -0700736 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
737 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700738 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700739 }
740 };
741
Adam Cohen95bb8002011-07-03 23:40:28 -0700742 public void completeDragExit() {
Sunny Goyalf4066152015-04-15 09:42:19 -0700743 if (mInfo.opened) {
744 mLauncher.closeFolder();
745 mRearrangeOnClose = true;
Sunny Goyal31abc292015-05-01 10:42:32 -0700746 } else if (mState == STATE_ANIMATING) {
747 mRearrangeOnClose = true;
Sunny Goyalf4066152015-04-15 09:42:19 -0700748 } else {
749 rearrangeChildren();
Sunny Goyal31abc292015-05-01 10:42:32 -0700750 clearDragInfo();
Sunny Goyalf4066152015-04-15 09:42:19 -0700751 }
Sunny Goyal31abc292015-05-01 10:42:32 -0700752 }
753
754 private void clearDragInfo() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700755 mCurrentDragInfo = null;
756 mCurrentDragView = null;
757 mSuppressOnAdd = false;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800758 mIsExternalDrag = false;
Adam Cohen3e8f8112011-07-02 18:03:00 -0700759 }
760
Adam Cohencb3382b2011-05-24 14:07:08 -0700761 public void onDragExit(DragObject d) {
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700762 // We only close the folder if this is a true drag exit, ie. not because
763 // a drop has occurred above the folder.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700764 if (!d.dragComplete) {
765 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
766 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
767 }
768 mReorderAlarm.cancelAlarm();
Sunny Goyal48461932015-03-09 17:41:09 -0700769
Sunny Goyalb8634152015-04-09 14:17:14 -0700770 mOnScrollHintAlarm.cancelAlarm();
771 mScrollPauseAlarm.cancelAlarm();
772 if (mScrollHintDir != DragController.SCROLL_NONE) {
773 mContent.clearScrollHint();
774 mScrollHintDir = DragController.SCROLL_NONE;
Sunny Goyal48461932015-03-09 17:41:09 -0700775 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700776 }
777
Sunny Goyale9b651e2015-04-24 11:44:51 -0700778 /**
779 * When performing an accessibility drop, onDrop is sent immediately after onDragEnter. So we
780 * need to complete all transient states based on timers.
781 */
782 @Override
783 public void prepareAccessibilityDrop() {
784 if (mReorderAlarm.alarmPending()) {
785 mReorderAlarm.cancelAlarm();
786 mReorderAlarmListener.onAlarm(mReorderAlarm);
787 }
788 }
789
Michael Jurka1e2f4652013-07-08 18:03:46 -0700790 public void onDropCompleted(final View target, final DragObject d,
791 final boolean isFlingToDelete, final boolean success) {
792 if (mDeferDropAfterUninstall) {
Michael Jurkaf3007582013-08-21 14:33:57 +0200793 Log.d(TAG, "Deferred handling drop because waiting for uninstall.");
Michael Jurka1e2f4652013-07-08 18:03:46 -0700794 mDeferredAction = new Runnable() {
795 public void run() {
796 onDropCompleted(target, d, isFlingToDelete, success);
797 mDeferredAction = null;
798 }
799 };
800 return;
801 }
802
803 boolean beingCalledAfterUninstall = mDeferredAction != null;
804 boolean successfulDrop =
805 success && (!beingCalledAfterUninstall || mUninstallSuccessful);
Winson Chung5f8afe62013-08-12 16:19:28 -0700806
Michael Jurka1e2f4652013-07-08 18:03:46 -0700807 if (successfulDrop) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800808 if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon && target != this) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700809 replaceFolderWithFinalItem();
810 }
811 } else {
812 // The drag failed, we need to return the item to the folder
Sunny Goyalb8c663c2015-04-23 11:43:48 -0700813 ShortcutInfo info = (ShortcutInfo) d.dragInfo;
814 View icon = (mCurrentDragView != null && mCurrentDragView.getTag() == info)
815 ? mCurrentDragView : mContent.createNewView(info);
816 ArrayList<View> views = getItemsInReadingOrder();
817 views.add(info.rank, icon);
818 mContent.arrangeChildren(views, views.size());
819 mItemsInvalidated = true;
820
821 mSuppressOnAdd = true;
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700822 mFolderIcon.onDrop(d);
Sunny Goyalb8c663c2015-04-23 11:43:48 -0700823 mSuppressOnAdd = false;
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700824 }
825
826 if (target != this) {
827 if (mOnExitAlarm.alarmPending()) {
828 mOnExitAlarm.cancelAlarm();
Michael Jurka54554252013-08-01 12:52:23 +0200829 if (!successfulDrop) {
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700830 mSuppressFolderDeletion = true;
831 }
Sunny Goyal5d85c442015-03-10 13:14:47 -0700832 mScrollPauseAlarm.cancelAlarm();
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700833 completeDragExit();
834 }
Adam Cohen9c58d822013-12-13 17:18:10 -0800835 }
836
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700837 mDeleteFolderOnDropCompleted = false;
838 mDragInProgress = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700839 mItemAddedBackToSelfViaIcon = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700840 mCurrentDragInfo = null;
841 mCurrentDragView = null;
842 mSuppressOnAdd = false;
Adam Cohen4045eb72011-10-06 11:44:26 -0700843
844 // Reordering may have occured, and we need to save the new item locations. We do this once
845 // at the end to prevent unnecessary database operations.
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700846 updateItemLocationsInDatabaseBatch();
Sunny Goyalb7e15ad2015-05-07 14:51:31 -0700847
848 // Use the item count to check for multi-page as the folder UI may not have
849 // been refreshed yet.
850 if (getItemCount() <= mContent.itemsPerPage()) {
851 // Show the animation, next time something is added to the folder.
852 mInfo.setOption(FolderInfo.FLAG_MULTI_PAGE_ANIMATION, false, mLauncher);
853 }
854
Adam Cohen4045eb72011-10-06 11:44:26 -0700855 }
856
Sunny Goyalfa401a12015-04-10 13:45:42 -0700857 @Override
Michael Jurka1e2f4652013-07-08 18:03:46 -0700858 public void deferCompleteDropAfterUninstallActivity() {
859 mDeferDropAfterUninstall = true;
860 }
861
Sunny Goyalfa401a12015-04-10 13:45:42 -0700862 @Override
Michael Jurka1e2f4652013-07-08 18:03:46 -0700863 public void onUninstallActivityReturned(boolean success) {
864 mDeferDropAfterUninstall = false;
865 mUninstallSuccessful = success;
866 if (mDeferredAction != null) {
867 mDeferredAction.run();
868 }
869 }
870
Winson Chunga48487a2012-03-20 16:19:37 -0700871 @Override
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800872 public float getIntrinsicIconScaleFactor() {
873 return 1f;
874 }
875
876 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800877 public boolean supportsFlingToDelete() {
878 return true;
879 }
880
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000881 @Override
882 public boolean supportsAppInfoDropTarget() {
883 return false;
884 }
885
886 @Override
887 public boolean supportsDeleteDropTarget() {
888 return true;
889 }
890
Sunny Goyalddec7342015-04-29 18:12:37 -0700891 @Override
892 public void onFlingToDelete(DragObject d, PointF vec) {
Winson Chunga48487a2012-03-20 16:19:37 -0700893 // Do nothing
894 }
895
896 @Override
897 public void onFlingToDeleteCompleted() {
898 // Do nothing
899 }
900
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700901 private void updateItemLocationsInDatabaseBatch() {
902 ArrayList<View> list = getItemsInReadingOrder();
903 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
904 for (int i = 0; i < list.size(); i++) {
905 View v = list.get(i);
906 ItemInfo info = (ItemInfo) v.getTag();
Sunny Goyal08f72612015-01-05 13:41:43 -0800907 info.rank = i;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700908 items.add(info);
909 }
910
911 LauncherModel.moveItemsInDatabase(mLauncher, items, mInfo.id, 0);
912 }
913
Adam Cohene25af792013-06-06 23:08:25 -0700914 public void addItemLocationsInDatabase() {
915 ArrayList<View> list = getItemsInReadingOrder();
916 for (int i = 0; i < list.size(); i++) {
917 View v = list.get(i);
918 ItemInfo info = (ItemInfo) v.getTag();
919 LauncherModel.addItemToDatabase(mLauncher, info, mInfo.id, 0,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700920 info.cellX, info.cellY);
Adam Cohene25af792013-06-06 23:08:25 -0700921 }
922 }
923
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700924 public void notifyDrop() {
925 if (mDragInProgress) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700926 mItemAddedBackToSelfViaIcon = true;
Adam Cohen76078c42011-06-09 15:06:52 -0700927 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700928 }
929
930 public boolean isDropEnabled() {
931 return true;
932 }
933
Adam Cohen2801caf2011-05-13 20:57:39 -0700934 public boolean isFull() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800935 return mContent.isFull();
Adam Cohen2801caf2011-05-13 20:57:39 -0700936 }
937
938 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700939 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700940
Winson Chung892c74d2013-08-22 16:15:50 -0700941 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700942 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700943 int height = getFolderHeight();
Adam Cohen2801caf2011-05-13 20:57:39 -0700944
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800945 float scale = parent.getDescendantRectRelativeToSelf(mFolderIcon, sTempRect);
Adam Cohen8e776a62011-06-28 18:10:06 -0700946
Adam Cohen2e6da152015-05-06 11:42:25 -0700947 DeviceProfile grid = mLauncher.getDeviceProfile();
Winson Chungaf40f202013-09-18 18:26:31 -0700948
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800949 int centerX = (int) (sTempRect.left + sTempRect.width() * scale / 2);
950 int centerY = (int) (sTempRect.top + sTempRect.height() * scale / 2);
Adam Cohen2801caf2011-05-13 20:57:39 -0700951 int centeredLeft = centerX - width / 2;
952 int centeredTop = centerY - height / 2;
Winson Chung3057b1c2013-10-10 17:35:35 -0700953 int currentPage = mLauncher.getWorkspace().getNextPage();
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800954 // In case the workspace is scrolling, we need to use the final scroll to compute
955 // the folders bounds.
956 mLauncher.getWorkspace().setFinalScrollForPageChange(currentPage);
Adam Cohen35e7e642011-07-17 14:47:18 -0700957 // We first fetch the currently visible CellLayoutChildren
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800958 CellLayout currentLayout = (CellLayout) mLauncher.getWorkspace().getChildAt(currentPage);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700959 ShortcutAndWidgetContainer boundingLayout = currentLayout.getShortcutsAndWidgets();
Adam Cohen35e7e642011-07-17 14:47:18 -0700960 Rect bounds = new Rect();
961 parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800962 // We reset the workspaces scroll
963 mLauncher.getWorkspace().resetFinalScrollForPageChange(currentPage);
Adam Cohen2801caf2011-05-13 20:57:39 -0700964
Adam Cohen35e7e642011-07-17 14:47:18 -0700965 // We need to bound the folder to the currently visible CellLayoutChildren
966 int left = Math.min(Math.max(bounds.left, centeredLeft),
967 bounds.left + bounds.width() - width);
968 int top = Math.min(Math.max(bounds.top, centeredTop),
969 bounds.top + bounds.height() - height);
Sunny Goyalc6205602015-05-21 20:46:33 -0700970 if (grid.isPhone && (grid.availableWidthPx - width) < grid.iconSizePx) {
Winson Chungaf40f202013-09-18 18:26:31 -0700971 // Center the folder if it is full (on phones only)
972 left = (grid.availableWidthPx - width) / 2;
973 } else if (width >= bounds.width()) {
974 // If the folder doesn't fit within the bounds, center it about the desired bounds
Adam Cohen35e7e642011-07-17 14:47:18 -0700975 left = bounds.left + (bounds.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700976 }
Adam Cohen35e7e642011-07-17 14:47:18 -0700977 if (height >= bounds.height()) {
978 top = bounds.top + (bounds.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700979 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700980
981 int folderPivotX = width / 2 + (centeredLeft - left);
982 int folderPivotY = height / 2 + (centeredTop - top);
983 setPivotX(folderPivotX);
984 setPivotY(folderPivotY);
Adam Cohen268c4752012-06-06 17:47:33 -0700985 mFolderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700986 (1.0f * folderPivotX / width));
Adam Cohen268c4752012-06-06 17:47:33 -0700987 mFolderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700988 (1.0f * folderPivotY / height));
Adam Cohen3bf84d32012-05-07 20:17:14 -0700989
Adam Cohen662b5982011-12-13 17:45:21 -0800990 lp.width = width;
991 lp.height = height;
992 lp.x = left;
993 lp.y = top;
Adam Cohen2801caf2011-05-13 20:57:39 -0700994 }
995
Adam Cohen268c4752012-06-06 17:47:33 -0700996 float getPivotXForIconAnimation() {
997 return mFolderIconPivotX;
998 }
999 float getPivotYForIconAnimation() {
1000 return mFolderIconPivotY;
1001 }
1002
Winson Chung892c74d2013-08-22 16:15:50 -07001003 private int getContentAreaHeight() {
Adam Cohen2e6da152015-05-06 11:42:25 -07001004 DeviceProfile grid = mLauncher.getDeviceProfile();
Sunny Goyalc6205602015-05-21 20:46:33 -07001005 Rect workspacePadding = grid.getWorkspacePadding(mContent.mIsRtl);
Winson Chung892c74d2013-08-22 16:15:50 -07001006 int maxContentAreaHeight = grid.availableHeightPx -
Winson Chung892c74d2013-08-22 16:15:50 -07001007 workspacePadding.top - workspacePadding.bottom -
Sunny Goyal290800b2015-03-05 11:33:33 -08001008 mFooterHeight;
Adam Cohen1960ea42013-11-12 11:33:14 +00001009 int height = Math.min(maxContentAreaHeight,
Winson Chung892c74d2013-08-22 16:15:50 -07001010 mContent.getDesiredHeight());
Adam Cohen1960ea42013-11-12 11:33:14 +00001011 return Math.max(height, MIN_CONTENT_DIMEN);
1012 }
1013
1014 private int getContentAreaWidth() {
1015 return Math.max(mContent.getDesiredWidth(), MIN_CONTENT_DIMEN);
Winson Chung892c74d2013-08-22 16:15:50 -07001016 }
1017
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001018 private int getFolderHeight() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001019 return getFolderHeight(getContentAreaHeight());
1020 }
1021
1022 private int getFolderHeight(int contentAreaHeight) {
Sunny Goyal290800b2015-03-05 11:33:33 -08001023 return getPaddingTop() + getPaddingBottom() + contentAreaHeight + mFooterHeight;
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001024 }
Adam Cohen234c4cd2011-07-17 21:03:04 -07001025
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001026 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001027 int contentWidth = getContentAreaWidth();
1028 int contentHeight = getContentAreaHeight();
Adam Cohen1960ea42013-11-12 11:33:14 +00001029
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001030 int contentAreaWidthSpec = MeasureSpec.makeMeasureSpec(contentWidth, MeasureSpec.EXACTLY);
1031 int contentAreaHeightSpec = MeasureSpec.makeMeasureSpec(contentHeight, MeasureSpec.EXACTLY);
1032
1033 mContent.setFixedSize(contentWidth, contentHeight);
1034 mContentWrapper.measure(contentAreaWidthSpec, contentAreaHeightSpec);
Sunny Goyal290800b2015-03-05 11:33:33 -08001035 mFooter.measure(contentAreaWidthSpec,
1036 MeasureSpec.makeMeasureSpec(mFooterHeight, MeasureSpec.EXACTLY));
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001037
1038 int folderWidth = getPaddingLeft() + getPaddingRight() + contentWidth;
1039 int folderHeight = getFolderHeight(contentHeight);
1040 setMeasuredDimension(folderWidth, folderHeight);
Adam Cohen234c4cd2011-07-17 21:03:04 -07001041 }
1042
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001043 /**
1044 * Rearranges the children based on their rank.
1045 */
1046 public void rearrangeChildren() {
1047 rearrangeChildren(-1);
1048 }
Adam Cohen2801caf2011-05-13 20:57:39 -07001049
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001050 /**
1051 * Rearranges the children based on their rank.
1052 * @param itemCount if greater than the total children count, empty spaces are left at the end,
1053 * otherwise it is ignored.
1054 */
1055 public void rearrangeChildren(int itemCount) {
1056 ArrayList<View> views = getItemsInReadingOrder();
1057 mContent.arrangeChildren(views, Math.max(itemCount, views.size()));
Adam Cohen7c693212011-05-18 15:26:57 -07001058 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -07001059 }
1060
Sunny Goyalc4918352015-03-10 18:15:48 -07001061 // TODO remove this once GSA code fix is submitted
1062 public ViewGroup getContent() {
1063 return (ViewGroup) mContent;
1064 }
1065
Adam Cohena9cf38f2011-05-02 15:36:58 -07001066 public int getItemCount() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001067 return mContent.getItemCount();
Adam Cohena9cf38f2011-05-02 15:36:58 -07001068 }
1069
Adam Cohen091440a2015-03-18 14:16:05 -07001070 @Thunk void onCloseComplete() {
Adam Cohen05e0f402011-08-01 12:12:49 -07001071 DragLayer parent = (DragLayer) getParent();
Michael Jurka5649c282012-06-18 10:33:21 -07001072 if (parent != null) {
1073 parent.removeView(this);
1074 }
Adam Cohen4554ee12011-08-03 16:13:21 -07001075 mDragController.removeDropTarget((DropTarget) this);
Adam Cohen05e0f402011-08-01 12:12:49 -07001076 clearFocus();
Adam Cohenac56cff2011-09-28 20:45:37 -07001077 mFolderIcon.requestFocus();
Adam Cohen05e0f402011-08-01 12:12:49 -07001078
Adam Cohen2801caf2011-05-13 20:57:39 -07001079 if (mRearrangeOnClose) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001080 rearrangeChildren();
Adam Cohen2801caf2011-05-13 20:57:39 -07001081 mRearrangeOnClose = false;
1082 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001083 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001084 if (!mDragInProgress && !mSuppressFolderDeletion) {
1085 replaceFolderWithFinalItem();
1086 } else if (mDragInProgress) {
1087 mDeleteFolderOnDropCompleted = true;
1088 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001089 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001090 mSuppressFolderDeletion = false;
Sunny Goyal31abc292015-05-01 10:42:32 -07001091 clearDragInfo();
Adam Cohenafb01ee2011-06-23 15:38:03 -07001092 }
1093
Adam Cohen091440a2015-03-18 14:16:05 -07001094 @Thunk void replaceFolderWithFinalItem() {
Adam Cohenafb01ee2011-06-23 15:38:03 -07001095 // Add the last remaining child to the workspace in place of the folder
Adam Cohenfb91f302012-06-11 15:45:18 -07001096 Runnable onCompleteRunnable = new Runnable() {
1097 @Override
1098 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -07001099 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screenId);
Adam Cohenafb01ee2011-06-23 15:38:03 -07001100
Winson Chung33231f52013-12-09 16:57:45 -08001101 View child = null;
Adam Cohenfb91f302012-06-11 15:45:18 -07001102 // Move the item from the folder to the workspace, in the position of the folder
1103 if (getItemCount() == 1) {
1104 ShortcutInfo finalItem = mInfo.contents.get(0);
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001105 child = mLauncher.createShortcut(cellLayout, finalItem);
Adam Cohenfb91f302012-06-11 15:45:18 -07001106 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -07001107 mInfo.screenId, mInfo.cellX, mInfo.cellY);
Adam Cohenfb91f302012-06-11 15:45:18 -07001108 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001109 if (getItemCount() <= 1) {
1110 // Remove the folder
1111 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
Dan Sandler0eb687f2014-01-10 13:24:55 -05001112 if (cellLayout != null) {
1113 // b/12446428 -- sometimes the cell layout has already gone away?
1114 cellLayout.removeView(mFolderIcon);
1115 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001116 if (mFolderIcon instanceof DropTarget) {
1117 mDragController.removeDropTarget((DropTarget) mFolderIcon);
1118 }
1119 mLauncher.removeFolder(mInfo);
1120 }
Adam Cohenc5e63f32012-07-12 16:16:57 -07001121 // We add the child after removing the folder to prevent both from existing at
Winson Chung0e6a7132013-08-23 12:55:10 -07001122 // the same time in the CellLayout. We need to add the new item with addInScreenFromBind()
1123 // to ensure that hotseat items are placed correctly.
Adam Cohenc5e63f32012-07-12 16:16:57 -07001124 if (child != null) {
Winson Chung0e6a7132013-08-23 12:55:10 -07001125 mLauncher.getWorkspace().addInScreenFromBind(child, mInfo.container, mInfo.screenId,
Adam Cohenc5e63f32012-07-12 16:16:57 -07001126 mInfo.cellX, mInfo.cellY, mInfo.spanX, mInfo.spanY);
1127 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001128 }
1129 };
Sunny Goyalbc753352015-03-05 09:40:44 -08001130 View finalChild = mContent.getLastItem();
Adam Cohenfb91f302012-06-11 15:45:18 -07001131 if (finalChild != null) {
1132 mFolderIcon.performDestroyAnimation(finalChild, onCompleteRunnable);
Winson Chung33231f52013-12-09 16:57:45 -08001133 } else {
1134 onCompleteRunnable.run();
Adam Cohenafb01ee2011-06-23 15:38:03 -07001135 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001136 mDestroyed = true;
1137 }
1138
1139 boolean isDestroyed() {
1140 return mDestroyed;
Adam Cohen2801caf2011-05-13 20:57:39 -07001141 }
1142
Adam Cohenac56cff2011-09-28 20:45:37 -07001143 // This method keeps track of the last item in the folder for the purposes
1144 // of keyboard focus
Sunny Goyal290800b2015-03-05 11:33:33 -08001145 public void updateTextViewFocus() {
Sunny Goyalbc753352015-03-05 09:40:44 -08001146 View lastChild = mContent.getLastItem();
Adam Cohenac56cff2011-09-28 20:45:37 -07001147 if (lastChild != null) {
1148 mFolderName.setNextFocusDownId(lastChild.getId());
1149 mFolderName.setNextFocusRightId(lastChild.getId());
1150 mFolderName.setNextFocusLeftId(lastChild.getId());
1151 mFolderName.setNextFocusUpId(lastChild.getId());
1152 }
1153 }
1154
Adam Cohenbfbfd262011-06-13 16:55:12 -07001155 public void onDrop(DragObject d) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001156 Runnable cleanUpRunnable = null;
1157
Adam Cohen689ff162014-05-08 17:27:56 -07001158 // If we are coming from All Apps space, we defer removing the extra empty screen
1159 // until the folder closes
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001160 if (d.dragSource != mLauncher.getWorkspace() && !(d.dragSource instanceof Folder)) {
1161 cleanUpRunnable = new Runnable() {
1162 @Override
1163 public void run() {
Adam Cohen689ff162014-05-08 17:27:56 -07001164 mLauncher.exitSpringLoadedDragModeDelayed(true,
1165 Launcher.EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
1166 null);
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001167 }
1168 };
Adam Cohenbfbfd262011-06-13 16:55:12 -07001169 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001170
Sunny Goyalb8634152015-04-09 14:17:14 -07001171 // If the icon was dropped while the page was being scrolled, we need to compute
1172 // the target location again such that the icon is placed of the final page.
1173 if (!mContent.rankOnCurrentPage(mEmptyCellRank)) {
1174 // Reorder again.
1175 mTargetRank = getTargetRank(d, null);
Sunny Goyal48461932015-03-09 17:41:09 -07001176
Sunny Goyalb8634152015-04-09 14:17:14 -07001177 // Rearrange items immediately.
1178 mReorderAlarmListener.onAlarm(mReorderAlarm);
Sunny Goyal48461932015-03-09 17:41:09 -07001179
Sunny Goyalb8634152015-04-09 14:17:14 -07001180 mOnScrollHintAlarm.cancelAlarm();
1181 mScrollPauseAlarm.cancelAlarm();
Sunny Goyal48461932015-03-09 17:41:09 -07001182 }
Sunny Goyalb8634152015-04-09 14:17:14 -07001183 mContent.completePendingPageChanges();
Sunny Goyal48461932015-03-09 17:41:09 -07001184
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001185 View currentDragView;
1186 ShortcutInfo si = mCurrentDragInfo;
1187 if (mIsExternalDrag) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001188 currentDragView = mContent.createAndAddViewForRank(si, mEmptyCellRank);
Sunny Goyal95abbb32014-08-04 10:53:22 -07001189 // Actually move the item in the database if it was an external drag. Call this
1190 // before creating the view, so that ShortcutInfo is updated appropriately.
1191 LauncherModel.addOrMoveItemInDatabase(
1192 mLauncher, si, mInfo.id, 0, si.cellX, si.cellY);
1193
1194 // We only need to update the locations if it doesn't get handled in #onDropCompleted.
1195 if (d.dragSource != this) {
1196 updateItemLocationsInDatabaseBatch();
1197 }
1198 mIsExternalDrag = false;
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001199 } else {
1200 currentDragView = mCurrentDragView;
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001201 mContent.addViewForRank(currentDragView, si, mEmptyCellRank);
Adam Cohenbfbfd262011-06-13 16:55:12 -07001202 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001203
1204 if (d.dragView.hasDrawn()) {
1205
1206 // Temporarily reset the scale such that the animation target gets calculated correctly.
1207 float scaleX = getScaleX();
1208 float scaleY = getScaleY();
1209 setScaleX(1.0f);
1210 setScaleY(1.0f);
1211 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, currentDragView,
1212 cleanUpRunnable, null);
1213 setScaleX(scaleX);
1214 setScaleY(scaleY);
1215 } else {
1216 d.deferDragViewCleanupPostAnimation = false;
1217 currentDragView.setVisibility(VISIBLE);
1218 }
1219 mItemsInvalidated = true;
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001220 rearrangeChildren();
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001221
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001222 // Temporarily suppress the listener, as we did all the work already here.
1223 mSuppressOnAdd = true;
1224 mInfo.add(si);
1225 mSuppressOnAdd = false;
Sunny Goyal4b020172014-08-28 14:51:14 -07001226 // Clear the drag info, as it is no longer being dragged.
1227 mCurrentDragInfo = null;
Sunny Goyalf4066152015-04-15 09:42:19 -07001228 mDragInProgress = false;
Sunny Goyalb7e15ad2015-05-07 14:51:31 -07001229
1230 if (mContent.getPageCount() > 1) {
1231 // The animation has already been shown while opening the folder.
1232 mInfo.setOption(FolderInfo.FLAG_MULTI_PAGE_ANIMATION, true, mLauncher);
1233 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001234 }
1235
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001236 // This is used so the item doesn't immediately appear in the folder when added. In one case
1237 // we need to create the illusion that the item isn't added back to the folder yet, to
1238 // to correspond to the animation of the icon back into the folder. This is
1239 public void hideItem(ShortcutInfo info) {
1240 View v = getViewForInfo(info);
1241 v.setVisibility(INVISIBLE);
1242 }
1243 public void showItem(ShortcutInfo info) {
1244 View v = getViewForInfo(info);
1245 v.setVisibility(VISIBLE);
1246 }
1247
Sunny Goyalb7e15ad2015-05-07 14:51:31 -07001248 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -07001249 public void onAdd(ShortcutInfo item) {
Adam Cohen05e0f402011-08-01 12:12:49 -07001250 // If the item was dropped onto this open folder, we have done the work associated
1251 // with adding the item to the folder, as indicated by mSuppressOnAdd being set
Adam Cohenbfbfd262011-06-13 16:55:12 -07001252 if (mSuppressOnAdd) return;
Sunny Goyal5d85c442015-03-10 13:14:47 -07001253 mContent.createAndAddViewForRank(item, mContent.allocateRankForNewItem(item));
Sunny Goyal2e688a82015-03-18 10:23:39 -07001254 mItemsInvalidated = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001255 LauncherModel.addOrMoveItemInDatabase(
1256 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
1257 }
1258
Adam Cohena9cf38f2011-05-02 15:36:58 -07001259 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -07001260 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001261 // If this item is being dragged from this open folder, we have already handled
1262 // the work associated with removing the item, so we don't have to do anything here.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001263 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001264 View v = getViewForInfo(item);
Sunny Goyal290800b2015-03-05 11:33:33 -08001265 mContent.removeItem(v);
Adam Cohen2801caf2011-05-13 20:57:39 -07001266 if (mState == STATE_ANIMATING) {
1267 mRearrangeOnClose = true;
1268 } else {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001269 rearrangeChildren();
Adam Cohen2801caf2011-05-13 20:57:39 -07001270 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001271 if (getItemCount() <= 1) {
1272 replaceFolderWithFinalItem();
1273 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001274 }
Adam Cohen7c693212011-05-18 15:26:57 -07001275
Sunny Goyalbc753352015-03-05 09:40:44 -08001276 private View getViewForInfo(final ShortcutInfo item) {
1277 return mContent.iterateOverItems(new ItemOperator() {
1278
1279 @Override
1280 public boolean evaluate(ItemInfo info, View view, View parent) {
1281 return info == item;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001282 }
Sunny Goyalbc753352015-03-05 09:40:44 -08001283 });
Adam Cohendf1e4e82011-06-24 15:57:39 -07001284 }
1285
Adam Cohen76078c42011-06-09 15:06:52 -07001286 public void onItemsChanged() {
Adam Cohenac56cff2011-09-28 20:45:37 -07001287 updateTextViewFocus();
Adam Cohen76078c42011-06-09 15:06:52 -07001288 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001289
Adam Cohen76fc0852011-06-17 13:26:23 -07001290 public void onTitleChanged(CharSequence title) {
1291 }
Adam Cohen76078c42011-06-09 15:06:52 -07001292
Adam Cohen7c693212011-05-18 15:26:57 -07001293 public ArrayList<View> getItemsInReadingOrder() {
1294 if (mItemsInvalidated) {
1295 mItemsInReadingOrder.clear();
Sunny Goyalbc753352015-03-05 09:40:44 -08001296 mContent.iterateOverItems(new ItemOperator() {
1297
1298 @Override
1299 public boolean evaluate(ItemInfo info, View view, View parent) {
1300 mItemsInReadingOrder.add(view);
1301 return false;
Adam Cohen7c693212011-05-18 15:26:57 -07001302 }
Sunny Goyalbc753352015-03-05 09:40:44 -08001303 });
Adam Cohen7c693212011-05-18 15:26:57 -07001304 mItemsInvalidated = false;
1305 }
1306 return mItemsInReadingOrder;
1307 }
Adam Cohen8dfcba42011-07-07 16:38:18 -07001308
1309 public void getLocationInDragLayer(int[] loc) {
1310 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
1311 }
Adam Cohenea0818d2011-09-30 20:06:58 -07001312
1313 public void onFocusChange(View v, boolean hasFocus) {
1314 if (v == mFolderName && hasFocus) {
1315 startEditingFolderName();
1316 }
1317 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001318
1319 @Override
1320 public void getHitRectRelativeToDragLayer(Rect outRect) {
1321 getHitRect(outRect);
Sunny Goyal48461932015-03-09 17:41:09 -07001322 outRect.left -= mScrollAreaOffset;
1323 outRect.right += mScrollAreaOffset;
1324 }
1325
1326 private class OnScrollHintListener implements OnAlarmListener {
1327
1328 private final DragObject mDragObject;
1329
1330 OnScrollHintListener(DragObject object) {
1331 mDragObject = object;
1332 }
1333
1334 /**
1335 * Scroll hint has been shown long enough. Now scroll to appropriate page.
1336 */
1337 @Override
1338 public void onAlarm(Alarm alarm) {
1339 if (mCurrentScrollDir == DragController.SCROLL_LEFT) {
Sunny Goyalb8634152015-04-09 14:17:14 -07001340 mContent.scrollLeft();
Sunny Goyal48461932015-03-09 17:41:09 -07001341 mScrollHintDir = DragController.SCROLL_NONE;
1342 } else if (mCurrentScrollDir == DragController.SCROLL_RIGHT) {
Sunny Goyalb8634152015-04-09 14:17:14 -07001343 mContent.scrollRight();
Sunny Goyal48461932015-03-09 17:41:09 -07001344 mScrollHintDir = DragController.SCROLL_NONE;
1345 } else {
1346 // This should not happen
1347 return;
1348 }
1349 mCurrentScrollDir = DragController.SCROLL_NONE;
1350
1351 // Pause drag event until the scrolling is finished
1352 mScrollPauseAlarm.setOnAlarmListener(new OnScrollFinishedListener(mDragObject));
1353 mScrollPauseAlarm.setAlarm(DragController.RESCROLL_DELAY);
1354 }
1355 }
1356
1357 private class OnScrollFinishedListener implements OnAlarmListener {
1358
1359 private final DragObject mDragObject;
1360
1361 OnScrollFinishedListener(DragObject object) {
1362 mDragObject = object;
1363 }
1364
1365 /**
1366 * Page scroll is complete.
1367 */
1368 @Override
1369 public void onAlarm(Alarm alarm) {
1370 // Reorder immediately on page change.
1371 onDragOver(mDragObject, 1);
1372 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001373 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001374}