blob: 4529a945943f4f419e18e6077780962bcdfa7713 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Adam Cohendf2cc412011-04-27 16:56:57 -070019import android.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
Adam Cohenc4fe9ea2014-08-18 18:54:10 -070021import android.animation.AnimatorSet;
Adam Cohendf2cc412011-04-27 16:56:57 -070022import android.animation.ObjectAnimator;
23import android.animation.PropertyValuesHolder;
Sunny Goyalc46bfef2015-01-05 12:40:08 -080024import android.annotation.TargetApi;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080025import android.content.Context;
Adam Cohen76fc0852011-06-17 13:26:23 -070026import android.content.res.Resources;
Winson Chungb745afb2015-03-02 11:51:23 -080027import android.graphics.Point;
Winson Chung043f2af2012-03-01 16:09:54 -080028import android.graphics.PointF;
Romain Guyfb5411e2010-02-24 10:04:17 -080029import android.graphics.Rect;
Sunny Goyalc46bfef2015-01-05 12:40:08 -080030import android.os.Build;
Adam Cohen7a14d0b2011-06-24 11:36:35 -070031import android.text.InputType;
Adam Cohene601a432011-07-26 21:51:30 -070032import android.text.Selection;
33import android.text.Spannable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.util.AttributeSet;
Adam Cohen3bf84d32012-05-07 20:17:14 -070035import android.util.Log;
Adam Cohen76fc0852011-06-17 13:26:23 -070036import android.view.ActionMode;
37import android.view.KeyEvent;
Adam Cohendf2cc412011-04-27 16:56:57 -070038import android.view.LayoutInflater;
Adam Cohen76fc0852011-06-17 13:26:23 -070039import android.view.Menu;
40import android.view.MenuItem;
Adam Cohen0c872ba2011-05-05 10:34:16 -070041import android.view.MotionEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.view.View;
Sunny Goyalc4918352015-03-10 18:15:48 -070043import android.view.ViewGroup;
Adam Cohen3371da02011-10-25 21:38:29 -070044import android.view.accessibility.AccessibilityEvent;
45import android.view.accessibility.AccessibilityManager;
Adam Cohenc4fe9ea2014-08-18 18:54:10 -070046import android.view.animation.AccelerateInterpolator;
Adam Cohen76fc0852011-06-17 13:26:23 -070047import android.view.inputmethod.EditorInfo;
48import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.widget.LinearLayout;
Adam Cohendf2cc412011-04-27 16:56:57 -070050import android.widget.TextView;
Sunny Goyal48461932015-03-09 17:41:09 -070051
Daniel Sandler325dc232013-06-05 22:57:57 -040052import com.android.launcher3.FolderInfo.FolderListener;
Sunny Goyalbc753352015-03-05 09:40:44 -080053import com.android.launcher3.Workspace.ItemOperator;
Adam Cohen091440a2015-03-18 14:16:05 -070054import com.android.launcher3.util.Thunk;
Romain Guyedcce092010-03-04 13:03:17 -080055
Adam Cohenc0dcf592011-06-01 15:30:43 -070056import java.util.ArrayList;
Adam Cohen3bf84d32012-05-07 20:17:14 -070057import java.util.Collections;
Adam Cohenc0dcf592011-06-01 15:30:43 -070058
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059/**
60 * Represents a set of icons chosen by the user or generated by the system.
61 */
Adam Cohen8dfcba42011-07-07 16:38:18 -070062public class Folder extends LinearLayout implements DragSource, View.OnClickListener,
Adam Cohenea0818d2011-09-30 20:06:58 -070063 View.OnLongClickListener, DropTarget, FolderListener, TextView.OnEditorActionListener,
64 View.OnFocusChangeListener {
Adam Cohendf2cc412011-04-27 16:56:57 -070065 private static final String TAG = "Launcher.Folder";
66
Sunny Goyalc3a609f2015-02-26 17:43:50 -080067 /**
68 * We avoid measuring {@link #mContentWrapper} with a 0 width or height, as this
69 * results in CellLayout being measured as UNSPECIFIED, which it does not support.
70 */
71 private static final int MIN_CONTENT_DIMEN = 5;
Sunny Goyal290800b2015-03-05 11:33:33 -080072 private static final boolean ALLOW_FOLDER_SCROLL = true;
Adam Cohen4eac29a2011-07-11 17:53:37 -070073
Adam Cohendf2cc412011-04-27 16:56:57 -070074 static final int STATE_NONE = -1;
75 static final int STATE_SMALL = 0;
76 static final int STATE_ANIMATING = 1;
77 static final int STATE_OPEN = 2;
78
Sunny Goyal48461932015-03-09 17:41:09 -070079 /**
80 * Fraction of the width to scroll when showing the next page hint.
81 */
82 private static final float SCROLL_HINT_FRACTION = 0.07f;
83
84 /**
85 * Time for which the scroll hint is shown before automatically changing page.
86 */
87 public static final int SCROLL_HINT_DURATION = DragController.SCROLL_DELAY;
88
89 /**
Sunny Goyal5d85c442015-03-10 13:14:47 -070090 * Time in milliseconds for which an icon sticks to the target position
91 * in case of a sorted folder.
92 */
93 private static final int SORTED_STICKY_REORDER_DELAY = 1500;
94
95 /**
Sunny Goyal48461932015-03-09 17:41:09 -070096 * Fraction of icon width which behave as scroll region.
97 */
98 private static final float ICON_OVERSCROLL_WIDTH_FACTOR = 0.45f;
99
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700100 private static final int REORDER_DELAY = 250;
Adam Cohen5d518fa2013-12-05 14:16:23 -0800101 private static final int ON_EXIT_CLOSE_DELAY = 400;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800102 private static final Rect sTempRect = new Rect();
103
104 private static String sDefaultFolderName;
105 private static String sHintText;
106
107 private final Alarm mReorderAlarm = new Alarm();
108 private final Alarm mOnExitAlarm = new Alarm();
109
Adam Cohen091440a2015-03-18 14:16:05 -0700110 @Thunk final ArrayList<View> mItemsInReadingOrder = new ArrayList<View>();
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800111
112 private final int mExpandDuration;
113 private final int mMaterialExpandDuration;
114 private final int mMaterialExpandStagger;
115
116 private final InputMethodManager mInputMethodManager;
117
118 protected final Launcher mLauncher;
119 protected DragController mDragController;
120 protected FolderInfo mInfo;
121
Adam Cohen091440a2015-03-18 14:16:05 -0700122 @Thunk FolderIcon mFolderIcon;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800123
Adam Cohen091440a2015-03-18 14:16:05 -0700124 @Thunk FolderContent mContent;
125 @Thunk View mContentWrapper;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800126 FolderEditText mFolderName;
127
Sunny Goyal290800b2015-03-05 11:33:33 -0800128 private View mFooter;
129 private int mFooterHeight;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800130
131 // Cell ranks used for drag and drop
Adam Cohen091440a2015-03-18 14:16:05 -0700132 @Thunk int mTargetRank, mPrevTargetRank, mEmptyCellRank;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800133
Adam Cohen091440a2015-03-18 14:16:05 -0700134 @Thunk int mState = STATE_NONE;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800135 private boolean mRearrangeOnClose = false;
Adam Cohen7c693212011-05-18 15:26:57 -0700136 boolean mItemsInvalidated = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700137 private ShortcutInfo mCurrentDragInfo;
138 private View mCurrentDragView;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800139 private boolean mIsExternalDrag;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700140 boolean mSuppressOnAdd = false;
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700141 private boolean mDragInProgress = false;
142 private boolean mDeleteFolderOnDropCompleted = false;
143 private boolean mSuppressFolderDeletion = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700144 private boolean mItemAddedBackToSelfViaIcon = false;
Adam Cohen091440a2015-03-18 14:16:05 -0700145 @Thunk float mFolderIconPivotX;
146 @Thunk float mFolderIconPivotY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700147 private boolean mIsEditingName = false;
Adam Cohen1960ea42013-11-12 11:33:14 +0000148
Adam Cohenfb91f302012-06-11 15:45:18 -0700149 private boolean mDestroyed;
150
Adam Cohen091440a2015-03-18 14:16:05 -0700151 @Thunk Runnable mDeferredAction;
Michael Jurka1e2f4652013-07-08 18:03:46 -0700152 private boolean mDeferDropAfterUninstall;
153 private boolean mUninstallSuccessful;
154
Sunny Goyal48461932015-03-09 17:41:09 -0700155 // Folder scrolling
156 private int mScrollAreaOffset;
157 private Alarm mOnScrollHintAlarm;
Adam Cohen091440a2015-03-18 14:16:05 -0700158 @Thunk Alarm mScrollPauseAlarm;
Sunny Goyal48461932015-03-09 17:41:09 -0700159
160 // TODO: Use {@link #mContent} once {@link #ALLOW_FOLDER_SCROLL} is removed.
Adam Cohen091440a2015-03-18 14:16:05 -0700161 @Thunk FolderPagedView mPagedView;
Sunny Goyal48461932015-03-09 17:41:09 -0700162
Adam Cohen091440a2015-03-18 14:16:05 -0700163 @Thunk int mScrollHintDir = DragController.SCROLL_NONE;
164 @Thunk int mCurrentScrollDir = DragController.SCROLL_NONE;
Sunny Goyal48461932015-03-09 17:41:09 -0700165
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166 /**
167 * Used to inflate the Workspace from XML.
168 *
169 * @param context The application's context.
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800170 * @param attrs The attributes set containing the Workspace's customization values.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171 */
172 public Folder(Context context, AttributeSet attrs) {
173 super(context, attrs);
174 setAlwaysDrawnWithCacheEnabled(false);
Adam Cohen76fc0852011-06-17 13:26:23 -0700175 mInputMethodManager = (InputMethodManager)
Michael Jurka8b805b12012-04-18 14:23:14 -0700176 getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
Adam Cohen76fc0852011-06-17 13:26:23 -0700177
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800178 Resources res = getResources();
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700179 mExpandDuration = res.getInteger(R.integer.config_folderExpandDuration);
180 mMaterialExpandDuration = res.getInteger(R.integer.config_materialFolderExpandDuration);
181 mMaterialExpandStagger = res.getInteger(R.integer.config_materialFolderExpandStagger);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700182
183 if (sDefaultFolderName == null) {
184 sDefaultFolderName = res.getString(R.string.folder_name);
185 }
Adam Cohena65beee2011-06-27 21:32:23 -0700186 if (sHintText == null) {
187 sHintText = res.getString(R.string.folder_hint_text);
188 }
Adam Cohen4eac29a2011-07-11 17:53:37 -0700189 mLauncher = (Launcher) context;
Adam Cohenac56cff2011-09-28 20:45:37 -0700190 // We need this view to be focusable in touch mode so that when text editing of the folder
191 // name is complete, we have something to focus on, thus hiding the cursor and giving
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800192 // reliable behavior when clicking the text field (since it will always gain focus on click).
Adam Cohenac56cff2011-09-28 20:45:37 -0700193 setFocusableInTouchMode(true);
Sunny Goyal48461932015-03-09 17:41:09 -0700194
195 if (ALLOW_FOLDER_SCROLL) {
196 mOnScrollHintAlarm = new Alarm();
197 mScrollPauseAlarm = new Alarm();
198 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800199 }
200
201 @Override
202 protected void onFinishInflate() {
203 super.onFinishInflate();
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800204 mContentWrapper = findViewById(R.id.folder_content_wrapper);
Sunny Goyalbc753352015-03-05 09:40:44 -0800205 mContent = (FolderContent) findViewById(R.id.folder_content);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800206 mContent.setFolder(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700207
Adam Cohenac56cff2011-09-28 20:45:37 -0700208 mFolderName = (FolderEditText) findViewById(R.id.folder_name);
209 mFolderName.setFolder(this);
Adam Cohenea0818d2011-09-30 20:06:58 -0700210 mFolderName.setOnFocusChangeListener(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700211
Adam Cohen76fc0852011-06-17 13:26:23 -0700212 // We disable action mode for now since it messes up the view on phones
213 mFolderName.setCustomSelectionActionModeCallback(mActionModeCallback);
Adam Cohen76fc0852011-06-17 13:26:23 -0700214 mFolderName.setOnEditorActionListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700215 mFolderName.setSelectAllOnFocus(true);
Adam Cohen7a14d0b2011-06-24 11:36:35 -0700216 mFolderName.setInputType(mFolderName.getInputType() |
217 InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800218
Sunny Goyal290800b2015-03-05 11:33:33 -0800219 mFooter = ALLOW_FOLDER_SCROLL ? findViewById(R.id.folder_footer) : mFolderName;
220 // We find out how tall footer wants to be (it is set to wrap_content), so that
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800221 // we can allocate the appropriate amount of space for it.
222 int measureSpec = MeasureSpec.UNSPECIFIED;
Sunny Goyal290800b2015-03-05 11:33:33 -0800223 mFooter.measure(measureSpec, measureSpec);
224 mFooterHeight = mFooter.getMeasuredHeight();
Sunny Goyal48461932015-03-09 17:41:09 -0700225
226 if (ALLOW_FOLDER_SCROLL) {
227 mPagedView = (FolderPagedView) mContent;
228 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800229 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700230
Adam Cohen76fc0852011-06-17 13:26:23 -0700231 private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
232 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
233 return false;
234 }
235
236 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
237 return false;
238 }
239
240 public void onDestroyActionMode(ActionMode mode) {
241 }
242
243 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
244 return false;
245 }
246 };
247
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800248 public void onClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700249 Object tag = v.getTag();
250 if (tag instanceof ShortcutInfo) {
Adam Cohenb5fe60c2013-06-06 22:03:51 -0700251 mLauncher.onClick(v);
Adam Cohendf2cc412011-04-27 16:56:57 -0700252 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800253 }
254
255 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -0700256 // Return if global dragging is not enabled
257 if (!mLauncher.isDraggingEnabled()) return true;
258
Adam Cohendf2cc412011-04-27 16:56:57 -0700259 Object tag = v.getTag();
260 if (tag instanceof ShortcutInfo) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700261 ShortcutInfo item = (ShortcutInfo) tag;
262 if (!v.isInTouchMode()) {
263 return false;
264 }
265
Winson Chungb745afb2015-03-02 11:51:23 -0800266 mLauncher.getWorkspace().beginDragShared(v, new Point(), this, false);
Adam Cohen76078c42011-06-09 15:06:52 -0700267
268 mCurrentDragInfo = item;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800269 mEmptyCellRank = item.rank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700270 mCurrentDragView = v;
Adam Cohenfc53cd22011-07-20 15:45:11 -0700271
Sunny Goyal290800b2015-03-05 11:33:33 -0800272 mContent.removeItem(mCurrentDragView);
Adam Cohenfc53cd22011-07-20 15:45:11 -0700273 mInfo.remove(mCurrentDragInfo);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700274 mDragInProgress = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700275 mItemAddedBackToSelfViaIcon = false;
Adam Cohendf2cc412011-04-27 16:56:57 -0700276 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800277 return true;
278 }
279
Adam Cohen76fc0852011-06-17 13:26:23 -0700280 public boolean isEditingName() {
281 return mIsEditingName;
282 }
283
284 public void startEditingFolderName() {
Adam Cohena65beee2011-06-27 21:32:23 -0700285 mFolderName.setHint("");
Adam Cohen76fc0852011-06-17 13:26:23 -0700286 mIsEditingName = true;
287 }
288
289 public void dismissEditingName() {
290 mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
291 doneEditingFolderName(true);
292 }
293
294 public void doneEditingFolderName(boolean commit) {
Adam Cohena65beee2011-06-27 21:32:23 -0700295 mFolderName.setHint(sHintText);
Adam Cohen1df26a32011-08-12 16:15:23 -0700296 // Convert to a string here to ensure that no other state associated with the text field
297 // gets saved.
Adam Cohen3371da02011-10-25 21:38:29 -0700298 String newTitle = mFolderName.getText().toString();
299 mInfo.setTitle(newTitle);
Adam Cohen76fc0852011-06-17 13:26:23 -0700300 LauncherModel.updateItemInDatabase(mLauncher, mInfo);
Adam Cohenac56cff2011-09-28 20:45:37 -0700301
Adam Cohen3371da02011-10-25 21:38:29 -0700302 if (commit) {
303 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700304 String.format(getContext().getString(R.string.folder_renamed), newTitle));
Adam Cohen3371da02011-10-25 21:38:29 -0700305 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700306 // In order to clear the focus from the text field, we set the focus on ourself. This
307 // ensures that every time the field is clicked, focus is gained, giving reliable behavior.
308 requestFocus();
309
Adam Cohene601a432011-07-26 21:51:30 -0700310 Selection.setSelection((Spannable) mFolderName.getText(), 0, 0);
Adam Cohen76fc0852011-06-17 13:26:23 -0700311 mIsEditingName = false;
312 }
313
314 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
315 if (actionId == EditorInfo.IME_ACTION_DONE) {
316 dismissEditingName();
317 return true;
318 }
319 return false;
320 }
321
322 public View getEditTextRegion() {
323 return mFolderName;
324 }
325
Adam Cohen0c872ba2011-05-05 10:34:16 -0700326 /**
327 * We need to handle touch events to prevent them from falling through to the workspace below.
328 */
329 @Override
330 public boolean onTouchEvent(MotionEvent ev) {
331 return true;
332 }
333
Joe Onorato00acb122009-08-04 16:04:30 -0400334 public void setDragController(DragController dragController) {
335 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800336 }
337
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800338 public void setFolderIcon(FolderIcon icon) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700339 mFolderIcon = icon;
340 }
341
Adam Cohen3371da02011-10-25 21:38:29 -0700342 @Override
343 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
344 // When the folder gets focus, we don't want to announce the list of items.
345 return true;
346 }
347
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800348 /**
349 * @return the FolderInfo object associated with this folder
350 */
351 FolderInfo getInfo() {
352 return mInfo;
353 }
354
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800355 void bind(FolderInfo info) {
356 mInfo = info;
Adam Cohendf2cc412011-04-27 16:56:57 -0700357 ArrayList<ShortcutInfo> children = info.contents;
Sunny Goyal08f72612015-01-05 13:41:43 -0800358 Collections.sort(children, Utilities.RANK_COMPARATOR);
Sunny Goyal08f72612015-01-05 13:41:43 -0800359
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800360 ArrayList<ShortcutInfo> overflow = mContent.bindItems(children);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700361
Jason Monk4ff73882014-04-24 16:48:07 -0400362 // If our folder has too many items we prune them from the list. This is an issue
Adam Cohenc508b2d2011-06-28 14:41:44 -0700363 // when upgrading from the old Folders implementation which could contain an unlimited
364 // number of items.
365 for (ShortcutInfo item: overflow) {
366 mInfo.remove(item);
367 LauncherModel.deleteItemFromDatabase(mLauncher, item);
368 }
369
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800370 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
371 if (lp == null) {
372 lp = new DragLayer.LayoutParams(0, 0);
373 lp.customPosition = true;
374 setLayoutParams(lp);
375 }
376 centerAboutIcon();
377
Adam Cohen4dbe6d92011-05-18 17:14:15 -0700378 mItemsInvalidated = true;
Adam Cohenac56cff2011-09-28 20:45:37 -0700379 updateTextViewFocus();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700380 mInfo.addListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700381
Adam Cohenafb01ee2011-06-23 15:38:03 -0700382 if (!sDefaultFolderName.contentEquals(mInfo.title)) {
Adam Cohen4ef610f2011-06-21 22:37:36 -0700383 mFolderName.setText(mInfo.title);
384 } else {
385 mFolderName.setText("");
386 }
Winson Chung33231f52013-12-09 16:57:45 -0800387
388 // In case any children didn't come across during loading, clean up the folder accordingly
389 mFolderIcon.post(new Runnable() {
390 public void run() {
391 if (getItemCount() <= 1) {
392 replaceFolderWithFinalItem();
393 }
394 }
395 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700396 }
397
398 /**
399 * Creates a new UserFolder, inflated from R.layout.user_folder.
400 *
401 * @param context The application's context.
402 *
403 * @return A new UserFolder.
404 */
405 static Folder fromXml(Context context) {
Sunny Goyal290800b2015-03-05 11:33:33 -0800406 return (Folder) LayoutInflater.from(context).inflate(
407 ALLOW_FOLDER_SCROLL ? R.layout.user_folder_scroll : R.layout.user_folder, null);
Adam Cohendf2cc412011-04-27 16:56:57 -0700408 }
409
410 /**
411 * This method is intended to make the UserFolder to be visually identical in size and position
412 * to its associated FolderIcon. This allows for a seamless transition into the expanded state.
413 */
414 private void positionAndSizeAsIcon() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700415 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800416 setScaleX(0.8f);
417 setScaleY(0.8f);
418 setAlpha(0f);
Adam Cohendf2cc412011-04-27 16:56:57 -0700419 mState = STATE_SMALL;
420 }
421
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700422 private void prepareReveal() {
423 setScaleX(1f);
424 setScaleY(1f);
425 setAlpha(1f);
426 mState = STATE_SMALL;
427 }
428
Adam Cohendf2cc412011-04-27 16:56:57 -0700429 public void animateOpen() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700430 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen6441de02011-12-14 14:25:32 -0800431
Sunny Goyal48461932015-03-09 17:41:09 -0700432 if (ALLOW_FOLDER_SCROLL) {
Sunny Goyal5d85c442015-03-10 13:14:47 -0700433 mPagedView.completePendingPageChanges();
434 if (!(mDragInProgress && mPagedView.mIsSorted)) {
435 // Open on the first page.
436 mPagedView.snapToPageImmediately(0);
437 }
Sunny Goyal48461932015-03-09 17:41:09 -0700438 }
439
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700440 Animator openFolderAnim = null;
441 final Runnable onCompleteRunnable;
Kenny Guyd794a3f2014-09-16 15:17:58 +0100442 if (!Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700443 positionAndSizeAsIcon();
444 centerAboutIcon();
445
446 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1);
447 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
448 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
449 final ObjectAnimator oa =
450 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
451 oa.setDuration(mExpandDuration);
452 openFolderAnim = oa;
453
454 setLayerType(LAYER_TYPE_HARDWARE, null);
455 onCompleteRunnable = new Runnable() {
456 @Override
457 public void run() {
458 setLayerType(LAYER_TYPE_NONE, null);
459 }
460 };
461 } else {
462 prepareReveal();
463 centerAboutIcon();
464
465 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
466 int height = getFolderHeight();
467
468 float transX = - 0.075f * (width / 2 - getPivotX());
469 float transY = - 0.075f * (height / 2 - getPivotY());
470 setTranslationX(transX);
471 setTranslationY(transY);
472 PropertyValuesHolder tx = PropertyValuesHolder.ofFloat("translationX", transX, 0);
473 PropertyValuesHolder ty = PropertyValuesHolder.ofFloat("translationY", transY, 0);
474
475 int rx = (int) Math.max(Math.max(width - getPivotX(), 0), getPivotX());
476 int ry = (int) Math.max(Math.max(height - getPivotY(), 0), getPivotY());
477 float radius = (float) Math.sqrt(rx * rx + ry * ry);
478 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
479 Animator reveal = LauncherAnimUtils.createCircularReveal(this, (int) getPivotX(),
480 (int) getPivotY(), 0, radius);
481 reveal.setDuration(mMaterialExpandDuration);
482 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
483
Sunny Goyalbc753352015-03-05 09:40:44 -0800484 mContentWrapper.setAlpha(0f);
485 Animator iconsAlpha = LauncherAnimUtils.ofFloat(mContentWrapper, "alpha", 0f, 1f);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700486 iconsAlpha.setDuration(mMaterialExpandDuration);
487 iconsAlpha.setStartDelay(mMaterialExpandStagger);
488 iconsAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
489
Sunny Goyal290800b2015-03-05 11:33:33 -0800490 mFooter.setAlpha(0f);
491 Animator textAlpha = LauncherAnimUtils.ofFloat(mFooter, "alpha", 0f, 1f);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700492 textAlpha.setDuration(mMaterialExpandDuration);
493 textAlpha.setStartDelay(mMaterialExpandStagger);
494 textAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
495
496 Animator drift = LauncherAnimUtils.ofPropertyValuesHolder(this, tx, ty);
497 drift.setDuration(mMaterialExpandDuration);
498 drift.setStartDelay(mMaterialExpandStagger);
499 drift.setInterpolator(new LogDecelerateInterpolator(60, 0));
500
501 anim.play(drift);
502 anim.play(iconsAlpha);
503 anim.play(textAlpha);
504 anim.play(reveal);
505
506 openFolderAnim = anim;
507
Sunny Goyalbc753352015-03-05 09:40:44 -0800508 mContentWrapper.setLayerType(LAYER_TYPE_HARDWARE, null);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700509 onCompleteRunnable = new Runnable() {
510 @Override
511 public void run() {
Sunny Goyalbc753352015-03-05 09:40:44 -0800512 mContentWrapper.setLayerType(LAYER_TYPE_NONE, null);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700513 }
514 };
515 }
516 openFolderAnim.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700517 @Override
518 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700519 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Sunny Goyalbc753352015-03-05 09:40:44 -0800520 mContent.getAccessibilityDescription());
Adam Cohendf2cc412011-04-27 16:56:57 -0700521 mState = STATE_ANIMATING;
522 }
523 @Override
524 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700525 mState = STATE_OPEN;
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700526
527 if (onCompleteRunnable != null) {
528 onCompleteRunnable.run();
529 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800530
Sunny Goyalbc753352015-03-05 09:40:44 -0800531 mContent.setFocusOnFirstChild();
Adam Cohendf2cc412011-04-27 16:56:57 -0700532 }
533 });
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700534 openFolderAnim.start();
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800535
536 // Make sure the folder picks up the last drag move even if the finger doesn't move.
537 if (mDragController.isDragging()) {
538 mDragController.forceTouchMove();
539 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700540
541 if (ALLOW_FOLDER_SCROLL) {
542 FolderPagedView pages = (FolderPagedView) mContent;
543 pages.verifyVisibleHighResIcons(pages.getNextPage());
544 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800545 }
546
547 public void beginExternalDrag(ShortcutInfo item) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800548 mCurrentDragInfo = item;
Sunny Goyal5d85c442015-03-10 13:14:47 -0700549 mEmptyCellRank = mContent.allocateRankForNewItem(item);
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800550 mIsExternalDrag = true;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800551 mDragInProgress = true;
Sunny Goyal5d85c442015-03-10 13:14:47 -0700552 if (ALLOW_FOLDER_SCROLL && mPagedView.mIsSorted) {
553 mScrollPauseAlarm.setOnAlarmListener(null);
554 mScrollPauseAlarm.cancelAlarm();
555 mScrollPauseAlarm.setAlarm(SORTED_STICKY_REORDER_DELAY);
556 }
557
Adam Cohendf2cc412011-04-27 16:56:57 -0700558 }
559
Adam Cohen091440a2015-03-18 14:16:05 -0700560 @Thunk void sendCustomAccessibilityEvent(int type, String text) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700561 AccessibilityManager accessibilityManager = (AccessibilityManager)
562 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
563 if (accessibilityManager.isEnabled()) {
Adam Cohen3371da02011-10-25 21:38:29 -0700564 AccessibilityEvent event = AccessibilityEvent.obtain(type);
565 onInitializeAccessibilityEvent(event);
566 event.getText().add(text);
Michael Jurka8b805b12012-04-18 14:23:14 -0700567 accessibilityManager.sendAccessibilityEvent(event);
Adam Cohen3371da02011-10-25 21:38:29 -0700568 }
569 }
570
Adam Cohendf2cc412011-04-27 16:56:57 -0700571 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700572 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800573 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
574 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
575 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
Michael Jurka032e6ba2013-04-22 15:08:42 +0200576 final ObjectAnimator oa =
Michael Jurka2ecf9952012-06-18 12:52:28 -0700577 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohendf2cc412011-04-27 16:56:57 -0700578
Adam Cohen2801caf2011-05-13 20:57:39 -0700579 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700580 @Override
581 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700582 onCloseComplete();
Michael Jurka0121c3e2012-05-31 08:36:04 -0700583 setLayerType(LAYER_TYPE_NONE, null);
Adam Cohen2801caf2011-05-13 20:57:39 -0700584 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700585 }
586 @Override
587 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700588 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700589 getContext().getString(R.string.folder_closed));
Adam Cohendf2cc412011-04-27 16:56:57 -0700590 mState = STATE_ANIMATING;
591 }
592 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700593 oa.setDuration(mExpandDuration);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700594 setLayerType(LAYER_TYPE_HARDWARE, null);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100595 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700596 }
597
Adam Cohencb3382b2011-05-24 14:07:08 -0700598 public boolean acceptDrop(DragObject d) {
599 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700600 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700601 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
602 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
603 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700604 }
605
Adam Cohencb3382b2011-05-24 14:07:08 -0700606 public void onDragEnter(DragObject d) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800607 mPrevTargetRank = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700608 mOnExitAlarm.cancelAlarm();
Sunny Goyal48461932015-03-09 17:41:09 -0700609 if (ALLOW_FOLDER_SCROLL) {
610 // Get the area offset such that the folder only closes if half the drag icon width
611 // is outside the folder area
612 mScrollAreaOffset = d.dragView.getDragRegionWidth() / 2 - d.xOffset;
613 }
Adam Cohenbfbfd262011-06-13 16:55:12 -0700614 }
615
616 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
617 public void onAlarm(Alarm alarm) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800618 mContent.realTimeReorder(mEmptyCellRank, mTargetRank);
619 mEmptyCellRank = mTargetRank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700620 }
621 };
622
Sunny Goyalc46bfef2015-01-05 12:40:08 -0800623 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohen2374abf2013-04-16 14:56:57 -0700624 public boolean isLayoutRtl() {
625 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
626 }
627
Sunny Goyal48461932015-03-09 17:41:09 -0700628 @Override
Adam Cohencb3382b2011-05-24 14:07:08 -0700629 public void onDragOver(DragObject d) {
Sunny Goyal48461932015-03-09 17:41:09 -0700630 onDragOver(d, REORDER_DELAY);
631 }
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700632
Sunny Goyal48461932015-03-09 17:41:09 -0700633 private int getTargetRank(DragObject d, float[] recycle) {
634 recycle = d.getVisualCenter(recycle);
635 return mContent.findNearestArea(
636 (int) recycle[0] - getPaddingLeft(), (int) recycle[1] - getPaddingTop());
637 }
638
Adam Cohen091440a2015-03-18 14:16:05 -0700639 @Thunk void onDragOver(DragObject d, int reorderDelay) {
Sunny Goyal48461932015-03-09 17:41:09 -0700640 if (ALLOW_FOLDER_SCROLL && mScrollPauseAlarm.alarmPending()) {
641 return;
642 }
643 final float[] r = new float[2];
644 mTargetRank = getTargetRank(d, r);
645
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800646 if (mTargetRank != mPrevTargetRank) {
Alan Viverette4cda5b72013-08-28 17:53:41 -0700647 mReorderAlarm.cancelAlarm();
Sunny Goyalc46bfef2015-01-05 12:40:08 -0800648 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
649 mReorderAlarm.setAlarm(REORDER_DELAY);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800650 mPrevTargetRank = mTargetRank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700651 }
Sunny Goyal48461932015-03-09 17:41:09 -0700652
653 if (!ALLOW_FOLDER_SCROLL) {
654 return;
655 }
656
657 float x = r[0];
658 int currentPage = mPagedView.getNextPage();
659 int cellWidth = mPagedView.getCurrentCellLayout().getCellWidth();
660 if (currentPage > 0 && x < cellWidth * ICON_OVERSCROLL_WIDTH_FACTOR) {
661 // Show scroll hint on the left
662 if (mScrollHintDir != DragController.SCROLL_LEFT) {
663 mPagedView.showScrollHint(-SCROLL_HINT_FRACTION);
664 mScrollHintDir = DragController.SCROLL_LEFT;
665 }
666
667 // Set alarm for when the hint is complete
668 if (!mOnScrollHintAlarm.alarmPending() || mCurrentScrollDir != DragController.SCROLL_LEFT) {
669 mCurrentScrollDir = DragController.SCROLL_LEFT;
670 mOnScrollHintAlarm.cancelAlarm();
671 mOnScrollHintAlarm.setOnAlarmListener(new OnScrollHintListener(d));
672 mOnScrollHintAlarm.setAlarm(SCROLL_HINT_DURATION);
673
674 mReorderAlarm.cancelAlarm();
675 mTargetRank = mEmptyCellRank;
676 }
677 } else if (currentPage < (mPagedView.getPageCount() - 1) &&
678 (x > (getWidth() - cellWidth * ICON_OVERSCROLL_WIDTH_FACTOR))) {
679 // Show scroll hint on the right
680 if (mScrollHintDir != DragController.SCROLL_RIGHT) {
681 mPagedView.showScrollHint(SCROLL_HINT_FRACTION);
682 mScrollHintDir = DragController.SCROLL_RIGHT;
683 }
684
685 // Set alarm for when the hint is complete
686 if (!mOnScrollHintAlarm.alarmPending() || mCurrentScrollDir != DragController.SCROLL_RIGHT) {
687 mCurrentScrollDir = DragController.SCROLL_RIGHT;
688 mOnScrollHintAlarm.cancelAlarm();
689 mOnScrollHintAlarm.setOnAlarmListener(new OnScrollHintListener(d));
690 mOnScrollHintAlarm.setAlarm(SCROLL_HINT_DURATION);
691
692 mReorderAlarm.cancelAlarm();
693 mTargetRank = mEmptyCellRank;
694 }
695 } else {
696 mOnScrollHintAlarm.cancelAlarm();
697 if (mScrollHintDir != DragController.SCROLL_NONE) {
698 mPagedView.clearScrollHint();
699 mScrollHintDir = DragController.SCROLL_NONE;
700 }
701 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700702 }
703
Adam Cohenbfbfd262011-06-13 16:55:12 -0700704 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
705 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700706 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700707 }
708 };
709
Adam Cohen95bb8002011-07-03 23:40:28 -0700710 public void completeDragExit() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700711 mLauncher.closeFolder();
712 mCurrentDragInfo = null;
713 mCurrentDragView = null;
714 mSuppressOnAdd = false;
715 mRearrangeOnClose = true;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800716 mIsExternalDrag = false;
Adam Cohen3e8f8112011-07-02 18:03:00 -0700717 }
718
Adam Cohencb3382b2011-05-24 14:07:08 -0700719 public void onDragExit(DragObject d) {
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700720 // We only close the folder if this is a true drag exit, ie. not because
721 // a drop has occurred above the folder.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700722 if (!d.dragComplete) {
723 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
724 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
725 }
726 mReorderAlarm.cancelAlarm();
Sunny Goyal48461932015-03-09 17:41:09 -0700727
728 if (ALLOW_FOLDER_SCROLL) {
729 mOnScrollHintAlarm.cancelAlarm();
730 mScrollPauseAlarm.cancelAlarm();
731 if (mScrollHintDir != DragController.SCROLL_NONE) {
732 mPagedView.clearScrollHint();
733 mScrollHintDir = DragController.SCROLL_NONE;
734 }
735 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700736 }
737
Michael Jurka1e2f4652013-07-08 18:03:46 -0700738 public void onDropCompleted(final View target, final DragObject d,
739 final boolean isFlingToDelete, final boolean success) {
740 if (mDeferDropAfterUninstall) {
Michael Jurkaf3007582013-08-21 14:33:57 +0200741 Log.d(TAG, "Deferred handling drop because waiting for uninstall.");
Michael Jurka1e2f4652013-07-08 18:03:46 -0700742 mDeferredAction = new Runnable() {
743 public void run() {
744 onDropCompleted(target, d, isFlingToDelete, success);
745 mDeferredAction = null;
746 }
747 };
748 return;
749 }
750
751 boolean beingCalledAfterUninstall = mDeferredAction != null;
752 boolean successfulDrop =
753 success && (!beingCalledAfterUninstall || mUninstallSuccessful);
Winson Chung5f8afe62013-08-12 16:19:28 -0700754
Michael Jurka1e2f4652013-07-08 18:03:46 -0700755 if (successfulDrop) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800756 if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon && target != this) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700757 replaceFolderWithFinalItem();
758 }
759 } else {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800760 rearrangeChildren();
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700761 // The drag failed, we need to return the item to the folder
762 mFolderIcon.onDrop(d);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700763 }
764
765 if (target != this) {
766 if (mOnExitAlarm.alarmPending()) {
767 mOnExitAlarm.cancelAlarm();
Michael Jurka54554252013-08-01 12:52:23 +0200768 if (!successfulDrop) {
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700769 mSuppressFolderDeletion = true;
770 }
Sunny Goyal5d85c442015-03-10 13:14:47 -0700771 mScrollPauseAlarm.cancelAlarm();
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700772 completeDragExit();
773 }
Adam Cohen9c58d822013-12-13 17:18:10 -0800774 }
775
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700776 mDeleteFolderOnDropCompleted = false;
777 mDragInProgress = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700778 mItemAddedBackToSelfViaIcon = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700779 mCurrentDragInfo = null;
780 mCurrentDragView = null;
781 mSuppressOnAdd = false;
Adam Cohen4045eb72011-10-06 11:44:26 -0700782
783 // Reordering may have occured, and we need to save the new item locations. We do this once
784 // at the end to prevent unnecessary database operations.
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700785 updateItemLocationsInDatabaseBatch();
Adam Cohen4045eb72011-10-06 11:44:26 -0700786 }
787
Michael Jurka1e2f4652013-07-08 18:03:46 -0700788 public void deferCompleteDropAfterUninstallActivity() {
789 mDeferDropAfterUninstall = true;
790 }
791
792 public void onUninstallActivityReturned(boolean success) {
793 mDeferDropAfterUninstall = false;
794 mUninstallSuccessful = success;
795 if (mDeferredAction != null) {
796 mDeferredAction.run();
797 }
798 }
799
Winson Chunga48487a2012-03-20 16:19:37 -0700800 @Override
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800801 public float getIntrinsicIconScaleFactor() {
802 return 1f;
803 }
804
805 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800806 public boolean supportsFlingToDelete() {
807 return true;
808 }
809
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000810 @Override
811 public boolean supportsAppInfoDropTarget() {
812 return false;
813 }
814
815 @Override
816 public boolean supportsDeleteDropTarget() {
817 return true;
818 }
819
Winson Chunga48487a2012-03-20 16:19:37 -0700820 public void onFlingToDelete(DragObject d, int x, int y, PointF vec) {
821 // Do nothing
822 }
823
824 @Override
825 public void onFlingToDeleteCompleted() {
826 // Do nothing
827 }
828
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700829 private void updateItemLocationsInDatabaseBatch() {
830 ArrayList<View> list = getItemsInReadingOrder();
831 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
832 for (int i = 0; i < list.size(); i++) {
833 View v = list.get(i);
834 ItemInfo info = (ItemInfo) v.getTag();
Sunny Goyal08f72612015-01-05 13:41:43 -0800835 info.rank = i;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700836 items.add(info);
837 }
838
839 LauncherModel.moveItemsInDatabase(mLauncher, items, mInfo.id, 0);
840 }
841
Adam Cohene25af792013-06-06 23:08:25 -0700842 public void addItemLocationsInDatabase() {
843 ArrayList<View> list = getItemsInReadingOrder();
844 for (int i = 0; i < list.size(); i++) {
845 View v = list.get(i);
846 ItemInfo info = (ItemInfo) v.getTag();
847 LauncherModel.addItemToDatabase(mLauncher, info, mInfo.id, 0,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700848 info.cellX, info.cellY);
Adam Cohene25af792013-06-06 23:08:25 -0700849 }
850 }
851
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700852 public void notifyDrop() {
853 if (mDragInProgress) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700854 mItemAddedBackToSelfViaIcon = true;
Adam Cohen76078c42011-06-09 15:06:52 -0700855 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700856 }
857
858 public boolean isDropEnabled() {
859 return true;
860 }
861
Adam Cohen2801caf2011-05-13 20:57:39 -0700862 public boolean isFull() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800863 return mContent.isFull();
Adam Cohen2801caf2011-05-13 20:57:39 -0700864 }
865
866 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700867 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700868
Winson Chung892c74d2013-08-22 16:15:50 -0700869 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700870 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700871 int height = getFolderHeight();
Adam Cohen2801caf2011-05-13 20:57:39 -0700872
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800873 float scale = parent.getDescendantRectRelativeToSelf(mFolderIcon, sTempRect);
Adam Cohen8e776a62011-06-28 18:10:06 -0700874
Winson Chungaf40f202013-09-18 18:26:31 -0700875 LauncherAppState app = LauncherAppState.getInstance();
876 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
877
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800878 int centerX = (int) (sTempRect.left + sTempRect.width() * scale / 2);
879 int centerY = (int) (sTempRect.top + sTempRect.height() * scale / 2);
Adam Cohen2801caf2011-05-13 20:57:39 -0700880 int centeredLeft = centerX - width / 2;
881 int centeredTop = centerY - height / 2;
Winson Chung3057b1c2013-10-10 17:35:35 -0700882 int currentPage = mLauncher.getWorkspace().getNextPage();
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800883 // In case the workspace is scrolling, we need to use the final scroll to compute
884 // the folders bounds.
885 mLauncher.getWorkspace().setFinalScrollForPageChange(currentPage);
Adam Cohen35e7e642011-07-17 14:47:18 -0700886 // We first fetch the currently visible CellLayoutChildren
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800887 CellLayout currentLayout = (CellLayout) mLauncher.getWorkspace().getChildAt(currentPage);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700888 ShortcutAndWidgetContainer boundingLayout = currentLayout.getShortcutsAndWidgets();
Adam Cohen35e7e642011-07-17 14:47:18 -0700889 Rect bounds = new Rect();
890 parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800891 // We reset the workspaces scroll
892 mLauncher.getWorkspace().resetFinalScrollForPageChange(currentPage);
Adam Cohen2801caf2011-05-13 20:57:39 -0700893
Adam Cohen35e7e642011-07-17 14:47:18 -0700894 // We need to bound the folder to the currently visible CellLayoutChildren
895 int left = Math.min(Math.max(bounds.left, centeredLeft),
896 bounds.left + bounds.width() - width);
897 int top = Math.min(Math.max(bounds.top, centeredTop),
898 bounds.top + bounds.height() - height);
Winson Chungaf40f202013-09-18 18:26:31 -0700899 if (grid.isPhone() && (grid.availableWidthPx - width) < grid.iconSizePx) {
900 // Center the folder if it is full (on phones only)
901 left = (grid.availableWidthPx - width) / 2;
902 } else if (width >= bounds.width()) {
903 // If the folder doesn't fit within the bounds, center it about the desired bounds
Adam Cohen35e7e642011-07-17 14:47:18 -0700904 left = bounds.left + (bounds.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700905 }
Adam Cohen35e7e642011-07-17 14:47:18 -0700906 if (height >= bounds.height()) {
907 top = bounds.top + (bounds.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700908 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700909
910 int folderPivotX = width / 2 + (centeredLeft - left);
911 int folderPivotY = height / 2 + (centeredTop - top);
912 setPivotX(folderPivotX);
913 setPivotY(folderPivotY);
Adam Cohen268c4752012-06-06 17:47:33 -0700914 mFolderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700915 (1.0f * folderPivotX / width));
Adam Cohen268c4752012-06-06 17:47:33 -0700916 mFolderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700917 (1.0f * folderPivotY / height));
Adam Cohen3bf84d32012-05-07 20:17:14 -0700918
Adam Cohen662b5982011-12-13 17:45:21 -0800919 lp.width = width;
920 lp.height = height;
921 lp.x = left;
922 lp.y = top;
Adam Cohen2801caf2011-05-13 20:57:39 -0700923 }
924
Adam Cohen268c4752012-06-06 17:47:33 -0700925 float getPivotXForIconAnimation() {
926 return mFolderIconPivotX;
927 }
928 float getPivotYForIconAnimation() {
929 return mFolderIconPivotY;
930 }
931
Winson Chung892c74d2013-08-22 16:15:50 -0700932 private int getContentAreaHeight() {
933 LauncherAppState app = LauncherAppState.getInstance();
934 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
935 Rect workspacePadding = grid.getWorkspacePadding(grid.isLandscape ?
936 CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
937 int maxContentAreaHeight = grid.availableHeightPx -
Winson Chung892c74d2013-08-22 16:15:50 -0700938 workspacePadding.top - workspacePadding.bottom -
Sunny Goyal290800b2015-03-05 11:33:33 -0800939 mFooterHeight;
Adam Cohen1960ea42013-11-12 11:33:14 +0000940 int height = Math.min(maxContentAreaHeight,
Winson Chung892c74d2013-08-22 16:15:50 -0700941 mContent.getDesiredHeight());
Adam Cohen1960ea42013-11-12 11:33:14 +0000942 return Math.max(height, MIN_CONTENT_DIMEN);
943 }
944
945 private int getContentAreaWidth() {
946 return Math.max(mContent.getDesiredWidth(), MIN_CONTENT_DIMEN);
Winson Chung892c74d2013-08-22 16:15:50 -0700947 }
948
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700949 private int getFolderHeight() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800950 return getFolderHeight(getContentAreaHeight());
951 }
952
953 private int getFolderHeight(int contentAreaHeight) {
Sunny Goyal290800b2015-03-05 11:33:33 -0800954 return getPaddingTop() + getPaddingBottom() + contentAreaHeight + mFooterHeight;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700955 }
Adam Cohen234c4cd2011-07-17 21:03:04 -0700956
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700957 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800958 int contentWidth = getContentAreaWidth();
959 int contentHeight = getContentAreaHeight();
Adam Cohen1960ea42013-11-12 11:33:14 +0000960
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800961 int contentAreaWidthSpec = MeasureSpec.makeMeasureSpec(contentWidth, MeasureSpec.EXACTLY);
962 int contentAreaHeightSpec = MeasureSpec.makeMeasureSpec(contentHeight, MeasureSpec.EXACTLY);
963
964 mContent.setFixedSize(contentWidth, contentHeight);
965 mContentWrapper.measure(contentAreaWidthSpec, contentAreaHeightSpec);
Sunny Goyal290800b2015-03-05 11:33:33 -0800966 mFooter.measure(contentAreaWidthSpec,
967 MeasureSpec.makeMeasureSpec(mFooterHeight, MeasureSpec.EXACTLY));
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800968
969 int folderWidth = getPaddingLeft() + getPaddingRight() + contentWidth;
970 int folderHeight = getFolderHeight(contentHeight);
971 setMeasuredDimension(folderWidth, folderHeight);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700972 }
973
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800974 /**
975 * Rearranges the children based on their rank.
976 */
977 public void rearrangeChildren() {
978 rearrangeChildren(-1);
979 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700980
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800981 /**
982 * Rearranges the children based on their rank.
983 * @param itemCount if greater than the total children count, empty spaces are left at the end,
984 * otherwise it is ignored.
985 */
986 public void rearrangeChildren(int itemCount) {
987 ArrayList<View> views = getItemsInReadingOrder();
988 mContent.arrangeChildren(views, Math.max(itemCount, views.size()));
Adam Cohen7c693212011-05-18 15:26:57 -0700989 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700990 }
991
Sunny Goyalc4918352015-03-10 18:15:48 -0700992 // TODO remove this once GSA code fix is submitted
993 public ViewGroup getContent() {
994 return (ViewGroup) mContent;
995 }
996
Adam Cohena9cf38f2011-05-02 15:36:58 -0700997 public int getItemCount() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800998 return mContent.getItemCount();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700999 }
1000
Adam Cohen091440a2015-03-18 14:16:05 -07001001 @Thunk void onCloseComplete() {
Adam Cohen05e0f402011-08-01 12:12:49 -07001002 DragLayer parent = (DragLayer) getParent();
Michael Jurka5649c282012-06-18 10:33:21 -07001003 if (parent != null) {
1004 parent.removeView(this);
1005 }
Adam Cohen4554ee12011-08-03 16:13:21 -07001006 mDragController.removeDropTarget((DropTarget) this);
Adam Cohen05e0f402011-08-01 12:12:49 -07001007 clearFocus();
Adam Cohenac56cff2011-09-28 20:45:37 -07001008 mFolderIcon.requestFocus();
Adam Cohen05e0f402011-08-01 12:12:49 -07001009
Adam Cohen2801caf2011-05-13 20:57:39 -07001010 if (mRearrangeOnClose) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001011 rearrangeChildren();
Adam Cohen2801caf2011-05-13 20:57:39 -07001012 mRearrangeOnClose = false;
1013 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001014 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001015 if (!mDragInProgress && !mSuppressFolderDeletion) {
1016 replaceFolderWithFinalItem();
1017 } else if (mDragInProgress) {
1018 mDeleteFolderOnDropCompleted = true;
1019 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001020 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001021 mSuppressFolderDeletion = false;
Adam Cohenafb01ee2011-06-23 15:38:03 -07001022 }
1023
Adam Cohen091440a2015-03-18 14:16:05 -07001024 @Thunk void replaceFolderWithFinalItem() {
Adam Cohenafb01ee2011-06-23 15:38:03 -07001025 // Add the last remaining child to the workspace in place of the folder
Adam Cohenfb91f302012-06-11 15:45:18 -07001026 Runnable onCompleteRunnable = new Runnable() {
1027 @Override
1028 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -07001029 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screenId);
Adam Cohenafb01ee2011-06-23 15:38:03 -07001030
Winson Chung33231f52013-12-09 16:57:45 -08001031 View child = null;
Adam Cohenfb91f302012-06-11 15:45:18 -07001032 // Move the item from the folder to the workspace, in the position of the folder
1033 if (getItemCount() == 1) {
1034 ShortcutInfo finalItem = mInfo.contents.get(0);
Adam Cohenc5e63f32012-07-12 16:16:57 -07001035 child = mLauncher.createShortcut(R.layout.application, cellLayout,
Adam Cohenfb91f302012-06-11 15:45:18 -07001036 finalItem);
1037 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -07001038 mInfo.screenId, mInfo.cellX, mInfo.cellY);
Adam Cohenfb91f302012-06-11 15:45:18 -07001039 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001040 if (getItemCount() <= 1) {
1041 // Remove the folder
1042 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
Dan Sandler0eb687f2014-01-10 13:24:55 -05001043 if (cellLayout != null) {
1044 // b/12446428 -- sometimes the cell layout has already gone away?
1045 cellLayout.removeView(mFolderIcon);
1046 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001047 if (mFolderIcon instanceof DropTarget) {
1048 mDragController.removeDropTarget((DropTarget) mFolderIcon);
1049 }
1050 mLauncher.removeFolder(mInfo);
1051 }
Adam Cohenc5e63f32012-07-12 16:16:57 -07001052 // We add the child after removing the folder to prevent both from existing at
Winson Chung0e6a7132013-08-23 12:55:10 -07001053 // the same time in the CellLayout. We need to add the new item with addInScreenFromBind()
1054 // to ensure that hotseat items are placed correctly.
Adam Cohenc5e63f32012-07-12 16:16:57 -07001055 if (child != null) {
Winson Chung0e6a7132013-08-23 12:55:10 -07001056 mLauncher.getWorkspace().addInScreenFromBind(child, mInfo.container, mInfo.screenId,
Adam Cohenc5e63f32012-07-12 16:16:57 -07001057 mInfo.cellX, mInfo.cellY, mInfo.spanX, mInfo.spanY);
1058 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001059 }
1060 };
Sunny Goyalbc753352015-03-05 09:40:44 -08001061 View finalChild = mContent.getLastItem();
Adam Cohenfb91f302012-06-11 15:45:18 -07001062 if (finalChild != null) {
1063 mFolderIcon.performDestroyAnimation(finalChild, onCompleteRunnable);
Winson Chung33231f52013-12-09 16:57:45 -08001064 } else {
1065 onCompleteRunnable.run();
Adam Cohenafb01ee2011-06-23 15:38:03 -07001066 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001067 mDestroyed = true;
1068 }
1069
1070 boolean isDestroyed() {
1071 return mDestroyed;
Adam Cohen2801caf2011-05-13 20:57:39 -07001072 }
1073
Adam Cohenac56cff2011-09-28 20:45:37 -07001074 // This method keeps track of the last item in the folder for the purposes
1075 // of keyboard focus
Sunny Goyal290800b2015-03-05 11:33:33 -08001076 public void updateTextViewFocus() {
Sunny Goyalbc753352015-03-05 09:40:44 -08001077 View lastChild = mContent.getLastItem();
Adam Cohenac56cff2011-09-28 20:45:37 -07001078 if (lastChild != null) {
1079 mFolderName.setNextFocusDownId(lastChild.getId());
1080 mFolderName.setNextFocusRightId(lastChild.getId());
1081 mFolderName.setNextFocusLeftId(lastChild.getId());
1082 mFolderName.setNextFocusUpId(lastChild.getId());
1083 }
1084 }
1085
Adam Cohenbfbfd262011-06-13 16:55:12 -07001086 public void onDrop(DragObject d) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001087 Runnable cleanUpRunnable = null;
1088
Adam Cohen689ff162014-05-08 17:27:56 -07001089 // If we are coming from All Apps space, we defer removing the extra empty screen
1090 // until the folder closes
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001091 if (d.dragSource != mLauncher.getWorkspace() && !(d.dragSource instanceof Folder)) {
1092 cleanUpRunnable = new Runnable() {
1093 @Override
1094 public void run() {
Adam Cohen689ff162014-05-08 17:27:56 -07001095 mLauncher.exitSpringLoadedDragModeDelayed(true,
1096 Launcher.EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
1097 null);
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001098 }
1099 };
Adam Cohenbfbfd262011-06-13 16:55:12 -07001100 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001101
Sunny Goyal48461932015-03-09 17:41:09 -07001102 if (ALLOW_FOLDER_SCROLL) {
1103 // If the icon was dropped while the page was being scrolled, we need to compute
1104 // the target location again such that the icon is placed of the final page.
1105 if (!mPagedView.rankOnCurrentPage(mEmptyCellRank)) {
1106 // Reorder again.
1107 mTargetRank = getTargetRank(d, null);
1108
1109 // Rearrange items immediately.
1110 mReorderAlarmListener.onAlarm(mReorderAlarm);
1111
1112 mOnScrollHintAlarm.cancelAlarm();
1113 mScrollPauseAlarm.cancelAlarm();
1114 }
1115 mPagedView.completePendingPageChanges();
1116 }
1117
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001118 View currentDragView;
1119 ShortcutInfo si = mCurrentDragInfo;
1120 if (mIsExternalDrag) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001121 currentDragView = mContent.createAndAddViewForRank(si, mEmptyCellRank);
Sunny Goyal95abbb32014-08-04 10:53:22 -07001122 // Actually move the item in the database if it was an external drag. Call this
1123 // before creating the view, so that ShortcutInfo is updated appropriately.
1124 LauncherModel.addOrMoveItemInDatabase(
1125 mLauncher, si, mInfo.id, 0, si.cellX, si.cellY);
1126
1127 // We only need to update the locations if it doesn't get handled in #onDropCompleted.
1128 if (d.dragSource != this) {
1129 updateItemLocationsInDatabaseBatch();
1130 }
1131 mIsExternalDrag = false;
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001132 } else {
1133 currentDragView = mCurrentDragView;
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001134 mContent.addViewForRank(currentDragView, si, mEmptyCellRank);
Adam Cohenbfbfd262011-06-13 16:55:12 -07001135 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001136
1137 if (d.dragView.hasDrawn()) {
1138
1139 // Temporarily reset the scale such that the animation target gets calculated correctly.
1140 float scaleX = getScaleX();
1141 float scaleY = getScaleY();
1142 setScaleX(1.0f);
1143 setScaleY(1.0f);
1144 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, currentDragView,
1145 cleanUpRunnable, null);
1146 setScaleX(scaleX);
1147 setScaleY(scaleY);
1148 } else {
1149 d.deferDragViewCleanupPostAnimation = false;
1150 currentDragView.setVisibility(VISIBLE);
1151 }
1152 mItemsInvalidated = true;
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001153 rearrangeChildren();
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001154
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001155 // Temporarily suppress the listener, as we did all the work already here.
1156 mSuppressOnAdd = true;
1157 mInfo.add(si);
1158 mSuppressOnAdd = false;
Sunny Goyal4b020172014-08-28 14:51:14 -07001159 // Clear the drag info, as it is no longer being dragged.
1160 mCurrentDragInfo = null;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001161 }
1162
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001163 // This is used so the item doesn't immediately appear in the folder when added. In one case
1164 // we need to create the illusion that the item isn't added back to the folder yet, to
1165 // to correspond to the animation of the icon back into the folder. This is
1166 public void hideItem(ShortcutInfo info) {
1167 View v = getViewForInfo(info);
1168 v.setVisibility(INVISIBLE);
1169 }
1170 public void showItem(ShortcutInfo info) {
1171 View v = getViewForInfo(info);
1172 v.setVisibility(VISIBLE);
1173 }
1174
Adam Cohenbfbfd262011-06-13 16:55:12 -07001175 public void onAdd(ShortcutInfo item) {
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));
Sunny Goyal2e688a82015-03-18 10:23:39 -07001180 mItemsInvalidated = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001181 LauncherModel.addOrMoveItemInDatabase(
1182 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
1183 }
1184
Adam Cohena9cf38f2011-05-02 15:36:58 -07001185 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -07001186 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001187 // If this item is being dragged from this open folder, we have already handled
1188 // the work associated with removing the item, so we don't have to do anything here.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001189 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001190 View v = getViewForInfo(item);
Sunny Goyal290800b2015-03-05 11:33:33 -08001191 mContent.removeItem(v);
Adam Cohen2801caf2011-05-13 20:57:39 -07001192 if (mState == STATE_ANIMATING) {
1193 mRearrangeOnClose = true;
1194 } else {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001195 rearrangeChildren();
Adam Cohen2801caf2011-05-13 20:57:39 -07001196 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001197 if (getItemCount() <= 1) {
1198 replaceFolderWithFinalItem();
1199 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001200 }
Adam Cohen7c693212011-05-18 15:26:57 -07001201
Sunny Goyalbc753352015-03-05 09:40:44 -08001202 private View getViewForInfo(final ShortcutInfo item) {
1203 return mContent.iterateOverItems(new ItemOperator() {
1204
1205 @Override
1206 public boolean evaluate(ItemInfo info, View view, View parent) {
1207 return info == item;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001208 }
Sunny Goyalbc753352015-03-05 09:40:44 -08001209 });
Adam Cohendf1e4e82011-06-24 15:57:39 -07001210 }
1211
Adam Cohen76078c42011-06-09 15:06:52 -07001212 public void onItemsChanged() {
Adam Cohenac56cff2011-09-28 20:45:37 -07001213 updateTextViewFocus();
Adam Cohen76078c42011-06-09 15:06:52 -07001214 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001215
Adam Cohen76fc0852011-06-17 13:26:23 -07001216 public void onTitleChanged(CharSequence title) {
1217 }
Adam Cohen76078c42011-06-09 15:06:52 -07001218
Adam Cohen7c693212011-05-18 15:26:57 -07001219 public ArrayList<View> getItemsInReadingOrder() {
1220 if (mItemsInvalidated) {
1221 mItemsInReadingOrder.clear();
Sunny Goyalbc753352015-03-05 09:40:44 -08001222 mContent.iterateOverItems(new ItemOperator() {
1223
1224 @Override
1225 public boolean evaluate(ItemInfo info, View view, View parent) {
1226 mItemsInReadingOrder.add(view);
1227 return false;
Adam Cohen7c693212011-05-18 15:26:57 -07001228 }
Sunny Goyalbc753352015-03-05 09:40:44 -08001229 });
Adam Cohen7c693212011-05-18 15:26:57 -07001230 mItemsInvalidated = false;
1231 }
1232 return mItemsInReadingOrder;
1233 }
Adam Cohen8dfcba42011-07-07 16:38:18 -07001234
1235 public void getLocationInDragLayer(int[] loc) {
1236 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
1237 }
Adam Cohenea0818d2011-09-30 20:06:58 -07001238
1239 public void onFocusChange(View v, boolean hasFocus) {
1240 if (v == mFolderName && hasFocus) {
1241 startEditingFolderName();
1242 }
1243 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001244
1245 @Override
1246 public void getHitRectRelativeToDragLayer(Rect outRect) {
1247 getHitRect(outRect);
Sunny Goyal48461932015-03-09 17:41:09 -07001248 outRect.left -= mScrollAreaOffset;
1249 outRect.right += mScrollAreaOffset;
1250 }
1251
1252 private class OnScrollHintListener implements OnAlarmListener {
1253
1254 private final DragObject mDragObject;
1255
1256 OnScrollHintListener(DragObject object) {
1257 mDragObject = object;
1258 }
1259
1260 /**
1261 * Scroll hint has been shown long enough. Now scroll to appropriate page.
1262 */
1263 @Override
1264 public void onAlarm(Alarm alarm) {
1265 if (mCurrentScrollDir == DragController.SCROLL_LEFT) {
1266 mPagedView.scrollLeft();
1267 mScrollHintDir = DragController.SCROLL_NONE;
1268 } else if (mCurrentScrollDir == DragController.SCROLL_RIGHT) {
1269 mPagedView.scrollRight();
1270 mScrollHintDir = DragController.SCROLL_NONE;
1271 } else {
1272 // This should not happen
1273 return;
1274 }
1275 mCurrentScrollDir = DragController.SCROLL_NONE;
1276
1277 // Pause drag event until the scrolling is finished
1278 mScrollPauseAlarm.setOnAlarmListener(new OnScrollFinishedListener(mDragObject));
1279 mScrollPauseAlarm.setAlarm(DragController.RESCROLL_DELAY);
1280 }
1281 }
1282
1283 private class OnScrollFinishedListener implements OnAlarmListener {
1284
1285 private final DragObject mDragObject;
1286
1287 OnScrollFinishedListener(DragObject object) {
1288 mDragObject = object;
1289 }
1290
1291 /**
1292 * Page scroll is complete.
1293 */
1294 @Override
1295 public void onAlarm(Alarm alarm) {
1296 // Reorder immediately on page change.
1297 onDragOver(mDragObject, 1);
1298 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001299 }
Sunny Goyalbc753352015-03-05 09:40:44 -08001300
1301 public static interface FolderContent {
1302 void setFolder(Folder f);
1303
Sunny Goyal290800b2015-03-05 11:33:33 -08001304 void removeItem(View v);
Sunny Goyalbc753352015-03-05 09:40:44 -08001305
1306 boolean isFull();
1307 int getItemCount();
1308
1309 int getDesiredWidth();
1310 int getDesiredHeight();
1311 void setFixedSize(int width, int height);
1312
1313 /**
1314 * Iterates over all its items in a reading order.
1315 * @return the view for which the operator returned true.
1316 */
1317 View iterateOverItems(ItemOperator op);
1318 View getLastItem();
1319
1320 String getAccessibilityDescription();
1321
1322 /**
1323 * Binds items to the layout.
1324 * @return list of items that could not be bound, probably because we hit the max size limit.
1325 */
1326 ArrayList<ShortcutInfo> bindItems(ArrayList<ShortcutInfo> children);
1327
1328 /**
Sunny Goyal5d85c442015-03-10 13:14:47 -07001329 * Create space for a new item, and returns the rank for that item.
Sunny Goyalbc753352015-03-05 09:40:44 -08001330 * Resizes the content if necessary.
1331 */
Sunny Goyal5d85c442015-03-10 13:14:47 -07001332 int allocateRankForNewItem(ShortcutInfo info);
Sunny Goyalbc753352015-03-05 09:40:44 -08001333
1334 View createAndAddViewForRank(ShortcutInfo item, int rank);
1335
1336 /**
1337 * Adds the {@param view} to the layout based on {@param rank} and updated the position
1338 * related attributes. It assumes that {@param item} is already attached to the view.
1339 */
1340 void addViewForRank(View view, ShortcutInfo item, int rank);
1341
1342 /**
1343 * Reorders the items such that the {@param empty} spot moves to {@param target}
1344 */
1345 void realTimeReorder(int empty, int target);
1346
1347 /**
1348 * @return the rank of the cell nearest to the provided pixel position.
1349 */
1350 int findNearestArea(int pixelX, int pixelY);
1351
1352 /**
1353 * Updates position and rank of all the children in the view based.
1354 * @param list the ordered list of children.
1355 * @param itemCount if greater than the total children count, empty spaces are left
1356 * at the end.
1357 */
1358 void arrangeChildren(ArrayList<View> list, int itemCount);
1359
1360 /**
1361 * Sets the focus on the first visible child.
1362 */
1363 void setFocusOnFirstChild();
1364 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001365}