blob: 85f58a1b1d219458a5aa9675406c3970c56a2e51 [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;
Winson Chung8f1eff72015-05-28 17:33:40 -070032import android.os.Bundle;
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;
Adam Cohen3bf84d32012-05-07 20:17:14 -070037import android.util.Log;
Adam Cohen76fc0852011-06-17 13:26:23 -070038import android.view.ActionMode;
39import android.view.KeyEvent;
Adam Cohen76fc0852011-06-17 13:26:23 -070040import android.view.Menu;
41import android.view.MenuItem;
Adam Cohen0c872ba2011-05-05 10:34:16 -070042import android.view.MotionEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.view.View;
Sunny Goyalc4918352015-03-10 18:15:48 -070044import android.view.ViewGroup;
Adam Cohen3371da02011-10-25 21:38:29 -070045import android.view.accessibility.AccessibilityEvent;
46import android.view.accessibility.AccessibilityManager;
Adam Cohenc4fe9ea2014-08-18 18:54:10 -070047import android.view.animation.AccelerateInterpolator;
Sunny Goyalc1cd23b2015-06-01 17:15:34 -070048import android.view.animation.AnimationUtils;
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,
Winson Chung8f1eff72015-05-28 17:33:40 -070070 View.OnFocusChangeListener, DragListener, UninstallSource, AccessibilityDragSource,
71 Stats.LaunchSourceProvider {
Adam Cohendf2cc412011-04-27 16:56:57 -070072 private static final String TAG = "Launcher.Folder";
73
Sunny Goyalc3a609f2015-02-26 17:43:50 -080074 /**
75 * We avoid measuring {@link #mContentWrapper} with a 0 width or height, as this
76 * results in CellLayout being measured as UNSPECIFIED, which it does not support.
77 */
78 private static final int MIN_CONTENT_DIMEN = 5;
Adam Cohen4eac29a2011-07-11 17:53:37 -070079
Adam Cohendf2cc412011-04-27 16:56:57 -070080 static final int STATE_NONE = -1;
81 static final int STATE_SMALL = 0;
82 static final int STATE_ANIMATING = 1;
83 static final int STATE_OPEN = 2;
84
Sunny Goyal48461932015-03-09 17:41:09 -070085 /**
Sunny Goyal48461932015-03-09 17:41:09 -070086 * Time for which the scroll hint is shown before automatically changing page.
87 */
88 public static final int SCROLL_HINT_DURATION = DragController.SCROLL_DELAY;
89
90 /**
91 * Fraction of icon width which behave as scroll region.
92 */
93 private static final float ICON_OVERSCROLL_WIDTH_FACTOR = 0.45f;
94
Sunny Goyala07c2f52015-06-01 11:00:38 -070095 private static final int FOLDER_NAME_ANIMATION_DURATION = 633;
Sunny Goyalb7e15ad2015-05-07 14:51:31 -070096
Adam Cohenf0f4eda2013-06-06 21:27:03 -070097 private static final int REORDER_DELAY = 250;
Adam Cohen5d518fa2013-12-05 14:16:23 -080098 private static final int ON_EXIT_CLOSE_DELAY = 400;
Sunny Goyalc3a609f2015-02-26 17:43:50 -080099 private static final Rect sTempRect = new Rect();
100
101 private static String sDefaultFolderName;
102 private static String sHintText;
103
104 private final Alarm mReorderAlarm = new Alarm();
105 private final Alarm mOnExitAlarm = new Alarm();
Sunny Goyalb8634152015-04-09 14:17:14 -0700106 private final Alarm mOnScrollHintAlarm = new Alarm();
107 @Thunk final Alarm mScrollPauseAlarm = new Alarm();
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800108
Adam Cohen091440a2015-03-18 14:16:05 -0700109 @Thunk final ArrayList<View> mItemsInReadingOrder = new ArrayList<View>();
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800110
111 private final int mExpandDuration;
112 private final int mMaterialExpandDuration;
113 private final int mMaterialExpandStagger;
114
115 private final InputMethodManager mInputMethodManager;
116
117 protected final Launcher mLauncher;
118 protected DragController mDragController;
119 protected FolderInfo mInfo;
120
Adam Cohen091440a2015-03-18 14:16:05 -0700121 @Thunk FolderIcon mFolderIcon;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800122
Sunny Goyalb8634152015-04-09 14:17:14 -0700123 @Thunk FolderPagedView mContent;
Adam Cohen091440a2015-03-18 14:16:05 -0700124 @Thunk View mContentWrapper;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800125 FolderEditText mFolderName;
126
Sunny Goyal290800b2015-03-05 11:33:33 -0800127 private View mFooter;
128 private int mFooterHeight;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800129
130 // Cell ranks used for drag and drop
Adam Cohen091440a2015-03-18 14:16:05 -0700131 @Thunk int mTargetRank, mPrevTargetRank, mEmptyCellRank;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800132
Adam Cohen091440a2015-03-18 14:16:05 -0700133 @Thunk int mState = STATE_NONE;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800134 private boolean mRearrangeOnClose = false;
Adam Cohen7c693212011-05-18 15:26:57 -0700135 boolean mItemsInvalidated = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700136 private ShortcutInfo mCurrentDragInfo;
137 private View mCurrentDragView;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800138 private boolean mIsExternalDrag;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700139 boolean mSuppressOnAdd = false;
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700140 private boolean mDragInProgress = false;
141 private boolean mDeleteFolderOnDropCompleted = false;
142 private boolean mSuppressFolderDeletion = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700143 private boolean mItemAddedBackToSelfViaIcon = false;
Adam Cohen091440a2015-03-18 14:16:05 -0700144 @Thunk float mFolderIconPivotX;
145 @Thunk float mFolderIconPivotY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700146 private boolean mIsEditingName = false;
Adam Cohen1960ea42013-11-12 11:33:14 +0000147
Adam Cohenfb91f302012-06-11 15:45:18 -0700148 private boolean mDestroyed;
149
Adam Cohen091440a2015-03-18 14:16:05 -0700150 @Thunk Runnable mDeferredAction;
Michael Jurka1e2f4652013-07-08 18:03:46 -0700151 private boolean mDeferDropAfterUninstall;
152 private boolean mUninstallSuccessful;
153
Sunny Goyal48461932015-03-09 17:41:09 -0700154 // Folder scrolling
155 private int mScrollAreaOffset;
Sunny Goyal48461932015-03-09 17:41:09 -0700156
Adam Cohen091440a2015-03-18 14:16:05 -0700157 @Thunk int mScrollHintDir = DragController.SCROLL_NONE;
158 @Thunk int mCurrentScrollDir = DragController.SCROLL_NONE;
Sunny Goyal48461932015-03-09 17:41:09 -0700159
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160 /**
161 * Used to inflate the Workspace from XML.
162 *
163 * @param context The application's context.
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800164 * @param attrs The attributes set containing the Workspace's customization values.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165 */
166 public Folder(Context context, AttributeSet attrs) {
167 super(context, attrs);
168 setAlwaysDrawnWithCacheEnabled(false);
Adam Cohen76fc0852011-06-17 13:26:23 -0700169 mInputMethodManager = (InputMethodManager)
Michael Jurka8b805b12012-04-18 14:23:14 -0700170 getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
Adam Cohen76fc0852011-06-17 13:26:23 -0700171
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800172 Resources res = getResources();
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700173 mExpandDuration = res.getInteger(R.integer.config_folderExpandDuration);
174 mMaterialExpandDuration = res.getInteger(R.integer.config_materialFolderExpandDuration);
175 mMaterialExpandStagger = res.getInteger(R.integer.config_materialFolderExpandStagger);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700176
177 if (sDefaultFolderName == null) {
178 sDefaultFolderName = res.getString(R.string.folder_name);
179 }
Adam Cohena65beee2011-06-27 21:32:23 -0700180 if (sHintText == null) {
181 sHintText = res.getString(R.string.folder_hint_text);
182 }
Adam Cohen4eac29a2011-07-11 17:53:37 -0700183 mLauncher = (Launcher) context;
Adam Cohenac56cff2011-09-28 20:45:37 -0700184 // We need this view to be focusable in touch mode so that when text editing of the folder
185 // name is complete, we have something to focus on, thus hiding the cursor and giving
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800186 // reliable behavior when clicking the text field (since it will always gain focus on click).
Adam Cohenac56cff2011-09-28 20:45:37 -0700187 setFocusableInTouchMode(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188 }
189
190 @Override
191 protected void onFinishInflate() {
192 super.onFinishInflate();
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800193 mContentWrapper = findViewById(R.id.folder_content_wrapper);
Sunny Goyalb8634152015-04-09 14:17:14 -0700194 mContent = (FolderPagedView) findViewById(R.id.folder_content);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800195 mContent.setFolder(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700196
Adam Cohenac56cff2011-09-28 20:45:37 -0700197 mFolderName = (FolderEditText) findViewById(R.id.folder_name);
198 mFolderName.setFolder(this);
Adam Cohenea0818d2011-09-30 20:06:58 -0700199 mFolderName.setOnFocusChangeListener(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700200
Adam Cohen76fc0852011-06-17 13:26:23 -0700201 // We disable action mode for now since it messes up the view on phones
202 mFolderName.setCustomSelectionActionModeCallback(mActionModeCallback);
Adam Cohen76fc0852011-06-17 13:26:23 -0700203 mFolderName.setOnEditorActionListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700204 mFolderName.setSelectAllOnFocus(true);
Adam Cohen7a14d0b2011-06-24 11:36:35 -0700205 mFolderName.setInputType(mFolderName.getInputType() |
206 InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800207
Sunny Goyalb8634152015-04-09 14:17:14 -0700208 mFooter = findViewById(R.id.folder_footer);
Sunny Goyalb8634152015-04-09 14:17:14 -0700209
Sunny Goyal290800b2015-03-05 11:33:33 -0800210 // We find out how tall footer wants to be (it is set to wrap_content), so that
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800211 // we can allocate the appropriate amount of space for it.
212 int measureSpec = MeasureSpec.UNSPECIFIED;
Sunny Goyal290800b2015-03-05 11:33:33 -0800213 mFooter.measure(measureSpec, measureSpec);
214 mFooterHeight = mFooter.getMeasuredHeight();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800215 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700216
Adam Cohen76fc0852011-06-17 13:26:23 -0700217 private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
218 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
219 return false;
220 }
221
222 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
223 return false;
224 }
225
226 public void onDestroyActionMode(ActionMode mode) {
227 }
228
229 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
230 return false;
231 }
232 };
233
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800234 public void onClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700235 Object tag = v.getTag();
236 if (tag instanceof ShortcutInfo) {
Adam Cohenb5fe60c2013-06-06 22:03:51 -0700237 mLauncher.onClick(v);
Adam Cohendf2cc412011-04-27 16:56:57 -0700238 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800239 }
240
241 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -0700242 // Return if global dragging is not enabled
243 if (!mLauncher.isDraggingEnabled()) return true;
Sunny Goyale9b651e2015-04-24 11:44:51 -0700244 return beginDrag(v, false);
245 }
Winson Chung36a62fe2012-05-06 18:04:42 -0700246
Sunny Goyale9b651e2015-04-24 11:44:51 -0700247 private boolean beginDrag(View v, boolean accessible) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700248 Object tag = v.getTag();
249 if (tag instanceof ShortcutInfo) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700250 ShortcutInfo item = (ShortcutInfo) tag;
251 if (!v.isInTouchMode()) {
252 return false;
253 }
254
Sunny Goyale9b651e2015-04-24 11:44:51 -0700255 mLauncher.getWorkspace().beginDragShared(v, new Point(), this, accessible);
Adam Cohen76078c42011-06-09 15:06:52 -0700256
257 mCurrentDragInfo = item;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800258 mEmptyCellRank = item.rank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700259 mCurrentDragView = v;
Adam Cohenfc53cd22011-07-20 15:45:11 -0700260
Sunny Goyal290800b2015-03-05 11:33:33 -0800261 mContent.removeItem(mCurrentDragView);
Adam Cohenfc53cd22011-07-20 15:45:11 -0700262 mInfo.remove(mCurrentDragInfo);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700263 mDragInProgress = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700264 mItemAddedBackToSelfViaIcon = false;
Adam Cohendf2cc412011-04-27 16:56:57 -0700265 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800266 return true;
267 }
268
Sunny Goyale9b651e2015-04-24 11:44:51 -0700269 @Override
270 public void startDrag(CellInfo cellInfo, boolean accessible) {
271 beginDrag(cellInfo.cell, accessible);
272 }
273
274 @Override
275 public void enableAccessibleDrag(boolean enable) {
276 mLauncher.getSearchBar().enableAccessibleDrag(enable);
277 for (int i = 0; i < mContent.getChildCount(); i++) {
278 mContent.getPageAt(i).enableAccessibleDrag(enable, CellLayout.FOLDER_ACCESSIBILITY_DRAG);
279 }
Sunny Goyalccc414b2015-04-30 12:28:16 -0700280
281 mFooter.setImportantForAccessibility(enable ? IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS :
282 IMPORTANT_FOR_ACCESSIBILITY_AUTO);
Sunny Goyale9b651e2015-04-24 11:44:51 -0700283 mLauncher.getWorkspace().setAddNewPageOnDrag(!enable);
284 }
285
Adam Cohen76fc0852011-06-17 13:26:23 -0700286 public boolean isEditingName() {
287 return mIsEditingName;
288 }
289
290 public void startEditingFolderName() {
Adam Cohena65beee2011-06-27 21:32:23 -0700291 mFolderName.setHint("");
Adam Cohen76fc0852011-06-17 13:26:23 -0700292 mIsEditingName = true;
293 }
294
295 public void dismissEditingName() {
296 mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
297 doneEditingFolderName(true);
298 }
299
300 public void doneEditingFolderName(boolean commit) {
Adam Cohena65beee2011-06-27 21:32:23 -0700301 mFolderName.setHint(sHintText);
Adam Cohen1df26a32011-08-12 16:15:23 -0700302 // Convert to a string here to ensure that no other state associated with the text field
303 // gets saved.
Sunny Goyal230eade2015-06-18 12:48:51 -0700304 String newTitle = mFolderName.getText().toString();
Adam Cohen3371da02011-10-25 21:38:29 -0700305 mInfo.setTitle(newTitle);
Adam Cohen76fc0852011-06-17 13:26:23 -0700306 LauncherModel.updateItemInDatabase(mLauncher, mInfo);
Adam Cohenac56cff2011-09-28 20:45:37 -0700307
Adam Cohen3371da02011-10-25 21:38:29 -0700308 if (commit) {
309 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Sunny Goyal230eade2015-06-18 12:48:51 -0700310 String.format(getContext().getString(R.string.folder_renamed), newTitle));
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)
Sunny Goyalc1cd23b2015-06-01 17:15:34 -0700562 .setInterpolator(Utilities.isLmpOrAbove() ?
563 AnimationUtils.loadInterpolator(mLauncher,
564 android.R.interpolator.fast_out_slow_in)
565 : new LogDecelerateInterpolator(100, 0));
Sunny Goyalb7e15ad2015-05-07 14:51:31 -0700566 mContent.animateMarkers();
567
568 if (updateAnimationFlag) {
569 mInfo.setOption(FolderInfo.FLAG_MULTI_PAGE_ANIMATION, true, mLauncher);
570 }
571 }
572 });
573 } else {
574 mFolderName.setTranslationX(0);
575 mContent.setMarkerScale(1);
576 }
577
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700578 openFolderAnim.start();
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800579
580 // Make sure the folder picks up the last drag move even if the finger doesn't move.
581 if (mDragController.isDragging()) {
582 mDragController.forceTouchMove();
583 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700584
Sunny Goyalb8634152015-04-09 14:17:14 -0700585 FolderPagedView pages = (FolderPagedView) mContent;
586 pages.verifyVisibleHighResIcons(pages.getNextPage());
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800587 }
588
589 public void beginExternalDrag(ShortcutInfo item) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800590 mCurrentDragInfo = item;
Sunny Goyal5d85c442015-03-10 13:14:47 -0700591 mEmptyCellRank = mContent.allocateRankForNewItem(item);
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800592 mIsExternalDrag = true;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800593 mDragInProgress = true;
Sunny Goyalb8634152015-04-09 14:17:14 -0700594
Sunny Goyalf4066152015-04-15 09:42:19 -0700595 // Since this folder opened by another controller, it might not get onDrop or
596 // onDropComplete. Perform cleanup once drag-n-drop ends.
597 mDragController.addDragListener(this);
598 }
599
600 @Override
601 public void onDragStart(DragSource source, Object info, int dragAction) { }
602
603 @Override
604 public void onDragEnd() {
605 if (mIsExternalDrag && mDragInProgress) {
606 completeDragExit();
607 }
608 mDragController.removeDragListener(this);
Adam Cohendf2cc412011-04-27 16:56:57 -0700609 }
610
Adam Cohen091440a2015-03-18 14:16:05 -0700611 @Thunk void sendCustomAccessibilityEvent(int type, String text) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700612 AccessibilityManager accessibilityManager = (AccessibilityManager)
613 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
614 if (accessibilityManager.isEnabled()) {
Adam Cohen3371da02011-10-25 21:38:29 -0700615 AccessibilityEvent event = AccessibilityEvent.obtain(type);
616 onInitializeAccessibilityEvent(event);
617 event.getText().add(text);
Michael Jurka8b805b12012-04-18 14:23:14 -0700618 accessibilityManager.sendAccessibilityEvent(event);
Adam Cohen3371da02011-10-25 21:38:29 -0700619 }
620 }
621
Adam Cohendf2cc412011-04-27 16:56:57 -0700622 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700623 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800624 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
625 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
626 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
Michael Jurka032e6ba2013-04-22 15:08:42 +0200627 final ObjectAnimator oa =
Michael Jurka2ecf9952012-06-18 12:52:28 -0700628 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohendf2cc412011-04-27 16:56:57 -0700629
Adam Cohen2801caf2011-05-13 20:57:39 -0700630 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700631 @Override
632 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700633 onCloseComplete();
Michael Jurka0121c3e2012-05-31 08:36:04 -0700634 setLayerType(LAYER_TYPE_NONE, null);
Adam Cohen2801caf2011-05-13 20:57:39 -0700635 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700636 }
637 @Override
638 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700639 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700640 getContext().getString(R.string.folder_closed));
Adam Cohendf2cc412011-04-27 16:56:57 -0700641 mState = STATE_ANIMATING;
642 }
643 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700644 oa.setDuration(mExpandDuration);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700645 setLayerType(LAYER_TYPE_HARDWARE, null);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100646 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700647 }
648
Adam Cohencb3382b2011-05-24 14:07:08 -0700649 public boolean acceptDrop(DragObject d) {
650 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700651 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700652 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
653 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
654 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700655 }
656
Adam Cohencb3382b2011-05-24 14:07:08 -0700657 public void onDragEnter(DragObject d) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800658 mPrevTargetRank = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700659 mOnExitAlarm.cancelAlarm();
Sunny Goyalb8634152015-04-09 14:17:14 -0700660 // Get the area offset such that the folder only closes if half the drag icon width
661 // is outside the folder area
662 mScrollAreaOffset = d.dragView.getDragRegionWidth() / 2 - d.xOffset;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700663 }
664
665 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
666 public void onAlarm(Alarm alarm) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800667 mContent.realTimeReorder(mEmptyCellRank, mTargetRank);
668 mEmptyCellRank = mTargetRank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700669 }
670 };
671
Sunny Goyalc46bfef2015-01-05 12:40:08 -0800672 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohen2374abf2013-04-16 14:56:57 -0700673 public boolean isLayoutRtl() {
674 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
675 }
676
Sunny Goyal48461932015-03-09 17:41:09 -0700677 @Override
Adam Cohencb3382b2011-05-24 14:07:08 -0700678 public void onDragOver(DragObject d) {
Sunny Goyal48461932015-03-09 17:41:09 -0700679 onDragOver(d, REORDER_DELAY);
680 }
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700681
Sunny Goyal48461932015-03-09 17:41:09 -0700682 private int getTargetRank(DragObject d, float[] recycle) {
683 recycle = d.getVisualCenter(recycle);
684 return mContent.findNearestArea(
685 (int) recycle[0] - getPaddingLeft(), (int) recycle[1] - getPaddingTop());
686 }
687
Adam Cohen091440a2015-03-18 14:16:05 -0700688 @Thunk void onDragOver(DragObject d, int reorderDelay) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700689 if (mScrollPauseAlarm.alarmPending()) {
Sunny Goyal48461932015-03-09 17:41:09 -0700690 return;
691 }
692 final float[] r = new float[2];
693 mTargetRank = getTargetRank(d, r);
694
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800695 if (mTargetRank != mPrevTargetRank) {
Alan Viverette4cda5b72013-08-28 17:53:41 -0700696 mReorderAlarm.cancelAlarm();
Sunny Goyalc46bfef2015-01-05 12:40:08 -0800697 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
698 mReorderAlarm.setAlarm(REORDER_DELAY);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800699 mPrevTargetRank = mTargetRank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700700 }
Sunny Goyal48461932015-03-09 17:41:09 -0700701
Sunny Goyal48461932015-03-09 17:41:09 -0700702 float x = r[0];
Sunny Goyalb8634152015-04-09 14:17:14 -0700703 int currentPage = mContent.getNextPage();
Sunny Goyal48461932015-03-09 17:41:09 -0700704
Sunny Goyalb8634152015-04-09 14:17:14 -0700705 float cellOverlap = mContent.getCurrentCellLayout().getCellWidth()
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700706 * ICON_OVERSCROLL_WIDTH_FACTOR;
707 boolean isOutsideLeftEdge = x < cellOverlap;
708 boolean isOutsideRightEdge = x > (getWidth() - cellOverlap);
Sunny Goyal48461932015-03-09 17:41:09 -0700709
Sunny Goyal70660032015-05-14 00:07:08 -0700710 if (currentPage > 0 && (mContent.mIsRtl ? isOutsideRightEdge : isOutsideLeftEdge)) {
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700711 showScrollHint(DragController.SCROLL_LEFT, d);
Sunny Goyalb8634152015-04-09 14:17:14 -0700712 } else if (currentPage < (mContent.getPageCount() - 1)
Sunny Goyal70660032015-05-14 00:07:08 -0700713 && (mContent.mIsRtl ? isOutsideLeftEdge : isOutsideRightEdge)) {
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700714 showScrollHint(DragController.SCROLL_RIGHT, d);
Sunny Goyal48461932015-03-09 17:41:09 -0700715 } else {
716 mOnScrollHintAlarm.cancelAlarm();
717 if (mScrollHintDir != DragController.SCROLL_NONE) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700718 mContent.clearScrollHint();
Sunny Goyal48461932015-03-09 17:41:09 -0700719 mScrollHintDir = DragController.SCROLL_NONE;
720 }
721 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700722 }
723
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700724 private void showScrollHint(int direction, DragObject d) {
725 // Show scroll hint on the right
726 if (mScrollHintDir != direction) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700727 mContent.showScrollHint(direction);
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700728 mScrollHintDir = direction;
729 }
730
731 // Set alarm for when the hint is complete
732 if (!mOnScrollHintAlarm.alarmPending() || mCurrentScrollDir != direction) {
733 mCurrentScrollDir = direction;
734 mOnScrollHintAlarm.cancelAlarm();
735 mOnScrollHintAlarm.setOnAlarmListener(new OnScrollHintListener(d));
736 mOnScrollHintAlarm.setAlarm(SCROLL_HINT_DURATION);
737
738 mReorderAlarm.cancelAlarm();
739 mTargetRank = mEmptyCellRank;
740 }
741 }
742
Adam Cohenbfbfd262011-06-13 16:55:12 -0700743 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
744 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700745 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700746 }
747 };
748
Adam Cohen95bb8002011-07-03 23:40:28 -0700749 public void completeDragExit() {
Sunny Goyalf4066152015-04-15 09:42:19 -0700750 if (mInfo.opened) {
751 mLauncher.closeFolder();
752 mRearrangeOnClose = true;
Sunny Goyal31abc292015-05-01 10:42:32 -0700753 } else if (mState == STATE_ANIMATING) {
754 mRearrangeOnClose = true;
Sunny Goyalf4066152015-04-15 09:42:19 -0700755 } else {
756 rearrangeChildren();
Sunny Goyal31abc292015-05-01 10:42:32 -0700757 clearDragInfo();
Sunny Goyalf4066152015-04-15 09:42:19 -0700758 }
Sunny Goyal31abc292015-05-01 10:42:32 -0700759 }
760
761 private void clearDragInfo() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700762 mCurrentDragInfo = null;
763 mCurrentDragView = null;
764 mSuppressOnAdd = false;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800765 mIsExternalDrag = false;
Adam Cohen3e8f8112011-07-02 18:03:00 -0700766 }
767
Adam Cohencb3382b2011-05-24 14:07:08 -0700768 public void onDragExit(DragObject d) {
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700769 // We only close the folder if this is a true drag exit, ie. not because
770 // a drop has occurred above the folder.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700771 if (!d.dragComplete) {
772 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
773 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
774 }
775 mReorderAlarm.cancelAlarm();
Sunny Goyal48461932015-03-09 17:41:09 -0700776
Sunny Goyalb8634152015-04-09 14:17:14 -0700777 mOnScrollHintAlarm.cancelAlarm();
778 mScrollPauseAlarm.cancelAlarm();
779 if (mScrollHintDir != DragController.SCROLL_NONE) {
780 mContent.clearScrollHint();
781 mScrollHintDir = DragController.SCROLL_NONE;
Sunny Goyal48461932015-03-09 17:41:09 -0700782 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700783 }
784
Sunny Goyale9b651e2015-04-24 11:44:51 -0700785 /**
786 * When performing an accessibility drop, onDrop is sent immediately after onDragEnter. So we
787 * need to complete all transient states based on timers.
788 */
789 @Override
790 public void prepareAccessibilityDrop() {
791 if (mReorderAlarm.alarmPending()) {
792 mReorderAlarm.cancelAlarm();
793 mReorderAlarmListener.onAlarm(mReorderAlarm);
794 }
795 }
796
Michael Jurka1e2f4652013-07-08 18:03:46 -0700797 public void onDropCompleted(final View target, final DragObject d,
798 final boolean isFlingToDelete, final boolean success) {
799 if (mDeferDropAfterUninstall) {
Michael Jurkaf3007582013-08-21 14:33:57 +0200800 Log.d(TAG, "Deferred handling drop because waiting for uninstall.");
Michael Jurka1e2f4652013-07-08 18:03:46 -0700801 mDeferredAction = new Runnable() {
802 public void run() {
803 onDropCompleted(target, d, isFlingToDelete, success);
804 mDeferredAction = null;
805 }
806 };
807 return;
808 }
809
810 boolean beingCalledAfterUninstall = mDeferredAction != null;
811 boolean successfulDrop =
812 success && (!beingCalledAfterUninstall || mUninstallSuccessful);
Winson Chung5f8afe62013-08-12 16:19:28 -0700813
Michael Jurka1e2f4652013-07-08 18:03:46 -0700814 if (successfulDrop) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800815 if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon && target != this) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700816 replaceFolderWithFinalItem();
817 }
818 } else {
819 // The drag failed, we need to return the item to the folder
Sunny Goyalb8c663c2015-04-23 11:43:48 -0700820 ShortcutInfo info = (ShortcutInfo) d.dragInfo;
821 View icon = (mCurrentDragView != null && mCurrentDragView.getTag() == info)
822 ? mCurrentDragView : mContent.createNewView(info);
823 ArrayList<View> views = getItemsInReadingOrder();
824 views.add(info.rank, icon);
825 mContent.arrangeChildren(views, views.size());
826 mItemsInvalidated = true;
827
828 mSuppressOnAdd = true;
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700829 mFolderIcon.onDrop(d);
Sunny Goyalb8c663c2015-04-23 11:43:48 -0700830 mSuppressOnAdd = false;
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700831 }
832
833 if (target != this) {
834 if (mOnExitAlarm.alarmPending()) {
835 mOnExitAlarm.cancelAlarm();
Michael Jurka54554252013-08-01 12:52:23 +0200836 if (!successfulDrop) {
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700837 mSuppressFolderDeletion = true;
838 }
Sunny Goyal5d85c442015-03-10 13:14:47 -0700839 mScrollPauseAlarm.cancelAlarm();
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700840 completeDragExit();
841 }
Adam Cohen9c58d822013-12-13 17:18:10 -0800842 }
843
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700844 mDeleteFolderOnDropCompleted = false;
845 mDragInProgress = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700846 mItemAddedBackToSelfViaIcon = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700847 mCurrentDragInfo = null;
848 mCurrentDragView = null;
849 mSuppressOnAdd = false;
Adam Cohen4045eb72011-10-06 11:44:26 -0700850
851 // Reordering may have occured, and we need to save the new item locations. We do this once
852 // at the end to prevent unnecessary database operations.
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700853 updateItemLocationsInDatabaseBatch();
Sunny Goyalb7e15ad2015-05-07 14:51:31 -0700854
855 // Use the item count to check for multi-page as the folder UI may not have
856 // been refreshed yet.
857 if (getItemCount() <= mContent.itemsPerPage()) {
858 // Show the animation, next time something is added to the folder.
859 mInfo.setOption(FolderInfo.FLAG_MULTI_PAGE_ANIMATION, false, mLauncher);
860 }
861
Adam Cohen4045eb72011-10-06 11:44:26 -0700862 }
863
Sunny Goyalfa401a12015-04-10 13:45:42 -0700864 @Override
Michael Jurka1e2f4652013-07-08 18:03:46 -0700865 public void deferCompleteDropAfterUninstallActivity() {
866 mDeferDropAfterUninstall = true;
867 }
868
Sunny Goyalfa401a12015-04-10 13:45:42 -0700869 @Override
Michael Jurka1e2f4652013-07-08 18:03:46 -0700870 public void onUninstallActivityReturned(boolean success) {
871 mDeferDropAfterUninstall = false;
872 mUninstallSuccessful = success;
873 if (mDeferredAction != null) {
874 mDeferredAction.run();
875 }
876 }
877
Winson Chunga48487a2012-03-20 16:19:37 -0700878 @Override
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800879 public float getIntrinsicIconScaleFactor() {
880 return 1f;
881 }
882
883 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800884 public boolean supportsFlingToDelete() {
885 return true;
886 }
887
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000888 @Override
889 public boolean supportsAppInfoDropTarget() {
890 return false;
891 }
892
893 @Override
894 public boolean supportsDeleteDropTarget() {
895 return true;
896 }
897
Sunny Goyalddec7342015-04-29 18:12:37 -0700898 @Override
899 public void onFlingToDelete(DragObject d, PointF vec) {
Winson Chunga48487a2012-03-20 16:19:37 -0700900 // Do nothing
901 }
902
903 @Override
904 public void onFlingToDeleteCompleted() {
905 // Do nothing
906 }
907
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700908 private void updateItemLocationsInDatabaseBatch() {
909 ArrayList<View> list = getItemsInReadingOrder();
910 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
911 for (int i = 0; i < list.size(); i++) {
912 View v = list.get(i);
913 ItemInfo info = (ItemInfo) v.getTag();
Sunny Goyal08f72612015-01-05 13:41:43 -0800914 info.rank = i;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700915 items.add(info);
916 }
917
918 LauncherModel.moveItemsInDatabase(mLauncher, items, mInfo.id, 0);
919 }
920
Adam Cohene25af792013-06-06 23:08:25 -0700921 public void addItemLocationsInDatabase() {
922 ArrayList<View> list = getItemsInReadingOrder();
923 for (int i = 0; i < list.size(); i++) {
924 View v = list.get(i);
925 ItemInfo info = (ItemInfo) v.getTag();
926 LauncherModel.addItemToDatabase(mLauncher, info, mInfo.id, 0,
Winson Chung8f1eff72015-05-28 17:33:40 -0700927 info.cellX, info.cellY);
Adam Cohene25af792013-06-06 23:08:25 -0700928 }
929 }
930
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700931 public void notifyDrop() {
932 if (mDragInProgress) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700933 mItemAddedBackToSelfViaIcon = true;
Adam Cohen76078c42011-06-09 15:06:52 -0700934 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700935 }
936
937 public boolean isDropEnabled() {
938 return true;
939 }
940
Adam Cohen2801caf2011-05-13 20:57:39 -0700941 public boolean isFull() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800942 return mContent.isFull();
Adam Cohen2801caf2011-05-13 20:57:39 -0700943 }
944
945 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700946 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700947
Winson Chung892c74d2013-08-22 16:15:50 -0700948 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700949 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700950 int height = getFolderHeight();
Adam Cohen2801caf2011-05-13 20:57:39 -0700951
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800952 float scale = parent.getDescendantRectRelativeToSelf(mFolderIcon, sTempRect);
Adam Cohen8e776a62011-06-28 18:10:06 -0700953
Adam Cohen2e6da152015-05-06 11:42:25 -0700954 DeviceProfile grid = mLauncher.getDeviceProfile();
Winson Chungaf40f202013-09-18 18:26:31 -0700955
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800956 int centerX = (int) (sTempRect.left + sTempRect.width() * scale / 2);
957 int centerY = (int) (sTempRect.top + sTempRect.height() * scale / 2);
Adam Cohen2801caf2011-05-13 20:57:39 -0700958 int centeredLeft = centerX - width / 2;
959 int centeredTop = centerY - height / 2;
Winson Chung3057b1c2013-10-10 17:35:35 -0700960 int currentPage = mLauncher.getWorkspace().getNextPage();
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800961 // In case the workspace is scrolling, we need to use the final scroll to compute
962 // the folders bounds.
963 mLauncher.getWorkspace().setFinalScrollForPageChange(currentPage);
Adam Cohen35e7e642011-07-17 14:47:18 -0700964 // We first fetch the currently visible CellLayoutChildren
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800965 CellLayout currentLayout = (CellLayout) mLauncher.getWorkspace().getChildAt(currentPage);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700966 ShortcutAndWidgetContainer boundingLayout = currentLayout.getShortcutsAndWidgets();
Adam Cohen35e7e642011-07-17 14:47:18 -0700967 Rect bounds = new Rect();
968 parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800969 // We reset the workspaces scroll
970 mLauncher.getWorkspace().resetFinalScrollForPageChange(currentPage);
Adam Cohen2801caf2011-05-13 20:57:39 -0700971
Adam Cohen35e7e642011-07-17 14:47:18 -0700972 // We need to bound the folder to the currently visible CellLayoutChildren
973 int left = Math.min(Math.max(bounds.left, centeredLeft),
974 bounds.left + bounds.width() - width);
975 int top = Math.min(Math.max(bounds.top, centeredTop),
976 bounds.top + bounds.height() - height);
Sunny Goyalc6205602015-05-21 20:46:33 -0700977 if (grid.isPhone && (grid.availableWidthPx - width) < grid.iconSizePx) {
Winson Chungaf40f202013-09-18 18:26:31 -0700978 // Center the folder if it is full (on phones only)
979 left = (grid.availableWidthPx - width) / 2;
980 } else if (width >= bounds.width()) {
981 // If the folder doesn't fit within the bounds, center it about the desired bounds
Adam Cohen35e7e642011-07-17 14:47:18 -0700982 left = bounds.left + (bounds.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700983 }
Adam Cohen35e7e642011-07-17 14:47:18 -0700984 if (height >= bounds.height()) {
985 top = bounds.top + (bounds.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700986 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700987
988 int folderPivotX = width / 2 + (centeredLeft - left);
989 int folderPivotY = height / 2 + (centeredTop - top);
990 setPivotX(folderPivotX);
991 setPivotY(folderPivotY);
Adam Cohen268c4752012-06-06 17:47:33 -0700992 mFolderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700993 (1.0f * folderPivotX / width));
Adam Cohen268c4752012-06-06 17:47:33 -0700994 mFolderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700995 (1.0f * folderPivotY / height));
Adam Cohen3bf84d32012-05-07 20:17:14 -0700996
Adam Cohen662b5982011-12-13 17:45:21 -0800997 lp.width = width;
998 lp.height = height;
999 lp.x = left;
1000 lp.y = top;
Adam Cohen2801caf2011-05-13 20:57:39 -07001001 }
1002
Adam Cohen268c4752012-06-06 17:47:33 -07001003 float getPivotXForIconAnimation() {
1004 return mFolderIconPivotX;
1005 }
1006 float getPivotYForIconAnimation() {
1007 return mFolderIconPivotY;
1008 }
1009
Winson Chung892c74d2013-08-22 16:15:50 -07001010 private int getContentAreaHeight() {
Adam Cohen2e6da152015-05-06 11:42:25 -07001011 DeviceProfile grid = mLauncher.getDeviceProfile();
Sunny Goyalc6205602015-05-21 20:46:33 -07001012 Rect workspacePadding = grid.getWorkspacePadding(mContent.mIsRtl);
Winson Chung892c74d2013-08-22 16:15:50 -07001013 int maxContentAreaHeight = grid.availableHeightPx -
Winson Chung892c74d2013-08-22 16:15:50 -07001014 workspacePadding.top - workspacePadding.bottom -
Sunny Goyal290800b2015-03-05 11:33:33 -08001015 mFooterHeight;
Adam Cohen1960ea42013-11-12 11:33:14 +00001016 int height = Math.min(maxContentAreaHeight,
Winson Chung892c74d2013-08-22 16:15:50 -07001017 mContent.getDesiredHeight());
Adam Cohen1960ea42013-11-12 11:33:14 +00001018 return Math.max(height, MIN_CONTENT_DIMEN);
1019 }
1020
1021 private int getContentAreaWidth() {
1022 return Math.max(mContent.getDesiredWidth(), MIN_CONTENT_DIMEN);
Winson Chung892c74d2013-08-22 16:15:50 -07001023 }
1024
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001025 private int getFolderHeight() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001026 return getFolderHeight(getContentAreaHeight());
1027 }
1028
1029 private int getFolderHeight(int contentAreaHeight) {
Sunny Goyal290800b2015-03-05 11:33:33 -08001030 return getPaddingTop() + getPaddingBottom() + contentAreaHeight + mFooterHeight;
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001031 }
Adam Cohen234c4cd2011-07-17 21:03:04 -07001032
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001033 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001034 int contentWidth = getContentAreaWidth();
1035 int contentHeight = getContentAreaHeight();
Adam Cohen1960ea42013-11-12 11:33:14 +00001036
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001037 int contentAreaWidthSpec = MeasureSpec.makeMeasureSpec(contentWidth, MeasureSpec.EXACTLY);
1038 int contentAreaHeightSpec = MeasureSpec.makeMeasureSpec(contentHeight, MeasureSpec.EXACTLY);
1039
1040 mContent.setFixedSize(contentWidth, contentHeight);
1041 mContentWrapper.measure(contentAreaWidthSpec, contentAreaHeightSpec);
Sunny Goyala07c2f52015-06-01 11:00:38 -07001042
1043 if (mContent.getChildCount() > 0) {
1044 int cellIconGap = (mContent.getPageAt(0).getCellWidth()
1045 - mLauncher.getDeviceProfile().iconSizePx) / 2;
1046 mFooter.setPadding(mContent.getPaddingLeft() + cellIconGap,
1047 mFooter.getPaddingTop(),
1048 mContent.getPaddingRight() + cellIconGap,
1049 mFooter.getPaddingBottom());
1050 }
Sunny Goyal290800b2015-03-05 11:33:33 -08001051 mFooter.measure(contentAreaWidthSpec,
1052 MeasureSpec.makeMeasureSpec(mFooterHeight, MeasureSpec.EXACTLY));
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001053
1054 int folderWidth = getPaddingLeft() + getPaddingRight() + contentWidth;
1055 int folderHeight = getFolderHeight(contentHeight);
1056 setMeasuredDimension(folderWidth, folderHeight);
Adam Cohen234c4cd2011-07-17 21:03:04 -07001057 }
1058
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001059 /**
1060 * Rearranges the children based on their rank.
1061 */
1062 public void rearrangeChildren() {
1063 rearrangeChildren(-1);
1064 }
Adam Cohen2801caf2011-05-13 20:57:39 -07001065
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001066 /**
1067 * Rearranges the children based on their rank.
1068 * @param itemCount if greater than the total children count, empty spaces are left at the end,
1069 * otherwise it is ignored.
1070 */
1071 public void rearrangeChildren(int itemCount) {
1072 ArrayList<View> views = getItemsInReadingOrder();
1073 mContent.arrangeChildren(views, Math.max(itemCount, views.size()));
Adam Cohen7c693212011-05-18 15:26:57 -07001074 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -07001075 }
1076
Sunny Goyalc4918352015-03-10 18:15:48 -07001077 // TODO remove this once GSA code fix is submitted
1078 public ViewGroup getContent() {
1079 return (ViewGroup) mContent;
1080 }
1081
Adam Cohena9cf38f2011-05-02 15:36:58 -07001082 public int getItemCount() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001083 return mContent.getItemCount();
Adam Cohena9cf38f2011-05-02 15:36:58 -07001084 }
1085
Adam Cohen091440a2015-03-18 14:16:05 -07001086 @Thunk void onCloseComplete() {
Adam Cohen05e0f402011-08-01 12:12:49 -07001087 DragLayer parent = (DragLayer) getParent();
Michael Jurka5649c282012-06-18 10:33:21 -07001088 if (parent != null) {
1089 parent.removeView(this);
1090 }
Adam Cohen4554ee12011-08-03 16:13:21 -07001091 mDragController.removeDropTarget((DropTarget) this);
Adam Cohen05e0f402011-08-01 12:12:49 -07001092 clearFocus();
Adam Cohenac56cff2011-09-28 20:45:37 -07001093 mFolderIcon.requestFocus();
Adam Cohen05e0f402011-08-01 12:12:49 -07001094
Adam Cohen2801caf2011-05-13 20:57:39 -07001095 if (mRearrangeOnClose) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001096 rearrangeChildren();
Adam Cohen2801caf2011-05-13 20:57:39 -07001097 mRearrangeOnClose = false;
1098 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001099 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001100 if (!mDragInProgress && !mSuppressFolderDeletion) {
1101 replaceFolderWithFinalItem();
1102 } else if (mDragInProgress) {
1103 mDeleteFolderOnDropCompleted = true;
1104 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001105 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001106 mSuppressFolderDeletion = false;
Sunny Goyal31abc292015-05-01 10:42:32 -07001107 clearDragInfo();
Adam Cohenafb01ee2011-06-23 15:38:03 -07001108 }
1109
Adam Cohen091440a2015-03-18 14:16:05 -07001110 @Thunk void replaceFolderWithFinalItem() {
Adam Cohenafb01ee2011-06-23 15:38:03 -07001111 // Add the last remaining child to the workspace in place of the folder
Adam Cohenfb91f302012-06-11 15:45:18 -07001112 Runnable onCompleteRunnable = new Runnable() {
1113 @Override
1114 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -07001115 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screenId);
Adam Cohenafb01ee2011-06-23 15:38:03 -07001116
Winson Chung33231f52013-12-09 16:57:45 -08001117 View child = null;
Adam Cohenfb91f302012-06-11 15:45:18 -07001118 // Move the item from the folder to the workspace, in the position of the folder
1119 if (getItemCount() == 1) {
1120 ShortcutInfo finalItem = mInfo.contents.get(0);
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001121 child = mLauncher.createShortcut(cellLayout, finalItem);
Adam Cohenfb91f302012-06-11 15:45:18 -07001122 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -07001123 mInfo.screenId, mInfo.cellX, mInfo.cellY);
Adam Cohenfb91f302012-06-11 15:45:18 -07001124 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001125 if (getItemCount() <= 1) {
1126 // Remove the folder
1127 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
Dan Sandler0eb687f2014-01-10 13:24:55 -05001128 if (cellLayout != null) {
1129 // b/12446428 -- sometimes the cell layout has already gone away?
1130 cellLayout.removeView(mFolderIcon);
1131 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001132 if (mFolderIcon instanceof DropTarget) {
1133 mDragController.removeDropTarget((DropTarget) mFolderIcon);
1134 }
1135 mLauncher.removeFolder(mInfo);
1136 }
Adam Cohenc5e63f32012-07-12 16:16:57 -07001137 // We add the child after removing the folder to prevent both from existing at
Winson Chung0e6a7132013-08-23 12:55:10 -07001138 // the same time in the CellLayout. We need to add the new item with addInScreenFromBind()
1139 // to ensure that hotseat items are placed correctly.
Adam Cohenc5e63f32012-07-12 16:16:57 -07001140 if (child != null) {
Winson Chung0e6a7132013-08-23 12:55:10 -07001141 mLauncher.getWorkspace().addInScreenFromBind(child, mInfo.container, mInfo.screenId,
Adam Cohenc5e63f32012-07-12 16:16:57 -07001142 mInfo.cellX, mInfo.cellY, mInfo.spanX, mInfo.spanY);
1143 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001144 }
1145 };
Sunny Goyalbc753352015-03-05 09:40:44 -08001146 View finalChild = mContent.getLastItem();
Adam Cohenfb91f302012-06-11 15:45:18 -07001147 if (finalChild != null) {
1148 mFolderIcon.performDestroyAnimation(finalChild, onCompleteRunnable);
Winson Chung33231f52013-12-09 16:57:45 -08001149 } else {
1150 onCompleteRunnable.run();
Adam Cohenafb01ee2011-06-23 15:38:03 -07001151 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001152 mDestroyed = true;
1153 }
1154
1155 boolean isDestroyed() {
1156 return mDestroyed;
Adam Cohen2801caf2011-05-13 20:57:39 -07001157 }
1158
Adam Cohenac56cff2011-09-28 20:45:37 -07001159 // This method keeps track of the last item in the folder for the purposes
1160 // of keyboard focus
Sunny Goyal290800b2015-03-05 11:33:33 -08001161 public void updateTextViewFocus() {
Sunny Goyalbc753352015-03-05 09:40:44 -08001162 View lastChild = mContent.getLastItem();
Adam Cohenac56cff2011-09-28 20:45:37 -07001163 if (lastChild != null) {
1164 mFolderName.setNextFocusDownId(lastChild.getId());
1165 mFolderName.setNextFocusRightId(lastChild.getId());
1166 mFolderName.setNextFocusLeftId(lastChild.getId());
1167 mFolderName.setNextFocusUpId(lastChild.getId());
1168 }
1169 }
1170
Adam Cohenbfbfd262011-06-13 16:55:12 -07001171 public void onDrop(DragObject d) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001172 Runnable cleanUpRunnable = null;
1173
Adam Cohen689ff162014-05-08 17:27:56 -07001174 // If we are coming from All Apps space, we defer removing the extra empty screen
1175 // until the folder closes
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001176 if (d.dragSource != mLauncher.getWorkspace() && !(d.dragSource instanceof Folder)) {
1177 cleanUpRunnable = new Runnable() {
1178 @Override
1179 public void run() {
Adam Cohen689ff162014-05-08 17:27:56 -07001180 mLauncher.exitSpringLoadedDragModeDelayed(true,
1181 Launcher.EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
1182 null);
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001183 }
1184 };
Adam Cohenbfbfd262011-06-13 16:55:12 -07001185 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001186
Sunny Goyalb8634152015-04-09 14:17:14 -07001187 // If the icon was dropped while the page was being scrolled, we need to compute
1188 // the target location again such that the icon is placed of the final page.
1189 if (!mContent.rankOnCurrentPage(mEmptyCellRank)) {
1190 // Reorder again.
1191 mTargetRank = getTargetRank(d, null);
Sunny Goyal48461932015-03-09 17:41:09 -07001192
Sunny Goyalb8634152015-04-09 14:17:14 -07001193 // Rearrange items immediately.
1194 mReorderAlarmListener.onAlarm(mReorderAlarm);
Sunny Goyal48461932015-03-09 17:41:09 -07001195
Sunny Goyalb8634152015-04-09 14:17:14 -07001196 mOnScrollHintAlarm.cancelAlarm();
1197 mScrollPauseAlarm.cancelAlarm();
Sunny Goyal48461932015-03-09 17:41:09 -07001198 }
Sunny Goyalb8634152015-04-09 14:17:14 -07001199 mContent.completePendingPageChanges();
Sunny Goyal48461932015-03-09 17:41:09 -07001200
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001201 View currentDragView;
1202 ShortcutInfo si = mCurrentDragInfo;
1203 if (mIsExternalDrag) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001204 currentDragView = mContent.createAndAddViewForRank(si, mEmptyCellRank);
Sunny Goyal95abbb32014-08-04 10:53:22 -07001205 // Actually move the item in the database if it was an external drag. Call this
1206 // before creating the view, so that ShortcutInfo is updated appropriately.
1207 LauncherModel.addOrMoveItemInDatabase(
1208 mLauncher, si, mInfo.id, 0, si.cellX, si.cellY);
1209
1210 // We only need to update the locations if it doesn't get handled in #onDropCompleted.
1211 if (d.dragSource != this) {
1212 updateItemLocationsInDatabaseBatch();
1213 }
1214 mIsExternalDrag = false;
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001215 } else {
1216 currentDragView = mCurrentDragView;
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001217 mContent.addViewForRank(currentDragView, si, mEmptyCellRank);
Adam Cohenbfbfd262011-06-13 16:55:12 -07001218 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001219
1220 if (d.dragView.hasDrawn()) {
1221
1222 // Temporarily reset the scale such that the animation target gets calculated correctly.
1223 float scaleX = getScaleX();
1224 float scaleY = getScaleY();
1225 setScaleX(1.0f);
1226 setScaleY(1.0f);
1227 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, currentDragView,
1228 cleanUpRunnable, null);
1229 setScaleX(scaleX);
1230 setScaleY(scaleY);
1231 } else {
1232 d.deferDragViewCleanupPostAnimation = false;
1233 currentDragView.setVisibility(VISIBLE);
1234 }
1235 mItemsInvalidated = true;
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001236 rearrangeChildren();
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001237
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001238 // Temporarily suppress the listener, as we did all the work already here.
1239 mSuppressOnAdd = true;
1240 mInfo.add(si);
1241 mSuppressOnAdd = false;
Sunny Goyal4b020172014-08-28 14:51:14 -07001242 // Clear the drag info, as it is no longer being dragged.
1243 mCurrentDragInfo = null;
Sunny Goyalf4066152015-04-15 09:42:19 -07001244 mDragInProgress = false;
Sunny Goyalb7e15ad2015-05-07 14:51:31 -07001245
1246 if (mContent.getPageCount() > 1) {
1247 // The animation has already been shown while opening the folder.
1248 mInfo.setOption(FolderInfo.FLAG_MULTI_PAGE_ANIMATION, true, mLauncher);
1249 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001250 }
1251
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001252 // This is used so the item doesn't immediately appear in the folder when added. In one case
1253 // we need to create the illusion that the item isn't added back to the folder yet, to
1254 // to correspond to the animation of the icon back into the folder. This is
1255 public void hideItem(ShortcutInfo info) {
1256 View v = getViewForInfo(info);
1257 v.setVisibility(INVISIBLE);
1258 }
1259 public void showItem(ShortcutInfo info) {
1260 View v = getViewForInfo(info);
1261 v.setVisibility(VISIBLE);
1262 }
1263
Sunny Goyalb7e15ad2015-05-07 14:51:31 -07001264 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -07001265 public void onAdd(ShortcutInfo item) {
Adam Cohen05e0f402011-08-01 12:12:49 -07001266 // If the item was dropped onto this open folder, we have done the work associated
1267 // with adding the item to the folder, as indicated by mSuppressOnAdd being set
Adam Cohenbfbfd262011-06-13 16:55:12 -07001268 if (mSuppressOnAdd) return;
Sunny Goyal5d85c442015-03-10 13:14:47 -07001269 mContent.createAndAddViewForRank(item, mContent.allocateRankForNewItem(item));
Sunny Goyal2e688a82015-03-18 10:23:39 -07001270 mItemsInvalidated = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001271 LauncherModel.addOrMoveItemInDatabase(
1272 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
1273 }
1274
Adam Cohena9cf38f2011-05-02 15:36:58 -07001275 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -07001276 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001277 // If this item is being dragged from this open folder, we have already handled
1278 // the work associated with removing the item, so we don't have to do anything here.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001279 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001280 View v = getViewForInfo(item);
Sunny Goyal290800b2015-03-05 11:33:33 -08001281 mContent.removeItem(v);
Adam Cohen2801caf2011-05-13 20:57:39 -07001282 if (mState == STATE_ANIMATING) {
1283 mRearrangeOnClose = true;
1284 } else {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001285 rearrangeChildren();
Adam Cohen2801caf2011-05-13 20:57:39 -07001286 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001287 if (getItemCount() <= 1) {
1288 replaceFolderWithFinalItem();
1289 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001290 }
Adam Cohen7c693212011-05-18 15:26:57 -07001291
Sunny Goyalbc753352015-03-05 09:40:44 -08001292 private View getViewForInfo(final ShortcutInfo item) {
1293 return mContent.iterateOverItems(new ItemOperator() {
1294
1295 @Override
1296 public boolean evaluate(ItemInfo info, View view, View parent) {
1297 return info == item;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001298 }
Sunny Goyalbc753352015-03-05 09:40:44 -08001299 });
Adam Cohendf1e4e82011-06-24 15:57:39 -07001300 }
1301
Adam Cohen76078c42011-06-09 15:06:52 -07001302 public void onItemsChanged() {
Adam Cohenac56cff2011-09-28 20:45:37 -07001303 updateTextViewFocus();
Adam Cohen76078c42011-06-09 15:06:52 -07001304 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001305
Adam Cohen76fc0852011-06-17 13:26:23 -07001306 public void onTitleChanged(CharSequence title) {
1307 }
Adam Cohen76078c42011-06-09 15:06:52 -07001308
Adam Cohen7c693212011-05-18 15:26:57 -07001309 public ArrayList<View> getItemsInReadingOrder() {
1310 if (mItemsInvalidated) {
1311 mItemsInReadingOrder.clear();
Sunny Goyalbc753352015-03-05 09:40:44 -08001312 mContent.iterateOverItems(new ItemOperator() {
1313
1314 @Override
1315 public boolean evaluate(ItemInfo info, View view, View parent) {
1316 mItemsInReadingOrder.add(view);
1317 return false;
Adam Cohen7c693212011-05-18 15:26:57 -07001318 }
Sunny Goyalbc753352015-03-05 09:40:44 -08001319 });
Adam Cohen7c693212011-05-18 15:26:57 -07001320 mItemsInvalidated = false;
1321 }
1322 return mItemsInReadingOrder;
1323 }
Adam Cohen8dfcba42011-07-07 16:38:18 -07001324
1325 public void getLocationInDragLayer(int[] loc) {
1326 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
1327 }
Adam Cohenea0818d2011-09-30 20:06:58 -07001328
1329 public void onFocusChange(View v, boolean hasFocus) {
1330 if (v == mFolderName && hasFocus) {
1331 startEditingFolderName();
1332 }
1333 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001334
1335 @Override
1336 public void getHitRectRelativeToDragLayer(Rect outRect) {
1337 getHitRect(outRect);
Sunny Goyal48461932015-03-09 17:41:09 -07001338 outRect.left -= mScrollAreaOffset;
1339 outRect.right += mScrollAreaOffset;
1340 }
1341
Winson Chung8f1eff72015-05-28 17:33:40 -07001342 @Override
1343 public void fillInLaunchSourceData(Bundle sourceData) {
1344 // Fill in from the folder icon's launch source provider first
1345 Stats.LaunchSourceUtils.populateSourceDataFromAncestorProvider(mFolderIcon, sourceData);
1346 sourceData.putString(Stats.SOURCE_EXTRA_SUB_CONTAINER, Stats.SUB_CONTAINER_FOLDER);
1347 sourceData.putInt(Stats.SOURCE_EXTRA_SUB_CONTAINER_PAGE, mContent.getCurrentPage());
1348 }
1349
Sunny Goyal48461932015-03-09 17:41:09 -07001350 private class OnScrollHintListener implements OnAlarmListener {
1351
1352 private final DragObject mDragObject;
1353
1354 OnScrollHintListener(DragObject object) {
1355 mDragObject = object;
1356 }
1357
1358 /**
1359 * Scroll hint has been shown long enough. Now scroll to appropriate page.
1360 */
1361 @Override
1362 public void onAlarm(Alarm alarm) {
1363 if (mCurrentScrollDir == DragController.SCROLL_LEFT) {
Sunny Goyalb8634152015-04-09 14:17:14 -07001364 mContent.scrollLeft();
Sunny Goyal48461932015-03-09 17:41:09 -07001365 mScrollHintDir = DragController.SCROLL_NONE;
1366 } else if (mCurrentScrollDir == DragController.SCROLL_RIGHT) {
Sunny Goyalb8634152015-04-09 14:17:14 -07001367 mContent.scrollRight();
Sunny Goyal48461932015-03-09 17:41:09 -07001368 mScrollHintDir = DragController.SCROLL_NONE;
1369 } else {
1370 // This should not happen
1371 return;
1372 }
1373 mCurrentScrollDir = DragController.SCROLL_NONE;
1374
1375 // Pause drag event until the scrolling is finished
1376 mScrollPauseAlarm.setOnAlarmListener(new OnScrollFinishedListener(mDragObject));
1377 mScrollPauseAlarm.setAlarm(DragController.RESCROLL_DELAY);
1378 }
1379 }
1380
1381 private class OnScrollFinishedListener implements OnAlarmListener {
1382
1383 private final DragObject mDragObject;
1384
1385 OnScrollFinishedListener(DragObject object) {
1386 mDragObject = object;
1387 }
1388
1389 /**
1390 * Page scroll is complete.
1391 */
1392 @Override
1393 public void onAlarm(Alarm alarm) {
1394 // Reorder immediately on page change.
1395 onDragOver(mDragObject, 1);
1396 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001397 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001398}