blob: deb94ca28c0374b41acd17e2c5f7aa812faf8f04 [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;
Romain Guyedcce092010-03-04 13:03:17 -080054
Adam Cohenc0dcf592011-06-01 15:30:43 -070055import java.util.ArrayList;
Adam Cohen3bf84d32012-05-07 20:17:14 -070056import java.util.Collections;
Adam Cohenc0dcf592011-06-01 15:30:43 -070057
The Android Open Source Project31dd5032009-03-03 19:32:27 -080058/**
59 * Represents a set of icons chosen by the user or generated by the system.
60 */
Adam Cohen8dfcba42011-07-07 16:38:18 -070061public class Folder extends LinearLayout implements DragSource, View.OnClickListener,
Adam Cohenea0818d2011-09-30 20:06:58 -070062 View.OnLongClickListener, DropTarget, FolderListener, TextView.OnEditorActionListener,
63 View.OnFocusChangeListener {
Adam Cohendf2cc412011-04-27 16:56:57 -070064 private static final String TAG = "Launcher.Folder";
65
Sunny Goyalc3a609f2015-02-26 17:43:50 -080066 /**
67 * We avoid measuring {@link #mContentWrapper} with a 0 width or height, as this
68 * results in CellLayout being measured as UNSPECIFIED, which it does not support.
69 */
70 private static final int MIN_CONTENT_DIMEN = 5;
Sunny Goyal290800b2015-03-05 11:33:33 -080071 private static final boolean ALLOW_FOLDER_SCROLL = true;
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 /**
79 * Fraction of the width to scroll when showing the next page hint.
80 */
81 private static final float SCROLL_HINT_FRACTION = 0.07f;
82
83 /**
84 * Time for which the scroll hint is shown before automatically changing page.
85 */
86 public static final int SCROLL_HINT_DURATION = DragController.SCROLL_DELAY;
87
88 /**
Sunny Goyal5d85c442015-03-10 13:14:47 -070089 * Time in milliseconds for which an icon sticks to the target position
90 * in case of a sorted folder.
91 */
92 private static final int SORTED_STICKY_REORDER_DELAY = 1500;
93
94 /**
Sunny Goyal48461932015-03-09 17:41:09 -070095 * Fraction of icon width which behave as scroll region.
96 */
97 private static final float ICON_OVERSCROLL_WIDTH_FACTOR = 0.45f;
98
Adam Cohenf0f4eda2013-06-06 21:27:03 -070099 private static final int REORDER_DELAY = 250;
Adam Cohen5d518fa2013-12-05 14:16:23 -0800100 private static final int ON_EXIT_CLOSE_DELAY = 400;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800101 private static final Rect sTempRect = new Rect();
102
103 private static String sDefaultFolderName;
104 private static String sHintText;
105
106 private final Alarm mReorderAlarm = new Alarm();
107 private final Alarm mOnExitAlarm = new Alarm();
108
109 private final ArrayList<View> mItemsInReadingOrder = new ArrayList<View>();
110
111 private final int mExpandDuration;
112 private final int mMaterialExpandDuration;
113 private final int mMaterialExpandStagger;
114
115 private final InputMethodManager mInputMethodManager;
116
117 protected final Launcher mLauncher;
118 protected DragController mDragController;
119 protected FolderInfo mInfo;
120
Adam Cohen2801caf2011-05-13 20:57:39 -0700121 private FolderIcon mFolderIcon;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800122
Sunny Goyalbc753352015-03-05 09:40:44 -0800123 private FolderContent mContent;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800124 private View mContentWrapper;
125 FolderEditText mFolderName;
126
Sunny Goyal290800b2015-03-05 11:33:33 -0800127 private View mFooter;
128 private int mFooterHeight;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800129
130 // Cell ranks used for drag and drop
131 private int mTargetRank, mPrevTargetRank, mEmptyCellRank;
132
133 private int mState = STATE_NONE;
134 private boolean mRearrangeOnClose = false;
Adam Cohen7c693212011-05-18 15:26:57 -0700135 boolean mItemsInvalidated = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700136 private ShortcutInfo mCurrentDragInfo;
137 private View mCurrentDragView;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800138 private boolean mIsExternalDrag;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700139 boolean mSuppressOnAdd = false;
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700140 private boolean mDragInProgress = false;
141 private boolean mDeleteFolderOnDropCompleted = false;
142 private boolean mSuppressFolderDeletion = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700143 private boolean mItemAddedBackToSelfViaIcon = false;
Adam Cohen268c4752012-06-06 17:47:33 -0700144 private float mFolderIconPivotX;
145 private float mFolderIconPivotY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700146 private boolean mIsEditingName = false;
Adam Cohen1960ea42013-11-12 11:33:14 +0000147
Adam Cohenfb91f302012-06-11 15:45:18 -0700148 private boolean mDestroyed;
149
Michael Jurka1e2f4652013-07-08 18:03:46 -0700150 private Runnable mDeferredAction;
151 private boolean mDeferDropAfterUninstall;
152 private boolean mUninstallSuccessful;
153
Sunny Goyal48461932015-03-09 17:41:09 -0700154 // Folder scrolling
155 private int mScrollAreaOffset;
156 private Alarm mOnScrollHintAlarm;
157 private Alarm mScrollPauseAlarm;
158
159 // TODO: Use {@link #mContent} once {@link #ALLOW_FOLDER_SCROLL} is removed.
160 private FolderPagedView mPagedView;
161
162 private int mScrollHintDir = DragController.SCROLL_NONE;
163 private int mCurrentScrollDir = DragController.SCROLL_NONE;
164
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165 /**
166 * Used to inflate the Workspace from XML.
167 *
168 * @param context The application's context.
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800169 * @param attrs The attributes set containing the Workspace's customization values.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170 */
171 public Folder(Context context, AttributeSet attrs) {
172 super(context, attrs);
173 setAlwaysDrawnWithCacheEnabled(false);
Adam Cohen76fc0852011-06-17 13:26:23 -0700174 mInputMethodManager = (InputMethodManager)
Michael Jurka8b805b12012-04-18 14:23:14 -0700175 getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
Adam Cohen76fc0852011-06-17 13:26:23 -0700176
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800177 Resources res = getResources();
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700178 mExpandDuration = res.getInteger(R.integer.config_folderExpandDuration);
179 mMaterialExpandDuration = res.getInteger(R.integer.config_materialFolderExpandDuration);
180 mMaterialExpandStagger = res.getInteger(R.integer.config_materialFolderExpandStagger);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700181
182 if (sDefaultFolderName == null) {
183 sDefaultFolderName = res.getString(R.string.folder_name);
184 }
Adam Cohena65beee2011-06-27 21:32:23 -0700185 if (sHintText == null) {
186 sHintText = res.getString(R.string.folder_hint_text);
187 }
Adam Cohen4eac29a2011-07-11 17:53:37 -0700188 mLauncher = (Launcher) context;
Adam Cohenac56cff2011-09-28 20:45:37 -0700189 // We need this view to be focusable in touch mode so that when text editing of the folder
190 // name is complete, we have something to focus on, thus hiding the cursor and giving
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800191 // reliable behavior when clicking the text field (since it will always gain focus on click).
Adam Cohenac56cff2011-09-28 20:45:37 -0700192 setFocusableInTouchMode(true);
Sunny Goyal48461932015-03-09 17:41:09 -0700193
194 if (ALLOW_FOLDER_SCROLL) {
195 mOnScrollHintAlarm = new Alarm();
196 mScrollPauseAlarm = new Alarm();
197 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800198 }
199
200 @Override
201 protected void onFinishInflate() {
202 super.onFinishInflate();
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800203 mContentWrapper = findViewById(R.id.folder_content_wrapper);
Sunny Goyalbc753352015-03-05 09:40:44 -0800204 mContent = (FolderContent) findViewById(R.id.folder_content);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800205 mContent.setFolder(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700206
Adam Cohenac56cff2011-09-28 20:45:37 -0700207 mFolderName = (FolderEditText) findViewById(R.id.folder_name);
208 mFolderName.setFolder(this);
Adam Cohenea0818d2011-09-30 20:06:58 -0700209 mFolderName.setOnFocusChangeListener(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700210
Adam Cohen76fc0852011-06-17 13:26:23 -0700211 // We disable action mode for now since it messes up the view on phones
212 mFolderName.setCustomSelectionActionModeCallback(mActionModeCallback);
Adam Cohen76fc0852011-06-17 13:26:23 -0700213 mFolderName.setOnEditorActionListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700214 mFolderName.setSelectAllOnFocus(true);
Adam Cohen7a14d0b2011-06-24 11:36:35 -0700215 mFolderName.setInputType(mFolderName.getInputType() |
216 InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800217
Sunny Goyal290800b2015-03-05 11:33:33 -0800218 mFooter = ALLOW_FOLDER_SCROLL ? findViewById(R.id.folder_footer) : mFolderName;
219 // We find out how tall footer wants to be (it is set to wrap_content), so that
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800220 // we can allocate the appropriate amount of space for it.
221 int measureSpec = MeasureSpec.UNSPECIFIED;
Sunny Goyal290800b2015-03-05 11:33:33 -0800222 mFooter.measure(measureSpec, measureSpec);
223 mFooterHeight = mFooter.getMeasuredHeight();
Sunny Goyal48461932015-03-09 17:41:09 -0700224
225 if (ALLOW_FOLDER_SCROLL) {
226 mPagedView = (FolderPagedView) mContent;
227 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800228 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700229
Adam Cohen76fc0852011-06-17 13:26:23 -0700230 private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
231 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
232 return false;
233 }
234
235 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
236 return false;
237 }
238
239 public void onDestroyActionMode(ActionMode mode) {
240 }
241
242 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
243 return false;
244 }
245 };
246
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800247 public void onClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700248 Object tag = v.getTag();
249 if (tag instanceof ShortcutInfo) {
Adam Cohenb5fe60c2013-06-06 22:03:51 -0700250 mLauncher.onClick(v);
Adam Cohendf2cc412011-04-27 16:56:57 -0700251 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800252 }
253
254 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -0700255 // Return if global dragging is not enabled
256 if (!mLauncher.isDraggingEnabled()) return true;
257
Adam Cohendf2cc412011-04-27 16:56:57 -0700258 Object tag = v.getTag();
259 if (tag instanceof ShortcutInfo) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700260 ShortcutInfo item = (ShortcutInfo) tag;
261 if (!v.isInTouchMode()) {
262 return false;
263 }
264
Winson Chungb745afb2015-03-02 11:51:23 -0800265 mLauncher.getWorkspace().beginDragShared(v, new Point(), this, false);
Adam Cohen76078c42011-06-09 15:06:52 -0700266
267 mCurrentDragInfo = item;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800268 mEmptyCellRank = item.rank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700269 mCurrentDragView = v;
Adam Cohenfc53cd22011-07-20 15:45:11 -0700270
Sunny Goyal290800b2015-03-05 11:33:33 -0800271 mContent.removeItem(mCurrentDragView);
Adam Cohenfc53cd22011-07-20 15:45:11 -0700272 mInfo.remove(mCurrentDragInfo);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700273 mDragInProgress = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700274 mItemAddedBackToSelfViaIcon = false;
Adam Cohendf2cc412011-04-27 16:56:57 -0700275 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800276 return true;
277 }
278
Adam Cohen76fc0852011-06-17 13:26:23 -0700279 public boolean isEditingName() {
280 return mIsEditingName;
281 }
282
283 public void startEditingFolderName() {
Adam Cohena65beee2011-06-27 21:32:23 -0700284 mFolderName.setHint("");
Adam Cohen76fc0852011-06-17 13:26:23 -0700285 mIsEditingName = true;
286 }
287
288 public void dismissEditingName() {
289 mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
290 doneEditingFolderName(true);
291 }
292
293 public void doneEditingFolderName(boolean commit) {
Adam Cohena65beee2011-06-27 21:32:23 -0700294 mFolderName.setHint(sHintText);
Adam Cohen1df26a32011-08-12 16:15:23 -0700295 // Convert to a string here to ensure that no other state associated with the text field
296 // gets saved.
Adam Cohen3371da02011-10-25 21:38:29 -0700297 String newTitle = mFolderName.getText().toString();
298 mInfo.setTitle(newTitle);
Adam Cohen76fc0852011-06-17 13:26:23 -0700299 LauncherModel.updateItemInDatabase(mLauncher, mInfo);
Adam Cohenac56cff2011-09-28 20:45:37 -0700300
Adam Cohen3371da02011-10-25 21:38:29 -0700301 if (commit) {
302 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700303 String.format(getContext().getString(R.string.folder_renamed), newTitle));
Adam Cohen3371da02011-10-25 21:38:29 -0700304 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700305 // In order to clear the focus from the text field, we set the focus on ourself. This
306 // ensures that every time the field is clicked, focus is gained, giving reliable behavior.
307 requestFocus();
308
Adam Cohene601a432011-07-26 21:51:30 -0700309 Selection.setSelection((Spannable) mFolderName.getText(), 0, 0);
Adam Cohen76fc0852011-06-17 13:26:23 -0700310 mIsEditingName = false;
311 }
312
313 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
314 if (actionId == EditorInfo.IME_ACTION_DONE) {
315 dismissEditingName();
316 return true;
317 }
318 return false;
319 }
320
321 public View getEditTextRegion() {
322 return mFolderName;
323 }
324
Adam Cohen0c872ba2011-05-05 10:34:16 -0700325 /**
326 * We need to handle touch events to prevent them from falling through to the workspace below.
327 */
328 @Override
329 public boolean onTouchEvent(MotionEvent ev) {
330 return true;
331 }
332
Joe Onorato00acb122009-08-04 16:04:30 -0400333 public void setDragController(DragController dragController) {
334 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800335 }
336
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800337 public void setFolderIcon(FolderIcon icon) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700338 mFolderIcon = icon;
339 }
340
Adam Cohen3371da02011-10-25 21:38:29 -0700341 @Override
342 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
343 // When the folder gets focus, we don't want to announce the list of items.
344 return true;
345 }
346
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800347 /**
348 * @return the FolderInfo object associated with this folder
349 */
350 FolderInfo getInfo() {
351 return mInfo;
352 }
353
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800354 void bind(FolderInfo info) {
355 mInfo = info;
Adam Cohendf2cc412011-04-27 16:56:57 -0700356 ArrayList<ShortcutInfo> children = info.contents;
Sunny Goyal08f72612015-01-05 13:41:43 -0800357 Collections.sort(children, Utilities.RANK_COMPARATOR);
Sunny Goyal08f72612015-01-05 13:41:43 -0800358
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800359 ArrayList<ShortcutInfo> overflow = mContent.bindItems(children);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700360
Jason Monk4ff73882014-04-24 16:48:07 -0400361 // If our folder has too many items we prune them from the list. This is an issue
Adam Cohenc508b2d2011-06-28 14:41:44 -0700362 // when upgrading from the old Folders implementation which could contain an unlimited
363 // number of items.
364 for (ShortcutInfo item: overflow) {
365 mInfo.remove(item);
366 LauncherModel.deleteItemFromDatabase(mLauncher, item);
367 }
368
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800369 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
370 if (lp == null) {
371 lp = new DragLayer.LayoutParams(0, 0);
372 lp.customPosition = true;
373 setLayoutParams(lp);
374 }
375 centerAboutIcon();
376
Adam Cohen4dbe6d92011-05-18 17:14:15 -0700377 mItemsInvalidated = true;
Adam Cohenac56cff2011-09-28 20:45:37 -0700378 updateTextViewFocus();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700379 mInfo.addListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700380
Adam Cohenafb01ee2011-06-23 15:38:03 -0700381 if (!sDefaultFolderName.contentEquals(mInfo.title)) {
Adam Cohen4ef610f2011-06-21 22:37:36 -0700382 mFolderName.setText(mInfo.title);
383 } else {
384 mFolderName.setText("");
385 }
Winson Chung33231f52013-12-09 16:57:45 -0800386
387 // In case any children didn't come across during loading, clean up the folder accordingly
388 mFolderIcon.post(new Runnable() {
389 public void run() {
390 if (getItemCount() <= 1) {
391 replaceFolderWithFinalItem();
392 }
393 }
394 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700395 }
396
397 /**
398 * Creates a new UserFolder, inflated from R.layout.user_folder.
399 *
400 * @param context The application's context.
401 *
402 * @return A new UserFolder.
403 */
404 static Folder fromXml(Context context) {
Sunny Goyal290800b2015-03-05 11:33:33 -0800405 return (Folder) LayoutInflater.from(context).inflate(
406 ALLOW_FOLDER_SCROLL ? R.layout.user_folder_scroll : R.layout.user_folder, null);
Adam Cohendf2cc412011-04-27 16:56:57 -0700407 }
408
409 /**
410 * This method is intended to make the UserFolder to be visually identical in size and position
411 * to its associated FolderIcon. This allows for a seamless transition into the expanded state.
412 */
413 private void positionAndSizeAsIcon() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700414 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800415 setScaleX(0.8f);
416 setScaleY(0.8f);
417 setAlpha(0f);
Adam Cohendf2cc412011-04-27 16:56:57 -0700418 mState = STATE_SMALL;
419 }
420
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700421 private void prepareReveal() {
422 setScaleX(1f);
423 setScaleY(1f);
424 setAlpha(1f);
425 mState = STATE_SMALL;
426 }
427
Adam Cohendf2cc412011-04-27 16:56:57 -0700428 public void animateOpen() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700429 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen6441de02011-12-14 14:25:32 -0800430
Sunny Goyal48461932015-03-09 17:41:09 -0700431 if (ALLOW_FOLDER_SCROLL) {
Sunny Goyal5d85c442015-03-10 13:14:47 -0700432 mPagedView.completePendingPageChanges();
433 if (!(mDragInProgress && mPagedView.mIsSorted)) {
434 // Open on the first page.
435 mPagedView.snapToPageImmediately(0);
436 }
Sunny Goyal48461932015-03-09 17:41:09 -0700437 }
438
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700439 Animator openFolderAnim = null;
440 final Runnable onCompleteRunnable;
Kenny Guyd794a3f2014-09-16 15:17:58 +0100441 if (!Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700442 positionAndSizeAsIcon();
443 centerAboutIcon();
444
445 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1);
446 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
447 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
448 final ObjectAnimator oa =
449 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
450 oa.setDuration(mExpandDuration);
451 openFolderAnim = oa;
452
453 setLayerType(LAYER_TYPE_HARDWARE, null);
454 onCompleteRunnable = new Runnable() {
455 @Override
456 public void run() {
457 setLayerType(LAYER_TYPE_NONE, null);
458 }
459 };
460 } else {
461 prepareReveal();
462 centerAboutIcon();
463
464 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
465 int height = getFolderHeight();
466
467 float transX = - 0.075f * (width / 2 - getPivotX());
468 float transY = - 0.075f * (height / 2 - getPivotY());
469 setTranslationX(transX);
470 setTranslationY(transY);
471 PropertyValuesHolder tx = PropertyValuesHolder.ofFloat("translationX", transX, 0);
472 PropertyValuesHolder ty = PropertyValuesHolder.ofFloat("translationY", transY, 0);
473
474 int rx = (int) Math.max(Math.max(width - getPivotX(), 0), getPivotX());
475 int ry = (int) Math.max(Math.max(height - getPivotY(), 0), getPivotY());
476 float radius = (float) Math.sqrt(rx * rx + ry * ry);
477 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
478 Animator reveal = LauncherAnimUtils.createCircularReveal(this, (int) getPivotX(),
479 (int) getPivotY(), 0, radius);
480 reveal.setDuration(mMaterialExpandDuration);
481 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
482
Sunny Goyalbc753352015-03-05 09:40:44 -0800483 mContentWrapper.setAlpha(0f);
484 Animator iconsAlpha = LauncherAnimUtils.ofFloat(mContentWrapper, "alpha", 0f, 1f);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700485 iconsAlpha.setDuration(mMaterialExpandDuration);
486 iconsAlpha.setStartDelay(mMaterialExpandStagger);
487 iconsAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
488
Sunny Goyal290800b2015-03-05 11:33:33 -0800489 mFooter.setAlpha(0f);
490 Animator textAlpha = LauncherAnimUtils.ofFloat(mFooter, "alpha", 0f, 1f);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700491 textAlpha.setDuration(mMaterialExpandDuration);
492 textAlpha.setStartDelay(mMaterialExpandStagger);
493 textAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
494
495 Animator drift = LauncherAnimUtils.ofPropertyValuesHolder(this, tx, ty);
496 drift.setDuration(mMaterialExpandDuration);
497 drift.setStartDelay(mMaterialExpandStagger);
498 drift.setInterpolator(new LogDecelerateInterpolator(60, 0));
499
500 anim.play(drift);
501 anim.play(iconsAlpha);
502 anim.play(textAlpha);
503 anim.play(reveal);
504
505 openFolderAnim = anim;
506
Sunny Goyalbc753352015-03-05 09:40:44 -0800507 mContentWrapper.setLayerType(LAYER_TYPE_HARDWARE, null);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700508 onCompleteRunnable = new Runnable() {
509 @Override
510 public void run() {
Sunny Goyalbc753352015-03-05 09:40:44 -0800511 mContentWrapper.setLayerType(LAYER_TYPE_NONE, null);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700512 }
513 };
514 }
515 openFolderAnim.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700516 @Override
517 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700518 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Sunny Goyalbc753352015-03-05 09:40:44 -0800519 mContent.getAccessibilityDescription());
Adam Cohendf2cc412011-04-27 16:56:57 -0700520 mState = STATE_ANIMATING;
521 }
522 @Override
523 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700524 mState = STATE_OPEN;
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700525
526 if (onCompleteRunnable != null) {
527 onCompleteRunnable.run();
528 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800529
Sunny Goyalbc753352015-03-05 09:40:44 -0800530 mContent.setFocusOnFirstChild();
Adam Cohendf2cc412011-04-27 16:56:57 -0700531 }
532 });
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700533 openFolderAnim.start();
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800534
535 // Make sure the folder picks up the last drag move even if the finger doesn't move.
536 if (mDragController.isDragging()) {
537 mDragController.forceTouchMove();
538 }
539 }
540
541 public void beginExternalDrag(ShortcutInfo item) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800542 mCurrentDragInfo = item;
Sunny Goyal5d85c442015-03-10 13:14:47 -0700543 mEmptyCellRank = mContent.allocateRankForNewItem(item);
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800544 mIsExternalDrag = true;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800545 mDragInProgress = true;
Sunny Goyal5d85c442015-03-10 13:14:47 -0700546 if (ALLOW_FOLDER_SCROLL && mPagedView.mIsSorted) {
547 mScrollPauseAlarm.setOnAlarmListener(null);
548 mScrollPauseAlarm.cancelAlarm();
549 mScrollPauseAlarm.setAlarm(SORTED_STICKY_REORDER_DELAY);
550 }
551
Adam Cohendf2cc412011-04-27 16:56:57 -0700552 }
553
Adam Cohen3371da02011-10-25 21:38:29 -0700554 private void sendCustomAccessibilityEvent(int type, String text) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700555 AccessibilityManager accessibilityManager = (AccessibilityManager)
556 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
557 if (accessibilityManager.isEnabled()) {
Adam Cohen3371da02011-10-25 21:38:29 -0700558 AccessibilityEvent event = AccessibilityEvent.obtain(type);
559 onInitializeAccessibilityEvent(event);
560 event.getText().add(text);
Michael Jurka8b805b12012-04-18 14:23:14 -0700561 accessibilityManager.sendAccessibilityEvent(event);
Adam Cohen3371da02011-10-25 21:38:29 -0700562 }
563 }
564
Adam Cohendf2cc412011-04-27 16:56:57 -0700565 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700566 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800567 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
568 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
569 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
Michael Jurka032e6ba2013-04-22 15:08:42 +0200570 final ObjectAnimator oa =
Michael Jurka2ecf9952012-06-18 12:52:28 -0700571 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohendf2cc412011-04-27 16:56:57 -0700572
Adam Cohen2801caf2011-05-13 20:57:39 -0700573 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700574 @Override
575 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700576 onCloseComplete();
Michael Jurka0121c3e2012-05-31 08:36:04 -0700577 setLayerType(LAYER_TYPE_NONE, null);
Adam Cohen2801caf2011-05-13 20:57:39 -0700578 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700579 }
580 @Override
581 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700582 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700583 getContext().getString(R.string.folder_closed));
Adam Cohendf2cc412011-04-27 16:56:57 -0700584 mState = STATE_ANIMATING;
585 }
586 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700587 oa.setDuration(mExpandDuration);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700588 setLayerType(LAYER_TYPE_HARDWARE, null);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100589 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700590 }
591
Adam Cohencb3382b2011-05-24 14:07:08 -0700592 public boolean acceptDrop(DragObject d) {
593 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700594 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700595 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
596 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
597 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700598 }
599
Adam Cohencb3382b2011-05-24 14:07:08 -0700600 public void onDragEnter(DragObject d) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800601 mPrevTargetRank = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700602 mOnExitAlarm.cancelAlarm();
Sunny Goyal48461932015-03-09 17:41:09 -0700603 if (ALLOW_FOLDER_SCROLL) {
604 // Get the area offset such that the folder only closes if half the drag icon width
605 // is outside the folder area
606 mScrollAreaOffset = d.dragView.getDragRegionWidth() / 2 - d.xOffset;
607 }
Adam Cohenbfbfd262011-06-13 16:55:12 -0700608 }
609
610 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
611 public void onAlarm(Alarm alarm) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800612 mContent.realTimeReorder(mEmptyCellRank, mTargetRank);
613 mEmptyCellRank = mTargetRank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700614 }
615 };
616
Sunny Goyalc46bfef2015-01-05 12:40:08 -0800617 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohen2374abf2013-04-16 14:56:57 -0700618 public boolean isLayoutRtl() {
619 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
620 }
621
Sunny Goyal48461932015-03-09 17:41:09 -0700622 @Override
Adam Cohencb3382b2011-05-24 14:07:08 -0700623 public void onDragOver(DragObject d) {
Sunny Goyal48461932015-03-09 17:41:09 -0700624 onDragOver(d, REORDER_DELAY);
625 }
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700626
Sunny Goyal48461932015-03-09 17:41:09 -0700627 private int getTargetRank(DragObject d, float[] recycle) {
628 recycle = d.getVisualCenter(recycle);
629 return mContent.findNearestArea(
630 (int) recycle[0] - getPaddingLeft(), (int) recycle[1] - getPaddingTop());
631 }
632
633 private void onDragOver(DragObject d, int reorderDelay) {
634 if (ALLOW_FOLDER_SCROLL && mScrollPauseAlarm.alarmPending()) {
635 return;
636 }
637 final float[] r = new float[2];
638 mTargetRank = getTargetRank(d, r);
639
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800640 if (mTargetRank != mPrevTargetRank) {
Alan Viverette4cda5b72013-08-28 17:53:41 -0700641 mReorderAlarm.cancelAlarm();
Sunny Goyalc46bfef2015-01-05 12:40:08 -0800642 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
643 mReorderAlarm.setAlarm(REORDER_DELAY);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800644 mPrevTargetRank = mTargetRank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700645 }
Sunny Goyal48461932015-03-09 17:41:09 -0700646
647 if (!ALLOW_FOLDER_SCROLL) {
648 return;
649 }
650
651 float x = r[0];
652 int currentPage = mPagedView.getNextPage();
653 int cellWidth = mPagedView.getCurrentCellLayout().getCellWidth();
654 if (currentPage > 0 && x < cellWidth * ICON_OVERSCROLL_WIDTH_FACTOR) {
655 // Show scroll hint on the left
656 if (mScrollHintDir != DragController.SCROLL_LEFT) {
657 mPagedView.showScrollHint(-SCROLL_HINT_FRACTION);
658 mScrollHintDir = DragController.SCROLL_LEFT;
659 }
660
661 // Set alarm for when the hint is complete
662 if (!mOnScrollHintAlarm.alarmPending() || mCurrentScrollDir != DragController.SCROLL_LEFT) {
663 mCurrentScrollDir = DragController.SCROLL_LEFT;
664 mOnScrollHintAlarm.cancelAlarm();
665 mOnScrollHintAlarm.setOnAlarmListener(new OnScrollHintListener(d));
666 mOnScrollHintAlarm.setAlarm(SCROLL_HINT_DURATION);
667
668 mReorderAlarm.cancelAlarm();
669 mTargetRank = mEmptyCellRank;
670 }
671 } else if (currentPage < (mPagedView.getPageCount() - 1) &&
672 (x > (getWidth() - cellWidth * ICON_OVERSCROLL_WIDTH_FACTOR))) {
673 // Show scroll hint on the right
674 if (mScrollHintDir != DragController.SCROLL_RIGHT) {
675 mPagedView.showScrollHint(SCROLL_HINT_FRACTION);
676 mScrollHintDir = DragController.SCROLL_RIGHT;
677 }
678
679 // Set alarm for when the hint is complete
680 if (!mOnScrollHintAlarm.alarmPending() || mCurrentScrollDir != DragController.SCROLL_RIGHT) {
681 mCurrentScrollDir = DragController.SCROLL_RIGHT;
682 mOnScrollHintAlarm.cancelAlarm();
683 mOnScrollHintAlarm.setOnAlarmListener(new OnScrollHintListener(d));
684 mOnScrollHintAlarm.setAlarm(SCROLL_HINT_DURATION);
685
686 mReorderAlarm.cancelAlarm();
687 mTargetRank = mEmptyCellRank;
688 }
689 } else {
690 mOnScrollHintAlarm.cancelAlarm();
691 if (mScrollHintDir != DragController.SCROLL_NONE) {
692 mPagedView.clearScrollHint();
693 mScrollHintDir = DragController.SCROLL_NONE;
694 }
695 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700696 }
697
Adam Cohenbfbfd262011-06-13 16:55:12 -0700698 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
699 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700700 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700701 }
702 };
703
Adam Cohen95bb8002011-07-03 23:40:28 -0700704 public void completeDragExit() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700705 mLauncher.closeFolder();
706 mCurrentDragInfo = null;
707 mCurrentDragView = null;
708 mSuppressOnAdd = false;
709 mRearrangeOnClose = true;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800710 mIsExternalDrag = false;
Adam Cohen3e8f8112011-07-02 18:03:00 -0700711 }
712
Adam Cohencb3382b2011-05-24 14:07:08 -0700713 public void onDragExit(DragObject d) {
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700714 // We only close the folder if this is a true drag exit, ie. not because
715 // a drop has occurred above the folder.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700716 if (!d.dragComplete) {
717 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
718 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
719 }
720 mReorderAlarm.cancelAlarm();
Sunny Goyal48461932015-03-09 17:41:09 -0700721
722 if (ALLOW_FOLDER_SCROLL) {
723 mOnScrollHintAlarm.cancelAlarm();
724 mScrollPauseAlarm.cancelAlarm();
725 if (mScrollHintDir != DragController.SCROLL_NONE) {
726 mPagedView.clearScrollHint();
727 mScrollHintDir = DragController.SCROLL_NONE;
728 }
729 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700730 }
731
Michael Jurka1e2f4652013-07-08 18:03:46 -0700732 public void onDropCompleted(final View target, final DragObject d,
733 final boolean isFlingToDelete, final boolean success) {
734 if (mDeferDropAfterUninstall) {
Michael Jurkaf3007582013-08-21 14:33:57 +0200735 Log.d(TAG, "Deferred handling drop because waiting for uninstall.");
Michael Jurka1e2f4652013-07-08 18:03:46 -0700736 mDeferredAction = new Runnable() {
737 public void run() {
738 onDropCompleted(target, d, isFlingToDelete, success);
739 mDeferredAction = null;
740 }
741 };
742 return;
743 }
744
745 boolean beingCalledAfterUninstall = mDeferredAction != null;
746 boolean successfulDrop =
747 success && (!beingCalledAfterUninstall || mUninstallSuccessful);
Winson Chung5f8afe62013-08-12 16:19:28 -0700748
Michael Jurka1e2f4652013-07-08 18:03:46 -0700749 if (successfulDrop) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800750 if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon && target != this) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700751 replaceFolderWithFinalItem();
752 }
753 } else {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800754 rearrangeChildren();
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700755 // The drag failed, we need to return the item to the folder
756 mFolderIcon.onDrop(d);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700757 }
758
759 if (target != this) {
760 if (mOnExitAlarm.alarmPending()) {
761 mOnExitAlarm.cancelAlarm();
Michael Jurka54554252013-08-01 12:52:23 +0200762 if (!successfulDrop) {
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700763 mSuppressFolderDeletion = true;
764 }
Sunny Goyal5d85c442015-03-10 13:14:47 -0700765 mScrollPauseAlarm.cancelAlarm();
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700766 completeDragExit();
767 }
Adam Cohen9c58d822013-12-13 17:18:10 -0800768 }
769
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700770 mDeleteFolderOnDropCompleted = false;
771 mDragInProgress = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700772 mItemAddedBackToSelfViaIcon = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700773 mCurrentDragInfo = null;
774 mCurrentDragView = null;
775 mSuppressOnAdd = false;
Adam Cohen4045eb72011-10-06 11:44:26 -0700776
777 // Reordering may have occured, and we need to save the new item locations. We do this once
778 // at the end to prevent unnecessary database operations.
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700779 updateItemLocationsInDatabaseBatch();
Adam Cohen4045eb72011-10-06 11:44:26 -0700780 }
781
Michael Jurka1e2f4652013-07-08 18:03:46 -0700782 public void deferCompleteDropAfterUninstallActivity() {
783 mDeferDropAfterUninstall = true;
784 }
785
786 public void onUninstallActivityReturned(boolean success) {
787 mDeferDropAfterUninstall = false;
788 mUninstallSuccessful = success;
789 if (mDeferredAction != null) {
790 mDeferredAction.run();
791 }
792 }
793
Winson Chunga48487a2012-03-20 16:19:37 -0700794 @Override
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800795 public float getIntrinsicIconScaleFactor() {
796 return 1f;
797 }
798
799 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800800 public boolean supportsFlingToDelete() {
801 return true;
802 }
803
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000804 @Override
805 public boolean supportsAppInfoDropTarget() {
806 return false;
807 }
808
809 @Override
810 public boolean supportsDeleteDropTarget() {
811 return true;
812 }
813
Winson Chunga48487a2012-03-20 16:19:37 -0700814 public void onFlingToDelete(DragObject d, int x, int y, PointF vec) {
815 // Do nothing
816 }
817
818 @Override
819 public void onFlingToDeleteCompleted() {
820 // Do nothing
821 }
822
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700823 private void updateItemLocationsInDatabaseBatch() {
824 ArrayList<View> list = getItemsInReadingOrder();
825 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
826 for (int i = 0; i < list.size(); i++) {
827 View v = list.get(i);
828 ItemInfo info = (ItemInfo) v.getTag();
Sunny Goyal08f72612015-01-05 13:41:43 -0800829 info.rank = i;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700830 items.add(info);
831 }
832
833 LauncherModel.moveItemsInDatabase(mLauncher, items, mInfo.id, 0);
834 }
835
Adam Cohene25af792013-06-06 23:08:25 -0700836 public void addItemLocationsInDatabase() {
837 ArrayList<View> list = getItemsInReadingOrder();
838 for (int i = 0; i < list.size(); i++) {
839 View v = list.get(i);
840 ItemInfo info = (ItemInfo) v.getTag();
841 LauncherModel.addItemToDatabase(mLauncher, info, mInfo.id, 0,
842 info.cellX, info.cellY, false);
843 }
844 }
845
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700846 public void notifyDrop() {
847 if (mDragInProgress) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700848 mItemAddedBackToSelfViaIcon = true;
Adam Cohen76078c42011-06-09 15:06:52 -0700849 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700850 }
851
852 public boolean isDropEnabled() {
853 return true;
854 }
855
Adam Cohen2801caf2011-05-13 20:57:39 -0700856 public boolean isFull() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800857 return mContent.isFull();
Adam Cohen2801caf2011-05-13 20:57:39 -0700858 }
859
860 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700861 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700862
Winson Chung892c74d2013-08-22 16:15:50 -0700863 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700864 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700865 int height = getFolderHeight();
Adam Cohen2801caf2011-05-13 20:57:39 -0700866
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800867 float scale = parent.getDescendantRectRelativeToSelf(mFolderIcon, sTempRect);
Adam Cohen8e776a62011-06-28 18:10:06 -0700868
Winson Chungaf40f202013-09-18 18:26:31 -0700869 LauncherAppState app = LauncherAppState.getInstance();
870 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
871
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800872 int centerX = (int) (sTempRect.left + sTempRect.width() * scale / 2);
873 int centerY = (int) (sTempRect.top + sTempRect.height() * scale / 2);
Adam Cohen2801caf2011-05-13 20:57:39 -0700874 int centeredLeft = centerX - width / 2;
875 int centeredTop = centerY - height / 2;
Winson Chung3057b1c2013-10-10 17:35:35 -0700876 int currentPage = mLauncher.getWorkspace().getNextPage();
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800877 // In case the workspace is scrolling, we need to use the final scroll to compute
878 // the folders bounds.
879 mLauncher.getWorkspace().setFinalScrollForPageChange(currentPage);
Adam Cohen35e7e642011-07-17 14:47:18 -0700880 // We first fetch the currently visible CellLayoutChildren
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800881 CellLayout currentLayout = (CellLayout) mLauncher.getWorkspace().getChildAt(currentPage);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700882 ShortcutAndWidgetContainer boundingLayout = currentLayout.getShortcutsAndWidgets();
Adam Cohen35e7e642011-07-17 14:47:18 -0700883 Rect bounds = new Rect();
884 parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800885 // We reset the workspaces scroll
886 mLauncher.getWorkspace().resetFinalScrollForPageChange(currentPage);
Adam Cohen2801caf2011-05-13 20:57:39 -0700887
Adam Cohen35e7e642011-07-17 14:47:18 -0700888 // We need to bound the folder to the currently visible CellLayoutChildren
889 int left = Math.min(Math.max(bounds.left, centeredLeft),
890 bounds.left + bounds.width() - width);
891 int top = Math.min(Math.max(bounds.top, centeredTop),
892 bounds.top + bounds.height() - height);
Winson Chungaf40f202013-09-18 18:26:31 -0700893 if (grid.isPhone() && (grid.availableWidthPx - width) < grid.iconSizePx) {
894 // Center the folder if it is full (on phones only)
895 left = (grid.availableWidthPx - width) / 2;
896 } else if (width >= bounds.width()) {
897 // If the folder doesn't fit within the bounds, center it about the desired bounds
Adam Cohen35e7e642011-07-17 14:47:18 -0700898 left = bounds.left + (bounds.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700899 }
Adam Cohen35e7e642011-07-17 14:47:18 -0700900 if (height >= bounds.height()) {
901 top = bounds.top + (bounds.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700902 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700903
904 int folderPivotX = width / 2 + (centeredLeft - left);
905 int folderPivotY = height / 2 + (centeredTop - top);
906 setPivotX(folderPivotX);
907 setPivotY(folderPivotY);
Adam Cohen268c4752012-06-06 17:47:33 -0700908 mFolderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700909 (1.0f * folderPivotX / width));
Adam Cohen268c4752012-06-06 17:47:33 -0700910 mFolderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700911 (1.0f * folderPivotY / height));
Adam Cohen3bf84d32012-05-07 20:17:14 -0700912
Adam Cohen662b5982011-12-13 17:45:21 -0800913 lp.width = width;
914 lp.height = height;
915 lp.x = left;
916 lp.y = top;
Adam Cohen2801caf2011-05-13 20:57:39 -0700917 }
918
Adam Cohen268c4752012-06-06 17:47:33 -0700919 float getPivotXForIconAnimation() {
920 return mFolderIconPivotX;
921 }
922 float getPivotYForIconAnimation() {
923 return mFolderIconPivotY;
924 }
925
Winson Chung892c74d2013-08-22 16:15:50 -0700926 private int getContentAreaHeight() {
927 LauncherAppState app = LauncherAppState.getInstance();
928 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
929 Rect workspacePadding = grid.getWorkspacePadding(grid.isLandscape ?
930 CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
931 int maxContentAreaHeight = grid.availableHeightPx -
Winson Chung892c74d2013-08-22 16:15:50 -0700932 workspacePadding.top - workspacePadding.bottom -
Sunny Goyal290800b2015-03-05 11:33:33 -0800933 mFooterHeight;
Adam Cohen1960ea42013-11-12 11:33:14 +0000934 int height = Math.min(maxContentAreaHeight,
Winson Chung892c74d2013-08-22 16:15:50 -0700935 mContent.getDesiredHeight());
Adam Cohen1960ea42013-11-12 11:33:14 +0000936 return Math.max(height, MIN_CONTENT_DIMEN);
937 }
938
939 private int getContentAreaWidth() {
940 return Math.max(mContent.getDesiredWidth(), MIN_CONTENT_DIMEN);
Winson Chung892c74d2013-08-22 16:15:50 -0700941 }
942
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700943 private int getFolderHeight() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800944 return getFolderHeight(getContentAreaHeight());
945 }
946
947 private int getFolderHeight(int contentAreaHeight) {
Sunny Goyal290800b2015-03-05 11:33:33 -0800948 return getPaddingTop() + getPaddingBottom() + contentAreaHeight + mFooterHeight;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700949 }
Adam Cohen234c4cd2011-07-17 21:03:04 -0700950
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700951 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800952 int contentWidth = getContentAreaWidth();
953 int contentHeight = getContentAreaHeight();
Adam Cohen1960ea42013-11-12 11:33:14 +0000954
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800955 int contentAreaWidthSpec = MeasureSpec.makeMeasureSpec(contentWidth, MeasureSpec.EXACTLY);
956 int contentAreaHeightSpec = MeasureSpec.makeMeasureSpec(contentHeight, MeasureSpec.EXACTLY);
957
958 mContent.setFixedSize(contentWidth, contentHeight);
959 mContentWrapper.measure(contentAreaWidthSpec, contentAreaHeightSpec);
Sunny Goyal290800b2015-03-05 11:33:33 -0800960 mFooter.measure(contentAreaWidthSpec,
961 MeasureSpec.makeMeasureSpec(mFooterHeight, MeasureSpec.EXACTLY));
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800962
963 int folderWidth = getPaddingLeft() + getPaddingRight() + contentWidth;
964 int folderHeight = getFolderHeight(contentHeight);
965 setMeasuredDimension(folderWidth, folderHeight);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700966 }
967
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800968 /**
969 * Rearranges the children based on their rank.
970 */
971 public void rearrangeChildren() {
972 rearrangeChildren(-1);
973 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700974
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800975 /**
976 * Rearranges the children based on their rank.
977 * @param itemCount if greater than the total children count, empty spaces are left at the end,
978 * otherwise it is ignored.
979 */
980 public void rearrangeChildren(int itemCount) {
981 ArrayList<View> views = getItemsInReadingOrder();
982 mContent.arrangeChildren(views, Math.max(itemCount, views.size()));
Adam Cohen7c693212011-05-18 15:26:57 -0700983 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700984 }
985
Sunny Goyalc4918352015-03-10 18:15:48 -0700986 // TODO remove this once GSA code fix is submitted
987 public ViewGroup getContent() {
988 return (ViewGroup) mContent;
989 }
990
Adam Cohena9cf38f2011-05-02 15:36:58 -0700991 public int getItemCount() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800992 return mContent.getItemCount();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700993 }
994
Adam Cohen2801caf2011-05-13 20:57:39 -0700995 private void onCloseComplete() {
Adam Cohen05e0f402011-08-01 12:12:49 -0700996 DragLayer parent = (DragLayer) getParent();
Michael Jurka5649c282012-06-18 10:33:21 -0700997 if (parent != null) {
998 parent.removeView(this);
999 }
Adam Cohen4554ee12011-08-03 16:13:21 -07001000 mDragController.removeDropTarget((DropTarget) this);
Adam Cohen05e0f402011-08-01 12:12:49 -07001001 clearFocus();
Adam Cohenac56cff2011-09-28 20:45:37 -07001002 mFolderIcon.requestFocus();
Adam Cohen05e0f402011-08-01 12:12:49 -07001003
Adam Cohen2801caf2011-05-13 20:57:39 -07001004 if (mRearrangeOnClose) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001005 rearrangeChildren();
Adam Cohen2801caf2011-05-13 20:57:39 -07001006 mRearrangeOnClose = false;
1007 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001008 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001009 if (!mDragInProgress && !mSuppressFolderDeletion) {
1010 replaceFolderWithFinalItem();
1011 } else if (mDragInProgress) {
1012 mDeleteFolderOnDropCompleted = true;
1013 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001014 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001015 mSuppressFolderDeletion = false;
Adam Cohenafb01ee2011-06-23 15:38:03 -07001016 }
1017
1018 private void replaceFolderWithFinalItem() {
Adam Cohenafb01ee2011-06-23 15:38:03 -07001019 // Add the last remaining child to the workspace in place of the folder
Adam Cohenfb91f302012-06-11 15:45:18 -07001020 Runnable onCompleteRunnable = new Runnable() {
1021 @Override
1022 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -07001023 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screenId);
Adam Cohenafb01ee2011-06-23 15:38:03 -07001024
Winson Chung33231f52013-12-09 16:57:45 -08001025 View child = null;
Adam Cohenfb91f302012-06-11 15:45:18 -07001026 // Move the item from the folder to the workspace, in the position of the folder
1027 if (getItemCount() == 1) {
1028 ShortcutInfo finalItem = mInfo.contents.get(0);
Adam Cohenc5e63f32012-07-12 16:16:57 -07001029 child = mLauncher.createShortcut(R.layout.application, cellLayout,
Adam Cohenfb91f302012-06-11 15:45:18 -07001030 finalItem);
1031 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -07001032 mInfo.screenId, mInfo.cellX, mInfo.cellY);
Adam Cohenfb91f302012-06-11 15:45:18 -07001033 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001034 if (getItemCount() <= 1) {
1035 // Remove the folder
1036 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
Dan Sandler0eb687f2014-01-10 13:24:55 -05001037 if (cellLayout != null) {
1038 // b/12446428 -- sometimes the cell layout has already gone away?
1039 cellLayout.removeView(mFolderIcon);
1040 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001041 if (mFolderIcon instanceof DropTarget) {
1042 mDragController.removeDropTarget((DropTarget) mFolderIcon);
1043 }
1044 mLauncher.removeFolder(mInfo);
1045 }
Adam Cohenc5e63f32012-07-12 16:16:57 -07001046 // We add the child after removing the folder to prevent both from existing at
Winson Chung0e6a7132013-08-23 12:55:10 -07001047 // the same time in the CellLayout. We need to add the new item with addInScreenFromBind()
1048 // to ensure that hotseat items are placed correctly.
Adam Cohenc5e63f32012-07-12 16:16:57 -07001049 if (child != null) {
Winson Chung0e6a7132013-08-23 12:55:10 -07001050 mLauncher.getWorkspace().addInScreenFromBind(child, mInfo.container, mInfo.screenId,
Adam Cohenc5e63f32012-07-12 16:16:57 -07001051 mInfo.cellX, mInfo.cellY, mInfo.spanX, mInfo.spanY);
1052 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001053 }
1054 };
Sunny Goyalbc753352015-03-05 09:40:44 -08001055 View finalChild = mContent.getLastItem();
Adam Cohenfb91f302012-06-11 15:45:18 -07001056 if (finalChild != null) {
1057 mFolderIcon.performDestroyAnimation(finalChild, onCompleteRunnable);
Winson Chung33231f52013-12-09 16:57:45 -08001058 } else {
1059 onCompleteRunnable.run();
Adam Cohenafb01ee2011-06-23 15:38:03 -07001060 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001061 mDestroyed = true;
1062 }
1063
1064 boolean isDestroyed() {
1065 return mDestroyed;
Adam Cohen2801caf2011-05-13 20:57:39 -07001066 }
1067
Adam Cohenac56cff2011-09-28 20:45:37 -07001068 // This method keeps track of the last item in the folder for the purposes
1069 // of keyboard focus
Sunny Goyal290800b2015-03-05 11:33:33 -08001070 public void updateTextViewFocus() {
Sunny Goyalbc753352015-03-05 09:40:44 -08001071 View lastChild = mContent.getLastItem();
Adam Cohenac56cff2011-09-28 20:45:37 -07001072 if (lastChild != null) {
1073 mFolderName.setNextFocusDownId(lastChild.getId());
1074 mFolderName.setNextFocusRightId(lastChild.getId());
1075 mFolderName.setNextFocusLeftId(lastChild.getId());
1076 mFolderName.setNextFocusUpId(lastChild.getId());
1077 }
1078 }
1079
Adam Cohenbfbfd262011-06-13 16:55:12 -07001080 public void onDrop(DragObject d) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001081 Runnable cleanUpRunnable = null;
1082
Adam Cohen689ff162014-05-08 17:27:56 -07001083 // If we are coming from All Apps space, we defer removing the extra empty screen
1084 // until the folder closes
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001085 if (d.dragSource != mLauncher.getWorkspace() && !(d.dragSource instanceof Folder)) {
1086 cleanUpRunnable = new Runnable() {
1087 @Override
1088 public void run() {
Adam Cohen689ff162014-05-08 17:27:56 -07001089 mLauncher.exitSpringLoadedDragModeDelayed(true,
1090 Launcher.EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
1091 null);
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001092 }
1093 };
Adam Cohenbfbfd262011-06-13 16:55:12 -07001094 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001095
Sunny Goyal48461932015-03-09 17:41:09 -07001096 if (ALLOW_FOLDER_SCROLL) {
1097 // If the icon was dropped while the page was being scrolled, we need to compute
1098 // the target location again such that the icon is placed of the final page.
1099 if (!mPagedView.rankOnCurrentPage(mEmptyCellRank)) {
1100 // Reorder again.
1101 mTargetRank = getTargetRank(d, null);
1102
1103 // Rearrange items immediately.
1104 mReorderAlarmListener.onAlarm(mReorderAlarm);
1105
1106 mOnScrollHintAlarm.cancelAlarm();
1107 mScrollPauseAlarm.cancelAlarm();
1108 }
1109 mPagedView.completePendingPageChanges();
1110 }
1111
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001112 View currentDragView;
1113 ShortcutInfo si = mCurrentDragInfo;
1114 if (mIsExternalDrag) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001115 currentDragView = mContent.createAndAddViewForRank(si, mEmptyCellRank);
Sunny Goyal95abbb32014-08-04 10:53:22 -07001116 // Actually move the item in the database if it was an external drag. Call this
1117 // before creating the view, so that ShortcutInfo is updated appropriately.
1118 LauncherModel.addOrMoveItemInDatabase(
1119 mLauncher, si, mInfo.id, 0, si.cellX, si.cellY);
1120
1121 // We only need to update the locations if it doesn't get handled in #onDropCompleted.
1122 if (d.dragSource != this) {
1123 updateItemLocationsInDatabaseBatch();
1124 }
1125 mIsExternalDrag = false;
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001126 } else {
1127 currentDragView = mCurrentDragView;
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001128 mContent.addViewForRank(currentDragView, si, mEmptyCellRank);
Adam Cohenbfbfd262011-06-13 16:55:12 -07001129 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001130
1131 if (d.dragView.hasDrawn()) {
1132
1133 // Temporarily reset the scale such that the animation target gets calculated correctly.
1134 float scaleX = getScaleX();
1135 float scaleY = getScaleY();
1136 setScaleX(1.0f);
1137 setScaleY(1.0f);
1138 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, currentDragView,
1139 cleanUpRunnable, null);
1140 setScaleX(scaleX);
1141 setScaleY(scaleY);
1142 } else {
1143 d.deferDragViewCleanupPostAnimation = false;
1144 currentDragView.setVisibility(VISIBLE);
1145 }
1146 mItemsInvalidated = true;
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001147 rearrangeChildren();
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001148
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001149 // Temporarily suppress the listener, as we did all the work already here.
1150 mSuppressOnAdd = true;
1151 mInfo.add(si);
1152 mSuppressOnAdd = false;
Sunny Goyal4b020172014-08-28 14:51:14 -07001153 // Clear the drag info, as it is no longer being dragged.
1154 mCurrentDragInfo = null;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001155 }
1156
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001157 // This is used so the item doesn't immediately appear in the folder when added. In one case
1158 // we need to create the illusion that the item isn't added back to the folder yet, to
1159 // to correspond to the animation of the icon back into the folder. This is
1160 public void hideItem(ShortcutInfo info) {
1161 View v = getViewForInfo(info);
1162 v.setVisibility(INVISIBLE);
1163 }
1164 public void showItem(ShortcutInfo info) {
1165 View v = getViewForInfo(info);
1166 v.setVisibility(VISIBLE);
1167 }
1168
Adam Cohenbfbfd262011-06-13 16:55:12 -07001169 public void onAdd(ShortcutInfo item) {
1170 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001171 // If the item was dropped onto this open folder, we have done the work associated
1172 // with adding the item to the folder, as indicated by mSuppressOnAdd being set
Adam Cohenbfbfd262011-06-13 16:55:12 -07001173 if (mSuppressOnAdd) return;
Sunny Goyal5d85c442015-03-10 13:14:47 -07001174 mContent.createAndAddViewForRank(item, mContent.allocateRankForNewItem(item));
Adam Cohenbfbfd262011-06-13 16:55:12 -07001175 LauncherModel.addOrMoveItemInDatabase(
1176 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
1177 }
1178
Adam Cohena9cf38f2011-05-02 15:36:58 -07001179 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -07001180 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001181 // If this item is being dragged from this open folder, we have already handled
1182 // the work associated with removing the item, so we don't have to do anything here.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001183 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001184 View v = getViewForInfo(item);
Sunny Goyal290800b2015-03-05 11:33:33 -08001185 mContent.removeItem(v);
Adam Cohen2801caf2011-05-13 20:57:39 -07001186 if (mState == STATE_ANIMATING) {
1187 mRearrangeOnClose = true;
1188 } else {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001189 rearrangeChildren();
Adam Cohen2801caf2011-05-13 20:57:39 -07001190 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001191 if (getItemCount() <= 1) {
1192 replaceFolderWithFinalItem();
1193 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001194 }
Adam Cohen7c693212011-05-18 15:26:57 -07001195
Sunny Goyalbc753352015-03-05 09:40:44 -08001196 private View getViewForInfo(final ShortcutInfo item) {
1197 return mContent.iterateOverItems(new ItemOperator() {
1198
1199 @Override
1200 public boolean evaluate(ItemInfo info, View view, View parent) {
1201 return info == item;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001202 }
Sunny Goyalbc753352015-03-05 09:40:44 -08001203 });
Adam Cohendf1e4e82011-06-24 15:57:39 -07001204 }
1205
Adam Cohen76078c42011-06-09 15:06:52 -07001206 public void onItemsChanged() {
Adam Cohenac56cff2011-09-28 20:45:37 -07001207 updateTextViewFocus();
Adam Cohen76078c42011-06-09 15:06:52 -07001208 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001209
Adam Cohen76fc0852011-06-17 13:26:23 -07001210 public void onTitleChanged(CharSequence title) {
1211 }
Adam Cohen76078c42011-06-09 15:06:52 -07001212
Adam Cohen7c693212011-05-18 15:26:57 -07001213 public ArrayList<View> getItemsInReadingOrder() {
1214 if (mItemsInvalidated) {
1215 mItemsInReadingOrder.clear();
Sunny Goyalbc753352015-03-05 09:40:44 -08001216 mContent.iterateOverItems(new ItemOperator() {
1217
1218 @Override
1219 public boolean evaluate(ItemInfo info, View view, View parent) {
1220 mItemsInReadingOrder.add(view);
1221 return false;
Adam Cohen7c693212011-05-18 15:26:57 -07001222 }
Sunny Goyalbc753352015-03-05 09:40:44 -08001223 });
Adam Cohen7c693212011-05-18 15:26:57 -07001224 mItemsInvalidated = false;
1225 }
1226 return mItemsInReadingOrder;
1227 }
Adam Cohen8dfcba42011-07-07 16:38:18 -07001228
1229 public void getLocationInDragLayer(int[] loc) {
1230 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
1231 }
Adam Cohenea0818d2011-09-30 20:06:58 -07001232
1233 public void onFocusChange(View v, boolean hasFocus) {
1234 if (v == mFolderName && hasFocus) {
1235 startEditingFolderName();
1236 }
1237 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001238
1239 @Override
1240 public void getHitRectRelativeToDragLayer(Rect outRect) {
1241 getHitRect(outRect);
Sunny Goyal48461932015-03-09 17:41:09 -07001242 outRect.left -= mScrollAreaOffset;
1243 outRect.right += mScrollAreaOffset;
1244 }
1245
1246 private class OnScrollHintListener implements OnAlarmListener {
1247
1248 private final DragObject mDragObject;
1249
1250 OnScrollHintListener(DragObject object) {
1251 mDragObject = object;
1252 }
1253
1254 /**
1255 * Scroll hint has been shown long enough. Now scroll to appropriate page.
1256 */
1257 @Override
1258 public void onAlarm(Alarm alarm) {
1259 if (mCurrentScrollDir == DragController.SCROLL_LEFT) {
1260 mPagedView.scrollLeft();
1261 mScrollHintDir = DragController.SCROLL_NONE;
1262 } else if (mCurrentScrollDir == DragController.SCROLL_RIGHT) {
1263 mPagedView.scrollRight();
1264 mScrollHintDir = DragController.SCROLL_NONE;
1265 } else {
1266 // This should not happen
1267 return;
1268 }
1269 mCurrentScrollDir = DragController.SCROLL_NONE;
1270
1271 // Pause drag event until the scrolling is finished
1272 mScrollPauseAlarm.setOnAlarmListener(new OnScrollFinishedListener(mDragObject));
1273 mScrollPauseAlarm.setAlarm(DragController.RESCROLL_DELAY);
1274 }
1275 }
1276
1277 private class OnScrollFinishedListener implements OnAlarmListener {
1278
1279 private final DragObject mDragObject;
1280
1281 OnScrollFinishedListener(DragObject object) {
1282 mDragObject = object;
1283 }
1284
1285 /**
1286 * Page scroll is complete.
1287 */
1288 @Override
1289 public void onAlarm(Alarm alarm) {
1290 // Reorder immediately on page change.
1291 onDragOver(mDragObject, 1);
1292 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001293 }
Sunny Goyalbc753352015-03-05 09:40:44 -08001294
1295 public static interface FolderContent {
1296 void setFolder(Folder f);
1297
Sunny Goyal290800b2015-03-05 11:33:33 -08001298 void removeItem(View v);
Sunny Goyalbc753352015-03-05 09:40:44 -08001299
1300 boolean isFull();
1301 int getItemCount();
1302
1303 int getDesiredWidth();
1304 int getDesiredHeight();
1305 void setFixedSize(int width, int height);
1306
1307 /**
1308 * Iterates over all its items in a reading order.
1309 * @return the view for which the operator returned true.
1310 */
1311 View iterateOverItems(ItemOperator op);
1312 View getLastItem();
1313
1314 String getAccessibilityDescription();
1315
1316 /**
1317 * Binds items to the layout.
1318 * @return list of items that could not be bound, probably because we hit the max size limit.
1319 */
1320 ArrayList<ShortcutInfo> bindItems(ArrayList<ShortcutInfo> children);
1321
1322 /**
Sunny Goyal5d85c442015-03-10 13:14:47 -07001323 * Create space for a new item, and returns the rank for that item.
Sunny Goyalbc753352015-03-05 09:40:44 -08001324 * Resizes the content if necessary.
1325 */
Sunny Goyal5d85c442015-03-10 13:14:47 -07001326 int allocateRankForNewItem(ShortcutInfo info);
Sunny Goyalbc753352015-03-05 09:40:44 -08001327
1328 View createAndAddViewForRank(ShortcutInfo item, int rank);
1329
1330 /**
1331 * Adds the {@param view} to the layout based on {@param rank} and updated the position
1332 * related attributes. It assumes that {@param item} is already attached to the view.
1333 */
1334 void addViewForRank(View view, ShortcutInfo item, int rank);
1335
1336 /**
1337 * Reorders the items such that the {@param empty} spot moves to {@param target}
1338 */
1339 void realTimeReorder(int empty, int target);
1340
1341 /**
1342 * @return the rank of the cell nearest to the provided pixel position.
1343 */
1344 int findNearestArea(int pixelX, int pixelY);
1345
1346 /**
1347 * Updates position and rank of all the children in the view based.
1348 * @param list the ordered list of children.
1349 * @param itemCount if greater than the total children count, empty spaces are left
1350 * at the end.
1351 */
1352 void arrangeChildren(ArrayList<View> list, int itemCount);
1353
1354 /**
1355 * Sets the focus on the first visible child.
1356 */
1357 void setFocusOnFirstChild();
1358 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001359}