blob: 7ff60de4fc74d38f3fddd9fbef77590165031d6a [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 /**
89 * Fraction of icon width which behave as scroll region.
90 */
91 private static final float ICON_OVERSCROLL_WIDTH_FACTOR = 0.45f;
92
Adam Cohenf0f4eda2013-06-06 21:27:03 -070093 private static final int REORDER_DELAY = 250;
Adam Cohen5d518fa2013-12-05 14:16:23 -080094 private static final int ON_EXIT_CLOSE_DELAY = 400;
Sunny Goyalc3a609f2015-02-26 17:43:50 -080095 private static final Rect sTempRect = new Rect();
96
97 private static String sDefaultFolderName;
98 private static String sHintText;
99
100 private final Alarm mReorderAlarm = new Alarm();
101 private final Alarm mOnExitAlarm = new Alarm();
102
103 private final ArrayList<View> mItemsInReadingOrder = new ArrayList<View>();
104
105 private final int mExpandDuration;
106 private final int mMaterialExpandDuration;
107 private final int mMaterialExpandStagger;
108
109 private final InputMethodManager mInputMethodManager;
110
111 protected final Launcher mLauncher;
112 protected DragController mDragController;
113 protected FolderInfo mInfo;
114
Adam Cohen2801caf2011-05-13 20:57:39 -0700115 private FolderIcon mFolderIcon;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800116
Sunny Goyalbc753352015-03-05 09:40:44 -0800117 private FolderContent mContent;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800118 private View mContentWrapper;
119 FolderEditText mFolderName;
120
Sunny Goyal290800b2015-03-05 11:33:33 -0800121 private View mFooter;
122 private int mFooterHeight;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800123
124 // Cell ranks used for drag and drop
125 private int mTargetRank, mPrevTargetRank, mEmptyCellRank;
126
127 private int mState = STATE_NONE;
128 private boolean mRearrangeOnClose = false;
Adam Cohen7c693212011-05-18 15:26:57 -0700129 boolean mItemsInvalidated = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700130 private ShortcutInfo mCurrentDragInfo;
131 private View mCurrentDragView;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800132 private boolean mIsExternalDrag;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700133 boolean mSuppressOnAdd = false;
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700134 private boolean mDragInProgress = false;
135 private boolean mDeleteFolderOnDropCompleted = false;
136 private boolean mSuppressFolderDeletion = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700137 private boolean mItemAddedBackToSelfViaIcon = false;
Adam Cohen268c4752012-06-06 17:47:33 -0700138 private float mFolderIconPivotX;
139 private float mFolderIconPivotY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700140 private boolean mIsEditingName = false;
Adam Cohen1960ea42013-11-12 11:33:14 +0000141
Adam Cohenfb91f302012-06-11 15:45:18 -0700142 private boolean mDestroyed;
143
Michael Jurka1e2f4652013-07-08 18:03:46 -0700144 private Runnable mDeferredAction;
145 private boolean mDeferDropAfterUninstall;
146 private boolean mUninstallSuccessful;
147
Sunny Goyal48461932015-03-09 17:41:09 -0700148 // Folder scrolling
149 private int mScrollAreaOffset;
150 private Alarm mOnScrollHintAlarm;
151 private Alarm mScrollPauseAlarm;
152
153 // TODO: Use {@link #mContent} once {@link #ALLOW_FOLDER_SCROLL} is removed.
154 private FolderPagedView mPagedView;
155
156 private int mScrollHintDir = DragController.SCROLL_NONE;
157 private int mCurrentScrollDir = DragController.SCROLL_NONE;
158
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159 /**
160 * Used to inflate the Workspace from XML.
161 *
162 * @param context The application's context.
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800163 * @param attrs The attributes set containing the Workspace's customization values.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164 */
165 public Folder(Context context, AttributeSet attrs) {
166 super(context, attrs);
167 setAlwaysDrawnWithCacheEnabled(false);
Adam Cohen76fc0852011-06-17 13:26:23 -0700168 mInputMethodManager = (InputMethodManager)
Michael Jurka8b805b12012-04-18 14:23:14 -0700169 getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
Adam Cohen76fc0852011-06-17 13:26:23 -0700170
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800171 Resources res = getResources();
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700172 mExpandDuration = res.getInteger(R.integer.config_folderExpandDuration);
173 mMaterialExpandDuration = res.getInteger(R.integer.config_materialFolderExpandDuration);
174 mMaterialExpandStagger = res.getInteger(R.integer.config_materialFolderExpandStagger);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700175
176 if (sDefaultFolderName == null) {
177 sDefaultFolderName = res.getString(R.string.folder_name);
178 }
Adam Cohena65beee2011-06-27 21:32:23 -0700179 if (sHintText == null) {
180 sHintText = res.getString(R.string.folder_hint_text);
181 }
Adam Cohen4eac29a2011-07-11 17:53:37 -0700182 mLauncher = (Launcher) context;
Adam Cohenac56cff2011-09-28 20:45:37 -0700183 // We need this view to be focusable in touch mode so that when text editing of the folder
184 // name is complete, we have something to focus on, thus hiding the cursor and giving
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800185 // reliable behavior when clicking the text field (since it will always gain focus on click).
Adam Cohenac56cff2011-09-28 20:45:37 -0700186 setFocusableInTouchMode(true);
Sunny Goyal48461932015-03-09 17:41:09 -0700187
188 if (ALLOW_FOLDER_SCROLL) {
189 mOnScrollHintAlarm = new Alarm();
190 mScrollPauseAlarm = new Alarm();
191 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192 }
193
194 @Override
195 protected void onFinishInflate() {
196 super.onFinishInflate();
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800197 mContentWrapper = findViewById(R.id.folder_content_wrapper);
Sunny Goyalbc753352015-03-05 09:40:44 -0800198 mContent = (FolderContent) findViewById(R.id.folder_content);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800199 mContent.setFolder(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700200
Adam Cohenac56cff2011-09-28 20:45:37 -0700201 mFolderName = (FolderEditText) findViewById(R.id.folder_name);
202 mFolderName.setFolder(this);
Adam Cohenea0818d2011-09-30 20:06:58 -0700203 mFolderName.setOnFocusChangeListener(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700204
Adam Cohen76fc0852011-06-17 13:26:23 -0700205 // We disable action mode for now since it messes up the view on phones
206 mFolderName.setCustomSelectionActionModeCallback(mActionModeCallback);
Adam Cohen76fc0852011-06-17 13:26:23 -0700207 mFolderName.setOnEditorActionListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700208 mFolderName.setSelectAllOnFocus(true);
Adam Cohen7a14d0b2011-06-24 11:36:35 -0700209 mFolderName.setInputType(mFolderName.getInputType() |
210 InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800211
Sunny Goyal290800b2015-03-05 11:33:33 -0800212 mFooter = ALLOW_FOLDER_SCROLL ? findViewById(R.id.folder_footer) : mFolderName;
213 // We find out how tall footer wants to be (it is set to wrap_content), so that
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800214 // we can allocate the appropriate amount of space for it.
215 int measureSpec = MeasureSpec.UNSPECIFIED;
Sunny Goyal290800b2015-03-05 11:33:33 -0800216 mFooter.measure(measureSpec, measureSpec);
217 mFooterHeight = mFooter.getMeasuredHeight();
Sunny Goyal48461932015-03-09 17:41:09 -0700218
219 if (ALLOW_FOLDER_SCROLL) {
220 mPagedView = (FolderPagedView) mContent;
221 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800222 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700223
Adam Cohen76fc0852011-06-17 13:26:23 -0700224 private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
225 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
226 return false;
227 }
228
229 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
230 return false;
231 }
232
233 public void onDestroyActionMode(ActionMode mode) {
234 }
235
236 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
237 return false;
238 }
239 };
240
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800241 public void onClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700242 Object tag = v.getTag();
243 if (tag instanceof ShortcutInfo) {
Adam Cohenb5fe60c2013-06-06 22:03:51 -0700244 mLauncher.onClick(v);
Adam Cohendf2cc412011-04-27 16:56:57 -0700245 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800246 }
247
248 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -0700249 // Return if global dragging is not enabled
250 if (!mLauncher.isDraggingEnabled()) return true;
251
Adam Cohendf2cc412011-04-27 16:56:57 -0700252 Object tag = v.getTag();
253 if (tag instanceof ShortcutInfo) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700254 ShortcutInfo item = (ShortcutInfo) tag;
255 if (!v.isInTouchMode()) {
256 return false;
257 }
258
Winson Chungb745afb2015-03-02 11:51:23 -0800259 mLauncher.getWorkspace().beginDragShared(v, new Point(), this, false);
Adam Cohen76078c42011-06-09 15:06:52 -0700260
261 mCurrentDragInfo = item;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800262 mEmptyCellRank = item.rank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700263 mCurrentDragView = v;
Adam Cohenfc53cd22011-07-20 15:45:11 -0700264
Sunny Goyal290800b2015-03-05 11:33:33 -0800265 mContent.removeItem(mCurrentDragView);
Adam Cohenfc53cd22011-07-20 15:45:11 -0700266 mInfo.remove(mCurrentDragInfo);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700267 mDragInProgress = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700268 mItemAddedBackToSelfViaIcon = false;
Adam Cohendf2cc412011-04-27 16:56:57 -0700269 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800270 return true;
271 }
272
Adam Cohen76fc0852011-06-17 13:26:23 -0700273 public boolean isEditingName() {
274 return mIsEditingName;
275 }
276
277 public void startEditingFolderName() {
Adam Cohena65beee2011-06-27 21:32:23 -0700278 mFolderName.setHint("");
Adam Cohen76fc0852011-06-17 13:26:23 -0700279 mIsEditingName = true;
280 }
281
282 public void dismissEditingName() {
283 mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
284 doneEditingFolderName(true);
285 }
286
287 public void doneEditingFolderName(boolean commit) {
Adam Cohena65beee2011-06-27 21:32:23 -0700288 mFolderName.setHint(sHintText);
Adam Cohen1df26a32011-08-12 16:15:23 -0700289 // Convert to a string here to ensure that no other state associated with the text field
290 // gets saved.
Adam Cohen3371da02011-10-25 21:38:29 -0700291 String newTitle = mFolderName.getText().toString();
292 mInfo.setTitle(newTitle);
Adam Cohen76fc0852011-06-17 13:26:23 -0700293 LauncherModel.updateItemInDatabase(mLauncher, mInfo);
Adam Cohenac56cff2011-09-28 20:45:37 -0700294
Adam Cohen3371da02011-10-25 21:38:29 -0700295 if (commit) {
296 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700297 String.format(getContext().getString(R.string.folder_renamed), newTitle));
Adam Cohen3371da02011-10-25 21:38:29 -0700298 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700299 // In order to clear the focus from the text field, we set the focus on ourself. This
300 // ensures that every time the field is clicked, focus is gained, giving reliable behavior.
301 requestFocus();
302
Adam Cohene601a432011-07-26 21:51:30 -0700303 Selection.setSelection((Spannable) mFolderName.getText(), 0, 0);
Adam Cohen76fc0852011-06-17 13:26:23 -0700304 mIsEditingName = false;
305 }
306
307 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
308 if (actionId == EditorInfo.IME_ACTION_DONE) {
309 dismissEditingName();
310 return true;
311 }
312 return false;
313 }
314
315 public View getEditTextRegion() {
316 return mFolderName;
317 }
318
Adam Cohen0c872ba2011-05-05 10:34:16 -0700319 /**
320 * We need to handle touch events to prevent them from falling through to the workspace below.
321 */
322 @Override
323 public boolean onTouchEvent(MotionEvent ev) {
324 return true;
325 }
326
Joe Onorato00acb122009-08-04 16:04:30 -0400327 public void setDragController(DragController dragController) {
328 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800329 }
330
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800331 public void setFolderIcon(FolderIcon icon) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700332 mFolderIcon = icon;
333 }
334
Adam Cohen3371da02011-10-25 21:38:29 -0700335 @Override
336 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
337 // When the folder gets focus, we don't want to announce the list of items.
338 return true;
339 }
340
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800341 /**
342 * @return the FolderInfo object associated with this folder
343 */
344 FolderInfo getInfo() {
345 return mInfo;
346 }
347
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800348 void bind(FolderInfo info) {
349 mInfo = info;
Adam Cohendf2cc412011-04-27 16:56:57 -0700350 ArrayList<ShortcutInfo> children = info.contents;
Sunny Goyal08f72612015-01-05 13:41:43 -0800351 Collections.sort(children, Utilities.RANK_COMPARATOR);
Sunny Goyal08f72612015-01-05 13:41:43 -0800352
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800353 ArrayList<ShortcutInfo> overflow = mContent.bindItems(children);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700354
Jason Monk4ff73882014-04-24 16:48:07 -0400355 // If our folder has too many items we prune them from the list. This is an issue
Adam Cohenc508b2d2011-06-28 14:41:44 -0700356 // when upgrading from the old Folders implementation which could contain an unlimited
357 // number of items.
358 for (ShortcutInfo item: overflow) {
359 mInfo.remove(item);
360 LauncherModel.deleteItemFromDatabase(mLauncher, item);
361 }
362
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800363 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
364 if (lp == null) {
365 lp = new DragLayer.LayoutParams(0, 0);
366 lp.customPosition = true;
367 setLayoutParams(lp);
368 }
369 centerAboutIcon();
370
Adam Cohen4dbe6d92011-05-18 17:14:15 -0700371 mItemsInvalidated = true;
Adam Cohenac56cff2011-09-28 20:45:37 -0700372 updateTextViewFocus();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700373 mInfo.addListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700374
Adam Cohenafb01ee2011-06-23 15:38:03 -0700375 if (!sDefaultFolderName.contentEquals(mInfo.title)) {
Adam Cohen4ef610f2011-06-21 22:37:36 -0700376 mFolderName.setText(mInfo.title);
377 } else {
378 mFolderName.setText("");
379 }
Winson Chung33231f52013-12-09 16:57:45 -0800380
381 // In case any children didn't come across during loading, clean up the folder accordingly
382 mFolderIcon.post(new Runnable() {
383 public void run() {
384 if (getItemCount() <= 1) {
385 replaceFolderWithFinalItem();
386 }
387 }
388 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700389 }
390
391 /**
392 * Creates a new UserFolder, inflated from R.layout.user_folder.
393 *
394 * @param context The application's context.
395 *
396 * @return A new UserFolder.
397 */
398 static Folder fromXml(Context context) {
Sunny Goyal290800b2015-03-05 11:33:33 -0800399 return (Folder) LayoutInflater.from(context).inflate(
400 ALLOW_FOLDER_SCROLL ? R.layout.user_folder_scroll : R.layout.user_folder, null);
Adam Cohendf2cc412011-04-27 16:56:57 -0700401 }
402
403 /**
404 * This method is intended to make the UserFolder to be visually identical in size and position
405 * to its associated FolderIcon. This allows for a seamless transition into the expanded state.
406 */
407 private void positionAndSizeAsIcon() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700408 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800409 setScaleX(0.8f);
410 setScaleY(0.8f);
411 setAlpha(0f);
Adam Cohendf2cc412011-04-27 16:56:57 -0700412 mState = STATE_SMALL;
413 }
414
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700415 private void prepareReveal() {
416 setScaleX(1f);
417 setScaleY(1f);
418 setAlpha(1f);
419 mState = STATE_SMALL;
420 }
421
Adam Cohendf2cc412011-04-27 16:56:57 -0700422 public void animateOpen() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700423 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen6441de02011-12-14 14:25:32 -0800424
Sunny Goyal48461932015-03-09 17:41:09 -0700425 if (ALLOW_FOLDER_SCROLL) {
426 // Always open on the first page.
427 mPagedView.snapToPageImmediately(0);
428 }
429
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700430 Animator openFolderAnim = null;
431 final Runnable onCompleteRunnable;
Kenny Guyd794a3f2014-09-16 15:17:58 +0100432 if (!Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700433 positionAndSizeAsIcon();
434 centerAboutIcon();
435
436 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1);
437 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
438 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
439 final ObjectAnimator oa =
440 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
441 oa.setDuration(mExpandDuration);
442 openFolderAnim = oa;
443
444 setLayerType(LAYER_TYPE_HARDWARE, null);
445 onCompleteRunnable = new Runnable() {
446 @Override
447 public void run() {
448 setLayerType(LAYER_TYPE_NONE, null);
449 }
450 };
451 } else {
452 prepareReveal();
453 centerAboutIcon();
454
455 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
456 int height = getFolderHeight();
457
458 float transX = - 0.075f * (width / 2 - getPivotX());
459 float transY = - 0.075f * (height / 2 - getPivotY());
460 setTranslationX(transX);
461 setTranslationY(transY);
462 PropertyValuesHolder tx = PropertyValuesHolder.ofFloat("translationX", transX, 0);
463 PropertyValuesHolder ty = PropertyValuesHolder.ofFloat("translationY", transY, 0);
464
465 int rx = (int) Math.max(Math.max(width - getPivotX(), 0), getPivotX());
466 int ry = (int) Math.max(Math.max(height - getPivotY(), 0), getPivotY());
467 float radius = (float) Math.sqrt(rx * rx + ry * ry);
468 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
469 Animator reveal = LauncherAnimUtils.createCircularReveal(this, (int) getPivotX(),
470 (int) getPivotY(), 0, radius);
471 reveal.setDuration(mMaterialExpandDuration);
472 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
473
Sunny Goyalbc753352015-03-05 09:40:44 -0800474 mContentWrapper.setAlpha(0f);
475 Animator iconsAlpha = LauncherAnimUtils.ofFloat(mContentWrapper, "alpha", 0f, 1f);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700476 iconsAlpha.setDuration(mMaterialExpandDuration);
477 iconsAlpha.setStartDelay(mMaterialExpandStagger);
478 iconsAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
479
Sunny Goyal290800b2015-03-05 11:33:33 -0800480 mFooter.setAlpha(0f);
481 Animator textAlpha = LauncherAnimUtils.ofFloat(mFooter, "alpha", 0f, 1f);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700482 textAlpha.setDuration(mMaterialExpandDuration);
483 textAlpha.setStartDelay(mMaterialExpandStagger);
484 textAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
485
486 Animator drift = LauncherAnimUtils.ofPropertyValuesHolder(this, tx, ty);
487 drift.setDuration(mMaterialExpandDuration);
488 drift.setStartDelay(mMaterialExpandStagger);
489 drift.setInterpolator(new LogDecelerateInterpolator(60, 0));
490
491 anim.play(drift);
492 anim.play(iconsAlpha);
493 anim.play(textAlpha);
494 anim.play(reveal);
495
496 openFolderAnim = anim;
497
Sunny Goyalbc753352015-03-05 09:40:44 -0800498 mContentWrapper.setLayerType(LAYER_TYPE_HARDWARE, null);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700499 onCompleteRunnable = new Runnable() {
500 @Override
501 public void run() {
Sunny Goyalbc753352015-03-05 09:40:44 -0800502 mContentWrapper.setLayerType(LAYER_TYPE_NONE, null);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700503 }
504 };
505 }
506 openFolderAnim.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700507 @Override
508 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700509 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Sunny Goyalbc753352015-03-05 09:40:44 -0800510 mContent.getAccessibilityDescription());
Adam Cohendf2cc412011-04-27 16:56:57 -0700511 mState = STATE_ANIMATING;
512 }
513 @Override
514 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700515 mState = STATE_OPEN;
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700516
517 if (onCompleteRunnable != null) {
518 onCompleteRunnable.run();
519 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800520
Sunny Goyalbc753352015-03-05 09:40:44 -0800521 mContent.setFocusOnFirstChild();
Adam Cohendf2cc412011-04-27 16:56:57 -0700522 }
523 });
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700524 openFolderAnim.start();
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800525
526 // Make sure the folder picks up the last drag move even if the finger doesn't move.
527 if (mDragController.isDragging()) {
528 mDragController.forceTouchMove();
529 }
530 }
531
532 public void beginExternalDrag(ShortcutInfo item) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800533 mCurrentDragInfo = item;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800534 mEmptyCellRank = mContent.allocateNewLastItemRank();
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800535 mIsExternalDrag = true;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800536 mDragInProgress = true;
Adam Cohendf2cc412011-04-27 16:56:57 -0700537 }
538
Adam Cohen3371da02011-10-25 21:38:29 -0700539 private void sendCustomAccessibilityEvent(int type, String text) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700540 AccessibilityManager accessibilityManager = (AccessibilityManager)
541 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
542 if (accessibilityManager.isEnabled()) {
Adam Cohen3371da02011-10-25 21:38:29 -0700543 AccessibilityEvent event = AccessibilityEvent.obtain(type);
544 onInitializeAccessibilityEvent(event);
545 event.getText().add(text);
Michael Jurka8b805b12012-04-18 14:23:14 -0700546 accessibilityManager.sendAccessibilityEvent(event);
Adam Cohen3371da02011-10-25 21:38:29 -0700547 }
548 }
549
Adam Cohendf2cc412011-04-27 16:56:57 -0700550 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700551 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800552 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
553 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
554 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
Michael Jurka032e6ba2013-04-22 15:08:42 +0200555 final ObjectAnimator oa =
Michael Jurka2ecf9952012-06-18 12:52:28 -0700556 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohendf2cc412011-04-27 16:56:57 -0700557
Adam Cohen2801caf2011-05-13 20:57:39 -0700558 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700559 @Override
560 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700561 onCloseComplete();
Michael Jurka0121c3e2012-05-31 08:36:04 -0700562 setLayerType(LAYER_TYPE_NONE, null);
Adam Cohen2801caf2011-05-13 20:57:39 -0700563 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700564 }
565 @Override
566 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700567 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700568 getContext().getString(R.string.folder_closed));
Adam Cohendf2cc412011-04-27 16:56:57 -0700569 mState = STATE_ANIMATING;
570 }
571 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700572 oa.setDuration(mExpandDuration);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700573 setLayerType(LAYER_TYPE_HARDWARE, null);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100574 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700575 }
576
Adam Cohencb3382b2011-05-24 14:07:08 -0700577 public boolean acceptDrop(DragObject d) {
578 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700579 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700580 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
581 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
582 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700583 }
584
Adam Cohencb3382b2011-05-24 14:07:08 -0700585 public void onDragEnter(DragObject d) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800586 mPrevTargetRank = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700587 mOnExitAlarm.cancelAlarm();
Sunny Goyal48461932015-03-09 17:41:09 -0700588 if (ALLOW_FOLDER_SCROLL) {
589 // Get the area offset such that the folder only closes if half the drag icon width
590 // is outside the folder area
591 mScrollAreaOffset = d.dragView.getDragRegionWidth() / 2 - d.xOffset;
592 }
Adam Cohenbfbfd262011-06-13 16:55:12 -0700593 }
594
595 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
596 public void onAlarm(Alarm alarm) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800597 mContent.realTimeReorder(mEmptyCellRank, mTargetRank);
598 mEmptyCellRank = mTargetRank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700599 }
600 };
601
Sunny Goyalc46bfef2015-01-05 12:40:08 -0800602 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohen2374abf2013-04-16 14:56:57 -0700603 public boolean isLayoutRtl() {
604 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
605 }
606
Sunny Goyal48461932015-03-09 17:41:09 -0700607 @Override
Adam Cohencb3382b2011-05-24 14:07:08 -0700608 public void onDragOver(DragObject d) {
Sunny Goyal48461932015-03-09 17:41:09 -0700609 onDragOver(d, REORDER_DELAY);
610 }
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700611
Sunny Goyal48461932015-03-09 17:41:09 -0700612 private int getTargetRank(DragObject d, float[] recycle) {
613 recycle = d.getVisualCenter(recycle);
614 return mContent.findNearestArea(
615 (int) recycle[0] - getPaddingLeft(), (int) recycle[1] - getPaddingTop());
616 }
617
618 private void onDragOver(DragObject d, int reorderDelay) {
619 if (ALLOW_FOLDER_SCROLL && mScrollPauseAlarm.alarmPending()) {
620 return;
621 }
622 final float[] r = new float[2];
623 mTargetRank = getTargetRank(d, r);
624
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800625 if (mTargetRank != mPrevTargetRank) {
Alan Viverette4cda5b72013-08-28 17:53:41 -0700626 mReorderAlarm.cancelAlarm();
Sunny Goyalc46bfef2015-01-05 12:40:08 -0800627 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
628 mReorderAlarm.setAlarm(REORDER_DELAY);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800629 mPrevTargetRank = mTargetRank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700630 }
Sunny Goyal48461932015-03-09 17:41:09 -0700631
632 if (!ALLOW_FOLDER_SCROLL) {
633 return;
634 }
635
636 float x = r[0];
637 int currentPage = mPagedView.getNextPage();
638 int cellWidth = mPagedView.getCurrentCellLayout().getCellWidth();
639 if (currentPage > 0 && x < cellWidth * ICON_OVERSCROLL_WIDTH_FACTOR) {
640 // Show scroll hint on the left
641 if (mScrollHintDir != DragController.SCROLL_LEFT) {
642 mPagedView.showScrollHint(-SCROLL_HINT_FRACTION);
643 mScrollHintDir = DragController.SCROLL_LEFT;
644 }
645
646 // Set alarm for when the hint is complete
647 if (!mOnScrollHintAlarm.alarmPending() || mCurrentScrollDir != DragController.SCROLL_LEFT) {
648 mCurrentScrollDir = DragController.SCROLL_LEFT;
649 mOnScrollHintAlarm.cancelAlarm();
650 mOnScrollHintAlarm.setOnAlarmListener(new OnScrollHintListener(d));
651 mOnScrollHintAlarm.setAlarm(SCROLL_HINT_DURATION);
652
653 mReorderAlarm.cancelAlarm();
654 mTargetRank = mEmptyCellRank;
655 }
656 } else if (currentPage < (mPagedView.getPageCount() - 1) &&
657 (x > (getWidth() - cellWidth * ICON_OVERSCROLL_WIDTH_FACTOR))) {
658 // Show scroll hint on the right
659 if (mScrollHintDir != DragController.SCROLL_RIGHT) {
660 mPagedView.showScrollHint(SCROLL_HINT_FRACTION);
661 mScrollHintDir = DragController.SCROLL_RIGHT;
662 }
663
664 // Set alarm for when the hint is complete
665 if (!mOnScrollHintAlarm.alarmPending() || mCurrentScrollDir != DragController.SCROLL_RIGHT) {
666 mCurrentScrollDir = DragController.SCROLL_RIGHT;
667 mOnScrollHintAlarm.cancelAlarm();
668 mOnScrollHintAlarm.setOnAlarmListener(new OnScrollHintListener(d));
669 mOnScrollHintAlarm.setAlarm(SCROLL_HINT_DURATION);
670
671 mReorderAlarm.cancelAlarm();
672 mTargetRank = mEmptyCellRank;
673 }
674 } else {
675 mOnScrollHintAlarm.cancelAlarm();
676 if (mScrollHintDir != DragController.SCROLL_NONE) {
677 mPagedView.clearScrollHint();
678 mScrollHintDir = DragController.SCROLL_NONE;
679 }
680 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700681 }
682
Adam Cohenbfbfd262011-06-13 16:55:12 -0700683 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
684 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700685 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700686 }
687 };
688
Adam Cohen95bb8002011-07-03 23:40:28 -0700689 public void completeDragExit() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700690 mLauncher.closeFolder();
691 mCurrentDragInfo = null;
692 mCurrentDragView = null;
693 mSuppressOnAdd = false;
694 mRearrangeOnClose = true;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800695 mIsExternalDrag = false;
Adam Cohen3e8f8112011-07-02 18:03:00 -0700696 }
697
Adam Cohencb3382b2011-05-24 14:07:08 -0700698 public void onDragExit(DragObject d) {
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700699 // We only close the folder if this is a true drag exit, ie. not because
700 // a drop has occurred above the folder.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700701 if (!d.dragComplete) {
702 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
703 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
704 }
705 mReorderAlarm.cancelAlarm();
Sunny Goyal48461932015-03-09 17:41:09 -0700706
707 if (ALLOW_FOLDER_SCROLL) {
708 mOnScrollHintAlarm.cancelAlarm();
709 mScrollPauseAlarm.cancelAlarm();
710 if (mScrollHintDir != DragController.SCROLL_NONE) {
711 mPagedView.clearScrollHint();
712 mScrollHintDir = DragController.SCROLL_NONE;
713 }
714 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700715 }
716
Michael Jurka1e2f4652013-07-08 18:03:46 -0700717 public void onDropCompleted(final View target, final DragObject d,
718 final boolean isFlingToDelete, final boolean success) {
719 if (mDeferDropAfterUninstall) {
Michael Jurkaf3007582013-08-21 14:33:57 +0200720 Log.d(TAG, "Deferred handling drop because waiting for uninstall.");
Michael Jurka1e2f4652013-07-08 18:03:46 -0700721 mDeferredAction = new Runnable() {
722 public void run() {
723 onDropCompleted(target, d, isFlingToDelete, success);
724 mDeferredAction = null;
725 }
726 };
727 return;
728 }
729
730 boolean beingCalledAfterUninstall = mDeferredAction != null;
731 boolean successfulDrop =
732 success && (!beingCalledAfterUninstall || mUninstallSuccessful);
Winson Chung5f8afe62013-08-12 16:19:28 -0700733
Michael Jurka1e2f4652013-07-08 18:03:46 -0700734 if (successfulDrop) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800735 if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon && target != this) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700736 replaceFolderWithFinalItem();
737 }
738 } else {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800739 rearrangeChildren();
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700740 // The drag failed, we need to return the item to the folder
741 mFolderIcon.onDrop(d);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700742 }
743
744 if (target != this) {
745 if (mOnExitAlarm.alarmPending()) {
746 mOnExitAlarm.cancelAlarm();
Michael Jurka54554252013-08-01 12:52:23 +0200747 if (!successfulDrop) {
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700748 mSuppressFolderDeletion = true;
749 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700750 completeDragExit();
751 }
Adam Cohen9c58d822013-12-13 17:18:10 -0800752 }
753
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700754 mDeleteFolderOnDropCompleted = false;
755 mDragInProgress = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700756 mItemAddedBackToSelfViaIcon = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700757 mCurrentDragInfo = null;
758 mCurrentDragView = null;
759 mSuppressOnAdd = false;
Adam Cohen4045eb72011-10-06 11:44:26 -0700760
761 // Reordering may have occured, and we need to save the new item locations. We do this once
762 // at the end to prevent unnecessary database operations.
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700763 updateItemLocationsInDatabaseBatch();
Adam Cohen4045eb72011-10-06 11:44:26 -0700764 }
765
Michael Jurka1e2f4652013-07-08 18:03:46 -0700766 public void deferCompleteDropAfterUninstallActivity() {
767 mDeferDropAfterUninstall = true;
768 }
769
770 public void onUninstallActivityReturned(boolean success) {
771 mDeferDropAfterUninstall = false;
772 mUninstallSuccessful = success;
773 if (mDeferredAction != null) {
774 mDeferredAction.run();
775 }
776 }
777
Winson Chunga48487a2012-03-20 16:19:37 -0700778 @Override
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800779 public float getIntrinsicIconScaleFactor() {
780 return 1f;
781 }
782
783 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800784 public boolean supportsFlingToDelete() {
785 return true;
786 }
787
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000788 @Override
789 public boolean supportsAppInfoDropTarget() {
790 return false;
791 }
792
793 @Override
794 public boolean supportsDeleteDropTarget() {
795 return true;
796 }
797
Winson Chunga48487a2012-03-20 16:19:37 -0700798 public void onFlingToDelete(DragObject d, int x, int y, PointF vec) {
799 // Do nothing
800 }
801
802 @Override
803 public void onFlingToDeleteCompleted() {
804 // Do nothing
805 }
806
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700807 private void updateItemLocationsInDatabaseBatch() {
808 ArrayList<View> list = getItemsInReadingOrder();
809 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
810 for (int i = 0; i < list.size(); i++) {
811 View v = list.get(i);
812 ItemInfo info = (ItemInfo) v.getTag();
Sunny Goyal08f72612015-01-05 13:41:43 -0800813 info.rank = i;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700814 items.add(info);
815 }
816
817 LauncherModel.moveItemsInDatabase(mLauncher, items, mInfo.id, 0);
818 }
819
Adam Cohene25af792013-06-06 23:08:25 -0700820 public void addItemLocationsInDatabase() {
821 ArrayList<View> list = getItemsInReadingOrder();
822 for (int i = 0; i < list.size(); i++) {
823 View v = list.get(i);
824 ItemInfo info = (ItemInfo) v.getTag();
825 LauncherModel.addItemToDatabase(mLauncher, info, mInfo.id, 0,
826 info.cellX, info.cellY, false);
827 }
828 }
829
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700830 public void notifyDrop() {
831 if (mDragInProgress) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700832 mItemAddedBackToSelfViaIcon = true;
Adam Cohen76078c42011-06-09 15:06:52 -0700833 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700834 }
835
836 public boolean isDropEnabled() {
837 return true;
838 }
839
Adam Cohen2801caf2011-05-13 20:57:39 -0700840 public boolean isFull() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800841 return mContent.isFull();
Adam Cohen2801caf2011-05-13 20:57:39 -0700842 }
843
844 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700845 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700846
Winson Chung892c74d2013-08-22 16:15:50 -0700847 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700848 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700849 int height = getFolderHeight();
Adam Cohen2801caf2011-05-13 20:57:39 -0700850
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800851 float scale = parent.getDescendantRectRelativeToSelf(mFolderIcon, sTempRect);
Adam Cohen8e776a62011-06-28 18:10:06 -0700852
Winson Chungaf40f202013-09-18 18:26:31 -0700853 LauncherAppState app = LauncherAppState.getInstance();
854 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
855
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800856 int centerX = (int) (sTempRect.left + sTempRect.width() * scale / 2);
857 int centerY = (int) (sTempRect.top + sTempRect.height() * scale / 2);
Adam Cohen2801caf2011-05-13 20:57:39 -0700858 int centeredLeft = centerX - width / 2;
859 int centeredTop = centerY - height / 2;
Winson Chung3057b1c2013-10-10 17:35:35 -0700860 int currentPage = mLauncher.getWorkspace().getNextPage();
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800861 // In case the workspace is scrolling, we need to use the final scroll to compute
862 // the folders bounds.
863 mLauncher.getWorkspace().setFinalScrollForPageChange(currentPage);
Adam Cohen35e7e642011-07-17 14:47:18 -0700864 // We first fetch the currently visible CellLayoutChildren
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800865 CellLayout currentLayout = (CellLayout) mLauncher.getWorkspace().getChildAt(currentPage);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700866 ShortcutAndWidgetContainer boundingLayout = currentLayout.getShortcutsAndWidgets();
Adam Cohen35e7e642011-07-17 14:47:18 -0700867 Rect bounds = new Rect();
868 parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800869 // We reset the workspaces scroll
870 mLauncher.getWorkspace().resetFinalScrollForPageChange(currentPage);
Adam Cohen2801caf2011-05-13 20:57:39 -0700871
Adam Cohen35e7e642011-07-17 14:47:18 -0700872 // We need to bound the folder to the currently visible CellLayoutChildren
873 int left = Math.min(Math.max(bounds.left, centeredLeft),
874 bounds.left + bounds.width() - width);
875 int top = Math.min(Math.max(bounds.top, centeredTop),
876 bounds.top + bounds.height() - height);
Winson Chungaf40f202013-09-18 18:26:31 -0700877 if (grid.isPhone() && (grid.availableWidthPx - width) < grid.iconSizePx) {
878 // Center the folder if it is full (on phones only)
879 left = (grid.availableWidthPx - width) / 2;
880 } else if (width >= bounds.width()) {
881 // If the folder doesn't fit within the bounds, center it about the desired bounds
Adam Cohen35e7e642011-07-17 14:47:18 -0700882 left = bounds.left + (bounds.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700883 }
Adam Cohen35e7e642011-07-17 14:47:18 -0700884 if (height >= bounds.height()) {
885 top = bounds.top + (bounds.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700886 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700887
888 int folderPivotX = width / 2 + (centeredLeft - left);
889 int folderPivotY = height / 2 + (centeredTop - top);
890 setPivotX(folderPivotX);
891 setPivotY(folderPivotY);
Adam Cohen268c4752012-06-06 17:47:33 -0700892 mFolderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700893 (1.0f * folderPivotX / width));
Adam Cohen268c4752012-06-06 17:47:33 -0700894 mFolderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700895 (1.0f * folderPivotY / height));
Adam Cohen3bf84d32012-05-07 20:17:14 -0700896
Adam Cohen662b5982011-12-13 17:45:21 -0800897 lp.width = width;
898 lp.height = height;
899 lp.x = left;
900 lp.y = top;
Adam Cohen2801caf2011-05-13 20:57:39 -0700901 }
902
Adam Cohen268c4752012-06-06 17:47:33 -0700903 float getPivotXForIconAnimation() {
904 return mFolderIconPivotX;
905 }
906 float getPivotYForIconAnimation() {
907 return mFolderIconPivotY;
908 }
909
Winson Chung892c74d2013-08-22 16:15:50 -0700910 private int getContentAreaHeight() {
911 LauncherAppState app = LauncherAppState.getInstance();
912 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
913 Rect workspacePadding = grid.getWorkspacePadding(grid.isLandscape ?
914 CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
915 int maxContentAreaHeight = grid.availableHeightPx -
Winson Chung892c74d2013-08-22 16:15:50 -0700916 workspacePadding.top - workspacePadding.bottom -
Sunny Goyal290800b2015-03-05 11:33:33 -0800917 mFooterHeight;
Adam Cohen1960ea42013-11-12 11:33:14 +0000918 int height = Math.min(maxContentAreaHeight,
Winson Chung892c74d2013-08-22 16:15:50 -0700919 mContent.getDesiredHeight());
Adam Cohen1960ea42013-11-12 11:33:14 +0000920 return Math.max(height, MIN_CONTENT_DIMEN);
921 }
922
923 private int getContentAreaWidth() {
924 return Math.max(mContent.getDesiredWidth(), MIN_CONTENT_DIMEN);
Winson Chung892c74d2013-08-22 16:15:50 -0700925 }
926
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700927 private int getFolderHeight() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800928 return getFolderHeight(getContentAreaHeight());
929 }
930
931 private int getFolderHeight(int contentAreaHeight) {
Sunny Goyal290800b2015-03-05 11:33:33 -0800932 return getPaddingTop() + getPaddingBottom() + contentAreaHeight + mFooterHeight;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700933 }
Adam Cohen234c4cd2011-07-17 21:03:04 -0700934
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700935 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800936 int contentWidth = getContentAreaWidth();
937 int contentHeight = getContentAreaHeight();
Adam Cohen1960ea42013-11-12 11:33:14 +0000938
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800939 int contentAreaWidthSpec = MeasureSpec.makeMeasureSpec(contentWidth, MeasureSpec.EXACTLY);
940 int contentAreaHeightSpec = MeasureSpec.makeMeasureSpec(contentHeight, MeasureSpec.EXACTLY);
941
942 mContent.setFixedSize(contentWidth, contentHeight);
943 mContentWrapper.measure(contentAreaWidthSpec, contentAreaHeightSpec);
Sunny Goyal290800b2015-03-05 11:33:33 -0800944 mFooter.measure(contentAreaWidthSpec,
945 MeasureSpec.makeMeasureSpec(mFooterHeight, MeasureSpec.EXACTLY));
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800946
947 int folderWidth = getPaddingLeft() + getPaddingRight() + contentWidth;
948 int folderHeight = getFolderHeight(contentHeight);
949 setMeasuredDimension(folderWidth, folderHeight);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700950 }
951
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800952 /**
953 * Rearranges the children based on their rank.
954 */
955 public void rearrangeChildren() {
956 rearrangeChildren(-1);
957 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700958
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800959 /**
960 * Rearranges the children based on their rank.
961 * @param itemCount if greater than the total children count, empty spaces are left at the end,
962 * otherwise it is ignored.
963 */
964 public void rearrangeChildren(int itemCount) {
965 ArrayList<View> views = getItemsInReadingOrder();
966 mContent.arrangeChildren(views, Math.max(itemCount, views.size()));
Adam Cohen7c693212011-05-18 15:26:57 -0700967 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700968 }
969
Sunny Goyalc4918352015-03-10 18:15:48 -0700970 // TODO remove this once GSA code fix is submitted
971 public ViewGroup getContent() {
972 return (ViewGroup) mContent;
973 }
974
Adam Cohena9cf38f2011-05-02 15:36:58 -0700975 public int getItemCount() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800976 return mContent.getItemCount();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700977 }
978
Adam Cohen2801caf2011-05-13 20:57:39 -0700979 private void onCloseComplete() {
Adam Cohen05e0f402011-08-01 12:12:49 -0700980 DragLayer parent = (DragLayer) getParent();
Michael Jurka5649c282012-06-18 10:33:21 -0700981 if (parent != null) {
982 parent.removeView(this);
983 }
Adam Cohen4554ee12011-08-03 16:13:21 -0700984 mDragController.removeDropTarget((DropTarget) this);
Adam Cohen05e0f402011-08-01 12:12:49 -0700985 clearFocus();
Adam Cohenac56cff2011-09-28 20:45:37 -0700986 mFolderIcon.requestFocus();
Adam Cohen05e0f402011-08-01 12:12:49 -0700987
Adam Cohen2801caf2011-05-13 20:57:39 -0700988 if (mRearrangeOnClose) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800989 rearrangeChildren();
Adam Cohen2801caf2011-05-13 20:57:39 -0700990 mRearrangeOnClose = false;
991 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700992 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700993 if (!mDragInProgress && !mSuppressFolderDeletion) {
994 replaceFolderWithFinalItem();
995 } else if (mDragInProgress) {
996 mDeleteFolderOnDropCompleted = true;
997 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700998 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700999 mSuppressFolderDeletion = false;
Adam Cohenafb01ee2011-06-23 15:38:03 -07001000 }
1001
1002 private void replaceFolderWithFinalItem() {
Adam Cohenafb01ee2011-06-23 15:38:03 -07001003 // Add the last remaining child to the workspace in place of the folder
Adam Cohenfb91f302012-06-11 15:45:18 -07001004 Runnable onCompleteRunnable = new Runnable() {
1005 @Override
1006 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -07001007 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screenId);
Adam Cohenafb01ee2011-06-23 15:38:03 -07001008
Winson Chung33231f52013-12-09 16:57:45 -08001009 View child = null;
Adam Cohenfb91f302012-06-11 15:45:18 -07001010 // Move the item from the folder to the workspace, in the position of the folder
1011 if (getItemCount() == 1) {
1012 ShortcutInfo finalItem = mInfo.contents.get(0);
Adam Cohenc5e63f32012-07-12 16:16:57 -07001013 child = mLauncher.createShortcut(R.layout.application, cellLayout,
Adam Cohenfb91f302012-06-11 15:45:18 -07001014 finalItem);
1015 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -07001016 mInfo.screenId, mInfo.cellX, mInfo.cellY);
Adam Cohenfb91f302012-06-11 15:45:18 -07001017 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001018 if (getItemCount() <= 1) {
1019 // Remove the folder
1020 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
Dan Sandler0eb687f2014-01-10 13:24:55 -05001021 if (cellLayout != null) {
1022 // b/12446428 -- sometimes the cell layout has already gone away?
1023 cellLayout.removeView(mFolderIcon);
1024 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001025 if (mFolderIcon instanceof DropTarget) {
1026 mDragController.removeDropTarget((DropTarget) mFolderIcon);
1027 }
1028 mLauncher.removeFolder(mInfo);
1029 }
Adam Cohenc5e63f32012-07-12 16:16:57 -07001030 // We add the child after removing the folder to prevent both from existing at
Winson Chung0e6a7132013-08-23 12:55:10 -07001031 // the same time in the CellLayout. We need to add the new item with addInScreenFromBind()
1032 // to ensure that hotseat items are placed correctly.
Adam Cohenc5e63f32012-07-12 16:16:57 -07001033 if (child != null) {
Winson Chung0e6a7132013-08-23 12:55:10 -07001034 mLauncher.getWorkspace().addInScreenFromBind(child, mInfo.container, mInfo.screenId,
Adam Cohenc5e63f32012-07-12 16:16:57 -07001035 mInfo.cellX, mInfo.cellY, mInfo.spanX, mInfo.spanY);
1036 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001037 }
1038 };
Sunny Goyalbc753352015-03-05 09:40:44 -08001039 View finalChild = mContent.getLastItem();
Adam Cohenfb91f302012-06-11 15:45:18 -07001040 if (finalChild != null) {
1041 mFolderIcon.performDestroyAnimation(finalChild, onCompleteRunnable);
Winson Chung33231f52013-12-09 16:57:45 -08001042 } else {
1043 onCompleteRunnable.run();
Adam Cohenafb01ee2011-06-23 15:38:03 -07001044 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001045 mDestroyed = true;
1046 }
1047
1048 boolean isDestroyed() {
1049 return mDestroyed;
Adam Cohen2801caf2011-05-13 20:57:39 -07001050 }
1051
Adam Cohenac56cff2011-09-28 20:45:37 -07001052 // This method keeps track of the last item in the folder for the purposes
1053 // of keyboard focus
Sunny Goyal290800b2015-03-05 11:33:33 -08001054 public void updateTextViewFocus() {
Sunny Goyalbc753352015-03-05 09:40:44 -08001055 View lastChild = mContent.getLastItem();
Adam Cohenac56cff2011-09-28 20:45:37 -07001056 if (lastChild != null) {
1057 mFolderName.setNextFocusDownId(lastChild.getId());
1058 mFolderName.setNextFocusRightId(lastChild.getId());
1059 mFolderName.setNextFocusLeftId(lastChild.getId());
1060 mFolderName.setNextFocusUpId(lastChild.getId());
1061 }
1062 }
1063
Adam Cohenbfbfd262011-06-13 16:55:12 -07001064 public void onDrop(DragObject d) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001065 Runnable cleanUpRunnable = null;
1066
Adam Cohen689ff162014-05-08 17:27:56 -07001067 // If we are coming from All Apps space, we defer removing the extra empty screen
1068 // until the folder closes
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001069 if (d.dragSource != mLauncher.getWorkspace() && !(d.dragSource instanceof Folder)) {
1070 cleanUpRunnable = new Runnable() {
1071 @Override
1072 public void run() {
Adam Cohen689ff162014-05-08 17:27:56 -07001073 mLauncher.exitSpringLoadedDragModeDelayed(true,
1074 Launcher.EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
1075 null);
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001076 }
1077 };
Adam Cohenbfbfd262011-06-13 16:55:12 -07001078 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001079
Sunny Goyal48461932015-03-09 17:41:09 -07001080 if (ALLOW_FOLDER_SCROLL) {
1081 // If the icon was dropped while the page was being scrolled, we need to compute
1082 // the target location again such that the icon is placed of the final page.
1083 if (!mPagedView.rankOnCurrentPage(mEmptyCellRank)) {
1084 // Reorder again.
1085 mTargetRank = getTargetRank(d, null);
1086
1087 // Rearrange items immediately.
1088 mReorderAlarmListener.onAlarm(mReorderAlarm);
1089
1090 mOnScrollHintAlarm.cancelAlarm();
1091 mScrollPauseAlarm.cancelAlarm();
1092 }
1093 mPagedView.completePendingPageChanges();
1094 }
1095
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001096 View currentDragView;
1097 ShortcutInfo si = mCurrentDragInfo;
1098 if (mIsExternalDrag) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001099 currentDragView = mContent.createAndAddViewForRank(si, mEmptyCellRank);
Sunny Goyal95abbb32014-08-04 10:53:22 -07001100 // Actually move the item in the database if it was an external drag. Call this
1101 // before creating the view, so that ShortcutInfo is updated appropriately.
1102 LauncherModel.addOrMoveItemInDatabase(
1103 mLauncher, si, mInfo.id, 0, si.cellX, si.cellY);
1104
1105 // We only need to update the locations if it doesn't get handled in #onDropCompleted.
1106 if (d.dragSource != this) {
1107 updateItemLocationsInDatabaseBatch();
1108 }
1109 mIsExternalDrag = false;
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001110 } else {
1111 currentDragView = mCurrentDragView;
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001112 mContent.addViewForRank(currentDragView, si, mEmptyCellRank);
Adam Cohenbfbfd262011-06-13 16:55:12 -07001113 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001114
1115 if (d.dragView.hasDrawn()) {
1116
1117 // Temporarily reset the scale such that the animation target gets calculated correctly.
1118 float scaleX = getScaleX();
1119 float scaleY = getScaleY();
1120 setScaleX(1.0f);
1121 setScaleY(1.0f);
1122 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, currentDragView,
1123 cleanUpRunnable, null);
1124 setScaleX(scaleX);
1125 setScaleY(scaleY);
1126 } else {
1127 d.deferDragViewCleanupPostAnimation = false;
1128 currentDragView.setVisibility(VISIBLE);
1129 }
1130 mItemsInvalidated = true;
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001131 rearrangeChildren();
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001132
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001133 // Temporarily suppress the listener, as we did all the work already here.
1134 mSuppressOnAdd = true;
1135 mInfo.add(si);
1136 mSuppressOnAdd = false;
Sunny Goyal4b020172014-08-28 14:51:14 -07001137 // Clear the drag info, as it is no longer being dragged.
1138 mCurrentDragInfo = null;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001139 }
1140
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001141 // This is used so the item doesn't immediately appear in the folder when added. In one case
1142 // we need to create the illusion that the item isn't added back to the folder yet, to
1143 // to correspond to the animation of the icon back into the folder. This is
1144 public void hideItem(ShortcutInfo info) {
1145 View v = getViewForInfo(info);
1146 v.setVisibility(INVISIBLE);
1147 }
1148 public void showItem(ShortcutInfo info) {
1149 View v = getViewForInfo(info);
1150 v.setVisibility(VISIBLE);
1151 }
1152
Adam Cohenbfbfd262011-06-13 16:55:12 -07001153 public void onAdd(ShortcutInfo item) {
1154 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001155 // If the item was dropped onto this open folder, we have done the work associated
1156 // with adding the item to the folder, as indicated by mSuppressOnAdd being set
Adam Cohenbfbfd262011-06-13 16:55:12 -07001157 if (mSuppressOnAdd) return;
Sunny Goyalbc753352015-03-05 09:40:44 -08001158 mContent.createAndAddViewForRank(item, mContent.allocateNewLastItemRank());
Adam Cohenbfbfd262011-06-13 16:55:12 -07001159 LauncherModel.addOrMoveItemInDatabase(
1160 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
1161 }
1162
Adam Cohena9cf38f2011-05-02 15:36:58 -07001163 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -07001164 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001165 // If this item is being dragged from this open folder, we have already handled
1166 // the work associated with removing the item, so we don't have to do anything here.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001167 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001168 View v = getViewForInfo(item);
Sunny Goyal290800b2015-03-05 11:33:33 -08001169 mContent.removeItem(v);
Adam Cohen2801caf2011-05-13 20:57:39 -07001170 if (mState == STATE_ANIMATING) {
1171 mRearrangeOnClose = true;
1172 } else {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001173 rearrangeChildren();
Adam Cohen2801caf2011-05-13 20:57:39 -07001174 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001175 if (getItemCount() <= 1) {
1176 replaceFolderWithFinalItem();
1177 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001178 }
Adam Cohen7c693212011-05-18 15:26:57 -07001179
Sunny Goyalbc753352015-03-05 09:40:44 -08001180 private View getViewForInfo(final ShortcutInfo item) {
1181 return mContent.iterateOverItems(new ItemOperator() {
1182
1183 @Override
1184 public boolean evaluate(ItemInfo info, View view, View parent) {
1185 return info == item;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001186 }
Sunny Goyalbc753352015-03-05 09:40:44 -08001187 });
Adam Cohendf1e4e82011-06-24 15:57:39 -07001188 }
1189
Adam Cohen76078c42011-06-09 15:06:52 -07001190 public void onItemsChanged() {
Adam Cohenac56cff2011-09-28 20:45:37 -07001191 updateTextViewFocus();
Adam Cohen76078c42011-06-09 15:06:52 -07001192 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001193
Adam Cohen76fc0852011-06-17 13:26:23 -07001194 public void onTitleChanged(CharSequence title) {
1195 }
Adam Cohen76078c42011-06-09 15:06:52 -07001196
Adam Cohen7c693212011-05-18 15:26:57 -07001197 public ArrayList<View> getItemsInReadingOrder() {
1198 if (mItemsInvalidated) {
1199 mItemsInReadingOrder.clear();
Sunny Goyalbc753352015-03-05 09:40:44 -08001200 mContent.iterateOverItems(new ItemOperator() {
1201
1202 @Override
1203 public boolean evaluate(ItemInfo info, View view, View parent) {
1204 mItemsInReadingOrder.add(view);
1205 return false;
Adam Cohen7c693212011-05-18 15:26:57 -07001206 }
Sunny Goyalbc753352015-03-05 09:40:44 -08001207 });
Adam Cohen7c693212011-05-18 15:26:57 -07001208 mItemsInvalidated = false;
1209 }
1210 return mItemsInReadingOrder;
1211 }
Adam Cohen8dfcba42011-07-07 16:38:18 -07001212
1213 public void getLocationInDragLayer(int[] loc) {
1214 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
1215 }
Adam Cohenea0818d2011-09-30 20:06:58 -07001216
1217 public void onFocusChange(View v, boolean hasFocus) {
1218 if (v == mFolderName && hasFocus) {
1219 startEditingFolderName();
1220 }
1221 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001222
1223 @Override
1224 public void getHitRectRelativeToDragLayer(Rect outRect) {
1225 getHitRect(outRect);
Sunny Goyal48461932015-03-09 17:41:09 -07001226 outRect.left -= mScrollAreaOffset;
1227 outRect.right += mScrollAreaOffset;
1228 }
1229
1230 private class OnScrollHintListener implements OnAlarmListener {
1231
1232 private final DragObject mDragObject;
1233
1234 OnScrollHintListener(DragObject object) {
1235 mDragObject = object;
1236 }
1237
1238 /**
1239 * Scroll hint has been shown long enough. Now scroll to appropriate page.
1240 */
1241 @Override
1242 public void onAlarm(Alarm alarm) {
1243 if (mCurrentScrollDir == DragController.SCROLL_LEFT) {
1244 mPagedView.scrollLeft();
1245 mScrollHintDir = DragController.SCROLL_NONE;
1246 } else if (mCurrentScrollDir == DragController.SCROLL_RIGHT) {
1247 mPagedView.scrollRight();
1248 mScrollHintDir = DragController.SCROLL_NONE;
1249 } else {
1250 // This should not happen
1251 return;
1252 }
1253 mCurrentScrollDir = DragController.SCROLL_NONE;
1254
1255 // Pause drag event until the scrolling is finished
1256 mScrollPauseAlarm.setOnAlarmListener(new OnScrollFinishedListener(mDragObject));
1257 mScrollPauseAlarm.setAlarm(DragController.RESCROLL_DELAY);
1258 }
1259 }
1260
1261 private class OnScrollFinishedListener implements OnAlarmListener {
1262
1263 private final DragObject mDragObject;
1264
1265 OnScrollFinishedListener(DragObject object) {
1266 mDragObject = object;
1267 }
1268
1269 /**
1270 * Page scroll is complete.
1271 */
1272 @Override
1273 public void onAlarm(Alarm alarm) {
1274 // Reorder immediately on page change.
1275 onDragOver(mDragObject, 1);
1276 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001277 }
Sunny Goyalbc753352015-03-05 09:40:44 -08001278
1279 public static interface FolderContent {
1280 void setFolder(Folder f);
1281
Sunny Goyal290800b2015-03-05 11:33:33 -08001282 void removeItem(View v);
Sunny Goyalbc753352015-03-05 09:40:44 -08001283
1284 boolean isFull();
1285 int getItemCount();
1286
1287 int getDesiredWidth();
1288 int getDesiredHeight();
1289 void setFixedSize(int width, int height);
1290
1291 /**
1292 * Iterates over all its items in a reading order.
1293 * @return the view for which the operator returned true.
1294 */
1295 View iterateOverItems(ItemOperator op);
1296 View getLastItem();
1297
1298 String getAccessibilityDescription();
1299
1300 /**
1301 * Binds items to the layout.
1302 * @return list of items that could not be bound, probably because we hit the max size limit.
1303 */
1304 ArrayList<ShortcutInfo> bindItems(ArrayList<ShortcutInfo> children);
1305
1306 /**
1307 * Create space for a new item at the end, and returns the rank for that item.
1308 * Resizes the content if necessary.
1309 */
1310 int allocateNewLastItemRank();
1311
1312 View createAndAddViewForRank(ShortcutInfo item, int rank);
1313
1314 /**
1315 * Adds the {@param view} to the layout based on {@param rank} and updated the position
1316 * related attributes. It assumes that {@param item} is already attached to the view.
1317 */
1318 void addViewForRank(View view, ShortcutInfo item, int rank);
1319
1320 /**
1321 * Reorders the items such that the {@param empty} spot moves to {@param target}
1322 */
1323 void realTimeReorder(int empty, int target);
1324
1325 /**
1326 * @return the rank of the cell nearest to the provided pixel position.
1327 */
1328 int findNearestArea(int pixelX, int pixelY);
1329
1330 /**
1331 * Updates position and rank of all the children in the view based.
1332 * @param list the ordered list of children.
1333 * @param itemCount if greater than the total children count, empty spaces are left
1334 * at the end.
1335 */
1336 void arrangeChildren(ArrayList<View> list, int itemCount);
1337
1338 /**
1339 * Sets the focus on the first visible child.
1340 */
1341 void setFocusOnFirstChild();
1342 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001343}