blob: c7c6571ef9fc41f182084737ff00f15bd61dea8a [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 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700539
540 if (ALLOW_FOLDER_SCROLL) {
541 FolderPagedView pages = (FolderPagedView) mContent;
542 pages.verifyVisibleHighResIcons(pages.getNextPage());
543 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800544 }
545
546 public void beginExternalDrag(ShortcutInfo item) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800547 mCurrentDragInfo = item;
Sunny Goyal5d85c442015-03-10 13:14:47 -0700548 mEmptyCellRank = mContent.allocateRankForNewItem(item);
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800549 mIsExternalDrag = true;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800550 mDragInProgress = true;
Sunny Goyal5d85c442015-03-10 13:14:47 -0700551 if (ALLOW_FOLDER_SCROLL && mPagedView.mIsSorted) {
552 mScrollPauseAlarm.setOnAlarmListener(null);
553 mScrollPauseAlarm.cancelAlarm();
554 mScrollPauseAlarm.setAlarm(SORTED_STICKY_REORDER_DELAY);
555 }
556
Adam Cohendf2cc412011-04-27 16:56:57 -0700557 }
558
Adam Cohen3371da02011-10-25 21:38:29 -0700559 private void sendCustomAccessibilityEvent(int type, String text) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700560 AccessibilityManager accessibilityManager = (AccessibilityManager)
561 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
562 if (accessibilityManager.isEnabled()) {
Adam Cohen3371da02011-10-25 21:38:29 -0700563 AccessibilityEvent event = AccessibilityEvent.obtain(type);
564 onInitializeAccessibilityEvent(event);
565 event.getText().add(text);
Michael Jurka8b805b12012-04-18 14:23:14 -0700566 accessibilityManager.sendAccessibilityEvent(event);
Adam Cohen3371da02011-10-25 21:38:29 -0700567 }
568 }
569
Adam Cohendf2cc412011-04-27 16:56:57 -0700570 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700571 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800572 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
573 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
574 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
Michael Jurka032e6ba2013-04-22 15:08:42 +0200575 final ObjectAnimator oa =
Michael Jurka2ecf9952012-06-18 12:52:28 -0700576 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohendf2cc412011-04-27 16:56:57 -0700577
Adam Cohen2801caf2011-05-13 20:57:39 -0700578 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700579 @Override
580 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700581 onCloseComplete();
Michael Jurka0121c3e2012-05-31 08:36:04 -0700582 setLayerType(LAYER_TYPE_NONE, null);
Adam Cohen2801caf2011-05-13 20:57:39 -0700583 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700584 }
585 @Override
586 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700587 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700588 getContext().getString(R.string.folder_closed));
Adam Cohendf2cc412011-04-27 16:56:57 -0700589 mState = STATE_ANIMATING;
590 }
591 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700592 oa.setDuration(mExpandDuration);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700593 setLayerType(LAYER_TYPE_HARDWARE, null);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100594 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700595 }
596
Adam Cohencb3382b2011-05-24 14:07:08 -0700597 public boolean acceptDrop(DragObject d) {
598 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700599 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700600 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
601 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
602 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700603 }
604
Adam Cohencb3382b2011-05-24 14:07:08 -0700605 public void onDragEnter(DragObject d) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800606 mPrevTargetRank = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700607 mOnExitAlarm.cancelAlarm();
Sunny Goyal48461932015-03-09 17:41:09 -0700608 if (ALLOW_FOLDER_SCROLL) {
609 // Get the area offset such that the folder only closes if half the drag icon width
610 // is outside the folder area
611 mScrollAreaOffset = d.dragView.getDragRegionWidth() / 2 - d.xOffset;
612 }
Adam Cohenbfbfd262011-06-13 16:55:12 -0700613 }
614
615 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
616 public void onAlarm(Alarm alarm) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800617 mContent.realTimeReorder(mEmptyCellRank, mTargetRank);
618 mEmptyCellRank = mTargetRank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700619 }
620 };
621
Sunny Goyalc46bfef2015-01-05 12:40:08 -0800622 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohen2374abf2013-04-16 14:56:57 -0700623 public boolean isLayoutRtl() {
624 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
625 }
626
Sunny Goyal48461932015-03-09 17:41:09 -0700627 @Override
Adam Cohencb3382b2011-05-24 14:07:08 -0700628 public void onDragOver(DragObject d) {
Sunny Goyal48461932015-03-09 17:41:09 -0700629 onDragOver(d, REORDER_DELAY);
630 }
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700631
Sunny Goyal48461932015-03-09 17:41:09 -0700632 private int getTargetRank(DragObject d, float[] recycle) {
633 recycle = d.getVisualCenter(recycle);
634 return mContent.findNearestArea(
635 (int) recycle[0] - getPaddingLeft(), (int) recycle[1] - getPaddingTop());
636 }
637
638 private void onDragOver(DragObject d, int reorderDelay) {
639 if (ALLOW_FOLDER_SCROLL && mScrollPauseAlarm.alarmPending()) {
640 return;
641 }
642 final float[] r = new float[2];
643 mTargetRank = getTargetRank(d, r);
644
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800645 if (mTargetRank != mPrevTargetRank) {
Alan Viverette4cda5b72013-08-28 17:53:41 -0700646 mReorderAlarm.cancelAlarm();
Sunny Goyalc46bfef2015-01-05 12:40:08 -0800647 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
648 mReorderAlarm.setAlarm(REORDER_DELAY);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800649 mPrevTargetRank = mTargetRank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700650 }
Sunny Goyal48461932015-03-09 17:41:09 -0700651
652 if (!ALLOW_FOLDER_SCROLL) {
653 return;
654 }
655
656 float x = r[0];
657 int currentPage = mPagedView.getNextPage();
658 int cellWidth = mPagedView.getCurrentCellLayout().getCellWidth();
659 if (currentPage > 0 && x < cellWidth * ICON_OVERSCROLL_WIDTH_FACTOR) {
660 // Show scroll hint on the left
661 if (mScrollHintDir != DragController.SCROLL_LEFT) {
662 mPagedView.showScrollHint(-SCROLL_HINT_FRACTION);
663 mScrollHintDir = DragController.SCROLL_LEFT;
664 }
665
666 // Set alarm for when the hint is complete
667 if (!mOnScrollHintAlarm.alarmPending() || mCurrentScrollDir != DragController.SCROLL_LEFT) {
668 mCurrentScrollDir = DragController.SCROLL_LEFT;
669 mOnScrollHintAlarm.cancelAlarm();
670 mOnScrollHintAlarm.setOnAlarmListener(new OnScrollHintListener(d));
671 mOnScrollHintAlarm.setAlarm(SCROLL_HINT_DURATION);
672
673 mReorderAlarm.cancelAlarm();
674 mTargetRank = mEmptyCellRank;
675 }
676 } else if (currentPage < (mPagedView.getPageCount() - 1) &&
677 (x > (getWidth() - cellWidth * ICON_OVERSCROLL_WIDTH_FACTOR))) {
678 // Show scroll hint on the right
679 if (mScrollHintDir != DragController.SCROLL_RIGHT) {
680 mPagedView.showScrollHint(SCROLL_HINT_FRACTION);
681 mScrollHintDir = DragController.SCROLL_RIGHT;
682 }
683
684 // Set alarm for when the hint is complete
685 if (!mOnScrollHintAlarm.alarmPending() || mCurrentScrollDir != DragController.SCROLL_RIGHT) {
686 mCurrentScrollDir = DragController.SCROLL_RIGHT;
687 mOnScrollHintAlarm.cancelAlarm();
688 mOnScrollHintAlarm.setOnAlarmListener(new OnScrollHintListener(d));
689 mOnScrollHintAlarm.setAlarm(SCROLL_HINT_DURATION);
690
691 mReorderAlarm.cancelAlarm();
692 mTargetRank = mEmptyCellRank;
693 }
694 } else {
695 mOnScrollHintAlarm.cancelAlarm();
696 if (mScrollHintDir != DragController.SCROLL_NONE) {
697 mPagedView.clearScrollHint();
698 mScrollHintDir = DragController.SCROLL_NONE;
699 }
700 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700701 }
702
Adam Cohenbfbfd262011-06-13 16:55:12 -0700703 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
704 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700705 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700706 }
707 };
708
Adam Cohen95bb8002011-07-03 23:40:28 -0700709 public void completeDragExit() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700710 mLauncher.closeFolder();
711 mCurrentDragInfo = null;
712 mCurrentDragView = null;
713 mSuppressOnAdd = false;
714 mRearrangeOnClose = true;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800715 mIsExternalDrag = false;
Adam Cohen3e8f8112011-07-02 18:03:00 -0700716 }
717
Adam Cohencb3382b2011-05-24 14:07:08 -0700718 public void onDragExit(DragObject d) {
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700719 // We only close the folder if this is a true drag exit, ie. not because
720 // a drop has occurred above the folder.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700721 if (!d.dragComplete) {
722 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
723 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
724 }
725 mReorderAlarm.cancelAlarm();
Sunny Goyal48461932015-03-09 17:41:09 -0700726
727 if (ALLOW_FOLDER_SCROLL) {
728 mOnScrollHintAlarm.cancelAlarm();
729 mScrollPauseAlarm.cancelAlarm();
730 if (mScrollHintDir != DragController.SCROLL_NONE) {
731 mPagedView.clearScrollHint();
732 mScrollHintDir = DragController.SCROLL_NONE;
733 }
734 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700735 }
736
Michael Jurka1e2f4652013-07-08 18:03:46 -0700737 public void onDropCompleted(final View target, final DragObject d,
738 final boolean isFlingToDelete, final boolean success) {
739 if (mDeferDropAfterUninstall) {
Michael Jurkaf3007582013-08-21 14:33:57 +0200740 Log.d(TAG, "Deferred handling drop because waiting for uninstall.");
Michael Jurka1e2f4652013-07-08 18:03:46 -0700741 mDeferredAction = new Runnable() {
742 public void run() {
743 onDropCompleted(target, d, isFlingToDelete, success);
744 mDeferredAction = null;
745 }
746 };
747 return;
748 }
749
750 boolean beingCalledAfterUninstall = mDeferredAction != null;
751 boolean successfulDrop =
752 success && (!beingCalledAfterUninstall || mUninstallSuccessful);
Winson Chung5f8afe62013-08-12 16:19:28 -0700753
Michael Jurka1e2f4652013-07-08 18:03:46 -0700754 if (successfulDrop) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800755 if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon && target != this) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700756 replaceFolderWithFinalItem();
757 }
758 } else {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800759 rearrangeChildren();
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700760 // The drag failed, we need to return the item to the folder
761 mFolderIcon.onDrop(d);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700762 }
763
764 if (target != this) {
765 if (mOnExitAlarm.alarmPending()) {
766 mOnExitAlarm.cancelAlarm();
Michael Jurka54554252013-08-01 12:52:23 +0200767 if (!successfulDrop) {
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700768 mSuppressFolderDeletion = true;
769 }
Sunny Goyal5d85c442015-03-10 13:14:47 -0700770 mScrollPauseAlarm.cancelAlarm();
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700771 completeDragExit();
772 }
Adam Cohen9c58d822013-12-13 17:18:10 -0800773 }
774
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700775 mDeleteFolderOnDropCompleted = false;
776 mDragInProgress = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700777 mItemAddedBackToSelfViaIcon = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700778 mCurrentDragInfo = null;
779 mCurrentDragView = null;
780 mSuppressOnAdd = false;
Adam Cohen4045eb72011-10-06 11:44:26 -0700781
782 // Reordering may have occured, and we need to save the new item locations. We do this once
783 // at the end to prevent unnecessary database operations.
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700784 updateItemLocationsInDatabaseBatch();
Adam Cohen4045eb72011-10-06 11:44:26 -0700785 }
786
Michael Jurka1e2f4652013-07-08 18:03:46 -0700787 public void deferCompleteDropAfterUninstallActivity() {
788 mDeferDropAfterUninstall = true;
789 }
790
791 public void onUninstallActivityReturned(boolean success) {
792 mDeferDropAfterUninstall = false;
793 mUninstallSuccessful = success;
794 if (mDeferredAction != null) {
795 mDeferredAction.run();
796 }
797 }
798
Winson Chunga48487a2012-03-20 16:19:37 -0700799 @Override
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800800 public float getIntrinsicIconScaleFactor() {
801 return 1f;
802 }
803
804 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800805 public boolean supportsFlingToDelete() {
806 return true;
807 }
808
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000809 @Override
810 public boolean supportsAppInfoDropTarget() {
811 return false;
812 }
813
814 @Override
815 public boolean supportsDeleteDropTarget() {
816 return true;
817 }
818
Winson Chunga48487a2012-03-20 16:19:37 -0700819 public void onFlingToDelete(DragObject d, int x, int y, PointF vec) {
820 // Do nothing
821 }
822
823 @Override
824 public void onFlingToDeleteCompleted() {
825 // Do nothing
826 }
827
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700828 private void updateItemLocationsInDatabaseBatch() {
829 ArrayList<View> list = getItemsInReadingOrder();
830 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
831 for (int i = 0; i < list.size(); i++) {
832 View v = list.get(i);
833 ItemInfo info = (ItemInfo) v.getTag();
Sunny Goyal08f72612015-01-05 13:41:43 -0800834 info.rank = i;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700835 items.add(info);
836 }
837
838 LauncherModel.moveItemsInDatabase(mLauncher, items, mInfo.id, 0);
839 }
840
Adam Cohene25af792013-06-06 23:08:25 -0700841 public void addItemLocationsInDatabase() {
842 ArrayList<View> list = getItemsInReadingOrder();
843 for (int i = 0; i < list.size(); i++) {
844 View v = list.get(i);
845 ItemInfo info = (ItemInfo) v.getTag();
846 LauncherModel.addItemToDatabase(mLauncher, info, mInfo.id, 0,
847 info.cellX, info.cellY, false);
848 }
849 }
850
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700851 public void notifyDrop() {
852 if (mDragInProgress) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700853 mItemAddedBackToSelfViaIcon = true;
Adam Cohen76078c42011-06-09 15:06:52 -0700854 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700855 }
856
857 public boolean isDropEnabled() {
858 return true;
859 }
860
Adam Cohen2801caf2011-05-13 20:57:39 -0700861 public boolean isFull() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800862 return mContent.isFull();
Adam Cohen2801caf2011-05-13 20:57:39 -0700863 }
864
865 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700866 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700867
Winson Chung892c74d2013-08-22 16:15:50 -0700868 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700869 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700870 int height = getFolderHeight();
Adam Cohen2801caf2011-05-13 20:57:39 -0700871
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800872 float scale = parent.getDescendantRectRelativeToSelf(mFolderIcon, sTempRect);
Adam Cohen8e776a62011-06-28 18:10:06 -0700873
Winson Chungaf40f202013-09-18 18:26:31 -0700874 LauncherAppState app = LauncherAppState.getInstance();
875 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
876
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800877 int centerX = (int) (sTempRect.left + sTempRect.width() * scale / 2);
878 int centerY = (int) (sTempRect.top + sTempRect.height() * scale / 2);
Adam Cohen2801caf2011-05-13 20:57:39 -0700879 int centeredLeft = centerX - width / 2;
880 int centeredTop = centerY - height / 2;
Winson Chung3057b1c2013-10-10 17:35:35 -0700881 int currentPage = mLauncher.getWorkspace().getNextPage();
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800882 // In case the workspace is scrolling, we need to use the final scroll to compute
883 // the folders bounds.
884 mLauncher.getWorkspace().setFinalScrollForPageChange(currentPage);
Adam Cohen35e7e642011-07-17 14:47:18 -0700885 // We first fetch the currently visible CellLayoutChildren
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800886 CellLayout currentLayout = (CellLayout) mLauncher.getWorkspace().getChildAt(currentPage);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700887 ShortcutAndWidgetContainer boundingLayout = currentLayout.getShortcutsAndWidgets();
Adam Cohen35e7e642011-07-17 14:47:18 -0700888 Rect bounds = new Rect();
889 parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800890 // We reset the workspaces scroll
891 mLauncher.getWorkspace().resetFinalScrollForPageChange(currentPage);
Adam Cohen2801caf2011-05-13 20:57:39 -0700892
Adam Cohen35e7e642011-07-17 14:47:18 -0700893 // We need to bound the folder to the currently visible CellLayoutChildren
894 int left = Math.min(Math.max(bounds.left, centeredLeft),
895 bounds.left + bounds.width() - width);
896 int top = Math.min(Math.max(bounds.top, centeredTop),
897 bounds.top + bounds.height() - height);
Winson Chungaf40f202013-09-18 18:26:31 -0700898 if (grid.isPhone() && (grid.availableWidthPx - width) < grid.iconSizePx) {
899 // Center the folder if it is full (on phones only)
900 left = (grid.availableWidthPx - width) / 2;
901 } else if (width >= bounds.width()) {
902 // If the folder doesn't fit within the bounds, center it about the desired bounds
Adam Cohen35e7e642011-07-17 14:47:18 -0700903 left = bounds.left + (bounds.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700904 }
Adam Cohen35e7e642011-07-17 14:47:18 -0700905 if (height >= bounds.height()) {
906 top = bounds.top + (bounds.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700907 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700908
909 int folderPivotX = width / 2 + (centeredLeft - left);
910 int folderPivotY = height / 2 + (centeredTop - top);
911 setPivotX(folderPivotX);
912 setPivotY(folderPivotY);
Adam Cohen268c4752012-06-06 17:47:33 -0700913 mFolderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700914 (1.0f * folderPivotX / width));
Adam Cohen268c4752012-06-06 17:47:33 -0700915 mFolderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700916 (1.0f * folderPivotY / height));
Adam Cohen3bf84d32012-05-07 20:17:14 -0700917
Adam Cohen662b5982011-12-13 17:45:21 -0800918 lp.width = width;
919 lp.height = height;
920 lp.x = left;
921 lp.y = top;
Adam Cohen2801caf2011-05-13 20:57:39 -0700922 }
923
Adam Cohen268c4752012-06-06 17:47:33 -0700924 float getPivotXForIconAnimation() {
925 return mFolderIconPivotX;
926 }
927 float getPivotYForIconAnimation() {
928 return mFolderIconPivotY;
929 }
930
Winson Chung892c74d2013-08-22 16:15:50 -0700931 private int getContentAreaHeight() {
932 LauncherAppState app = LauncherAppState.getInstance();
933 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
934 Rect workspacePadding = grid.getWorkspacePadding(grid.isLandscape ?
935 CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
936 int maxContentAreaHeight = grid.availableHeightPx -
Winson Chung892c74d2013-08-22 16:15:50 -0700937 workspacePadding.top - workspacePadding.bottom -
Sunny Goyal290800b2015-03-05 11:33:33 -0800938 mFooterHeight;
Adam Cohen1960ea42013-11-12 11:33:14 +0000939 int height = Math.min(maxContentAreaHeight,
Winson Chung892c74d2013-08-22 16:15:50 -0700940 mContent.getDesiredHeight());
Adam Cohen1960ea42013-11-12 11:33:14 +0000941 return Math.max(height, MIN_CONTENT_DIMEN);
942 }
943
944 private int getContentAreaWidth() {
945 return Math.max(mContent.getDesiredWidth(), MIN_CONTENT_DIMEN);
Winson Chung892c74d2013-08-22 16:15:50 -0700946 }
947
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700948 private int getFolderHeight() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800949 return getFolderHeight(getContentAreaHeight());
950 }
951
952 private int getFolderHeight(int contentAreaHeight) {
Sunny Goyal290800b2015-03-05 11:33:33 -0800953 return getPaddingTop() + getPaddingBottom() + contentAreaHeight + mFooterHeight;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700954 }
Adam Cohen234c4cd2011-07-17 21:03:04 -0700955
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700956 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800957 int contentWidth = getContentAreaWidth();
958 int contentHeight = getContentAreaHeight();
Adam Cohen1960ea42013-11-12 11:33:14 +0000959
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800960 int contentAreaWidthSpec = MeasureSpec.makeMeasureSpec(contentWidth, MeasureSpec.EXACTLY);
961 int contentAreaHeightSpec = MeasureSpec.makeMeasureSpec(contentHeight, MeasureSpec.EXACTLY);
962
963 mContent.setFixedSize(contentWidth, contentHeight);
964 mContentWrapper.measure(contentAreaWidthSpec, contentAreaHeightSpec);
Sunny Goyal290800b2015-03-05 11:33:33 -0800965 mFooter.measure(contentAreaWidthSpec,
966 MeasureSpec.makeMeasureSpec(mFooterHeight, MeasureSpec.EXACTLY));
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800967
968 int folderWidth = getPaddingLeft() + getPaddingRight() + contentWidth;
969 int folderHeight = getFolderHeight(contentHeight);
970 setMeasuredDimension(folderWidth, folderHeight);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700971 }
972
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800973 /**
974 * Rearranges the children based on their rank.
975 */
976 public void rearrangeChildren() {
977 rearrangeChildren(-1);
978 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700979
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800980 /**
981 * Rearranges the children based on their rank.
982 * @param itemCount if greater than the total children count, empty spaces are left at the end,
983 * otherwise it is ignored.
984 */
985 public void rearrangeChildren(int itemCount) {
986 ArrayList<View> views = getItemsInReadingOrder();
987 mContent.arrangeChildren(views, Math.max(itemCount, views.size()));
Adam Cohen7c693212011-05-18 15:26:57 -0700988 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700989 }
990
Sunny Goyalc4918352015-03-10 18:15:48 -0700991 // TODO remove this once GSA code fix is submitted
992 public ViewGroup getContent() {
993 return (ViewGroup) mContent;
994 }
995
Adam Cohena9cf38f2011-05-02 15:36:58 -0700996 public int getItemCount() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800997 return mContent.getItemCount();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700998 }
999
Adam Cohen2801caf2011-05-13 20:57:39 -07001000 private void onCloseComplete() {
Adam Cohen05e0f402011-08-01 12:12:49 -07001001 DragLayer parent = (DragLayer) getParent();
Michael Jurka5649c282012-06-18 10:33:21 -07001002 if (parent != null) {
1003 parent.removeView(this);
1004 }
Adam Cohen4554ee12011-08-03 16:13:21 -07001005 mDragController.removeDropTarget((DropTarget) this);
Adam Cohen05e0f402011-08-01 12:12:49 -07001006 clearFocus();
Adam Cohenac56cff2011-09-28 20:45:37 -07001007 mFolderIcon.requestFocus();
Adam Cohen05e0f402011-08-01 12:12:49 -07001008
Adam Cohen2801caf2011-05-13 20:57:39 -07001009 if (mRearrangeOnClose) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001010 rearrangeChildren();
Adam Cohen2801caf2011-05-13 20:57:39 -07001011 mRearrangeOnClose = false;
1012 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001013 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001014 if (!mDragInProgress && !mSuppressFolderDeletion) {
1015 replaceFolderWithFinalItem();
1016 } else if (mDragInProgress) {
1017 mDeleteFolderOnDropCompleted = true;
1018 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001019 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001020 mSuppressFolderDeletion = false;
Adam Cohenafb01ee2011-06-23 15:38:03 -07001021 }
1022
1023 private void replaceFolderWithFinalItem() {
Adam Cohenafb01ee2011-06-23 15:38:03 -07001024 // Add the last remaining child to the workspace in place of the folder
Adam Cohenfb91f302012-06-11 15:45:18 -07001025 Runnable onCompleteRunnable = new Runnable() {
1026 @Override
1027 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -07001028 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screenId);
Adam Cohenafb01ee2011-06-23 15:38:03 -07001029
Winson Chung33231f52013-12-09 16:57:45 -08001030 View child = null;
Adam Cohenfb91f302012-06-11 15:45:18 -07001031 // Move the item from the folder to the workspace, in the position of the folder
1032 if (getItemCount() == 1) {
1033 ShortcutInfo finalItem = mInfo.contents.get(0);
Adam Cohenc5e63f32012-07-12 16:16:57 -07001034 child = mLauncher.createShortcut(R.layout.application, cellLayout,
Adam Cohenfb91f302012-06-11 15:45:18 -07001035 finalItem);
1036 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -07001037 mInfo.screenId, mInfo.cellX, mInfo.cellY);
Adam Cohenfb91f302012-06-11 15:45:18 -07001038 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001039 if (getItemCount() <= 1) {
1040 // Remove the folder
1041 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
Dan Sandler0eb687f2014-01-10 13:24:55 -05001042 if (cellLayout != null) {
1043 // b/12446428 -- sometimes the cell layout has already gone away?
1044 cellLayout.removeView(mFolderIcon);
1045 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001046 if (mFolderIcon instanceof DropTarget) {
1047 mDragController.removeDropTarget((DropTarget) mFolderIcon);
1048 }
1049 mLauncher.removeFolder(mInfo);
1050 }
Adam Cohenc5e63f32012-07-12 16:16:57 -07001051 // We add the child after removing the folder to prevent both from existing at
Winson Chung0e6a7132013-08-23 12:55:10 -07001052 // the same time in the CellLayout. We need to add the new item with addInScreenFromBind()
1053 // to ensure that hotseat items are placed correctly.
Adam Cohenc5e63f32012-07-12 16:16:57 -07001054 if (child != null) {
Winson Chung0e6a7132013-08-23 12:55:10 -07001055 mLauncher.getWorkspace().addInScreenFromBind(child, mInfo.container, mInfo.screenId,
Adam Cohenc5e63f32012-07-12 16:16:57 -07001056 mInfo.cellX, mInfo.cellY, mInfo.spanX, mInfo.spanY);
1057 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001058 }
1059 };
Sunny Goyalbc753352015-03-05 09:40:44 -08001060 View finalChild = mContent.getLastItem();
Adam Cohenfb91f302012-06-11 15:45:18 -07001061 if (finalChild != null) {
1062 mFolderIcon.performDestroyAnimation(finalChild, onCompleteRunnable);
Winson Chung33231f52013-12-09 16:57:45 -08001063 } else {
1064 onCompleteRunnable.run();
Adam Cohenafb01ee2011-06-23 15:38:03 -07001065 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001066 mDestroyed = true;
1067 }
1068
1069 boolean isDestroyed() {
1070 return mDestroyed;
Adam Cohen2801caf2011-05-13 20:57:39 -07001071 }
1072
Adam Cohenac56cff2011-09-28 20:45:37 -07001073 // This method keeps track of the last item in the folder for the purposes
1074 // of keyboard focus
Sunny Goyal290800b2015-03-05 11:33:33 -08001075 public void updateTextViewFocus() {
Sunny Goyalbc753352015-03-05 09:40:44 -08001076 View lastChild = mContent.getLastItem();
Adam Cohenac56cff2011-09-28 20:45:37 -07001077 if (lastChild != null) {
1078 mFolderName.setNextFocusDownId(lastChild.getId());
1079 mFolderName.setNextFocusRightId(lastChild.getId());
1080 mFolderName.setNextFocusLeftId(lastChild.getId());
1081 mFolderName.setNextFocusUpId(lastChild.getId());
1082 }
1083 }
1084
Adam Cohenbfbfd262011-06-13 16:55:12 -07001085 public void onDrop(DragObject d) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001086 Runnable cleanUpRunnable = null;
1087
Adam Cohen689ff162014-05-08 17:27:56 -07001088 // If we are coming from All Apps space, we defer removing the extra empty screen
1089 // until the folder closes
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001090 if (d.dragSource != mLauncher.getWorkspace() && !(d.dragSource instanceof Folder)) {
1091 cleanUpRunnable = new Runnable() {
1092 @Override
1093 public void run() {
Adam Cohen689ff162014-05-08 17:27:56 -07001094 mLauncher.exitSpringLoadedDragModeDelayed(true,
1095 Launcher.EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
1096 null);
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001097 }
1098 };
Adam Cohenbfbfd262011-06-13 16:55:12 -07001099 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001100
Sunny Goyal48461932015-03-09 17:41:09 -07001101 if (ALLOW_FOLDER_SCROLL) {
1102 // If the icon was dropped while the page was being scrolled, we need to compute
1103 // the target location again such that the icon is placed of the final page.
1104 if (!mPagedView.rankOnCurrentPage(mEmptyCellRank)) {
1105 // Reorder again.
1106 mTargetRank = getTargetRank(d, null);
1107
1108 // Rearrange items immediately.
1109 mReorderAlarmListener.onAlarm(mReorderAlarm);
1110
1111 mOnScrollHintAlarm.cancelAlarm();
1112 mScrollPauseAlarm.cancelAlarm();
1113 }
1114 mPagedView.completePendingPageChanges();
1115 }
1116
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001117 View currentDragView;
1118 ShortcutInfo si = mCurrentDragInfo;
1119 if (mIsExternalDrag) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001120 currentDragView = mContent.createAndAddViewForRank(si, mEmptyCellRank);
Sunny Goyal95abbb32014-08-04 10:53:22 -07001121 // Actually move the item in the database if it was an external drag. Call this
1122 // before creating the view, so that ShortcutInfo is updated appropriately.
1123 LauncherModel.addOrMoveItemInDatabase(
1124 mLauncher, si, mInfo.id, 0, si.cellX, si.cellY);
1125
1126 // We only need to update the locations if it doesn't get handled in #onDropCompleted.
1127 if (d.dragSource != this) {
1128 updateItemLocationsInDatabaseBatch();
1129 }
1130 mIsExternalDrag = false;
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001131 } else {
1132 currentDragView = mCurrentDragView;
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001133 mContent.addViewForRank(currentDragView, si, mEmptyCellRank);
Adam Cohenbfbfd262011-06-13 16:55:12 -07001134 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001135
1136 if (d.dragView.hasDrawn()) {
1137
1138 // Temporarily reset the scale such that the animation target gets calculated correctly.
1139 float scaleX = getScaleX();
1140 float scaleY = getScaleY();
1141 setScaleX(1.0f);
1142 setScaleY(1.0f);
1143 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, currentDragView,
1144 cleanUpRunnable, null);
1145 setScaleX(scaleX);
1146 setScaleY(scaleY);
1147 } else {
1148 d.deferDragViewCleanupPostAnimation = false;
1149 currentDragView.setVisibility(VISIBLE);
1150 }
1151 mItemsInvalidated = true;
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001152 rearrangeChildren();
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001153
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001154 // Temporarily suppress the listener, as we did all the work already here.
1155 mSuppressOnAdd = true;
1156 mInfo.add(si);
1157 mSuppressOnAdd = false;
Sunny Goyal4b020172014-08-28 14:51:14 -07001158 // Clear the drag info, as it is no longer being dragged.
1159 mCurrentDragInfo = null;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001160 }
1161
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001162 // This is used so the item doesn't immediately appear in the folder when added. In one case
1163 // we need to create the illusion that the item isn't added back to the folder yet, to
1164 // to correspond to the animation of the icon back into the folder. This is
1165 public void hideItem(ShortcutInfo info) {
1166 View v = getViewForInfo(info);
1167 v.setVisibility(INVISIBLE);
1168 }
1169 public void showItem(ShortcutInfo info) {
1170 View v = getViewForInfo(info);
1171 v.setVisibility(VISIBLE);
1172 }
1173
Adam Cohenbfbfd262011-06-13 16:55:12 -07001174 public void onAdd(ShortcutInfo item) {
1175 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001176 // If the item was dropped onto this open folder, we have done the work associated
1177 // with adding the item to the folder, as indicated by mSuppressOnAdd being set
Adam Cohenbfbfd262011-06-13 16:55:12 -07001178 if (mSuppressOnAdd) return;
Sunny Goyal5d85c442015-03-10 13:14:47 -07001179 mContent.createAndAddViewForRank(item, mContent.allocateRankForNewItem(item));
Adam Cohenbfbfd262011-06-13 16:55:12 -07001180 LauncherModel.addOrMoveItemInDatabase(
1181 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
1182 }
1183
Adam Cohena9cf38f2011-05-02 15:36:58 -07001184 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -07001185 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001186 // If this item is being dragged from this open folder, we have already handled
1187 // the work associated with removing the item, so we don't have to do anything here.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001188 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001189 View v = getViewForInfo(item);
Sunny Goyal290800b2015-03-05 11:33:33 -08001190 mContent.removeItem(v);
Adam Cohen2801caf2011-05-13 20:57:39 -07001191 if (mState == STATE_ANIMATING) {
1192 mRearrangeOnClose = true;
1193 } else {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001194 rearrangeChildren();
Adam Cohen2801caf2011-05-13 20:57:39 -07001195 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001196 if (getItemCount() <= 1) {
1197 replaceFolderWithFinalItem();
1198 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001199 }
Adam Cohen7c693212011-05-18 15:26:57 -07001200
Sunny Goyalbc753352015-03-05 09:40:44 -08001201 private View getViewForInfo(final ShortcutInfo item) {
1202 return mContent.iterateOverItems(new ItemOperator() {
1203
1204 @Override
1205 public boolean evaluate(ItemInfo info, View view, View parent) {
1206 return info == item;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001207 }
Sunny Goyalbc753352015-03-05 09:40:44 -08001208 });
Adam Cohendf1e4e82011-06-24 15:57:39 -07001209 }
1210
Adam Cohen76078c42011-06-09 15:06:52 -07001211 public void onItemsChanged() {
Adam Cohenac56cff2011-09-28 20:45:37 -07001212 updateTextViewFocus();
Adam Cohen76078c42011-06-09 15:06:52 -07001213 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001214
Adam Cohen76fc0852011-06-17 13:26:23 -07001215 public void onTitleChanged(CharSequence title) {
1216 }
Adam Cohen76078c42011-06-09 15:06:52 -07001217
Adam Cohen7c693212011-05-18 15:26:57 -07001218 public ArrayList<View> getItemsInReadingOrder() {
1219 if (mItemsInvalidated) {
1220 mItemsInReadingOrder.clear();
Sunny Goyalbc753352015-03-05 09:40:44 -08001221 mContent.iterateOverItems(new ItemOperator() {
1222
1223 @Override
1224 public boolean evaluate(ItemInfo info, View view, View parent) {
1225 mItemsInReadingOrder.add(view);
1226 return false;
Adam Cohen7c693212011-05-18 15:26:57 -07001227 }
Sunny Goyalbc753352015-03-05 09:40:44 -08001228 });
Adam Cohen7c693212011-05-18 15:26:57 -07001229 mItemsInvalidated = false;
1230 }
1231 return mItemsInReadingOrder;
1232 }
Adam Cohen8dfcba42011-07-07 16:38:18 -07001233
1234 public void getLocationInDragLayer(int[] loc) {
1235 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
1236 }
Adam Cohenea0818d2011-09-30 20:06:58 -07001237
1238 public void onFocusChange(View v, boolean hasFocus) {
1239 if (v == mFolderName && hasFocus) {
1240 startEditingFolderName();
1241 }
1242 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001243
1244 @Override
1245 public void getHitRectRelativeToDragLayer(Rect outRect) {
1246 getHitRect(outRect);
Sunny Goyal48461932015-03-09 17:41:09 -07001247 outRect.left -= mScrollAreaOffset;
1248 outRect.right += mScrollAreaOffset;
1249 }
1250
1251 private class OnScrollHintListener implements OnAlarmListener {
1252
1253 private final DragObject mDragObject;
1254
1255 OnScrollHintListener(DragObject object) {
1256 mDragObject = object;
1257 }
1258
1259 /**
1260 * Scroll hint has been shown long enough. Now scroll to appropriate page.
1261 */
1262 @Override
1263 public void onAlarm(Alarm alarm) {
1264 if (mCurrentScrollDir == DragController.SCROLL_LEFT) {
1265 mPagedView.scrollLeft();
1266 mScrollHintDir = DragController.SCROLL_NONE;
1267 } else if (mCurrentScrollDir == DragController.SCROLL_RIGHT) {
1268 mPagedView.scrollRight();
1269 mScrollHintDir = DragController.SCROLL_NONE;
1270 } else {
1271 // This should not happen
1272 return;
1273 }
1274 mCurrentScrollDir = DragController.SCROLL_NONE;
1275
1276 // Pause drag event until the scrolling is finished
1277 mScrollPauseAlarm.setOnAlarmListener(new OnScrollFinishedListener(mDragObject));
1278 mScrollPauseAlarm.setAlarm(DragController.RESCROLL_DELAY);
1279 }
1280 }
1281
1282 private class OnScrollFinishedListener implements OnAlarmListener {
1283
1284 private final DragObject mDragObject;
1285
1286 OnScrollFinishedListener(DragObject object) {
1287 mDragObject = object;
1288 }
1289
1290 /**
1291 * Page scroll is complete.
1292 */
1293 @Override
1294 public void onAlarm(Alarm alarm) {
1295 // Reorder immediately on page change.
1296 onDragOver(mDragObject, 1);
1297 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001298 }
Sunny Goyalbc753352015-03-05 09:40:44 -08001299
1300 public static interface FolderContent {
1301 void setFolder(Folder f);
1302
Sunny Goyal290800b2015-03-05 11:33:33 -08001303 void removeItem(View v);
Sunny Goyalbc753352015-03-05 09:40:44 -08001304
1305 boolean isFull();
1306 int getItemCount();
1307
1308 int getDesiredWidth();
1309 int getDesiredHeight();
1310 void setFixedSize(int width, int height);
1311
1312 /**
1313 * Iterates over all its items in a reading order.
1314 * @return the view for which the operator returned true.
1315 */
1316 View iterateOverItems(ItemOperator op);
1317 View getLastItem();
1318
1319 String getAccessibilityDescription();
1320
1321 /**
1322 * Binds items to the layout.
1323 * @return list of items that could not be bound, probably because we hit the max size limit.
1324 */
1325 ArrayList<ShortcutInfo> bindItems(ArrayList<ShortcutInfo> children);
1326
1327 /**
Sunny Goyal5d85c442015-03-10 13:14:47 -07001328 * Create space for a new item, and returns the rank for that item.
Sunny Goyalbc753352015-03-05 09:40:44 -08001329 * Resizes the content if necessary.
1330 */
Sunny Goyal5d85c442015-03-10 13:14:47 -07001331 int allocateRankForNewItem(ShortcutInfo info);
Sunny Goyalbc753352015-03-05 09:40:44 -08001332
1333 View createAndAddViewForRank(ShortcutInfo item, int rank);
1334
1335 /**
1336 * Adds the {@param view} to the layout based on {@param rank} and updated the position
1337 * related attributes. It assumes that {@param item} is already attached to the view.
1338 */
1339 void addViewForRank(View view, ShortcutInfo item, int rank);
1340
1341 /**
1342 * Reorders the items such that the {@param empty} spot moves to {@param target}
1343 */
1344 void realTimeReorder(int empty, int target);
1345
1346 /**
1347 * @return the rank of the cell nearest to the provided pixel position.
1348 */
1349 int findNearestArea(int pixelX, int pixelY);
1350
1351 /**
1352 * Updates position and rank of all the children in the view based.
1353 * @param list the ordered list of children.
1354 * @param itemCount if greater than the total children count, empty spaces are left
1355 * at the end.
1356 */
1357 void arrangeChildren(ArrayList<View> list, int itemCount);
1358
1359 /**
1360 * Sets the focus on the first visible child.
1361 */
1362 void setFocusOnFirstChild();
1363 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001364}