blob: 657fb7d9e930b026c35769fff0d682460f659768 [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;
Sunny Goyal1dd0f8b2015-07-06 13:04:02 -070064import java.util.Comparator;
Adam Cohenc0dcf592011-06-01 15:30:43 -070065
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066/**
67 * Represents a set of icons chosen by the user or generated by the system.
68 */
Adam Cohen8dfcba42011-07-07 16:38:18 -070069public class Folder extends LinearLayout implements DragSource, View.OnClickListener,
Adam Cohenea0818d2011-09-30 20:06:58 -070070 View.OnLongClickListener, DropTarget, FolderListener, TextView.OnEditorActionListener,
Winson Chung8f1eff72015-05-28 17:33:40 -070071 View.OnFocusChangeListener, DragListener, UninstallSource, AccessibilityDragSource,
72 Stats.LaunchSourceProvider {
Adam Cohendf2cc412011-04-27 16:56:57 -070073 private static final String TAG = "Launcher.Folder";
74
Sunny Goyalc3a609f2015-02-26 17:43:50 -080075 /**
76 * We avoid measuring {@link #mContentWrapper} with a 0 width or height, as this
77 * results in CellLayout being measured as UNSPECIFIED, which it does not support.
78 */
79 private static final int MIN_CONTENT_DIMEN = 5;
Adam Cohen4eac29a2011-07-11 17:53:37 -070080
Adam Cohendf2cc412011-04-27 16:56:57 -070081 static final int STATE_NONE = -1;
82 static final int STATE_SMALL = 0;
83 static final int STATE_ANIMATING = 1;
84 static final int STATE_OPEN = 2;
85
Sunny Goyal48461932015-03-09 17:41:09 -070086 /**
Sunny Goyal48461932015-03-09 17:41:09 -070087 * Time for which the scroll hint is shown before automatically changing page.
88 */
89 public static final int SCROLL_HINT_DURATION = DragController.SCROLL_DELAY;
90
91 /**
92 * Fraction of icon width which behave as scroll region.
93 */
94 private static final float ICON_OVERSCROLL_WIDTH_FACTOR = 0.45f;
95
Sunny Goyala07c2f52015-06-01 11:00:38 -070096 private static final int FOLDER_NAME_ANIMATION_DURATION = 633;
Sunny Goyalb7e15ad2015-05-07 14:51:31 -070097
Adam Cohenf0f4eda2013-06-06 21:27:03 -070098 private static final int REORDER_DELAY = 250;
Adam Cohen5d518fa2013-12-05 14:16:23 -080099 private static final int ON_EXIT_CLOSE_DELAY = 400;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800100 private static final Rect sTempRect = new Rect();
101
102 private static String sDefaultFolderName;
103 private static String sHintText;
104
105 private final Alarm mReorderAlarm = new Alarm();
106 private final Alarm mOnExitAlarm = new Alarm();
Sunny Goyalb8634152015-04-09 14:17:14 -0700107 private final Alarm mOnScrollHintAlarm = new Alarm();
108 @Thunk final Alarm mScrollPauseAlarm = new Alarm();
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800109
Adam Cohen091440a2015-03-18 14:16:05 -0700110 @Thunk final ArrayList<View> mItemsInReadingOrder = new ArrayList<View>();
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800111
112 private final int mExpandDuration;
113 private final int mMaterialExpandDuration;
114 private final int mMaterialExpandStagger;
115
116 private final InputMethodManager mInputMethodManager;
117
118 protected final Launcher mLauncher;
119 protected DragController mDragController;
120 protected FolderInfo mInfo;
121
Adam Cohen091440a2015-03-18 14:16:05 -0700122 @Thunk FolderIcon mFolderIcon;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800123
Sunny Goyalb8634152015-04-09 14:17:14 -0700124 @Thunk FolderPagedView mContent;
Adam Cohen091440a2015-03-18 14:16:05 -0700125 @Thunk View mContentWrapper;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800126 FolderEditText mFolderName;
127
Sunny Goyal290800b2015-03-05 11:33:33 -0800128 private View mFooter;
129 private int mFooterHeight;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800130
131 // Cell ranks used for drag and drop
Adam Cohen091440a2015-03-18 14:16:05 -0700132 @Thunk int mTargetRank, mPrevTargetRank, mEmptyCellRank;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800133
Adam Cohen091440a2015-03-18 14:16:05 -0700134 @Thunk int mState = STATE_NONE;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800135 private boolean mRearrangeOnClose = false;
Adam Cohen7c693212011-05-18 15:26:57 -0700136 boolean mItemsInvalidated = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700137 private ShortcutInfo mCurrentDragInfo;
138 private View mCurrentDragView;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800139 private boolean mIsExternalDrag;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700140 boolean mSuppressOnAdd = false;
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700141 private boolean mDragInProgress = false;
142 private boolean mDeleteFolderOnDropCompleted = false;
143 private boolean mSuppressFolderDeletion = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700144 private boolean mItemAddedBackToSelfViaIcon = false;
Adam Cohen091440a2015-03-18 14:16:05 -0700145 @Thunk float mFolderIconPivotX;
146 @Thunk float mFolderIconPivotY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700147 private boolean mIsEditingName = false;
Adam Cohen1960ea42013-11-12 11:33:14 +0000148
Adam Cohenfb91f302012-06-11 15:45:18 -0700149 private boolean mDestroyed;
150
Adam Cohen091440a2015-03-18 14:16:05 -0700151 @Thunk Runnable mDeferredAction;
Michael Jurka1e2f4652013-07-08 18:03:46 -0700152 private boolean mDeferDropAfterUninstall;
153 private boolean mUninstallSuccessful;
154
Sunny Goyal48461932015-03-09 17:41:09 -0700155 // Folder scrolling
156 private int mScrollAreaOffset;
Sunny Goyal48461932015-03-09 17:41:09 -0700157
Adam Cohen091440a2015-03-18 14:16:05 -0700158 @Thunk int mScrollHintDir = DragController.SCROLL_NONE;
159 @Thunk int mCurrentScrollDir = DragController.SCROLL_NONE;
Sunny Goyal48461932015-03-09 17:41:09 -0700160
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161 /**
162 * Used to inflate the Workspace from XML.
163 *
164 * @param context The application's context.
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800165 * @param attrs The attributes set containing the Workspace's customization values.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166 */
167 public Folder(Context context, AttributeSet attrs) {
168 super(context, attrs);
169 setAlwaysDrawnWithCacheEnabled(false);
Adam Cohen76fc0852011-06-17 13:26:23 -0700170 mInputMethodManager = (InputMethodManager)
Michael Jurka8b805b12012-04-18 14:23:14 -0700171 getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
Adam Cohen76fc0852011-06-17 13:26:23 -0700172
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800173 Resources res = getResources();
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700174 mExpandDuration = res.getInteger(R.integer.config_folderExpandDuration);
175 mMaterialExpandDuration = res.getInteger(R.integer.config_materialFolderExpandDuration);
176 mMaterialExpandStagger = res.getInteger(R.integer.config_materialFolderExpandStagger);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700177
178 if (sDefaultFolderName == null) {
179 sDefaultFolderName = res.getString(R.string.folder_name);
180 }
Adam Cohena65beee2011-06-27 21:32:23 -0700181 if (sHintText == null) {
182 sHintText = res.getString(R.string.folder_hint_text);
183 }
Adam Cohen4eac29a2011-07-11 17:53:37 -0700184 mLauncher = (Launcher) context;
Adam Cohenac56cff2011-09-28 20:45:37 -0700185 // We need this view to be focusable in touch mode so that when text editing of the folder
186 // name is complete, we have something to focus on, thus hiding the cursor and giving
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800187 // reliable behavior when clicking the text field (since it will always gain focus on click).
Adam Cohenac56cff2011-09-28 20:45:37 -0700188 setFocusableInTouchMode(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800189 }
190
191 @Override
192 protected void onFinishInflate() {
193 super.onFinishInflate();
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800194 mContentWrapper = findViewById(R.id.folder_content_wrapper);
Sunny Goyalb8634152015-04-09 14:17:14 -0700195 mContent = (FolderPagedView) findViewById(R.id.folder_content);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800196 mContent.setFolder(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700197
Adam Cohenac56cff2011-09-28 20:45:37 -0700198 mFolderName = (FolderEditText) findViewById(R.id.folder_name);
199 mFolderName.setFolder(this);
Adam Cohenea0818d2011-09-30 20:06:58 -0700200 mFolderName.setOnFocusChangeListener(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700201
Adam Cohen76fc0852011-06-17 13:26:23 -0700202 // We disable action mode for now since it messes up the view on phones
203 mFolderName.setCustomSelectionActionModeCallback(mActionModeCallback);
Adam Cohen76fc0852011-06-17 13:26:23 -0700204 mFolderName.setOnEditorActionListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700205 mFolderName.setSelectAllOnFocus(true);
Adam Cohen7a14d0b2011-06-24 11:36:35 -0700206 mFolderName.setInputType(mFolderName.getInputType() |
207 InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800208
Sunny Goyalb8634152015-04-09 14:17:14 -0700209 mFooter = findViewById(R.id.folder_footer);
Sunny Goyalb8634152015-04-09 14:17:14 -0700210
Sunny Goyal290800b2015-03-05 11:33:33 -0800211 // We find out how tall footer wants to be (it is set to wrap_content), so that
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800212 // we can allocate the appropriate amount of space for it.
213 int measureSpec = MeasureSpec.UNSPECIFIED;
Sunny Goyal290800b2015-03-05 11:33:33 -0800214 mFooter.measure(measureSpec, measureSpec);
215 mFooterHeight = mFooter.getMeasuredHeight();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700217
Adam Cohen76fc0852011-06-17 13:26:23 -0700218 private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
219 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
220 return false;
221 }
222
223 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
224 return false;
225 }
226
227 public void onDestroyActionMode(ActionMode mode) {
228 }
229
230 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
231 return false;
232 }
233 };
234
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800235 public void onClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700236 Object tag = v.getTag();
237 if (tag instanceof ShortcutInfo) {
Adam Cohenb5fe60c2013-06-06 22:03:51 -0700238 mLauncher.onClick(v);
Adam Cohendf2cc412011-04-27 16:56:57 -0700239 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800240 }
241
242 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -0700243 // Return if global dragging is not enabled
244 if (!mLauncher.isDraggingEnabled()) return true;
Sunny Goyale9b651e2015-04-24 11:44:51 -0700245 return beginDrag(v, false);
246 }
Winson Chung36a62fe2012-05-06 18:04:42 -0700247
Sunny Goyale9b651e2015-04-24 11:44:51 -0700248 private boolean beginDrag(View v, boolean accessible) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700249 Object tag = v.getTag();
250 if (tag instanceof ShortcutInfo) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700251 ShortcutInfo item = (ShortcutInfo) tag;
252 if (!v.isInTouchMode()) {
253 return false;
254 }
255
Sunny Goyale9b651e2015-04-24 11:44:51 -0700256 mLauncher.getWorkspace().beginDragShared(v, new Point(), this, accessible);
Adam Cohen76078c42011-06-09 15:06:52 -0700257
258 mCurrentDragInfo = item;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800259 mEmptyCellRank = item.rank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700260 mCurrentDragView = v;
Adam Cohenfc53cd22011-07-20 15:45:11 -0700261
Sunny Goyal290800b2015-03-05 11:33:33 -0800262 mContent.removeItem(mCurrentDragView);
Adam Cohenfc53cd22011-07-20 15:45:11 -0700263 mInfo.remove(mCurrentDragInfo);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700264 mDragInProgress = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700265 mItemAddedBackToSelfViaIcon = false;
Adam Cohendf2cc412011-04-27 16:56:57 -0700266 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800267 return true;
268 }
269
Sunny Goyale9b651e2015-04-24 11:44:51 -0700270 @Override
271 public void startDrag(CellInfo cellInfo, boolean accessible) {
272 beginDrag(cellInfo.cell, accessible);
273 }
274
275 @Override
276 public void enableAccessibleDrag(boolean enable) {
277 mLauncher.getSearchBar().enableAccessibleDrag(enable);
278 for (int i = 0; i < mContent.getChildCount(); i++) {
279 mContent.getPageAt(i).enableAccessibleDrag(enable, CellLayout.FOLDER_ACCESSIBILITY_DRAG);
280 }
Sunny Goyalccc414b2015-04-30 12:28:16 -0700281
282 mFooter.setImportantForAccessibility(enable ? IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS :
283 IMPORTANT_FOR_ACCESSIBILITY_AUTO);
Sunny Goyale9b651e2015-04-24 11:44:51 -0700284 mLauncher.getWorkspace().setAddNewPageOnDrag(!enable);
285 }
286
Adam Cohen76fc0852011-06-17 13:26:23 -0700287 public boolean isEditingName() {
288 return mIsEditingName;
289 }
290
291 public void startEditingFolderName() {
Adam Cohena65beee2011-06-27 21:32:23 -0700292 mFolderName.setHint("");
Adam Cohen76fc0852011-06-17 13:26:23 -0700293 mIsEditingName = true;
294 }
295
296 public void dismissEditingName() {
297 mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
298 doneEditingFolderName(true);
299 }
300
301 public void doneEditingFolderName(boolean commit) {
Adam Cohena65beee2011-06-27 21:32:23 -0700302 mFolderName.setHint(sHintText);
Adam Cohen1df26a32011-08-12 16:15:23 -0700303 // Convert to a string here to ensure that no other state associated with the text field
304 // gets saved.
Sunny Goyal230eade2015-06-18 12:48:51 -0700305 String newTitle = mFolderName.getText().toString();
Adam Cohen3371da02011-10-25 21:38:29 -0700306 mInfo.setTitle(newTitle);
Adam Cohen76fc0852011-06-17 13:26:23 -0700307 LauncherModel.updateItemInDatabase(mLauncher, mInfo);
Adam Cohenac56cff2011-09-28 20:45:37 -0700308
Adam Cohen3371da02011-10-25 21:38:29 -0700309 if (commit) {
310 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Sunny Goyal230eade2015-06-18 12:48:51 -0700311 String.format(getContext().getString(R.string.folder_renamed), newTitle));
Adam Cohen3371da02011-10-25 21:38:29 -0700312 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700313 // In order to clear the focus from the text field, we set the focus on ourself. This
314 // ensures that every time the field is clicked, focus is gained, giving reliable behavior.
315 requestFocus();
316
Adam Cohene601a432011-07-26 21:51:30 -0700317 Selection.setSelection((Spannable) mFolderName.getText(), 0, 0);
Adam Cohen76fc0852011-06-17 13:26:23 -0700318 mIsEditingName = false;
319 }
320
321 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
322 if (actionId == EditorInfo.IME_ACTION_DONE) {
323 dismissEditingName();
324 return true;
325 }
326 return false;
327 }
328
329 public View getEditTextRegion() {
330 return mFolderName;
331 }
332
Adam Cohen0c872ba2011-05-05 10:34:16 -0700333 /**
334 * We need to handle touch events to prevent them from falling through to the workspace below.
335 */
Sunny Goyal70660032015-05-14 00:07:08 -0700336 @SuppressLint("ClickableViewAccessibility")
Adam Cohen0c872ba2011-05-05 10:34:16 -0700337 @Override
338 public boolean onTouchEvent(MotionEvent ev) {
339 return true;
340 }
341
Joe Onorato00acb122009-08-04 16:04:30 -0400342 public void setDragController(DragController dragController) {
343 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800344 }
345
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800346 public void setFolderIcon(FolderIcon icon) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700347 mFolderIcon = icon;
348 }
349
Adam Cohen3371da02011-10-25 21:38:29 -0700350 @Override
351 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
352 // When the folder gets focus, we don't want to announce the list of items.
353 return true;
354 }
355
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800356 /**
357 * @return the FolderInfo object associated with this folder
358 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700359 public FolderInfo getInfo() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800360 return mInfo;
361 }
362
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800363 void bind(FolderInfo info) {
364 mInfo = info;
Adam Cohendf2cc412011-04-27 16:56:57 -0700365 ArrayList<ShortcutInfo> children = info.contents;
Sunny Goyal1dd0f8b2015-07-06 13:04:02 -0700366 Collections.sort(children, ITEM_POS_COMPARATOR);
Sunny Goyal08f72612015-01-05 13:41:43 -0800367
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800368 ArrayList<ShortcutInfo> overflow = mContent.bindItems(children);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700369
Jason Monk4ff73882014-04-24 16:48:07 -0400370 // If our folder has too many items we prune them from the list. This is an issue
Adam Cohenc508b2d2011-06-28 14:41:44 -0700371 // when upgrading from the old Folders implementation which could contain an unlimited
372 // number of items.
373 for (ShortcutInfo item: overflow) {
374 mInfo.remove(item);
375 LauncherModel.deleteItemFromDatabase(mLauncher, item);
376 }
377
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800378 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
379 if (lp == null) {
380 lp = new DragLayer.LayoutParams(0, 0);
381 lp.customPosition = true;
382 setLayoutParams(lp);
383 }
384 centerAboutIcon();
385
Adam Cohen4dbe6d92011-05-18 17:14:15 -0700386 mItemsInvalidated = true;
Adam Cohenac56cff2011-09-28 20:45:37 -0700387 updateTextViewFocus();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700388 mInfo.addListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700389
Adam Cohenafb01ee2011-06-23 15:38:03 -0700390 if (!sDefaultFolderName.contentEquals(mInfo.title)) {
Adam Cohen4ef610f2011-06-21 22:37:36 -0700391 mFolderName.setText(mInfo.title);
392 } else {
393 mFolderName.setText("");
394 }
Winson Chung33231f52013-12-09 16:57:45 -0800395
396 // In case any children didn't come across during loading, clean up the folder accordingly
397 mFolderIcon.post(new Runnable() {
398 public void run() {
399 if (getItemCount() <= 1) {
400 replaceFolderWithFinalItem();
401 }
402 }
403 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700404 }
405
406 /**
407 * Creates a new UserFolder, inflated from R.layout.user_folder.
408 *
409 * @param context The application's context.
410 *
411 * @return A new UserFolder.
412 */
Sunny Goyalc23da842015-05-14 20:44:01 -0700413 @SuppressLint("InflateParams")
Sunny Goyal70660032015-05-14 00:07:08 -0700414 static Folder fromXml(Launcher launcher) {
Sunny Goyalc23da842015-05-14 20:44:01 -0700415 return (Folder) launcher.getLayoutInflater().inflate(R.layout.user_folder, null);
Adam Cohendf2cc412011-04-27 16:56:57 -0700416 }
417
418 /**
419 * This method is intended to make the UserFolder to be visually identical in size and position
420 * to its associated FolderIcon. This allows for a seamless transition into the expanded state.
421 */
422 private void positionAndSizeAsIcon() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700423 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800424 setScaleX(0.8f);
425 setScaleY(0.8f);
426 setAlpha(0f);
Adam Cohendf2cc412011-04-27 16:56:57 -0700427 mState = STATE_SMALL;
428 }
429
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700430 private void prepareReveal() {
431 setScaleX(1f);
432 setScaleY(1f);
433 setAlpha(1f);
434 mState = STATE_SMALL;
435 }
436
Adam Cohendf2cc412011-04-27 16:56:57 -0700437 public void animateOpen() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700438 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen6441de02011-12-14 14:25:32 -0800439
Sunny Goyalb8634152015-04-09 14:17:14 -0700440 mContent.completePendingPageChanges();
Sunny Goyal8167dc22015-04-27 13:44:01 -0700441 if (!mDragInProgress) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700442 // Open on the first page.
443 mContent.snapToPageImmediately(0);
Sunny Goyal48461932015-03-09 17:41:09 -0700444 }
445
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700446 Animator openFolderAnim = null;
447 final Runnable onCompleteRunnable;
Kenny Guyd794a3f2014-09-16 15:17:58 +0100448 if (!Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700449 positionAndSizeAsIcon();
450 centerAboutIcon();
451
452 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1);
453 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
454 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
455 final ObjectAnimator oa =
456 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
457 oa.setDuration(mExpandDuration);
458 openFolderAnim = oa;
459
460 setLayerType(LAYER_TYPE_HARDWARE, null);
461 onCompleteRunnable = new Runnable() {
462 @Override
463 public void run() {
464 setLayerType(LAYER_TYPE_NONE, null);
465 }
466 };
467 } else {
468 prepareReveal();
469 centerAboutIcon();
470
471 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
472 int height = getFolderHeight();
473
474 float transX = - 0.075f * (width / 2 - getPivotX());
475 float transY = - 0.075f * (height / 2 - getPivotY());
476 setTranslationX(transX);
477 setTranslationY(transY);
478 PropertyValuesHolder tx = PropertyValuesHolder.ofFloat("translationX", transX, 0);
479 PropertyValuesHolder ty = PropertyValuesHolder.ofFloat("translationY", transY, 0);
480
Sunny Goyal75deaf32015-05-07 16:13:12 -0700481 Animator drift = LauncherAnimUtils.ofPropertyValuesHolder(this, tx, ty);
482 drift.setDuration(mMaterialExpandDuration);
483 drift.setStartDelay(mMaterialExpandStagger);
484 drift.setInterpolator(new LogDecelerateInterpolator(100, 0));
485
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700486 int rx = (int) Math.max(Math.max(width - getPivotX(), 0), getPivotX());
487 int ry = (int) Math.max(Math.max(height - getPivotY(), 0), getPivotY());
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700488 float radius = (float) Math.hypot(rx, ry);
Sunny Goyal75deaf32015-05-07 16:13:12 -0700489
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700490 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
491 Animator reveal = LauncherAnimUtils.createCircularReveal(this, (int) getPivotX(),
492 (int) getPivotY(), 0, radius);
493 reveal.setDuration(mMaterialExpandDuration);
494 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
495
Sunny Goyalbc753352015-03-05 09:40:44 -0800496 mContentWrapper.setAlpha(0f);
497 Animator iconsAlpha = LauncherAnimUtils.ofFloat(mContentWrapper, "alpha", 0f, 1f);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700498 iconsAlpha.setDuration(mMaterialExpandDuration);
499 iconsAlpha.setStartDelay(mMaterialExpandStagger);
500 iconsAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
501
Sunny Goyal290800b2015-03-05 11:33:33 -0800502 mFooter.setAlpha(0f);
503 Animator textAlpha = LauncherAnimUtils.ofFloat(mFooter, "alpha", 0f, 1f);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700504 textAlpha.setDuration(mMaterialExpandDuration);
505 textAlpha.setStartDelay(mMaterialExpandStagger);
506 textAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
507
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700508 anim.play(drift);
509 anim.play(iconsAlpha);
510 anim.play(textAlpha);
511 anim.play(reveal);
512
513 openFolderAnim = anim;
514
Sunny Goyalbc753352015-03-05 09:40:44 -0800515 mContentWrapper.setLayerType(LAYER_TYPE_HARDWARE, null);
Sunny Goyal75deaf32015-05-07 16:13:12 -0700516 mFooter.setLayerType(LAYER_TYPE_HARDWARE, null);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700517 onCompleteRunnable = new Runnable() {
518 @Override
519 public void run() {
Sunny Goyalbc753352015-03-05 09:40:44 -0800520 mContentWrapper.setLayerType(LAYER_TYPE_NONE, null);
Sunny Goyal75deaf32015-05-07 16:13:12 -0700521 mContentWrapper.setLayerType(LAYER_TYPE_NONE, null);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700522 }
523 };
524 }
525 openFolderAnim.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700526 @Override
527 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700528 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Sunny Goyalbc753352015-03-05 09:40:44 -0800529 mContent.getAccessibilityDescription());
Adam Cohendf2cc412011-04-27 16:56:57 -0700530 mState = STATE_ANIMATING;
531 }
532 @Override
533 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700534 mState = STATE_OPEN;
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700535
536 if (onCompleteRunnable != null) {
537 onCompleteRunnable.run();
538 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800539
Sunny Goyalbc753352015-03-05 09:40:44 -0800540 mContent.setFocusOnFirstChild();
Adam Cohendf2cc412011-04-27 16:56:57 -0700541 }
542 });
Sunny Goyalb7e15ad2015-05-07 14:51:31 -0700543
544 // Footer animation
545 if (mContent.getPageCount() > 1 && !mInfo.hasOption(FolderInfo.FLAG_MULTI_PAGE_ANIMATION)) {
546 int footerWidth = mContent.getDesiredWidth()
547 - mFooter.getPaddingLeft() - mFooter.getPaddingRight();
548
549 float textWidth = mFolderName.getPaint().measureText(mFolderName.getText().toString());
Sunny Goyala07c2f52015-06-01 11:00:38 -0700550 float translation = (footerWidth - textWidth) / 2;
551 mFolderName.setTranslationX(mContent.mIsRtl ? -translation : translation);
Sunny Goyalb7e15ad2015-05-07 14:51:31 -0700552 mContent.setMarkerScale(0);
553
554 // Do not update the flag if we are in drag mode. The flag will be updated, when we
555 // actually drop the icon.
556 final boolean updateAnimationFlag = !mDragInProgress;
557 openFolderAnim.addListener(new AnimatorListenerAdapter() {
558
559 @Override
560 public void onAnimationEnd(Animator animation) {
Sunny Goyala07c2f52015-06-01 11:00:38 -0700561 mFolderName.animate().setDuration(FOLDER_NAME_ANIMATION_DURATION)
562 .translationX(0)
Sunny Goyalc1cd23b2015-06-01 17:15:34 -0700563 .setInterpolator(Utilities.isLmpOrAbove() ?
564 AnimationUtils.loadInterpolator(mLauncher,
565 android.R.interpolator.fast_out_slow_in)
566 : new LogDecelerateInterpolator(100, 0));
Sunny Goyalb7e15ad2015-05-07 14:51:31 -0700567 mContent.animateMarkers();
568
569 if (updateAnimationFlag) {
570 mInfo.setOption(FolderInfo.FLAG_MULTI_PAGE_ANIMATION, true, mLauncher);
571 }
572 }
573 });
574 } else {
575 mFolderName.setTranslationX(0);
576 mContent.setMarkerScale(1);
577 }
578
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700579 openFolderAnim.start();
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800580
581 // Make sure the folder picks up the last drag move even if the finger doesn't move.
582 if (mDragController.isDragging()) {
583 mDragController.forceTouchMove();
584 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700585
Sunny Goyalb8634152015-04-09 14:17:14 -0700586 FolderPagedView pages = (FolderPagedView) mContent;
587 pages.verifyVisibleHighResIcons(pages.getNextPage());
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800588 }
589
590 public void beginExternalDrag(ShortcutInfo item) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800591 mCurrentDragInfo = item;
Sunny Goyal5d85c442015-03-10 13:14:47 -0700592 mEmptyCellRank = mContent.allocateRankForNewItem(item);
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800593 mIsExternalDrag = true;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800594 mDragInProgress = true;
Sunny Goyalb8634152015-04-09 14:17:14 -0700595
Sunny Goyalf4066152015-04-15 09:42:19 -0700596 // Since this folder opened by another controller, it might not get onDrop or
597 // onDropComplete. Perform cleanup once drag-n-drop ends.
598 mDragController.addDragListener(this);
599 }
600
601 @Override
602 public void onDragStart(DragSource source, Object info, int dragAction) { }
603
604 @Override
605 public void onDragEnd() {
606 if (mIsExternalDrag && mDragInProgress) {
607 completeDragExit();
608 }
609 mDragController.removeDragListener(this);
Adam Cohendf2cc412011-04-27 16:56:57 -0700610 }
611
Adam Cohen091440a2015-03-18 14:16:05 -0700612 @Thunk void sendCustomAccessibilityEvent(int type, String text) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700613 AccessibilityManager accessibilityManager = (AccessibilityManager)
614 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
615 if (accessibilityManager.isEnabled()) {
Adam Cohen3371da02011-10-25 21:38:29 -0700616 AccessibilityEvent event = AccessibilityEvent.obtain(type);
617 onInitializeAccessibilityEvent(event);
618 event.getText().add(text);
Michael Jurka8b805b12012-04-18 14:23:14 -0700619 accessibilityManager.sendAccessibilityEvent(event);
Adam Cohen3371da02011-10-25 21:38:29 -0700620 }
621 }
622
Adam Cohendf2cc412011-04-27 16:56:57 -0700623 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700624 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800625 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
626 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
627 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
Michael Jurka032e6ba2013-04-22 15:08:42 +0200628 final ObjectAnimator oa =
Michael Jurka2ecf9952012-06-18 12:52:28 -0700629 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohendf2cc412011-04-27 16:56:57 -0700630
Adam Cohen2801caf2011-05-13 20:57:39 -0700631 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700632 @Override
633 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700634 onCloseComplete();
Michael Jurka0121c3e2012-05-31 08:36:04 -0700635 setLayerType(LAYER_TYPE_NONE, null);
Adam Cohen2801caf2011-05-13 20:57:39 -0700636 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700637 }
638 @Override
639 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700640 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700641 getContext().getString(R.string.folder_closed));
Adam Cohendf2cc412011-04-27 16:56:57 -0700642 mState = STATE_ANIMATING;
643 }
644 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700645 oa.setDuration(mExpandDuration);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700646 setLayerType(LAYER_TYPE_HARDWARE, null);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100647 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700648 }
649
Adam Cohencb3382b2011-05-24 14:07:08 -0700650 public boolean acceptDrop(DragObject d) {
651 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700652 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700653 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
654 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
655 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700656 }
657
Adam Cohencb3382b2011-05-24 14:07:08 -0700658 public void onDragEnter(DragObject d) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800659 mPrevTargetRank = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700660 mOnExitAlarm.cancelAlarm();
Sunny Goyalb8634152015-04-09 14:17:14 -0700661 // Get the area offset such that the folder only closes if half the drag icon width
662 // is outside the folder area
663 mScrollAreaOffset = d.dragView.getDragRegionWidth() / 2 - d.xOffset;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700664 }
665
666 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
667 public void onAlarm(Alarm alarm) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800668 mContent.realTimeReorder(mEmptyCellRank, mTargetRank);
669 mEmptyCellRank = mTargetRank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700670 }
671 };
672
Sunny Goyalc46bfef2015-01-05 12:40:08 -0800673 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohen2374abf2013-04-16 14:56:57 -0700674 public boolean isLayoutRtl() {
675 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
676 }
677
Sunny Goyal48461932015-03-09 17:41:09 -0700678 @Override
Adam Cohencb3382b2011-05-24 14:07:08 -0700679 public void onDragOver(DragObject d) {
Sunny Goyal48461932015-03-09 17:41:09 -0700680 onDragOver(d, REORDER_DELAY);
681 }
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700682
Sunny Goyal48461932015-03-09 17:41:09 -0700683 private int getTargetRank(DragObject d, float[] recycle) {
684 recycle = d.getVisualCenter(recycle);
685 return mContent.findNearestArea(
686 (int) recycle[0] - getPaddingLeft(), (int) recycle[1] - getPaddingTop());
687 }
688
Adam Cohen091440a2015-03-18 14:16:05 -0700689 @Thunk void onDragOver(DragObject d, int reorderDelay) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700690 if (mScrollPauseAlarm.alarmPending()) {
Sunny Goyal48461932015-03-09 17:41:09 -0700691 return;
692 }
693 final float[] r = new float[2];
694 mTargetRank = getTargetRank(d, r);
695
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800696 if (mTargetRank != mPrevTargetRank) {
Alan Viverette4cda5b72013-08-28 17:53:41 -0700697 mReorderAlarm.cancelAlarm();
Sunny Goyalc46bfef2015-01-05 12:40:08 -0800698 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
699 mReorderAlarm.setAlarm(REORDER_DELAY);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800700 mPrevTargetRank = mTargetRank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700701 }
Sunny Goyal48461932015-03-09 17:41:09 -0700702
Sunny Goyal48461932015-03-09 17:41:09 -0700703 float x = r[0];
Sunny Goyalb8634152015-04-09 14:17:14 -0700704 int currentPage = mContent.getNextPage();
Sunny Goyal48461932015-03-09 17:41:09 -0700705
Sunny Goyalb8634152015-04-09 14:17:14 -0700706 float cellOverlap = mContent.getCurrentCellLayout().getCellWidth()
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700707 * ICON_OVERSCROLL_WIDTH_FACTOR;
708 boolean isOutsideLeftEdge = x < cellOverlap;
709 boolean isOutsideRightEdge = x > (getWidth() - cellOverlap);
Sunny Goyal48461932015-03-09 17:41:09 -0700710
Sunny Goyal70660032015-05-14 00:07:08 -0700711 if (currentPage > 0 && (mContent.mIsRtl ? isOutsideRightEdge : isOutsideLeftEdge)) {
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700712 showScrollHint(DragController.SCROLL_LEFT, d);
Sunny Goyalb8634152015-04-09 14:17:14 -0700713 } else if (currentPage < (mContent.getPageCount() - 1)
Sunny Goyal70660032015-05-14 00:07:08 -0700714 && (mContent.mIsRtl ? isOutsideLeftEdge : isOutsideRightEdge)) {
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700715 showScrollHint(DragController.SCROLL_RIGHT, d);
Sunny Goyal48461932015-03-09 17:41:09 -0700716 } else {
717 mOnScrollHintAlarm.cancelAlarm();
718 if (mScrollHintDir != DragController.SCROLL_NONE) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700719 mContent.clearScrollHint();
Sunny Goyal48461932015-03-09 17:41:09 -0700720 mScrollHintDir = DragController.SCROLL_NONE;
721 }
722 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700723 }
724
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700725 private void showScrollHint(int direction, DragObject d) {
726 // Show scroll hint on the right
727 if (mScrollHintDir != direction) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700728 mContent.showScrollHint(direction);
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700729 mScrollHintDir = direction;
730 }
731
732 // Set alarm for when the hint is complete
733 if (!mOnScrollHintAlarm.alarmPending() || mCurrentScrollDir != direction) {
734 mCurrentScrollDir = direction;
735 mOnScrollHintAlarm.cancelAlarm();
736 mOnScrollHintAlarm.setOnAlarmListener(new OnScrollHintListener(d));
737 mOnScrollHintAlarm.setAlarm(SCROLL_HINT_DURATION);
738
739 mReorderAlarm.cancelAlarm();
740 mTargetRank = mEmptyCellRank;
741 }
742 }
743
Adam Cohenbfbfd262011-06-13 16:55:12 -0700744 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
745 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700746 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700747 }
748 };
749
Adam Cohen95bb8002011-07-03 23:40:28 -0700750 public void completeDragExit() {
Sunny Goyalf4066152015-04-15 09:42:19 -0700751 if (mInfo.opened) {
752 mLauncher.closeFolder();
753 mRearrangeOnClose = true;
Sunny Goyal31abc292015-05-01 10:42:32 -0700754 } else if (mState == STATE_ANIMATING) {
755 mRearrangeOnClose = true;
Sunny Goyalf4066152015-04-15 09:42:19 -0700756 } else {
757 rearrangeChildren();
Sunny Goyal31abc292015-05-01 10:42:32 -0700758 clearDragInfo();
Sunny Goyalf4066152015-04-15 09:42:19 -0700759 }
Sunny Goyal31abc292015-05-01 10:42:32 -0700760 }
761
762 private void clearDragInfo() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700763 mCurrentDragInfo = null;
764 mCurrentDragView = null;
765 mSuppressOnAdd = false;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800766 mIsExternalDrag = false;
Adam Cohen3e8f8112011-07-02 18:03:00 -0700767 }
768
Adam Cohencb3382b2011-05-24 14:07:08 -0700769 public void onDragExit(DragObject d) {
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700770 // We only close the folder if this is a true drag exit, ie. not because
771 // a drop has occurred above the folder.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700772 if (!d.dragComplete) {
773 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
774 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
775 }
776 mReorderAlarm.cancelAlarm();
Sunny Goyal48461932015-03-09 17:41:09 -0700777
Sunny Goyalb8634152015-04-09 14:17:14 -0700778 mOnScrollHintAlarm.cancelAlarm();
779 mScrollPauseAlarm.cancelAlarm();
780 if (mScrollHintDir != DragController.SCROLL_NONE) {
781 mContent.clearScrollHint();
782 mScrollHintDir = DragController.SCROLL_NONE;
Sunny Goyal48461932015-03-09 17:41:09 -0700783 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700784 }
785
Sunny Goyale9b651e2015-04-24 11:44:51 -0700786 /**
787 * When performing an accessibility drop, onDrop is sent immediately after onDragEnter. So we
788 * need to complete all transient states based on timers.
789 */
790 @Override
791 public void prepareAccessibilityDrop() {
792 if (mReorderAlarm.alarmPending()) {
793 mReorderAlarm.cancelAlarm();
794 mReorderAlarmListener.onAlarm(mReorderAlarm);
795 }
796 }
797
Michael Jurka1e2f4652013-07-08 18:03:46 -0700798 public void onDropCompleted(final View target, final DragObject d,
799 final boolean isFlingToDelete, final boolean success) {
800 if (mDeferDropAfterUninstall) {
Michael Jurkaf3007582013-08-21 14:33:57 +0200801 Log.d(TAG, "Deferred handling drop because waiting for uninstall.");
Michael Jurka1e2f4652013-07-08 18:03:46 -0700802 mDeferredAction = new Runnable() {
803 public void run() {
804 onDropCompleted(target, d, isFlingToDelete, success);
805 mDeferredAction = null;
806 }
807 };
808 return;
809 }
810
811 boolean beingCalledAfterUninstall = mDeferredAction != null;
812 boolean successfulDrop =
813 success && (!beingCalledAfterUninstall || mUninstallSuccessful);
Winson Chung5f8afe62013-08-12 16:19:28 -0700814
Michael Jurka1e2f4652013-07-08 18:03:46 -0700815 if (successfulDrop) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800816 if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon && target != this) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700817 replaceFolderWithFinalItem();
818 }
819 } else {
820 // The drag failed, we need to return the item to the folder
Sunny Goyalb8c663c2015-04-23 11:43:48 -0700821 ShortcutInfo info = (ShortcutInfo) d.dragInfo;
822 View icon = (mCurrentDragView != null && mCurrentDragView.getTag() == info)
823 ? mCurrentDragView : mContent.createNewView(info);
824 ArrayList<View> views = getItemsInReadingOrder();
825 views.add(info.rank, icon);
826 mContent.arrangeChildren(views, views.size());
827 mItemsInvalidated = true;
828
829 mSuppressOnAdd = true;
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700830 mFolderIcon.onDrop(d);
Sunny Goyalb8c663c2015-04-23 11:43:48 -0700831 mSuppressOnAdd = false;
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700832 }
833
834 if (target != this) {
835 if (mOnExitAlarm.alarmPending()) {
836 mOnExitAlarm.cancelAlarm();
Michael Jurka54554252013-08-01 12:52:23 +0200837 if (!successfulDrop) {
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700838 mSuppressFolderDeletion = true;
839 }
Sunny Goyal5d85c442015-03-10 13:14:47 -0700840 mScrollPauseAlarm.cancelAlarm();
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700841 completeDragExit();
842 }
Adam Cohen9c58d822013-12-13 17:18:10 -0800843 }
844
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700845 mDeleteFolderOnDropCompleted = false;
846 mDragInProgress = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700847 mItemAddedBackToSelfViaIcon = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700848 mCurrentDragInfo = null;
849 mCurrentDragView = null;
850 mSuppressOnAdd = false;
Adam Cohen4045eb72011-10-06 11:44:26 -0700851
852 // Reordering may have occured, and we need to save the new item locations. We do this once
853 // at the end to prevent unnecessary database operations.
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700854 updateItemLocationsInDatabaseBatch();
Sunny Goyalb7e15ad2015-05-07 14:51:31 -0700855
856 // Use the item count to check for multi-page as the folder UI may not have
857 // been refreshed yet.
858 if (getItemCount() <= mContent.itemsPerPage()) {
859 // Show the animation, next time something is added to the folder.
860 mInfo.setOption(FolderInfo.FLAG_MULTI_PAGE_ANIMATION, false, mLauncher);
861 }
862
Adam Cohen4045eb72011-10-06 11:44:26 -0700863 }
864
Sunny Goyalfa401a12015-04-10 13:45:42 -0700865 @Override
Michael Jurka1e2f4652013-07-08 18:03:46 -0700866 public void deferCompleteDropAfterUninstallActivity() {
867 mDeferDropAfterUninstall = true;
868 }
869
Sunny Goyalfa401a12015-04-10 13:45:42 -0700870 @Override
Michael Jurka1e2f4652013-07-08 18:03:46 -0700871 public void onUninstallActivityReturned(boolean success) {
872 mDeferDropAfterUninstall = false;
873 mUninstallSuccessful = success;
874 if (mDeferredAction != null) {
875 mDeferredAction.run();
876 }
877 }
878
Winson Chunga48487a2012-03-20 16:19:37 -0700879 @Override
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800880 public float getIntrinsicIconScaleFactor() {
881 return 1f;
882 }
883
884 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800885 public boolean supportsFlingToDelete() {
886 return true;
887 }
888
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000889 @Override
890 public boolean supportsAppInfoDropTarget() {
891 return false;
892 }
893
894 @Override
895 public boolean supportsDeleteDropTarget() {
896 return true;
897 }
898
Sunny Goyalddec7342015-04-29 18:12:37 -0700899 @Override
900 public void onFlingToDelete(DragObject d, PointF vec) {
Winson Chunga48487a2012-03-20 16:19:37 -0700901 // Do nothing
902 }
903
904 @Override
905 public void onFlingToDeleteCompleted() {
906 // Do nothing
907 }
908
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700909 private void updateItemLocationsInDatabaseBatch() {
910 ArrayList<View> list = getItemsInReadingOrder();
911 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
912 for (int i = 0; i < list.size(); i++) {
913 View v = list.get(i);
914 ItemInfo info = (ItemInfo) v.getTag();
Sunny Goyal08f72612015-01-05 13:41:43 -0800915 info.rank = i;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700916 items.add(info);
917 }
918
919 LauncherModel.moveItemsInDatabase(mLauncher, items, mInfo.id, 0);
920 }
921
Adam Cohene25af792013-06-06 23:08:25 -0700922 public void addItemLocationsInDatabase() {
923 ArrayList<View> list = getItemsInReadingOrder();
924 for (int i = 0; i < list.size(); i++) {
925 View v = list.get(i);
926 ItemInfo info = (ItemInfo) v.getTag();
927 LauncherModel.addItemToDatabase(mLauncher, info, mInfo.id, 0,
Winson Chung8f1eff72015-05-28 17:33:40 -0700928 info.cellX, info.cellY);
Adam Cohene25af792013-06-06 23:08:25 -0700929 }
930 }
931
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700932 public void notifyDrop() {
933 if (mDragInProgress) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700934 mItemAddedBackToSelfViaIcon = true;
Adam Cohen76078c42011-06-09 15:06:52 -0700935 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700936 }
937
938 public boolean isDropEnabled() {
939 return true;
940 }
941
Adam Cohen2801caf2011-05-13 20:57:39 -0700942 public boolean isFull() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800943 return mContent.isFull();
Adam Cohen2801caf2011-05-13 20:57:39 -0700944 }
945
946 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700947 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700948
Winson Chung892c74d2013-08-22 16:15:50 -0700949 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700950 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700951 int height = getFolderHeight();
Adam Cohen2801caf2011-05-13 20:57:39 -0700952
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800953 float scale = parent.getDescendantRectRelativeToSelf(mFolderIcon, sTempRect);
Adam Cohen8e776a62011-06-28 18:10:06 -0700954
Adam Cohen2e6da152015-05-06 11:42:25 -0700955 DeviceProfile grid = mLauncher.getDeviceProfile();
Winson Chungaf40f202013-09-18 18:26:31 -0700956
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800957 int centerX = (int) (sTempRect.left + sTempRect.width() * scale / 2);
958 int centerY = (int) (sTempRect.top + sTempRect.height() * scale / 2);
Adam Cohen2801caf2011-05-13 20:57:39 -0700959 int centeredLeft = centerX - width / 2;
960 int centeredTop = centerY - height / 2;
Winson Chung3057b1c2013-10-10 17:35:35 -0700961 int currentPage = mLauncher.getWorkspace().getNextPage();
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800962 // In case the workspace is scrolling, we need to use the final scroll to compute
963 // the folders bounds.
964 mLauncher.getWorkspace().setFinalScrollForPageChange(currentPage);
Adam Cohen35e7e642011-07-17 14:47:18 -0700965 // We first fetch the currently visible CellLayoutChildren
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800966 CellLayout currentLayout = (CellLayout) mLauncher.getWorkspace().getChildAt(currentPage);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700967 ShortcutAndWidgetContainer boundingLayout = currentLayout.getShortcutsAndWidgets();
Adam Cohen35e7e642011-07-17 14:47:18 -0700968 Rect bounds = new Rect();
969 parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800970 // We reset the workspaces scroll
971 mLauncher.getWorkspace().resetFinalScrollForPageChange(currentPage);
Adam Cohen2801caf2011-05-13 20:57:39 -0700972
Adam Cohen35e7e642011-07-17 14:47:18 -0700973 // We need to bound the folder to the currently visible CellLayoutChildren
974 int left = Math.min(Math.max(bounds.left, centeredLeft),
975 bounds.left + bounds.width() - width);
976 int top = Math.min(Math.max(bounds.top, centeredTop),
977 bounds.top + bounds.height() - height);
Sunny Goyalc6205602015-05-21 20:46:33 -0700978 if (grid.isPhone && (grid.availableWidthPx - width) < grid.iconSizePx) {
Winson Chungaf40f202013-09-18 18:26:31 -0700979 // Center the folder if it is full (on phones only)
980 left = (grid.availableWidthPx - width) / 2;
981 } else if (width >= bounds.width()) {
982 // If the folder doesn't fit within the bounds, center it about the desired bounds
Adam Cohen35e7e642011-07-17 14:47:18 -0700983 left = bounds.left + (bounds.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700984 }
Adam Cohen35e7e642011-07-17 14:47:18 -0700985 if (height >= bounds.height()) {
986 top = bounds.top + (bounds.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700987 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700988
989 int folderPivotX = width / 2 + (centeredLeft - left);
990 int folderPivotY = height / 2 + (centeredTop - top);
991 setPivotX(folderPivotX);
992 setPivotY(folderPivotY);
Adam Cohen268c4752012-06-06 17:47:33 -0700993 mFolderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700994 (1.0f * folderPivotX / width));
Adam Cohen268c4752012-06-06 17:47:33 -0700995 mFolderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700996 (1.0f * folderPivotY / height));
Adam Cohen3bf84d32012-05-07 20:17:14 -0700997
Adam Cohen662b5982011-12-13 17:45:21 -0800998 lp.width = width;
999 lp.height = height;
1000 lp.x = left;
1001 lp.y = top;
Adam Cohen2801caf2011-05-13 20:57:39 -07001002 }
1003
Adam Cohen268c4752012-06-06 17:47:33 -07001004 float getPivotXForIconAnimation() {
1005 return mFolderIconPivotX;
1006 }
1007 float getPivotYForIconAnimation() {
1008 return mFolderIconPivotY;
1009 }
1010
Winson Chung892c74d2013-08-22 16:15:50 -07001011 private int getContentAreaHeight() {
Adam Cohen2e6da152015-05-06 11:42:25 -07001012 DeviceProfile grid = mLauncher.getDeviceProfile();
Sunny Goyalc6205602015-05-21 20:46:33 -07001013 Rect workspacePadding = grid.getWorkspacePadding(mContent.mIsRtl);
Winson Chung892c74d2013-08-22 16:15:50 -07001014 int maxContentAreaHeight = grid.availableHeightPx -
Winson Chung892c74d2013-08-22 16:15:50 -07001015 workspacePadding.top - workspacePadding.bottom -
Sunny Goyal290800b2015-03-05 11:33:33 -08001016 mFooterHeight;
Adam Cohen1960ea42013-11-12 11:33:14 +00001017 int height = Math.min(maxContentAreaHeight,
Winson Chung892c74d2013-08-22 16:15:50 -07001018 mContent.getDesiredHeight());
Adam Cohen1960ea42013-11-12 11:33:14 +00001019 return Math.max(height, MIN_CONTENT_DIMEN);
1020 }
1021
1022 private int getContentAreaWidth() {
1023 return Math.max(mContent.getDesiredWidth(), MIN_CONTENT_DIMEN);
Winson Chung892c74d2013-08-22 16:15:50 -07001024 }
1025
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001026 private int getFolderHeight() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001027 return getFolderHeight(getContentAreaHeight());
1028 }
1029
1030 private int getFolderHeight(int contentAreaHeight) {
Sunny Goyal290800b2015-03-05 11:33:33 -08001031 return getPaddingTop() + getPaddingBottom() + contentAreaHeight + mFooterHeight;
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001032 }
Adam Cohen234c4cd2011-07-17 21:03:04 -07001033
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001034 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001035 int contentWidth = getContentAreaWidth();
1036 int contentHeight = getContentAreaHeight();
Adam Cohen1960ea42013-11-12 11:33:14 +00001037
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001038 int contentAreaWidthSpec = MeasureSpec.makeMeasureSpec(contentWidth, MeasureSpec.EXACTLY);
1039 int contentAreaHeightSpec = MeasureSpec.makeMeasureSpec(contentHeight, MeasureSpec.EXACTLY);
1040
1041 mContent.setFixedSize(contentWidth, contentHeight);
1042 mContentWrapper.measure(contentAreaWidthSpec, contentAreaHeightSpec);
Sunny Goyala07c2f52015-06-01 11:00:38 -07001043
1044 if (mContent.getChildCount() > 0) {
1045 int cellIconGap = (mContent.getPageAt(0).getCellWidth()
1046 - mLauncher.getDeviceProfile().iconSizePx) / 2;
1047 mFooter.setPadding(mContent.getPaddingLeft() + cellIconGap,
1048 mFooter.getPaddingTop(),
1049 mContent.getPaddingRight() + cellIconGap,
1050 mFooter.getPaddingBottom());
1051 }
Sunny Goyal290800b2015-03-05 11:33:33 -08001052 mFooter.measure(contentAreaWidthSpec,
1053 MeasureSpec.makeMeasureSpec(mFooterHeight, MeasureSpec.EXACTLY));
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001054
1055 int folderWidth = getPaddingLeft() + getPaddingRight() + contentWidth;
1056 int folderHeight = getFolderHeight(contentHeight);
1057 setMeasuredDimension(folderWidth, folderHeight);
Adam Cohen234c4cd2011-07-17 21:03:04 -07001058 }
1059
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001060 /**
1061 * Rearranges the children based on their rank.
1062 */
1063 public void rearrangeChildren() {
1064 rearrangeChildren(-1);
1065 }
Adam Cohen2801caf2011-05-13 20:57:39 -07001066
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001067 /**
1068 * Rearranges the children based on their rank.
1069 * @param itemCount if greater than the total children count, empty spaces are left at the end,
1070 * otherwise it is ignored.
1071 */
1072 public void rearrangeChildren(int itemCount) {
1073 ArrayList<View> views = getItemsInReadingOrder();
1074 mContent.arrangeChildren(views, Math.max(itemCount, views.size()));
Adam Cohen7c693212011-05-18 15:26:57 -07001075 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -07001076 }
1077
Sunny Goyalc4918352015-03-10 18:15:48 -07001078 // TODO remove this once GSA code fix is submitted
1079 public ViewGroup getContent() {
1080 return (ViewGroup) mContent;
1081 }
1082
Adam Cohena9cf38f2011-05-02 15:36:58 -07001083 public int getItemCount() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001084 return mContent.getItemCount();
Adam Cohena9cf38f2011-05-02 15:36:58 -07001085 }
1086
Adam Cohen091440a2015-03-18 14:16:05 -07001087 @Thunk void onCloseComplete() {
Adam Cohen05e0f402011-08-01 12:12:49 -07001088 DragLayer parent = (DragLayer) getParent();
Michael Jurka5649c282012-06-18 10:33:21 -07001089 if (parent != null) {
1090 parent.removeView(this);
1091 }
Adam Cohen4554ee12011-08-03 16:13:21 -07001092 mDragController.removeDropTarget((DropTarget) this);
Adam Cohen05e0f402011-08-01 12:12:49 -07001093 clearFocus();
Adam Cohenac56cff2011-09-28 20:45:37 -07001094 mFolderIcon.requestFocus();
Adam Cohen05e0f402011-08-01 12:12:49 -07001095
Adam Cohen2801caf2011-05-13 20:57:39 -07001096 if (mRearrangeOnClose) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001097 rearrangeChildren();
Adam Cohen2801caf2011-05-13 20:57:39 -07001098 mRearrangeOnClose = false;
1099 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001100 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001101 if (!mDragInProgress && !mSuppressFolderDeletion) {
1102 replaceFolderWithFinalItem();
1103 } else if (mDragInProgress) {
1104 mDeleteFolderOnDropCompleted = true;
1105 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001106 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001107 mSuppressFolderDeletion = false;
Sunny Goyal31abc292015-05-01 10:42:32 -07001108 clearDragInfo();
Adam Cohenafb01ee2011-06-23 15:38:03 -07001109 }
1110
Adam Cohen091440a2015-03-18 14:16:05 -07001111 @Thunk void replaceFolderWithFinalItem() {
Adam Cohenafb01ee2011-06-23 15:38:03 -07001112 // Add the last remaining child to the workspace in place of the folder
Adam Cohenfb91f302012-06-11 15:45:18 -07001113 Runnable onCompleteRunnable = new Runnable() {
1114 @Override
1115 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -07001116 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screenId);
Adam Cohenafb01ee2011-06-23 15:38:03 -07001117
Winson Chung33231f52013-12-09 16:57:45 -08001118 View child = null;
Adam Cohenfb91f302012-06-11 15:45:18 -07001119 // Move the item from the folder to the workspace, in the position of the folder
1120 if (getItemCount() == 1) {
1121 ShortcutInfo finalItem = mInfo.contents.get(0);
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001122 child = mLauncher.createShortcut(cellLayout, finalItem);
Adam Cohenfb91f302012-06-11 15:45:18 -07001123 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -07001124 mInfo.screenId, mInfo.cellX, mInfo.cellY);
Adam Cohenfb91f302012-06-11 15:45:18 -07001125 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001126 if (getItemCount() <= 1) {
1127 // Remove the folder
1128 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
Dan Sandler0eb687f2014-01-10 13:24:55 -05001129 if (cellLayout != null) {
1130 // b/12446428 -- sometimes the cell layout has already gone away?
1131 cellLayout.removeView(mFolderIcon);
1132 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001133 if (mFolderIcon instanceof DropTarget) {
1134 mDragController.removeDropTarget((DropTarget) mFolderIcon);
1135 }
1136 mLauncher.removeFolder(mInfo);
1137 }
Adam Cohenc5e63f32012-07-12 16:16:57 -07001138 // We add the child after removing the folder to prevent both from existing at
Winson Chung0e6a7132013-08-23 12:55:10 -07001139 // the same time in the CellLayout. We need to add the new item with addInScreenFromBind()
1140 // to ensure that hotseat items are placed correctly.
Adam Cohenc5e63f32012-07-12 16:16:57 -07001141 if (child != null) {
Winson Chung0e6a7132013-08-23 12:55:10 -07001142 mLauncher.getWorkspace().addInScreenFromBind(child, mInfo.container, mInfo.screenId,
Adam Cohenc5e63f32012-07-12 16:16:57 -07001143 mInfo.cellX, mInfo.cellY, mInfo.spanX, mInfo.spanY);
1144 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001145 }
1146 };
Sunny Goyalbc753352015-03-05 09:40:44 -08001147 View finalChild = mContent.getLastItem();
Adam Cohenfb91f302012-06-11 15:45:18 -07001148 if (finalChild != null) {
1149 mFolderIcon.performDestroyAnimation(finalChild, onCompleteRunnable);
Winson Chung33231f52013-12-09 16:57:45 -08001150 } else {
1151 onCompleteRunnable.run();
Adam Cohenafb01ee2011-06-23 15:38:03 -07001152 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001153 mDestroyed = true;
1154 }
1155
1156 boolean isDestroyed() {
1157 return mDestroyed;
Adam Cohen2801caf2011-05-13 20:57:39 -07001158 }
1159
Adam Cohenac56cff2011-09-28 20:45:37 -07001160 // This method keeps track of the last item in the folder for the purposes
1161 // of keyboard focus
Sunny Goyal290800b2015-03-05 11:33:33 -08001162 public void updateTextViewFocus() {
Sunny Goyalbc753352015-03-05 09:40:44 -08001163 View lastChild = mContent.getLastItem();
Adam Cohenac56cff2011-09-28 20:45:37 -07001164 if (lastChild != null) {
1165 mFolderName.setNextFocusDownId(lastChild.getId());
1166 mFolderName.setNextFocusRightId(lastChild.getId());
1167 mFolderName.setNextFocusLeftId(lastChild.getId());
1168 mFolderName.setNextFocusUpId(lastChild.getId());
1169 }
1170 }
1171
Adam Cohenbfbfd262011-06-13 16:55:12 -07001172 public void onDrop(DragObject d) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001173 Runnable cleanUpRunnable = null;
1174
Adam Cohen689ff162014-05-08 17:27:56 -07001175 // If we are coming from All Apps space, we defer removing the extra empty screen
1176 // until the folder closes
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001177 if (d.dragSource != mLauncher.getWorkspace() && !(d.dragSource instanceof Folder)) {
1178 cleanUpRunnable = new Runnable() {
1179 @Override
1180 public void run() {
Adam Cohen689ff162014-05-08 17:27:56 -07001181 mLauncher.exitSpringLoadedDragModeDelayed(true,
1182 Launcher.EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
1183 null);
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001184 }
1185 };
Adam Cohenbfbfd262011-06-13 16:55:12 -07001186 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001187
Sunny Goyalb8634152015-04-09 14:17:14 -07001188 // If the icon was dropped while the page was being scrolled, we need to compute
1189 // the target location again such that the icon is placed of the final page.
1190 if (!mContent.rankOnCurrentPage(mEmptyCellRank)) {
1191 // Reorder again.
1192 mTargetRank = getTargetRank(d, null);
Sunny Goyal48461932015-03-09 17:41:09 -07001193
Sunny Goyalb8634152015-04-09 14:17:14 -07001194 // Rearrange items immediately.
1195 mReorderAlarmListener.onAlarm(mReorderAlarm);
Sunny Goyal48461932015-03-09 17:41:09 -07001196
Sunny Goyalb8634152015-04-09 14:17:14 -07001197 mOnScrollHintAlarm.cancelAlarm();
1198 mScrollPauseAlarm.cancelAlarm();
Sunny Goyal48461932015-03-09 17:41:09 -07001199 }
Sunny Goyalb8634152015-04-09 14:17:14 -07001200 mContent.completePendingPageChanges();
Sunny Goyal48461932015-03-09 17:41:09 -07001201
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001202 View currentDragView;
1203 ShortcutInfo si = mCurrentDragInfo;
1204 if (mIsExternalDrag) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001205 currentDragView = mContent.createAndAddViewForRank(si, mEmptyCellRank);
Sunny Goyal95abbb32014-08-04 10:53:22 -07001206 // Actually move the item in the database if it was an external drag. Call this
1207 // before creating the view, so that ShortcutInfo is updated appropriately.
1208 LauncherModel.addOrMoveItemInDatabase(
1209 mLauncher, si, mInfo.id, 0, si.cellX, si.cellY);
1210
1211 // We only need to update the locations if it doesn't get handled in #onDropCompleted.
1212 if (d.dragSource != this) {
1213 updateItemLocationsInDatabaseBatch();
1214 }
1215 mIsExternalDrag = false;
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001216 } else {
1217 currentDragView = mCurrentDragView;
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001218 mContent.addViewForRank(currentDragView, si, mEmptyCellRank);
Adam Cohenbfbfd262011-06-13 16:55:12 -07001219 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001220
1221 if (d.dragView.hasDrawn()) {
1222
1223 // Temporarily reset the scale such that the animation target gets calculated correctly.
1224 float scaleX = getScaleX();
1225 float scaleY = getScaleY();
1226 setScaleX(1.0f);
1227 setScaleY(1.0f);
1228 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, currentDragView,
1229 cleanUpRunnable, null);
1230 setScaleX(scaleX);
1231 setScaleY(scaleY);
1232 } else {
1233 d.deferDragViewCleanupPostAnimation = false;
1234 currentDragView.setVisibility(VISIBLE);
1235 }
1236 mItemsInvalidated = true;
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001237 rearrangeChildren();
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001238
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001239 // Temporarily suppress the listener, as we did all the work already here.
1240 mSuppressOnAdd = true;
1241 mInfo.add(si);
1242 mSuppressOnAdd = false;
Sunny Goyal4b020172014-08-28 14:51:14 -07001243 // Clear the drag info, as it is no longer being dragged.
1244 mCurrentDragInfo = null;
Sunny Goyalf4066152015-04-15 09:42:19 -07001245 mDragInProgress = false;
Sunny Goyalb7e15ad2015-05-07 14:51:31 -07001246
1247 if (mContent.getPageCount() > 1) {
1248 // The animation has already been shown while opening the folder.
1249 mInfo.setOption(FolderInfo.FLAG_MULTI_PAGE_ANIMATION, true, mLauncher);
1250 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001251 }
1252
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001253 // This is used so the item doesn't immediately appear in the folder when added. In one case
1254 // we need to create the illusion that the item isn't added back to the folder yet, to
1255 // to correspond to the animation of the icon back into the folder. This is
1256 public void hideItem(ShortcutInfo info) {
1257 View v = getViewForInfo(info);
1258 v.setVisibility(INVISIBLE);
1259 }
1260 public void showItem(ShortcutInfo info) {
1261 View v = getViewForInfo(info);
1262 v.setVisibility(VISIBLE);
1263 }
1264
Sunny Goyalb7e15ad2015-05-07 14:51:31 -07001265 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -07001266 public void onAdd(ShortcutInfo item) {
Adam Cohen05e0f402011-08-01 12:12:49 -07001267 // If the item was dropped onto this open folder, we have done the work associated
1268 // with adding the item to the folder, as indicated by mSuppressOnAdd being set
Adam Cohenbfbfd262011-06-13 16:55:12 -07001269 if (mSuppressOnAdd) return;
Sunny Goyal5d85c442015-03-10 13:14:47 -07001270 mContent.createAndAddViewForRank(item, mContent.allocateRankForNewItem(item));
Sunny Goyal2e688a82015-03-18 10:23:39 -07001271 mItemsInvalidated = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001272 LauncherModel.addOrMoveItemInDatabase(
1273 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
1274 }
1275
Adam Cohena9cf38f2011-05-02 15:36:58 -07001276 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -07001277 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001278 // If this item is being dragged from this open folder, we have already handled
1279 // the work associated with removing the item, so we don't have to do anything here.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001280 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001281 View v = getViewForInfo(item);
Sunny Goyal290800b2015-03-05 11:33:33 -08001282 mContent.removeItem(v);
Adam Cohen2801caf2011-05-13 20:57:39 -07001283 if (mState == STATE_ANIMATING) {
1284 mRearrangeOnClose = true;
1285 } else {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001286 rearrangeChildren();
Adam Cohen2801caf2011-05-13 20:57:39 -07001287 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001288 if (getItemCount() <= 1) {
1289 replaceFolderWithFinalItem();
1290 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001291 }
Adam Cohen7c693212011-05-18 15:26:57 -07001292
Sunny Goyalbc753352015-03-05 09:40:44 -08001293 private View getViewForInfo(final ShortcutInfo item) {
1294 return mContent.iterateOverItems(new ItemOperator() {
1295
1296 @Override
1297 public boolean evaluate(ItemInfo info, View view, View parent) {
1298 return info == item;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001299 }
Sunny Goyalbc753352015-03-05 09:40:44 -08001300 });
Adam Cohendf1e4e82011-06-24 15:57:39 -07001301 }
1302
Adam Cohen76078c42011-06-09 15:06:52 -07001303 public void onItemsChanged() {
Adam Cohenac56cff2011-09-28 20:45:37 -07001304 updateTextViewFocus();
Adam Cohen76078c42011-06-09 15:06:52 -07001305 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001306
Adam Cohen76fc0852011-06-17 13:26:23 -07001307 public void onTitleChanged(CharSequence title) {
1308 }
Adam Cohen76078c42011-06-09 15:06:52 -07001309
Adam Cohen7c693212011-05-18 15:26:57 -07001310 public ArrayList<View> getItemsInReadingOrder() {
1311 if (mItemsInvalidated) {
1312 mItemsInReadingOrder.clear();
Sunny Goyalbc753352015-03-05 09:40:44 -08001313 mContent.iterateOverItems(new ItemOperator() {
1314
1315 @Override
1316 public boolean evaluate(ItemInfo info, View view, View parent) {
1317 mItemsInReadingOrder.add(view);
1318 return false;
Adam Cohen7c693212011-05-18 15:26:57 -07001319 }
Sunny Goyalbc753352015-03-05 09:40:44 -08001320 });
Adam Cohen7c693212011-05-18 15:26:57 -07001321 mItemsInvalidated = false;
1322 }
1323 return mItemsInReadingOrder;
1324 }
Adam Cohen8dfcba42011-07-07 16:38:18 -07001325
1326 public void getLocationInDragLayer(int[] loc) {
1327 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
1328 }
Adam Cohenea0818d2011-09-30 20:06:58 -07001329
1330 public void onFocusChange(View v, boolean hasFocus) {
1331 if (v == mFolderName && hasFocus) {
1332 startEditingFolderName();
1333 }
1334 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001335
1336 @Override
1337 public void getHitRectRelativeToDragLayer(Rect outRect) {
1338 getHitRect(outRect);
Sunny Goyal48461932015-03-09 17:41:09 -07001339 outRect.left -= mScrollAreaOffset;
1340 outRect.right += mScrollAreaOffset;
1341 }
1342
Winson Chung8f1eff72015-05-28 17:33:40 -07001343 @Override
1344 public void fillInLaunchSourceData(Bundle sourceData) {
1345 // Fill in from the folder icon's launch source provider first
1346 Stats.LaunchSourceUtils.populateSourceDataFromAncestorProvider(mFolderIcon, sourceData);
1347 sourceData.putString(Stats.SOURCE_EXTRA_SUB_CONTAINER, Stats.SUB_CONTAINER_FOLDER);
1348 sourceData.putInt(Stats.SOURCE_EXTRA_SUB_CONTAINER_PAGE, mContent.getCurrentPage());
1349 }
1350
Sunny Goyal48461932015-03-09 17:41:09 -07001351 private class OnScrollHintListener implements OnAlarmListener {
1352
1353 private final DragObject mDragObject;
1354
1355 OnScrollHintListener(DragObject object) {
1356 mDragObject = object;
1357 }
1358
1359 /**
1360 * Scroll hint has been shown long enough. Now scroll to appropriate page.
1361 */
1362 @Override
1363 public void onAlarm(Alarm alarm) {
1364 if (mCurrentScrollDir == DragController.SCROLL_LEFT) {
Sunny Goyalb8634152015-04-09 14:17:14 -07001365 mContent.scrollLeft();
Sunny Goyal48461932015-03-09 17:41:09 -07001366 mScrollHintDir = DragController.SCROLL_NONE;
1367 } else if (mCurrentScrollDir == DragController.SCROLL_RIGHT) {
Sunny Goyalb8634152015-04-09 14:17:14 -07001368 mContent.scrollRight();
Sunny Goyal48461932015-03-09 17:41:09 -07001369 mScrollHintDir = DragController.SCROLL_NONE;
1370 } else {
1371 // This should not happen
1372 return;
1373 }
1374 mCurrentScrollDir = DragController.SCROLL_NONE;
1375
1376 // Pause drag event until the scrolling is finished
1377 mScrollPauseAlarm.setOnAlarmListener(new OnScrollFinishedListener(mDragObject));
1378 mScrollPauseAlarm.setAlarm(DragController.RESCROLL_DELAY);
1379 }
1380 }
1381
1382 private class OnScrollFinishedListener implements OnAlarmListener {
1383
1384 private final DragObject mDragObject;
1385
1386 OnScrollFinishedListener(DragObject object) {
1387 mDragObject = object;
1388 }
1389
1390 /**
1391 * Page scroll is complete.
1392 */
1393 @Override
1394 public void onAlarm(Alarm alarm) {
1395 // Reorder immediately on page change.
1396 onDragOver(mDragObject, 1);
1397 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001398 }
Sunny Goyal1dd0f8b2015-07-06 13:04:02 -07001399
1400 // Compares item position based on rank and position giving priority to the rank.
1401 private static final Comparator<ItemInfo> ITEM_POS_COMPARATOR = new Comparator<ItemInfo>() {
1402
1403 @Override
1404 public int compare(ItemInfo lhs, ItemInfo rhs) {
1405 if (lhs.rank != rhs.rank) {
1406 return lhs.rank - rhs.rank;
1407 } else if (lhs.cellY != rhs.cellY) {
1408 return lhs.cellY - rhs.cellY;
1409 } else {
1410 return lhs.cellX - rhs.cellX;
1411 }
1412 }
1413 };
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001414}