blob: 2e19f6eba261f34a30f2bd27e020155e81e0bfbb [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 Goyal2245fa22015-07-06 11:45:18 -070061import com.android.launcher3.util.UiThreadCircularReveal;
Sunny Goyal83a8f042015-05-19 12:52:12 -070062
Adam Cohenc0dcf592011-06-01 15:30:43 -070063import java.util.ArrayList;
Adam Cohen3bf84d32012-05-07 20:17:14 -070064import java.util.Collections;
Sunny Goyal1dd0f8b2015-07-06 13:04:02 -070065import java.util.Comparator;
Adam Cohenc0dcf592011-06-01 15:30:43 -070066
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067/**
68 * Represents a set of icons chosen by the user or generated by the system.
69 */
Adam Cohen8dfcba42011-07-07 16:38:18 -070070public class Folder extends LinearLayout implements DragSource, View.OnClickListener,
Adam Cohenea0818d2011-09-30 20:06:58 -070071 View.OnLongClickListener, DropTarget, FolderListener, TextView.OnEditorActionListener,
Winson Chung8f1eff72015-05-28 17:33:40 -070072 View.OnFocusChangeListener, DragListener, UninstallSource, AccessibilityDragSource,
73 Stats.LaunchSourceProvider {
Adam Cohendf2cc412011-04-27 16:56:57 -070074 private static final String TAG = "Launcher.Folder";
75
Sunny Goyalc3a609f2015-02-26 17:43:50 -080076 /**
77 * We avoid measuring {@link #mContentWrapper} with a 0 width or height, as this
78 * results in CellLayout being measured as UNSPECIFIED, which it does not support.
79 */
80 private static final int MIN_CONTENT_DIMEN = 5;
Adam Cohen4eac29a2011-07-11 17:53:37 -070081
Adam Cohendf2cc412011-04-27 16:56:57 -070082 static final int STATE_NONE = -1;
83 static final int STATE_SMALL = 0;
84 static final int STATE_ANIMATING = 1;
85 static final int STATE_OPEN = 2;
86
Sunny Goyal48461932015-03-09 17:41:09 -070087 /**
Sunny Goyal48461932015-03-09 17:41:09 -070088 * Time for which the scroll hint is shown before automatically changing page.
89 */
90 public static final int SCROLL_HINT_DURATION = DragController.SCROLL_DELAY;
91
92 /**
93 * Fraction of icon width which behave as scroll region.
94 */
95 private static final float ICON_OVERSCROLL_WIDTH_FACTOR = 0.45f;
96
Sunny Goyala07c2f52015-06-01 11:00:38 -070097 private static final int FOLDER_NAME_ANIMATION_DURATION = 633;
Sunny Goyalb7e15ad2015-05-07 14:51:31 -070098
Adam Cohenf0f4eda2013-06-06 21:27:03 -070099 private static final int REORDER_DELAY = 250;
Adam Cohen5d518fa2013-12-05 14:16:23 -0800100 private static final int ON_EXIT_CLOSE_DELAY = 400;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800101 private static final Rect sTempRect = new Rect();
102
103 private static String sDefaultFolderName;
104 private static String sHintText;
105
106 private final Alarm mReorderAlarm = new Alarm();
107 private final Alarm mOnExitAlarm = new Alarm();
Sunny Goyalb8634152015-04-09 14:17:14 -0700108 private final Alarm mOnScrollHintAlarm = new Alarm();
109 @Thunk final Alarm mScrollPauseAlarm = new Alarm();
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800110
Adam Cohen091440a2015-03-18 14:16:05 -0700111 @Thunk final ArrayList<View> mItemsInReadingOrder = new ArrayList<View>();
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800112
113 private final int mExpandDuration;
114 private final int mMaterialExpandDuration;
115 private final int mMaterialExpandStagger;
116
117 private final InputMethodManager mInputMethodManager;
118
119 protected final Launcher mLauncher;
120 protected DragController mDragController;
121 protected FolderInfo mInfo;
122
Adam Cohen091440a2015-03-18 14:16:05 -0700123 @Thunk FolderIcon mFolderIcon;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800124
Sunny Goyalb8634152015-04-09 14:17:14 -0700125 @Thunk FolderPagedView mContent;
Adam Cohen091440a2015-03-18 14:16:05 -0700126 @Thunk View mContentWrapper;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800127 FolderEditText mFolderName;
128
Sunny Goyal290800b2015-03-05 11:33:33 -0800129 private View mFooter;
130 private int mFooterHeight;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800131
132 // Cell ranks used for drag and drop
Adam Cohen091440a2015-03-18 14:16:05 -0700133 @Thunk int mTargetRank, mPrevTargetRank, mEmptyCellRank;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800134
Adam Cohen091440a2015-03-18 14:16:05 -0700135 @Thunk int mState = STATE_NONE;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800136 private boolean mRearrangeOnClose = false;
Adam Cohen7c693212011-05-18 15:26:57 -0700137 boolean mItemsInvalidated = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700138 private ShortcutInfo mCurrentDragInfo;
139 private View mCurrentDragView;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800140 private boolean mIsExternalDrag;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700141 boolean mSuppressOnAdd = false;
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700142 private boolean mDragInProgress = false;
143 private boolean mDeleteFolderOnDropCompleted = false;
144 private boolean mSuppressFolderDeletion = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700145 private boolean mItemAddedBackToSelfViaIcon = false;
Adam Cohen091440a2015-03-18 14:16:05 -0700146 @Thunk float mFolderIconPivotX;
147 @Thunk float mFolderIconPivotY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700148 private boolean mIsEditingName = false;
Adam Cohen1960ea42013-11-12 11:33:14 +0000149
Adam Cohenfb91f302012-06-11 15:45:18 -0700150 private boolean mDestroyed;
151
Adam Cohen091440a2015-03-18 14:16:05 -0700152 @Thunk Runnable mDeferredAction;
Michael Jurka1e2f4652013-07-08 18:03:46 -0700153 private boolean mDeferDropAfterUninstall;
154 private boolean mUninstallSuccessful;
155
Sunny Goyal48461932015-03-09 17:41:09 -0700156 // Folder scrolling
157 private int mScrollAreaOffset;
Sunny Goyal48461932015-03-09 17:41:09 -0700158
Adam Cohen091440a2015-03-18 14:16:05 -0700159 @Thunk int mScrollHintDir = DragController.SCROLL_NONE;
160 @Thunk int mCurrentScrollDir = DragController.SCROLL_NONE;
Sunny Goyal48461932015-03-09 17:41:09 -0700161
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162 /**
163 * Used to inflate the Workspace from XML.
164 *
165 * @param context The application's context.
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800166 * @param attrs The attributes set containing the Workspace's customization values.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167 */
168 public Folder(Context context, AttributeSet attrs) {
169 super(context, attrs);
170 setAlwaysDrawnWithCacheEnabled(false);
Adam Cohen76fc0852011-06-17 13:26:23 -0700171 mInputMethodManager = (InputMethodManager)
Michael Jurka8b805b12012-04-18 14:23:14 -0700172 getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
Adam Cohen76fc0852011-06-17 13:26:23 -0700173
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800174 Resources res = getResources();
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700175 mExpandDuration = res.getInteger(R.integer.config_folderExpandDuration);
176 mMaterialExpandDuration = res.getInteger(R.integer.config_materialFolderExpandDuration);
177 mMaterialExpandStagger = res.getInteger(R.integer.config_materialFolderExpandStagger);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700178
179 if (sDefaultFolderName == null) {
180 sDefaultFolderName = res.getString(R.string.folder_name);
181 }
Adam Cohena65beee2011-06-27 21:32:23 -0700182 if (sHintText == null) {
183 sHintText = res.getString(R.string.folder_hint_text);
184 }
Adam Cohen4eac29a2011-07-11 17:53:37 -0700185 mLauncher = (Launcher) context;
Adam Cohenac56cff2011-09-28 20:45:37 -0700186 // We need this view to be focusable in touch mode so that when text editing of the folder
187 // name is complete, we have something to focus on, thus hiding the cursor and giving
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800188 // reliable behavior when clicking the text field (since it will always gain focus on click).
Adam Cohenac56cff2011-09-28 20:45:37 -0700189 setFocusableInTouchMode(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190 }
191
192 @Override
193 protected void onFinishInflate() {
194 super.onFinishInflate();
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800195 mContentWrapper = findViewById(R.id.folder_content_wrapper);
Sunny Goyalb8634152015-04-09 14:17:14 -0700196 mContent = (FolderPagedView) findViewById(R.id.folder_content);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800197 mContent.setFolder(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700198
Adam Cohenac56cff2011-09-28 20:45:37 -0700199 mFolderName = (FolderEditText) findViewById(R.id.folder_name);
200 mFolderName.setFolder(this);
Adam Cohenea0818d2011-09-30 20:06:58 -0700201 mFolderName.setOnFocusChangeListener(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700202
Adam Cohen76fc0852011-06-17 13:26:23 -0700203 // We disable action mode for now since it messes up the view on phones
204 mFolderName.setCustomSelectionActionModeCallback(mActionModeCallback);
Adam Cohen76fc0852011-06-17 13:26:23 -0700205 mFolderName.setOnEditorActionListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700206 mFolderName.setSelectAllOnFocus(true);
Adam Cohen7a14d0b2011-06-24 11:36:35 -0700207 mFolderName.setInputType(mFolderName.getInputType() |
208 InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800209
Sunny Goyalb8634152015-04-09 14:17:14 -0700210 mFooter = findViewById(R.id.folder_footer);
Sunny Goyalb8634152015-04-09 14:17:14 -0700211
Sunny Goyal290800b2015-03-05 11:33:33 -0800212 // We find out how tall footer wants to be (it is set to wrap_content), so that
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800213 // we can allocate the appropriate amount of space for it.
214 int measureSpec = MeasureSpec.UNSPECIFIED;
Sunny Goyal290800b2015-03-05 11:33:33 -0800215 mFooter.measure(measureSpec, measureSpec);
216 mFooterHeight = mFooter.getMeasuredHeight();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800217 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700218
Adam Cohen76fc0852011-06-17 13:26:23 -0700219 private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
220 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
221 return false;
222 }
223
224 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
225 return false;
226 }
227
228 public void onDestroyActionMode(ActionMode mode) {
229 }
230
231 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
232 return false;
233 }
234 };
235
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800236 public void onClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700237 Object tag = v.getTag();
238 if (tag instanceof ShortcutInfo) {
Adam Cohenb5fe60c2013-06-06 22:03:51 -0700239 mLauncher.onClick(v);
Adam Cohendf2cc412011-04-27 16:56:57 -0700240 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800241 }
242
243 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -0700244 // Return if global dragging is not enabled
245 if (!mLauncher.isDraggingEnabled()) return true;
Sunny Goyale9b651e2015-04-24 11:44:51 -0700246 return beginDrag(v, false);
247 }
Winson Chung36a62fe2012-05-06 18:04:42 -0700248
Sunny Goyale9b651e2015-04-24 11:44:51 -0700249 private boolean beginDrag(View v, boolean accessible) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700250 Object tag = v.getTag();
251 if (tag instanceof ShortcutInfo) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700252 ShortcutInfo item = (ShortcutInfo) tag;
253 if (!v.isInTouchMode()) {
254 return false;
255 }
256
Sunny Goyale9b651e2015-04-24 11:44:51 -0700257 mLauncher.getWorkspace().beginDragShared(v, new Point(), this, accessible);
Adam Cohen76078c42011-06-09 15:06:52 -0700258
259 mCurrentDragInfo = item;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800260 mEmptyCellRank = item.rank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700261 mCurrentDragView = v;
Adam Cohenfc53cd22011-07-20 15:45:11 -0700262
Sunny Goyal290800b2015-03-05 11:33:33 -0800263 mContent.removeItem(mCurrentDragView);
Adam Cohenfc53cd22011-07-20 15:45:11 -0700264 mInfo.remove(mCurrentDragInfo);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700265 mDragInProgress = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700266 mItemAddedBackToSelfViaIcon = false;
Adam Cohendf2cc412011-04-27 16:56:57 -0700267 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800268 return true;
269 }
270
Sunny Goyale9b651e2015-04-24 11:44:51 -0700271 @Override
272 public void startDrag(CellInfo cellInfo, boolean accessible) {
273 beginDrag(cellInfo.cell, accessible);
274 }
275
276 @Override
277 public void enableAccessibleDrag(boolean enable) {
278 mLauncher.getSearchBar().enableAccessibleDrag(enable);
279 for (int i = 0; i < mContent.getChildCount(); i++) {
280 mContent.getPageAt(i).enableAccessibleDrag(enable, CellLayout.FOLDER_ACCESSIBILITY_DRAG);
281 }
Sunny Goyalccc414b2015-04-30 12:28:16 -0700282
283 mFooter.setImportantForAccessibility(enable ? IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS :
284 IMPORTANT_FOR_ACCESSIBILITY_AUTO);
Sunny Goyale9b651e2015-04-24 11:44:51 -0700285 mLauncher.getWorkspace().setAddNewPageOnDrag(!enable);
286 }
287
Adam Cohen76fc0852011-06-17 13:26:23 -0700288 public boolean isEditingName() {
289 return mIsEditingName;
290 }
291
292 public void startEditingFolderName() {
Adam Cohena65beee2011-06-27 21:32:23 -0700293 mFolderName.setHint("");
Adam Cohen76fc0852011-06-17 13:26:23 -0700294 mIsEditingName = true;
295 }
296
297 public void dismissEditingName() {
298 mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
299 doneEditingFolderName(true);
300 }
301
302 public void doneEditingFolderName(boolean commit) {
Adam Cohena65beee2011-06-27 21:32:23 -0700303 mFolderName.setHint(sHintText);
Adam Cohen1df26a32011-08-12 16:15:23 -0700304 // Convert to a string here to ensure that no other state associated with the text field
305 // gets saved.
Sunny Goyal230eade2015-06-18 12:48:51 -0700306 String newTitle = mFolderName.getText().toString();
Adam Cohen3371da02011-10-25 21:38:29 -0700307 mInfo.setTitle(newTitle);
Adam Cohen76fc0852011-06-17 13:26:23 -0700308 LauncherModel.updateItemInDatabase(mLauncher, mInfo);
Adam Cohenac56cff2011-09-28 20:45:37 -0700309
Adam Cohen3371da02011-10-25 21:38:29 -0700310 if (commit) {
311 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Sunny Goyal230eade2015-06-18 12:48:51 -0700312 String.format(getContext().getString(R.string.folder_renamed), newTitle));
Adam Cohen3371da02011-10-25 21:38:29 -0700313 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700314 // In order to clear the focus from the text field, we set the focus on ourself. This
315 // ensures that every time the field is clicked, focus is gained, giving reliable behavior.
316 requestFocus();
317
Adam Cohene601a432011-07-26 21:51:30 -0700318 Selection.setSelection((Spannable) mFolderName.getText(), 0, 0);
Adam Cohen76fc0852011-06-17 13:26:23 -0700319 mIsEditingName = false;
320 }
321
322 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
323 if (actionId == EditorInfo.IME_ACTION_DONE) {
324 dismissEditingName();
325 return true;
326 }
327 return false;
328 }
329
330 public View getEditTextRegion() {
331 return mFolderName;
332 }
333
Adam Cohen0c872ba2011-05-05 10:34:16 -0700334 /**
335 * We need to handle touch events to prevent them from falling through to the workspace below.
336 */
Sunny Goyal70660032015-05-14 00:07:08 -0700337 @SuppressLint("ClickableViewAccessibility")
Adam Cohen0c872ba2011-05-05 10:34:16 -0700338 @Override
339 public boolean onTouchEvent(MotionEvent ev) {
340 return true;
341 }
342
Joe Onorato00acb122009-08-04 16:04:30 -0400343 public void setDragController(DragController dragController) {
344 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800345 }
346
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800347 public void setFolderIcon(FolderIcon icon) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700348 mFolderIcon = icon;
349 }
350
Adam Cohen3371da02011-10-25 21:38:29 -0700351 @Override
352 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
353 // When the folder gets focus, we don't want to announce the list of items.
354 return true;
355 }
356
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800357 /**
358 * @return the FolderInfo object associated with this folder
359 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700360 public FolderInfo getInfo() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800361 return mInfo;
362 }
363
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800364 void bind(FolderInfo info) {
365 mInfo = info;
Adam Cohendf2cc412011-04-27 16:56:57 -0700366 ArrayList<ShortcutInfo> children = info.contents;
Sunny Goyal1dd0f8b2015-07-06 13:04:02 -0700367 Collections.sort(children, ITEM_POS_COMPARATOR);
Sunny Goyal08f72612015-01-05 13:41:43 -0800368
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800369 ArrayList<ShortcutInfo> overflow = mContent.bindItems(children);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700370
Jason Monk4ff73882014-04-24 16:48:07 -0400371 // If our folder has too many items we prune them from the list. This is an issue
Adam Cohenc508b2d2011-06-28 14:41:44 -0700372 // when upgrading from the old Folders implementation which could contain an unlimited
373 // number of items.
374 for (ShortcutInfo item: overflow) {
375 mInfo.remove(item);
376 LauncherModel.deleteItemFromDatabase(mLauncher, item);
377 }
378
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800379 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
380 if (lp == null) {
381 lp = new DragLayer.LayoutParams(0, 0);
382 lp.customPosition = true;
383 setLayoutParams(lp);
384 }
385 centerAboutIcon();
386
Adam Cohen4dbe6d92011-05-18 17:14:15 -0700387 mItemsInvalidated = true;
Adam Cohenac56cff2011-09-28 20:45:37 -0700388 updateTextViewFocus();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700389 mInfo.addListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700390
Adam Cohenafb01ee2011-06-23 15:38:03 -0700391 if (!sDefaultFolderName.contentEquals(mInfo.title)) {
Adam Cohen4ef610f2011-06-21 22:37:36 -0700392 mFolderName.setText(mInfo.title);
393 } else {
394 mFolderName.setText("");
395 }
Winson Chung33231f52013-12-09 16:57:45 -0800396
397 // In case any children didn't come across during loading, clean up the folder accordingly
398 mFolderIcon.post(new Runnable() {
399 public void run() {
400 if (getItemCount() <= 1) {
401 replaceFolderWithFinalItem();
402 }
403 }
404 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700405 }
406
407 /**
408 * Creates a new UserFolder, inflated from R.layout.user_folder.
409 *
410 * @param context The application's context.
411 *
412 * @return A new UserFolder.
413 */
Sunny Goyalc23da842015-05-14 20:44:01 -0700414 @SuppressLint("InflateParams")
Sunny Goyal70660032015-05-14 00:07:08 -0700415 static Folder fromXml(Launcher launcher) {
Sunny Goyalc23da842015-05-14 20:44:01 -0700416 return (Folder) launcher.getLayoutInflater().inflate(R.layout.user_folder, null);
Adam Cohendf2cc412011-04-27 16:56:57 -0700417 }
418
419 /**
420 * This method is intended to make the UserFolder to be visually identical in size and position
421 * to its associated FolderIcon. This allows for a seamless transition into the expanded state.
422 */
423 private void positionAndSizeAsIcon() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700424 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800425 setScaleX(0.8f);
426 setScaleY(0.8f);
427 setAlpha(0f);
Adam Cohendf2cc412011-04-27 16:56:57 -0700428 mState = STATE_SMALL;
429 }
430
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700431 private void prepareReveal() {
432 setScaleX(1f);
433 setScaleY(1f);
434 setAlpha(1f);
435 mState = STATE_SMALL;
436 }
437
Adam Cohendf2cc412011-04-27 16:56:57 -0700438 public void animateOpen() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700439 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen6441de02011-12-14 14:25:32 -0800440
Sunny Goyalb8634152015-04-09 14:17:14 -0700441 mContent.completePendingPageChanges();
Sunny Goyal8167dc22015-04-27 13:44:01 -0700442 if (!mDragInProgress) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700443 // Open on the first page.
444 mContent.snapToPageImmediately(0);
Sunny Goyal48461932015-03-09 17:41:09 -0700445 }
446
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700447 Animator openFolderAnim = null;
448 final Runnable onCompleteRunnable;
Kenny Guyd794a3f2014-09-16 15:17:58 +0100449 if (!Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700450 positionAndSizeAsIcon();
451 centerAboutIcon();
452
453 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1);
454 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
455 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
456 final ObjectAnimator oa =
457 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
458 oa.setDuration(mExpandDuration);
459 openFolderAnim = oa;
460
461 setLayerType(LAYER_TYPE_HARDWARE, null);
462 onCompleteRunnable = new Runnable() {
463 @Override
464 public void run() {
465 setLayerType(LAYER_TYPE_NONE, null);
466 }
467 };
468 } else {
469 prepareReveal();
470 centerAboutIcon();
471
Sunny Goyal2245fa22015-07-06 11:45:18 -0700472 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700473 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
474 int height = getFolderHeight();
475
476 float transX = - 0.075f * (width / 2 - getPivotX());
477 float transY = - 0.075f * (height / 2 - getPivotY());
478 setTranslationX(transX);
479 setTranslationY(transY);
480 PropertyValuesHolder tx = PropertyValuesHolder.ofFloat("translationX", transX, 0);
481 PropertyValuesHolder ty = PropertyValuesHolder.ofFloat("translationY", transY, 0);
482
Sunny Goyal2245fa22015-07-06 11:45:18 -0700483 Animator drift = ObjectAnimator.ofPropertyValuesHolder(this, tx, ty);
Sunny Goyal75deaf32015-05-07 16:13:12 -0700484 drift.setDuration(mMaterialExpandDuration);
485 drift.setStartDelay(mMaterialExpandStagger);
486 drift.setInterpolator(new LogDecelerateInterpolator(100, 0));
487
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700488 int rx = (int) Math.max(Math.max(width - getPivotX(), 0), getPivotX());
489 int ry = (int) Math.max(Math.max(height - getPivotY(), 0), getPivotY());
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700490 float radius = (float) Math.hypot(rx, ry);
Sunny Goyal75deaf32015-05-07 16:13:12 -0700491
Sunny Goyal2245fa22015-07-06 11:45:18 -0700492 Animator reveal = UiThreadCircularReveal.createCircularReveal(this, (int) getPivotX(),
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700493 (int) getPivotY(), 0, radius);
494 reveal.setDuration(mMaterialExpandDuration);
495 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
496
Sunny Goyalbc753352015-03-05 09:40:44 -0800497 mContentWrapper.setAlpha(0f);
Sunny Goyal2245fa22015-07-06 11:45:18 -0700498 Animator iconsAlpha = ObjectAnimator.ofFloat(mContentWrapper, "alpha", 0f, 1f);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700499 iconsAlpha.setDuration(mMaterialExpandDuration);
500 iconsAlpha.setStartDelay(mMaterialExpandStagger);
501 iconsAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
502
Sunny Goyal290800b2015-03-05 11:33:33 -0800503 mFooter.setAlpha(0f);
Sunny Goyal2245fa22015-07-06 11:45:18 -0700504 Animator textAlpha = ObjectAnimator.ofFloat(mFooter, "alpha", 0f, 1f);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700505 textAlpha.setDuration(mMaterialExpandDuration);
506 textAlpha.setStartDelay(mMaterialExpandStagger);
507 textAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
508
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700509 anim.play(drift);
510 anim.play(iconsAlpha);
511 anim.play(textAlpha);
512 anim.play(reveal);
513
514 openFolderAnim = anim;
515
Sunny Goyalbc753352015-03-05 09:40:44 -0800516 mContentWrapper.setLayerType(LAYER_TYPE_HARDWARE, null);
Sunny Goyal75deaf32015-05-07 16:13:12 -0700517 mFooter.setLayerType(LAYER_TYPE_HARDWARE, null);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700518 onCompleteRunnable = new Runnable() {
519 @Override
520 public void run() {
Sunny Goyalbc753352015-03-05 09:40:44 -0800521 mContentWrapper.setLayerType(LAYER_TYPE_NONE, null);
Sunny Goyal75deaf32015-05-07 16:13:12 -0700522 mContentWrapper.setLayerType(LAYER_TYPE_NONE, null);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700523 }
524 };
525 }
526 openFolderAnim.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700527 @Override
528 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700529 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Sunny Goyalbc753352015-03-05 09:40:44 -0800530 mContent.getAccessibilityDescription());
Adam Cohendf2cc412011-04-27 16:56:57 -0700531 mState = STATE_ANIMATING;
532 }
533 @Override
534 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700535 mState = STATE_OPEN;
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700536
537 if (onCompleteRunnable != null) {
538 onCompleteRunnable.run();
539 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800540
Sunny Goyalbc753352015-03-05 09:40:44 -0800541 mContent.setFocusOnFirstChild();
Adam Cohendf2cc412011-04-27 16:56:57 -0700542 }
543 });
Sunny Goyalb7e15ad2015-05-07 14:51:31 -0700544
545 // Footer animation
546 if (mContent.getPageCount() > 1 && !mInfo.hasOption(FolderInfo.FLAG_MULTI_PAGE_ANIMATION)) {
547 int footerWidth = mContent.getDesiredWidth()
548 - mFooter.getPaddingLeft() - mFooter.getPaddingRight();
549
550 float textWidth = mFolderName.getPaint().measureText(mFolderName.getText().toString());
Sunny Goyala07c2f52015-06-01 11:00:38 -0700551 float translation = (footerWidth - textWidth) / 2;
552 mFolderName.setTranslationX(mContent.mIsRtl ? -translation : translation);
Sunny Goyalb7e15ad2015-05-07 14:51:31 -0700553 mContent.setMarkerScale(0);
554
555 // Do not update the flag if we are in drag mode. The flag will be updated, when we
556 // actually drop the icon.
557 final boolean updateAnimationFlag = !mDragInProgress;
558 openFolderAnim.addListener(new AnimatorListenerAdapter() {
559
560 @Override
561 public void onAnimationEnd(Animator animation) {
Sunny Goyala07c2f52015-06-01 11:00:38 -0700562 mFolderName.animate().setDuration(FOLDER_NAME_ANIMATION_DURATION)
563 .translationX(0)
Sunny Goyalc1cd23b2015-06-01 17:15:34 -0700564 .setInterpolator(Utilities.isLmpOrAbove() ?
565 AnimationUtils.loadInterpolator(mLauncher,
566 android.R.interpolator.fast_out_slow_in)
567 : new LogDecelerateInterpolator(100, 0));
Sunny Goyalb7e15ad2015-05-07 14:51:31 -0700568 mContent.animateMarkers();
569
570 if (updateAnimationFlag) {
571 mInfo.setOption(FolderInfo.FLAG_MULTI_PAGE_ANIMATION, true, mLauncher);
572 }
573 }
574 });
575 } else {
576 mFolderName.setTranslationX(0);
577 mContent.setMarkerScale(1);
578 }
579
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700580 openFolderAnim.start();
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800581
582 // Make sure the folder picks up the last drag move even if the finger doesn't move.
583 if (mDragController.isDragging()) {
584 mDragController.forceTouchMove();
585 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700586
Sunny Goyalb8634152015-04-09 14:17:14 -0700587 FolderPagedView pages = (FolderPagedView) mContent;
588 pages.verifyVisibleHighResIcons(pages.getNextPage());
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800589 }
590
591 public void beginExternalDrag(ShortcutInfo item) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800592 mCurrentDragInfo = item;
Sunny Goyal5d85c442015-03-10 13:14:47 -0700593 mEmptyCellRank = mContent.allocateRankForNewItem(item);
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800594 mIsExternalDrag = true;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800595 mDragInProgress = true;
Sunny Goyalb8634152015-04-09 14:17:14 -0700596
Sunny Goyalf4066152015-04-15 09:42:19 -0700597 // Since this folder opened by another controller, it might not get onDrop or
598 // onDropComplete. Perform cleanup once drag-n-drop ends.
599 mDragController.addDragListener(this);
600 }
601
602 @Override
603 public void onDragStart(DragSource source, Object info, int dragAction) { }
604
605 @Override
606 public void onDragEnd() {
607 if (mIsExternalDrag && mDragInProgress) {
608 completeDragExit();
609 }
610 mDragController.removeDragListener(this);
Adam Cohendf2cc412011-04-27 16:56:57 -0700611 }
612
Adam Cohen091440a2015-03-18 14:16:05 -0700613 @Thunk void sendCustomAccessibilityEvent(int type, String text) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700614 AccessibilityManager accessibilityManager = (AccessibilityManager)
615 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
616 if (accessibilityManager.isEnabled()) {
Adam Cohen3371da02011-10-25 21:38:29 -0700617 AccessibilityEvent event = AccessibilityEvent.obtain(type);
618 onInitializeAccessibilityEvent(event);
619 event.getText().add(text);
Michael Jurka8b805b12012-04-18 14:23:14 -0700620 accessibilityManager.sendAccessibilityEvent(event);
Adam Cohen3371da02011-10-25 21:38:29 -0700621 }
622 }
623
Adam Cohendf2cc412011-04-27 16:56:57 -0700624 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700625 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800626 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
627 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
628 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
Michael Jurka032e6ba2013-04-22 15:08:42 +0200629 final ObjectAnimator oa =
Michael Jurka2ecf9952012-06-18 12:52:28 -0700630 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohendf2cc412011-04-27 16:56:57 -0700631
Adam Cohen2801caf2011-05-13 20:57:39 -0700632 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700633 @Override
634 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700635 onCloseComplete();
Michael Jurka0121c3e2012-05-31 08:36:04 -0700636 setLayerType(LAYER_TYPE_NONE, null);
Adam Cohen2801caf2011-05-13 20:57:39 -0700637 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700638 }
639 @Override
640 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700641 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700642 getContext().getString(R.string.folder_closed));
Adam Cohendf2cc412011-04-27 16:56:57 -0700643 mState = STATE_ANIMATING;
644 }
645 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700646 oa.setDuration(mExpandDuration);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700647 setLayerType(LAYER_TYPE_HARDWARE, null);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100648 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700649 }
650
Adam Cohencb3382b2011-05-24 14:07:08 -0700651 public boolean acceptDrop(DragObject d) {
652 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700653 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700654 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
655 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
656 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700657 }
658
Adam Cohencb3382b2011-05-24 14:07:08 -0700659 public void onDragEnter(DragObject d) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800660 mPrevTargetRank = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700661 mOnExitAlarm.cancelAlarm();
Sunny Goyalb8634152015-04-09 14:17:14 -0700662 // Get the area offset such that the folder only closes if half the drag icon width
663 // is outside the folder area
664 mScrollAreaOffset = d.dragView.getDragRegionWidth() / 2 - d.xOffset;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700665 }
666
667 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
668 public void onAlarm(Alarm alarm) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800669 mContent.realTimeReorder(mEmptyCellRank, mTargetRank);
670 mEmptyCellRank = mTargetRank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700671 }
672 };
673
Sunny Goyalc46bfef2015-01-05 12:40:08 -0800674 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohen2374abf2013-04-16 14:56:57 -0700675 public boolean isLayoutRtl() {
676 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
677 }
678
Sunny Goyal48461932015-03-09 17:41:09 -0700679 @Override
Adam Cohencb3382b2011-05-24 14:07:08 -0700680 public void onDragOver(DragObject d) {
Sunny Goyal48461932015-03-09 17:41:09 -0700681 onDragOver(d, REORDER_DELAY);
682 }
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700683
Sunny Goyal48461932015-03-09 17:41:09 -0700684 private int getTargetRank(DragObject d, float[] recycle) {
685 recycle = d.getVisualCenter(recycle);
686 return mContent.findNearestArea(
687 (int) recycle[0] - getPaddingLeft(), (int) recycle[1] - getPaddingTop());
688 }
689
Adam Cohen091440a2015-03-18 14:16:05 -0700690 @Thunk void onDragOver(DragObject d, int reorderDelay) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700691 if (mScrollPauseAlarm.alarmPending()) {
Sunny Goyal48461932015-03-09 17:41:09 -0700692 return;
693 }
694 final float[] r = new float[2];
695 mTargetRank = getTargetRank(d, r);
696
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800697 if (mTargetRank != mPrevTargetRank) {
Alan Viverette4cda5b72013-08-28 17:53:41 -0700698 mReorderAlarm.cancelAlarm();
Sunny Goyalc46bfef2015-01-05 12:40:08 -0800699 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
700 mReorderAlarm.setAlarm(REORDER_DELAY);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800701 mPrevTargetRank = mTargetRank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700702 }
Sunny Goyal48461932015-03-09 17:41:09 -0700703
Sunny Goyal48461932015-03-09 17:41:09 -0700704 float x = r[0];
Sunny Goyalb8634152015-04-09 14:17:14 -0700705 int currentPage = mContent.getNextPage();
Sunny Goyal48461932015-03-09 17:41:09 -0700706
Sunny Goyalb8634152015-04-09 14:17:14 -0700707 float cellOverlap = mContent.getCurrentCellLayout().getCellWidth()
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700708 * ICON_OVERSCROLL_WIDTH_FACTOR;
709 boolean isOutsideLeftEdge = x < cellOverlap;
710 boolean isOutsideRightEdge = x > (getWidth() - cellOverlap);
Sunny Goyal48461932015-03-09 17:41:09 -0700711
Sunny Goyal70660032015-05-14 00:07:08 -0700712 if (currentPage > 0 && (mContent.mIsRtl ? isOutsideRightEdge : isOutsideLeftEdge)) {
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700713 showScrollHint(DragController.SCROLL_LEFT, d);
Sunny Goyalb8634152015-04-09 14:17:14 -0700714 } else if (currentPage < (mContent.getPageCount() - 1)
Sunny Goyal70660032015-05-14 00:07:08 -0700715 && (mContent.mIsRtl ? isOutsideLeftEdge : isOutsideRightEdge)) {
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700716 showScrollHint(DragController.SCROLL_RIGHT, d);
Sunny Goyal48461932015-03-09 17:41:09 -0700717 } else {
718 mOnScrollHintAlarm.cancelAlarm();
719 if (mScrollHintDir != DragController.SCROLL_NONE) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700720 mContent.clearScrollHint();
Sunny Goyal48461932015-03-09 17:41:09 -0700721 mScrollHintDir = DragController.SCROLL_NONE;
722 }
723 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700724 }
725
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700726 private void showScrollHint(int direction, DragObject d) {
727 // Show scroll hint on the right
728 if (mScrollHintDir != direction) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700729 mContent.showScrollHint(direction);
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700730 mScrollHintDir = direction;
731 }
732
733 // Set alarm for when the hint is complete
734 if (!mOnScrollHintAlarm.alarmPending() || mCurrentScrollDir != direction) {
735 mCurrentScrollDir = direction;
736 mOnScrollHintAlarm.cancelAlarm();
737 mOnScrollHintAlarm.setOnAlarmListener(new OnScrollHintListener(d));
738 mOnScrollHintAlarm.setAlarm(SCROLL_HINT_DURATION);
739
740 mReorderAlarm.cancelAlarm();
741 mTargetRank = mEmptyCellRank;
742 }
743 }
744
Adam Cohenbfbfd262011-06-13 16:55:12 -0700745 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
746 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700747 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700748 }
749 };
750
Adam Cohen95bb8002011-07-03 23:40:28 -0700751 public void completeDragExit() {
Sunny Goyalf4066152015-04-15 09:42:19 -0700752 if (mInfo.opened) {
753 mLauncher.closeFolder();
754 mRearrangeOnClose = true;
Sunny Goyal31abc292015-05-01 10:42:32 -0700755 } else if (mState == STATE_ANIMATING) {
756 mRearrangeOnClose = true;
Sunny Goyalf4066152015-04-15 09:42:19 -0700757 } else {
758 rearrangeChildren();
Sunny Goyal31abc292015-05-01 10:42:32 -0700759 clearDragInfo();
Sunny Goyalf4066152015-04-15 09:42:19 -0700760 }
Sunny Goyal31abc292015-05-01 10:42:32 -0700761 }
762
763 private void clearDragInfo() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700764 mCurrentDragInfo = null;
765 mCurrentDragView = null;
766 mSuppressOnAdd = false;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800767 mIsExternalDrag = false;
Adam Cohen3e8f8112011-07-02 18:03:00 -0700768 }
769
Adam Cohencb3382b2011-05-24 14:07:08 -0700770 public void onDragExit(DragObject d) {
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700771 // We only close the folder if this is a true drag exit, ie. not because
772 // a drop has occurred above the folder.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700773 if (!d.dragComplete) {
774 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
775 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
776 }
777 mReorderAlarm.cancelAlarm();
Sunny Goyal48461932015-03-09 17:41:09 -0700778
Sunny Goyalb8634152015-04-09 14:17:14 -0700779 mOnScrollHintAlarm.cancelAlarm();
780 mScrollPauseAlarm.cancelAlarm();
781 if (mScrollHintDir != DragController.SCROLL_NONE) {
782 mContent.clearScrollHint();
783 mScrollHintDir = DragController.SCROLL_NONE;
Sunny Goyal48461932015-03-09 17:41:09 -0700784 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700785 }
786
Sunny Goyale9b651e2015-04-24 11:44:51 -0700787 /**
788 * When performing an accessibility drop, onDrop is sent immediately after onDragEnter. So we
789 * need to complete all transient states based on timers.
790 */
791 @Override
792 public void prepareAccessibilityDrop() {
793 if (mReorderAlarm.alarmPending()) {
794 mReorderAlarm.cancelAlarm();
795 mReorderAlarmListener.onAlarm(mReorderAlarm);
796 }
797 }
798
Michael Jurka1e2f4652013-07-08 18:03:46 -0700799 public void onDropCompleted(final View target, final DragObject d,
800 final boolean isFlingToDelete, final boolean success) {
801 if (mDeferDropAfterUninstall) {
Michael Jurkaf3007582013-08-21 14:33:57 +0200802 Log.d(TAG, "Deferred handling drop because waiting for uninstall.");
Michael Jurka1e2f4652013-07-08 18:03:46 -0700803 mDeferredAction = new Runnable() {
804 public void run() {
805 onDropCompleted(target, d, isFlingToDelete, success);
806 mDeferredAction = null;
807 }
808 };
809 return;
810 }
811
812 boolean beingCalledAfterUninstall = mDeferredAction != null;
813 boolean successfulDrop =
814 success && (!beingCalledAfterUninstall || mUninstallSuccessful);
Winson Chung5f8afe62013-08-12 16:19:28 -0700815
Michael Jurka1e2f4652013-07-08 18:03:46 -0700816 if (successfulDrop) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800817 if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon && target != this) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700818 replaceFolderWithFinalItem();
819 }
820 } else {
821 // The drag failed, we need to return the item to the folder
Sunny Goyalb8c663c2015-04-23 11:43:48 -0700822 ShortcutInfo info = (ShortcutInfo) d.dragInfo;
823 View icon = (mCurrentDragView != null && mCurrentDragView.getTag() == info)
824 ? mCurrentDragView : mContent.createNewView(info);
825 ArrayList<View> views = getItemsInReadingOrder();
826 views.add(info.rank, icon);
827 mContent.arrangeChildren(views, views.size());
828 mItemsInvalidated = true;
829
830 mSuppressOnAdd = true;
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700831 mFolderIcon.onDrop(d);
Sunny Goyalb8c663c2015-04-23 11:43:48 -0700832 mSuppressOnAdd = false;
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700833 }
834
835 if (target != this) {
836 if (mOnExitAlarm.alarmPending()) {
837 mOnExitAlarm.cancelAlarm();
Michael Jurka54554252013-08-01 12:52:23 +0200838 if (!successfulDrop) {
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700839 mSuppressFolderDeletion = true;
840 }
Sunny Goyal5d85c442015-03-10 13:14:47 -0700841 mScrollPauseAlarm.cancelAlarm();
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700842 completeDragExit();
843 }
Adam Cohen9c58d822013-12-13 17:18:10 -0800844 }
845
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700846 mDeleteFolderOnDropCompleted = false;
847 mDragInProgress = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700848 mItemAddedBackToSelfViaIcon = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700849 mCurrentDragInfo = null;
850 mCurrentDragView = null;
851 mSuppressOnAdd = false;
Adam Cohen4045eb72011-10-06 11:44:26 -0700852
853 // Reordering may have occured, and we need to save the new item locations. We do this once
854 // at the end to prevent unnecessary database operations.
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700855 updateItemLocationsInDatabaseBatch();
Sunny Goyalb7e15ad2015-05-07 14:51:31 -0700856
857 // Use the item count to check for multi-page as the folder UI may not have
858 // been refreshed yet.
859 if (getItemCount() <= mContent.itemsPerPage()) {
860 // Show the animation, next time something is added to the folder.
861 mInfo.setOption(FolderInfo.FLAG_MULTI_PAGE_ANIMATION, false, mLauncher);
862 }
863
Adam Cohen4045eb72011-10-06 11:44:26 -0700864 }
865
Sunny Goyalfa401a12015-04-10 13:45:42 -0700866 @Override
Michael Jurka1e2f4652013-07-08 18:03:46 -0700867 public void deferCompleteDropAfterUninstallActivity() {
868 mDeferDropAfterUninstall = true;
869 }
870
Sunny Goyalfa401a12015-04-10 13:45:42 -0700871 @Override
Michael Jurka1e2f4652013-07-08 18:03:46 -0700872 public void onUninstallActivityReturned(boolean success) {
873 mDeferDropAfterUninstall = false;
874 mUninstallSuccessful = success;
875 if (mDeferredAction != null) {
876 mDeferredAction.run();
877 }
878 }
879
Winson Chunga48487a2012-03-20 16:19:37 -0700880 @Override
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800881 public float getIntrinsicIconScaleFactor() {
882 return 1f;
883 }
884
885 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800886 public boolean supportsFlingToDelete() {
887 return true;
888 }
889
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000890 @Override
891 public boolean supportsAppInfoDropTarget() {
892 return false;
893 }
894
895 @Override
896 public boolean supportsDeleteDropTarget() {
897 return true;
898 }
899
Sunny Goyalddec7342015-04-29 18:12:37 -0700900 @Override
901 public void onFlingToDelete(DragObject d, PointF vec) {
Winson Chunga48487a2012-03-20 16:19:37 -0700902 // Do nothing
903 }
904
905 @Override
906 public void onFlingToDeleteCompleted() {
907 // Do nothing
908 }
909
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700910 private void updateItemLocationsInDatabaseBatch() {
911 ArrayList<View> list = getItemsInReadingOrder();
912 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
913 for (int i = 0; i < list.size(); i++) {
914 View v = list.get(i);
915 ItemInfo info = (ItemInfo) v.getTag();
Sunny Goyal08f72612015-01-05 13:41:43 -0800916 info.rank = i;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700917 items.add(info);
918 }
919
920 LauncherModel.moveItemsInDatabase(mLauncher, items, mInfo.id, 0);
921 }
922
Adam Cohene25af792013-06-06 23:08:25 -0700923 public void addItemLocationsInDatabase() {
924 ArrayList<View> list = getItemsInReadingOrder();
925 for (int i = 0; i < list.size(); i++) {
926 View v = list.get(i);
927 ItemInfo info = (ItemInfo) v.getTag();
928 LauncherModel.addItemToDatabase(mLauncher, info, mInfo.id, 0,
Winson Chung8f1eff72015-05-28 17:33:40 -0700929 info.cellX, info.cellY);
Adam Cohene25af792013-06-06 23:08:25 -0700930 }
931 }
932
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700933 public void notifyDrop() {
934 if (mDragInProgress) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700935 mItemAddedBackToSelfViaIcon = true;
Adam Cohen76078c42011-06-09 15:06:52 -0700936 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700937 }
938
939 public boolean isDropEnabled() {
940 return true;
941 }
942
Adam Cohen2801caf2011-05-13 20:57:39 -0700943 public boolean isFull() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800944 return mContent.isFull();
Adam Cohen2801caf2011-05-13 20:57:39 -0700945 }
946
947 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700948 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700949
Winson Chung892c74d2013-08-22 16:15:50 -0700950 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700951 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700952 int height = getFolderHeight();
Adam Cohen2801caf2011-05-13 20:57:39 -0700953
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800954 float scale = parent.getDescendantRectRelativeToSelf(mFolderIcon, sTempRect);
Adam Cohen8e776a62011-06-28 18:10:06 -0700955
Adam Cohen2e6da152015-05-06 11:42:25 -0700956 DeviceProfile grid = mLauncher.getDeviceProfile();
Winson Chungaf40f202013-09-18 18:26:31 -0700957
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800958 int centerX = (int) (sTempRect.left + sTempRect.width() * scale / 2);
959 int centerY = (int) (sTempRect.top + sTempRect.height() * scale / 2);
Adam Cohen2801caf2011-05-13 20:57:39 -0700960 int centeredLeft = centerX - width / 2;
961 int centeredTop = centerY - height / 2;
Adam Cohen2801caf2011-05-13 20:57:39 -0700962
Sunny Goyal41d84172015-07-08 23:28:51 -0700963 // We need to bound the folder to the currently visible workspace area
964 mLauncher.getWorkspace().getPageAreaRelativeToDragLayer(sTempRect);
965 int left = Math.min(Math.max(sTempRect.left, centeredLeft),
966 sTempRect.left + sTempRect.width() - width);
967 int top = Math.min(Math.max(sTempRect.top, centeredTop),
968 sTempRect.top + sTempRect.height() - height);
Sunny Goyalc6205602015-05-21 20:46:33 -0700969 if (grid.isPhone && (grid.availableWidthPx - width) < grid.iconSizePx) {
Winson Chungaf40f202013-09-18 18:26:31 -0700970 // Center the folder if it is full (on phones only)
971 left = (grid.availableWidthPx - width) / 2;
Sunny Goyal41d84172015-07-08 23:28:51 -0700972 } else if (width >= sTempRect.width()) {
Winson Chungaf40f202013-09-18 18:26:31 -0700973 // If the folder doesn't fit within the bounds, center it about the desired bounds
Sunny Goyal41d84172015-07-08 23:28:51 -0700974 left = sTempRect.left + (sTempRect.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700975 }
Sunny Goyal41d84172015-07-08 23:28:51 -0700976 if (height >= sTempRect.height()) {
977 top = sTempRect.top + (sTempRect.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700978 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700979
980 int folderPivotX = width / 2 + (centeredLeft - left);
981 int folderPivotY = height / 2 + (centeredTop - top);
982 setPivotX(folderPivotX);
983 setPivotY(folderPivotY);
Adam Cohen268c4752012-06-06 17:47:33 -0700984 mFolderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700985 (1.0f * folderPivotX / width));
Adam Cohen268c4752012-06-06 17:47:33 -0700986 mFolderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700987 (1.0f * folderPivotY / height));
Adam Cohen3bf84d32012-05-07 20:17:14 -0700988
Adam Cohen662b5982011-12-13 17:45:21 -0800989 lp.width = width;
990 lp.height = height;
991 lp.x = left;
992 lp.y = top;
Adam Cohen2801caf2011-05-13 20:57:39 -0700993 }
994
Adam Cohen268c4752012-06-06 17:47:33 -0700995 float getPivotXForIconAnimation() {
996 return mFolderIconPivotX;
997 }
998 float getPivotYForIconAnimation() {
999 return mFolderIconPivotY;
1000 }
1001
Winson Chung892c74d2013-08-22 16:15:50 -07001002 private int getContentAreaHeight() {
Adam Cohen2e6da152015-05-06 11:42:25 -07001003 DeviceProfile grid = mLauncher.getDeviceProfile();
Sunny Goyalc6205602015-05-21 20:46:33 -07001004 Rect workspacePadding = grid.getWorkspacePadding(mContent.mIsRtl);
Winson Chung892c74d2013-08-22 16:15:50 -07001005 int maxContentAreaHeight = grid.availableHeightPx -
Winson Chung892c74d2013-08-22 16:15:50 -07001006 workspacePadding.top - workspacePadding.bottom -
Sunny Goyal290800b2015-03-05 11:33:33 -08001007 mFooterHeight;
Adam Cohen1960ea42013-11-12 11:33:14 +00001008 int height = Math.min(maxContentAreaHeight,
Winson Chung892c74d2013-08-22 16:15:50 -07001009 mContent.getDesiredHeight());
Adam Cohen1960ea42013-11-12 11:33:14 +00001010 return Math.max(height, MIN_CONTENT_DIMEN);
1011 }
1012
1013 private int getContentAreaWidth() {
1014 return Math.max(mContent.getDesiredWidth(), MIN_CONTENT_DIMEN);
Winson Chung892c74d2013-08-22 16:15:50 -07001015 }
1016
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001017 private int getFolderHeight() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001018 return getFolderHeight(getContentAreaHeight());
1019 }
1020
1021 private int getFolderHeight(int contentAreaHeight) {
Sunny Goyal290800b2015-03-05 11:33:33 -08001022 return getPaddingTop() + getPaddingBottom() + contentAreaHeight + mFooterHeight;
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001023 }
Adam Cohen234c4cd2011-07-17 21:03:04 -07001024
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001025 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001026 int contentWidth = getContentAreaWidth();
1027 int contentHeight = getContentAreaHeight();
Adam Cohen1960ea42013-11-12 11:33:14 +00001028
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001029 int contentAreaWidthSpec = MeasureSpec.makeMeasureSpec(contentWidth, MeasureSpec.EXACTLY);
1030 int contentAreaHeightSpec = MeasureSpec.makeMeasureSpec(contentHeight, MeasureSpec.EXACTLY);
1031
1032 mContent.setFixedSize(contentWidth, contentHeight);
1033 mContentWrapper.measure(contentAreaWidthSpec, contentAreaHeightSpec);
Sunny Goyala07c2f52015-06-01 11:00:38 -07001034
1035 if (mContent.getChildCount() > 0) {
1036 int cellIconGap = (mContent.getPageAt(0).getCellWidth()
1037 - mLauncher.getDeviceProfile().iconSizePx) / 2;
1038 mFooter.setPadding(mContent.getPaddingLeft() + cellIconGap,
1039 mFooter.getPaddingTop(),
1040 mContent.getPaddingRight() + cellIconGap,
1041 mFooter.getPaddingBottom());
1042 }
Sunny Goyal290800b2015-03-05 11:33:33 -08001043 mFooter.measure(contentAreaWidthSpec,
1044 MeasureSpec.makeMeasureSpec(mFooterHeight, MeasureSpec.EXACTLY));
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001045
1046 int folderWidth = getPaddingLeft() + getPaddingRight() + contentWidth;
1047 int folderHeight = getFolderHeight(contentHeight);
1048 setMeasuredDimension(folderWidth, folderHeight);
Adam Cohen234c4cd2011-07-17 21:03:04 -07001049 }
1050
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001051 /**
1052 * Rearranges the children based on their rank.
1053 */
1054 public void rearrangeChildren() {
1055 rearrangeChildren(-1);
1056 }
Adam Cohen2801caf2011-05-13 20:57:39 -07001057
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001058 /**
1059 * Rearranges the children based on their rank.
1060 * @param itemCount if greater than the total children count, empty spaces are left at the end,
1061 * otherwise it is ignored.
1062 */
1063 public void rearrangeChildren(int itemCount) {
1064 ArrayList<View> views = getItemsInReadingOrder();
1065 mContent.arrangeChildren(views, Math.max(itemCount, views.size()));
Adam Cohen7c693212011-05-18 15:26:57 -07001066 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -07001067 }
1068
Sunny Goyalc4918352015-03-10 18:15:48 -07001069 // TODO remove this once GSA code fix is submitted
1070 public ViewGroup getContent() {
1071 return (ViewGroup) mContent;
1072 }
1073
Adam Cohena9cf38f2011-05-02 15:36:58 -07001074 public int getItemCount() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001075 return mContent.getItemCount();
Adam Cohena9cf38f2011-05-02 15:36:58 -07001076 }
1077
Adam Cohen091440a2015-03-18 14:16:05 -07001078 @Thunk void onCloseComplete() {
Adam Cohen05e0f402011-08-01 12:12:49 -07001079 DragLayer parent = (DragLayer) getParent();
Michael Jurka5649c282012-06-18 10:33:21 -07001080 if (parent != null) {
1081 parent.removeView(this);
1082 }
Adam Cohen4554ee12011-08-03 16:13:21 -07001083 mDragController.removeDropTarget((DropTarget) this);
Adam Cohen05e0f402011-08-01 12:12:49 -07001084 clearFocus();
Adam Cohenac56cff2011-09-28 20:45:37 -07001085 mFolderIcon.requestFocus();
Adam Cohen05e0f402011-08-01 12:12:49 -07001086
Adam Cohen2801caf2011-05-13 20:57:39 -07001087 if (mRearrangeOnClose) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001088 rearrangeChildren();
Adam Cohen2801caf2011-05-13 20:57:39 -07001089 mRearrangeOnClose = false;
1090 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001091 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001092 if (!mDragInProgress && !mSuppressFolderDeletion) {
1093 replaceFolderWithFinalItem();
1094 } else if (mDragInProgress) {
1095 mDeleteFolderOnDropCompleted = true;
1096 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001097 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001098 mSuppressFolderDeletion = false;
Sunny Goyal31abc292015-05-01 10:42:32 -07001099 clearDragInfo();
Adam Cohenafb01ee2011-06-23 15:38:03 -07001100 }
1101
Adam Cohen091440a2015-03-18 14:16:05 -07001102 @Thunk void replaceFolderWithFinalItem() {
Adam Cohenafb01ee2011-06-23 15:38:03 -07001103 // Add the last remaining child to the workspace in place of the folder
Adam Cohenfb91f302012-06-11 15:45:18 -07001104 Runnable onCompleteRunnable = new Runnable() {
1105 @Override
1106 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -07001107 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screenId);
Adam Cohenafb01ee2011-06-23 15:38:03 -07001108
Winson Chung33231f52013-12-09 16:57:45 -08001109 View child = null;
Adam Cohenfb91f302012-06-11 15:45:18 -07001110 // Move the item from the folder to the workspace, in the position of the folder
1111 if (getItemCount() == 1) {
1112 ShortcutInfo finalItem = mInfo.contents.get(0);
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001113 child = mLauncher.createShortcut(cellLayout, finalItem);
Adam Cohenfb91f302012-06-11 15:45:18 -07001114 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -07001115 mInfo.screenId, mInfo.cellX, mInfo.cellY);
Adam Cohenfb91f302012-06-11 15:45:18 -07001116 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001117 if (getItemCount() <= 1) {
1118 // Remove the folder
1119 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
Dan Sandler0eb687f2014-01-10 13:24:55 -05001120 if (cellLayout != null) {
1121 // b/12446428 -- sometimes the cell layout has already gone away?
1122 cellLayout.removeView(mFolderIcon);
1123 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001124 if (mFolderIcon instanceof DropTarget) {
1125 mDragController.removeDropTarget((DropTarget) mFolderIcon);
1126 }
1127 mLauncher.removeFolder(mInfo);
1128 }
Adam Cohenc5e63f32012-07-12 16:16:57 -07001129 // We add the child after removing the folder to prevent both from existing at
Winson Chung0e6a7132013-08-23 12:55:10 -07001130 // the same time in the CellLayout. We need to add the new item with addInScreenFromBind()
1131 // to ensure that hotseat items are placed correctly.
Adam Cohenc5e63f32012-07-12 16:16:57 -07001132 if (child != null) {
Winson Chung0e6a7132013-08-23 12:55:10 -07001133 mLauncher.getWorkspace().addInScreenFromBind(child, mInfo.container, mInfo.screenId,
Adam Cohenc5e63f32012-07-12 16:16:57 -07001134 mInfo.cellX, mInfo.cellY, mInfo.spanX, mInfo.spanY);
1135 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001136 }
1137 };
Sunny Goyalbc753352015-03-05 09:40:44 -08001138 View finalChild = mContent.getLastItem();
Adam Cohenfb91f302012-06-11 15:45:18 -07001139 if (finalChild != null) {
1140 mFolderIcon.performDestroyAnimation(finalChild, onCompleteRunnable);
Winson Chung33231f52013-12-09 16:57:45 -08001141 } else {
1142 onCompleteRunnable.run();
Adam Cohenafb01ee2011-06-23 15:38:03 -07001143 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001144 mDestroyed = true;
1145 }
1146
1147 boolean isDestroyed() {
1148 return mDestroyed;
Adam Cohen2801caf2011-05-13 20:57:39 -07001149 }
1150
Adam Cohenac56cff2011-09-28 20:45:37 -07001151 // This method keeps track of the last item in the folder for the purposes
1152 // of keyboard focus
Sunny Goyal290800b2015-03-05 11:33:33 -08001153 public void updateTextViewFocus() {
Sunny Goyalbc753352015-03-05 09:40:44 -08001154 View lastChild = mContent.getLastItem();
Adam Cohenac56cff2011-09-28 20:45:37 -07001155 if (lastChild != null) {
1156 mFolderName.setNextFocusDownId(lastChild.getId());
1157 mFolderName.setNextFocusRightId(lastChild.getId());
1158 mFolderName.setNextFocusLeftId(lastChild.getId());
1159 mFolderName.setNextFocusUpId(lastChild.getId());
1160 }
1161 }
1162
Adam Cohenbfbfd262011-06-13 16:55:12 -07001163 public void onDrop(DragObject d) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001164 Runnable cleanUpRunnable = null;
1165
Adam Cohen689ff162014-05-08 17:27:56 -07001166 // If we are coming from All Apps space, we defer removing the extra empty screen
1167 // until the folder closes
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001168 if (d.dragSource != mLauncher.getWorkspace() && !(d.dragSource instanceof Folder)) {
1169 cleanUpRunnable = new Runnable() {
1170 @Override
1171 public void run() {
Adam Cohen689ff162014-05-08 17:27:56 -07001172 mLauncher.exitSpringLoadedDragModeDelayed(true,
1173 Launcher.EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
1174 null);
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001175 }
1176 };
Adam Cohenbfbfd262011-06-13 16:55:12 -07001177 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001178
Sunny Goyalb8634152015-04-09 14:17:14 -07001179 // If the icon was dropped while the page was being scrolled, we need to compute
1180 // the target location again such that the icon is placed of the final page.
1181 if (!mContent.rankOnCurrentPage(mEmptyCellRank)) {
1182 // Reorder again.
1183 mTargetRank = getTargetRank(d, null);
Sunny Goyal48461932015-03-09 17:41:09 -07001184
Sunny Goyalb8634152015-04-09 14:17:14 -07001185 // Rearrange items immediately.
1186 mReorderAlarmListener.onAlarm(mReorderAlarm);
Sunny Goyal48461932015-03-09 17:41:09 -07001187
Sunny Goyalb8634152015-04-09 14:17:14 -07001188 mOnScrollHintAlarm.cancelAlarm();
1189 mScrollPauseAlarm.cancelAlarm();
Sunny Goyal48461932015-03-09 17:41:09 -07001190 }
Sunny Goyalb8634152015-04-09 14:17:14 -07001191 mContent.completePendingPageChanges();
Sunny Goyal48461932015-03-09 17:41:09 -07001192
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001193 View currentDragView;
1194 ShortcutInfo si = mCurrentDragInfo;
1195 if (mIsExternalDrag) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001196 currentDragView = mContent.createAndAddViewForRank(si, mEmptyCellRank);
Sunny Goyal95abbb32014-08-04 10:53:22 -07001197 // Actually move the item in the database if it was an external drag. Call this
1198 // before creating the view, so that ShortcutInfo is updated appropriately.
1199 LauncherModel.addOrMoveItemInDatabase(
1200 mLauncher, si, mInfo.id, 0, si.cellX, si.cellY);
1201
1202 // We only need to update the locations if it doesn't get handled in #onDropCompleted.
1203 if (d.dragSource != this) {
1204 updateItemLocationsInDatabaseBatch();
1205 }
1206 mIsExternalDrag = false;
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001207 } else {
1208 currentDragView = mCurrentDragView;
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001209 mContent.addViewForRank(currentDragView, si, mEmptyCellRank);
Adam Cohenbfbfd262011-06-13 16:55:12 -07001210 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001211
1212 if (d.dragView.hasDrawn()) {
1213
1214 // Temporarily reset the scale such that the animation target gets calculated correctly.
1215 float scaleX = getScaleX();
1216 float scaleY = getScaleY();
1217 setScaleX(1.0f);
1218 setScaleY(1.0f);
1219 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, currentDragView,
1220 cleanUpRunnable, null);
1221 setScaleX(scaleX);
1222 setScaleY(scaleY);
1223 } else {
1224 d.deferDragViewCleanupPostAnimation = false;
1225 currentDragView.setVisibility(VISIBLE);
1226 }
1227 mItemsInvalidated = true;
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001228 rearrangeChildren();
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001229
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001230 // Temporarily suppress the listener, as we did all the work already here.
1231 mSuppressOnAdd = true;
1232 mInfo.add(si);
1233 mSuppressOnAdd = false;
Sunny Goyal4b020172014-08-28 14:51:14 -07001234 // Clear the drag info, as it is no longer being dragged.
1235 mCurrentDragInfo = null;
Sunny Goyalf4066152015-04-15 09:42:19 -07001236 mDragInProgress = false;
Sunny Goyalb7e15ad2015-05-07 14:51:31 -07001237
1238 if (mContent.getPageCount() > 1) {
1239 // The animation has already been shown while opening the folder.
1240 mInfo.setOption(FolderInfo.FLAG_MULTI_PAGE_ANIMATION, true, mLauncher);
1241 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001242 }
1243
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001244 // This is used so the item doesn't immediately appear in the folder when added. In one case
1245 // we need to create the illusion that the item isn't added back to the folder yet, to
1246 // to correspond to the animation of the icon back into the folder. This is
1247 public void hideItem(ShortcutInfo info) {
1248 View v = getViewForInfo(info);
1249 v.setVisibility(INVISIBLE);
1250 }
1251 public void showItem(ShortcutInfo info) {
1252 View v = getViewForInfo(info);
1253 v.setVisibility(VISIBLE);
1254 }
1255
Sunny Goyalb7e15ad2015-05-07 14:51:31 -07001256 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -07001257 public void onAdd(ShortcutInfo item) {
Adam Cohen05e0f402011-08-01 12:12:49 -07001258 // If the item was dropped onto this open folder, we have done the work associated
1259 // with adding the item to the folder, as indicated by mSuppressOnAdd being set
Adam Cohenbfbfd262011-06-13 16:55:12 -07001260 if (mSuppressOnAdd) return;
Sunny Goyal5d85c442015-03-10 13:14:47 -07001261 mContent.createAndAddViewForRank(item, mContent.allocateRankForNewItem(item));
Sunny Goyal2e688a82015-03-18 10:23:39 -07001262 mItemsInvalidated = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001263 LauncherModel.addOrMoveItemInDatabase(
1264 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
1265 }
1266
Adam Cohena9cf38f2011-05-02 15:36:58 -07001267 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -07001268 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001269 // If this item is being dragged from this open folder, we have already handled
1270 // the work associated with removing the item, so we don't have to do anything here.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001271 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001272 View v = getViewForInfo(item);
Sunny Goyal290800b2015-03-05 11:33:33 -08001273 mContent.removeItem(v);
Adam Cohen2801caf2011-05-13 20:57:39 -07001274 if (mState == STATE_ANIMATING) {
1275 mRearrangeOnClose = true;
1276 } else {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001277 rearrangeChildren();
Adam Cohen2801caf2011-05-13 20:57:39 -07001278 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001279 if (getItemCount() <= 1) {
1280 replaceFolderWithFinalItem();
1281 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001282 }
Adam Cohen7c693212011-05-18 15:26:57 -07001283
Sunny Goyalbc753352015-03-05 09:40:44 -08001284 private View getViewForInfo(final ShortcutInfo item) {
1285 return mContent.iterateOverItems(new ItemOperator() {
1286
1287 @Override
1288 public boolean evaluate(ItemInfo info, View view, View parent) {
1289 return info == item;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001290 }
Sunny Goyalbc753352015-03-05 09:40:44 -08001291 });
Adam Cohendf1e4e82011-06-24 15:57:39 -07001292 }
1293
Adam Cohen76078c42011-06-09 15:06:52 -07001294 public void onItemsChanged() {
Adam Cohenac56cff2011-09-28 20:45:37 -07001295 updateTextViewFocus();
Adam Cohen76078c42011-06-09 15:06:52 -07001296 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001297
Adam Cohen76fc0852011-06-17 13:26:23 -07001298 public void onTitleChanged(CharSequence title) {
1299 }
Adam Cohen76078c42011-06-09 15:06:52 -07001300
Adam Cohen7c693212011-05-18 15:26:57 -07001301 public ArrayList<View> getItemsInReadingOrder() {
1302 if (mItemsInvalidated) {
1303 mItemsInReadingOrder.clear();
Sunny Goyalbc753352015-03-05 09:40:44 -08001304 mContent.iterateOverItems(new ItemOperator() {
1305
1306 @Override
1307 public boolean evaluate(ItemInfo info, View view, View parent) {
1308 mItemsInReadingOrder.add(view);
1309 return false;
Adam Cohen7c693212011-05-18 15:26:57 -07001310 }
Sunny Goyalbc753352015-03-05 09:40:44 -08001311 });
Adam Cohen7c693212011-05-18 15:26:57 -07001312 mItemsInvalidated = false;
1313 }
1314 return mItemsInReadingOrder;
1315 }
Adam Cohen8dfcba42011-07-07 16:38:18 -07001316
1317 public void getLocationInDragLayer(int[] loc) {
1318 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
1319 }
Adam Cohenea0818d2011-09-30 20:06:58 -07001320
1321 public void onFocusChange(View v, boolean hasFocus) {
1322 if (v == mFolderName && hasFocus) {
1323 startEditingFolderName();
1324 }
1325 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001326
1327 @Override
1328 public void getHitRectRelativeToDragLayer(Rect outRect) {
1329 getHitRect(outRect);
Sunny Goyal48461932015-03-09 17:41:09 -07001330 outRect.left -= mScrollAreaOffset;
1331 outRect.right += mScrollAreaOffset;
1332 }
1333
Winson Chung8f1eff72015-05-28 17:33:40 -07001334 @Override
1335 public void fillInLaunchSourceData(Bundle sourceData) {
1336 // Fill in from the folder icon's launch source provider first
1337 Stats.LaunchSourceUtils.populateSourceDataFromAncestorProvider(mFolderIcon, sourceData);
1338 sourceData.putString(Stats.SOURCE_EXTRA_SUB_CONTAINER, Stats.SUB_CONTAINER_FOLDER);
1339 sourceData.putInt(Stats.SOURCE_EXTRA_SUB_CONTAINER_PAGE, mContent.getCurrentPage());
1340 }
1341
Sunny Goyal48461932015-03-09 17:41:09 -07001342 private class OnScrollHintListener implements OnAlarmListener {
1343
1344 private final DragObject mDragObject;
1345
1346 OnScrollHintListener(DragObject object) {
1347 mDragObject = object;
1348 }
1349
1350 /**
1351 * Scroll hint has been shown long enough. Now scroll to appropriate page.
1352 */
1353 @Override
1354 public void onAlarm(Alarm alarm) {
1355 if (mCurrentScrollDir == DragController.SCROLL_LEFT) {
Sunny Goyalb8634152015-04-09 14:17:14 -07001356 mContent.scrollLeft();
Sunny Goyal48461932015-03-09 17:41:09 -07001357 mScrollHintDir = DragController.SCROLL_NONE;
1358 } else if (mCurrentScrollDir == DragController.SCROLL_RIGHT) {
Sunny Goyalb8634152015-04-09 14:17:14 -07001359 mContent.scrollRight();
Sunny Goyal48461932015-03-09 17:41:09 -07001360 mScrollHintDir = DragController.SCROLL_NONE;
1361 } else {
1362 // This should not happen
1363 return;
1364 }
1365 mCurrentScrollDir = DragController.SCROLL_NONE;
1366
1367 // Pause drag event until the scrolling is finished
1368 mScrollPauseAlarm.setOnAlarmListener(new OnScrollFinishedListener(mDragObject));
1369 mScrollPauseAlarm.setAlarm(DragController.RESCROLL_DELAY);
1370 }
1371 }
1372
1373 private class OnScrollFinishedListener implements OnAlarmListener {
1374
1375 private final DragObject mDragObject;
1376
1377 OnScrollFinishedListener(DragObject object) {
1378 mDragObject = object;
1379 }
1380
1381 /**
1382 * Page scroll is complete.
1383 */
1384 @Override
1385 public void onAlarm(Alarm alarm) {
1386 // Reorder immediately on page change.
1387 onDragOver(mDragObject, 1);
1388 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001389 }
Sunny Goyal1dd0f8b2015-07-06 13:04:02 -07001390
1391 // Compares item position based on rank and position giving priority to the rank.
1392 private static final Comparator<ItemInfo> ITEM_POS_COMPARATOR = new Comparator<ItemInfo>() {
1393
1394 @Override
1395 public int compare(ItemInfo lhs, ItemInfo rhs) {
1396 if (lhs.rank != rhs.rank) {
1397 return lhs.rank - rhs.rank;
1398 } else if (lhs.cellY != rhs.cellY) {
1399 return lhs.cellY - rhs.cellY;
1400 } else {
1401 return lhs.cellX - rhs.cellX;
1402 }
1403 }
1404 };
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001405}