blob: a2828054c6f0223818285e5ede76dc3e4ccae580 [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 }
278 mLauncher.getWorkspace().setAddNewPageOnDrag(!enable);
279 }
280
Adam Cohen76fc0852011-06-17 13:26:23 -0700281 public boolean isEditingName() {
282 return mIsEditingName;
283 }
284
285 public void startEditingFolderName() {
Adam Cohena65beee2011-06-27 21:32:23 -0700286 mFolderName.setHint("");
Adam Cohen76fc0852011-06-17 13:26:23 -0700287 mIsEditingName = true;
288 }
289
290 public void dismissEditingName() {
291 mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
292 doneEditingFolderName(true);
293 }
294
295 public void doneEditingFolderName(boolean commit) {
Adam Cohena65beee2011-06-27 21:32:23 -0700296 mFolderName.setHint(sHintText);
Adam Cohen1df26a32011-08-12 16:15:23 -0700297 // Convert to a string here to ensure that no other state associated with the text field
298 // gets saved.
Adam Cohen3371da02011-10-25 21:38:29 -0700299 String newTitle = mFolderName.getText().toString();
300 mInfo.setTitle(newTitle);
Adam Cohen76fc0852011-06-17 13:26:23 -0700301 LauncherModel.updateItemInDatabase(mLauncher, mInfo);
Adam Cohenac56cff2011-09-28 20:45:37 -0700302
Adam Cohen3371da02011-10-25 21:38:29 -0700303 if (commit) {
304 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700305 String.format(getContext().getString(R.string.folder_renamed), newTitle));
Adam Cohen3371da02011-10-25 21:38:29 -0700306 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700307 // In order to clear the focus from the text field, we set the focus on ourself. This
308 // ensures that every time the field is clicked, focus is gained, giving reliable behavior.
309 requestFocus();
310
Adam Cohene601a432011-07-26 21:51:30 -0700311 Selection.setSelection((Spannable) mFolderName.getText(), 0, 0);
Adam Cohen76fc0852011-06-17 13:26:23 -0700312 mIsEditingName = false;
313 }
314
315 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
316 if (actionId == EditorInfo.IME_ACTION_DONE) {
317 dismissEditingName();
318 return true;
319 }
320 return false;
321 }
322
323 public View getEditTextRegion() {
324 return mFolderName;
325 }
326
Adam Cohen0c872ba2011-05-05 10:34:16 -0700327 /**
328 * We need to handle touch events to prevent them from falling through to the workspace below.
329 */
330 @Override
331 public boolean onTouchEvent(MotionEvent ev) {
332 return true;
333 }
334
Joe Onorato00acb122009-08-04 16:04:30 -0400335 public void setDragController(DragController dragController) {
336 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800337 }
338
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800339 public void setFolderIcon(FolderIcon icon) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700340 mFolderIcon = icon;
341 }
342
Adam Cohen3371da02011-10-25 21:38:29 -0700343 @Override
344 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
345 // When the folder gets focus, we don't want to announce the list of items.
346 return true;
347 }
348
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800349 /**
350 * @return the FolderInfo object associated with this folder
351 */
352 FolderInfo getInfo() {
353 return mInfo;
354 }
355
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800356 void bind(FolderInfo info) {
357 mInfo = info;
Adam Cohendf2cc412011-04-27 16:56:57 -0700358 ArrayList<ShortcutInfo> children = info.contents;
Sunny Goyal08f72612015-01-05 13:41:43 -0800359 Collections.sort(children, Utilities.RANK_COMPARATOR);
Sunny Goyal08f72612015-01-05 13:41:43 -0800360
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800361 ArrayList<ShortcutInfo> overflow = mContent.bindItems(children);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700362
Jason Monk4ff73882014-04-24 16:48:07 -0400363 // If our folder has too many items we prune them from the list. This is an issue
Adam Cohenc508b2d2011-06-28 14:41:44 -0700364 // when upgrading from the old Folders implementation which could contain an unlimited
365 // number of items.
366 for (ShortcutInfo item: overflow) {
367 mInfo.remove(item);
368 LauncherModel.deleteItemFromDatabase(mLauncher, item);
369 }
370
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800371 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
372 if (lp == null) {
373 lp = new DragLayer.LayoutParams(0, 0);
374 lp.customPosition = true;
375 setLayoutParams(lp);
376 }
377 centerAboutIcon();
378
Adam Cohen4dbe6d92011-05-18 17:14:15 -0700379 mItemsInvalidated = true;
Adam Cohenac56cff2011-09-28 20:45:37 -0700380 updateTextViewFocus();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700381 mInfo.addListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700382
Adam Cohenafb01ee2011-06-23 15:38:03 -0700383 if (!sDefaultFolderName.contentEquals(mInfo.title)) {
Adam Cohen4ef610f2011-06-21 22:37:36 -0700384 mFolderName.setText(mInfo.title);
385 } else {
386 mFolderName.setText("");
387 }
Winson Chung33231f52013-12-09 16:57:45 -0800388
389 // In case any children didn't come across during loading, clean up the folder accordingly
390 mFolderIcon.post(new Runnable() {
391 public void run() {
392 if (getItemCount() <= 1) {
393 replaceFolderWithFinalItem();
394 }
395 }
396 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700397 }
398
399 /**
400 * Creates a new UserFolder, inflated from R.layout.user_folder.
401 *
402 * @param context The application's context.
403 *
404 * @return A new UserFolder.
405 */
406 static Folder fromXml(Context context) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700407 return (Folder) LayoutInflater.from(context).inflate(R.layout.user_folder, null);
Adam Cohendf2cc412011-04-27 16:56:57 -0700408 }
409
410 /**
411 * This method is intended to make the UserFolder to be visually identical in size and position
412 * to its associated FolderIcon. This allows for a seamless transition into the expanded state.
413 */
414 private void positionAndSizeAsIcon() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700415 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800416 setScaleX(0.8f);
417 setScaleY(0.8f);
418 setAlpha(0f);
Adam Cohendf2cc412011-04-27 16:56:57 -0700419 mState = STATE_SMALL;
420 }
421
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700422 private void prepareReveal() {
423 setScaleX(1f);
424 setScaleY(1f);
425 setAlpha(1f);
426 mState = STATE_SMALL;
427 }
428
Adam Cohendf2cc412011-04-27 16:56:57 -0700429 public void animateOpen() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700430 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen6441de02011-12-14 14:25:32 -0800431
Sunny Goyalb8634152015-04-09 14:17:14 -0700432 mContent.completePendingPageChanges();
Sunny Goyal8167dc22015-04-27 13:44:01 -0700433 if (!mDragInProgress) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700434 // Open on the first page.
435 mContent.snapToPageImmediately(0);
Sunny Goyal48461932015-03-09 17:41:09 -0700436 }
437
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700438 Animator openFolderAnim = null;
439 final Runnable onCompleteRunnable;
Kenny Guyd794a3f2014-09-16 15:17:58 +0100440 if (!Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700441 positionAndSizeAsIcon();
442 centerAboutIcon();
443
444 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1);
445 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
446 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
447 final ObjectAnimator oa =
448 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
449 oa.setDuration(mExpandDuration);
450 openFolderAnim = oa;
451
452 setLayerType(LAYER_TYPE_HARDWARE, null);
453 onCompleteRunnable = new Runnable() {
454 @Override
455 public void run() {
456 setLayerType(LAYER_TYPE_NONE, null);
457 }
458 };
459 } else {
460 prepareReveal();
461 centerAboutIcon();
462
463 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
464 int height = getFolderHeight();
465
466 float transX = - 0.075f * (width / 2 - getPivotX());
467 float transY = - 0.075f * (height / 2 - getPivotY());
468 setTranslationX(transX);
469 setTranslationY(transY);
470 PropertyValuesHolder tx = PropertyValuesHolder.ofFloat("translationX", transX, 0);
471 PropertyValuesHolder ty = PropertyValuesHolder.ofFloat("translationY", transY, 0);
472
473 int rx = (int) Math.max(Math.max(width - getPivotX(), 0), getPivotX());
474 int ry = (int) Math.max(Math.max(height - getPivotY(), 0), getPivotY());
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700475 float radius = (float) Math.hypot(rx, ry);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700476 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
477 Animator reveal = LauncherAnimUtils.createCircularReveal(this, (int) getPivotX(),
478 (int) getPivotY(), 0, radius);
479 reveal.setDuration(mMaterialExpandDuration);
480 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
481
Sunny Goyalbc753352015-03-05 09:40:44 -0800482 mContentWrapper.setAlpha(0f);
483 Animator iconsAlpha = LauncherAnimUtils.ofFloat(mContentWrapper, "alpha", 0f, 1f);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700484 iconsAlpha.setDuration(mMaterialExpandDuration);
485 iconsAlpha.setStartDelay(mMaterialExpandStagger);
486 iconsAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
487
Sunny Goyal290800b2015-03-05 11:33:33 -0800488 mFooter.setAlpha(0f);
489 Animator textAlpha = LauncherAnimUtils.ofFloat(mFooter, "alpha", 0f, 1f);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700490 textAlpha.setDuration(mMaterialExpandDuration);
491 textAlpha.setStartDelay(mMaterialExpandStagger);
492 textAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
493
494 Animator drift = LauncherAnimUtils.ofPropertyValuesHolder(this, tx, ty);
495 drift.setDuration(mMaterialExpandDuration);
496 drift.setStartDelay(mMaterialExpandStagger);
497 drift.setInterpolator(new LogDecelerateInterpolator(60, 0));
498
499 anim.play(drift);
500 anim.play(iconsAlpha);
501 anim.play(textAlpha);
502 anim.play(reveal);
503
504 openFolderAnim = anim;
505
Sunny Goyalbc753352015-03-05 09:40:44 -0800506 mContentWrapper.setLayerType(LAYER_TYPE_HARDWARE, null);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700507 onCompleteRunnable = new Runnable() {
508 @Override
509 public void run() {
Sunny Goyalbc753352015-03-05 09:40:44 -0800510 mContentWrapper.setLayerType(LAYER_TYPE_NONE, null);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700511 }
512 };
513 }
514 openFolderAnim.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700515 @Override
516 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700517 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Sunny Goyalbc753352015-03-05 09:40:44 -0800518 mContent.getAccessibilityDescription());
Adam Cohendf2cc412011-04-27 16:56:57 -0700519 mState = STATE_ANIMATING;
520 }
521 @Override
522 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700523 mState = STATE_OPEN;
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700524
525 if (onCompleteRunnable != null) {
526 onCompleteRunnable.run();
527 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800528
Sunny Goyalbc753352015-03-05 09:40:44 -0800529 mContent.setFocusOnFirstChild();
Adam Cohendf2cc412011-04-27 16:56:57 -0700530 }
531 });
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700532 openFolderAnim.start();
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800533
534 // Make sure the folder picks up the last drag move even if the finger doesn't move.
535 if (mDragController.isDragging()) {
536 mDragController.forceTouchMove();
537 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700538
Sunny Goyalb8634152015-04-09 14:17:14 -0700539 FolderPagedView pages = (FolderPagedView) mContent;
540 pages.verifyVisibleHighResIcons(pages.getNextPage());
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800541 }
542
543 public void beginExternalDrag(ShortcutInfo item) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800544 mCurrentDragInfo = item;
Sunny Goyal5d85c442015-03-10 13:14:47 -0700545 mEmptyCellRank = mContent.allocateRankForNewItem(item);
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800546 mIsExternalDrag = true;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800547 mDragInProgress = true;
Sunny Goyalb8634152015-04-09 14:17:14 -0700548
Sunny Goyalf4066152015-04-15 09:42:19 -0700549 // Since this folder opened by another controller, it might not get onDrop or
550 // onDropComplete. Perform cleanup once drag-n-drop ends.
551 mDragController.addDragListener(this);
552 }
553
554 @Override
555 public void onDragStart(DragSource source, Object info, int dragAction) { }
556
557 @Override
558 public void onDragEnd() {
559 if (mIsExternalDrag && mDragInProgress) {
560 completeDragExit();
561 }
562 mDragController.removeDragListener(this);
Adam Cohendf2cc412011-04-27 16:56:57 -0700563 }
564
Adam Cohen091440a2015-03-18 14:16:05 -0700565 @Thunk void sendCustomAccessibilityEvent(int type, String text) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700566 AccessibilityManager accessibilityManager = (AccessibilityManager)
567 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
568 if (accessibilityManager.isEnabled()) {
Adam Cohen3371da02011-10-25 21:38:29 -0700569 AccessibilityEvent event = AccessibilityEvent.obtain(type);
570 onInitializeAccessibilityEvent(event);
571 event.getText().add(text);
Michael Jurka8b805b12012-04-18 14:23:14 -0700572 accessibilityManager.sendAccessibilityEvent(event);
Adam Cohen3371da02011-10-25 21:38:29 -0700573 }
574 }
575
Adam Cohendf2cc412011-04-27 16:56:57 -0700576 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700577 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800578 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
579 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
580 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
Michael Jurka032e6ba2013-04-22 15:08:42 +0200581 final ObjectAnimator oa =
Michael Jurka2ecf9952012-06-18 12:52:28 -0700582 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohendf2cc412011-04-27 16:56:57 -0700583
Adam Cohen2801caf2011-05-13 20:57:39 -0700584 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700585 @Override
586 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700587 onCloseComplete();
Michael Jurka0121c3e2012-05-31 08:36:04 -0700588 setLayerType(LAYER_TYPE_NONE, null);
Adam Cohen2801caf2011-05-13 20:57:39 -0700589 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700590 }
591 @Override
592 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700593 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700594 getContext().getString(R.string.folder_closed));
Adam Cohendf2cc412011-04-27 16:56:57 -0700595 mState = STATE_ANIMATING;
596 }
597 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700598 oa.setDuration(mExpandDuration);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700599 setLayerType(LAYER_TYPE_HARDWARE, null);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100600 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700601 }
602
Adam Cohencb3382b2011-05-24 14:07:08 -0700603 public boolean acceptDrop(DragObject d) {
604 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700605 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700606 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
607 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
608 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700609 }
610
Adam Cohencb3382b2011-05-24 14:07:08 -0700611 public void onDragEnter(DragObject d) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800612 mPrevTargetRank = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700613 mOnExitAlarm.cancelAlarm();
Sunny Goyalb8634152015-04-09 14:17:14 -0700614 // Get the area offset such that the folder only closes if half the drag icon width
615 // is outside the folder area
616 mScrollAreaOffset = d.dragView.getDragRegionWidth() / 2 - d.xOffset;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700617 }
618
619 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
620 public void onAlarm(Alarm alarm) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800621 mContent.realTimeReorder(mEmptyCellRank, mTargetRank);
622 mEmptyCellRank = mTargetRank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700623 }
624 };
625
Sunny Goyalc46bfef2015-01-05 12:40:08 -0800626 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohen2374abf2013-04-16 14:56:57 -0700627 public boolean isLayoutRtl() {
628 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
629 }
630
Sunny Goyal48461932015-03-09 17:41:09 -0700631 @Override
Adam Cohencb3382b2011-05-24 14:07:08 -0700632 public void onDragOver(DragObject d) {
Sunny Goyal48461932015-03-09 17:41:09 -0700633 onDragOver(d, REORDER_DELAY);
634 }
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700635
Sunny Goyal48461932015-03-09 17:41:09 -0700636 private int getTargetRank(DragObject d, float[] recycle) {
637 recycle = d.getVisualCenter(recycle);
638 return mContent.findNearestArea(
639 (int) recycle[0] - getPaddingLeft(), (int) recycle[1] - getPaddingTop());
640 }
641
Adam Cohen091440a2015-03-18 14:16:05 -0700642 @Thunk void onDragOver(DragObject d, int reorderDelay) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700643 if (mScrollPauseAlarm.alarmPending()) {
Sunny Goyal48461932015-03-09 17:41:09 -0700644 return;
645 }
646 final float[] r = new float[2];
647 mTargetRank = getTargetRank(d, r);
648
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800649 if (mTargetRank != mPrevTargetRank) {
Alan Viverette4cda5b72013-08-28 17:53:41 -0700650 mReorderAlarm.cancelAlarm();
Sunny Goyalc46bfef2015-01-05 12:40:08 -0800651 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
652 mReorderAlarm.setAlarm(REORDER_DELAY);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800653 mPrevTargetRank = mTargetRank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700654 }
Sunny Goyal48461932015-03-09 17:41:09 -0700655
Sunny Goyal48461932015-03-09 17:41:09 -0700656 float x = r[0];
Sunny Goyalb8634152015-04-09 14:17:14 -0700657 int currentPage = mContent.getNextPage();
Sunny Goyal48461932015-03-09 17:41:09 -0700658
Sunny Goyalb8634152015-04-09 14:17:14 -0700659 float cellOverlap = mContent.getCurrentCellLayout().getCellWidth()
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700660 * ICON_OVERSCROLL_WIDTH_FACTOR;
661 boolean isOutsideLeftEdge = x < cellOverlap;
662 boolean isOutsideRightEdge = x > (getWidth() - cellOverlap);
Sunny Goyal48461932015-03-09 17:41:09 -0700663
Sunny Goyalb8634152015-04-09 14:17:14 -0700664 if (currentPage > 0 && (mContent.rtlLayout ? isOutsideRightEdge : isOutsideLeftEdge)) {
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700665 showScrollHint(DragController.SCROLL_LEFT, d);
Sunny Goyalb8634152015-04-09 14:17:14 -0700666 } else if (currentPage < (mContent.getPageCount() - 1)
667 && (mContent.rtlLayout ? isOutsideLeftEdge : isOutsideRightEdge)) {
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700668 showScrollHint(DragController.SCROLL_RIGHT, d);
Sunny Goyal48461932015-03-09 17:41:09 -0700669 } else {
670 mOnScrollHintAlarm.cancelAlarm();
671 if (mScrollHintDir != DragController.SCROLL_NONE) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700672 mContent.clearScrollHint();
Sunny Goyal48461932015-03-09 17:41:09 -0700673 mScrollHintDir = DragController.SCROLL_NONE;
674 }
675 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700676 }
677
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700678 private void showScrollHint(int direction, DragObject d) {
679 // Show scroll hint on the right
680 if (mScrollHintDir != direction) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700681 mContent.showScrollHint(direction);
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700682 mScrollHintDir = direction;
683 }
684
685 // Set alarm for when the hint is complete
686 if (!mOnScrollHintAlarm.alarmPending() || mCurrentScrollDir != direction) {
687 mCurrentScrollDir = direction;
688 mOnScrollHintAlarm.cancelAlarm();
689 mOnScrollHintAlarm.setOnAlarmListener(new OnScrollHintListener(d));
690 mOnScrollHintAlarm.setAlarm(SCROLL_HINT_DURATION);
691
692 mReorderAlarm.cancelAlarm();
693 mTargetRank = mEmptyCellRank;
694 }
695 }
696
Adam Cohenbfbfd262011-06-13 16:55:12 -0700697 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
698 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700699 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700700 }
701 };
702
Adam Cohen95bb8002011-07-03 23:40:28 -0700703 public void completeDragExit() {
Sunny Goyalf4066152015-04-15 09:42:19 -0700704 if (mInfo.opened) {
705 mLauncher.closeFolder();
706 mRearrangeOnClose = true;
Sunny Goyal31abc292015-05-01 10:42:32 -0700707 } else if (mState == STATE_ANIMATING) {
708 mRearrangeOnClose = true;
Sunny Goyalf4066152015-04-15 09:42:19 -0700709 } else {
710 rearrangeChildren();
Sunny Goyal31abc292015-05-01 10:42:32 -0700711 clearDragInfo();
Sunny Goyalf4066152015-04-15 09:42:19 -0700712 }
Sunny Goyal31abc292015-05-01 10:42:32 -0700713 }
714
715 private void clearDragInfo() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700716 mCurrentDragInfo = null;
717 mCurrentDragView = null;
718 mSuppressOnAdd = false;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800719 mIsExternalDrag = false;
Adam Cohen3e8f8112011-07-02 18:03:00 -0700720 }
721
Adam Cohencb3382b2011-05-24 14:07:08 -0700722 public void onDragExit(DragObject d) {
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700723 // We only close the folder if this is a true drag exit, ie. not because
724 // a drop has occurred above the folder.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700725 if (!d.dragComplete) {
726 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
727 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
728 }
729 mReorderAlarm.cancelAlarm();
Sunny Goyal48461932015-03-09 17:41:09 -0700730
Sunny Goyalb8634152015-04-09 14:17:14 -0700731 mOnScrollHintAlarm.cancelAlarm();
732 mScrollPauseAlarm.cancelAlarm();
733 if (mScrollHintDir != DragController.SCROLL_NONE) {
734 mContent.clearScrollHint();
735 mScrollHintDir = DragController.SCROLL_NONE;
Sunny Goyal48461932015-03-09 17:41:09 -0700736 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700737 }
738
Sunny Goyale9b651e2015-04-24 11:44:51 -0700739 /**
740 * When performing an accessibility drop, onDrop is sent immediately after onDragEnter. So we
741 * need to complete all transient states based on timers.
742 */
743 @Override
744 public void prepareAccessibilityDrop() {
745 if (mReorderAlarm.alarmPending()) {
746 mReorderAlarm.cancelAlarm();
747 mReorderAlarmListener.onAlarm(mReorderAlarm);
748 }
749 }
750
Michael Jurka1e2f4652013-07-08 18:03:46 -0700751 public void onDropCompleted(final View target, final DragObject d,
752 final boolean isFlingToDelete, final boolean success) {
753 if (mDeferDropAfterUninstall) {
Michael Jurkaf3007582013-08-21 14:33:57 +0200754 Log.d(TAG, "Deferred handling drop because waiting for uninstall.");
Michael Jurka1e2f4652013-07-08 18:03:46 -0700755 mDeferredAction = new Runnable() {
756 public void run() {
757 onDropCompleted(target, d, isFlingToDelete, success);
758 mDeferredAction = null;
759 }
760 };
761 return;
762 }
763
764 boolean beingCalledAfterUninstall = mDeferredAction != null;
765 boolean successfulDrop =
766 success && (!beingCalledAfterUninstall || mUninstallSuccessful);
Winson Chung5f8afe62013-08-12 16:19:28 -0700767
Michael Jurka1e2f4652013-07-08 18:03:46 -0700768 if (successfulDrop) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800769 if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon && target != this) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700770 replaceFolderWithFinalItem();
771 }
772 } else {
773 // The drag failed, we need to return the item to the folder
Sunny Goyalb8c663c2015-04-23 11:43:48 -0700774 ShortcutInfo info = (ShortcutInfo) d.dragInfo;
775 View icon = (mCurrentDragView != null && mCurrentDragView.getTag() == info)
776 ? mCurrentDragView : mContent.createNewView(info);
777 ArrayList<View> views = getItemsInReadingOrder();
778 views.add(info.rank, icon);
779 mContent.arrangeChildren(views, views.size());
780 mItemsInvalidated = true;
781
782 mSuppressOnAdd = true;
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700783 mFolderIcon.onDrop(d);
Sunny Goyalb8c663c2015-04-23 11:43:48 -0700784 mSuppressOnAdd = false;
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700785 }
786
787 if (target != this) {
788 if (mOnExitAlarm.alarmPending()) {
789 mOnExitAlarm.cancelAlarm();
Michael Jurka54554252013-08-01 12:52:23 +0200790 if (!successfulDrop) {
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700791 mSuppressFolderDeletion = true;
792 }
Sunny Goyal5d85c442015-03-10 13:14:47 -0700793 mScrollPauseAlarm.cancelAlarm();
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700794 completeDragExit();
795 }
Adam Cohen9c58d822013-12-13 17:18:10 -0800796 }
797
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700798 mDeleteFolderOnDropCompleted = false;
799 mDragInProgress = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700800 mItemAddedBackToSelfViaIcon = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700801 mCurrentDragInfo = null;
802 mCurrentDragView = null;
803 mSuppressOnAdd = false;
Adam Cohen4045eb72011-10-06 11:44:26 -0700804
805 // Reordering may have occured, and we need to save the new item locations. We do this once
806 // at the end to prevent unnecessary database operations.
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700807 updateItemLocationsInDatabaseBatch();
Adam Cohen4045eb72011-10-06 11:44:26 -0700808 }
809
Sunny Goyalfa401a12015-04-10 13:45:42 -0700810 @Override
Michael Jurka1e2f4652013-07-08 18:03:46 -0700811 public void deferCompleteDropAfterUninstallActivity() {
812 mDeferDropAfterUninstall = true;
813 }
814
Sunny Goyalfa401a12015-04-10 13:45:42 -0700815 @Override
Michael Jurka1e2f4652013-07-08 18:03:46 -0700816 public void onUninstallActivityReturned(boolean success) {
817 mDeferDropAfterUninstall = false;
818 mUninstallSuccessful = success;
819 if (mDeferredAction != null) {
820 mDeferredAction.run();
821 }
822 }
823
Winson Chunga48487a2012-03-20 16:19:37 -0700824 @Override
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800825 public float getIntrinsicIconScaleFactor() {
826 return 1f;
827 }
828
829 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800830 public boolean supportsFlingToDelete() {
831 return true;
832 }
833
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000834 @Override
835 public boolean supportsAppInfoDropTarget() {
836 return false;
837 }
838
839 @Override
840 public boolean supportsDeleteDropTarget() {
841 return true;
842 }
843
Sunny Goyalddec7342015-04-29 18:12:37 -0700844 @Override
845 public void onFlingToDelete(DragObject d, PointF vec) {
Winson Chunga48487a2012-03-20 16:19:37 -0700846 // Do nothing
847 }
848
849 @Override
850 public void onFlingToDeleteCompleted() {
851 // Do nothing
852 }
853
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700854 private void updateItemLocationsInDatabaseBatch() {
855 ArrayList<View> list = getItemsInReadingOrder();
856 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
857 for (int i = 0; i < list.size(); i++) {
858 View v = list.get(i);
859 ItemInfo info = (ItemInfo) v.getTag();
Sunny Goyal08f72612015-01-05 13:41:43 -0800860 info.rank = i;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700861 items.add(info);
862 }
863
864 LauncherModel.moveItemsInDatabase(mLauncher, items, mInfo.id, 0);
865 }
866
Adam Cohene25af792013-06-06 23:08:25 -0700867 public void addItemLocationsInDatabase() {
868 ArrayList<View> list = getItemsInReadingOrder();
869 for (int i = 0; i < list.size(); i++) {
870 View v = list.get(i);
871 ItemInfo info = (ItemInfo) v.getTag();
872 LauncherModel.addItemToDatabase(mLauncher, info, mInfo.id, 0,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700873 info.cellX, info.cellY);
Adam Cohene25af792013-06-06 23:08:25 -0700874 }
875 }
876
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700877 public void notifyDrop() {
878 if (mDragInProgress) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700879 mItemAddedBackToSelfViaIcon = true;
Adam Cohen76078c42011-06-09 15:06:52 -0700880 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700881 }
882
883 public boolean isDropEnabled() {
884 return true;
885 }
886
Adam Cohen2801caf2011-05-13 20:57:39 -0700887 public boolean isFull() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800888 return mContent.isFull();
Adam Cohen2801caf2011-05-13 20:57:39 -0700889 }
890
891 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700892 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700893
Winson Chung892c74d2013-08-22 16:15:50 -0700894 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700895 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700896 int height = getFolderHeight();
Adam Cohen2801caf2011-05-13 20:57:39 -0700897
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800898 float scale = parent.getDescendantRectRelativeToSelf(mFolderIcon, sTempRect);
Adam Cohen8e776a62011-06-28 18:10:06 -0700899
Winson Chungaf40f202013-09-18 18:26:31 -0700900 LauncherAppState app = LauncherAppState.getInstance();
901 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
902
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800903 int centerX = (int) (sTempRect.left + sTempRect.width() * scale / 2);
904 int centerY = (int) (sTempRect.top + sTempRect.height() * scale / 2);
Adam Cohen2801caf2011-05-13 20:57:39 -0700905 int centeredLeft = centerX - width / 2;
906 int centeredTop = centerY - height / 2;
Winson Chung3057b1c2013-10-10 17:35:35 -0700907 int currentPage = mLauncher.getWorkspace().getNextPage();
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800908 // In case the workspace is scrolling, we need to use the final scroll to compute
909 // the folders bounds.
910 mLauncher.getWorkspace().setFinalScrollForPageChange(currentPage);
Adam Cohen35e7e642011-07-17 14:47:18 -0700911 // We first fetch the currently visible CellLayoutChildren
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800912 CellLayout currentLayout = (CellLayout) mLauncher.getWorkspace().getChildAt(currentPage);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700913 ShortcutAndWidgetContainer boundingLayout = currentLayout.getShortcutsAndWidgets();
Adam Cohen35e7e642011-07-17 14:47:18 -0700914 Rect bounds = new Rect();
915 parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800916 // We reset the workspaces scroll
917 mLauncher.getWorkspace().resetFinalScrollForPageChange(currentPage);
Adam Cohen2801caf2011-05-13 20:57:39 -0700918
Adam Cohen35e7e642011-07-17 14:47:18 -0700919 // We need to bound the folder to the currently visible CellLayoutChildren
920 int left = Math.min(Math.max(bounds.left, centeredLeft),
921 bounds.left + bounds.width() - width);
922 int top = Math.min(Math.max(bounds.top, centeredTop),
923 bounds.top + bounds.height() - height);
Winson Chungaf40f202013-09-18 18:26:31 -0700924 if (grid.isPhone() && (grid.availableWidthPx - width) < grid.iconSizePx) {
925 // Center the folder if it is full (on phones only)
926 left = (grid.availableWidthPx - width) / 2;
927 } else if (width >= bounds.width()) {
928 // If the folder doesn't fit within the bounds, center it about the desired bounds
Adam Cohen35e7e642011-07-17 14:47:18 -0700929 left = bounds.left + (bounds.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700930 }
Adam Cohen35e7e642011-07-17 14:47:18 -0700931 if (height >= bounds.height()) {
932 top = bounds.top + (bounds.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700933 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700934
935 int folderPivotX = width / 2 + (centeredLeft - left);
936 int folderPivotY = height / 2 + (centeredTop - top);
937 setPivotX(folderPivotX);
938 setPivotY(folderPivotY);
Adam Cohen268c4752012-06-06 17:47:33 -0700939 mFolderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700940 (1.0f * folderPivotX / width));
Adam Cohen268c4752012-06-06 17:47:33 -0700941 mFolderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700942 (1.0f * folderPivotY / height));
Adam Cohen3bf84d32012-05-07 20:17:14 -0700943
Adam Cohen662b5982011-12-13 17:45:21 -0800944 lp.width = width;
945 lp.height = height;
946 lp.x = left;
947 lp.y = top;
Adam Cohen2801caf2011-05-13 20:57:39 -0700948 }
949
Adam Cohen268c4752012-06-06 17:47:33 -0700950 float getPivotXForIconAnimation() {
951 return mFolderIconPivotX;
952 }
953 float getPivotYForIconAnimation() {
954 return mFolderIconPivotY;
955 }
956
Winson Chung892c74d2013-08-22 16:15:50 -0700957 private int getContentAreaHeight() {
958 LauncherAppState app = LauncherAppState.getInstance();
959 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
960 Rect workspacePadding = grid.getWorkspacePadding(grid.isLandscape ?
961 CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
962 int maxContentAreaHeight = grid.availableHeightPx -
Winson Chung892c74d2013-08-22 16:15:50 -0700963 workspacePadding.top - workspacePadding.bottom -
Sunny Goyal290800b2015-03-05 11:33:33 -0800964 mFooterHeight;
Adam Cohen1960ea42013-11-12 11:33:14 +0000965 int height = Math.min(maxContentAreaHeight,
Winson Chung892c74d2013-08-22 16:15:50 -0700966 mContent.getDesiredHeight());
Adam Cohen1960ea42013-11-12 11:33:14 +0000967 return Math.max(height, MIN_CONTENT_DIMEN);
968 }
969
970 private int getContentAreaWidth() {
971 return Math.max(mContent.getDesiredWidth(), MIN_CONTENT_DIMEN);
Winson Chung892c74d2013-08-22 16:15:50 -0700972 }
973
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700974 private int getFolderHeight() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800975 return getFolderHeight(getContentAreaHeight());
976 }
977
978 private int getFolderHeight(int contentAreaHeight) {
Sunny Goyal290800b2015-03-05 11:33:33 -0800979 return getPaddingTop() + getPaddingBottom() + contentAreaHeight + mFooterHeight;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700980 }
Adam Cohen234c4cd2011-07-17 21:03:04 -0700981
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700982 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800983 int contentWidth = getContentAreaWidth();
984 int contentHeight = getContentAreaHeight();
Adam Cohen1960ea42013-11-12 11:33:14 +0000985
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800986 int contentAreaWidthSpec = MeasureSpec.makeMeasureSpec(contentWidth, MeasureSpec.EXACTLY);
987 int contentAreaHeightSpec = MeasureSpec.makeMeasureSpec(contentHeight, MeasureSpec.EXACTLY);
988
989 mContent.setFixedSize(contentWidth, contentHeight);
990 mContentWrapper.measure(contentAreaWidthSpec, contentAreaHeightSpec);
Sunny Goyal290800b2015-03-05 11:33:33 -0800991 mFooter.measure(contentAreaWidthSpec,
992 MeasureSpec.makeMeasureSpec(mFooterHeight, MeasureSpec.EXACTLY));
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800993
994 int folderWidth = getPaddingLeft() + getPaddingRight() + contentWidth;
995 int folderHeight = getFolderHeight(contentHeight);
996 setMeasuredDimension(folderWidth, folderHeight);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700997 }
998
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800999 /**
1000 * Rearranges the children based on their rank.
1001 */
1002 public void rearrangeChildren() {
1003 rearrangeChildren(-1);
1004 }
Adam Cohen2801caf2011-05-13 20:57:39 -07001005
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001006 /**
1007 * Rearranges the children based on their rank.
1008 * @param itemCount if greater than the total children count, empty spaces are left at the end,
1009 * otherwise it is ignored.
1010 */
1011 public void rearrangeChildren(int itemCount) {
1012 ArrayList<View> views = getItemsInReadingOrder();
1013 mContent.arrangeChildren(views, Math.max(itemCount, views.size()));
Adam Cohen7c693212011-05-18 15:26:57 -07001014 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -07001015 }
1016
Sunny Goyalc4918352015-03-10 18:15:48 -07001017 // TODO remove this once GSA code fix is submitted
1018 public ViewGroup getContent() {
1019 return (ViewGroup) mContent;
1020 }
1021
Adam Cohena9cf38f2011-05-02 15:36:58 -07001022 public int getItemCount() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001023 return mContent.getItemCount();
Adam Cohena9cf38f2011-05-02 15:36:58 -07001024 }
1025
Adam Cohen091440a2015-03-18 14:16:05 -07001026 @Thunk void onCloseComplete() {
Adam Cohen05e0f402011-08-01 12:12:49 -07001027 DragLayer parent = (DragLayer) getParent();
Michael Jurka5649c282012-06-18 10:33:21 -07001028 if (parent != null) {
1029 parent.removeView(this);
1030 }
Adam Cohen4554ee12011-08-03 16:13:21 -07001031 mDragController.removeDropTarget((DropTarget) this);
Adam Cohen05e0f402011-08-01 12:12:49 -07001032 clearFocus();
Adam Cohenac56cff2011-09-28 20:45:37 -07001033 mFolderIcon.requestFocus();
Adam Cohen05e0f402011-08-01 12:12:49 -07001034
Adam Cohen2801caf2011-05-13 20:57:39 -07001035 if (mRearrangeOnClose) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001036 rearrangeChildren();
Adam Cohen2801caf2011-05-13 20:57:39 -07001037 mRearrangeOnClose = false;
1038 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001039 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001040 if (!mDragInProgress && !mSuppressFolderDeletion) {
1041 replaceFolderWithFinalItem();
1042 } else if (mDragInProgress) {
1043 mDeleteFolderOnDropCompleted = true;
1044 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001045 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001046 mSuppressFolderDeletion = false;
Sunny Goyal31abc292015-05-01 10:42:32 -07001047 clearDragInfo();
Adam Cohenafb01ee2011-06-23 15:38:03 -07001048 }
1049
Adam Cohen091440a2015-03-18 14:16:05 -07001050 @Thunk void replaceFolderWithFinalItem() {
Adam Cohenafb01ee2011-06-23 15:38:03 -07001051 // Add the last remaining child to the workspace in place of the folder
Adam Cohenfb91f302012-06-11 15:45:18 -07001052 Runnable onCompleteRunnable = new Runnable() {
1053 @Override
1054 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -07001055 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screenId);
Adam Cohenafb01ee2011-06-23 15:38:03 -07001056
Winson Chung33231f52013-12-09 16:57:45 -08001057 View child = null;
Adam Cohenfb91f302012-06-11 15:45:18 -07001058 // Move the item from the folder to the workspace, in the position of the folder
1059 if (getItemCount() == 1) {
1060 ShortcutInfo finalItem = mInfo.contents.get(0);
Adam Cohenc5e63f32012-07-12 16:16:57 -07001061 child = mLauncher.createShortcut(R.layout.application, cellLayout,
Adam Cohenfb91f302012-06-11 15:45:18 -07001062 finalItem);
1063 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -07001064 mInfo.screenId, mInfo.cellX, mInfo.cellY);
Adam Cohenfb91f302012-06-11 15:45:18 -07001065 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001066 if (getItemCount() <= 1) {
1067 // Remove the folder
1068 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
Dan Sandler0eb687f2014-01-10 13:24:55 -05001069 if (cellLayout != null) {
1070 // b/12446428 -- sometimes the cell layout has already gone away?
1071 cellLayout.removeView(mFolderIcon);
1072 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001073 if (mFolderIcon instanceof DropTarget) {
1074 mDragController.removeDropTarget((DropTarget) mFolderIcon);
1075 }
1076 mLauncher.removeFolder(mInfo);
1077 }
Adam Cohenc5e63f32012-07-12 16:16:57 -07001078 // We add the child after removing the folder to prevent both from existing at
Winson Chung0e6a7132013-08-23 12:55:10 -07001079 // the same time in the CellLayout. We need to add the new item with addInScreenFromBind()
1080 // to ensure that hotseat items are placed correctly.
Adam Cohenc5e63f32012-07-12 16:16:57 -07001081 if (child != null) {
Winson Chung0e6a7132013-08-23 12:55:10 -07001082 mLauncher.getWorkspace().addInScreenFromBind(child, mInfo.container, mInfo.screenId,
Adam Cohenc5e63f32012-07-12 16:16:57 -07001083 mInfo.cellX, mInfo.cellY, mInfo.spanX, mInfo.spanY);
1084 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001085 }
1086 };
Sunny Goyalbc753352015-03-05 09:40:44 -08001087 View finalChild = mContent.getLastItem();
Adam Cohenfb91f302012-06-11 15:45:18 -07001088 if (finalChild != null) {
1089 mFolderIcon.performDestroyAnimation(finalChild, onCompleteRunnable);
Winson Chung33231f52013-12-09 16:57:45 -08001090 } else {
1091 onCompleteRunnable.run();
Adam Cohenafb01ee2011-06-23 15:38:03 -07001092 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001093 mDestroyed = true;
1094 }
1095
1096 boolean isDestroyed() {
1097 return mDestroyed;
Adam Cohen2801caf2011-05-13 20:57:39 -07001098 }
1099
Adam Cohenac56cff2011-09-28 20:45:37 -07001100 // This method keeps track of the last item in the folder for the purposes
1101 // of keyboard focus
Sunny Goyal290800b2015-03-05 11:33:33 -08001102 public void updateTextViewFocus() {
Sunny Goyalbc753352015-03-05 09:40:44 -08001103 View lastChild = mContent.getLastItem();
Adam Cohenac56cff2011-09-28 20:45:37 -07001104 if (lastChild != null) {
1105 mFolderName.setNextFocusDownId(lastChild.getId());
1106 mFolderName.setNextFocusRightId(lastChild.getId());
1107 mFolderName.setNextFocusLeftId(lastChild.getId());
1108 mFolderName.setNextFocusUpId(lastChild.getId());
1109 }
1110 }
1111
Adam Cohenbfbfd262011-06-13 16:55:12 -07001112 public void onDrop(DragObject d) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001113 Runnable cleanUpRunnable = null;
1114
Adam Cohen689ff162014-05-08 17:27:56 -07001115 // If we are coming from All Apps space, we defer removing the extra empty screen
1116 // until the folder closes
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001117 if (d.dragSource != mLauncher.getWorkspace() && !(d.dragSource instanceof Folder)) {
1118 cleanUpRunnable = new Runnable() {
1119 @Override
1120 public void run() {
Adam Cohen689ff162014-05-08 17:27:56 -07001121 mLauncher.exitSpringLoadedDragModeDelayed(true,
1122 Launcher.EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
1123 null);
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001124 }
1125 };
Adam Cohenbfbfd262011-06-13 16:55:12 -07001126 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001127
Sunny Goyalb8634152015-04-09 14:17:14 -07001128 // If the icon was dropped while the page was being scrolled, we need to compute
1129 // the target location again such that the icon is placed of the final page.
1130 if (!mContent.rankOnCurrentPage(mEmptyCellRank)) {
1131 // Reorder again.
1132 mTargetRank = getTargetRank(d, null);
Sunny Goyal48461932015-03-09 17:41:09 -07001133
Sunny Goyalb8634152015-04-09 14:17:14 -07001134 // Rearrange items immediately.
1135 mReorderAlarmListener.onAlarm(mReorderAlarm);
Sunny Goyal48461932015-03-09 17:41:09 -07001136
Sunny Goyalb8634152015-04-09 14:17:14 -07001137 mOnScrollHintAlarm.cancelAlarm();
1138 mScrollPauseAlarm.cancelAlarm();
Sunny Goyal48461932015-03-09 17:41:09 -07001139 }
Sunny Goyalb8634152015-04-09 14:17:14 -07001140 mContent.completePendingPageChanges();
Sunny Goyal48461932015-03-09 17:41:09 -07001141
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001142 View currentDragView;
1143 ShortcutInfo si = mCurrentDragInfo;
1144 if (mIsExternalDrag) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001145 currentDragView = mContent.createAndAddViewForRank(si, mEmptyCellRank);
Sunny Goyal95abbb32014-08-04 10:53:22 -07001146 // Actually move the item in the database if it was an external drag. Call this
1147 // before creating the view, so that ShortcutInfo is updated appropriately.
1148 LauncherModel.addOrMoveItemInDatabase(
1149 mLauncher, si, mInfo.id, 0, si.cellX, si.cellY);
1150
1151 // We only need to update the locations if it doesn't get handled in #onDropCompleted.
1152 if (d.dragSource != this) {
1153 updateItemLocationsInDatabaseBatch();
1154 }
1155 mIsExternalDrag = false;
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001156 } else {
1157 currentDragView = mCurrentDragView;
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001158 mContent.addViewForRank(currentDragView, si, mEmptyCellRank);
Adam Cohenbfbfd262011-06-13 16:55:12 -07001159 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001160
1161 if (d.dragView.hasDrawn()) {
1162
1163 // Temporarily reset the scale such that the animation target gets calculated correctly.
1164 float scaleX = getScaleX();
1165 float scaleY = getScaleY();
1166 setScaleX(1.0f);
1167 setScaleY(1.0f);
1168 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, currentDragView,
1169 cleanUpRunnable, null);
1170 setScaleX(scaleX);
1171 setScaleY(scaleY);
1172 } else {
1173 d.deferDragViewCleanupPostAnimation = false;
1174 currentDragView.setVisibility(VISIBLE);
1175 }
1176 mItemsInvalidated = true;
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001177 rearrangeChildren();
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001178
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001179 // Temporarily suppress the listener, as we did all the work already here.
1180 mSuppressOnAdd = true;
1181 mInfo.add(si);
1182 mSuppressOnAdd = false;
Sunny Goyal4b020172014-08-28 14:51:14 -07001183 // Clear the drag info, as it is no longer being dragged.
1184 mCurrentDragInfo = null;
Sunny Goyalf4066152015-04-15 09:42:19 -07001185 mDragInProgress = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001186 }
1187
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001188 // This is used so the item doesn't immediately appear in the folder when added. In one case
1189 // we need to create the illusion that the item isn't added back to the folder yet, to
1190 // to correspond to the animation of the icon back into the folder. This is
1191 public void hideItem(ShortcutInfo info) {
1192 View v = getViewForInfo(info);
1193 v.setVisibility(INVISIBLE);
1194 }
1195 public void showItem(ShortcutInfo info) {
1196 View v = getViewForInfo(info);
1197 v.setVisibility(VISIBLE);
1198 }
1199
Adam Cohenbfbfd262011-06-13 16:55:12 -07001200 public void onAdd(ShortcutInfo item) {
Adam Cohen05e0f402011-08-01 12:12:49 -07001201 // If the item was dropped onto this open folder, we have done the work associated
1202 // with adding the item to the folder, as indicated by mSuppressOnAdd being set
Adam Cohenbfbfd262011-06-13 16:55:12 -07001203 if (mSuppressOnAdd) return;
Sunny Goyal5d85c442015-03-10 13:14:47 -07001204 mContent.createAndAddViewForRank(item, mContent.allocateRankForNewItem(item));
Sunny Goyal2e688a82015-03-18 10:23:39 -07001205 mItemsInvalidated = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001206 LauncherModel.addOrMoveItemInDatabase(
1207 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
1208 }
1209
Adam Cohena9cf38f2011-05-02 15:36:58 -07001210 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -07001211 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001212 // If this item is being dragged from this open folder, we have already handled
1213 // the work associated with removing the item, so we don't have to do anything here.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001214 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001215 View v = getViewForInfo(item);
Sunny Goyal290800b2015-03-05 11:33:33 -08001216 mContent.removeItem(v);
Adam Cohen2801caf2011-05-13 20:57:39 -07001217 if (mState == STATE_ANIMATING) {
1218 mRearrangeOnClose = true;
1219 } else {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001220 rearrangeChildren();
Adam Cohen2801caf2011-05-13 20:57:39 -07001221 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001222 if (getItemCount() <= 1) {
1223 replaceFolderWithFinalItem();
1224 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001225 }
Adam Cohen7c693212011-05-18 15:26:57 -07001226
Sunny Goyalbc753352015-03-05 09:40:44 -08001227 private View getViewForInfo(final ShortcutInfo item) {
1228 return mContent.iterateOverItems(new ItemOperator() {
1229
1230 @Override
1231 public boolean evaluate(ItemInfo info, View view, View parent) {
1232 return info == item;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001233 }
Sunny Goyalbc753352015-03-05 09:40:44 -08001234 });
Adam Cohendf1e4e82011-06-24 15:57:39 -07001235 }
1236
Adam Cohen76078c42011-06-09 15:06:52 -07001237 public void onItemsChanged() {
Adam Cohenac56cff2011-09-28 20:45:37 -07001238 updateTextViewFocus();
Adam Cohen76078c42011-06-09 15:06:52 -07001239 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001240
Adam Cohen76fc0852011-06-17 13:26:23 -07001241 public void onTitleChanged(CharSequence title) {
1242 }
Adam Cohen76078c42011-06-09 15:06:52 -07001243
Adam Cohen7c693212011-05-18 15:26:57 -07001244 public ArrayList<View> getItemsInReadingOrder() {
1245 if (mItemsInvalidated) {
1246 mItemsInReadingOrder.clear();
Sunny Goyalbc753352015-03-05 09:40:44 -08001247 mContent.iterateOverItems(new ItemOperator() {
1248
1249 @Override
1250 public boolean evaluate(ItemInfo info, View view, View parent) {
1251 mItemsInReadingOrder.add(view);
1252 return false;
Adam Cohen7c693212011-05-18 15:26:57 -07001253 }
Sunny Goyalbc753352015-03-05 09:40:44 -08001254 });
Adam Cohen7c693212011-05-18 15:26:57 -07001255 mItemsInvalidated = false;
1256 }
1257 return mItemsInReadingOrder;
1258 }
Adam Cohen8dfcba42011-07-07 16:38:18 -07001259
1260 public void getLocationInDragLayer(int[] loc) {
1261 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
1262 }
Adam Cohenea0818d2011-09-30 20:06:58 -07001263
1264 public void onFocusChange(View v, boolean hasFocus) {
1265 if (v == mFolderName && hasFocus) {
1266 startEditingFolderName();
1267 }
1268 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001269
1270 @Override
1271 public void getHitRectRelativeToDragLayer(Rect outRect) {
1272 getHitRect(outRect);
Sunny Goyal48461932015-03-09 17:41:09 -07001273 outRect.left -= mScrollAreaOffset;
1274 outRect.right += mScrollAreaOffset;
1275 }
1276
1277 private class OnScrollHintListener implements OnAlarmListener {
1278
1279 private final DragObject mDragObject;
1280
1281 OnScrollHintListener(DragObject object) {
1282 mDragObject = object;
1283 }
1284
1285 /**
1286 * Scroll hint has been shown long enough. Now scroll to appropriate page.
1287 */
1288 @Override
1289 public void onAlarm(Alarm alarm) {
1290 if (mCurrentScrollDir == DragController.SCROLL_LEFT) {
Sunny Goyalb8634152015-04-09 14:17:14 -07001291 mContent.scrollLeft();
Sunny Goyal48461932015-03-09 17:41:09 -07001292 mScrollHintDir = DragController.SCROLL_NONE;
1293 } else if (mCurrentScrollDir == DragController.SCROLL_RIGHT) {
Sunny Goyalb8634152015-04-09 14:17:14 -07001294 mContent.scrollRight();
Sunny Goyal48461932015-03-09 17:41:09 -07001295 mScrollHintDir = DragController.SCROLL_NONE;
1296 } else {
1297 // This should not happen
1298 return;
1299 }
1300 mCurrentScrollDir = DragController.SCROLL_NONE;
1301
1302 // Pause drag event until the scrolling is finished
1303 mScrollPauseAlarm.setOnAlarmListener(new OnScrollFinishedListener(mDragObject));
1304 mScrollPauseAlarm.setAlarm(DragController.RESCROLL_DELAY);
1305 }
1306 }
1307
1308 private class OnScrollFinishedListener implements OnAlarmListener {
1309
1310 private final DragObject mDragObject;
1311
1312 OnScrollFinishedListener(DragObject object) {
1313 mDragObject = object;
1314 }
1315
1316 /**
1317 * Page scroll is complete.
1318 */
1319 @Override
1320 public void onAlarm(Alarm alarm) {
1321 // Reorder immediately on page change.
1322 onDragOver(mDragObject, 1);
1323 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001324 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001325}