blob: 0eb1fd8bb2e498c535f3214d04dfae07e4fd4a4a [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
Daniel Sandler325dc232013-06-05 22:57:57 -040052import com.android.launcher3.FolderInfo.FolderListener;
Sunny Goyalbc753352015-03-05 09:40:44 -080053import com.android.launcher3.Workspace.ItemOperator;
Adam Cohen091440a2015-03-18 14:16:05 -070054import com.android.launcher3.util.Thunk;
Romain Guyedcce092010-03-04 13:03:17 -080055
Adam Cohenc0dcf592011-06-01 15:30:43 -070056import java.util.ArrayList;
Adam Cohen3bf84d32012-05-07 20:17:14 -070057import java.util.Collections;
Adam Cohenc0dcf592011-06-01 15:30:43 -070058
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059/**
60 * Represents a set of icons chosen by the user or generated by the system.
61 */
Adam Cohen8dfcba42011-07-07 16:38:18 -070062public class Folder extends LinearLayout implements DragSource, View.OnClickListener,
Adam Cohenea0818d2011-09-30 20:06:58 -070063 View.OnLongClickListener, DropTarget, FolderListener, TextView.OnEditorActionListener,
64 View.OnFocusChangeListener {
Adam Cohendf2cc412011-04-27 16:56:57 -070065 private static final String TAG = "Launcher.Folder";
66
Sunny Goyalc3a609f2015-02-26 17:43:50 -080067 /**
68 * We avoid measuring {@link #mContentWrapper} with a 0 width or height, as this
69 * results in CellLayout being measured as UNSPECIFIED, which it does not support.
70 */
71 private static final int MIN_CONTENT_DIMEN = 5;
Adam Cohen4eac29a2011-07-11 17:53:37 -070072
Adam Cohendf2cc412011-04-27 16:56:57 -070073 static final int STATE_NONE = -1;
74 static final int STATE_SMALL = 0;
75 static final int STATE_ANIMATING = 1;
76 static final int STATE_OPEN = 2;
77
Sunny Goyal48461932015-03-09 17:41:09 -070078 /**
Sunny Goyal48461932015-03-09 17:41:09 -070079 * Time for which the scroll hint is shown before automatically changing page.
80 */
81 public static final int SCROLL_HINT_DURATION = DragController.SCROLL_DELAY;
82
83 /**
Sunny Goyal5d85c442015-03-10 13:14:47 -070084 * Time in milliseconds for which an icon sticks to the target position
85 * in case of a sorted folder.
86 */
87 private static final int SORTED_STICKY_REORDER_DELAY = 1500;
88
89 /**
Sunny Goyal48461932015-03-09 17:41:09 -070090 * Fraction of icon width which behave as scroll region.
91 */
92 private static final float ICON_OVERSCROLL_WIDTH_FACTOR = 0.45f;
93
Adam Cohenf0f4eda2013-06-06 21:27:03 -070094 private static final int REORDER_DELAY = 250;
Adam Cohen5d518fa2013-12-05 14:16:23 -080095 private static final int ON_EXIT_CLOSE_DELAY = 400;
Sunny Goyalc3a609f2015-02-26 17:43:50 -080096 private static final Rect sTempRect = new Rect();
97
98 private static String sDefaultFolderName;
99 private static String sHintText;
100
101 private final Alarm mReorderAlarm = new Alarm();
102 private final Alarm mOnExitAlarm = new Alarm();
Sunny Goyalb8634152015-04-09 14:17:14 -0700103 private final Alarm mOnScrollHintAlarm = new Alarm();
104 @Thunk final Alarm mScrollPauseAlarm = new Alarm();
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800105
Adam Cohen091440a2015-03-18 14:16:05 -0700106 @Thunk final ArrayList<View> mItemsInReadingOrder = new ArrayList<View>();
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800107
108 private final int mExpandDuration;
109 private final int mMaterialExpandDuration;
110 private final int mMaterialExpandStagger;
111
112 private final InputMethodManager mInputMethodManager;
113
114 protected final Launcher mLauncher;
115 protected DragController mDragController;
116 protected FolderInfo mInfo;
117
Adam Cohen091440a2015-03-18 14:16:05 -0700118 @Thunk FolderIcon mFolderIcon;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800119
Sunny Goyalb8634152015-04-09 14:17:14 -0700120 @Thunk FolderPagedView mContent;
Adam Cohen091440a2015-03-18 14:16:05 -0700121 @Thunk View mContentWrapper;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800122 FolderEditText mFolderName;
123
Sunny Goyal290800b2015-03-05 11:33:33 -0800124 private View mFooter;
125 private int mFooterHeight;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800126
127 // Cell ranks used for drag and drop
Adam Cohen091440a2015-03-18 14:16:05 -0700128 @Thunk int mTargetRank, mPrevTargetRank, mEmptyCellRank;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800129
Adam Cohen091440a2015-03-18 14:16:05 -0700130 @Thunk int mState = STATE_NONE;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800131 private boolean mRearrangeOnClose = false;
Adam Cohen7c693212011-05-18 15:26:57 -0700132 boolean mItemsInvalidated = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700133 private ShortcutInfo mCurrentDragInfo;
134 private View mCurrentDragView;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800135 private boolean mIsExternalDrag;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700136 boolean mSuppressOnAdd = false;
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700137 private boolean mDragInProgress = false;
138 private boolean mDeleteFolderOnDropCompleted = false;
139 private boolean mSuppressFolderDeletion = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700140 private boolean mItemAddedBackToSelfViaIcon = false;
Adam Cohen091440a2015-03-18 14:16:05 -0700141 @Thunk float mFolderIconPivotX;
142 @Thunk float mFolderIconPivotY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700143 private boolean mIsEditingName = false;
Adam Cohen1960ea42013-11-12 11:33:14 +0000144
Adam Cohenfb91f302012-06-11 15:45:18 -0700145 private boolean mDestroyed;
146
Adam Cohen091440a2015-03-18 14:16:05 -0700147 @Thunk Runnable mDeferredAction;
Michael Jurka1e2f4652013-07-08 18:03:46 -0700148 private boolean mDeferDropAfterUninstall;
149 private boolean mUninstallSuccessful;
150
Sunny Goyal48461932015-03-09 17:41:09 -0700151 // Folder scrolling
152 private int mScrollAreaOffset;
Sunny Goyal48461932015-03-09 17:41:09 -0700153
Adam Cohen091440a2015-03-18 14:16:05 -0700154 @Thunk int mScrollHintDir = DragController.SCROLL_NONE;
155 @Thunk int mCurrentScrollDir = DragController.SCROLL_NONE;
Sunny Goyal48461932015-03-09 17:41:09 -0700156
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157 /**
158 * Used to inflate the Workspace from XML.
159 *
160 * @param context The application's context.
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800161 * @param attrs The attributes set containing the Workspace's customization values.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162 */
163 public Folder(Context context, AttributeSet attrs) {
164 super(context, attrs);
165 setAlwaysDrawnWithCacheEnabled(false);
Adam Cohen76fc0852011-06-17 13:26:23 -0700166 mInputMethodManager = (InputMethodManager)
Michael Jurka8b805b12012-04-18 14:23:14 -0700167 getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
Adam Cohen76fc0852011-06-17 13:26:23 -0700168
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800169 Resources res = getResources();
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700170 mExpandDuration = res.getInteger(R.integer.config_folderExpandDuration);
171 mMaterialExpandDuration = res.getInteger(R.integer.config_materialFolderExpandDuration);
172 mMaterialExpandStagger = res.getInteger(R.integer.config_materialFolderExpandStagger);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700173
174 if (sDefaultFolderName == null) {
175 sDefaultFolderName = res.getString(R.string.folder_name);
176 }
Adam Cohena65beee2011-06-27 21:32:23 -0700177 if (sHintText == null) {
178 sHintText = res.getString(R.string.folder_hint_text);
179 }
Adam Cohen4eac29a2011-07-11 17:53:37 -0700180 mLauncher = (Launcher) context;
Adam Cohenac56cff2011-09-28 20:45:37 -0700181 // We need this view to be focusable in touch mode so that when text editing of the folder
182 // name is complete, we have something to focus on, thus hiding the cursor and giving
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800183 // reliable behavior when clicking the text field (since it will always gain focus on click).
Adam Cohenac56cff2011-09-28 20:45:37 -0700184 setFocusableInTouchMode(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185 }
186
187 @Override
188 protected void onFinishInflate() {
189 super.onFinishInflate();
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800190 mContentWrapper = findViewById(R.id.folder_content_wrapper);
Sunny Goyalb8634152015-04-09 14:17:14 -0700191 mContent = (FolderPagedView) findViewById(R.id.folder_content);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800192 mContent.setFolder(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700193
Adam Cohenac56cff2011-09-28 20:45:37 -0700194 mFolderName = (FolderEditText) findViewById(R.id.folder_name);
195 mFolderName.setFolder(this);
Adam Cohenea0818d2011-09-30 20:06:58 -0700196 mFolderName.setOnFocusChangeListener(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700197
Adam Cohen76fc0852011-06-17 13:26:23 -0700198 // We disable action mode for now since it messes up the view on phones
199 mFolderName.setCustomSelectionActionModeCallback(mActionModeCallback);
Adam Cohen76fc0852011-06-17 13:26:23 -0700200 mFolderName.setOnEditorActionListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700201 mFolderName.setSelectAllOnFocus(true);
Adam Cohen7a14d0b2011-06-24 11:36:35 -0700202 mFolderName.setInputType(mFolderName.getInputType() |
203 InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800204
Sunny Goyalb8634152015-04-09 14:17:14 -0700205 mFooter = findViewById(R.id.folder_footer);
206 updateFooterHeight();
207 }
208
209 public void updateFooterHeight() {
Sunny Goyal290800b2015-03-05 11:33:33 -0800210 // We find out how tall footer wants to be (it is set to wrap_content), so that
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800211 // we can allocate the appropriate amount of space for it.
212 int measureSpec = MeasureSpec.UNSPECIFIED;
Sunny Goyal290800b2015-03-05 11:33:33 -0800213 mFooter.measure(measureSpec, measureSpec);
214 mFooterHeight = mFooter.getMeasuredHeight();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800215 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700216
Adam Cohen76fc0852011-06-17 13:26:23 -0700217 private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
218 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
219 return false;
220 }
221
222 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
223 return false;
224 }
225
226 public void onDestroyActionMode(ActionMode mode) {
227 }
228
229 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
230 return false;
231 }
232 };
233
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800234 public void onClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700235 Object tag = v.getTag();
236 if (tag instanceof ShortcutInfo) {
Adam Cohenb5fe60c2013-06-06 22:03:51 -0700237 mLauncher.onClick(v);
Adam Cohendf2cc412011-04-27 16:56:57 -0700238 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800239 }
240
241 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -0700242 // Return if global dragging is not enabled
243 if (!mLauncher.isDraggingEnabled()) return true;
244
Adam Cohendf2cc412011-04-27 16:56:57 -0700245 Object tag = v.getTag();
246 if (tag instanceof ShortcutInfo) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700247 ShortcutInfo item = (ShortcutInfo) tag;
248 if (!v.isInTouchMode()) {
249 return false;
250 }
251
Winson Chungb745afb2015-03-02 11:51:23 -0800252 mLauncher.getWorkspace().beginDragShared(v, new Point(), this, false);
Adam Cohen76078c42011-06-09 15:06:52 -0700253
254 mCurrentDragInfo = item;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800255 mEmptyCellRank = item.rank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700256 mCurrentDragView = v;
Adam Cohenfc53cd22011-07-20 15:45:11 -0700257
Sunny Goyal290800b2015-03-05 11:33:33 -0800258 mContent.removeItem(mCurrentDragView);
Adam Cohenfc53cd22011-07-20 15:45:11 -0700259 mInfo.remove(mCurrentDragInfo);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700260 mDragInProgress = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700261 mItemAddedBackToSelfViaIcon = false;
Adam Cohendf2cc412011-04-27 16:56:57 -0700262 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800263 return true;
264 }
265
Adam Cohen76fc0852011-06-17 13:26:23 -0700266 public boolean isEditingName() {
267 return mIsEditingName;
268 }
269
270 public void startEditingFolderName() {
Adam Cohena65beee2011-06-27 21:32:23 -0700271 mFolderName.setHint("");
Adam Cohen76fc0852011-06-17 13:26:23 -0700272 mIsEditingName = true;
273 }
274
275 public void dismissEditingName() {
276 mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
277 doneEditingFolderName(true);
278 }
279
280 public void doneEditingFolderName(boolean commit) {
Adam Cohena65beee2011-06-27 21:32:23 -0700281 mFolderName.setHint(sHintText);
Adam Cohen1df26a32011-08-12 16:15:23 -0700282 // Convert to a string here to ensure that no other state associated with the text field
283 // gets saved.
Adam Cohen3371da02011-10-25 21:38:29 -0700284 String newTitle = mFolderName.getText().toString();
285 mInfo.setTitle(newTitle);
Adam Cohen76fc0852011-06-17 13:26:23 -0700286 LauncherModel.updateItemInDatabase(mLauncher, mInfo);
Adam Cohenac56cff2011-09-28 20:45:37 -0700287
Adam Cohen3371da02011-10-25 21:38:29 -0700288 if (commit) {
289 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700290 String.format(getContext().getString(R.string.folder_renamed), newTitle));
Adam Cohen3371da02011-10-25 21:38:29 -0700291 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700292 // In order to clear the focus from the text field, we set the focus on ourself. This
293 // ensures that every time the field is clicked, focus is gained, giving reliable behavior.
294 requestFocus();
295
Adam Cohene601a432011-07-26 21:51:30 -0700296 Selection.setSelection((Spannable) mFolderName.getText(), 0, 0);
Adam Cohen76fc0852011-06-17 13:26:23 -0700297 mIsEditingName = false;
298 }
299
300 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
301 if (actionId == EditorInfo.IME_ACTION_DONE) {
302 dismissEditingName();
303 return true;
304 }
305 return false;
306 }
307
308 public View getEditTextRegion() {
309 return mFolderName;
310 }
311
Adam Cohen0c872ba2011-05-05 10:34:16 -0700312 /**
313 * We need to handle touch events to prevent them from falling through to the workspace below.
314 */
315 @Override
316 public boolean onTouchEvent(MotionEvent ev) {
317 return true;
318 }
319
Joe Onorato00acb122009-08-04 16:04:30 -0400320 public void setDragController(DragController dragController) {
321 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800322 }
323
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800324 public void setFolderIcon(FolderIcon icon) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700325 mFolderIcon = icon;
326 }
327
Adam Cohen3371da02011-10-25 21:38:29 -0700328 @Override
329 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
330 // When the folder gets focus, we don't want to announce the list of items.
331 return true;
332 }
333
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800334 /**
335 * @return the FolderInfo object associated with this folder
336 */
337 FolderInfo getInfo() {
338 return mInfo;
339 }
340
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800341 void bind(FolderInfo info) {
342 mInfo = info;
Adam Cohendf2cc412011-04-27 16:56:57 -0700343 ArrayList<ShortcutInfo> children = info.contents;
Sunny Goyal08f72612015-01-05 13:41:43 -0800344 Collections.sort(children, Utilities.RANK_COMPARATOR);
Sunny Goyal08f72612015-01-05 13:41:43 -0800345
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800346 ArrayList<ShortcutInfo> overflow = mContent.bindItems(children);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700347
Jason Monk4ff73882014-04-24 16:48:07 -0400348 // If our folder has too many items we prune them from the list. This is an issue
Adam Cohenc508b2d2011-06-28 14:41:44 -0700349 // when upgrading from the old Folders implementation which could contain an unlimited
350 // number of items.
351 for (ShortcutInfo item: overflow) {
352 mInfo.remove(item);
353 LauncherModel.deleteItemFromDatabase(mLauncher, item);
354 }
355
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800356 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
357 if (lp == null) {
358 lp = new DragLayer.LayoutParams(0, 0);
359 lp.customPosition = true;
360 setLayoutParams(lp);
361 }
362 centerAboutIcon();
363
Adam Cohen4dbe6d92011-05-18 17:14:15 -0700364 mItemsInvalidated = true;
Adam Cohenac56cff2011-09-28 20:45:37 -0700365 updateTextViewFocus();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700366 mInfo.addListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700367
Adam Cohenafb01ee2011-06-23 15:38:03 -0700368 if (!sDefaultFolderName.contentEquals(mInfo.title)) {
Adam Cohen4ef610f2011-06-21 22:37:36 -0700369 mFolderName.setText(mInfo.title);
370 } else {
371 mFolderName.setText("");
372 }
Winson Chung33231f52013-12-09 16:57:45 -0800373
374 // In case any children didn't come across during loading, clean up the folder accordingly
375 mFolderIcon.post(new Runnable() {
376 public void run() {
377 if (getItemCount() <= 1) {
378 replaceFolderWithFinalItem();
379 }
380 }
381 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700382 }
383
384 /**
385 * Creates a new UserFolder, inflated from R.layout.user_folder.
386 *
387 * @param context The application's context.
388 *
389 * @return A new UserFolder.
390 */
391 static Folder fromXml(Context context) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700392 return (Folder) LayoutInflater.from(context).inflate(R.layout.user_folder, null);
Adam Cohendf2cc412011-04-27 16:56:57 -0700393 }
394
395 /**
396 * This method is intended to make the UserFolder to be visually identical in size and position
397 * to its associated FolderIcon. This allows for a seamless transition into the expanded state.
398 */
399 private void positionAndSizeAsIcon() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700400 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800401 setScaleX(0.8f);
402 setScaleY(0.8f);
403 setAlpha(0f);
Adam Cohendf2cc412011-04-27 16:56:57 -0700404 mState = STATE_SMALL;
405 }
406
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700407 private void prepareReveal() {
408 setScaleX(1f);
409 setScaleY(1f);
410 setAlpha(1f);
411 mState = STATE_SMALL;
412 }
413
Adam Cohendf2cc412011-04-27 16:56:57 -0700414 public void animateOpen() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700415 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen6441de02011-12-14 14:25:32 -0800416
Sunny Goyalb8634152015-04-09 14:17:14 -0700417 mContent.completePendingPageChanges();
418 if (!(mDragInProgress && mContent.mIsSorted)) {
419 // Open on the first page.
420 mContent.snapToPageImmediately(0);
Sunny Goyal48461932015-03-09 17:41:09 -0700421 }
422
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700423 Animator openFolderAnim = null;
424 final Runnable onCompleteRunnable;
Kenny Guyd794a3f2014-09-16 15:17:58 +0100425 if (!Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700426 positionAndSizeAsIcon();
427 centerAboutIcon();
428
429 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1);
430 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
431 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
432 final ObjectAnimator oa =
433 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
434 oa.setDuration(mExpandDuration);
435 openFolderAnim = oa;
436
437 setLayerType(LAYER_TYPE_HARDWARE, null);
438 onCompleteRunnable = new Runnable() {
439 @Override
440 public void run() {
441 setLayerType(LAYER_TYPE_NONE, null);
442 }
443 };
444 } else {
445 prepareReveal();
446 centerAboutIcon();
447
448 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
449 int height = getFolderHeight();
450
451 float transX = - 0.075f * (width / 2 - getPivotX());
452 float transY = - 0.075f * (height / 2 - getPivotY());
453 setTranslationX(transX);
454 setTranslationY(transY);
455 PropertyValuesHolder tx = PropertyValuesHolder.ofFloat("translationX", transX, 0);
456 PropertyValuesHolder ty = PropertyValuesHolder.ofFloat("translationY", transY, 0);
457
458 int rx = (int) Math.max(Math.max(width - getPivotX(), 0), getPivotX());
459 int ry = (int) Math.max(Math.max(height - getPivotY(), 0), getPivotY());
460 float radius = (float) Math.sqrt(rx * rx + ry * ry);
461 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
462 Animator reveal = LauncherAnimUtils.createCircularReveal(this, (int) getPivotX(),
463 (int) getPivotY(), 0, radius);
464 reveal.setDuration(mMaterialExpandDuration);
465 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
466
Sunny Goyalbc753352015-03-05 09:40:44 -0800467 mContentWrapper.setAlpha(0f);
468 Animator iconsAlpha = LauncherAnimUtils.ofFloat(mContentWrapper, "alpha", 0f, 1f);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700469 iconsAlpha.setDuration(mMaterialExpandDuration);
470 iconsAlpha.setStartDelay(mMaterialExpandStagger);
471 iconsAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
472
Sunny Goyal290800b2015-03-05 11:33:33 -0800473 mFooter.setAlpha(0f);
474 Animator textAlpha = LauncherAnimUtils.ofFloat(mFooter, "alpha", 0f, 1f);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700475 textAlpha.setDuration(mMaterialExpandDuration);
476 textAlpha.setStartDelay(mMaterialExpandStagger);
477 textAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
478
479 Animator drift = LauncherAnimUtils.ofPropertyValuesHolder(this, tx, ty);
480 drift.setDuration(mMaterialExpandDuration);
481 drift.setStartDelay(mMaterialExpandStagger);
482 drift.setInterpolator(new LogDecelerateInterpolator(60, 0));
483
484 anim.play(drift);
485 anim.play(iconsAlpha);
486 anim.play(textAlpha);
487 anim.play(reveal);
488
489 openFolderAnim = anim;
490
Sunny Goyalbc753352015-03-05 09:40:44 -0800491 mContentWrapper.setLayerType(LAYER_TYPE_HARDWARE, null);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700492 onCompleteRunnable = new Runnable() {
493 @Override
494 public void run() {
Sunny Goyalbc753352015-03-05 09:40:44 -0800495 mContentWrapper.setLayerType(LAYER_TYPE_NONE, null);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700496 }
497 };
498 }
499 openFolderAnim.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700500 @Override
501 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700502 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Sunny Goyalbc753352015-03-05 09:40:44 -0800503 mContent.getAccessibilityDescription());
Adam Cohendf2cc412011-04-27 16:56:57 -0700504 mState = STATE_ANIMATING;
505 }
506 @Override
507 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700508 mState = STATE_OPEN;
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700509
510 if (onCompleteRunnable != null) {
511 onCompleteRunnable.run();
512 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800513
Sunny Goyalbc753352015-03-05 09:40:44 -0800514 mContent.setFocusOnFirstChild();
Adam Cohendf2cc412011-04-27 16:56:57 -0700515 }
516 });
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700517 openFolderAnim.start();
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800518
519 // Make sure the folder picks up the last drag move even if the finger doesn't move.
520 if (mDragController.isDragging()) {
521 mDragController.forceTouchMove();
522 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700523
Sunny Goyalb8634152015-04-09 14:17:14 -0700524 FolderPagedView pages = (FolderPagedView) mContent;
525 pages.verifyVisibleHighResIcons(pages.getNextPage());
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800526 }
527
528 public void beginExternalDrag(ShortcutInfo item) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800529 mCurrentDragInfo = item;
Sunny Goyal5d85c442015-03-10 13:14:47 -0700530 mEmptyCellRank = mContent.allocateRankForNewItem(item);
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800531 mIsExternalDrag = true;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800532 mDragInProgress = true;
Sunny Goyalb8634152015-04-09 14:17:14 -0700533
534 if (mContent.mIsSorted) {
Sunny Goyal5d85c442015-03-10 13:14:47 -0700535 mScrollPauseAlarm.setOnAlarmListener(null);
536 mScrollPauseAlarm.cancelAlarm();
537 mScrollPauseAlarm.setAlarm(SORTED_STICKY_REORDER_DELAY);
538 }
539
Adam Cohendf2cc412011-04-27 16:56:57 -0700540 }
541
Adam Cohen091440a2015-03-18 14:16:05 -0700542 @Thunk void sendCustomAccessibilityEvent(int type, String text) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700543 AccessibilityManager accessibilityManager = (AccessibilityManager)
544 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
545 if (accessibilityManager.isEnabled()) {
Adam Cohen3371da02011-10-25 21:38:29 -0700546 AccessibilityEvent event = AccessibilityEvent.obtain(type);
547 onInitializeAccessibilityEvent(event);
548 event.getText().add(text);
Michael Jurka8b805b12012-04-18 14:23:14 -0700549 accessibilityManager.sendAccessibilityEvent(event);
Adam Cohen3371da02011-10-25 21:38:29 -0700550 }
551 }
552
Adam Cohendf2cc412011-04-27 16:56:57 -0700553 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700554 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800555 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
556 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
557 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
Michael Jurka032e6ba2013-04-22 15:08:42 +0200558 final ObjectAnimator oa =
Michael Jurka2ecf9952012-06-18 12:52:28 -0700559 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohendf2cc412011-04-27 16:56:57 -0700560
Adam Cohen2801caf2011-05-13 20:57:39 -0700561 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700562 @Override
563 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700564 onCloseComplete();
Michael Jurka0121c3e2012-05-31 08:36:04 -0700565 setLayerType(LAYER_TYPE_NONE, null);
Adam Cohen2801caf2011-05-13 20:57:39 -0700566 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700567 }
568 @Override
569 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700570 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700571 getContext().getString(R.string.folder_closed));
Adam Cohendf2cc412011-04-27 16:56:57 -0700572 mState = STATE_ANIMATING;
573 }
574 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700575 oa.setDuration(mExpandDuration);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700576 setLayerType(LAYER_TYPE_HARDWARE, null);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100577 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700578 }
579
Adam Cohencb3382b2011-05-24 14:07:08 -0700580 public boolean acceptDrop(DragObject d) {
581 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700582 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700583 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
584 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
585 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700586 }
587
Adam Cohencb3382b2011-05-24 14:07:08 -0700588 public void onDragEnter(DragObject d) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800589 mPrevTargetRank = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700590 mOnExitAlarm.cancelAlarm();
Sunny Goyalb8634152015-04-09 14:17:14 -0700591 // Get the area offset such that the folder only closes if half the drag icon width
592 // is outside the folder area
593 mScrollAreaOffset = d.dragView.getDragRegionWidth() / 2 - d.xOffset;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700594 }
595
596 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
597 public void onAlarm(Alarm alarm) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800598 mContent.realTimeReorder(mEmptyCellRank, mTargetRank);
599 mEmptyCellRank = mTargetRank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700600 }
601 };
602
Sunny Goyalc46bfef2015-01-05 12:40:08 -0800603 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohen2374abf2013-04-16 14:56:57 -0700604 public boolean isLayoutRtl() {
605 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
606 }
607
Sunny Goyal48461932015-03-09 17:41:09 -0700608 @Override
Adam Cohencb3382b2011-05-24 14:07:08 -0700609 public void onDragOver(DragObject d) {
Sunny Goyal48461932015-03-09 17:41:09 -0700610 onDragOver(d, REORDER_DELAY);
611 }
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700612
Sunny Goyal48461932015-03-09 17:41:09 -0700613 private int getTargetRank(DragObject d, float[] recycle) {
614 recycle = d.getVisualCenter(recycle);
615 return mContent.findNearestArea(
616 (int) recycle[0] - getPaddingLeft(), (int) recycle[1] - getPaddingTop());
617 }
618
Adam Cohen091440a2015-03-18 14:16:05 -0700619 @Thunk void onDragOver(DragObject d, int reorderDelay) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700620 if (mScrollPauseAlarm.alarmPending()) {
Sunny Goyal48461932015-03-09 17:41:09 -0700621 return;
622 }
623 final float[] r = new float[2];
624 mTargetRank = getTargetRank(d, r);
625
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800626 if (mTargetRank != mPrevTargetRank) {
Alan Viverette4cda5b72013-08-28 17:53:41 -0700627 mReorderAlarm.cancelAlarm();
Sunny Goyalc46bfef2015-01-05 12:40:08 -0800628 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
629 mReorderAlarm.setAlarm(REORDER_DELAY);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800630 mPrevTargetRank = mTargetRank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700631 }
Sunny Goyal48461932015-03-09 17:41:09 -0700632
Sunny Goyal48461932015-03-09 17:41:09 -0700633 float x = r[0];
Sunny Goyalb8634152015-04-09 14:17:14 -0700634 int currentPage = mContent.getNextPage();
Sunny Goyal48461932015-03-09 17:41:09 -0700635
Sunny Goyalb8634152015-04-09 14:17:14 -0700636 float cellOverlap = mContent.getCurrentCellLayout().getCellWidth()
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700637 * ICON_OVERSCROLL_WIDTH_FACTOR;
638 boolean isOutsideLeftEdge = x < cellOverlap;
639 boolean isOutsideRightEdge = x > (getWidth() - cellOverlap);
Sunny Goyal48461932015-03-09 17:41:09 -0700640
Sunny Goyalb8634152015-04-09 14:17:14 -0700641 if (currentPage > 0 && (mContent.rtlLayout ? isOutsideRightEdge : isOutsideLeftEdge)) {
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700642 showScrollHint(DragController.SCROLL_LEFT, d);
Sunny Goyalb8634152015-04-09 14:17:14 -0700643 } else if (currentPage < (mContent.getPageCount() - 1)
644 && (mContent.rtlLayout ? isOutsideLeftEdge : isOutsideRightEdge)) {
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700645 showScrollHint(DragController.SCROLL_RIGHT, d);
Sunny Goyal48461932015-03-09 17:41:09 -0700646 } else {
647 mOnScrollHintAlarm.cancelAlarm();
648 if (mScrollHintDir != DragController.SCROLL_NONE) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700649 mContent.clearScrollHint();
Sunny Goyal48461932015-03-09 17:41:09 -0700650 mScrollHintDir = DragController.SCROLL_NONE;
651 }
652 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700653 }
654
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700655 private void showScrollHint(int direction, DragObject d) {
656 // Show scroll hint on the right
657 if (mScrollHintDir != direction) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700658 mContent.showScrollHint(direction);
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700659 mScrollHintDir = direction;
660 }
661
662 // Set alarm for when the hint is complete
663 if (!mOnScrollHintAlarm.alarmPending() || mCurrentScrollDir != direction) {
664 mCurrentScrollDir = direction;
665 mOnScrollHintAlarm.cancelAlarm();
666 mOnScrollHintAlarm.setOnAlarmListener(new OnScrollHintListener(d));
667 mOnScrollHintAlarm.setAlarm(SCROLL_HINT_DURATION);
668
669 mReorderAlarm.cancelAlarm();
670 mTargetRank = mEmptyCellRank;
671 }
672 }
673
Adam Cohenbfbfd262011-06-13 16:55:12 -0700674 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
675 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700676 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700677 }
678 };
679
Adam Cohen95bb8002011-07-03 23:40:28 -0700680 public void completeDragExit() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700681 mLauncher.closeFolder();
682 mCurrentDragInfo = null;
683 mCurrentDragView = null;
684 mSuppressOnAdd = false;
685 mRearrangeOnClose = true;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800686 mIsExternalDrag = false;
Adam Cohen3e8f8112011-07-02 18:03:00 -0700687 }
688
Adam Cohencb3382b2011-05-24 14:07:08 -0700689 public void onDragExit(DragObject d) {
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700690 // We only close the folder if this is a true drag exit, ie. not because
691 // a drop has occurred above the folder.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700692 if (!d.dragComplete) {
693 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
694 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
695 }
696 mReorderAlarm.cancelAlarm();
Sunny Goyal48461932015-03-09 17:41:09 -0700697
Sunny Goyalb8634152015-04-09 14:17:14 -0700698 mOnScrollHintAlarm.cancelAlarm();
699 mScrollPauseAlarm.cancelAlarm();
700 if (mScrollHintDir != DragController.SCROLL_NONE) {
701 mContent.clearScrollHint();
702 mScrollHintDir = DragController.SCROLL_NONE;
Sunny Goyal48461932015-03-09 17:41:09 -0700703 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700704 }
705
Michael Jurka1e2f4652013-07-08 18:03:46 -0700706 public void onDropCompleted(final View target, final DragObject d,
707 final boolean isFlingToDelete, final boolean success) {
708 if (mDeferDropAfterUninstall) {
Michael Jurkaf3007582013-08-21 14:33:57 +0200709 Log.d(TAG, "Deferred handling drop because waiting for uninstall.");
Michael Jurka1e2f4652013-07-08 18:03:46 -0700710 mDeferredAction = new Runnable() {
711 public void run() {
712 onDropCompleted(target, d, isFlingToDelete, success);
713 mDeferredAction = null;
714 }
715 };
716 return;
717 }
718
719 boolean beingCalledAfterUninstall = mDeferredAction != null;
720 boolean successfulDrop =
721 success && (!beingCalledAfterUninstall || mUninstallSuccessful);
Winson Chung5f8afe62013-08-12 16:19:28 -0700722
Michael Jurka1e2f4652013-07-08 18:03:46 -0700723 if (successfulDrop) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800724 if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon && target != this) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700725 replaceFolderWithFinalItem();
726 }
727 } else {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800728 rearrangeChildren();
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700729 // The drag failed, we need to return the item to the folder
730 mFolderIcon.onDrop(d);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700731 }
732
733 if (target != this) {
734 if (mOnExitAlarm.alarmPending()) {
735 mOnExitAlarm.cancelAlarm();
Michael Jurka54554252013-08-01 12:52:23 +0200736 if (!successfulDrop) {
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700737 mSuppressFolderDeletion = true;
738 }
Sunny Goyal5d85c442015-03-10 13:14:47 -0700739 mScrollPauseAlarm.cancelAlarm();
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700740 completeDragExit();
741 }
Adam Cohen9c58d822013-12-13 17:18:10 -0800742 }
743
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700744 mDeleteFolderOnDropCompleted = false;
745 mDragInProgress = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700746 mItemAddedBackToSelfViaIcon = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700747 mCurrentDragInfo = null;
748 mCurrentDragView = null;
749 mSuppressOnAdd = false;
Adam Cohen4045eb72011-10-06 11:44:26 -0700750
751 // Reordering may have occured, and we need to save the new item locations. We do this once
752 // at the end to prevent unnecessary database operations.
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700753 updateItemLocationsInDatabaseBatch();
Adam Cohen4045eb72011-10-06 11:44:26 -0700754 }
755
Michael Jurka1e2f4652013-07-08 18:03:46 -0700756 public void deferCompleteDropAfterUninstallActivity() {
757 mDeferDropAfterUninstall = true;
758 }
759
760 public void onUninstallActivityReturned(boolean success) {
761 mDeferDropAfterUninstall = false;
762 mUninstallSuccessful = success;
763 if (mDeferredAction != null) {
764 mDeferredAction.run();
765 }
766 }
767
Winson Chunga48487a2012-03-20 16:19:37 -0700768 @Override
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800769 public float getIntrinsicIconScaleFactor() {
770 return 1f;
771 }
772
773 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800774 public boolean supportsFlingToDelete() {
775 return true;
776 }
777
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000778 @Override
779 public boolean supportsAppInfoDropTarget() {
780 return false;
781 }
782
783 @Override
784 public boolean supportsDeleteDropTarget() {
785 return true;
786 }
787
Winson Chunga48487a2012-03-20 16:19:37 -0700788 public void onFlingToDelete(DragObject d, int x, int y, PointF vec) {
789 // Do nothing
790 }
791
792 @Override
793 public void onFlingToDeleteCompleted() {
794 // Do nothing
795 }
796
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700797 private void updateItemLocationsInDatabaseBatch() {
798 ArrayList<View> list = getItemsInReadingOrder();
799 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
800 for (int i = 0; i < list.size(); i++) {
801 View v = list.get(i);
802 ItemInfo info = (ItemInfo) v.getTag();
Sunny Goyal08f72612015-01-05 13:41:43 -0800803 info.rank = i;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700804 items.add(info);
805 }
806
807 LauncherModel.moveItemsInDatabase(mLauncher, items, mInfo.id, 0);
808 }
809
Adam Cohene25af792013-06-06 23:08:25 -0700810 public void addItemLocationsInDatabase() {
811 ArrayList<View> list = getItemsInReadingOrder();
812 for (int i = 0; i < list.size(); i++) {
813 View v = list.get(i);
814 ItemInfo info = (ItemInfo) v.getTag();
815 LauncherModel.addItemToDatabase(mLauncher, info, mInfo.id, 0,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700816 info.cellX, info.cellY);
Adam Cohene25af792013-06-06 23:08:25 -0700817 }
818 }
819
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700820 public void notifyDrop() {
821 if (mDragInProgress) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700822 mItemAddedBackToSelfViaIcon = true;
Adam Cohen76078c42011-06-09 15:06:52 -0700823 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700824 }
825
826 public boolean isDropEnabled() {
827 return true;
828 }
829
Adam Cohen2801caf2011-05-13 20:57:39 -0700830 public boolean isFull() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800831 return mContent.isFull();
Adam Cohen2801caf2011-05-13 20:57:39 -0700832 }
833
834 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700835 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700836
Winson Chung892c74d2013-08-22 16:15:50 -0700837 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700838 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700839 int height = getFolderHeight();
Adam Cohen2801caf2011-05-13 20:57:39 -0700840
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800841 float scale = parent.getDescendantRectRelativeToSelf(mFolderIcon, sTempRect);
Adam Cohen8e776a62011-06-28 18:10:06 -0700842
Winson Chungaf40f202013-09-18 18:26:31 -0700843 LauncherAppState app = LauncherAppState.getInstance();
844 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
845
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800846 int centerX = (int) (sTempRect.left + sTempRect.width() * scale / 2);
847 int centerY = (int) (sTempRect.top + sTempRect.height() * scale / 2);
Adam Cohen2801caf2011-05-13 20:57:39 -0700848 int centeredLeft = centerX - width / 2;
849 int centeredTop = centerY - height / 2;
Winson Chung3057b1c2013-10-10 17:35:35 -0700850 int currentPage = mLauncher.getWorkspace().getNextPage();
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800851 // In case the workspace is scrolling, we need to use the final scroll to compute
852 // the folders bounds.
853 mLauncher.getWorkspace().setFinalScrollForPageChange(currentPage);
Adam Cohen35e7e642011-07-17 14:47:18 -0700854 // We first fetch the currently visible CellLayoutChildren
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800855 CellLayout currentLayout = (CellLayout) mLauncher.getWorkspace().getChildAt(currentPage);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700856 ShortcutAndWidgetContainer boundingLayout = currentLayout.getShortcutsAndWidgets();
Adam Cohen35e7e642011-07-17 14:47:18 -0700857 Rect bounds = new Rect();
858 parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800859 // We reset the workspaces scroll
860 mLauncher.getWorkspace().resetFinalScrollForPageChange(currentPage);
Adam Cohen2801caf2011-05-13 20:57:39 -0700861
Adam Cohen35e7e642011-07-17 14:47:18 -0700862 // We need to bound the folder to the currently visible CellLayoutChildren
863 int left = Math.min(Math.max(bounds.left, centeredLeft),
864 bounds.left + bounds.width() - width);
865 int top = Math.min(Math.max(bounds.top, centeredTop),
866 bounds.top + bounds.height() - height);
Winson Chungaf40f202013-09-18 18:26:31 -0700867 if (grid.isPhone() && (grid.availableWidthPx - width) < grid.iconSizePx) {
868 // Center the folder if it is full (on phones only)
869 left = (grid.availableWidthPx - width) / 2;
870 } else if (width >= bounds.width()) {
871 // If the folder doesn't fit within the bounds, center it about the desired bounds
Adam Cohen35e7e642011-07-17 14:47:18 -0700872 left = bounds.left + (bounds.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700873 }
Adam Cohen35e7e642011-07-17 14:47:18 -0700874 if (height >= bounds.height()) {
875 top = bounds.top + (bounds.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700876 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700877
878 int folderPivotX = width / 2 + (centeredLeft - left);
879 int folderPivotY = height / 2 + (centeredTop - top);
880 setPivotX(folderPivotX);
881 setPivotY(folderPivotY);
Adam Cohen268c4752012-06-06 17:47:33 -0700882 mFolderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700883 (1.0f * folderPivotX / width));
Adam Cohen268c4752012-06-06 17:47:33 -0700884 mFolderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700885 (1.0f * folderPivotY / height));
Adam Cohen3bf84d32012-05-07 20:17:14 -0700886
Adam Cohen662b5982011-12-13 17:45:21 -0800887 lp.width = width;
888 lp.height = height;
889 lp.x = left;
890 lp.y = top;
Adam Cohen2801caf2011-05-13 20:57:39 -0700891 }
892
Adam Cohen268c4752012-06-06 17:47:33 -0700893 float getPivotXForIconAnimation() {
894 return mFolderIconPivotX;
895 }
896 float getPivotYForIconAnimation() {
897 return mFolderIconPivotY;
898 }
899
Winson Chung892c74d2013-08-22 16:15:50 -0700900 private int getContentAreaHeight() {
901 LauncherAppState app = LauncherAppState.getInstance();
902 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
903 Rect workspacePadding = grid.getWorkspacePadding(grid.isLandscape ?
904 CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
905 int maxContentAreaHeight = grid.availableHeightPx -
Winson Chung892c74d2013-08-22 16:15:50 -0700906 workspacePadding.top - workspacePadding.bottom -
Sunny Goyal290800b2015-03-05 11:33:33 -0800907 mFooterHeight;
Adam Cohen1960ea42013-11-12 11:33:14 +0000908 int height = Math.min(maxContentAreaHeight,
Winson Chung892c74d2013-08-22 16:15:50 -0700909 mContent.getDesiredHeight());
Adam Cohen1960ea42013-11-12 11:33:14 +0000910 return Math.max(height, MIN_CONTENT_DIMEN);
911 }
912
913 private int getContentAreaWidth() {
914 return Math.max(mContent.getDesiredWidth(), MIN_CONTENT_DIMEN);
Winson Chung892c74d2013-08-22 16:15:50 -0700915 }
916
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700917 private int getFolderHeight() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800918 return getFolderHeight(getContentAreaHeight());
919 }
920
921 private int getFolderHeight(int contentAreaHeight) {
Sunny Goyal290800b2015-03-05 11:33:33 -0800922 return getPaddingTop() + getPaddingBottom() + contentAreaHeight + mFooterHeight;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700923 }
Adam Cohen234c4cd2011-07-17 21:03:04 -0700924
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700925 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800926 int contentWidth = getContentAreaWidth();
927 int contentHeight = getContentAreaHeight();
Adam Cohen1960ea42013-11-12 11:33:14 +0000928
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800929 int contentAreaWidthSpec = MeasureSpec.makeMeasureSpec(contentWidth, MeasureSpec.EXACTLY);
930 int contentAreaHeightSpec = MeasureSpec.makeMeasureSpec(contentHeight, MeasureSpec.EXACTLY);
931
932 mContent.setFixedSize(contentWidth, contentHeight);
933 mContentWrapper.measure(contentAreaWidthSpec, contentAreaHeightSpec);
Sunny Goyal290800b2015-03-05 11:33:33 -0800934 mFooter.measure(contentAreaWidthSpec,
935 MeasureSpec.makeMeasureSpec(mFooterHeight, MeasureSpec.EXACTLY));
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800936
937 int folderWidth = getPaddingLeft() + getPaddingRight() + contentWidth;
938 int folderHeight = getFolderHeight(contentHeight);
939 setMeasuredDimension(folderWidth, folderHeight);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700940 }
941
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800942 /**
943 * Rearranges the children based on their rank.
944 */
945 public void rearrangeChildren() {
946 rearrangeChildren(-1);
947 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700948
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800949 /**
950 * Rearranges the children based on their rank.
951 * @param itemCount if greater than the total children count, empty spaces are left at the end,
952 * otherwise it is ignored.
953 */
954 public void rearrangeChildren(int itemCount) {
955 ArrayList<View> views = getItemsInReadingOrder();
956 mContent.arrangeChildren(views, Math.max(itemCount, views.size()));
Adam Cohen7c693212011-05-18 15:26:57 -0700957 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700958 }
959
Sunny Goyalc4918352015-03-10 18:15:48 -0700960 // TODO remove this once GSA code fix is submitted
961 public ViewGroup getContent() {
962 return (ViewGroup) mContent;
963 }
964
Adam Cohena9cf38f2011-05-02 15:36:58 -0700965 public int getItemCount() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800966 return mContent.getItemCount();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700967 }
968
Adam Cohen091440a2015-03-18 14:16:05 -0700969 @Thunk void onCloseComplete() {
Adam Cohen05e0f402011-08-01 12:12:49 -0700970 DragLayer parent = (DragLayer) getParent();
Michael Jurka5649c282012-06-18 10:33:21 -0700971 if (parent != null) {
972 parent.removeView(this);
973 }
Adam Cohen4554ee12011-08-03 16:13:21 -0700974 mDragController.removeDropTarget((DropTarget) this);
Adam Cohen05e0f402011-08-01 12:12:49 -0700975 clearFocus();
Adam Cohenac56cff2011-09-28 20:45:37 -0700976 mFolderIcon.requestFocus();
Adam Cohen05e0f402011-08-01 12:12:49 -0700977
Adam Cohen2801caf2011-05-13 20:57:39 -0700978 if (mRearrangeOnClose) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800979 rearrangeChildren();
Adam Cohen2801caf2011-05-13 20:57:39 -0700980 mRearrangeOnClose = false;
981 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700982 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700983 if (!mDragInProgress && !mSuppressFolderDeletion) {
984 replaceFolderWithFinalItem();
985 } else if (mDragInProgress) {
986 mDeleteFolderOnDropCompleted = true;
987 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700988 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700989 mSuppressFolderDeletion = false;
Adam Cohenafb01ee2011-06-23 15:38:03 -0700990 }
991
Adam Cohen091440a2015-03-18 14:16:05 -0700992 @Thunk void replaceFolderWithFinalItem() {
Adam Cohenafb01ee2011-06-23 15:38:03 -0700993 // Add the last remaining child to the workspace in place of the folder
Adam Cohenfb91f302012-06-11 15:45:18 -0700994 Runnable onCompleteRunnable = new Runnable() {
995 @Override
996 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -0700997 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screenId);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700998
Winson Chung33231f52013-12-09 16:57:45 -0800999 View child = null;
Adam Cohenfb91f302012-06-11 15:45:18 -07001000 // Move the item from the folder to the workspace, in the position of the folder
1001 if (getItemCount() == 1) {
1002 ShortcutInfo finalItem = mInfo.contents.get(0);
Adam Cohenc5e63f32012-07-12 16:16:57 -07001003 child = mLauncher.createShortcut(R.layout.application, cellLayout,
Adam Cohenfb91f302012-06-11 15:45:18 -07001004 finalItem);
1005 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -07001006 mInfo.screenId, mInfo.cellX, mInfo.cellY);
Adam Cohenfb91f302012-06-11 15:45:18 -07001007 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001008 if (getItemCount() <= 1) {
1009 // Remove the folder
1010 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
Dan Sandler0eb687f2014-01-10 13:24:55 -05001011 if (cellLayout != null) {
1012 // b/12446428 -- sometimes the cell layout has already gone away?
1013 cellLayout.removeView(mFolderIcon);
1014 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001015 if (mFolderIcon instanceof DropTarget) {
1016 mDragController.removeDropTarget((DropTarget) mFolderIcon);
1017 }
1018 mLauncher.removeFolder(mInfo);
1019 }
Adam Cohenc5e63f32012-07-12 16:16:57 -07001020 // We add the child after removing the folder to prevent both from existing at
Winson Chung0e6a7132013-08-23 12:55:10 -07001021 // the same time in the CellLayout. We need to add the new item with addInScreenFromBind()
1022 // to ensure that hotseat items are placed correctly.
Adam Cohenc5e63f32012-07-12 16:16:57 -07001023 if (child != null) {
Winson Chung0e6a7132013-08-23 12:55:10 -07001024 mLauncher.getWorkspace().addInScreenFromBind(child, mInfo.container, mInfo.screenId,
Adam Cohenc5e63f32012-07-12 16:16:57 -07001025 mInfo.cellX, mInfo.cellY, mInfo.spanX, mInfo.spanY);
1026 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001027 }
1028 };
Sunny Goyalbc753352015-03-05 09:40:44 -08001029 View finalChild = mContent.getLastItem();
Adam Cohenfb91f302012-06-11 15:45:18 -07001030 if (finalChild != null) {
1031 mFolderIcon.performDestroyAnimation(finalChild, onCompleteRunnable);
Winson Chung33231f52013-12-09 16:57:45 -08001032 } else {
1033 onCompleteRunnable.run();
Adam Cohenafb01ee2011-06-23 15:38:03 -07001034 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001035 mDestroyed = true;
1036 }
1037
1038 boolean isDestroyed() {
1039 return mDestroyed;
Adam Cohen2801caf2011-05-13 20:57:39 -07001040 }
1041
Adam Cohenac56cff2011-09-28 20:45:37 -07001042 // This method keeps track of the last item in the folder for the purposes
1043 // of keyboard focus
Sunny Goyal290800b2015-03-05 11:33:33 -08001044 public void updateTextViewFocus() {
Sunny Goyalbc753352015-03-05 09:40:44 -08001045 View lastChild = mContent.getLastItem();
Adam Cohenac56cff2011-09-28 20:45:37 -07001046 if (lastChild != null) {
1047 mFolderName.setNextFocusDownId(lastChild.getId());
1048 mFolderName.setNextFocusRightId(lastChild.getId());
1049 mFolderName.setNextFocusLeftId(lastChild.getId());
1050 mFolderName.setNextFocusUpId(lastChild.getId());
1051 }
1052 }
1053
Adam Cohenbfbfd262011-06-13 16:55:12 -07001054 public void onDrop(DragObject d) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001055 Runnable cleanUpRunnable = null;
1056
Adam Cohen689ff162014-05-08 17:27:56 -07001057 // If we are coming from All Apps space, we defer removing the extra empty screen
1058 // until the folder closes
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001059 if (d.dragSource != mLauncher.getWorkspace() && !(d.dragSource instanceof Folder)) {
1060 cleanUpRunnable = new Runnable() {
1061 @Override
1062 public void run() {
Adam Cohen689ff162014-05-08 17:27:56 -07001063 mLauncher.exitSpringLoadedDragModeDelayed(true,
1064 Launcher.EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
1065 null);
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001066 }
1067 };
Adam Cohenbfbfd262011-06-13 16:55:12 -07001068 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001069
Sunny Goyalb8634152015-04-09 14:17:14 -07001070 // If the icon was dropped while the page was being scrolled, we need to compute
1071 // the target location again such that the icon is placed of the final page.
1072 if (!mContent.rankOnCurrentPage(mEmptyCellRank)) {
1073 // Reorder again.
1074 mTargetRank = getTargetRank(d, null);
Sunny Goyal48461932015-03-09 17:41:09 -07001075
Sunny Goyalb8634152015-04-09 14:17:14 -07001076 // Rearrange items immediately.
1077 mReorderAlarmListener.onAlarm(mReorderAlarm);
Sunny Goyal48461932015-03-09 17:41:09 -07001078
Sunny Goyalb8634152015-04-09 14:17:14 -07001079 mOnScrollHintAlarm.cancelAlarm();
1080 mScrollPauseAlarm.cancelAlarm();
Sunny Goyal48461932015-03-09 17:41:09 -07001081 }
Sunny Goyalb8634152015-04-09 14:17:14 -07001082 mContent.completePendingPageChanges();
Sunny Goyal48461932015-03-09 17:41:09 -07001083
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001084 View currentDragView;
1085 ShortcutInfo si = mCurrentDragInfo;
1086 if (mIsExternalDrag) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001087 currentDragView = mContent.createAndAddViewForRank(si, mEmptyCellRank);
Sunny Goyal95abbb32014-08-04 10:53:22 -07001088 // Actually move the item in the database if it was an external drag. Call this
1089 // before creating the view, so that ShortcutInfo is updated appropriately.
1090 LauncherModel.addOrMoveItemInDatabase(
1091 mLauncher, si, mInfo.id, 0, si.cellX, si.cellY);
1092
1093 // We only need to update the locations if it doesn't get handled in #onDropCompleted.
1094 if (d.dragSource != this) {
1095 updateItemLocationsInDatabaseBatch();
1096 }
1097 mIsExternalDrag = false;
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001098 } else {
1099 currentDragView = mCurrentDragView;
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001100 mContent.addViewForRank(currentDragView, si, mEmptyCellRank);
Adam Cohenbfbfd262011-06-13 16:55:12 -07001101 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001102
1103 if (d.dragView.hasDrawn()) {
1104
1105 // Temporarily reset the scale such that the animation target gets calculated correctly.
1106 float scaleX = getScaleX();
1107 float scaleY = getScaleY();
1108 setScaleX(1.0f);
1109 setScaleY(1.0f);
1110 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, currentDragView,
1111 cleanUpRunnable, null);
1112 setScaleX(scaleX);
1113 setScaleY(scaleY);
1114 } else {
1115 d.deferDragViewCleanupPostAnimation = false;
1116 currentDragView.setVisibility(VISIBLE);
1117 }
1118 mItemsInvalidated = true;
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001119 rearrangeChildren();
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001120
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001121 // Temporarily suppress the listener, as we did all the work already here.
1122 mSuppressOnAdd = true;
1123 mInfo.add(si);
1124 mSuppressOnAdd = false;
Sunny Goyal4b020172014-08-28 14:51:14 -07001125 // Clear the drag info, as it is no longer being dragged.
1126 mCurrentDragInfo = null;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001127 }
1128
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001129 // This is used so the item doesn't immediately appear in the folder when added. In one case
1130 // we need to create the illusion that the item isn't added back to the folder yet, to
1131 // to correspond to the animation of the icon back into the folder. This is
1132 public void hideItem(ShortcutInfo info) {
1133 View v = getViewForInfo(info);
1134 v.setVisibility(INVISIBLE);
1135 }
1136 public void showItem(ShortcutInfo info) {
1137 View v = getViewForInfo(info);
1138 v.setVisibility(VISIBLE);
1139 }
1140
Adam Cohenbfbfd262011-06-13 16:55:12 -07001141 public void onAdd(ShortcutInfo item) {
Adam Cohen05e0f402011-08-01 12:12:49 -07001142 // If the item was dropped onto this open folder, we have done the work associated
1143 // with adding the item to the folder, as indicated by mSuppressOnAdd being set
Adam Cohenbfbfd262011-06-13 16:55:12 -07001144 if (mSuppressOnAdd) return;
Sunny Goyal5d85c442015-03-10 13:14:47 -07001145 mContent.createAndAddViewForRank(item, mContent.allocateRankForNewItem(item));
Sunny Goyal2e688a82015-03-18 10:23:39 -07001146 mItemsInvalidated = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001147 LauncherModel.addOrMoveItemInDatabase(
1148 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
1149 }
1150
Adam Cohena9cf38f2011-05-02 15:36:58 -07001151 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -07001152 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001153 // If this item is being dragged from this open folder, we have already handled
1154 // the work associated with removing the item, so we don't have to do anything here.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001155 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001156 View v = getViewForInfo(item);
Sunny Goyal290800b2015-03-05 11:33:33 -08001157 mContent.removeItem(v);
Adam Cohen2801caf2011-05-13 20:57:39 -07001158 if (mState == STATE_ANIMATING) {
1159 mRearrangeOnClose = true;
1160 } else {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001161 rearrangeChildren();
Adam Cohen2801caf2011-05-13 20:57:39 -07001162 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001163 if (getItemCount() <= 1) {
1164 replaceFolderWithFinalItem();
1165 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001166 }
Adam Cohen7c693212011-05-18 15:26:57 -07001167
Sunny Goyalbc753352015-03-05 09:40:44 -08001168 private View getViewForInfo(final ShortcutInfo item) {
1169 return mContent.iterateOverItems(new ItemOperator() {
1170
1171 @Override
1172 public boolean evaluate(ItemInfo info, View view, View parent) {
1173 return info == item;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001174 }
Sunny Goyalbc753352015-03-05 09:40:44 -08001175 });
Adam Cohendf1e4e82011-06-24 15:57:39 -07001176 }
1177
Adam Cohen76078c42011-06-09 15:06:52 -07001178 public void onItemsChanged() {
Adam Cohenac56cff2011-09-28 20:45:37 -07001179 updateTextViewFocus();
Adam Cohen76078c42011-06-09 15:06:52 -07001180 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001181
Adam Cohen76fc0852011-06-17 13:26:23 -07001182 public void onTitleChanged(CharSequence title) {
1183 }
Adam Cohen76078c42011-06-09 15:06:52 -07001184
Adam Cohen7c693212011-05-18 15:26:57 -07001185 public ArrayList<View> getItemsInReadingOrder() {
1186 if (mItemsInvalidated) {
1187 mItemsInReadingOrder.clear();
Sunny Goyalbc753352015-03-05 09:40:44 -08001188 mContent.iterateOverItems(new ItemOperator() {
1189
1190 @Override
1191 public boolean evaluate(ItemInfo info, View view, View parent) {
1192 mItemsInReadingOrder.add(view);
1193 return false;
Adam Cohen7c693212011-05-18 15:26:57 -07001194 }
Sunny Goyalbc753352015-03-05 09:40:44 -08001195 });
Adam Cohen7c693212011-05-18 15:26:57 -07001196 mItemsInvalidated = false;
1197 }
1198 return mItemsInReadingOrder;
1199 }
Adam Cohen8dfcba42011-07-07 16:38:18 -07001200
1201 public void getLocationInDragLayer(int[] loc) {
1202 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
1203 }
Adam Cohenea0818d2011-09-30 20:06:58 -07001204
1205 public void onFocusChange(View v, boolean hasFocus) {
1206 if (v == mFolderName && hasFocus) {
1207 startEditingFolderName();
1208 }
1209 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001210
1211 @Override
1212 public void getHitRectRelativeToDragLayer(Rect outRect) {
1213 getHitRect(outRect);
Sunny Goyal48461932015-03-09 17:41:09 -07001214 outRect.left -= mScrollAreaOffset;
1215 outRect.right += mScrollAreaOffset;
1216 }
1217
1218 private class OnScrollHintListener implements OnAlarmListener {
1219
1220 private final DragObject mDragObject;
1221
1222 OnScrollHintListener(DragObject object) {
1223 mDragObject = object;
1224 }
1225
1226 /**
1227 * Scroll hint has been shown long enough. Now scroll to appropriate page.
1228 */
1229 @Override
1230 public void onAlarm(Alarm alarm) {
1231 if (mCurrentScrollDir == DragController.SCROLL_LEFT) {
Sunny Goyalb8634152015-04-09 14:17:14 -07001232 mContent.scrollLeft();
Sunny Goyal48461932015-03-09 17:41:09 -07001233 mScrollHintDir = DragController.SCROLL_NONE;
1234 } else if (mCurrentScrollDir == DragController.SCROLL_RIGHT) {
Sunny Goyalb8634152015-04-09 14:17:14 -07001235 mContent.scrollRight();
Sunny Goyal48461932015-03-09 17:41:09 -07001236 mScrollHintDir = DragController.SCROLL_NONE;
1237 } else {
1238 // This should not happen
1239 return;
1240 }
1241 mCurrentScrollDir = DragController.SCROLL_NONE;
1242
1243 // Pause drag event until the scrolling is finished
1244 mScrollPauseAlarm.setOnAlarmListener(new OnScrollFinishedListener(mDragObject));
1245 mScrollPauseAlarm.setAlarm(DragController.RESCROLL_DELAY);
1246 }
1247 }
1248
1249 private class OnScrollFinishedListener implements OnAlarmListener {
1250
1251 private final DragObject mDragObject;
1252
1253 OnScrollFinishedListener(DragObject object) {
1254 mDragObject = object;
1255 }
1256
1257 /**
1258 * Page scroll is complete.
1259 */
1260 @Override
1261 public void onAlarm(Alarm alarm) {
1262 // Reorder immediately on page change.
1263 onDragOver(mDragObject, 1);
1264 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001265 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001266}