blob: b1aba63c32bc16e18a67807944ae032d4ccd676d [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 Goyalc46bfef2015-01-05 12:40:08 -080024import android.annotation.TargetApi;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080025import android.content.Context;
Adam Cohen76fc0852011-06-17 13:26:23 -070026import android.content.res.Resources;
Winson Chungb745afb2015-03-02 11:51:23 -080027import android.graphics.Point;
Winson Chung043f2af2012-03-01 16:09:54 -080028import android.graphics.PointF;
Romain Guyfb5411e2010-02-24 10:04:17 -080029import android.graphics.Rect;
Sunny Goyalc46bfef2015-01-05 12:40:08 -080030import android.os.Build;
Adam Cohen7a14d0b2011-06-24 11:36:35 -070031import android.text.InputType;
Adam Cohene601a432011-07-26 21:51:30 -070032import android.text.Selection;
33import android.text.Spannable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.util.AttributeSet;
Adam Cohen3bf84d32012-05-07 20:17:14 -070035import android.util.Log;
Adam Cohen76fc0852011-06-17 13:26:23 -070036import android.view.ActionMode;
37import android.view.KeyEvent;
Adam Cohendf2cc412011-04-27 16:56:57 -070038import android.view.LayoutInflater;
Adam Cohen76fc0852011-06-17 13:26:23 -070039import android.view.Menu;
40import android.view.MenuItem;
Adam Cohen0c872ba2011-05-05 10:34:16 -070041import android.view.MotionEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.view.View;
Sunny Goyalc4918352015-03-10 18:15:48 -070043import android.view.ViewGroup;
Adam Cohen3371da02011-10-25 21:38:29 -070044import android.view.accessibility.AccessibilityEvent;
45import android.view.accessibility.AccessibilityManager;
Adam Cohenc4fe9ea2014-08-18 18:54:10 -070046import android.view.animation.AccelerateInterpolator;
Adam Cohen76fc0852011-06-17 13:26:23 -070047import android.view.inputmethod.EditorInfo;
48import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.widget.LinearLayout;
Adam Cohendf2cc412011-04-27 16:56:57 -070050import android.widget.TextView;
Sunny Goyal48461932015-03-09 17:41:09 -070051
Sunny Goyale9b651e2015-04-24 11:44:51 -070052import com.android.launcher3.CellLayout.CellInfo;
Sunny Goyalf4066152015-04-15 09:42:19 -070053import com.android.launcher3.DragController.DragListener;
Daniel Sandler325dc232013-06-05 22:57:57 -040054import com.android.launcher3.FolderInfo.FolderListener;
Sunny Goyale9b651e2015-04-24 11:44:51 -070055import com.android.launcher3.LauncherAccessibilityDelegate.AccessibilityDragSource;
Sunny Goyalfa401a12015-04-10 13:45:42 -070056import com.android.launcher3.UninstallDropTarget.UninstallSource;
Sunny Goyalbc753352015-03-05 09:40:44 -080057import com.android.launcher3.Workspace.ItemOperator;
Adam Cohen091440a2015-03-18 14:16:05 -070058import com.android.launcher3.util.Thunk;
Romain Guyedcce092010-03-04 13:03:17 -080059
Adam Cohenc0dcf592011-06-01 15:30:43 -070060import java.util.ArrayList;
Adam Cohen3bf84d32012-05-07 20:17:14 -070061import java.util.Collections;
Adam Cohenc0dcf592011-06-01 15:30:43 -070062
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063/**
64 * Represents a set of icons chosen by the user or generated by the system.
65 */
Adam Cohen8dfcba42011-07-07 16:38:18 -070066public class Folder extends LinearLayout implements DragSource, View.OnClickListener,
Adam Cohenea0818d2011-09-30 20:06:58 -070067 View.OnLongClickListener, DropTarget, FolderListener, TextView.OnEditorActionListener,
Sunny Goyale9b651e2015-04-24 11:44:51 -070068 View.OnFocusChangeListener, DragListener, UninstallSource, AccessibilityDragSource {
Adam Cohendf2cc412011-04-27 16:56:57 -070069 private static final String TAG = "Launcher.Folder";
70
Sunny Goyalc3a609f2015-02-26 17:43:50 -080071 /**
72 * We avoid measuring {@link #mContentWrapper} with a 0 width or height, as this
73 * results in CellLayout being measured as UNSPECIFIED, which it does not support.
74 */
75 private static final int MIN_CONTENT_DIMEN = 5;
Adam Cohen4eac29a2011-07-11 17:53:37 -070076
Adam Cohendf2cc412011-04-27 16:56:57 -070077 static final int STATE_NONE = -1;
78 static final int STATE_SMALL = 0;
79 static final int STATE_ANIMATING = 1;
80 static final int STATE_OPEN = 2;
81
Sunny Goyal48461932015-03-09 17:41:09 -070082 /**
Sunny Goyal48461932015-03-09 17:41:09 -070083 * Time for which the scroll hint is shown before automatically changing page.
84 */
85 public static final int SCROLL_HINT_DURATION = DragController.SCROLL_DELAY;
86
87 /**
88 * Fraction of icon width which behave as scroll region.
89 */
90 private static final float ICON_OVERSCROLL_WIDTH_FACTOR = 0.45f;
91
Adam Cohenf0f4eda2013-06-06 21:27:03 -070092 private static final int REORDER_DELAY = 250;
Adam Cohen5d518fa2013-12-05 14:16:23 -080093 private static final int ON_EXIT_CLOSE_DELAY = 400;
Sunny Goyalc3a609f2015-02-26 17:43:50 -080094 private static final Rect sTempRect = new Rect();
95
96 private static String sDefaultFolderName;
97 private static String sHintText;
98
99 private final Alarm mReorderAlarm = new Alarm();
100 private final Alarm mOnExitAlarm = new Alarm();
Sunny Goyalb8634152015-04-09 14:17:14 -0700101 private final Alarm mOnScrollHintAlarm = new Alarm();
102 @Thunk final Alarm mScrollPauseAlarm = new Alarm();
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800103
Adam Cohen091440a2015-03-18 14:16:05 -0700104 @Thunk final ArrayList<View> mItemsInReadingOrder = new ArrayList<View>();
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800105
106 private final int mExpandDuration;
107 private final int mMaterialExpandDuration;
108 private final int mMaterialExpandStagger;
109
110 private final InputMethodManager mInputMethodManager;
111
112 protected final Launcher mLauncher;
113 protected DragController mDragController;
114 protected FolderInfo mInfo;
115
Adam Cohen091440a2015-03-18 14:16:05 -0700116 @Thunk FolderIcon mFolderIcon;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800117
Sunny Goyalb8634152015-04-09 14:17:14 -0700118 @Thunk FolderPagedView mContent;
Adam Cohen091440a2015-03-18 14:16:05 -0700119 @Thunk View mContentWrapper;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800120 FolderEditText mFolderName;
121
Sunny Goyal290800b2015-03-05 11:33:33 -0800122 private View mFooter;
123 private int mFooterHeight;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800124
125 // Cell ranks used for drag and drop
Adam Cohen091440a2015-03-18 14:16:05 -0700126 @Thunk int mTargetRank, mPrevTargetRank, mEmptyCellRank;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800127
Adam Cohen091440a2015-03-18 14:16:05 -0700128 @Thunk int mState = STATE_NONE;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800129 private boolean mRearrangeOnClose = false;
Adam Cohen7c693212011-05-18 15:26:57 -0700130 boolean mItemsInvalidated = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700131 private ShortcutInfo mCurrentDragInfo;
132 private View mCurrentDragView;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800133 private boolean mIsExternalDrag;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700134 boolean mSuppressOnAdd = false;
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700135 private boolean mDragInProgress = false;
136 private boolean mDeleteFolderOnDropCompleted = false;
137 private boolean mSuppressFolderDeletion = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700138 private boolean mItemAddedBackToSelfViaIcon = false;
Adam Cohen091440a2015-03-18 14:16:05 -0700139 @Thunk float mFolderIconPivotX;
140 @Thunk float mFolderIconPivotY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700141 private boolean mIsEditingName = false;
Adam Cohen1960ea42013-11-12 11:33:14 +0000142
Adam Cohenfb91f302012-06-11 15:45:18 -0700143 private boolean mDestroyed;
144
Adam Cohen091440a2015-03-18 14:16:05 -0700145 @Thunk Runnable mDeferredAction;
Michael Jurka1e2f4652013-07-08 18:03:46 -0700146 private boolean mDeferDropAfterUninstall;
147 private boolean mUninstallSuccessful;
148
Sunny Goyal48461932015-03-09 17:41:09 -0700149 // Folder scrolling
150 private int mScrollAreaOffset;
Sunny Goyal48461932015-03-09 17:41:09 -0700151
Adam Cohen091440a2015-03-18 14:16:05 -0700152 @Thunk int mScrollHintDir = DragController.SCROLL_NONE;
153 @Thunk int mCurrentScrollDir = DragController.SCROLL_NONE;
Sunny Goyal48461932015-03-09 17:41:09 -0700154
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155 /**
156 * Used to inflate the Workspace from XML.
157 *
158 * @param context The application's context.
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800159 * @param attrs The attributes set containing the Workspace's customization values.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160 */
161 public Folder(Context context, AttributeSet attrs) {
162 super(context, attrs);
163 setAlwaysDrawnWithCacheEnabled(false);
Adam Cohen76fc0852011-06-17 13:26:23 -0700164 mInputMethodManager = (InputMethodManager)
Michael Jurka8b805b12012-04-18 14:23:14 -0700165 getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
Adam Cohen76fc0852011-06-17 13:26:23 -0700166
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800167 Resources res = getResources();
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700168 mExpandDuration = res.getInteger(R.integer.config_folderExpandDuration);
169 mMaterialExpandDuration = res.getInteger(R.integer.config_materialFolderExpandDuration);
170 mMaterialExpandStagger = res.getInteger(R.integer.config_materialFolderExpandStagger);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700171
172 if (sDefaultFolderName == null) {
173 sDefaultFolderName = res.getString(R.string.folder_name);
174 }
Adam Cohena65beee2011-06-27 21:32:23 -0700175 if (sHintText == null) {
176 sHintText = res.getString(R.string.folder_hint_text);
177 }
Adam Cohen4eac29a2011-07-11 17:53:37 -0700178 mLauncher = (Launcher) context;
Adam Cohenac56cff2011-09-28 20:45:37 -0700179 // We need this view to be focusable in touch mode so that when text editing of the folder
180 // name is complete, we have something to focus on, thus hiding the cursor and giving
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800181 // reliable behavior when clicking the text field (since it will always gain focus on click).
Adam Cohenac56cff2011-09-28 20:45:37 -0700182 setFocusableInTouchMode(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 }
184
185 @Override
186 protected void onFinishInflate() {
187 super.onFinishInflate();
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800188 mContentWrapper = findViewById(R.id.folder_content_wrapper);
Sunny Goyalb8634152015-04-09 14:17:14 -0700189 mContent = (FolderPagedView) findViewById(R.id.folder_content);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800190 mContent.setFolder(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700191
Adam Cohenac56cff2011-09-28 20:45:37 -0700192 mFolderName = (FolderEditText) findViewById(R.id.folder_name);
193 mFolderName.setFolder(this);
Adam Cohenea0818d2011-09-30 20:06:58 -0700194 mFolderName.setOnFocusChangeListener(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700195
Adam Cohen76fc0852011-06-17 13:26:23 -0700196 // We disable action mode for now since it messes up the view on phones
197 mFolderName.setCustomSelectionActionModeCallback(mActionModeCallback);
Adam Cohen76fc0852011-06-17 13:26:23 -0700198 mFolderName.setOnEditorActionListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700199 mFolderName.setSelectAllOnFocus(true);
Adam Cohen7a14d0b2011-06-24 11:36:35 -0700200 mFolderName.setInputType(mFolderName.getInputType() |
201 InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800202
Sunny Goyalb8634152015-04-09 14:17:14 -0700203 mFooter = findViewById(R.id.folder_footer);
204 updateFooterHeight();
205 }
206
207 public void updateFooterHeight() {
Sunny Goyal290800b2015-03-05 11:33:33 -0800208 // We find out how tall footer wants to be (it is set to wrap_content), so that
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800209 // we can allocate the appropriate amount of space for it.
210 int measureSpec = MeasureSpec.UNSPECIFIED;
Sunny Goyal290800b2015-03-05 11:33:33 -0800211 mFooter.measure(measureSpec, measureSpec);
212 mFooterHeight = mFooter.getMeasuredHeight();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800213 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700214
Adam Cohen76fc0852011-06-17 13:26:23 -0700215 private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
216 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
217 return false;
218 }
219
220 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
221 return false;
222 }
223
224 public void onDestroyActionMode(ActionMode mode) {
225 }
226
227 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
228 return false;
229 }
230 };
231
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800232 public void onClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700233 Object tag = v.getTag();
234 if (tag instanceof ShortcutInfo) {
Adam Cohenb5fe60c2013-06-06 22:03:51 -0700235 mLauncher.onClick(v);
Adam Cohendf2cc412011-04-27 16:56:57 -0700236 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800237 }
238
239 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -0700240 // Return if global dragging is not enabled
241 if (!mLauncher.isDraggingEnabled()) return true;
Sunny Goyale9b651e2015-04-24 11:44:51 -0700242 return beginDrag(v, false);
243 }
Winson Chung36a62fe2012-05-06 18:04:42 -0700244
Sunny Goyale9b651e2015-04-24 11:44:51 -0700245 private boolean beginDrag(View v, boolean accessible) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700246 Object tag = v.getTag();
247 if (tag instanceof ShortcutInfo) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700248 ShortcutInfo item = (ShortcutInfo) tag;
249 if (!v.isInTouchMode()) {
250 return false;
251 }
252
Sunny Goyale9b651e2015-04-24 11:44:51 -0700253 mLauncher.getWorkspace().beginDragShared(v, new Point(), this, accessible);
Adam Cohen76078c42011-06-09 15:06:52 -0700254
255 mCurrentDragInfo = item;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800256 mEmptyCellRank = item.rank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700257 mCurrentDragView = v;
Adam Cohenfc53cd22011-07-20 15:45:11 -0700258
Sunny Goyal290800b2015-03-05 11:33:33 -0800259 mContent.removeItem(mCurrentDragView);
Adam Cohenfc53cd22011-07-20 15:45:11 -0700260 mInfo.remove(mCurrentDragInfo);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700261 mDragInProgress = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700262 mItemAddedBackToSelfViaIcon = false;
Adam Cohendf2cc412011-04-27 16:56:57 -0700263 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800264 return true;
265 }
266
Sunny Goyale9b651e2015-04-24 11:44:51 -0700267 @Override
268 public void startDrag(CellInfo cellInfo, boolean accessible) {
269 beginDrag(cellInfo.cell, accessible);
270 }
271
272 @Override
273 public void enableAccessibleDrag(boolean enable) {
274 mLauncher.getSearchBar().enableAccessibleDrag(enable);
275 for (int i = 0; i < mContent.getChildCount(); i++) {
276 mContent.getPageAt(i).enableAccessibleDrag(enable, CellLayout.FOLDER_ACCESSIBILITY_DRAG);
277 }
Sunny Goyalccc414b2015-04-30 12:28:16 -0700278
279 mFooter.setImportantForAccessibility(enable ? IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS :
280 IMPORTANT_FOR_ACCESSIBILITY_AUTO);
Sunny Goyale9b651e2015-04-24 11:44:51 -0700281 mLauncher.getWorkspace().setAddNewPageOnDrag(!enable);
282 }
283
Adam Cohen76fc0852011-06-17 13:26:23 -0700284 public boolean isEditingName() {
285 return mIsEditingName;
286 }
287
288 public void startEditingFolderName() {
Adam Cohena65beee2011-06-27 21:32:23 -0700289 mFolderName.setHint("");
Adam Cohen76fc0852011-06-17 13:26:23 -0700290 mIsEditingName = true;
291 }
292
293 public void dismissEditingName() {
294 mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
295 doneEditingFolderName(true);
296 }
297
298 public void doneEditingFolderName(boolean commit) {
Adam Cohena65beee2011-06-27 21:32:23 -0700299 mFolderName.setHint(sHintText);
Adam Cohen1df26a32011-08-12 16:15:23 -0700300 // Convert to a string here to ensure that no other state associated with the text field
301 // gets saved.
Adam Cohen3371da02011-10-25 21:38:29 -0700302 String newTitle = mFolderName.getText().toString();
303 mInfo.setTitle(newTitle);
Adam Cohen76fc0852011-06-17 13:26:23 -0700304 LauncherModel.updateItemInDatabase(mLauncher, mInfo);
Adam Cohenac56cff2011-09-28 20:45:37 -0700305
Adam Cohen3371da02011-10-25 21:38:29 -0700306 if (commit) {
307 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700308 String.format(getContext().getString(R.string.folder_renamed), newTitle));
Adam Cohen3371da02011-10-25 21:38:29 -0700309 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700310 // In order to clear the focus from the text field, we set the focus on ourself. This
311 // ensures that every time the field is clicked, focus is gained, giving reliable behavior.
312 requestFocus();
313
Adam Cohene601a432011-07-26 21:51:30 -0700314 Selection.setSelection((Spannable) mFolderName.getText(), 0, 0);
Adam Cohen76fc0852011-06-17 13:26:23 -0700315 mIsEditingName = false;
316 }
317
318 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
319 if (actionId == EditorInfo.IME_ACTION_DONE) {
320 dismissEditingName();
321 return true;
322 }
323 return false;
324 }
325
326 public View getEditTextRegion() {
327 return mFolderName;
328 }
329
Adam Cohen0c872ba2011-05-05 10:34:16 -0700330 /**
331 * We need to handle touch events to prevent them from falling through to the workspace below.
332 */
333 @Override
334 public boolean onTouchEvent(MotionEvent ev) {
335 return true;
336 }
337
Joe Onorato00acb122009-08-04 16:04:30 -0400338 public void setDragController(DragController dragController) {
339 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800340 }
341
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800342 public void setFolderIcon(FolderIcon icon) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700343 mFolderIcon = icon;
344 }
345
Adam Cohen3371da02011-10-25 21:38:29 -0700346 @Override
347 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
348 // When the folder gets focus, we don't want to announce the list of items.
349 return true;
350 }
351
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800352 /**
353 * @return the FolderInfo object associated with this folder
354 */
355 FolderInfo getInfo() {
356 return mInfo;
357 }
358
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800359 void bind(FolderInfo info) {
360 mInfo = info;
Adam Cohendf2cc412011-04-27 16:56:57 -0700361 ArrayList<ShortcutInfo> children = info.contents;
Sunny Goyal08f72612015-01-05 13:41:43 -0800362 Collections.sort(children, Utilities.RANK_COMPARATOR);
Sunny Goyal08f72612015-01-05 13:41:43 -0800363
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800364 ArrayList<ShortcutInfo> overflow = mContent.bindItems(children);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700365
Jason Monk4ff73882014-04-24 16:48:07 -0400366 // If our folder has too many items we prune them from the list. This is an issue
Adam Cohenc508b2d2011-06-28 14:41:44 -0700367 // when upgrading from the old Folders implementation which could contain an unlimited
368 // number of items.
369 for (ShortcutInfo item: overflow) {
370 mInfo.remove(item);
371 LauncherModel.deleteItemFromDatabase(mLauncher, item);
372 }
373
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800374 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
375 if (lp == null) {
376 lp = new DragLayer.LayoutParams(0, 0);
377 lp.customPosition = true;
378 setLayoutParams(lp);
379 }
380 centerAboutIcon();
381
Adam Cohen4dbe6d92011-05-18 17:14:15 -0700382 mItemsInvalidated = true;
Adam Cohenac56cff2011-09-28 20:45:37 -0700383 updateTextViewFocus();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700384 mInfo.addListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700385
Adam Cohenafb01ee2011-06-23 15:38:03 -0700386 if (!sDefaultFolderName.contentEquals(mInfo.title)) {
Adam Cohen4ef610f2011-06-21 22:37:36 -0700387 mFolderName.setText(mInfo.title);
388 } else {
389 mFolderName.setText("");
390 }
Winson Chung33231f52013-12-09 16:57:45 -0800391
392 // In case any children didn't come across during loading, clean up the folder accordingly
393 mFolderIcon.post(new Runnable() {
394 public void run() {
395 if (getItemCount() <= 1) {
396 replaceFolderWithFinalItem();
397 }
398 }
399 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700400 }
401
402 /**
403 * Creates a new UserFolder, inflated from R.layout.user_folder.
404 *
405 * @param context The application's context.
406 *
407 * @return A new UserFolder.
408 */
409 static Folder fromXml(Context context) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700410 return (Folder) LayoutInflater.from(context).inflate(R.layout.user_folder, null);
Adam Cohendf2cc412011-04-27 16:56:57 -0700411 }
412
413 /**
414 * This method is intended to make the UserFolder to be visually identical in size and position
415 * to its associated FolderIcon. This allows for a seamless transition into the expanded state.
416 */
417 private void positionAndSizeAsIcon() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700418 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800419 setScaleX(0.8f);
420 setScaleY(0.8f);
421 setAlpha(0f);
Adam Cohendf2cc412011-04-27 16:56:57 -0700422 mState = STATE_SMALL;
423 }
424
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700425 private void prepareReveal() {
426 setScaleX(1f);
427 setScaleY(1f);
428 setAlpha(1f);
429 mState = STATE_SMALL;
430 }
431
Adam Cohendf2cc412011-04-27 16:56:57 -0700432 public void animateOpen() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700433 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen6441de02011-12-14 14:25:32 -0800434
Sunny Goyalb8634152015-04-09 14:17:14 -0700435 mContent.completePendingPageChanges();
Sunny Goyal8167dc22015-04-27 13:44:01 -0700436 if (!mDragInProgress) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700437 // Open on the first page.
438 mContent.snapToPageImmediately(0);
Sunny Goyal48461932015-03-09 17:41:09 -0700439 }
440
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700441 Animator openFolderAnim = null;
442 final Runnable onCompleteRunnable;
Kenny Guyd794a3f2014-09-16 15:17:58 +0100443 if (!Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700444 positionAndSizeAsIcon();
445 centerAboutIcon();
446
447 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1);
448 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
449 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
450 final ObjectAnimator oa =
451 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
452 oa.setDuration(mExpandDuration);
453 openFolderAnim = oa;
454
455 setLayerType(LAYER_TYPE_HARDWARE, null);
456 onCompleteRunnable = new Runnable() {
457 @Override
458 public void run() {
459 setLayerType(LAYER_TYPE_NONE, null);
460 }
461 };
462 } else {
463 prepareReveal();
464 centerAboutIcon();
465
466 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
467 int height = getFolderHeight();
468
469 float transX = - 0.075f * (width / 2 - getPivotX());
470 float transY = - 0.075f * (height / 2 - getPivotY());
471 setTranslationX(transX);
472 setTranslationY(transY);
473 PropertyValuesHolder tx = PropertyValuesHolder.ofFloat("translationX", transX, 0);
474 PropertyValuesHolder ty = PropertyValuesHolder.ofFloat("translationY", transY, 0);
475
476 int rx = (int) Math.max(Math.max(width - getPivotX(), 0), getPivotX());
477 int ry = (int) Math.max(Math.max(height - getPivotY(), 0), getPivotY());
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700478 float radius = (float) Math.hypot(rx, ry);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700479 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
480 Animator reveal = LauncherAnimUtils.createCircularReveal(this, (int) getPivotX(),
481 (int) getPivotY(), 0, radius);
482 reveal.setDuration(mMaterialExpandDuration);
483 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
484
Sunny Goyalbc753352015-03-05 09:40:44 -0800485 mContentWrapper.setAlpha(0f);
486 Animator iconsAlpha = LauncherAnimUtils.ofFloat(mContentWrapper, "alpha", 0f, 1f);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700487 iconsAlpha.setDuration(mMaterialExpandDuration);
488 iconsAlpha.setStartDelay(mMaterialExpandStagger);
489 iconsAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
490
Sunny Goyal290800b2015-03-05 11:33:33 -0800491 mFooter.setAlpha(0f);
492 Animator textAlpha = LauncherAnimUtils.ofFloat(mFooter, "alpha", 0f, 1f);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700493 textAlpha.setDuration(mMaterialExpandDuration);
494 textAlpha.setStartDelay(mMaterialExpandStagger);
495 textAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
496
497 Animator drift = LauncherAnimUtils.ofPropertyValuesHolder(this, tx, ty);
498 drift.setDuration(mMaterialExpandDuration);
499 drift.setStartDelay(mMaterialExpandStagger);
500 drift.setInterpolator(new LogDecelerateInterpolator(60, 0));
501
502 anim.play(drift);
503 anim.play(iconsAlpha);
504 anim.play(textAlpha);
505 anim.play(reveal);
506
507 openFolderAnim = anim;
508
Sunny Goyalbc753352015-03-05 09:40:44 -0800509 mContentWrapper.setLayerType(LAYER_TYPE_HARDWARE, null);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700510 onCompleteRunnable = new Runnable() {
511 @Override
512 public void run() {
Sunny Goyalbc753352015-03-05 09:40:44 -0800513 mContentWrapper.setLayerType(LAYER_TYPE_NONE, null);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700514 }
515 };
516 }
517 openFolderAnim.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700518 @Override
519 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700520 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Sunny Goyalbc753352015-03-05 09:40:44 -0800521 mContent.getAccessibilityDescription());
Adam Cohendf2cc412011-04-27 16:56:57 -0700522 mState = STATE_ANIMATING;
523 }
524 @Override
525 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700526 mState = STATE_OPEN;
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700527
528 if (onCompleteRunnable != null) {
529 onCompleteRunnable.run();
530 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800531
Sunny Goyalbc753352015-03-05 09:40:44 -0800532 mContent.setFocusOnFirstChild();
Adam Cohendf2cc412011-04-27 16:56:57 -0700533 }
534 });
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700535 openFolderAnim.start();
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800536
537 // Make sure the folder picks up the last drag move even if the finger doesn't move.
538 if (mDragController.isDragging()) {
539 mDragController.forceTouchMove();
540 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700541
Sunny Goyalb8634152015-04-09 14:17:14 -0700542 FolderPagedView pages = (FolderPagedView) mContent;
543 pages.verifyVisibleHighResIcons(pages.getNextPage());
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800544 }
545
546 public void beginExternalDrag(ShortcutInfo item) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800547 mCurrentDragInfo = item;
Sunny Goyal5d85c442015-03-10 13:14:47 -0700548 mEmptyCellRank = mContent.allocateRankForNewItem(item);
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800549 mIsExternalDrag = true;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800550 mDragInProgress = true;
Sunny Goyalb8634152015-04-09 14:17:14 -0700551
Sunny Goyalf4066152015-04-15 09:42:19 -0700552 // Since this folder opened by another controller, it might not get onDrop or
553 // onDropComplete. Perform cleanup once drag-n-drop ends.
554 mDragController.addDragListener(this);
555 }
556
557 @Override
558 public void onDragStart(DragSource source, Object info, int dragAction) { }
559
560 @Override
561 public void onDragEnd() {
562 if (mIsExternalDrag && mDragInProgress) {
563 completeDragExit();
564 }
565 mDragController.removeDragListener(this);
Adam Cohendf2cc412011-04-27 16:56:57 -0700566 }
567
Adam Cohen091440a2015-03-18 14:16:05 -0700568 @Thunk void sendCustomAccessibilityEvent(int type, String text) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700569 AccessibilityManager accessibilityManager = (AccessibilityManager)
570 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
571 if (accessibilityManager.isEnabled()) {
Adam Cohen3371da02011-10-25 21:38:29 -0700572 AccessibilityEvent event = AccessibilityEvent.obtain(type);
573 onInitializeAccessibilityEvent(event);
574 event.getText().add(text);
Michael Jurka8b805b12012-04-18 14:23:14 -0700575 accessibilityManager.sendAccessibilityEvent(event);
Adam Cohen3371da02011-10-25 21:38:29 -0700576 }
577 }
578
Adam Cohendf2cc412011-04-27 16:56:57 -0700579 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700580 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800581 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
582 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
583 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
Michael Jurka032e6ba2013-04-22 15:08:42 +0200584 final ObjectAnimator oa =
Michael Jurka2ecf9952012-06-18 12:52:28 -0700585 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohendf2cc412011-04-27 16:56:57 -0700586
Adam Cohen2801caf2011-05-13 20:57:39 -0700587 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700588 @Override
589 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700590 onCloseComplete();
Michael Jurka0121c3e2012-05-31 08:36:04 -0700591 setLayerType(LAYER_TYPE_NONE, null);
Adam Cohen2801caf2011-05-13 20:57:39 -0700592 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700593 }
594 @Override
595 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700596 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700597 getContext().getString(R.string.folder_closed));
Adam Cohendf2cc412011-04-27 16:56:57 -0700598 mState = STATE_ANIMATING;
599 }
600 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700601 oa.setDuration(mExpandDuration);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700602 setLayerType(LAYER_TYPE_HARDWARE, null);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100603 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700604 }
605
Adam Cohencb3382b2011-05-24 14:07:08 -0700606 public boolean acceptDrop(DragObject d) {
607 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700608 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700609 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
610 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
611 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700612 }
613
Adam Cohencb3382b2011-05-24 14:07:08 -0700614 public void onDragEnter(DragObject d) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800615 mPrevTargetRank = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700616 mOnExitAlarm.cancelAlarm();
Sunny Goyalb8634152015-04-09 14:17:14 -0700617 // Get the area offset such that the folder only closes if half the drag icon width
618 // is outside the folder area
619 mScrollAreaOffset = d.dragView.getDragRegionWidth() / 2 - d.xOffset;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700620 }
621
622 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
623 public void onAlarm(Alarm alarm) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800624 mContent.realTimeReorder(mEmptyCellRank, mTargetRank);
625 mEmptyCellRank = mTargetRank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700626 }
627 };
628
Sunny Goyalc46bfef2015-01-05 12:40:08 -0800629 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohen2374abf2013-04-16 14:56:57 -0700630 public boolean isLayoutRtl() {
631 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
632 }
633
Sunny Goyal48461932015-03-09 17:41:09 -0700634 @Override
Adam Cohencb3382b2011-05-24 14:07:08 -0700635 public void onDragOver(DragObject d) {
Sunny Goyal48461932015-03-09 17:41:09 -0700636 onDragOver(d, REORDER_DELAY);
637 }
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700638
Sunny Goyal48461932015-03-09 17:41:09 -0700639 private int getTargetRank(DragObject d, float[] recycle) {
640 recycle = d.getVisualCenter(recycle);
641 return mContent.findNearestArea(
642 (int) recycle[0] - getPaddingLeft(), (int) recycle[1] - getPaddingTop());
643 }
644
Adam Cohen091440a2015-03-18 14:16:05 -0700645 @Thunk void onDragOver(DragObject d, int reorderDelay) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700646 if (mScrollPauseAlarm.alarmPending()) {
Sunny Goyal48461932015-03-09 17:41:09 -0700647 return;
648 }
649 final float[] r = new float[2];
650 mTargetRank = getTargetRank(d, r);
651
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800652 if (mTargetRank != mPrevTargetRank) {
Alan Viverette4cda5b72013-08-28 17:53:41 -0700653 mReorderAlarm.cancelAlarm();
Sunny Goyalc46bfef2015-01-05 12:40:08 -0800654 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
655 mReorderAlarm.setAlarm(REORDER_DELAY);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800656 mPrevTargetRank = mTargetRank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700657 }
Sunny Goyal48461932015-03-09 17:41:09 -0700658
Sunny Goyal48461932015-03-09 17:41:09 -0700659 float x = r[0];
Sunny Goyalb8634152015-04-09 14:17:14 -0700660 int currentPage = mContent.getNextPage();
Sunny Goyal48461932015-03-09 17:41:09 -0700661
Sunny Goyalb8634152015-04-09 14:17:14 -0700662 float cellOverlap = mContent.getCurrentCellLayout().getCellWidth()
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700663 * ICON_OVERSCROLL_WIDTH_FACTOR;
664 boolean isOutsideLeftEdge = x < cellOverlap;
665 boolean isOutsideRightEdge = x > (getWidth() - cellOverlap);
Sunny Goyal48461932015-03-09 17:41:09 -0700666
Sunny Goyalb8634152015-04-09 14:17:14 -0700667 if (currentPage > 0 && (mContent.rtlLayout ? isOutsideRightEdge : isOutsideLeftEdge)) {
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700668 showScrollHint(DragController.SCROLL_LEFT, d);
Sunny Goyalb8634152015-04-09 14:17:14 -0700669 } else if (currentPage < (mContent.getPageCount() - 1)
670 && (mContent.rtlLayout ? isOutsideLeftEdge : isOutsideRightEdge)) {
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700671 showScrollHint(DragController.SCROLL_RIGHT, d);
Sunny Goyal48461932015-03-09 17:41:09 -0700672 } else {
673 mOnScrollHintAlarm.cancelAlarm();
674 if (mScrollHintDir != DragController.SCROLL_NONE) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700675 mContent.clearScrollHint();
Sunny Goyal48461932015-03-09 17:41:09 -0700676 mScrollHintDir = DragController.SCROLL_NONE;
677 }
678 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700679 }
680
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700681 private void showScrollHint(int direction, DragObject d) {
682 // Show scroll hint on the right
683 if (mScrollHintDir != direction) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700684 mContent.showScrollHint(direction);
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700685 mScrollHintDir = direction;
686 }
687
688 // Set alarm for when the hint is complete
689 if (!mOnScrollHintAlarm.alarmPending() || mCurrentScrollDir != direction) {
690 mCurrentScrollDir = direction;
691 mOnScrollHintAlarm.cancelAlarm();
692 mOnScrollHintAlarm.setOnAlarmListener(new OnScrollHintListener(d));
693 mOnScrollHintAlarm.setAlarm(SCROLL_HINT_DURATION);
694
695 mReorderAlarm.cancelAlarm();
696 mTargetRank = mEmptyCellRank;
697 }
698 }
699
Adam Cohenbfbfd262011-06-13 16:55:12 -0700700 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
701 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700702 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700703 }
704 };
705
Adam Cohen95bb8002011-07-03 23:40:28 -0700706 public void completeDragExit() {
Sunny Goyalf4066152015-04-15 09:42:19 -0700707 if (mInfo.opened) {
708 mLauncher.closeFolder();
709 mRearrangeOnClose = true;
Sunny Goyal31abc292015-05-01 10:42:32 -0700710 } else if (mState == STATE_ANIMATING) {
711 mRearrangeOnClose = true;
Sunny Goyalf4066152015-04-15 09:42:19 -0700712 } else {
713 rearrangeChildren();
Sunny Goyal31abc292015-05-01 10:42:32 -0700714 clearDragInfo();
Sunny Goyalf4066152015-04-15 09:42:19 -0700715 }
Sunny Goyal31abc292015-05-01 10:42:32 -0700716 }
717
718 private void clearDragInfo() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700719 mCurrentDragInfo = null;
720 mCurrentDragView = null;
721 mSuppressOnAdd = false;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800722 mIsExternalDrag = false;
Adam Cohen3e8f8112011-07-02 18:03:00 -0700723 }
724
Adam Cohencb3382b2011-05-24 14:07:08 -0700725 public void onDragExit(DragObject d) {
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700726 // We only close the folder if this is a true drag exit, ie. not because
727 // a drop has occurred above the folder.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700728 if (!d.dragComplete) {
729 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
730 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
731 }
732 mReorderAlarm.cancelAlarm();
Sunny Goyal48461932015-03-09 17:41:09 -0700733
Sunny Goyalb8634152015-04-09 14:17:14 -0700734 mOnScrollHintAlarm.cancelAlarm();
735 mScrollPauseAlarm.cancelAlarm();
736 if (mScrollHintDir != DragController.SCROLL_NONE) {
737 mContent.clearScrollHint();
738 mScrollHintDir = DragController.SCROLL_NONE;
Sunny Goyal48461932015-03-09 17:41:09 -0700739 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700740 }
741
Sunny Goyale9b651e2015-04-24 11:44:51 -0700742 /**
743 * When performing an accessibility drop, onDrop is sent immediately after onDragEnter. So we
744 * need to complete all transient states based on timers.
745 */
746 @Override
747 public void prepareAccessibilityDrop() {
748 if (mReorderAlarm.alarmPending()) {
749 mReorderAlarm.cancelAlarm();
750 mReorderAlarmListener.onAlarm(mReorderAlarm);
751 }
752 }
753
Michael Jurka1e2f4652013-07-08 18:03:46 -0700754 public void onDropCompleted(final View target, final DragObject d,
755 final boolean isFlingToDelete, final boolean success) {
756 if (mDeferDropAfterUninstall) {
Michael Jurkaf3007582013-08-21 14:33:57 +0200757 Log.d(TAG, "Deferred handling drop because waiting for uninstall.");
Michael Jurka1e2f4652013-07-08 18:03:46 -0700758 mDeferredAction = new Runnable() {
759 public void run() {
760 onDropCompleted(target, d, isFlingToDelete, success);
761 mDeferredAction = null;
762 }
763 };
764 return;
765 }
766
767 boolean beingCalledAfterUninstall = mDeferredAction != null;
768 boolean successfulDrop =
769 success && (!beingCalledAfterUninstall || mUninstallSuccessful);
Winson Chung5f8afe62013-08-12 16:19:28 -0700770
Michael Jurka1e2f4652013-07-08 18:03:46 -0700771 if (successfulDrop) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800772 if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon && target != this) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700773 replaceFolderWithFinalItem();
774 }
775 } else {
776 // The drag failed, we need to return the item to the folder
Sunny Goyalb8c663c2015-04-23 11:43:48 -0700777 ShortcutInfo info = (ShortcutInfo) d.dragInfo;
778 View icon = (mCurrentDragView != null && mCurrentDragView.getTag() == info)
779 ? mCurrentDragView : mContent.createNewView(info);
780 ArrayList<View> views = getItemsInReadingOrder();
781 views.add(info.rank, icon);
782 mContent.arrangeChildren(views, views.size());
783 mItemsInvalidated = true;
784
785 mSuppressOnAdd = true;
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700786 mFolderIcon.onDrop(d);
Sunny Goyalb8c663c2015-04-23 11:43:48 -0700787 mSuppressOnAdd = false;
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700788 }
789
790 if (target != this) {
791 if (mOnExitAlarm.alarmPending()) {
792 mOnExitAlarm.cancelAlarm();
Michael Jurka54554252013-08-01 12:52:23 +0200793 if (!successfulDrop) {
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700794 mSuppressFolderDeletion = true;
795 }
Sunny Goyal5d85c442015-03-10 13:14:47 -0700796 mScrollPauseAlarm.cancelAlarm();
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700797 completeDragExit();
798 }
Adam Cohen9c58d822013-12-13 17:18:10 -0800799 }
800
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700801 mDeleteFolderOnDropCompleted = false;
802 mDragInProgress = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700803 mItemAddedBackToSelfViaIcon = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700804 mCurrentDragInfo = null;
805 mCurrentDragView = null;
806 mSuppressOnAdd = false;
Adam Cohen4045eb72011-10-06 11:44:26 -0700807
808 // Reordering may have occured, and we need to save the new item locations. We do this once
809 // at the end to prevent unnecessary database operations.
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700810 updateItemLocationsInDatabaseBatch();
Adam Cohen4045eb72011-10-06 11:44:26 -0700811 }
812
Sunny Goyalfa401a12015-04-10 13:45:42 -0700813 @Override
Michael Jurka1e2f4652013-07-08 18:03:46 -0700814 public void deferCompleteDropAfterUninstallActivity() {
815 mDeferDropAfterUninstall = true;
816 }
817
Sunny Goyalfa401a12015-04-10 13:45:42 -0700818 @Override
Michael Jurka1e2f4652013-07-08 18:03:46 -0700819 public void onUninstallActivityReturned(boolean success) {
820 mDeferDropAfterUninstall = false;
821 mUninstallSuccessful = success;
822 if (mDeferredAction != null) {
823 mDeferredAction.run();
824 }
825 }
826
Winson Chunga48487a2012-03-20 16:19:37 -0700827 @Override
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800828 public float getIntrinsicIconScaleFactor() {
829 return 1f;
830 }
831
832 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800833 public boolean supportsFlingToDelete() {
834 return true;
835 }
836
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000837 @Override
838 public boolean supportsAppInfoDropTarget() {
839 return false;
840 }
841
842 @Override
843 public boolean supportsDeleteDropTarget() {
844 return true;
845 }
846
Sunny Goyalddec7342015-04-29 18:12:37 -0700847 @Override
848 public void onFlingToDelete(DragObject d, PointF vec) {
Winson Chunga48487a2012-03-20 16:19:37 -0700849 // Do nothing
850 }
851
852 @Override
853 public void onFlingToDeleteCompleted() {
854 // Do nothing
855 }
856
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700857 private void updateItemLocationsInDatabaseBatch() {
858 ArrayList<View> list = getItemsInReadingOrder();
859 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
860 for (int i = 0; i < list.size(); i++) {
861 View v = list.get(i);
862 ItemInfo info = (ItemInfo) v.getTag();
Sunny Goyal08f72612015-01-05 13:41:43 -0800863 info.rank = i;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700864 items.add(info);
865 }
866
867 LauncherModel.moveItemsInDatabase(mLauncher, items, mInfo.id, 0);
868 }
869
Adam Cohene25af792013-06-06 23:08:25 -0700870 public void addItemLocationsInDatabase() {
871 ArrayList<View> list = getItemsInReadingOrder();
872 for (int i = 0; i < list.size(); i++) {
873 View v = list.get(i);
874 ItemInfo info = (ItemInfo) v.getTag();
875 LauncherModel.addItemToDatabase(mLauncher, info, mInfo.id, 0,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700876 info.cellX, info.cellY);
Adam Cohene25af792013-06-06 23:08:25 -0700877 }
878 }
879
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700880 public void notifyDrop() {
881 if (mDragInProgress) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700882 mItemAddedBackToSelfViaIcon = true;
Adam Cohen76078c42011-06-09 15:06:52 -0700883 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700884 }
885
886 public boolean isDropEnabled() {
887 return true;
888 }
889
Adam Cohen2801caf2011-05-13 20:57:39 -0700890 public boolean isFull() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800891 return mContent.isFull();
Adam Cohen2801caf2011-05-13 20:57:39 -0700892 }
893
894 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700895 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700896
Winson Chung892c74d2013-08-22 16:15:50 -0700897 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700898 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700899 int height = getFolderHeight();
Adam Cohen2801caf2011-05-13 20:57:39 -0700900
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800901 float scale = parent.getDescendantRectRelativeToSelf(mFolderIcon, sTempRect);
Adam Cohen8e776a62011-06-28 18:10:06 -0700902
Winson Chungaf40f202013-09-18 18:26:31 -0700903 LauncherAppState app = LauncherAppState.getInstance();
904 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
905
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800906 int centerX = (int) (sTempRect.left + sTempRect.width() * scale / 2);
907 int centerY = (int) (sTempRect.top + sTempRect.height() * scale / 2);
Adam Cohen2801caf2011-05-13 20:57:39 -0700908 int centeredLeft = centerX - width / 2;
909 int centeredTop = centerY - height / 2;
Winson Chung3057b1c2013-10-10 17:35:35 -0700910 int currentPage = mLauncher.getWorkspace().getNextPage();
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800911 // In case the workspace is scrolling, we need to use the final scroll to compute
912 // the folders bounds.
913 mLauncher.getWorkspace().setFinalScrollForPageChange(currentPage);
Adam Cohen35e7e642011-07-17 14:47:18 -0700914 // We first fetch the currently visible CellLayoutChildren
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800915 CellLayout currentLayout = (CellLayout) mLauncher.getWorkspace().getChildAt(currentPage);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700916 ShortcutAndWidgetContainer boundingLayout = currentLayout.getShortcutsAndWidgets();
Adam Cohen35e7e642011-07-17 14:47:18 -0700917 Rect bounds = new Rect();
918 parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800919 // We reset the workspaces scroll
920 mLauncher.getWorkspace().resetFinalScrollForPageChange(currentPage);
Adam Cohen2801caf2011-05-13 20:57:39 -0700921
Adam Cohen35e7e642011-07-17 14:47:18 -0700922 // We need to bound the folder to the currently visible CellLayoutChildren
923 int left = Math.min(Math.max(bounds.left, centeredLeft),
924 bounds.left + bounds.width() - width);
925 int top = Math.min(Math.max(bounds.top, centeredTop),
926 bounds.top + bounds.height() - height);
Winson Chungaf40f202013-09-18 18:26:31 -0700927 if (grid.isPhone() && (grid.availableWidthPx - width) < grid.iconSizePx) {
928 // Center the folder if it is full (on phones only)
929 left = (grid.availableWidthPx - width) / 2;
930 } else if (width >= bounds.width()) {
931 // If the folder doesn't fit within the bounds, center it about the desired bounds
Adam Cohen35e7e642011-07-17 14:47:18 -0700932 left = bounds.left + (bounds.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700933 }
Adam Cohen35e7e642011-07-17 14:47:18 -0700934 if (height >= bounds.height()) {
935 top = bounds.top + (bounds.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700936 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700937
938 int folderPivotX = width / 2 + (centeredLeft - left);
939 int folderPivotY = height / 2 + (centeredTop - top);
940 setPivotX(folderPivotX);
941 setPivotY(folderPivotY);
Adam Cohen268c4752012-06-06 17:47:33 -0700942 mFolderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700943 (1.0f * folderPivotX / width));
Adam Cohen268c4752012-06-06 17:47:33 -0700944 mFolderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700945 (1.0f * folderPivotY / height));
Adam Cohen3bf84d32012-05-07 20:17:14 -0700946
Adam Cohen662b5982011-12-13 17:45:21 -0800947 lp.width = width;
948 lp.height = height;
949 lp.x = left;
950 lp.y = top;
Adam Cohen2801caf2011-05-13 20:57:39 -0700951 }
952
Adam Cohen268c4752012-06-06 17:47:33 -0700953 float getPivotXForIconAnimation() {
954 return mFolderIconPivotX;
955 }
956 float getPivotYForIconAnimation() {
957 return mFolderIconPivotY;
958 }
959
Winson Chung892c74d2013-08-22 16:15:50 -0700960 private int getContentAreaHeight() {
961 LauncherAppState app = LauncherAppState.getInstance();
962 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
963 Rect workspacePadding = grid.getWorkspacePadding(grid.isLandscape ?
964 CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
965 int maxContentAreaHeight = grid.availableHeightPx -
Winson Chung892c74d2013-08-22 16:15:50 -0700966 workspacePadding.top - workspacePadding.bottom -
Sunny Goyal290800b2015-03-05 11:33:33 -0800967 mFooterHeight;
Adam Cohen1960ea42013-11-12 11:33:14 +0000968 int height = Math.min(maxContentAreaHeight,
Winson Chung892c74d2013-08-22 16:15:50 -0700969 mContent.getDesiredHeight());
Adam Cohen1960ea42013-11-12 11:33:14 +0000970 return Math.max(height, MIN_CONTENT_DIMEN);
971 }
972
973 private int getContentAreaWidth() {
974 return Math.max(mContent.getDesiredWidth(), MIN_CONTENT_DIMEN);
Winson Chung892c74d2013-08-22 16:15:50 -0700975 }
976
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700977 private int getFolderHeight() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800978 return getFolderHeight(getContentAreaHeight());
979 }
980
981 private int getFolderHeight(int contentAreaHeight) {
Sunny Goyal290800b2015-03-05 11:33:33 -0800982 return getPaddingTop() + getPaddingBottom() + contentAreaHeight + mFooterHeight;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700983 }
Adam Cohen234c4cd2011-07-17 21:03:04 -0700984
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700985 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800986 int contentWidth = getContentAreaWidth();
987 int contentHeight = getContentAreaHeight();
Adam Cohen1960ea42013-11-12 11:33:14 +0000988
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800989 int contentAreaWidthSpec = MeasureSpec.makeMeasureSpec(contentWidth, MeasureSpec.EXACTLY);
990 int contentAreaHeightSpec = MeasureSpec.makeMeasureSpec(contentHeight, MeasureSpec.EXACTLY);
991
992 mContent.setFixedSize(contentWidth, contentHeight);
993 mContentWrapper.measure(contentAreaWidthSpec, contentAreaHeightSpec);
Sunny Goyal290800b2015-03-05 11:33:33 -0800994 mFooter.measure(contentAreaWidthSpec,
995 MeasureSpec.makeMeasureSpec(mFooterHeight, MeasureSpec.EXACTLY));
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800996
997 int folderWidth = getPaddingLeft() + getPaddingRight() + contentWidth;
998 int folderHeight = getFolderHeight(contentHeight);
999 setMeasuredDimension(folderWidth, folderHeight);
Adam Cohen234c4cd2011-07-17 21:03:04 -07001000 }
1001
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001002 /**
1003 * Rearranges the children based on their rank.
1004 */
1005 public void rearrangeChildren() {
1006 rearrangeChildren(-1);
1007 }
Adam Cohen2801caf2011-05-13 20:57:39 -07001008
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001009 /**
1010 * Rearranges the children based on their rank.
1011 * @param itemCount if greater than the total children count, empty spaces are left at the end,
1012 * otherwise it is ignored.
1013 */
1014 public void rearrangeChildren(int itemCount) {
1015 ArrayList<View> views = getItemsInReadingOrder();
1016 mContent.arrangeChildren(views, Math.max(itemCount, views.size()));
Adam Cohen7c693212011-05-18 15:26:57 -07001017 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -07001018 }
1019
Sunny Goyalc4918352015-03-10 18:15:48 -07001020 // TODO remove this once GSA code fix is submitted
1021 public ViewGroup getContent() {
1022 return (ViewGroup) mContent;
1023 }
1024
Adam Cohena9cf38f2011-05-02 15:36:58 -07001025 public int getItemCount() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001026 return mContent.getItemCount();
Adam Cohena9cf38f2011-05-02 15:36:58 -07001027 }
1028
Adam Cohen091440a2015-03-18 14:16:05 -07001029 @Thunk void onCloseComplete() {
Adam Cohen05e0f402011-08-01 12:12:49 -07001030 DragLayer parent = (DragLayer) getParent();
Michael Jurka5649c282012-06-18 10:33:21 -07001031 if (parent != null) {
1032 parent.removeView(this);
1033 }
Adam Cohen4554ee12011-08-03 16:13:21 -07001034 mDragController.removeDropTarget((DropTarget) this);
Adam Cohen05e0f402011-08-01 12:12:49 -07001035 clearFocus();
Adam Cohenac56cff2011-09-28 20:45:37 -07001036 mFolderIcon.requestFocus();
Adam Cohen05e0f402011-08-01 12:12:49 -07001037
Adam Cohen2801caf2011-05-13 20:57:39 -07001038 if (mRearrangeOnClose) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001039 rearrangeChildren();
Adam Cohen2801caf2011-05-13 20:57:39 -07001040 mRearrangeOnClose = false;
1041 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001042 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001043 if (!mDragInProgress && !mSuppressFolderDeletion) {
1044 replaceFolderWithFinalItem();
1045 } else if (mDragInProgress) {
1046 mDeleteFolderOnDropCompleted = true;
1047 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001048 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001049 mSuppressFolderDeletion = false;
Sunny Goyal31abc292015-05-01 10:42:32 -07001050 clearDragInfo();
Adam Cohenafb01ee2011-06-23 15:38:03 -07001051 }
1052
Adam Cohen091440a2015-03-18 14:16:05 -07001053 @Thunk void replaceFolderWithFinalItem() {
Adam Cohenafb01ee2011-06-23 15:38:03 -07001054 // Add the last remaining child to the workspace in place of the folder
Adam Cohenfb91f302012-06-11 15:45:18 -07001055 Runnable onCompleteRunnable = new Runnable() {
1056 @Override
1057 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -07001058 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screenId);
Adam Cohenafb01ee2011-06-23 15:38:03 -07001059
Winson Chung33231f52013-12-09 16:57:45 -08001060 View child = null;
Adam Cohenfb91f302012-06-11 15:45:18 -07001061 // Move the item from the folder to the workspace, in the position of the folder
1062 if (getItemCount() == 1) {
1063 ShortcutInfo finalItem = mInfo.contents.get(0);
Adam Cohenc5e63f32012-07-12 16:16:57 -07001064 child = mLauncher.createShortcut(R.layout.application, cellLayout,
Adam Cohenfb91f302012-06-11 15:45:18 -07001065 finalItem);
1066 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -07001067 mInfo.screenId, mInfo.cellX, mInfo.cellY);
Adam Cohenfb91f302012-06-11 15:45:18 -07001068 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001069 if (getItemCount() <= 1) {
1070 // Remove the folder
1071 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
Dan Sandler0eb687f2014-01-10 13:24:55 -05001072 if (cellLayout != null) {
1073 // b/12446428 -- sometimes the cell layout has already gone away?
1074 cellLayout.removeView(mFolderIcon);
1075 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001076 if (mFolderIcon instanceof DropTarget) {
1077 mDragController.removeDropTarget((DropTarget) mFolderIcon);
1078 }
1079 mLauncher.removeFolder(mInfo);
1080 }
Adam Cohenc5e63f32012-07-12 16:16:57 -07001081 // We add the child after removing the folder to prevent both from existing at
Winson Chung0e6a7132013-08-23 12:55:10 -07001082 // the same time in the CellLayout. We need to add the new item with addInScreenFromBind()
1083 // to ensure that hotseat items are placed correctly.
Adam Cohenc5e63f32012-07-12 16:16:57 -07001084 if (child != null) {
Winson Chung0e6a7132013-08-23 12:55:10 -07001085 mLauncher.getWorkspace().addInScreenFromBind(child, mInfo.container, mInfo.screenId,
Adam Cohenc5e63f32012-07-12 16:16:57 -07001086 mInfo.cellX, mInfo.cellY, mInfo.spanX, mInfo.spanY);
1087 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001088 }
1089 };
Sunny Goyalbc753352015-03-05 09:40:44 -08001090 View finalChild = mContent.getLastItem();
Adam Cohenfb91f302012-06-11 15:45:18 -07001091 if (finalChild != null) {
1092 mFolderIcon.performDestroyAnimation(finalChild, onCompleteRunnable);
Winson Chung33231f52013-12-09 16:57:45 -08001093 } else {
1094 onCompleteRunnable.run();
Adam Cohenafb01ee2011-06-23 15:38:03 -07001095 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001096 mDestroyed = true;
1097 }
1098
1099 boolean isDestroyed() {
1100 return mDestroyed;
Adam Cohen2801caf2011-05-13 20:57:39 -07001101 }
1102
Adam Cohenac56cff2011-09-28 20:45:37 -07001103 // This method keeps track of the last item in the folder for the purposes
1104 // of keyboard focus
Sunny Goyal290800b2015-03-05 11:33:33 -08001105 public void updateTextViewFocus() {
Sunny Goyalbc753352015-03-05 09:40:44 -08001106 View lastChild = mContent.getLastItem();
Adam Cohenac56cff2011-09-28 20:45:37 -07001107 if (lastChild != null) {
1108 mFolderName.setNextFocusDownId(lastChild.getId());
1109 mFolderName.setNextFocusRightId(lastChild.getId());
1110 mFolderName.setNextFocusLeftId(lastChild.getId());
1111 mFolderName.setNextFocusUpId(lastChild.getId());
1112 }
1113 }
1114
Adam Cohenbfbfd262011-06-13 16:55:12 -07001115 public void onDrop(DragObject d) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001116 Runnable cleanUpRunnable = null;
1117
Adam Cohen689ff162014-05-08 17:27:56 -07001118 // If we are coming from All Apps space, we defer removing the extra empty screen
1119 // until the folder closes
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001120 if (d.dragSource != mLauncher.getWorkspace() && !(d.dragSource instanceof Folder)) {
1121 cleanUpRunnable = new Runnable() {
1122 @Override
1123 public void run() {
Adam Cohen689ff162014-05-08 17:27:56 -07001124 mLauncher.exitSpringLoadedDragModeDelayed(true,
1125 Launcher.EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
1126 null);
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001127 }
1128 };
Adam Cohenbfbfd262011-06-13 16:55:12 -07001129 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001130
Sunny Goyalb8634152015-04-09 14:17:14 -07001131 // If the icon was dropped while the page was being scrolled, we need to compute
1132 // the target location again such that the icon is placed of the final page.
1133 if (!mContent.rankOnCurrentPage(mEmptyCellRank)) {
1134 // Reorder again.
1135 mTargetRank = getTargetRank(d, null);
Sunny Goyal48461932015-03-09 17:41:09 -07001136
Sunny Goyalb8634152015-04-09 14:17:14 -07001137 // Rearrange items immediately.
1138 mReorderAlarmListener.onAlarm(mReorderAlarm);
Sunny Goyal48461932015-03-09 17:41:09 -07001139
Sunny Goyalb8634152015-04-09 14:17:14 -07001140 mOnScrollHintAlarm.cancelAlarm();
1141 mScrollPauseAlarm.cancelAlarm();
Sunny Goyal48461932015-03-09 17:41:09 -07001142 }
Sunny Goyalb8634152015-04-09 14:17:14 -07001143 mContent.completePendingPageChanges();
Sunny Goyal48461932015-03-09 17:41:09 -07001144
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001145 View currentDragView;
1146 ShortcutInfo si = mCurrentDragInfo;
1147 if (mIsExternalDrag) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001148 currentDragView = mContent.createAndAddViewForRank(si, mEmptyCellRank);
Sunny Goyal95abbb32014-08-04 10:53:22 -07001149 // Actually move the item in the database if it was an external drag. Call this
1150 // before creating the view, so that ShortcutInfo is updated appropriately.
1151 LauncherModel.addOrMoveItemInDatabase(
1152 mLauncher, si, mInfo.id, 0, si.cellX, si.cellY);
1153
1154 // We only need to update the locations if it doesn't get handled in #onDropCompleted.
1155 if (d.dragSource != this) {
1156 updateItemLocationsInDatabaseBatch();
1157 }
1158 mIsExternalDrag = false;
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001159 } else {
1160 currentDragView = mCurrentDragView;
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001161 mContent.addViewForRank(currentDragView, si, mEmptyCellRank);
Adam Cohenbfbfd262011-06-13 16:55:12 -07001162 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001163
1164 if (d.dragView.hasDrawn()) {
1165
1166 // Temporarily reset the scale such that the animation target gets calculated correctly.
1167 float scaleX = getScaleX();
1168 float scaleY = getScaleY();
1169 setScaleX(1.0f);
1170 setScaleY(1.0f);
1171 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, currentDragView,
1172 cleanUpRunnable, null);
1173 setScaleX(scaleX);
1174 setScaleY(scaleY);
1175 } else {
1176 d.deferDragViewCleanupPostAnimation = false;
1177 currentDragView.setVisibility(VISIBLE);
1178 }
1179 mItemsInvalidated = true;
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001180 rearrangeChildren();
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001181
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001182 // Temporarily suppress the listener, as we did all the work already here.
1183 mSuppressOnAdd = true;
1184 mInfo.add(si);
1185 mSuppressOnAdd = false;
Sunny Goyal4b020172014-08-28 14:51:14 -07001186 // Clear the drag info, as it is no longer being dragged.
1187 mCurrentDragInfo = null;
Sunny Goyalf4066152015-04-15 09:42:19 -07001188 mDragInProgress = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001189 }
1190
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001191 // This is used so the item doesn't immediately appear in the folder when added. In one case
1192 // we need to create the illusion that the item isn't added back to the folder yet, to
1193 // to correspond to the animation of the icon back into the folder. This is
1194 public void hideItem(ShortcutInfo info) {
1195 View v = getViewForInfo(info);
1196 v.setVisibility(INVISIBLE);
1197 }
1198 public void showItem(ShortcutInfo info) {
1199 View v = getViewForInfo(info);
1200 v.setVisibility(VISIBLE);
1201 }
1202
Adam Cohenbfbfd262011-06-13 16:55:12 -07001203 public void onAdd(ShortcutInfo item) {
Adam Cohen05e0f402011-08-01 12:12:49 -07001204 // If the item was dropped onto this open folder, we have done the work associated
1205 // with adding the item to the folder, as indicated by mSuppressOnAdd being set
Adam Cohenbfbfd262011-06-13 16:55:12 -07001206 if (mSuppressOnAdd) return;
Sunny Goyal5d85c442015-03-10 13:14:47 -07001207 mContent.createAndAddViewForRank(item, mContent.allocateRankForNewItem(item));
Sunny Goyal2e688a82015-03-18 10:23:39 -07001208 mItemsInvalidated = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001209 LauncherModel.addOrMoveItemInDatabase(
1210 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
1211 }
1212
Adam Cohena9cf38f2011-05-02 15:36:58 -07001213 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -07001214 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001215 // If this item is being dragged from this open folder, we have already handled
1216 // the work associated with removing the item, so we don't have to do anything here.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001217 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001218 View v = getViewForInfo(item);
Sunny Goyal290800b2015-03-05 11:33:33 -08001219 mContent.removeItem(v);
Adam Cohen2801caf2011-05-13 20:57:39 -07001220 if (mState == STATE_ANIMATING) {
1221 mRearrangeOnClose = true;
1222 } else {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001223 rearrangeChildren();
Adam Cohen2801caf2011-05-13 20:57:39 -07001224 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001225 if (getItemCount() <= 1) {
1226 replaceFolderWithFinalItem();
1227 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001228 }
Adam Cohen7c693212011-05-18 15:26:57 -07001229
Sunny Goyalbc753352015-03-05 09:40:44 -08001230 private View getViewForInfo(final ShortcutInfo item) {
1231 return mContent.iterateOverItems(new ItemOperator() {
1232
1233 @Override
1234 public boolean evaluate(ItemInfo info, View view, View parent) {
1235 return info == item;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001236 }
Sunny Goyalbc753352015-03-05 09:40:44 -08001237 });
Adam Cohendf1e4e82011-06-24 15:57:39 -07001238 }
1239
Adam Cohen76078c42011-06-09 15:06:52 -07001240 public void onItemsChanged() {
Adam Cohenac56cff2011-09-28 20:45:37 -07001241 updateTextViewFocus();
Adam Cohen76078c42011-06-09 15:06:52 -07001242 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001243
Adam Cohen76fc0852011-06-17 13:26:23 -07001244 public void onTitleChanged(CharSequence title) {
1245 }
Adam Cohen76078c42011-06-09 15:06:52 -07001246
Adam Cohen7c693212011-05-18 15:26:57 -07001247 public ArrayList<View> getItemsInReadingOrder() {
1248 if (mItemsInvalidated) {
1249 mItemsInReadingOrder.clear();
Sunny Goyalbc753352015-03-05 09:40:44 -08001250 mContent.iterateOverItems(new ItemOperator() {
1251
1252 @Override
1253 public boolean evaluate(ItemInfo info, View view, View parent) {
1254 mItemsInReadingOrder.add(view);
1255 return false;
Adam Cohen7c693212011-05-18 15:26:57 -07001256 }
Sunny Goyalbc753352015-03-05 09:40:44 -08001257 });
Adam Cohen7c693212011-05-18 15:26:57 -07001258 mItemsInvalidated = false;
1259 }
1260 return mItemsInReadingOrder;
1261 }
Adam Cohen8dfcba42011-07-07 16:38:18 -07001262
1263 public void getLocationInDragLayer(int[] loc) {
1264 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
1265 }
Adam Cohenea0818d2011-09-30 20:06:58 -07001266
1267 public void onFocusChange(View v, boolean hasFocus) {
1268 if (v == mFolderName && hasFocus) {
1269 startEditingFolderName();
1270 }
1271 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001272
1273 @Override
1274 public void getHitRectRelativeToDragLayer(Rect outRect) {
1275 getHitRect(outRect);
Sunny Goyal48461932015-03-09 17:41:09 -07001276 outRect.left -= mScrollAreaOffset;
1277 outRect.right += mScrollAreaOffset;
1278 }
1279
1280 private class OnScrollHintListener implements OnAlarmListener {
1281
1282 private final DragObject mDragObject;
1283
1284 OnScrollHintListener(DragObject object) {
1285 mDragObject = object;
1286 }
1287
1288 /**
1289 * Scroll hint has been shown long enough. Now scroll to appropriate page.
1290 */
1291 @Override
1292 public void onAlarm(Alarm alarm) {
1293 if (mCurrentScrollDir == DragController.SCROLL_LEFT) {
Sunny Goyalb8634152015-04-09 14:17:14 -07001294 mContent.scrollLeft();
Sunny Goyal48461932015-03-09 17:41:09 -07001295 mScrollHintDir = DragController.SCROLL_NONE;
1296 } else if (mCurrentScrollDir == DragController.SCROLL_RIGHT) {
Sunny Goyalb8634152015-04-09 14:17:14 -07001297 mContent.scrollRight();
Sunny Goyal48461932015-03-09 17:41:09 -07001298 mScrollHintDir = DragController.SCROLL_NONE;
1299 } else {
1300 // This should not happen
1301 return;
1302 }
1303 mCurrentScrollDir = DragController.SCROLL_NONE;
1304
1305 // Pause drag event until the scrolling is finished
1306 mScrollPauseAlarm.setOnAlarmListener(new OnScrollFinishedListener(mDragObject));
1307 mScrollPauseAlarm.setAlarm(DragController.RESCROLL_DELAY);
1308 }
1309 }
1310
1311 private class OnScrollFinishedListener implements OnAlarmListener {
1312
1313 private final DragObject mDragObject;
1314
1315 OnScrollFinishedListener(DragObject object) {
1316 mDragObject = object;
1317 }
1318
1319 /**
1320 * Page scroll is complete.
1321 */
1322 @Override
1323 public void onAlarm(Alarm alarm) {
1324 // Reorder immediately on page change.
1325 onDragOver(mDragObject, 1);
1326 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001327 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001328}