blob: 03a9019e8085f9a7f98d5eb0485219bd642adccf [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 Goyalf4066152015-04-15 09:42:19 -070052import com.android.launcher3.DragController.DragListener;
Daniel Sandler325dc232013-06-05 22:57:57 -040053import com.android.launcher3.FolderInfo.FolderListener;
Sunny Goyalfa401a12015-04-10 13:45:42 -070054import com.android.launcher3.UninstallDropTarget.UninstallSource;
Sunny Goyalbc753352015-03-05 09:40:44 -080055import com.android.launcher3.Workspace.ItemOperator;
Adam Cohen091440a2015-03-18 14:16:05 -070056import com.android.launcher3.util.Thunk;
Romain Guyedcce092010-03-04 13:03:17 -080057
Adam Cohenc0dcf592011-06-01 15:30:43 -070058import java.util.ArrayList;
Adam Cohen3bf84d32012-05-07 20:17:14 -070059import java.util.Collections;
Adam Cohenc0dcf592011-06-01 15:30:43 -070060
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061/**
62 * Represents a set of icons chosen by the user or generated by the system.
63 */
Adam Cohen8dfcba42011-07-07 16:38:18 -070064public class Folder extends LinearLayout implements DragSource, View.OnClickListener,
Adam Cohenea0818d2011-09-30 20:06:58 -070065 View.OnLongClickListener, DropTarget, FolderListener, TextView.OnEditorActionListener,
Sunny Goyalfa401a12015-04-10 13:45:42 -070066 View.OnFocusChangeListener, DragListener, UninstallSource {
Adam Cohendf2cc412011-04-27 16:56:57 -070067 private static final String TAG = "Launcher.Folder";
68
Sunny Goyalc3a609f2015-02-26 17:43:50 -080069 /**
70 * We avoid measuring {@link #mContentWrapper} with a 0 width or height, as this
71 * results in CellLayout being measured as UNSPECIFIED, which it does not support.
72 */
73 private static final int MIN_CONTENT_DIMEN = 5;
Adam Cohen4eac29a2011-07-11 17:53:37 -070074
Adam Cohendf2cc412011-04-27 16:56:57 -070075 static final int STATE_NONE = -1;
76 static final int STATE_SMALL = 0;
77 static final int STATE_ANIMATING = 1;
78 static final int STATE_OPEN = 2;
79
Sunny Goyal48461932015-03-09 17:41:09 -070080 /**
Sunny Goyal48461932015-03-09 17:41:09 -070081 * Time for which the scroll hint is shown before automatically changing page.
82 */
83 public static final int SCROLL_HINT_DURATION = DragController.SCROLL_DELAY;
84
85 /**
86 * Fraction of icon width which behave as scroll region.
87 */
88 private static final float ICON_OVERSCROLL_WIDTH_FACTOR = 0.45f;
89
Adam Cohenf0f4eda2013-06-06 21:27:03 -070090 private static final int REORDER_DELAY = 250;
Adam Cohen5d518fa2013-12-05 14:16:23 -080091 private static final int ON_EXIT_CLOSE_DELAY = 400;
Sunny Goyalc3a609f2015-02-26 17:43:50 -080092 private static final Rect sTempRect = new Rect();
93
94 private static String sDefaultFolderName;
95 private static String sHintText;
96
97 private final Alarm mReorderAlarm = new Alarm();
98 private final Alarm mOnExitAlarm = new Alarm();
Sunny Goyalb8634152015-04-09 14:17:14 -070099 private final Alarm mOnScrollHintAlarm = new Alarm();
100 @Thunk final Alarm mScrollPauseAlarm = new Alarm();
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800101
Adam Cohen091440a2015-03-18 14:16:05 -0700102 @Thunk final ArrayList<View> mItemsInReadingOrder = new ArrayList<View>();
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800103
104 private final int mExpandDuration;
105 private final int mMaterialExpandDuration;
106 private final int mMaterialExpandStagger;
107
108 private final InputMethodManager mInputMethodManager;
109
110 protected final Launcher mLauncher;
111 protected DragController mDragController;
112 protected FolderInfo mInfo;
113
Adam Cohen091440a2015-03-18 14:16:05 -0700114 @Thunk FolderIcon mFolderIcon;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800115
Sunny Goyalb8634152015-04-09 14:17:14 -0700116 @Thunk FolderPagedView mContent;
Adam Cohen091440a2015-03-18 14:16:05 -0700117 @Thunk View mContentWrapper;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800118 FolderEditText mFolderName;
119
Sunny Goyal290800b2015-03-05 11:33:33 -0800120 private View mFooter;
121 private int mFooterHeight;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800122
123 // Cell ranks used for drag and drop
Adam Cohen091440a2015-03-18 14:16:05 -0700124 @Thunk int mTargetRank, mPrevTargetRank, mEmptyCellRank;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800125
Adam Cohen091440a2015-03-18 14:16:05 -0700126 @Thunk int mState = STATE_NONE;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800127 private boolean mRearrangeOnClose = false;
Adam Cohen7c693212011-05-18 15:26:57 -0700128 boolean mItemsInvalidated = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700129 private ShortcutInfo mCurrentDragInfo;
130 private View mCurrentDragView;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800131 private boolean mIsExternalDrag;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700132 boolean mSuppressOnAdd = false;
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700133 private boolean mDragInProgress = false;
134 private boolean mDeleteFolderOnDropCompleted = false;
135 private boolean mSuppressFolderDeletion = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700136 private boolean mItemAddedBackToSelfViaIcon = false;
Adam Cohen091440a2015-03-18 14:16:05 -0700137 @Thunk float mFolderIconPivotX;
138 @Thunk float mFolderIconPivotY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700139 private boolean mIsEditingName = false;
Adam Cohen1960ea42013-11-12 11:33:14 +0000140
Adam Cohenfb91f302012-06-11 15:45:18 -0700141 private boolean mDestroyed;
142
Adam Cohen091440a2015-03-18 14:16:05 -0700143 @Thunk Runnable mDeferredAction;
Michael Jurka1e2f4652013-07-08 18:03:46 -0700144 private boolean mDeferDropAfterUninstall;
145 private boolean mUninstallSuccessful;
146
Sunny Goyal48461932015-03-09 17:41:09 -0700147 // Folder scrolling
148 private int mScrollAreaOffset;
Sunny Goyal48461932015-03-09 17:41:09 -0700149
Adam Cohen091440a2015-03-18 14:16:05 -0700150 @Thunk int mScrollHintDir = DragController.SCROLL_NONE;
151 @Thunk int mCurrentScrollDir = DragController.SCROLL_NONE;
Sunny Goyal48461932015-03-09 17:41:09 -0700152
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153 /**
154 * Used to inflate the Workspace from XML.
155 *
156 * @param context The application's context.
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800157 * @param attrs The attributes set containing the Workspace's customization values.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158 */
159 public Folder(Context context, AttributeSet attrs) {
160 super(context, attrs);
161 setAlwaysDrawnWithCacheEnabled(false);
Adam Cohen76fc0852011-06-17 13:26:23 -0700162 mInputMethodManager = (InputMethodManager)
Michael Jurka8b805b12012-04-18 14:23:14 -0700163 getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
Adam Cohen76fc0852011-06-17 13:26:23 -0700164
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800165 Resources res = getResources();
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700166 mExpandDuration = res.getInteger(R.integer.config_folderExpandDuration);
167 mMaterialExpandDuration = res.getInteger(R.integer.config_materialFolderExpandDuration);
168 mMaterialExpandStagger = res.getInteger(R.integer.config_materialFolderExpandStagger);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700169
170 if (sDefaultFolderName == null) {
171 sDefaultFolderName = res.getString(R.string.folder_name);
172 }
Adam Cohena65beee2011-06-27 21:32:23 -0700173 if (sHintText == null) {
174 sHintText = res.getString(R.string.folder_hint_text);
175 }
Adam Cohen4eac29a2011-07-11 17:53:37 -0700176 mLauncher = (Launcher) context;
Adam Cohenac56cff2011-09-28 20:45:37 -0700177 // We need this view to be focusable in touch mode so that when text editing of the folder
178 // name is complete, we have something to focus on, thus hiding the cursor and giving
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800179 // reliable behavior when clicking the text field (since it will always gain focus on click).
Adam Cohenac56cff2011-09-28 20:45:37 -0700180 setFocusableInTouchMode(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181 }
182
183 @Override
184 protected void onFinishInflate() {
185 super.onFinishInflate();
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800186 mContentWrapper = findViewById(R.id.folder_content_wrapper);
Sunny Goyalb8634152015-04-09 14:17:14 -0700187 mContent = (FolderPagedView) findViewById(R.id.folder_content);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800188 mContent.setFolder(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700189
Adam Cohenac56cff2011-09-28 20:45:37 -0700190 mFolderName = (FolderEditText) findViewById(R.id.folder_name);
191 mFolderName.setFolder(this);
Adam Cohenea0818d2011-09-30 20:06:58 -0700192 mFolderName.setOnFocusChangeListener(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700193
Adam Cohen76fc0852011-06-17 13:26:23 -0700194 // We disable action mode for now since it messes up the view on phones
195 mFolderName.setCustomSelectionActionModeCallback(mActionModeCallback);
Adam Cohen76fc0852011-06-17 13:26:23 -0700196 mFolderName.setOnEditorActionListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700197 mFolderName.setSelectAllOnFocus(true);
Adam Cohen7a14d0b2011-06-24 11:36:35 -0700198 mFolderName.setInputType(mFolderName.getInputType() |
199 InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800200
Sunny Goyalb8634152015-04-09 14:17:14 -0700201 mFooter = findViewById(R.id.folder_footer);
202 updateFooterHeight();
203 }
204
205 public void updateFooterHeight() {
Sunny Goyal290800b2015-03-05 11:33:33 -0800206 // We find out how tall footer wants to be (it is set to wrap_content), so that
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800207 // we can allocate the appropriate amount of space for it.
208 int measureSpec = MeasureSpec.UNSPECIFIED;
Sunny Goyal290800b2015-03-05 11:33:33 -0800209 mFooter.measure(measureSpec, measureSpec);
210 mFooterHeight = mFooter.getMeasuredHeight();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800211 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700212
Adam Cohen76fc0852011-06-17 13:26:23 -0700213 private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
214 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
215 return false;
216 }
217
218 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
219 return false;
220 }
221
222 public void onDestroyActionMode(ActionMode mode) {
223 }
224
225 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
226 return false;
227 }
228 };
229
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800230 public void onClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700231 Object tag = v.getTag();
232 if (tag instanceof ShortcutInfo) {
Adam Cohenb5fe60c2013-06-06 22:03:51 -0700233 mLauncher.onClick(v);
Adam Cohendf2cc412011-04-27 16:56:57 -0700234 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800235 }
236
237 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -0700238 // Return if global dragging is not enabled
239 if (!mLauncher.isDraggingEnabled()) return true;
240
Adam Cohendf2cc412011-04-27 16:56:57 -0700241 Object tag = v.getTag();
242 if (tag instanceof ShortcutInfo) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700243 ShortcutInfo item = (ShortcutInfo) tag;
244 if (!v.isInTouchMode()) {
245 return false;
246 }
247
Winson Chungb745afb2015-03-02 11:51:23 -0800248 mLauncher.getWorkspace().beginDragShared(v, new Point(), this, false);
Adam Cohen76078c42011-06-09 15:06:52 -0700249
250 mCurrentDragInfo = item;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800251 mEmptyCellRank = item.rank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700252 mCurrentDragView = v;
Adam Cohenfc53cd22011-07-20 15:45:11 -0700253
Sunny Goyal290800b2015-03-05 11:33:33 -0800254 mContent.removeItem(mCurrentDragView);
Adam Cohenfc53cd22011-07-20 15:45:11 -0700255 mInfo.remove(mCurrentDragInfo);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700256 mDragInProgress = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700257 mItemAddedBackToSelfViaIcon = false;
Adam Cohendf2cc412011-04-27 16:56:57 -0700258 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800259 return true;
260 }
261
Adam Cohen76fc0852011-06-17 13:26:23 -0700262 public boolean isEditingName() {
263 return mIsEditingName;
264 }
265
266 public void startEditingFolderName() {
Adam Cohena65beee2011-06-27 21:32:23 -0700267 mFolderName.setHint("");
Adam Cohen76fc0852011-06-17 13:26:23 -0700268 mIsEditingName = true;
269 }
270
271 public void dismissEditingName() {
272 mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
273 doneEditingFolderName(true);
274 }
275
276 public void doneEditingFolderName(boolean commit) {
Adam Cohena65beee2011-06-27 21:32:23 -0700277 mFolderName.setHint(sHintText);
Adam Cohen1df26a32011-08-12 16:15:23 -0700278 // Convert to a string here to ensure that no other state associated with the text field
279 // gets saved.
Adam Cohen3371da02011-10-25 21:38:29 -0700280 String newTitle = mFolderName.getText().toString();
281 mInfo.setTitle(newTitle);
Adam Cohen76fc0852011-06-17 13:26:23 -0700282 LauncherModel.updateItemInDatabase(mLauncher, mInfo);
Adam Cohenac56cff2011-09-28 20:45:37 -0700283
Adam Cohen3371da02011-10-25 21:38:29 -0700284 if (commit) {
285 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700286 String.format(getContext().getString(R.string.folder_renamed), newTitle));
Adam Cohen3371da02011-10-25 21:38:29 -0700287 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700288 // In order to clear the focus from the text field, we set the focus on ourself. This
289 // ensures that every time the field is clicked, focus is gained, giving reliable behavior.
290 requestFocus();
291
Adam Cohene601a432011-07-26 21:51:30 -0700292 Selection.setSelection((Spannable) mFolderName.getText(), 0, 0);
Adam Cohen76fc0852011-06-17 13:26:23 -0700293 mIsEditingName = false;
294 }
295
296 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
297 if (actionId == EditorInfo.IME_ACTION_DONE) {
298 dismissEditingName();
299 return true;
300 }
301 return false;
302 }
303
304 public View getEditTextRegion() {
305 return mFolderName;
306 }
307
Adam Cohen0c872ba2011-05-05 10:34:16 -0700308 /**
309 * We need to handle touch events to prevent them from falling through to the workspace below.
310 */
311 @Override
312 public boolean onTouchEvent(MotionEvent ev) {
313 return true;
314 }
315
Joe Onorato00acb122009-08-04 16:04:30 -0400316 public void setDragController(DragController dragController) {
317 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800318 }
319
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800320 public void setFolderIcon(FolderIcon icon) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700321 mFolderIcon = icon;
322 }
323
Adam Cohen3371da02011-10-25 21:38:29 -0700324 @Override
325 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
326 // When the folder gets focus, we don't want to announce the list of items.
327 return true;
328 }
329
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800330 /**
331 * @return the FolderInfo object associated with this folder
332 */
333 FolderInfo getInfo() {
334 return mInfo;
335 }
336
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800337 void bind(FolderInfo info) {
338 mInfo = info;
Adam Cohendf2cc412011-04-27 16:56:57 -0700339 ArrayList<ShortcutInfo> children = info.contents;
Sunny Goyal08f72612015-01-05 13:41:43 -0800340 Collections.sort(children, Utilities.RANK_COMPARATOR);
Sunny Goyal08f72612015-01-05 13:41:43 -0800341
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800342 ArrayList<ShortcutInfo> overflow = mContent.bindItems(children);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700343
Jason Monk4ff73882014-04-24 16:48:07 -0400344 // If our folder has too many items we prune them from the list. This is an issue
Adam Cohenc508b2d2011-06-28 14:41:44 -0700345 // when upgrading from the old Folders implementation which could contain an unlimited
346 // number of items.
347 for (ShortcutInfo item: overflow) {
348 mInfo.remove(item);
349 LauncherModel.deleteItemFromDatabase(mLauncher, item);
350 }
351
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800352 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
353 if (lp == null) {
354 lp = new DragLayer.LayoutParams(0, 0);
355 lp.customPosition = true;
356 setLayoutParams(lp);
357 }
358 centerAboutIcon();
359
Adam Cohen4dbe6d92011-05-18 17:14:15 -0700360 mItemsInvalidated = true;
Adam Cohenac56cff2011-09-28 20:45:37 -0700361 updateTextViewFocus();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700362 mInfo.addListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700363
Adam Cohenafb01ee2011-06-23 15:38:03 -0700364 if (!sDefaultFolderName.contentEquals(mInfo.title)) {
Adam Cohen4ef610f2011-06-21 22:37:36 -0700365 mFolderName.setText(mInfo.title);
366 } else {
367 mFolderName.setText("");
368 }
Winson Chung33231f52013-12-09 16:57:45 -0800369
370 // In case any children didn't come across during loading, clean up the folder accordingly
371 mFolderIcon.post(new Runnable() {
372 public void run() {
373 if (getItemCount() <= 1) {
374 replaceFolderWithFinalItem();
375 }
376 }
377 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700378 }
379
380 /**
381 * Creates a new UserFolder, inflated from R.layout.user_folder.
382 *
383 * @param context The application's context.
384 *
385 * @return A new UserFolder.
386 */
387 static Folder fromXml(Context context) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700388 return (Folder) LayoutInflater.from(context).inflate(R.layout.user_folder, null);
Adam Cohendf2cc412011-04-27 16:56:57 -0700389 }
390
391 /**
392 * This method is intended to make the UserFolder to be visually identical in size and position
393 * to its associated FolderIcon. This allows for a seamless transition into the expanded state.
394 */
395 private void positionAndSizeAsIcon() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700396 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800397 setScaleX(0.8f);
398 setScaleY(0.8f);
399 setAlpha(0f);
Adam Cohendf2cc412011-04-27 16:56:57 -0700400 mState = STATE_SMALL;
401 }
402
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700403 private void prepareReveal() {
404 setScaleX(1f);
405 setScaleY(1f);
406 setAlpha(1f);
407 mState = STATE_SMALL;
408 }
409
Adam Cohendf2cc412011-04-27 16:56:57 -0700410 public void animateOpen() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700411 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen6441de02011-12-14 14:25:32 -0800412
Sunny Goyalb8634152015-04-09 14:17:14 -0700413 mContent.completePendingPageChanges();
Sunny Goyal8167dc22015-04-27 13:44:01 -0700414 if (!mDragInProgress) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700415 // Open on the first page.
416 mContent.snapToPageImmediately(0);
Sunny Goyal48461932015-03-09 17:41:09 -0700417 }
418
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700419 Animator openFolderAnim = null;
420 final Runnable onCompleteRunnable;
Kenny Guyd794a3f2014-09-16 15:17:58 +0100421 if (!Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700422 positionAndSizeAsIcon();
423 centerAboutIcon();
424
425 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1);
426 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
427 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
428 final ObjectAnimator oa =
429 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
430 oa.setDuration(mExpandDuration);
431 openFolderAnim = oa;
432
433 setLayerType(LAYER_TYPE_HARDWARE, null);
434 onCompleteRunnable = new Runnable() {
435 @Override
436 public void run() {
437 setLayerType(LAYER_TYPE_NONE, null);
438 }
439 };
440 } else {
441 prepareReveal();
442 centerAboutIcon();
443
444 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
445 int height = getFolderHeight();
446
447 float transX = - 0.075f * (width / 2 - getPivotX());
448 float transY = - 0.075f * (height / 2 - getPivotY());
449 setTranslationX(transX);
450 setTranslationY(transY);
451 PropertyValuesHolder tx = PropertyValuesHolder.ofFloat("translationX", transX, 0);
452 PropertyValuesHolder ty = PropertyValuesHolder.ofFloat("translationY", transY, 0);
453
454 int rx = (int) Math.max(Math.max(width - getPivotX(), 0), getPivotX());
455 int ry = (int) Math.max(Math.max(height - getPivotY(), 0), getPivotY());
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700456 float radius = (float) Math.hypot(rx, ry);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700457 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
458 Animator reveal = LauncherAnimUtils.createCircularReveal(this, (int) getPivotX(),
459 (int) getPivotY(), 0, radius);
460 reveal.setDuration(mMaterialExpandDuration);
461 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
462
Sunny Goyalbc753352015-03-05 09:40:44 -0800463 mContentWrapper.setAlpha(0f);
464 Animator iconsAlpha = LauncherAnimUtils.ofFloat(mContentWrapper, "alpha", 0f, 1f);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700465 iconsAlpha.setDuration(mMaterialExpandDuration);
466 iconsAlpha.setStartDelay(mMaterialExpandStagger);
467 iconsAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
468
Sunny Goyal290800b2015-03-05 11:33:33 -0800469 mFooter.setAlpha(0f);
470 Animator textAlpha = LauncherAnimUtils.ofFloat(mFooter, "alpha", 0f, 1f);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700471 textAlpha.setDuration(mMaterialExpandDuration);
472 textAlpha.setStartDelay(mMaterialExpandStagger);
473 textAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
474
475 Animator drift = LauncherAnimUtils.ofPropertyValuesHolder(this, tx, ty);
476 drift.setDuration(mMaterialExpandDuration);
477 drift.setStartDelay(mMaterialExpandStagger);
478 drift.setInterpolator(new LogDecelerateInterpolator(60, 0));
479
480 anim.play(drift);
481 anim.play(iconsAlpha);
482 anim.play(textAlpha);
483 anim.play(reveal);
484
485 openFolderAnim = anim;
486
Sunny Goyalbc753352015-03-05 09:40:44 -0800487 mContentWrapper.setLayerType(LAYER_TYPE_HARDWARE, null);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700488 onCompleteRunnable = new Runnable() {
489 @Override
490 public void run() {
Sunny Goyalbc753352015-03-05 09:40:44 -0800491 mContentWrapper.setLayerType(LAYER_TYPE_NONE, null);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700492 }
493 };
494 }
495 openFolderAnim.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700496 @Override
497 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700498 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Sunny Goyalbc753352015-03-05 09:40:44 -0800499 mContent.getAccessibilityDescription());
Adam Cohendf2cc412011-04-27 16:56:57 -0700500 mState = STATE_ANIMATING;
501 }
502 @Override
503 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700504 mState = STATE_OPEN;
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700505
506 if (onCompleteRunnable != null) {
507 onCompleteRunnable.run();
508 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800509
Sunny Goyalbc753352015-03-05 09:40:44 -0800510 mContent.setFocusOnFirstChild();
Adam Cohendf2cc412011-04-27 16:56:57 -0700511 }
512 });
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700513 openFolderAnim.start();
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800514
515 // Make sure the folder picks up the last drag move even if the finger doesn't move.
516 if (mDragController.isDragging()) {
517 mDragController.forceTouchMove();
518 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700519
Sunny Goyalb8634152015-04-09 14:17:14 -0700520 FolderPagedView pages = (FolderPagedView) mContent;
521 pages.verifyVisibleHighResIcons(pages.getNextPage());
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800522 }
523
524 public void beginExternalDrag(ShortcutInfo item) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800525 mCurrentDragInfo = item;
Sunny Goyal5d85c442015-03-10 13:14:47 -0700526 mEmptyCellRank = mContent.allocateRankForNewItem(item);
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800527 mIsExternalDrag = true;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800528 mDragInProgress = true;
Sunny Goyalb8634152015-04-09 14:17:14 -0700529
Sunny Goyalf4066152015-04-15 09:42:19 -0700530 // Since this folder opened by another controller, it might not get onDrop or
531 // onDropComplete. Perform cleanup once drag-n-drop ends.
532 mDragController.addDragListener(this);
533 }
534
535 @Override
536 public void onDragStart(DragSource source, Object info, int dragAction) { }
537
538 @Override
539 public void onDragEnd() {
540 if (mIsExternalDrag && mDragInProgress) {
541 completeDragExit();
542 }
543 mDragController.removeDragListener(this);
Adam Cohendf2cc412011-04-27 16:56:57 -0700544 }
545
Adam Cohen091440a2015-03-18 14:16:05 -0700546 @Thunk void sendCustomAccessibilityEvent(int type, String text) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700547 AccessibilityManager accessibilityManager = (AccessibilityManager)
548 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
549 if (accessibilityManager.isEnabled()) {
Adam Cohen3371da02011-10-25 21:38:29 -0700550 AccessibilityEvent event = AccessibilityEvent.obtain(type);
551 onInitializeAccessibilityEvent(event);
552 event.getText().add(text);
Michael Jurka8b805b12012-04-18 14:23:14 -0700553 accessibilityManager.sendAccessibilityEvent(event);
Adam Cohen3371da02011-10-25 21:38:29 -0700554 }
555 }
556
Adam Cohendf2cc412011-04-27 16:56:57 -0700557 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700558 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800559 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
560 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
561 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
Michael Jurka032e6ba2013-04-22 15:08:42 +0200562 final ObjectAnimator oa =
Michael Jurka2ecf9952012-06-18 12:52:28 -0700563 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohendf2cc412011-04-27 16:56:57 -0700564
Adam Cohen2801caf2011-05-13 20:57:39 -0700565 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700566 @Override
567 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700568 onCloseComplete();
Michael Jurka0121c3e2012-05-31 08:36:04 -0700569 setLayerType(LAYER_TYPE_NONE, null);
Adam Cohen2801caf2011-05-13 20:57:39 -0700570 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700571 }
572 @Override
573 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700574 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700575 getContext().getString(R.string.folder_closed));
Adam Cohendf2cc412011-04-27 16:56:57 -0700576 mState = STATE_ANIMATING;
577 }
578 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700579 oa.setDuration(mExpandDuration);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700580 setLayerType(LAYER_TYPE_HARDWARE, null);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100581 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700582 }
583
Adam Cohencb3382b2011-05-24 14:07:08 -0700584 public boolean acceptDrop(DragObject d) {
585 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700586 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700587 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
588 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
589 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700590 }
591
Adam Cohencb3382b2011-05-24 14:07:08 -0700592 public void onDragEnter(DragObject d) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800593 mPrevTargetRank = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700594 mOnExitAlarm.cancelAlarm();
Sunny Goyalb8634152015-04-09 14:17:14 -0700595 // Get the area offset such that the folder only closes if half the drag icon width
596 // is outside the folder area
597 mScrollAreaOffset = d.dragView.getDragRegionWidth() / 2 - d.xOffset;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700598 }
599
600 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
601 public void onAlarm(Alarm alarm) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800602 mContent.realTimeReorder(mEmptyCellRank, mTargetRank);
603 mEmptyCellRank = mTargetRank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700604 }
605 };
606
Sunny Goyalc46bfef2015-01-05 12:40:08 -0800607 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohen2374abf2013-04-16 14:56:57 -0700608 public boolean isLayoutRtl() {
609 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
610 }
611
Sunny Goyal48461932015-03-09 17:41:09 -0700612 @Override
Adam Cohencb3382b2011-05-24 14:07:08 -0700613 public void onDragOver(DragObject d) {
Sunny Goyal48461932015-03-09 17:41:09 -0700614 onDragOver(d, REORDER_DELAY);
615 }
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700616
Sunny Goyal48461932015-03-09 17:41:09 -0700617 private int getTargetRank(DragObject d, float[] recycle) {
618 recycle = d.getVisualCenter(recycle);
619 return mContent.findNearestArea(
620 (int) recycle[0] - getPaddingLeft(), (int) recycle[1] - getPaddingTop());
621 }
622
Adam Cohen091440a2015-03-18 14:16:05 -0700623 @Thunk void onDragOver(DragObject d, int reorderDelay) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700624 if (mScrollPauseAlarm.alarmPending()) {
Sunny Goyal48461932015-03-09 17:41:09 -0700625 return;
626 }
627 final float[] r = new float[2];
628 mTargetRank = getTargetRank(d, r);
629
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800630 if (mTargetRank != mPrevTargetRank) {
Alan Viverette4cda5b72013-08-28 17:53:41 -0700631 mReorderAlarm.cancelAlarm();
Sunny Goyalc46bfef2015-01-05 12:40:08 -0800632 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
633 mReorderAlarm.setAlarm(REORDER_DELAY);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800634 mPrevTargetRank = mTargetRank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700635 }
Sunny Goyal48461932015-03-09 17:41:09 -0700636
Sunny Goyal48461932015-03-09 17:41:09 -0700637 float x = r[0];
Sunny Goyalb8634152015-04-09 14:17:14 -0700638 int currentPage = mContent.getNextPage();
Sunny Goyal48461932015-03-09 17:41:09 -0700639
Sunny Goyalb8634152015-04-09 14:17:14 -0700640 float cellOverlap = mContent.getCurrentCellLayout().getCellWidth()
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700641 * ICON_OVERSCROLL_WIDTH_FACTOR;
642 boolean isOutsideLeftEdge = x < cellOverlap;
643 boolean isOutsideRightEdge = x > (getWidth() - cellOverlap);
Sunny Goyal48461932015-03-09 17:41:09 -0700644
Sunny Goyalb8634152015-04-09 14:17:14 -0700645 if (currentPage > 0 && (mContent.rtlLayout ? isOutsideRightEdge : isOutsideLeftEdge)) {
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700646 showScrollHint(DragController.SCROLL_LEFT, d);
Sunny Goyalb8634152015-04-09 14:17:14 -0700647 } else if (currentPage < (mContent.getPageCount() - 1)
648 && (mContent.rtlLayout ? isOutsideLeftEdge : isOutsideRightEdge)) {
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700649 showScrollHint(DragController.SCROLL_RIGHT, d);
Sunny Goyal48461932015-03-09 17:41:09 -0700650 } else {
651 mOnScrollHintAlarm.cancelAlarm();
652 if (mScrollHintDir != DragController.SCROLL_NONE) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700653 mContent.clearScrollHint();
Sunny Goyal48461932015-03-09 17:41:09 -0700654 mScrollHintDir = DragController.SCROLL_NONE;
655 }
656 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700657 }
658
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700659 private void showScrollHint(int direction, DragObject d) {
660 // Show scroll hint on the right
661 if (mScrollHintDir != direction) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700662 mContent.showScrollHint(direction);
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700663 mScrollHintDir = direction;
664 }
665
666 // Set alarm for when the hint is complete
667 if (!mOnScrollHintAlarm.alarmPending() || mCurrentScrollDir != direction) {
668 mCurrentScrollDir = direction;
669 mOnScrollHintAlarm.cancelAlarm();
670 mOnScrollHintAlarm.setOnAlarmListener(new OnScrollHintListener(d));
671 mOnScrollHintAlarm.setAlarm(SCROLL_HINT_DURATION);
672
673 mReorderAlarm.cancelAlarm();
674 mTargetRank = mEmptyCellRank;
675 }
676 }
677
Adam Cohenbfbfd262011-06-13 16:55:12 -0700678 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
679 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700680 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700681 }
682 };
683
Adam Cohen95bb8002011-07-03 23:40:28 -0700684 public void completeDragExit() {
Sunny Goyalf4066152015-04-15 09:42:19 -0700685 if (mInfo.opened) {
686 mLauncher.closeFolder();
687 mRearrangeOnClose = true;
688 } else {
689 rearrangeChildren();
690 }
Adam Cohen3e8f8112011-07-02 18:03:00 -0700691 mCurrentDragInfo = null;
692 mCurrentDragView = null;
693 mSuppressOnAdd = false;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800694 mIsExternalDrag = false;
Adam Cohen3e8f8112011-07-02 18:03:00 -0700695 }
696
Adam Cohencb3382b2011-05-24 14:07:08 -0700697 public void onDragExit(DragObject d) {
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700698 // We only close the folder if this is a true drag exit, ie. not because
699 // a drop has occurred above the folder.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700700 if (!d.dragComplete) {
701 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
702 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
703 }
704 mReorderAlarm.cancelAlarm();
Sunny Goyal48461932015-03-09 17:41:09 -0700705
Sunny Goyalb8634152015-04-09 14:17:14 -0700706 mOnScrollHintAlarm.cancelAlarm();
707 mScrollPauseAlarm.cancelAlarm();
708 if (mScrollHintDir != DragController.SCROLL_NONE) {
709 mContent.clearScrollHint();
710 mScrollHintDir = DragController.SCROLL_NONE;
Sunny Goyal48461932015-03-09 17:41:09 -0700711 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700712 }
713
Michael Jurka1e2f4652013-07-08 18:03:46 -0700714 public void onDropCompleted(final View target, final DragObject d,
715 final boolean isFlingToDelete, final boolean success) {
716 if (mDeferDropAfterUninstall) {
Michael Jurkaf3007582013-08-21 14:33:57 +0200717 Log.d(TAG, "Deferred handling drop because waiting for uninstall.");
Michael Jurka1e2f4652013-07-08 18:03:46 -0700718 mDeferredAction = new Runnable() {
719 public void run() {
720 onDropCompleted(target, d, isFlingToDelete, success);
721 mDeferredAction = null;
722 }
723 };
724 return;
725 }
726
727 boolean beingCalledAfterUninstall = mDeferredAction != null;
728 boolean successfulDrop =
729 success && (!beingCalledAfterUninstall || mUninstallSuccessful);
Winson Chung5f8afe62013-08-12 16:19:28 -0700730
Michael Jurka1e2f4652013-07-08 18:03:46 -0700731 if (successfulDrop) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800732 if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon && target != this) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700733 replaceFolderWithFinalItem();
734 }
735 } else {
736 // The drag failed, we need to return the item to the folder
Sunny Goyalb8c663c2015-04-23 11:43:48 -0700737 ShortcutInfo info = (ShortcutInfo) d.dragInfo;
738 View icon = (mCurrentDragView != null && mCurrentDragView.getTag() == info)
739 ? mCurrentDragView : mContent.createNewView(info);
740 ArrayList<View> views = getItemsInReadingOrder();
741 views.add(info.rank, icon);
742 mContent.arrangeChildren(views, views.size());
743 mItemsInvalidated = true;
744
745 mSuppressOnAdd = true;
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700746 mFolderIcon.onDrop(d);
Sunny Goyalb8c663c2015-04-23 11:43:48 -0700747 mSuppressOnAdd = false;
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700748 }
749
750 if (target != this) {
751 if (mOnExitAlarm.alarmPending()) {
752 mOnExitAlarm.cancelAlarm();
Michael Jurka54554252013-08-01 12:52:23 +0200753 if (!successfulDrop) {
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700754 mSuppressFolderDeletion = true;
755 }
Sunny Goyal5d85c442015-03-10 13:14:47 -0700756 mScrollPauseAlarm.cancelAlarm();
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700757 completeDragExit();
758 }
Adam Cohen9c58d822013-12-13 17:18:10 -0800759 }
760
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700761 mDeleteFolderOnDropCompleted = false;
762 mDragInProgress = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700763 mItemAddedBackToSelfViaIcon = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700764 mCurrentDragInfo = null;
765 mCurrentDragView = null;
766 mSuppressOnAdd = false;
Adam Cohen4045eb72011-10-06 11:44:26 -0700767
768 // Reordering may have occured, and we need to save the new item locations. We do this once
769 // at the end to prevent unnecessary database operations.
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700770 updateItemLocationsInDatabaseBatch();
Adam Cohen4045eb72011-10-06 11:44:26 -0700771 }
772
Sunny Goyalfa401a12015-04-10 13:45:42 -0700773 @Override
Michael Jurka1e2f4652013-07-08 18:03:46 -0700774 public void deferCompleteDropAfterUninstallActivity() {
775 mDeferDropAfterUninstall = true;
776 }
777
Sunny Goyalfa401a12015-04-10 13:45:42 -0700778 @Override
Michael Jurka1e2f4652013-07-08 18:03:46 -0700779 public void onUninstallActivityReturned(boolean success) {
780 mDeferDropAfterUninstall = false;
781 mUninstallSuccessful = success;
782 if (mDeferredAction != null) {
783 mDeferredAction.run();
784 }
785 }
786
Winson Chunga48487a2012-03-20 16:19:37 -0700787 @Override
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800788 public float getIntrinsicIconScaleFactor() {
789 return 1f;
790 }
791
792 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800793 public boolean supportsFlingToDelete() {
794 return true;
795 }
796
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000797 @Override
798 public boolean supportsAppInfoDropTarget() {
799 return false;
800 }
801
802 @Override
803 public boolean supportsDeleteDropTarget() {
804 return true;
805 }
806
Winson Chunga48487a2012-03-20 16:19:37 -0700807 public void onFlingToDelete(DragObject d, int x, int y, PointF vec) {
808 // Do nothing
809 }
810
811 @Override
812 public void onFlingToDeleteCompleted() {
813 // Do nothing
814 }
815
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700816 private void updateItemLocationsInDatabaseBatch() {
817 ArrayList<View> list = getItemsInReadingOrder();
818 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
819 for (int i = 0; i < list.size(); i++) {
820 View v = list.get(i);
821 ItemInfo info = (ItemInfo) v.getTag();
Sunny Goyal08f72612015-01-05 13:41:43 -0800822 info.rank = i;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700823 items.add(info);
824 }
825
826 LauncherModel.moveItemsInDatabase(mLauncher, items, mInfo.id, 0);
827 }
828
Adam Cohene25af792013-06-06 23:08:25 -0700829 public void addItemLocationsInDatabase() {
830 ArrayList<View> list = getItemsInReadingOrder();
831 for (int i = 0; i < list.size(); i++) {
832 View v = list.get(i);
833 ItemInfo info = (ItemInfo) v.getTag();
834 LauncherModel.addItemToDatabase(mLauncher, info, mInfo.id, 0,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700835 info.cellX, info.cellY);
Adam Cohene25af792013-06-06 23:08:25 -0700836 }
837 }
838
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700839 public void notifyDrop() {
840 if (mDragInProgress) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700841 mItemAddedBackToSelfViaIcon = true;
Adam Cohen76078c42011-06-09 15:06:52 -0700842 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700843 }
844
845 public boolean isDropEnabled() {
846 return true;
847 }
848
Adam Cohen2801caf2011-05-13 20:57:39 -0700849 public boolean isFull() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800850 return mContent.isFull();
Adam Cohen2801caf2011-05-13 20:57:39 -0700851 }
852
853 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700854 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700855
Winson Chung892c74d2013-08-22 16:15:50 -0700856 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700857 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700858 int height = getFolderHeight();
Adam Cohen2801caf2011-05-13 20:57:39 -0700859
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800860 float scale = parent.getDescendantRectRelativeToSelf(mFolderIcon, sTempRect);
Adam Cohen8e776a62011-06-28 18:10:06 -0700861
Winson Chungaf40f202013-09-18 18:26:31 -0700862 LauncherAppState app = LauncherAppState.getInstance();
863 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
864
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800865 int centerX = (int) (sTempRect.left + sTempRect.width() * scale / 2);
866 int centerY = (int) (sTempRect.top + sTempRect.height() * scale / 2);
Adam Cohen2801caf2011-05-13 20:57:39 -0700867 int centeredLeft = centerX - width / 2;
868 int centeredTop = centerY - height / 2;
Winson Chung3057b1c2013-10-10 17:35:35 -0700869 int currentPage = mLauncher.getWorkspace().getNextPage();
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800870 // In case the workspace is scrolling, we need to use the final scroll to compute
871 // the folders bounds.
872 mLauncher.getWorkspace().setFinalScrollForPageChange(currentPage);
Adam Cohen35e7e642011-07-17 14:47:18 -0700873 // We first fetch the currently visible CellLayoutChildren
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800874 CellLayout currentLayout = (CellLayout) mLauncher.getWorkspace().getChildAt(currentPage);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700875 ShortcutAndWidgetContainer boundingLayout = currentLayout.getShortcutsAndWidgets();
Adam Cohen35e7e642011-07-17 14:47:18 -0700876 Rect bounds = new Rect();
877 parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800878 // We reset the workspaces scroll
879 mLauncher.getWorkspace().resetFinalScrollForPageChange(currentPage);
Adam Cohen2801caf2011-05-13 20:57:39 -0700880
Adam Cohen35e7e642011-07-17 14:47:18 -0700881 // We need to bound the folder to the currently visible CellLayoutChildren
882 int left = Math.min(Math.max(bounds.left, centeredLeft),
883 bounds.left + bounds.width() - width);
884 int top = Math.min(Math.max(bounds.top, centeredTop),
885 bounds.top + bounds.height() - height);
Winson Chungaf40f202013-09-18 18:26:31 -0700886 if (grid.isPhone() && (grid.availableWidthPx - width) < grid.iconSizePx) {
887 // Center the folder if it is full (on phones only)
888 left = (grid.availableWidthPx - width) / 2;
889 } else if (width >= bounds.width()) {
890 // If the folder doesn't fit within the bounds, center it about the desired bounds
Adam Cohen35e7e642011-07-17 14:47:18 -0700891 left = bounds.left + (bounds.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700892 }
Adam Cohen35e7e642011-07-17 14:47:18 -0700893 if (height >= bounds.height()) {
894 top = bounds.top + (bounds.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700895 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700896
897 int folderPivotX = width / 2 + (centeredLeft - left);
898 int folderPivotY = height / 2 + (centeredTop - top);
899 setPivotX(folderPivotX);
900 setPivotY(folderPivotY);
Adam Cohen268c4752012-06-06 17:47:33 -0700901 mFolderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700902 (1.0f * folderPivotX / width));
Adam Cohen268c4752012-06-06 17:47:33 -0700903 mFolderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700904 (1.0f * folderPivotY / height));
Adam Cohen3bf84d32012-05-07 20:17:14 -0700905
Adam Cohen662b5982011-12-13 17:45:21 -0800906 lp.width = width;
907 lp.height = height;
908 lp.x = left;
909 lp.y = top;
Adam Cohen2801caf2011-05-13 20:57:39 -0700910 }
911
Adam Cohen268c4752012-06-06 17:47:33 -0700912 float getPivotXForIconAnimation() {
913 return mFolderIconPivotX;
914 }
915 float getPivotYForIconAnimation() {
916 return mFolderIconPivotY;
917 }
918
Winson Chung892c74d2013-08-22 16:15:50 -0700919 private int getContentAreaHeight() {
920 LauncherAppState app = LauncherAppState.getInstance();
921 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
922 Rect workspacePadding = grid.getWorkspacePadding(grid.isLandscape ?
923 CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
924 int maxContentAreaHeight = grid.availableHeightPx -
Winson Chung892c74d2013-08-22 16:15:50 -0700925 workspacePadding.top - workspacePadding.bottom -
Sunny Goyal290800b2015-03-05 11:33:33 -0800926 mFooterHeight;
Adam Cohen1960ea42013-11-12 11:33:14 +0000927 int height = Math.min(maxContentAreaHeight,
Winson Chung892c74d2013-08-22 16:15:50 -0700928 mContent.getDesiredHeight());
Adam Cohen1960ea42013-11-12 11:33:14 +0000929 return Math.max(height, MIN_CONTENT_DIMEN);
930 }
931
932 private int getContentAreaWidth() {
933 return Math.max(mContent.getDesiredWidth(), MIN_CONTENT_DIMEN);
Winson Chung892c74d2013-08-22 16:15:50 -0700934 }
935
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700936 private int getFolderHeight() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800937 return getFolderHeight(getContentAreaHeight());
938 }
939
940 private int getFolderHeight(int contentAreaHeight) {
Sunny Goyal290800b2015-03-05 11:33:33 -0800941 return getPaddingTop() + getPaddingBottom() + contentAreaHeight + mFooterHeight;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700942 }
Adam Cohen234c4cd2011-07-17 21:03:04 -0700943
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700944 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800945 int contentWidth = getContentAreaWidth();
946 int contentHeight = getContentAreaHeight();
Adam Cohen1960ea42013-11-12 11:33:14 +0000947
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800948 int contentAreaWidthSpec = MeasureSpec.makeMeasureSpec(contentWidth, MeasureSpec.EXACTLY);
949 int contentAreaHeightSpec = MeasureSpec.makeMeasureSpec(contentHeight, MeasureSpec.EXACTLY);
950
951 mContent.setFixedSize(contentWidth, contentHeight);
952 mContentWrapper.measure(contentAreaWidthSpec, contentAreaHeightSpec);
Sunny Goyal290800b2015-03-05 11:33:33 -0800953 mFooter.measure(contentAreaWidthSpec,
954 MeasureSpec.makeMeasureSpec(mFooterHeight, MeasureSpec.EXACTLY));
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800955
956 int folderWidth = getPaddingLeft() + getPaddingRight() + contentWidth;
957 int folderHeight = getFolderHeight(contentHeight);
958 setMeasuredDimension(folderWidth, folderHeight);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700959 }
960
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800961 /**
962 * Rearranges the children based on their rank.
963 */
964 public void rearrangeChildren() {
965 rearrangeChildren(-1);
966 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700967
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800968 /**
969 * Rearranges the children based on their rank.
970 * @param itemCount if greater than the total children count, empty spaces are left at the end,
971 * otherwise it is ignored.
972 */
973 public void rearrangeChildren(int itemCount) {
974 ArrayList<View> views = getItemsInReadingOrder();
975 mContent.arrangeChildren(views, Math.max(itemCount, views.size()));
Adam Cohen7c693212011-05-18 15:26:57 -0700976 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700977 }
978
Sunny Goyalc4918352015-03-10 18:15:48 -0700979 // TODO remove this once GSA code fix is submitted
980 public ViewGroup getContent() {
981 return (ViewGroup) mContent;
982 }
983
Adam Cohena9cf38f2011-05-02 15:36:58 -0700984 public int getItemCount() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800985 return mContent.getItemCount();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700986 }
987
Adam Cohen091440a2015-03-18 14:16:05 -0700988 @Thunk void onCloseComplete() {
Adam Cohen05e0f402011-08-01 12:12:49 -0700989 DragLayer parent = (DragLayer) getParent();
Michael Jurka5649c282012-06-18 10:33:21 -0700990 if (parent != null) {
991 parent.removeView(this);
992 }
Adam Cohen4554ee12011-08-03 16:13:21 -0700993 mDragController.removeDropTarget((DropTarget) this);
Adam Cohen05e0f402011-08-01 12:12:49 -0700994 clearFocus();
Adam Cohenac56cff2011-09-28 20:45:37 -0700995 mFolderIcon.requestFocus();
Adam Cohen05e0f402011-08-01 12:12:49 -0700996
Adam Cohen2801caf2011-05-13 20:57:39 -0700997 if (mRearrangeOnClose) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800998 rearrangeChildren();
Adam Cohen2801caf2011-05-13 20:57:39 -0700999 mRearrangeOnClose = false;
1000 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001001 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001002 if (!mDragInProgress && !mSuppressFolderDeletion) {
1003 replaceFolderWithFinalItem();
1004 } else if (mDragInProgress) {
1005 mDeleteFolderOnDropCompleted = true;
1006 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001007 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001008 mSuppressFolderDeletion = false;
Adam Cohenafb01ee2011-06-23 15:38:03 -07001009 }
1010
Adam Cohen091440a2015-03-18 14:16:05 -07001011 @Thunk void replaceFolderWithFinalItem() {
Adam Cohenafb01ee2011-06-23 15:38:03 -07001012 // Add the last remaining child to the workspace in place of the folder
Adam Cohenfb91f302012-06-11 15:45:18 -07001013 Runnable onCompleteRunnable = new Runnable() {
1014 @Override
1015 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -07001016 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screenId);
Adam Cohenafb01ee2011-06-23 15:38:03 -07001017
Winson Chung33231f52013-12-09 16:57:45 -08001018 View child = null;
Adam Cohenfb91f302012-06-11 15:45:18 -07001019 // Move the item from the folder to the workspace, in the position of the folder
1020 if (getItemCount() == 1) {
1021 ShortcutInfo finalItem = mInfo.contents.get(0);
Adam Cohenc5e63f32012-07-12 16:16:57 -07001022 child = mLauncher.createShortcut(R.layout.application, cellLayout,
Adam Cohenfb91f302012-06-11 15:45:18 -07001023 finalItem);
1024 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -07001025 mInfo.screenId, mInfo.cellX, mInfo.cellY);
Adam Cohenfb91f302012-06-11 15:45:18 -07001026 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001027 if (getItemCount() <= 1) {
1028 // Remove the folder
1029 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
Dan Sandler0eb687f2014-01-10 13:24:55 -05001030 if (cellLayout != null) {
1031 // b/12446428 -- sometimes the cell layout has already gone away?
1032 cellLayout.removeView(mFolderIcon);
1033 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001034 if (mFolderIcon instanceof DropTarget) {
1035 mDragController.removeDropTarget((DropTarget) mFolderIcon);
1036 }
1037 mLauncher.removeFolder(mInfo);
1038 }
Adam Cohenc5e63f32012-07-12 16:16:57 -07001039 // We add the child after removing the folder to prevent both from existing at
Winson Chung0e6a7132013-08-23 12:55:10 -07001040 // the same time in the CellLayout. We need to add the new item with addInScreenFromBind()
1041 // to ensure that hotseat items are placed correctly.
Adam Cohenc5e63f32012-07-12 16:16:57 -07001042 if (child != null) {
Winson Chung0e6a7132013-08-23 12:55:10 -07001043 mLauncher.getWorkspace().addInScreenFromBind(child, mInfo.container, mInfo.screenId,
Adam Cohenc5e63f32012-07-12 16:16:57 -07001044 mInfo.cellX, mInfo.cellY, mInfo.spanX, mInfo.spanY);
1045 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001046 }
1047 };
Sunny Goyalbc753352015-03-05 09:40:44 -08001048 View finalChild = mContent.getLastItem();
Adam Cohenfb91f302012-06-11 15:45:18 -07001049 if (finalChild != null) {
1050 mFolderIcon.performDestroyAnimation(finalChild, onCompleteRunnable);
Winson Chung33231f52013-12-09 16:57:45 -08001051 } else {
1052 onCompleteRunnable.run();
Adam Cohenafb01ee2011-06-23 15:38:03 -07001053 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001054 mDestroyed = true;
1055 }
1056
1057 boolean isDestroyed() {
1058 return mDestroyed;
Adam Cohen2801caf2011-05-13 20:57:39 -07001059 }
1060
Adam Cohenac56cff2011-09-28 20:45:37 -07001061 // This method keeps track of the last item in the folder for the purposes
1062 // of keyboard focus
Sunny Goyal290800b2015-03-05 11:33:33 -08001063 public void updateTextViewFocus() {
Sunny Goyalbc753352015-03-05 09:40:44 -08001064 View lastChild = mContent.getLastItem();
Adam Cohenac56cff2011-09-28 20:45:37 -07001065 if (lastChild != null) {
1066 mFolderName.setNextFocusDownId(lastChild.getId());
1067 mFolderName.setNextFocusRightId(lastChild.getId());
1068 mFolderName.setNextFocusLeftId(lastChild.getId());
1069 mFolderName.setNextFocusUpId(lastChild.getId());
1070 }
1071 }
1072
Adam Cohenbfbfd262011-06-13 16:55:12 -07001073 public void onDrop(DragObject d) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001074 Runnable cleanUpRunnable = null;
1075
Adam Cohen689ff162014-05-08 17:27:56 -07001076 // If we are coming from All Apps space, we defer removing the extra empty screen
1077 // until the folder closes
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001078 if (d.dragSource != mLauncher.getWorkspace() && !(d.dragSource instanceof Folder)) {
1079 cleanUpRunnable = new Runnable() {
1080 @Override
1081 public void run() {
Adam Cohen689ff162014-05-08 17:27:56 -07001082 mLauncher.exitSpringLoadedDragModeDelayed(true,
1083 Launcher.EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
1084 null);
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001085 }
1086 };
Adam Cohenbfbfd262011-06-13 16:55:12 -07001087 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001088
Sunny Goyalb8634152015-04-09 14:17:14 -07001089 // If the icon was dropped while the page was being scrolled, we need to compute
1090 // the target location again such that the icon is placed of the final page.
1091 if (!mContent.rankOnCurrentPage(mEmptyCellRank)) {
1092 // Reorder again.
1093 mTargetRank = getTargetRank(d, null);
Sunny Goyal48461932015-03-09 17:41:09 -07001094
Sunny Goyalb8634152015-04-09 14:17:14 -07001095 // Rearrange items immediately.
1096 mReorderAlarmListener.onAlarm(mReorderAlarm);
Sunny Goyal48461932015-03-09 17:41:09 -07001097
Sunny Goyalb8634152015-04-09 14:17:14 -07001098 mOnScrollHintAlarm.cancelAlarm();
1099 mScrollPauseAlarm.cancelAlarm();
Sunny Goyal48461932015-03-09 17:41:09 -07001100 }
Sunny Goyalb8634152015-04-09 14:17:14 -07001101 mContent.completePendingPageChanges();
Sunny Goyal48461932015-03-09 17:41:09 -07001102
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001103 View currentDragView;
1104 ShortcutInfo si = mCurrentDragInfo;
1105 if (mIsExternalDrag) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001106 currentDragView = mContent.createAndAddViewForRank(si, mEmptyCellRank);
Sunny Goyal95abbb32014-08-04 10:53:22 -07001107 // Actually move the item in the database if it was an external drag. Call this
1108 // before creating the view, so that ShortcutInfo is updated appropriately.
1109 LauncherModel.addOrMoveItemInDatabase(
1110 mLauncher, si, mInfo.id, 0, si.cellX, si.cellY);
1111
1112 // We only need to update the locations if it doesn't get handled in #onDropCompleted.
1113 if (d.dragSource != this) {
1114 updateItemLocationsInDatabaseBatch();
1115 }
1116 mIsExternalDrag = false;
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001117 } else {
1118 currentDragView = mCurrentDragView;
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001119 mContent.addViewForRank(currentDragView, si, mEmptyCellRank);
Adam Cohenbfbfd262011-06-13 16:55:12 -07001120 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001121
1122 if (d.dragView.hasDrawn()) {
1123
1124 // Temporarily reset the scale such that the animation target gets calculated correctly.
1125 float scaleX = getScaleX();
1126 float scaleY = getScaleY();
1127 setScaleX(1.0f);
1128 setScaleY(1.0f);
1129 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, currentDragView,
1130 cleanUpRunnable, null);
1131 setScaleX(scaleX);
1132 setScaleY(scaleY);
1133 } else {
1134 d.deferDragViewCleanupPostAnimation = false;
1135 currentDragView.setVisibility(VISIBLE);
1136 }
1137 mItemsInvalidated = true;
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001138 rearrangeChildren();
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001139
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001140 // Temporarily suppress the listener, as we did all the work already here.
1141 mSuppressOnAdd = true;
1142 mInfo.add(si);
1143 mSuppressOnAdd = false;
Sunny Goyal4b020172014-08-28 14:51:14 -07001144 // Clear the drag info, as it is no longer being dragged.
1145 mCurrentDragInfo = null;
Sunny Goyalf4066152015-04-15 09:42:19 -07001146 mDragInProgress = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001147 }
1148
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001149 // This is used so the item doesn't immediately appear in the folder when added. In one case
1150 // we need to create the illusion that the item isn't added back to the folder yet, to
1151 // to correspond to the animation of the icon back into the folder. This is
1152 public void hideItem(ShortcutInfo info) {
1153 View v = getViewForInfo(info);
1154 v.setVisibility(INVISIBLE);
1155 }
1156 public void showItem(ShortcutInfo info) {
1157 View v = getViewForInfo(info);
1158 v.setVisibility(VISIBLE);
1159 }
1160
Adam Cohenbfbfd262011-06-13 16:55:12 -07001161 public void onAdd(ShortcutInfo item) {
Adam Cohen05e0f402011-08-01 12:12:49 -07001162 // If the item was dropped onto this open folder, we have done the work associated
1163 // with adding the item to the folder, as indicated by mSuppressOnAdd being set
Adam Cohenbfbfd262011-06-13 16:55:12 -07001164 if (mSuppressOnAdd) return;
Sunny Goyal5d85c442015-03-10 13:14:47 -07001165 mContent.createAndAddViewForRank(item, mContent.allocateRankForNewItem(item));
Sunny Goyal2e688a82015-03-18 10:23:39 -07001166 mItemsInvalidated = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001167 LauncherModel.addOrMoveItemInDatabase(
1168 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
1169 }
1170
Adam Cohena9cf38f2011-05-02 15:36:58 -07001171 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -07001172 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001173 // If this item is being dragged from this open folder, we have already handled
1174 // the work associated with removing the item, so we don't have to do anything here.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001175 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001176 View v = getViewForInfo(item);
Sunny Goyal290800b2015-03-05 11:33:33 -08001177 mContent.removeItem(v);
Adam Cohen2801caf2011-05-13 20:57:39 -07001178 if (mState == STATE_ANIMATING) {
1179 mRearrangeOnClose = true;
1180 } else {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001181 rearrangeChildren();
Adam Cohen2801caf2011-05-13 20:57:39 -07001182 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001183 if (getItemCount() <= 1) {
1184 replaceFolderWithFinalItem();
1185 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001186 }
Adam Cohen7c693212011-05-18 15:26:57 -07001187
Sunny Goyalbc753352015-03-05 09:40:44 -08001188 private View getViewForInfo(final ShortcutInfo item) {
1189 return mContent.iterateOverItems(new ItemOperator() {
1190
1191 @Override
1192 public boolean evaluate(ItemInfo info, View view, View parent) {
1193 return info == item;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001194 }
Sunny Goyalbc753352015-03-05 09:40:44 -08001195 });
Adam Cohendf1e4e82011-06-24 15:57:39 -07001196 }
1197
Adam Cohen76078c42011-06-09 15:06:52 -07001198 public void onItemsChanged() {
Adam Cohenac56cff2011-09-28 20:45:37 -07001199 updateTextViewFocus();
Adam Cohen76078c42011-06-09 15:06:52 -07001200 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001201
Adam Cohen76fc0852011-06-17 13:26:23 -07001202 public void onTitleChanged(CharSequence title) {
1203 }
Adam Cohen76078c42011-06-09 15:06:52 -07001204
Adam Cohen7c693212011-05-18 15:26:57 -07001205 public ArrayList<View> getItemsInReadingOrder() {
1206 if (mItemsInvalidated) {
1207 mItemsInReadingOrder.clear();
Sunny Goyalbc753352015-03-05 09:40:44 -08001208 mContent.iterateOverItems(new ItemOperator() {
1209
1210 @Override
1211 public boolean evaluate(ItemInfo info, View view, View parent) {
1212 mItemsInReadingOrder.add(view);
1213 return false;
Adam Cohen7c693212011-05-18 15:26:57 -07001214 }
Sunny Goyalbc753352015-03-05 09:40:44 -08001215 });
Adam Cohen7c693212011-05-18 15:26:57 -07001216 mItemsInvalidated = false;
1217 }
1218 return mItemsInReadingOrder;
1219 }
Adam Cohen8dfcba42011-07-07 16:38:18 -07001220
1221 public void getLocationInDragLayer(int[] loc) {
1222 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
1223 }
Adam Cohenea0818d2011-09-30 20:06:58 -07001224
1225 public void onFocusChange(View v, boolean hasFocus) {
1226 if (v == mFolderName && hasFocus) {
1227 startEditingFolderName();
1228 }
1229 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001230
1231 @Override
1232 public void getHitRectRelativeToDragLayer(Rect outRect) {
1233 getHitRect(outRect);
Sunny Goyal48461932015-03-09 17:41:09 -07001234 outRect.left -= mScrollAreaOffset;
1235 outRect.right += mScrollAreaOffset;
1236 }
1237
1238 private class OnScrollHintListener implements OnAlarmListener {
1239
1240 private final DragObject mDragObject;
1241
1242 OnScrollHintListener(DragObject object) {
1243 mDragObject = object;
1244 }
1245
1246 /**
1247 * Scroll hint has been shown long enough. Now scroll to appropriate page.
1248 */
1249 @Override
1250 public void onAlarm(Alarm alarm) {
1251 if (mCurrentScrollDir == DragController.SCROLL_LEFT) {
Sunny Goyalb8634152015-04-09 14:17:14 -07001252 mContent.scrollLeft();
Sunny Goyal48461932015-03-09 17:41:09 -07001253 mScrollHintDir = DragController.SCROLL_NONE;
1254 } else if (mCurrentScrollDir == DragController.SCROLL_RIGHT) {
Sunny Goyalb8634152015-04-09 14:17:14 -07001255 mContent.scrollRight();
Sunny Goyal48461932015-03-09 17:41:09 -07001256 mScrollHintDir = DragController.SCROLL_NONE;
1257 } else {
1258 // This should not happen
1259 return;
1260 }
1261 mCurrentScrollDir = DragController.SCROLL_NONE;
1262
1263 // Pause drag event until the scrolling is finished
1264 mScrollPauseAlarm.setOnAlarmListener(new OnScrollFinishedListener(mDragObject));
1265 mScrollPauseAlarm.setAlarm(DragController.RESCROLL_DELAY);
1266 }
1267 }
1268
1269 private class OnScrollFinishedListener implements OnAlarmListener {
1270
1271 private final DragObject mDragObject;
1272
1273 OnScrollFinishedListener(DragObject object) {
1274 mDragObject = object;
1275 }
1276
1277 /**
1278 * Page scroll is complete.
1279 */
1280 @Override
1281 public void onAlarm(Alarm alarm) {
1282 // Reorder immediately on page change.
1283 onDragOver(mDragObject, 1);
1284 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001285 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001286}