blob: 6ca488853f4e0a7f8ef978e2b37e744b333360a3 [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 Goyal75deaf32015-05-07 16:13:12 -070030import android.graphics.drawable.InsetDrawable;
Sunny Goyalc46bfef2015-01-05 12:40:08 -080031import android.os.Build;
Adam Cohen7a14d0b2011-06-24 11:36:35 -070032import android.text.InputType;
Adam Cohene601a432011-07-26 21:51:30 -070033import android.text.Selection;
34import android.text.Spannable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.util.AttributeSet;
Adam Cohen3bf84d32012-05-07 20:17:14 -070036import android.util.Log;
Adam Cohen76fc0852011-06-17 13:26:23 -070037import android.view.ActionMode;
38import android.view.KeyEvent;
Adam Cohendf2cc412011-04-27 16:56:57 -070039import android.view.LayoutInflater;
Adam Cohen76fc0852011-06-17 13:26:23 -070040import android.view.Menu;
41import android.view.MenuItem;
Adam Cohen0c872ba2011-05-05 10:34:16 -070042import android.view.MotionEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.view.View;
Sunny Goyalc4918352015-03-10 18:15:48 -070044import android.view.ViewGroup;
Adam Cohen3371da02011-10-25 21:38:29 -070045import android.view.accessibility.AccessibilityEvent;
46import android.view.accessibility.AccessibilityManager;
Adam Cohenc4fe9ea2014-08-18 18:54:10 -070047import android.view.animation.AccelerateInterpolator;
Adam Cohen76fc0852011-06-17 13:26:23 -070048import android.view.inputmethod.EditorInfo;
49import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.widget.LinearLayout;
Adam Cohendf2cc412011-04-27 16:56:57 -070051import android.widget.TextView;
Sunny Goyal48461932015-03-09 17:41:09 -070052
Sunny Goyale9b651e2015-04-24 11:44:51 -070053import com.android.launcher3.CellLayout.CellInfo;
Sunny Goyalf4066152015-04-15 09:42:19 -070054import com.android.launcher3.DragController.DragListener;
Daniel Sandler325dc232013-06-05 22:57:57 -040055import com.android.launcher3.FolderInfo.FolderListener;
Sunny Goyale9b651e2015-04-24 11:44:51 -070056import com.android.launcher3.LauncherAccessibilityDelegate.AccessibilityDragSource;
Sunny Goyalfa401a12015-04-10 13:45:42 -070057import com.android.launcher3.UninstallDropTarget.UninstallSource;
Sunny Goyalbc753352015-03-05 09:40:44 -080058import com.android.launcher3.Workspace.ItemOperator;
Adam Cohen091440a2015-03-18 14:16:05 -070059import com.android.launcher3.util.Thunk;
Romain Guyedcce092010-03-04 13:03:17 -080060
Adam Cohenc0dcf592011-06-01 15:30:43 -070061import java.util.ArrayList;
Adam Cohen3bf84d32012-05-07 20:17:14 -070062import java.util.Collections;
Adam Cohenc0dcf592011-06-01 15:30:43 -070063
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064/**
65 * Represents a set of icons chosen by the user or generated by the system.
66 */
Adam Cohen8dfcba42011-07-07 16:38:18 -070067public class Folder extends LinearLayout implements DragSource, View.OnClickListener,
Adam Cohenea0818d2011-09-30 20:06:58 -070068 View.OnLongClickListener, DropTarget, FolderListener, TextView.OnEditorActionListener,
Sunny Goyale9b651e2015-04-24 11:44:51 -070069 View.OnFocusChangeListener, DragListener, UninstallSource, AccessibilityDragSource {
Adam Cohendf2cc412011-04-27 16:56:57 -070070 private static final String TAG = "Launcher.Folder";
71
Sunny Goyalc3a609f2015-02-26 17:43:50 -080072 /**
73 * We avoid measuring {@link #mContentWrapper} with a 0 width or height, as this
74 * results in CellLayout being measured as UNSPECIFIED, which it does not support.
75 */
76 private static final int MIN_CONTENT_DIMEN = 5;
Adam Cohen4eac29a2011-07-11 17:53:37 -070077
Adam Cohendf2cc412011-04-27 16:56:57 -070078 static final int STATE_NONE = -1;
79 static final int STATE_SMALL = 0;
80 static final int STATE_ANIMATING = 1;
81 static final int STATE_OPEN = 2;
82
Sunny Goyal48461932015-03-09 17:41:09 -070083 /**
Sunny Goyal48461932015-03-09 17:41:09 -070084 * 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();
Sunny Goyalb8634152015-04-09 14:17:14 -0700102 private final Alarm mOnScrollHintAlarm = new Alarm();
103 @Thunk final Alarm mScrollPauseAlarm = new Alarm();
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800104
Adam Cohen091440a2015-03-18 14:16:05 -0700105 @Thunk final ArrayList<View> mItemsInReadingOrder = new ArrayList<View>();
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800106
107 private final int mExpandDuration;
108 private final int mMaterialExpandDuration;
109 private final int mMaterialExpandStagger;
110
111 private final InputMethodManager mInputMethodManager;
112
113 protected final Launcher mLauncher;
114 protected DragController mDragController;
115 protected FolderInfo mInfo;
116
Adam Cohen091440a2015-03-18 14:16:05 -0700117 @Thunk FolderIcon mFolderIcon;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800118
Sunny Goyalb8634152015-04-09 14:17:14 -0700119 @Thunk FolderPagedView mContent;
Adam Cohen091440a2015-03-18 14:16:05 -0700120 @Thunk View mContentWrapper;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800121 FolderEditText mFolderName;
122
Sunny Goyal290800b2015-03-05 11:33:33 -0800123 private View mFooter;
124 private int mFooterHeight;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800125
126 // Cell ranks used for drag and drop
Adam Cohen091440a2015-03-18 14:16:05 -0700127 @Thunk int mTargetRank, mPrevTargetRank, mEmptyCellRank;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800128
Adam Cohen091440a2015-03-18 14:16:05 -0700129 @Thunk int mState = STATE_NONE;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800130 private boolean mRearrangeOnClose = false;
Adam Cohen7c693212011-05-18 15:26:57 -0700131 boolean mItemsInvalidated = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700132 private ShortcutInfo mCurrentDragInfo;
133 private View mCurrentDragView;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800134 private boolean mIsExternalDrag;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700135 boolean mSuppressOnAdd = false;
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700136 private boolean mDragInProgress = false;
137 private boolean mDeleteFolderOnDropCompleted = false;
138 private boolean mSuppressFolderDeletion = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700139 private boolean mItemAddedBackToSelfViaIcon = false;
Adam Cohen091440a2015-03-18 14:16:05 -0700140 @Thunk float mFolderIconPivotX;
141 @Thunk float mFolderIconPivotY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700142 private boolean mIsEditingName = false;
Adam Cohen1960ea42013-11-12 11:33:14 +0000143
Adam Cohenfb91f302012-06-11 15:45:18 -0700144 private boolean mDestroyed;
145
Adam Cohen091440a2015-03-18 14:16:05 -0700146 @Thunk Runnable mDeferredAction;
Michael Jurka1e2f4652013-07-08 18:03:46 -0700147 private boolean mDeferDropAfterUninstall;
148 private boolean mUninstallSuccessful;
149
Sunny Goyal48461932015-03-09 17:41:09 -0700150 // Folder scrolling
151 private int mScrollAreaOffset;
Sunny Goyal48461932015-03-09 17:41:09 -0700152
Adam Cohen091440a2015-03-18 14:16:05 -0700153 @Thunk int mScrollHintDir = DragController.SCROLL_NONE;
154 @Thunk int mCurrentScrollDir = DragController.SCROLL_NONE;
Sunny Goyal48461932015-03-09 17:41:09 -0700155
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156 /**
157 * Used to inflate the Workspace from XML.
158 *
159 * @param context The application's context.
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800160 * @param attrs The attributes set containing the Workspace's customization values.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161 */
162 public Folder(Context context, AttributeSet attrs) {
163 super(context, attrs);
164 setAlwaysDrawnWithCacheEnabled(false);
Adam Cohen76fc0852011-06-17 13:26:23 -0700165 mInputMethodManager = (InputMethodManager)
Michael Jurka8b805b12012-04-18 14:23:14 -0700166 getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
Adam Cohen76fc0852011-06-17 13:26:23 -0700167
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800168 Resources res = getResources();
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700169 mExpandDuration = res.getInteger(R.integer.config_folderExpandDuration);
170 mMaterialExpandDuration = res.getInteger(R.integer.config_materialFolderExpandDuration);
171 mMaterialExpandStagger = res.getInteger(R.integer.config_materialFolderExpandStagger);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700172
173 if (sDefaultFolderName == null) {
174 sDefaultFolderName = res.getString(R.string.folder_name);
175 }
Adam Cohena65beee2011-06-27 21:32:23 -0700176 if (sHintText == null) {
177 sHintText = res.getString(R.string.folder_hint_text);
178 }
Adam Cohen4eac29a2011-07-11 17:53:37 -0700179 mLauncher = (Launcher) context;
Adam Cohenac56cff2011-09-28 20:45:37 -0700180 // We need this view to be focusable in touch mode so that when text editing of the folder
181 // name is complete, we have something to focus on, thus hiding the cursor and giving
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800182 // reliable behavior when clicking the text field (since it will always gain focus on click).
Adam Cohenac56cff2011-09-28 20:45:37 -0700183 setFocusableInTouchMode(true);
Sunny Goyal75deaf32015-05-07 16:13:12 -0700184
185 if (Utilities.isLmpOrAbove()) {
186 int padding = getResources().getDimensionPixelSize(R.dimen.folder_shadow_padding);
187 setBackground(new InsetDrawable(
188 getResources().getDrawable(R.drawable.apps_list_bg),
189 padding, padding, padding, padding));
190 } else {
191 setBackgroundResource(R.drawable.quantum_panel);
192 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193 }
194
195 @Override
196 protected void onFinishInflate() {
197 super.onFinishInflate();
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800198 mContentWrapper = findViewById(R.id.folder_content_wrapper);
Sunny Goyalb8634152015-04-09 14:17:14 -0700199 mContent = (FolderPagedView) findViewById(R.id.folder_content);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800200 mContent.setFolder(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700201
Adam Cohenac56cff2011-09-28 20:45:37 -0700202 mFolderName = (FolderEditText) findViewById(R.id.folder_name);
203 mFolderName.setFolder(this);
Adam Cohenea0818d2011-09-30 20:06:58 -0700204 mFolderName.setOnFocusChangeListener(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700205
Adam Cohen76fc0852011-06-17 13:26:23 -0700206 // We disable action mode for now since it messes up the view on phones
207 mFolderName.setCustomSelectionActionModeCallback(mActionModeCallback);
Adam Cohen76fc0852011-06-17 13:26:23 -0700208 mFolderName.setOnEditorActionListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700209 mFolderName.setSelectAllOnFocus(true);
Adam Cohen7a14d0b2011-06-24 11:36:35 -0700210 mFolderName.setInputType(mFolderName.getInputType() |
211 InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800212
Sunny Goyalb8634152015-04-09 14:17:14 -0700213 mFooter = findViewById(R.id.folder_footer);
214 updateFooterHeight();
215 }
216
217 public void updateFooterHeight() {
Sunny Goyal290800b2015-03-05 11:33:33 -0800218 // We find out how tall footer wants to be (it is set to wrap_content), so that
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800219 // we can allocate the appropriate amount of space for it.
220 int measureSpec = MeasureSpec.UNSPECIFIED;
Sunny Goyal290800b2015-03-05 11:33:33 -0800221 mFooter.measure(measureSpec, measureSpec);
222 mFooterHeight = mFooter.getMeasuredHeight();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800223 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700224
Adam Cohen76fc0852011-06-17 13:26:23 -0700225 private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
226 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
227 return false;
228 }
229
230 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
231 return false;
232 }
233
234 public void onDestroyActionMode(ActionMode mode) {
235 }
236
237 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
238 return false;
239 }
240 };
241
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800242 public void onClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700243 Object tag = v.getTag();
244 if (tag instanceof ShortcutInfo) {
Adam Cohenb5fe60c2013-06-06 22:03:51 -0700245 mLauncher.onClick(v);
Adam Cohendf2cc412011-04-27 16:56:57 -0700246 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800247 }
248
249 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -0700250 // Return if global dragging is not enabled
251 if (!mLauncher.isDraggingEnabled()) return true;
Sunny Goyale9b651e2015-04-24 11:44:51 -0700252 return beginDrag(v, false);
253 }
Winson Chung36a62fe2012-05-06 18:04:42 -0700254
Sunny Goyale9b651e2015-04-24 11:44:51 -0700255 private boolean beginDrag(View v, boolean accessible) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700256 Object tag = v.getTag();
257 if (tag instanceof ShortcutInfo) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700258 ShortcutInfo item = (ShortcutInfo) tag;
259 if (!v.isInTouchMode()) {
260 return false;
261 }
262
Sunny Goyale9b651e2015-04-24 11:44:51 -0700263 mLauncher.getWorkspace().beginDragShared(v, new Point(), this, accessible);
Adam Cohen76078c42011-06-09 15:06:52 -0700264
265 mCurrentDragInfo = item;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800266 mEmptyCellRank = item.rank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700267 mCurrentDragView = v;
Adam Cohenfc53cd22011-07-20 15:45:11 -0700268
Sunny Goyal290800b2015-03-05 11:33:33 -0800269 mContent.removeItem(mCurrentDragView);
Adam Cohenfc53cd22011-07-20 15:45:11 -0700270 mInfo.remove(mCurrentDragInfo);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700271 mDragInProgress = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700272 mItemAddedBackToSelfViaIcon = false;
Adam Cohendf2cc412011-04-27 16:56:57 -0700273 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800274 return true;
275 }
276
Sunny Goyale9b651e2015-04-24 11:44:51 -0700277 @Override
278 public void startDrag(CellInfo cellInfo, boolean accessible) {
279 beginDrag(cellInfo.cell, accessible);
280 }
281
282 @Override
283 public void enableAccessibleDrag(boolean enable) {
284 mLauncher.getSearchBar().enableAccessibleDrag(enable);
285 for (int i = 0; i < mContent.getChildCount(); i++) {
286 mContent.getPageAt(i).enableAccessibleDrag(enable, CellLayout.FOLDER_ACCESSIBILITY_DRAG);
287 }
Sunny Goyalccc414b2015-04-30 12:28:16 -0700288
289 mFooter.setImportantForAccessibility(enable ? IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS :
290 IMPORTANT_FOR_ACCESSIBILITY_AUTO);
Sunny Goyale9b651e2015-04-24 11:44:51 -0700291 mLauncher.getWorkspace().setAddNewPageOnDrag(!enable);
292 }
293
Adam Cohen76fc0852011-06-17 13:26:23 -0700294 public boolean isEditingName() {
295 return mIsEditingName;
296 }
297
298 public void startEditingFolderName() {
Adam Cohena65beee2011-06-27 21:32:23 -0700299 mFolderName.setHint("");
Adam Cohen76fc0852011-06-17 13:26:23 -0700300 mIsEditingName = true;
301 }
302
303 public void dismissEditingName() {
304 mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
305 doneEditingFolderName(true);
306 }
307
308 public void doneEditingFolderName(boolean commit) {
Adam Cohena65beee2011-06-27 21:32:23 -0700309 mFolderName.setHint(sHintText);
Adam Cohen1df26a32011-08-12 16:15:23 -0700310 // Convert to a string here to ensure that no other state associated with the text field
311 // gets saved.
Winson Chung82b016c2015-05-08 17:00:10 -0700312 CharSequence newTitle = mFolderName.getText();
Adam Cohen3371da02011-10-25 21:38:29 -0700313 mInfo.setTitle(newTitle);
Adam Cohen76fc0852011-06-17 13:26:23 -0700314 LauncherModel.updateItemInDatabase(mLauncher, mInfo);
Adam Cohenac56cff2011-09-28 20:45:37 -0700315
Adam Cohen3371da02011-10-25 21:38:29 -0700316 if (commit) {
317 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Winson Chung82b016c2015-05-08 17:00:10 -0700318 String.format(getContext().getString(R.string.folder_renamed),
319 newTitle.toString()));
Adam Cohen3371da02011-10-25 21:38:29 -0700320 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700321 // In order to clear the focus from the text field, we set the focus on ourself. This
322 // ensures that every time the field is clicked, focus is gained, giving reliable behavior.
323 requestFocus();
324
Adam Cohene601a432011-07-26 21:51:30 -0700325 Selection.setSelection((Spannable) mFolderName.getText(), 0, 0);
Adam Cohen76fc0852011-06-17 13:26:23 -0700326 mIsEditingName = false;
327 }
328
329 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
330 if (actionId == EditorInfo.IME_ACTION_DONE) {
331 dismissEditingName();
332 return true;
333 }
334 return false;
335 }
336
337 public View getEditTextRegion() {
338 return mFolderName;
339 }
340
Adam Cohen0c872ba2011-05-05 10:34:16 -0700341 /**
342 * We need to handle touch events to prevent them from falling through to the workspace below.
343 */
344 @Override
345 public boolean onTouchEvent(MotionEvent ev) {
346 return true;
347 }
348
Joe Onorato00acb122009-08-04 16:04:30 -0400349 public void setDragController(DragController dragController) {
350 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800351 }
352
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800353 public void setFolderIcon(FolderIcon icon) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700354 mFolderIcon = icon;
355 }
356
Adam Cohen3371da02011-10-25 21:38:29 -0700357 @Override
358 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
359 // When the folder gets focus, we don't want to announce the list of items.
360 return true;
361 }
362
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800363 /**
364 * @return the FolderInfo object associated with this folder
365 */
366 FolderInfo getInfo() {
367 return mInfo;
368 }
369
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800370 void bind(FolderInfo info) {
371 mInfo = info;
Adam Cohendf2cc412011-04-27 16:56:57 -0700372 ArrayList<ShortcutInfo> children = info.contents;
Sunny Goyal08f72612015-01-05 13:41:43 -0800373 Collections.sort(children, Utilities.RANK_COMPARATOR);
Sunny Goyal08f72612015-01-05 13:41:43 -0800374
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800375 ArrayList<ShortcutInfo> overflow = mContent.bindItems(children);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700376
Jason Monk4ff73882014-04-24 16:48:07 -0400377 // If our folder has too many items we prune them from the list. This is an issue
Adam Cohenc508b2d2011-06-28 14:41:44 -0700378 // when upgrading from the old Folders implementation which could contain an unlimited
379 // number of items.
380 for (ShortcutInfo item: overflow) {
381 mInfo.remove(item);
382 LauncherModel.deleteItemFromDatabase(mLauncher, item);
383 }
384
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800385 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
386 if (lp == null) {
387 lp = new DragLayer.LayoutParams(0, 0);
388 lp.customPosition = true;
389 setLayoutParams(lp);
390 }
391 centerAboutIcon();
392
Adam Cohen4dbe6d92011-05-18 17:14:15 -0700393 mItemsInvalidated = true;
Adam Cohenac56cff2011-09-28 20:45:37 -0700394 updateTextViewFocus();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700395 mInfo.addListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700396
Adam Cohenafb01ee2011-06-23 15:38:03 -0700397 if (!sDefaultFolderName.contentEquals(mInfo.title)) {
Adam Cohen4ef610f2011-06-21 22:37:36 -0700398 mFolderName.setText(mInfo.title);
399 } else {
400 mFolderName.setText("");
401 }
Winson Chung33231f52013-12-09 16:57:45 -0800402
403 // In case any children didn't come across during loading, clean up the folder accordingly
404 mFolderIcon.post(new Runnable() {
405 public void run() {
406 if (getItemCount() <= 1) {
407 replaceFolderWithFinalItem();
408 }
409 }
410 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700411 }
412
413 /**
414 * Creates a new UserFolder, inflated from R.layout.user_folder.
415 *
416 * @param context The application's context.
417 *
418 * @return A new UserFolder.
419 */
420 static Folder fromXml(Context context) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700421 return (Folder) LayoutInflater.from(context).inflate(R.layout.user_folder, null);
Adam Cohendf2cc412011-04-27 16:56:57 -0700422 }
423
424 /**
425 * This method is intended to make the UserFolder to be visually identical in size and position
426 * to its associated FolderIcon. This allows for a seamless transition into the expanded state.
427 */
428 private void positionAndSizeAsIcon() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700429 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800430 setScaleX(0.8f);
431 setScaleY(0.8f);
432 setAlpha(0f);
Adam Cohendf2cc412011-04-27 16:56:57 -0700433 mState = STATE_SMALL;
434 }
435
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700436 private void prepareReveal() {
437 setScaleX(1f);
438 setScaleY(1f);
439 setAlpha(1f);
440 mState = STATE_SMALL;
441 }
442
Adam Cohendf2cc412011-04-27 16:56:57 -0700443 public void animateOpen() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700444 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen6441de02011-12-14 14:25:32 -0800445
Sunny Goyalb8634152015-04-09 14:17:14 -0700446 mContent.completePendingPageChanges();
Sunny Goyal8167dc22015-04-27 13:44:01 -0700447 if (!mDragInProgress) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700448 // Open on the first page.
449 mContent.snapToPageImmediately(0);
Sunny Goyal48461932015-03-09 17:41:09 -0700450 }
451
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700452 Animator openFolderAnim = null;
453 final Runnable onCompleteRunnable;
Kenny Guyd794a3f2014-09-16 15:17:58 +0100454 if (!Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700455 positionAndSizeAsIcon();
456 centerAboutIcon();
457
458 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1);
459 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
460 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
461 final ObjectAnimator oa =
462 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
463 oa.setDuration(mExpandDuration);
464 openFolderAnim = oa;
465
466 setLayerType(LAYER_TYPE_HARDWARE, null);
467 onCompleteRunnable = new Runnable() {
468 @Override
469 public void run() {
470 setLayerType(LAYER_TYPE_NONE, null);
471 }
472 };
473 } else {
474 prepareReveal();
475 centerAboutIcon();
476
477 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
478 int height = getFolderHeight();
479
480 float transX = - 0.075f * (width / 2 - getPivotX());
481 float transY = - 0.075f * (height / 2 - getPivotY());
482 setTranslationX(transX);
483 setTranslationY(transY);
484 PropertyValuesHolder tx = PropertyValuesHolder.ofFloat("translationX", transX, 0);
485 PropertyValuesHolder ty = PropertyValuesHolder.ofFloat("translationY", transY, 0);
486
Sunny Goyal75deaf32015-05-07 16:13:12 -0700487 Animator drift = LauncherAnimUtils.ofPropertyValuesHolder(this, tx, ty);
488 drift.setDuration(mMaterialExpandDuration);
489 drift.setStartDelay(mMaterialExpandStagger);
490 drift.setInterpolator(new LogDecelerateInterpolator(100, 0));
491
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700492 int rx = (int) Math.max(Math.max(width - getPivotX(), 0), getPivotX());
493 int ry = (int) Math.max(Math.max(height - getPivotY(), 0), getPivotY());
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700494 float radius = (float) Math.hypot(rx, ry);
Sunny Goyal75deaf32015-05-07 16:13:12 -0700495
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700496 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
497 Animator reveal = LauncherAnimUtils.createCircularReveal(this, (int) getPivotX(),
498 (int) getPivotY(), 0, radius);
499 reveal.setDuration(mMaterialExpandDuration);
500 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
501
Sunny Goyalbc753352015-03-05 09:40:44 -0800502 mContentWrapper.setAlpha(0f);
503 Animator iconsAlpha = LauncherAnimUtils.ofFloat(mContentWrapper, "alpha", 0f, 1f);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700504 iconsAlpha.setDuration(mMaterialExpandDuration);
505 iconsAlpha.setStartDelay(mMaterialExpandStagger);
506 iconsAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
507
Sunny Goyal290800b2015-03-05 11:33:33 -0800508 mFooter.setAlpha(0f);
509 Animator textAlpha = LauncherAnimUtils.ofFloat(mFooter, "alpha", 0f, 1f);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700510 textAlpha.setDuration(mMaterialExpandDuration);
511 textAlpha.setStartDelay(mMaterialExpandStagger);
512 textAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
513
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700514
515 anim.play(drift);
516 anim.play(iconsAlpha);
517 anim.play(textAlpha);
518 anim.play(reveal);
519
520 openFolderAnim = anim;
521
Sunny Goyalbc753352015-03-05 09:40:44 -0800522 mContentWrapper.setLayerType(LAYER_TYPE_HARDWARE, null);
Sunny Goyal75deaf32015-05-07 16:13:12 -0700523 mFooter.setLayerType(LAYER_TYPE_HARDWARE, null);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700524 onCompleteRunnable = new Runnable() {
525 @Override
526 public void run() {
Sunny Goyalbc753352015-03-05 09:40:44 -0800527 mContentWrapper.setLayerType(LAYER_TYPE_NONE, null);
Sunny Goyal75deaf32015-05-07 16:13:12 -0700528 mContentWrapper.setLayerType(LAYER_TYPE_NONE, null);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700529 }
530 };
531 }
532 openFolderAnim.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700533 @Override
534 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700535 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Sunny Goyalbc753352015-03-05 09:40:44 -0800536 mContent.getAccessibilityDescription());
Adam Cohendf2cc412011-04-27 16:56:57 -0700537 mState = STATE_ANIMATING;
538 }
539 @Override
540 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700541 mState = STATE_OPEN;
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700542
543 if (onCompleteRunnable != null) {
544 onCompleteRunnable.run();
545 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800546
Sunny Goyalbc753352015-03-05 09:40:44 -0800547 mContent.setFocusOnFirstChild();
Adam Cohendf2cc412011-04-27 16:56:57 -0700548 }
549 });
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700550 openFolderAnim.start();
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800551
552 // Make sure the folder picks up the last drag move even if the finger doesn't move.
553 if (mDragController.isDragging()) {
554 mDragController.forceTouchMove();
555 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700556
Sunny Goyalb8634152015-04-09 14:17:14 -0700557 FolderPagedView pages = (FolderPagedView) mContent;
558 pages.verifyVisibleHighResIcons(pages.getNextPage());
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800559 }
560
561 public void beginExternalDrag(ShortcutInfo item) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800562 mCurrentDragInfo = item;
Sunny Goyal5d85c442015-03-10 13:14:47 -0700563 mEmptyCellRank = mContent.allocateRankForNewItem(item);
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800564 mIsExternalDrag = true;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800565 mDragInProgress = true;
Sunny Goyalb8634152015-04-09 14:17:14 -0700566
Sunny Goyalf4066152015-04-15 09:42:19 -0700567 // Since this folder opened by another controller, it might not get onDrop or
568 // onDropComplete. Perform cleanup once drag-n-drop ends.
569 mDragController.addDragListener(this);
570 }
571
572 @Override
573 public void onDragStart(DragSource source, Object info, int dragAction) { }
574
575 @Override
576 public void onDragEnd() {
577 if (mIsExternalDrag && mDragInProgress) {
578 completeDragExit();
579 }
580 mDragController.removeDragListener(this);
Adam Cohendf2cc412011-04-27 16:56:57 -0700581 }
582
Adam Cohen091440a2015-03-18 14:16:05 -0700583 @Thunk void sendCustomAccessibilityEvent(int type, String text) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700584 AccessibilityManager accessibilityManager = (AccessibilityManager)
585 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
586 if (accessibilityManager.isEnabled()) {
Adam Cohen3371da02011-10-25 21:38:29 -0700587 AccessibilityEvent event = AccessibilityEvent.obtain(type);
588 onInitializeAccessibilityEvent(event);
589 event.getText().add(text);
Michael Jurka8b805b12012-04-18 14:23:14 -0700590 accessibilityManager.sendAccessibilityEvent(event);
Adam Cohen3371da02011-10-25 21:38:29 -0700591 }
592 }
593
Adam Cohendf2cc412011-04-27 16:56:57 -0700594 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700595 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800596 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
597 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
598 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
Michael Jurka032e6ba2013-04-22 15:08:42 +0200599 final ObjectAnimator oa =
Michael Jurka2ecf9952012-06-18 12:52:28 -0700600 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohendf2cc412011-04-27 16:56:57 -0700601
Adam Cohen2801caf2011-05-13 20:57:39 -0700602 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700603 @Override
604 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700605 onCloseComplete();
Michael Jurka0121c3e2012-05-31 08:36:04 -0700606 setLayerType(LAYER_TYPE_NONE, null);
Adam Cohen2801caf2011-05-13 20:57:39 -0700607 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700608 }
609 @Override
610 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700611 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700612 getContext().getString(R.string.folder_closed));
Adam Cohendf2cc412011-04-27 16:56:57 -0700613 mState = STATE_ANIMATING;
614 }
615 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700616 oa.setDuration(mExpandDuration);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700617 setLayerType(LAYER_TYPE_HARDWARE, null);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100618 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700619 }
620
Adam Cohencb3382b2011-05-24 14:07:08 -0700621 public boolean acceptDrop(DragObject d) {
622 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700623 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700624 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
625 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
626 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700627 }
628
Adam Cohencb3382b2011-05-24 14:07:08 -0700629 public void onDragEnter(DragObject d) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800630 mPrevTargetRank = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700631 mOnExitAlarm.cancelAlarm();
Sunny Goyalb8634152015-04-09 14:17:14 -0700632 // Get the area offset such that the folder only closes if half the drag icon width
633 // is outside the folder area
634 mScrollAreaOffset = d.dragView.getDragRegionWidth() / 2 - d.xOffset;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700635 }
636
637 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
638 public void onAlarm(Alarm alarm) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800639 mContent.realTimeReorder(mEmptyCellRank, mTargetRank);
640 mEmptyCellRank = mTargetRank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700641 }
642 };
643
Sunny Goyalc46bfef2015-01-05 12:40:08 -0800644 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohen2374abf2013-04-16 14:56:57 -0700645 public boolean isLayoutRtl() {
646 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
647 }
648
Sunny Goyal48461932015-03-09 17:41:09 -0700649 @Override
Adam Cohencb3382b2011-05-24 14:07:08 -0700650 public void onDragOver(DragObject d) {
Sunny Goyal48461932015-03-09 17:41:09 -0700651 onDragOver(d, REORDER_DELAY);
652 }
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700653
Sunny Goyal48461932015-03-09 17:41:09 -0700654 private int getTargetRank(DragObject d, float[] recycle) {
655 recycle = d.getVisualCenter(recycle);
656 return mContent.findNearestArea(
657 (int) recycle[0] - getPaddingLeft(), (int) recycle[1] - getPaddingTop());
658 }
659
Adam Cohen091440a2015-03-18 14:16:05 -0700660 @Thunk void onDragOver(DragObject d, int reorderDelay) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700661 if (mScrollPauseAlarm.alarmPending()) {
Sunny Goyal48461932015-03-09 17:41:09 -0700662 return;
663 }
664 final float[] r = new float[2];
665 mTargetRank = getTargetRank(d, r);
666
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800667 if (mTargetRank != mPrevTargetRank) {
Alan Viverette4cda5b72013-08-28 17:53:41 -0700668 mReorderAlarm.cancelAlarm();
Sunny Goyalc46bfef2015-01-05 12:40:08 -0800669 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
670 mReorderAlarm.setAlarm(REORDER_DELAY);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800671 mPrevTargetRank = mTargetRank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700672 }
Sunny Goyal48461932015-03-09 17:41:09 -0700673
Sunny Goyal48461932015-03-09 17:41:09 -0700674 float x = r[0];
Sunny Goyalb8634152015-04-09 14:17:14 -0700675 int currentPage = mContent.getNextPage();
Sunny Goyal48461932015-03-09 17:41:09 -0700676
Sunny Goyalb8634152015-04-09 14:17:14 -0700677 float cellOverlap = mContent.getCurrentCellLayout().getCellWidth()
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700678 * ICON_OVERSCROLL_WIDTH_FACTOR;
679 boolean isOutsideLeftEdge = x < cellOverlap;
680 boolean isOutsideRightEdge = x > (getWidth() - cellOverlap);
Sunny Goyal48461932015-03-09 17:41:09 -0700681
Sunny Goyalb8634152015-04-09 14:17:14 -0700682 if (currentPage > 0 && (mContent.rtlLayout ? isOutsideRightEdge : isOutsideLeftEdge)) {
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700683 showScrollHint(DragController.SCROLL_LEFT, d);
Sunny Goyalb8634152015-04-09 14:17:14 -0700684 } else if (currentPage < (mContent.getPageCount() - 1)
685 && (mContent.rtlLayout ? isOutsideLeftEdge : isOutsideRightEdge)) {
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700686 showScrollHint(DragController.SCROLL_RIGHT, d);
Sunny Goyal48461932015-03-09 17:41:09 -0700687 } else {
688 mOnScrollHintAlarm.cancelAlarm();
689 if (mScrollHintDir != DragController.SCROLL_NONE) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700690 mContent.clearScrollHint();
Sunny Goyal48461932015-03-09 17:41:09 -0700691 mScrollHintDir = DragController.SCROLL_NONE;
692 }
693 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700694 }
695
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700696 private void showScrollHint(int direction, DragObject d) {
697 // Show scroll hint on the right
698 if (mScrollHintDir != direction) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700699 mContent.showScrollHint(direction);
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700700 mScrollHintDir = direction;
701 }
702
703 // Set alarm for when the hint is complete
704 if (!mOnScrollHintAlarm.alarmPending() || mCurrentScrollDir != direction) {
705 mCurrentScrollDir = direction;
706 mOnScrollHintAlarm.cancelAlarm();
707 mOnScrollHintAlarm.setOnAlarmListener(new OnScrollHintListener(d));
708 mOnScrollHintAlarm.setAlarm(SCROLL_HINT_DURATION);
709
710 mReorderAlarm.cancelAlarm();
711 mTargetRank = mEmptyCellRank;
712 }
713 }
714
Adam Cohenbfbfd262011-06-13 16:55:12 -0700715 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
716 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700717 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700718 }
719 };
720
Adam Cohen95bb8002011-07-03 23:40:28 -0700721 public void completeDragExit() {
Sunny Goyalf4066152015-04-15 09:42:19 -0700722 if (mInfo.opened) {
723 mLauncher.closeFolder();
724 mRearrangeOnClose = true;
Sunny Goyal31abc292015-05-01 10:42:32 -0700725 } else if (mState == STATE_ANIMATING) {
726 mRearrangeOnClose = true;
Sunny Goyalf4066152015-04-15 09:42:19 -0700727 } else {
728 rearrangeChildren();
Sunny Goyal31abc292015-05-01 10:42:32 -0700729 clearDragInfo();
Sunny Goyalf4066152015-04-15 09:42:19 -0700730 }
Sunny Goyal31abc292015-05-01 10:42:32 -0700731 }
732
733 private void clearDragInfo() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700734 mCurrentDragInfo = null;
735 mCurrentDragView = null;
736 mSuppressOnAdd = false;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800737 mIsExternalDrag = false;
Adam Cohen3e8f8112011-07-02 18:03:00 -0700738 }
739
Adam Cohencb3382b2011-05-24 14:07:08 -0700740 public void onDragExit(DragObject d) {
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700741 // We only close the folder if this is a true drag exit, ie. not because
742 // a drop has occurred above the folder.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700743 if (!d.dragComplete) {
744 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
745 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
746 }
747 mReorderAlarm.cancelAlarm();
Sunny Goyal48461932015-03-09 17:41:09 -0700748
Sunny Goyalb8634152015-04-09 14:17:14 -0700749 mOnScrollHintAlarm.cancelAlarm();
750 mScrollPauseAlarm.cancelAlarm();
751 if (mScrollHintDir != DragController.SCROLL_NONE) {
752 mContent.clearScrollHint();
753 mScrollHintDir = DragController.SCROLL_NONE;
Sunny Goyal48461932015-03-09 17:41:09 -0700754 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700755 }
756
Sunny Goyale9b651e2015-04-24 11:44:51 -0700757 /**
758 * When performing an accessibility drop, onDrop is sent immediately after onDragEnter. So we
759 * need to complete all transient states based on timers.
760 */
761 @Override
762 public void prepareAccessibilityDrop() {
763 if (mReorderAlarm.alarmPending()) {
764 mReorderAlarm.cancelAlarm();
765 mReorderAlarmListener.onAlarm(mReorderAlarm);
766 }
767 }
768
Michael Jurka1e2f4652013-07-08 18:03:46 -0700769 public void onDropCompleted(final View target, final DragObject d,
770 final boolean isFlingToDelete, final boolean success) {
771 if (mDeferDropAfterUninstall) {
Michael Jurkaf3007582013-08-21 14:33:57 +0200772 Log.d(TAG, "Deferred handling drop because waiting for uninstall.");
Michael Jurka1e2f4652013-07-08 18:03:46 -0700773 mDeferredAction = new Runnable() {
774 public void run() {
775 onDropCompleted(target, d, isFlingToDelete, success);
776 mDeferredAction = null;
777 }
778 };
779 return;
780 }
781
782 boolean beingCalledAfterUninstall = mDeferredAction != null;
783 boolean successfulDrop =
784 success && (!beingCalledAfterUninstall || mUninstallSuccessful);
Winson Chung5f8afe62013-08-12 16:19:28 -0700785
Michael Jurka1e2f4652013-07-08 18:03:46 -0700786 if (successfulDrop) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800787 if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon && target != this) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700788 replaceFolderWithFinalItem();
789 }
790 } else {
791 // The drag failed, we need to return the item to the folder
Sunny Goyalb8c663c2015-04-23 11:43:48 -0700792 ShortcutInfo info = (ShortcutInfo) d.dragInfo;
793 View icon = (mCurrentDragView != null && mCurrentDragView.getTag() == info)
794 ? mCurrentDragView : mContent.createNewView(info);
795 ArrayList<View> views = getItemsInReadingOrder();
796 views.add(info.rank, icon);
797 mContent.arrangeChildren(views, views.size());
798 mItemsInvalidated = true;
799
800 mSuppressOnAdd = true;
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700801 mFolderIcon.onDrop(d);
Sunny Goyalb8c663c2015-04-23 11:43:48 -0700802 mSuppressOnAdd = false;
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700803 }
804
805 if (target != this) {
806 if (mOnExitAlarm.alarmPending()) {
807 mOnExitAlarm.cancelAlarm();
Michael Jurka54554252013-08-01 12:52:23 +0200808 if (!successfulDrop) {
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700809 mSuppressFolderDeletion = true;
810 }
Sunny Goyal5d85c442015-03-10 13:14:47 -0700811 mScrollPauseAlarm.cancelAlarm();
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700812 completeDragExit();
813 }
Adam Cohen9c58d822013-12-13 17:18:10 -0800814 }
815
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700816 mDeleteFolderOnDropCompleted = false;
817 mDragInProgress = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700818 mItemAddedBackToSelfViaIcon = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700819 mCurrentDragInfo = null;
820 mCurrentDragView = null;
821 mSuppressOnAdd = false;
Adam Cohen4045eb72011-10-06 11:44:26 -0700822
823 // Reordering may have occured, and we need to save the new item locations. We do this once
824 // at the end to prevent unnecessary database operations.
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700825 updateItemLocationsInDatabaseBatch();
Adam Cohen4045eb72011-10-06 11:44:26 -0700826 }
827
Sunny Goyalfa401a12015-04-10 13:45:42 -0700828 @Override
Michael Jurka1e2f4652013-07-08 18:03:46 -0700829 public void deferCompleteDropAfterUninstallActivity() {
830 mDeferDropAfterUninstall = true;
831 }
832
Sunny Goyalfa401a12015-04-10 13:45:42 -0700833 @Override
Michael Jurka1e2f4652013-07-08 18:03:46 -0700834 public void onUninstallActivityReturned(boolean success) {
835 mDeferDropAfterUninstall = false;
836 mUninstallSuccessful = success;
837 if (mDeferredAction != null) {
838 mDeferredAction.run();
839 }
840 }
841
Winson Chunga48487a2012-03-20 16:19:37 -0700842 @Override
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800843 public float getIntrinsicIconScaleFactor() {
844 return 1f;
845 }
846
847 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800848 public boolean supportsFlingToDelete() {
849 return true;
850 }
851
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000852 @Override
853 public boolean supportsAppInfoDropTarget() {
854 return false;
855 }
856
857 @Override
858 public boolean supportsDeleteDropTarget() {
859 return true;
860 }
861
Sunny Goyalddec7342015-04-29 18:12:37 -0700862 @Override
863 public void onFlingToDelete(DragObject d, PointF vec) {
Winson Chunga48487a2012-03-20 16:19:37 -0700864 // Do nothing
865 }
866
867 @Override
868 public void onFlingToDeleteCompleted() {
869 // Do nothing
870 }
871
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700872 private void updateItemLocationsInDatabaseBatch() {
873 ArrayList<View> list = getItemsInReadingOrder();
874 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
875 for (int i = 0; i < list.size(); i++) {
876 View v = list.get(i);
877 ItemInfo info = (ItemInfo) v.getTag();
Sunny Goyal08f72612015-01-05 13:41:43 -0800878 info.rank = i;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700879 items.add(info);
880 }
881
882 LauncherModel.moveItemsInDatabase(mLauncher, items, mInfo.id, 0);
883 }
884
Adam Cohene25af792013-06-06 23:08:25 -0700885 public void addItemLocationsInDatabase() {
886 ArrayList<View> list = getItemsInReadingOrder();
887 for (int i = 0; i < list.size(); i++) {
888 View v = list.get(i);
889 ItemInfo info = (ItemInfo) v.getTag();
890 LauncherModel.addItemToDatabase(mLauncher, info, mInfo.id, 0,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700891 info.cellX, info.cellY);
Adam Cohene25af792013-06-06 23:08:25 -0700892 }
893 }
894
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700895 public void notifyDrop() {
896 if (mDragInProgress) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700897 mItemAddedBackToSelfViaIcon = true;
Adam Cohen76078c42011-06-09 15:06:52 -0700898 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700899 }
900
901 public boolean isDropEnabled() {
902 return true;
903 }
904
Adam Cohen2801caf2011-05-13 20:57:39 -0700905 public boolean isFull() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800906 return mContent.isFull();
Adam Cohen2801caf2011-05-13 20:57:39 -0700907 }
908
909 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700910 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700911
Winson Chung892c74d2013-08-22 16:15:50 -0700912 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700913 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700914 int height = getFolderHeight();
Adam Cohen2801caf2011-05-13 20:57:39 -0700915
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800916 float scale = parent.getDescendantRectRelativeToSelf(mFolderIcon, sTempRect);
Adam Cohen8e776a62011-06-28 18:10:06 -0700917
Winson Chungaf40f202013-09-18 18:26:31 -0700918 LauncherAppState app = LauncherAppState.getInstance();
919 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
920
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800921 int centerX = (int) (sTempRect.left + sTempRect.width() * scale / 2);
922 int centerY = (int) (sTempRect.top + sTempRect.height() * scale / 2);
Adam Cohen2801caf2011-05-13 20:57:39 -0700923 int centeredLeft = centerX - width / 2;
924 int centeredTop = centerY - height / 2;
Winson Chung3057b1c2013-10-10 17:35:35 -0700925 int currentPage = mLauncher.getWorkspace().getNextPage();
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800926 // In case the workspace is scrolling, we need to use the final scroll to compute
927 // the folders bounds.
928 mLauncher.getWorkspace().setFinalScrollForPageChange(currentPage);
Adam Cohen35e7e642011-07-17 14:47:18 -0700929 // We first fetch the currently visible CellLayoutChildren
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800930 CellLayout currentLayout = (CellLayout) mLauncher.getWorkspace().getChildAt(currentPage);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700931 ShortcutAndWidgetContainer boundingLayout = currentLayout.getShortcutsAndWidgets();
Adam Cohen35e7e642011-07-17 14:47:18 -0700932 Rect bounds = new Rect();
933 parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800934 // We reset the workspaces scroll
935 mLauncher.getWorkspace().resetFinalScrollForPageChange(currentPage);
Adam Cohen2801caf2011-05-13 20:57:39 -0700936
Adam Cohen35e7e642011-07-17 14:47:18 -0700937 // We need to bound the folder to the currently visible CellLayoutChildren
938 int left = Math.min(Math.max(bounds.left, centeredLeft),
939 bounds.left + bounds.width() - width);
940 int top = Math.min(Math.max(bounds.top, centeredTop),
941 bounds.top + bounds.height() - height);
Winson Chungaf40f202013-09-18 18:26:31 -0700942 if (grid.isPhone() && (grid.availableWidthPx - width) < grid.iconSizePx) {
943 // Center the folder if it is full (on phones only)
944 left = (grid.availableWidthPx - width) / 2;
945 } else if (width >= bounds.width()) {
946 // If the folder doesn't fit within the bounds, center it about the desired bounds
Adam Cohen35e7e642011-07-17 14:47:18 -0700947 left = bounds.left + (bounds.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700948 }
Adam Cohen35e7e642011-07-17 14:47:18 -0700949 if (height >= bounds.height()) {
950 top = bounds.top + (bounds.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700951 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700952
953 int folderPivotX = width / 2 + (centeredLeft - left);
954 int folderPivotY = height / 2 + (centeredTop - top);
955 setPivotX(folderPivotX);
956 setPivotY(folderPivotY);
Adam Cohen268c4752012-06-06 17:47:33 -0700957 mFolderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700958 (1.0f * folderPivotX / width));
Adam Cohen268c4752012-06-06 17:47:33 -0700959 mFolderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700960 (1.0f * folderPivotY / height));
Adam Cohen3bf84d32012-05-07 20:17:14 -0700961
Adam Cohen662b5982011-12-13 17:45:21 -0800962 lp.width = width;
963 lp.height = height;
964 lp.x = left;
965 lp.y = top;
Adam Cohen2801caf2011-05-13 20:57:39 -0700966 }
967
Adam Cohen268c4752012-06-06 17:47:33 -0700968 float getPivotXForIconAnimation() {
969 return mFolderIconPivotX;
970 }
971 float getPivotYForIconAnimation() {
972 return mFolderIconPivotY;
973 }
974
Winson Chung892c74d2013-08-22 16:15:50 -0700975 private int getContentAreaHeight() {
976 LauncherAppState app = LauncherAppState.getInstance();
977 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
978 Rect workspacePadding = grid.getWorkspacePadding(grid.isLandscape ?
979 CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
980 int maxContentAreaHeight = grid.availableHeightPx -
Winson Chung892c74d2013-08-22 16:15:50 -0700981 workspacePadding.top - workspacePadding.bottom -
Sunny Goyal290800b2015-03-05 11:33:33 -0800982 mFooterHeight;
Adam Cohen1960ea42013-11-12 11:33:14 +0000983 int height = Math.min(maxContentAreaHeight,
Winson Chung892c74d2013-08-22 16:15:50 -0700984 mContent.getDesiredHeight());
Adam Cohen1960ea42013-11-12 11:33:14 +0000985 return Math.max(height, MIN_CONTENT_DIMEN);
986 }
987
988 private int getContentAreaWidth() {
989 return Math.max(mContent.getDesiredWidth(), MIN_CONTENT_DIMEN);
Winson Chung892c74d2013-08-22 16:15:50 -0700990 }
991
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700992 private int getFolderHeight() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800993 return getFolderHeight(getContentAreaHeight());
994 }
995
996 private int getFolderHeight(int contentAreaHeight) {
Sunny Goyal290800b2015-03-05 11:33:33 -0800997 return getPaddingTop() + getPaddingBottom() + contentAreaHeight + mFooterHeight;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700998 }
Adam Cohen234c4cd2011-07-17 21:03:04 -0700999
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001000 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001001 int contentWidth = getContentAreaWidth();
1002 int contentHeight = getContentAreaHeight();
Adam Cohen1960ea42013-11-12 11:33:14 +00001003
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001004 int contentAreaWidthSpec = MeasureSpec.makeMeasureSpec(contentWidth, MeasureSpec.EXACTLY);
1005 int contentAreaHeightSpec = MeasureSpec.makeMeasureSpec(contentHeight, MeasureSpec.EXACTLY);
1006
1007 mContent.setFixedSize(contentWidth, contentHeight);
1008 mContentWrapper.measure(contentAreaWidthSpec, contentAreaHeightSpec);
Sunny Goyal290800b2015-03-05 11:33:33 -08001009 mFooter.measure(contentAreaWidthSpec,
1010 MeasureSpec.makeMeasureSpec(mFooterHeight, MeasureSpec.EXACTLY));
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001011
1012 int folderWidth = getPaddingLeft() + getPaddingRight() + contentWidth;
1013 int folderHeight = getFolderHeight(contentHeight);
1014 setMeasuredDimension(folderWidth, folderHeight);
Adam Cohen234c4cd2011-07-17 21:03:04 -07001015 }
1016
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001017 /**
1018 * Rearranges the children based on their rank.
1019 */
1020 public void rearrangeChildren() {
1021 rearrangeChildren(-1);
1022 }
Adam Cohen2801caf2011-05-13 20:57:39 -07001023
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001024 /**
1025 * Rearranges the children based on their rank.
1026 * @param itemCount if greater than the total children count, empty spaces are left at the end,
1027 * otherwise it is ignored.
1028 */
1029 public void rearrangeChildren(int itemCount) {
1030 ArrayList<View> views = getItemsInReadingOrder();
1031 mContent.arrangeChildren(views, Math.max(itemCount, views.size()));
Adam Cohen7c693212011-05-18 15:26:57 -07001032 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -07001033 }
1034
Sunny Goyalc4918352015-03-10 18:15:48 -07001035 // TODO remove this once GSA code fix is submitted
1036 public ViewGroup getContent() {
1037 return (ViewGroup) mContent;
1038 }
1039
Adam Cohena9cf38f2011-05-02 15:36:58 -07001040 public int getItemCount() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001041 return mContent.getItemCount();
Adam Cohena9cf38f2011-05-02 15:36:58 -07001042 }
1043
Adam Cohen091440a2015-03-18 14:16:05 -07001044 @Thunk void onCloseComplete() {
Adam Cohen05e0f402011-08-01 12:12:49 -07001045 DragLayer parent = (DragLayer) getParent();
Michael Jurka5649c282012-06-18 10:33:21 -07001046 if (parent != null) {
1047 parent.removeView(this);
1048 }
Adam Cohen4554ee12011-08-03 16:13:21 -07001049 mDragController.removeDropTarget((DropTarget) this);
Adam Cohen05e0f402011-08-01 12:12:49 -07001050 clearFocus();
Adam Cohenac56cff2011-09-28 20:45:37 -07001051 mFolderIcon.requestFocus();
Adam Cohen05e0f402011-08-01 12:12:49 -07001052
Adam Cohen2801caf2011-05-13 20:57:39 -07001053 if (mRearrangeOnClose) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001054 rearrangeChildren();
Adam Cohen2801caf2011-05-13 20:57:39 -07001055 mRearrangeOnClose = false;
1056 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001057 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001058 if (!mDragInProgress && !mSuppressFolderDeletion) {
1059 replaceFolderWithFinalItem();
1060 } else if (mDragInProgress) {
1061 mDeleteFolderOnDropCompleted = true;
1062 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001063 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001064 mSuppressFolderDeletion = false;
Sunny Goyal31abc292015-05-01 10:42:32 -07001065 clearDragInfo();
Adam Cohenafb01ee2011-06-23 15:38:03 -07001066 }
1067
Adam Cohen091440a2015-03-18 14:16:05 -07001068 @Thunk void replaceFolderWithFinalItem() {
Adam Cohenafb01ee2011-06-23 15:38:03 -07001069 // Add the last remaining child to the workspace in place of the folder
Adam Cohenfb91f302012-06-11 15:45:18 -07001070 Runnable onCompleteRunnable = new Runnable() {
1071 @Override
1072 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -07001073 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screenId);
Adam Cohenafb01ee2011-06-23 15:38:03 -07001074
Winson Chung33231f52013-12-09 16:57:45 -08001075 View child = null;
Adam Cohenfb91f302012-06-11 15:45:18 -07001076 // Move the item from the folder to the workspace, in the position of the folder
1077 if (getItemCount() == 1) {
1078 ShortcutInfo finalItem = mInfo.contents.get(0);
Adam Cohenc5e63f32012-07-12 16:16:57 -07001079 child = mLauncher.createShortcut(R.layout.application, cellLayout,
Adam Cohenfb91f302012-06-11 15:45:18 -07001080 finalItem);
1081 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -07001082 mInfo.screenId, mInfo.cellX, mInfo.cellY);
Adam Cohenfb91f302012-06-11 15:45:18 -07001083 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001084 if (getItemCount() <= 1) {
1085 // Remove the folder
1086 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
Dan Sandler0eb687f2014-01-10 13:24:55 -05001087 if (cellLayout != null) {
1088 // b/12446428 -- sometimes the cell layout has already gone away?
1089 cellLayout.removeView(mFolderIcon);
1090 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001091 if (mFolderIcon instanceof DropTarget) {
1092 mDragController.removeDropTarget((DropTarget) mFolderIcon);
1093 }
1094 mLauncher.removeFolder(mInfo);
1095 }
Adam Cohenc5e63f32012-07-12 16:16:57 -07001096 // We add the child after removing the folder to prevent both from existing at
Winson Chung0e6a7132013-08-23 12:55:10 -07001097 // the same time in the CellLayout. We need to add the new item with addInScreenFromBind()
1098 // to ensure that hotseat items are placed correctly.
Adam Cohenc5e63f32012-07-12 16:16:57 -07001099 if (child != null) {
Winson Chung0e6a7132013-08-23 12:55:10 -07001100 mLauncher.getWorkspace().addInScreenFromBind(child, mInfo.container, mInfo.screenId,
Adam Cohenc5e63f32012-07-12 16:16:57 -07001101 mInfo.cellX, mInfo.cellY, mInfo.spanX, mInfo.spanY);
1102 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001103 }
1104 };
Sunny Goyalbc753352015-03-05 09:40:44 -08001105 View finalChild = mContent.getLastItem();
Adam Cohenfb91f302012-06-11 15:45:18 -07001106 if (finalChild != null) {
1107 mFolderIcon.performDestroyAnimation(finalChild, onCompleteRunnable);
Winson Chung33231f52013-12-09 16:57:45 -08001108 } else {
1109 onCompleteRunnable.run();
Adam Cohenafb01ee2011-06-23 15:38:03 -07001110 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001111 mDestroyed = true;
1112 }
1113
1114 boolean isDestroyed() {
1115 return mDestroyed;
Adam Cohen2801caf2011-05-13 20:57:39 -07001116 }
1117
Adam Cohenac56cff2011-09-28 20:45:37 -07001118 // This method keeps track of the last item in the folder for the purposes
1119 // of keyboard focus
Sunny Goyal290800b2015-03-05 11:33:33 -08001120 public void updateTextViewFocus() {
Sunny Goyalbc753352015-03-05 09:40:44 -08001121 View lastChild = mContent.getLastItem();
Adam Cohenac56cff2011-09-28 20:45:37 -07001122 if (lastChild != null) {
1123 mFolderName.setNextFocusDownId(lastChild.getId());
1124 mFolderName.setNextFocusRightId(lastChild.getId());
1125 mFolderName.setNextFocusLeftId(lastChild.getId());
1126 mFolderName.setNextFocusUpId(lastChild.getId());
1127 }
1128 }
1129
Adam Cohenbfbfd262011-06-13 16:55:12 -07001130 public void onDrop(DragObject d) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001131 Runnable cleanUpRunnable = null;
1132
Adam Cohen689ff162014-05-08 17:27:56 -07001133 // If we are coming from All Apps space, we defer removing the extra empty screen
1134 // until the folder closes
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001135 if (d.dragSource != mLauncher.getWorkspace() && !(d.dragSource instanceof Folder)) {
1136 cleanUpRunnable = new Runnable() {
1137 @Override
1138 public void run() {
Adam Cohen689ff162014-05-08 17:27:56 -07001139 mLauncher.exitSpringLoadedDragModeDelayed(true,
1140 Launcher.EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
1141 null);
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001142 }
1143 };
Adam Cohenbfbfd262011-06-13 16:55:12 -07001144 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001145
Sunny Goyalb8634152015-04-09 14:17:14 -07001146 // If the icon was dropped while the page was being scrolled, we need to compute
1147 // the target location again such that the icon is placed of the final page.
1148 if (!mContent.rankOnCurrentPage(mEmptyCellRank)) {
1149 // Reorder again.
1150 mTargetRank = getTargetRank(d, null);
Sunny Goyal48461932015-03-09 17:41:09 -07001151
Sunny Goyalb8634152015-04-09 14:17:14 -07001152 // Rearrange items immediately.
1153 mReorderAlarmListener.onAlarm(mReorderAlarm);
Sunny Goyal48461932015-03-09 17:41:09 -07001154
Sunny Goyalb8634152015-04-09 14:17:14 -07001155 mOnScrollHintAlarm.cancelAlarm();
1156 mScrollPauseAlarm.cancelAlarm();
Sunny Goyal48461932015-03-09 17:41:09 -07001157 }
Sunny Goyalb8634152015-04-09 14:17:14 -07001158 mContent.completePendingPageChanges();
Sunny Goyal48461932015-03-09 17:41:09 -07001159
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001160 View currentDragView;
1161 ShortcutInfo si = mCurrentDragInfo;
1162 if (mIsExternalDrag) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001163 currentDragView = mContent.createAndAddViewForRank(si, mEmptyCellRank);
Sunny Goyal95abbb32014-08-04 10:53:22 -07001164 // Actually move the item in the database if it was an external drag. Call this
1165 // before creating the view, so that ShortcutInfo is updated appropriately.
1166 LauncherModel.addOrMoveItemInDatabase(
1167 mLauncher, si, mInfo.id, 0, si.cellX, si.cellY);
1168
1169 // We only need to update the locations if it doesn't get handled in #onDropCompleted.
1170 if (d.dragSource != this) {
1171 updateItemLocationsInDatabaseBatch();
1172 }
1173 mIsExternalDrag = false;
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001174 } else {
1175 currentDragView = mCurrentDragView;
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001176 mContent.addViewForRank(currentDragView, si, mEmptyCellRank);
Adam Cohenbfbfd262011-06-13 16:55:12 -07001177 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001178
1179 if (d.dragView.hasDrawn()) {
1180
1181 // Temporarily reset the scale such that the animation target gets calculated correctly.
1182 float scaleX = getScaleX();
1183 float scaleY = getScaleY();
1184 setScaleX(1.0f);
1185 setScaleY(1.0f);
1186 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, currentDragView,
1187 cleanUpRunnable, null);
1188 setScaleX(scaleX);
1189 setScaleY(scaleY);
1190 } else {
1191 d.deferDragViewCleanupPostAnimation = false;
1192 currentDragView.setVisibility(VISIBLE);
1193 }
1194 mItemsInvalidated = true;
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001195 rearrangeChildren();
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001196
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001197 // Temporarily suppress the listener, as we did all the work already here.
1198 mSuppressOnAdd = true;
1199 mInfo.add(si);
1200 mSuppressOnAdd = false;
Sunny Goyal4b020172014-08-28 14:51:14 -07001201 // Clear the drag info, as it is no longer being dragged.
1202 mCurrentDragInfo = null;
Sunny Goyalf4066152015-04-15 09:42:19 -07001203 mDragInProgress = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001204 }
1205
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001206 // This is used so the item doesn't immediately appear in the folder when added. In one case
1207 // we need to create the illusion that the item isn't added back to the folder yet, to
1208 // to correspond to the animation of the icon back into the folder. This is
1209 public void hideItem(ShortcutInfo info) {
1210 View v = getViewForInfo(info);
1211 v.setVisibility(INVISIBLE);
1212 }
1213 public void showItem(ShortcutInfo info) {
1214 View v = getViewForInfo(info);
1215 v.setVisibility(VISIBLE);
1216 }
1217
Adam Cohenbfbfd262011-06-13 16:55:12 -07001218 public void onAdd(ShortcutInfo item) {
Adam Cohen05e0f402011-08-01 12:12:49 -07001219 // If the item was dropped onto this open folder, we have done the work associated
1220 // with adding the item to the folder, as indicated by mSuppressOnAdd being set
Adam Cohenbfbfd262011-06-13 16:55:12 -07001221 if (mSuppressOnAdd) return;
Sunny Goyal5d85c442015-03-10 13:14:47 -07001222 mContent.createAndAddViewForRank(item, mContent.allocateRankForNewItem(item));
Sunny Goyal2e688a82015-03-18 10:23:39 -07001223 mItemsInvalidated = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001224 LauncherModel.addOrMoveItemInDatabase(
1225 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
1226 }
1227
Adam Cohena9cf38f2011-05-02 15:36:58 -07001228 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -07001229 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001230 // If this item is being dragged from this open folder, we have already handled
1231 // the work associated with removing the item, so we don't have to do anything here.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001232 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001233 View v = getViewForInfo(item);
Sunny Goyal290800b2015-03-05 11:33:33 -08001234 mContent.removeItem(v);
Adam Cohen2801caf2011-05-13 20:57:39 -07001235 if (mState == STATE_ANIMATING) {
1236 mRearrangeOnClose = true;
1237 } else {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001238 rearrangeChildren();
Adam Cohen2801caf2011-05-13 20:57:39 -07001239 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001240 if (getItemCount() <= 1) {
1241 replaceFolderWithFinalItem();
1242 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001243 }
Adam Cohen7c693212011-05-18 15:26:57 -07001244
Sunny Goyalbc753352015-03-05 09:40:44 -08001245 private View getViewForInfo(final ShortcutInfo item) {
1246 return mContent.iterateOverItems(new ItemOperator() {
1247
1248 @Override
1249 public boolean evaluate(ItemInfo info, View view, View parent) {
1250 return info == item;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001251 }
Sunny Goyalbc753352015-03-05 09:40:44 -08001252 });
Adam Cohendf1e4e82011-06-24 15:57:39 -07001253 }
1254
Adam Cohen76078c42011-06-09 15:06:52 -07001255 public void onItemsChanged() {
Adam Cohenac56cff2011-09-28 20:45:37 -07001256 updateTextViewFocus();
Adam Cohen76078c42011-06-09 15:06:52 -07001257 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001258
Adam Cohen76fc0852011-06-17 13:26:23 -07001259 public void onTitleChanged(CharSequence title) {
1260 }
Adam Cohen76078c42011-06-09 15:06:52 -07001261
Adam Cohen7c693212011-05-18 15:26:57 -07001262 public ArrayList<View> getItemsInReadingOrder() {
1263 if (mItemsInvalidated) {
1264 mItemsInReadingOrder.clear();
Sunny Goyalbc753352015-03-05 09:40:44 -08001265 mContent.iterateOverItems(new ItemOperator() {
1266
1267 @Override
1268 public boolean evaluate(ItemInfo info, View view, View parent) {
1269 mItemsInReadingOrder.add(view);
1270 return false;
Adam Cohen7c693212011-05-18 15:26:57 -07001271 }
Sunny Goyalbc753352015-03-05 09:40:44 -08001272 });
Adam Cohen7c693212011-05-18 15:26:57 -07001273 mItemsInvalidated = false;
1274 }
1275 return mItemsInReadingOrder;
1276 }
Adam Cohen8dfcba42011-07-07 16:38:18 -07001277
1278 public void getLocationInDragLayer(int[] loc) {
1279 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
1280 }
Adam Cohenea0818d2011-09-30 20:06:58 -07001281
1282 public void onFocusChange(View v, boolean hasFocus) {
1283 if (v == mFolderName && hasFocus) {
1284 startEditingFolderName();
1285 }
1286 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001287
1288 @Override
1289 public void getHitRectRelativeToDragLayer(Rect outRect) {
1290 getHitRect(outRect);
Sunny Goyal48461932015-03-09 17:41:09 -07001291 outRect.left -= mScrollAreaOffset;
1292 outRect.right += mScrollAreaOffset;
1293 }
1294
1295 private class OnScrollHintListener implements OnAlarmListener {
1296
1297 private final DragObject mDragObject;
1298
1299 OnScrollHintListener(DragObject object) {
1300 mDragObject = object;
1301 }
1302
1303 /**
1304 * Scroll hint has been shown long enough. Now scroll to appropriate page.
1305 */
1306 @Override
1307 public void onAlarm(Alarm alarm) {
1308 if (mCurrentScrollDir == DragController.SCROLL_LEFT) {
Sunny Goyalb8634152015-04-09 14:17:14 -07001309 mContent.scrollLeft();
Sunny Goyal48461932015-03-09 17:41:09 -07001310 mScrollHintDir = DragController.SCROLL_NONE;
1311 } else if (mCurrentScrollDir == DragController.SCROLL_RIGHT) {
Sunny Goyalb8634152015-04-09 14:17:14 -07001312 mContent.scrollRight();
Sunny Goyal48461932015-03-09 17:41:09 -07001313 mScrollHintDir = DragController.SCROLL_NONE;
1314 } else {
1315 // This should not happen
1316 return;
1317 }
1318 mCurrentScrollDir = DragController.SCROLL_NONE;
1319
1320 // Pause drag event until the scrolling is finished
1321 mScrollPauseAlarm.setOnAlarmListener(new OnScrollFinishedListener(mDragObject));
1322 mScrollPauseAlarm.setAlarm(DragController.RESCROLL_DELAY);
1323 }
1324 }
1325
1326 private class OnScrollFinishedListener implements OnAlarmListener {
1327
1328 private final DragObject mDragObject;
1329
1330 OnScrollFinishedListener(DragObject object) {
1331 mDragObject = object;
1332 }
1333
1334 /**
1335 * Page scroll is complete.
1336 */
1337 @Override
1338 public void onAlarm(Alarm alarm) {
1339 // Reorder immediately on page change.
1340 onDragOver(mDragObject, 1);
1341 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001342 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001343}