blob: d98b0eb6a255a19643b8208bab9f039551d29b2b [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 }
288 mLauncher.getWorkspace().setAddNewPageOnDrag(!enable);
289 }
290
Adam Cohen76fc0852011-06-17 13:26:23 -0700291 public boolean isEditingName() {
292 return mIsEditingName;
293 }
294
295 public void startEditingFolderName() {
Adam Cohena65beee2011-06-27 21:32:23 -0700296 mFolderName.setHint("");
Adam Cohen76fc0852011-06-17 13:26:23 -0700297 mIsEditingName = true;
298 }
299
300 public void dismissEditingName() {
301 mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
302 doneEditingFolderName(true);
303 }
304
305 public void doneEditingFolderName(boolean commit) {
Adam Cohena65beee2011-06-27 21:32:23 -0700306 mFolderName.setHint(sHintText);
Adam Cohen1df26a32011-08-12 16:15:23 -0700307 // Convert to a string here to ensure that no other state associated with the text field
308 // gets saved.
Adam Cohen3371da02011-10-25 21:38:29 -0700309 String newTitle = mFolderName.getText().toString();
310 mInfo.setTitle(newTitle);
Adam Cohen76fc0852011-06-17 13:26:23 -0700311 LauncherModel.updateItemInDatabase(mLauncher, mInfo);
Adam Cohenac56cff2011-09-28 20:45:37 -0700312
Adam Cohen3371da02011-10-25 21:38:29 -0700313 if (commit) {
314 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700315 String.format(getContext().getString(R.string.folder_renamed), newTitle));
Adam Cohen3371da02011-10-25 21:38:29 -0700316 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700317 // In order to clear the focus from the text field, we set the focus on ourself. This
318 // ensures that every time the field is clicked, focus is gained, giving reliable behavior.
319 requestFocus();
320
Adam Cohene601a432011-07-26 21:51:30 -0700321 Selection.setSelection((Spannable) mFolderName.getText(), 0, 0);
Adam Cohen76fc0852011-06-17 13:26:23 -0700322 mIsEditingName = false;
323 }
324
325 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
326 if (actionId == EditorInfo.IME_ACTION_DONE) {
327 dismissEditingName();
328 return true;
329 }
330 return false;
331 }
332
333 public View getEditTextRegion() {
334 return mFolderName;
335 }
336
Adam Cohen0c872ba2011-05-05 10:34:16 -0700337 /**
338 * We need to handle touch events to prevent them from falling through to the workspace below.
339 */
340 @Override
341 public boolean onTouchEvent(MotionEvent ev) {
342 return true;
343 }
344
Joe Onorato00acb122009-08-04 16:04:30 -0400345 public void setDragController(DragController dragController) {
346 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800347 }
348
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800349 public void setFolderIcon(FolderIcon icon) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700350 mFolderIcon = icon;
351 }
352
Adam Cohen3371da02011-10-25 21:38:29 -0700353 @Override
354 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
355 // When the folder gets focus, we don't want to announce the list of items.
356 return true;
357 }
358
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800359 /**
360 * @return the FolderInfo object associated with this folder
361 */
362 FolderInfo getInfo() {
363 return mInfo;
364 }
365
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800366 void bind(FolderInfo info) {
367 mInfo = info;
Adam Cohendf2cc412011-04-27 16:56:57 -0700368 ArrayList<ShortcutInfo> children = info.contents;
Sunny Goyal08f72612015-01-05 13:41:43 -0800369 Collections.sort(children, Utilities.RANK_COMPARATOR);
Sunny Goyal08f72612015-01-05 13:41:43 -0800370
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800371 ArrayList<ShortcutInfo> overflow = mContent.bindItems(children);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700372
Jason Monk4ff73882014-04-24 16:48:07 -0400373 // If our folder has too many items we prune them from the list. This is an issue
Adam Cohenc508b2d2011-06-28 14:41:44 -0700374 // when upgrading from the old Folders implementation which could contain an unlimited
375 // number of items.
376 for (ShortcutInfo item: overflow) {
377 mInfo.remove(item);
378 LauncherModel.deleteItemFromDatabase(mLauncher, item);
379 }
380
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800381 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
382 if (lp == null) {
383 lp = new DragLayer.LayoutParams(0, 0);
384 lp.customPosition = true;
385 setLayoutParams(lp);
386 }
387 centerAboutIcon();
388
Adam Cohen4dbe6d92011-05-18 17:14:15 -0700389 mItemsInvalidated = true;
Adam Cohenac56cff2011-09-28 20:45:37 -0700390 updateTextViewFocus();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700391 mInfo.addListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700392
Adam Cohenafb01ee2011-06-23 15:38:03 -0700393 if (!sDefaultFolderName.contentEquals(mInfo.title)) {
Adam Cohen4ef610f2011-06-21 22:37:36 -0700394 mFolderName.setText(mInfo.title);
395 } else {
396 mFolderName.setText("");
397 }
Winson Chung33231f52013-12-09 16:57:45 -0800398
399 // In case any children didn't come across during loading, clean up the folder accordingly
400 mFolderIcon.post(new Runnable() {
401 public void run() {
402 if (getItemCount() <= 1) {
403 replaceFolderWithFinalItem();
404 }
405 }
406 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700407 }
408
409 /**
410 * Creates a new UserFolder, inflated from R.layout.user_folder.
411 *
412 * @param context The application's context.
413 *
414 * @return A new UserFolder.
415 */
416 static Folder fromXml(Context context) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700417 return (Folder) LayoutInflater.from(context).inflate(R.layout.user_folder, null);
Adam Cohendf2cc412011-04-27 16:56:57 -0700418 }
419
420 /**
421 * This method is intended to make the UserFolder to be visually identical in size and position
422 * to its associated FolderIcon. This allows for a seamless transition into the expanded state.
423 */
424 private void positionAndSizeAsIcon() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700425 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800426 setScaleX(0.8f);
427 setScaleY(0.8f);
428 setAlpha(0f);
Adam Cohendf2cc412011-04-27 16:56:57 -0700429 mState = STATE_SMALL;
430 }
431
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700432 private void prepareReveal() {
433 setScaleX(1f);
434 setScaleY(1f);
435 setAlpha(1f);
436 mState = STATE_SMALL;
437 }
438
Adam Cohendf2cc412011-04-27 16:56:57 -0700439 public void animateOpen() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700440 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen6441de02011-12-14 14:25:32 -0800441
Sunny Goyalb8634152015-04-09 14:17:14 -0700442 mContent.completePendingPageChanges();
Sunny Goyal8167dc22015-04-27 13:44:01 -0700443 if (!mDragInProgress) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700444 // Open on the first page.
445 mContent.snapToPageImmediately(0);
Sunny Goyal48461932015-03-09 17:41:09 -0700446 }
447
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700448 Animator openFolderAnim = null;
449 final Runnable onCompleteRunnable;
Kenny Guyd794a3f2014-09-16 15:17:58 +0100450 if (!Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700451 positionAndSizeAsIcon();
452 centerAboutIcon();
453
454 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1);
455 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
456 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
457 final ObjectAnimator oa =
458 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
459 oa.setDuration(mExpandDuration);
460 openFolderAnim = oa;
461
462 setLayerType(LAYER_TYPE_HARDWARE, null);
463 onCompleteRunnable = new Runnable() {
464 @Override
465 public void run() {
466 setLayerType(LAYER_TYPE_NONE, null);
467 }
468 };
469 } else {
470 prepareReveal();
471 centerAboutIcon();
472
473 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
474 int height = getFolderHeight();
475
476 float transX = - 0.075f * (width / 2 - getPivotX());
477 float transY = - 0.075f * (height / 2 - getPivotY());
478 setTranslationX(transX);
479 setTranslationY(transY);
480 PropertyValuesHolder tx = PropertyValuesHolder.ofFloat("translationX", transX, 0);
481 PropertyValuesHolder ty = PropertyValuesHolder.ofFloat("translationY", transY, 0);
482
Sunny Goyal75deaf32015-05-07 16:13:12 -0700483 Animator drift = LauncherAnimUtils.ofPropertyValuesHolder(this, tx, ty);
484 drift.setDuration(mMaterialExpandDuration);
485 drift.setStartDelay(mMaterialExpandStagger);
486 drift.setInterpolator(new LogDecelerateInterpolator(100, 0));
487
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700488 int rx = (int) Math.max(Math.max(width - getPivotX(), 0), getPivotX());
489 int ry = (int) Math.max(Math.max(height - getPivotY(), 0), getPivotY());
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700490 float radius = (float) Math.hypot(rx, ry);
Sunny Goyal75deaf32015-05-07 16:13:12 -0700491
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700492 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
493 Animator reveal = LauncherAnimUtils.createCircularReveal(this, (int) getPivotX(),
494 (int) getPivotY(), 0, radius);
495 reveal.setDuration(mMaterialExpandDuration);
496 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
497
Sunny Goyalbc753352015-03-05 09:40:44 -0800498 mContentWrapper.setAlpha(0f);
499 Animator iconsAlpha = LauncherAnimUtils.ofFloat(mContentWrapper, "alpha", 0f, 1f);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700500 iconsAlpha.setDuration(mMaterialExpandDuration);
501 iconsAlpha.setStartDelay(mMaterialExpandStagger);
502 iconsAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
503
Sunny Goyal290800b2015-03-05 11:33:33 -0800504 mFooter.setAlpha(0f);
505 Animator textAlpha = LauncherAnimUtils.ofFloat(mFooter, "alpha", 0f, 1f);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700506 textAlpha.setDuration(mMaterialExpandDuration);
507 textAlpha.setStartDelay(mMaterialExpandStagger);
508 textAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
509
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700510
511 anim.play(drift);
512 anim.play(iconsAlpha);
513 anim.play(textAlpha);
514 anim.play(reveal);
515
516 openFolderAnim = anim;
517
Sunny Goyalbc753352015-03-05 09:40:44 -0800518 mContentWrapper.setLayerType(LAYER_TYPE_HARDWARE, null);
Sunny Goyal75deaf32015-05-07 16:13:12 -0700519 mFooter.setLayerType(LAYER_TYPE_HARDWARE, null);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700520 onCompleteRunnable = new Runnable() {
521 @Override
522 public void run() {
Sunny Goyalbc753352015-03-05 09:40:44 -0800523 mContentWrapper.setLayerType(LAYER_TYPE_NONE, null);
Sunny Goyal75deaf32015-05-07 16:13:12 -0700524 mContentWrapper.setLayerType(LAYER_TYPE_NONE, null);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700525 }
526 };
527 }
528 openFolderAnim.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700529 @Override
530 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700531 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Sunny Goyalbc753352015-03-05 09:40:44 -0800532 mContent.getAccessibilityDescription());
Adam Cohendf2cc412011-04-27 16:56:57 -0700533 mState = STATE_ANIMATING;
534 }
535 @Override
536 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700537 mState = STATE_OPEN;
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700538
539 if (onCompleteRunnable != null) {
540 onCompleteRunnable.run();
541 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800542
Sunny Goyalbc753352015-03-05 09:40:44 -0800543 mContent.setFocusOnFirstChild();
Adam Cohendf2cc412011-04-27 16:56:57 -0700544 }
545 });
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700546 openFolderAnim.start();
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800547
548 // Make sure the folder picks up the last drag move even if the finger doesn't move.
549 if (mDragController.isDragging()) {
550 mDragController.forceTouchMove();
551 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700552
Sunny Goyalb8634152015-04-09 14:17:14 -0700553 FolderPagedView pages = (FolderPagedView) mContent;
554 pages.verifyVisibleHighResIcons(pages.getNextPage());
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800555 }
556
557 public void beginExternalDrag(ShortcutInfo item) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800558 mCurrentDragInfo = item;
Sunny Goyal5d85c442015-03-10 13:14:47 -0700559 mEmptyCellRank = mContent.allocateRankForNewItem(item);
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800560 mIsExternalDrag = true;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800561 mDragInProgress = true;
Sunny Goyalb8634152015-04-09 14:17:14 -0700562
Sunny Goyalf4066152015-04-15 09:42:19 -0700563 // Since this folder opened by another controller, it might not get onDrop or
564 // onDropComplete. Perform cleanup once drag-n-drop ends.
565 mDragController.addDragListener(this);
566 }
567
568 @Override
569 public void onDragStart(DragSource source, Object info, int dragAction) { }
570
571 @Override
572 public void onDragEnd() {
573 if (mIsExternalDrag && mDragInProgress) {
574 completeDragExit();
575 }
576 mDragController.removeDragListener(this);
Adam Cohendf2cc412011-04-27 16:56:57 -0700577 }
578
Adam Cohen091440a2015-03-18 14:16:05 -0700579 @Thunk void sendCustomAccessibilityEvent(int type, String text) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700580 AccessibilityManager accessibilityManager = (AccessibilityManager)
581 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
582 if (accessibilityManager.isEnabled()) {
Adam Cohen3371da02011-10-25 21:38:29 -0700583 AccessibilityEvent event = AccessibilityEvent.obtain(type);
584 onInitializeAccessibilityEvent(event);
585 event.getText().add(text);
Michael Jurka8b805b12012-04-18 14:23:14 -0700586 accessibilityManager.sendAccessibilityEvent(event);
Adam Cohen3371da02011-10-25 21:38:29 -0700587 }
588 }
589
Adam Cohendf2cc412011-04-27 16:56:57 -0700590 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700591 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800592 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
593 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
594 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
Michael Jurka032e6ba2013-04-22 15:08:42 +0200595 final ObjectAnimator oa =
Michael Jurka2ecf9952012-06-18 12:52:28 -0700596 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohendf2cc412011-04-27 16:56:57 -0700597
Adam Cohen2801caf2011-05-13 20:57:39 -0700598 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700599 @Override
600 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700601 onCloseComplete();
Michael Jurka0121c3e2012-05-31 08:36:04 -0700602 setLayerType(LAYER_TYPE_NONE, null);
Adam Cohen2801caf2011-05-13 20:57:39 -0700603 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700604 }
605 @Override
606 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700607 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700608 getContext().getString(R.string.folder_closed));
Adam Cohendf2cc412011-04-27 16:56:57 -0700609 mState = STATE_ANIMATING;
610 }
611 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700612 oa.setDuration(mExpandDuration);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700613 setLayerType(LAYER_TYPE_HARDWARE, null);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100614 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700615 }
616
Adam Cohencb3382b2011-05-24 14:07:08 -0700617 public boolean acceptDrop(DragObject d) {
618 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700619 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700620 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
621 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
622 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700623 }
624
Adam Cohencb3382b2011-05-24 14:07:08 -0700625 public void onDragEnter(DragObject d) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800626 mPrevTargetRank = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700627 mOnExitAlarm.cancelAlarm();
Sunny Goyalb8634152015-04-09 14:17:14 -0700628 // Get the area offset such that the folder only closes if half the drag icon width
629 // is outside the folder area
630 mScrollAreaOffset = d.dragView.getDragRegionWidth() / 2 - d.xOffset;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700631 }
632
633 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
634 public void onAlarm(Alarm alarm) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800635 mContent.realTimeReorder(mEmptyCellRank, mTargetRank);
636 mEmptyCellRank = mTargetRank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700637 }
638 };
639
Sunny Goyalc46bfef2015-01-05 12:40:08 -0800640 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohen2374abf2013-04-16 14:56:57 -0700641 public boolean isLayoutRtl() {
642 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
643 }
644
Sunny Goyal48461932015-03-09 17:41:09 -0700645 @Override
Adam Cohencb3382b2011-05-24 14:07:08 -0700646 public void onDragOver(DragObject d) {
Sunny Goyal48461932015-03-09 17:41:09 -0700647 onDragOver(d, REORDER_DELAY);
648 }
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700649
Sunny Goyal48461932015-03-09 17:41:09 -0700650 private int getTargetRank(DragObject d, float[] recycle) {
651 recycle = d.getVisualCenter(recycle);
652 return mContent.findNearestArea(
653 (int) recycle[0] - getPaddingLeft(), (int) recycle[1] - getPaddingTop());
654 }
655
Adam Cohen091440a2015-03-18 14:16:05 -0700656 @Thunk void onDragOver(DragObject d, int reorderDelay) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700657 if (mScrollPauseAlarm.alarmPending()) {
Sunny Goyal48461932015-03-09 17:41:09 -0700658 return;
659 }
660 final float[] r = new float[2];
661 mTargetRank = getTargetRank(d, r);
662
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800663 if (mTargetRank != mPrevTargetRank) {
Alan Viverette4cda5b72013-08-28 17:53:41 -0700664 mReorderAlarm.cancelAlarm();
Sunny Goyalc46bfef2015-01-05 12:40:08 -0800665 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
666 mReorderAlarm.setAlarm(REORDER_DELAY);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800667 mPrevTargetRank = mTargetRank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700668 }
Sunny Goyal48461932015-03-09 17:41:09 -0700669
Sunny Goyal48461932015-03-09 17:41:09 -0700670 float x = r[0];
Sunny Goyalb8634152015-04-09 14:17:14 -0700671 int currentPage = mContent.getNextPage();
Sunny Goyal48461932015-03-09 17:41:09 -0700672
Sunny Goyalb8634152015-04-09 14:17:14 -0700673 float cellOverlap = mContent.getCurrentCellLayout().getCellWidth()
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700674 * ICON_OVERSCROLL_WIDTH_FACTOR;
675 boolean isOutsideLeftEdge = x < cellOverlap;
676 boolean isOutsideRightEdge = x > (getWidth() - cellOverlap);
Sunny Goyal48461932015-03-09 17:41:09 -0700677
Sunny Goyalb8634152015-04-09 14:17:14 -0700678 if (currentPage > 0 && (mContent.rtlLayout ? isOutsideRightEdge : isOutsideLeftEdge)) {
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700679 showScrollHint(DragController.SCROLL_LEFT, d);
Sunny Goyalb8634152015-04-09 14:17:14 -0700680 } else if (currentPage < (mContent.getPageCount() - 1)
681 && (mContent.rtlLayout ? isOutsideLeftEdge : isOutsideRightEdge)) {
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700682 showScrollHint(DragController.SCROLL_RIGHT, d);
Sunny Goyal48461932015-03-09 17:41:09 -0700683 } else {
684 mOnScrollHintAlarm.cancelAlarm();
685 if (mScrollHintDir != DragController.SCROLL_NONE) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700686 mContent.clearScrollHint();
Sunny Goyal48461932015-03-09 17:41:09 -0700687 mScrollHintDir = DragController.SCROLL_NONE;
688 }
689 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700690 }
691
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700692 private void showScrollHint(int direction, DragObject d) {
693 // Show scroll hint on the right
694 if (mScrollHintDir != direction) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700695 mContent.showScrollHint(direction);
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700696 mScrollHintDir = direction;
697 }
698
699 // Set alarm for when the hint is complete
700 if (!mOnScrollHintAlarm.alarmPending() || mCurrentScrollDir != direction) {
701 mCurrentScrollDir = direction;
702 mOnScrollHintAlarm.cancelAlarm();
703 mOnScrollHintAlarm.setOnAlarmListener(new OnScrollHintListener(d));
704 mOnScrollHintAlarm.setAlarm(SCROLL_HINT_DURATION);
705
706 mReorderAlarm.cancelAlarm();
707 mTargetRank = mEmptyCellRank;
708 }
709 }
710
Adam Cohenbfbfd262011-06-13 16:55:12 -0700711 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
712 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700713 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700714 }
715 };
716
Adam Cohen95bb8002011-07-03 23:40:28 -0700717 public void completeDragExit() {
Sunny Goyalf4066152015-04-15 09:42:19 -0700718 if (mInfo.opened) {
719 mLauncher.closeFolder();
720 mRearrangeOnClose = true;
Sunny Goyal31abc292015-05-01 10:42:32 -0700721 } else if (mState == STATE_ANIMATING) {
722 mRearrangeOnClose = true;
Sunny Goyalf4066152015-04-15 09:42:19 -0700723 } else {
724 rearrangeChildren();
Sunny Goyal31abc292015-05-01 10:42:32 -0700725 clearDragInfo();
Sunny Goyalf4066152015-04-15 09:42:19 -0700726 }
Sunny Goyal31abc292015-05-01 10:42:32 -0700727 }
728
729 private void clearDragInfo() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700730 mCurrentDragInfo = null;
731 mCurrentDragView = null;
732 mSuppressOnAdd = false;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800733 mIsExternalDrag = false;
Adam Cohen3e8f8112011-07-02 18:03:00 -0700734 }
735
Adam Cohencb3382b2011-05-24 14:07:08 -0700736 public void onDragExit(DragObject d) {
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700737 // We only close the folder if this is a true drag exit, ie. not because
738 // a drop has occurred above the folder.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700739 if (!d.dragComplete) {
740 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
741 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
742 }
743 mReorderAlarm.cancelAlarm();
Sunny Goyal48461932015-03-09 17:41:09 -0700744
Sunny Goyalb8634152015-04-09 14:17:14 -0700745 mOnScrollHintAlarm.cancelAlarm();
746 mScrollPauseAlarm.cancelAlarm();
747 if (mScrollHintDir != DragController.SCROLL_NONE) {
748 mContent.clearScrollHint();
749 mScrollHintDir = DragController.SCROLL_NONE;
Sunny Goyal48461932015-03-09 17:41:09 -0700750 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700751 }
752
Sunny Goyale9b651e2015-04-24 11:44:51 -0700753 /**
754 * When performing an accessibility drop, onDrop is sent immediately after onDragEnter. So we
755 * need to complete all transient states based on timers.
756 */
757 @Override
758 public void prepareAccessibilityDrop() {
759 if (mReorderAlarm.alarmPending()) {
760 mReorderAlarm.cancelAlarm();
761 mReorderAlarmListener.onAlarm(mReorderAlarm);
762 }
763 }
764
Michael Jurka1e2f4652013-07-08 18:03:46 -0700765 public void onDropCompleted(final View target, final DragObject d,
766 final boolean isFlingToDelete, final boolean success) {
767 if (mDeferDropAfterUninstall) {
Michael Jurkaf3007582013-08-21 14:33:57 +0200768 Log.d(TAG, "Deferred handling drop because waiting for uninstall.");
Michael Jurka1e2f4652013-07-08 18:03:46 -0700769 mDeferredAction = new Runnable() {
770 public void run() {
771 onDropCompleted(target, d, isFlingToDelete, success);
772 mDeferredAction = null;
773 }
774 };
775 return;
776 }
777
778 boolean beingCalledAfterUninstall = mDeferredAction != null;
779 boolean successfulDrop =
780 success && (!beingCalledAfterUninstall || mUninstallSuccessful);
Winson Chung5f8afe62013-08-12 16:19:28 -0700781
Michael Jurka1e2f4652013-07-08 18:03:46 -0700782 if (successfulDrop) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800783 if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon && target != this) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700784 replaceFolderWithFinalItem();
785 }
786 } else {
787 // The drag failed, we need to return the item to the folder
Sunny Goyalb8c663c2015-04-23 11:43:48 -0700788 ShortcutInfo info = (ShortcutInfo) d.dragInfo;
789 View icon = (mCurrentDragView != null && mCurrentDragView.getTag() == info)
790 ? mCurrentDragView : mContent.createNewView(info);
791 ArrayList<View> views = getItemsInReadingOrder();
792 views.add(info.rank, icon);
793 mContent.arrangeChildren(views, views.size());
794 mItemsInvalidated = true;
795
796 mSuppressOnAdd = true;
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700797 mFolderIcon.onDrop(d);
Sunny Goyalb8c663c2015-04-23 11:43:48 -0700798 mSuppressOnAdd = false;
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700799 }
800
801 if (target != this) {
802 if (mOnExitAlarm.alarmPending()) {
803 mOnExitAlarm.cancelAlarm();
Michael Jurka54554252013-08-01 12:52:23 +0200804 if (!successfulDrop) {
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700805 mSuppressFolderDeletion = true;
806 }
Sunny Goyal5d85c442015-03-10 13:14:47 -0700807 mScrollPauseAlarm.cancelAlarm();
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700808 completeDragExit();
809 }
Adam Cohen9c58d822013-12-13 17:18:10 -0800810 }
811
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700812 mDeleteFolderOnDropCompleted = false;
813 mDragInProgress = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700814 mItemAddedBackToSelfViaIcon = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700815 mCurrentDragInfo = null;
816 mCurrentDragView = null;
817 mSuppressOnAdd = false;
Adam Cohen4045eb72011-10-06 11:44:26 -0700818
819 // Reordering may have occured, and we need to save the new item locations. We do this once
820 // at the end to prevent unnecessary database operations.
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700821 updateItemLocationsInDatabaseBatch();
Adam Cohen4045eb72011-10-06 11:44:26 -0700822 }
823
Sunny Goyalfa401a12015-04-10 13:45:42 -0700824 @Override
Michael Jurka1e2f4652013-07-08 18:03:46 -0700825 public void deferCompleteDropAfterUninstallActivity() {
826 mDeferDropAfterUninstall = true;
827 }
828
Sunny Goyalfa401a12015-04-10 13:45:42 -0700829 @Override
Michael Jurka1e2f4652013-07-08 18:03:46 -0700830 public void onUninstallActivityReturned(boolean success) {
831 mDeferDropAfterUninstall = false;
832 mUninstallSuccessful = success;
833 if (mDeferredAction != null) {
834 mDeferredAction.run();
835 }
836 }
837
Winson Chunga48487a2012-03-20 16:19:37 -0700838 @Override
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800839 public float getIntrinsicIconScaleFactor() {
840 return 1f;
841 }
842
843 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800844 public boolean supportsFlingToDelete() {
845 return true;
846 }
847
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000848 @Override
849 public boolean supportsAppInfoDropTarget() {
850 return false;
851 }
852
853 @Override
854 public boolean supportsDeleteDropTarget() {
855 return true;
856 }
857
Sunny Goyalddec7342015-04-29 18:12:37 -0700858 @Override
859 public void onFlingToDelete(DragObject d, PointF vec) {
Winson Chunga48487a2012-03-20 16:19:37 -0700860 // Do nothing
861 }
862
863 @Override
864 public void onFlingToDeleteCompleted() {
865 // Do nothing
866 }
867
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700868 private void updateItemLocationsInDatabaseBatch() {
869 ArrayList<View> list = getItemsInReadingOrder();
870 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
871 for (int i = 0; i < list.size(); i++) {
872 View v = list.get(i);
873 ItemInfo info = (ItemInfo) v.getTag();
Sunny Goyal08f72612015-01-05 13:41:43 -0800874 info.rank = i;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700875 items.add(info);
876 }
877
878 LauncherModel.moveItemsInDatabase(mLauncher, items, mInfo.id, 0);
879 }
880
Adam Cohene25af792013-06-06 23:08:25 -0700881 public void addItemLocationsInDatabase() {
882 ArrayList<View> list = getItemsInReadingOrder();
883 for (int i = 0; i < list.size(); i++) {
884 View v = list.get(i);
885 ItemInfo info = (ItemInfo) v.getTag();
886 LauncherModel.addItemToDatabase(mLauncher, info, mInfo.id, 0,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700887 info.cellX, info.cellY);
Adam Cohene25af792013-06-06 23:08:25 -0700888 }
889 }
890
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700891 public void notifyDrop() {
892 if (mDragInProgress) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700893 mItemAddedBackToSelfViaIcon = true;
Adam Cohen76078c42011-06-09 15:06:52 -0700894 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700895 }
896
897 public boolean isDropEnabled() {
898 return true;
899 }
900
Adam Cohen2801caf2011-05-13 20:57:39 -0700901 public boolean isFull() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800902 return mContent.isFull();
Adam Cohen2801caf2011-05-13 20:57:39 -0700903 }
904
905 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700906 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700907
Winson Chung892c74d2013-08-22 16:15:50 -0700908 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700909 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700910 int height = getFolderHeight();
Adam Cohen2801caf2011-05-13 20:57:39 -0700911
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800912 float scale = parent.getDescendantRectRelativeToSelf(mFolderIcon, sTempRect);
Adam Cohen8e776a62011-06-28 18:10:06 -0700913
Winson Chungaf40f202013-09-18 18:26:31 -0700914 LauncherAppState app = LauncherAppState.getInstance();
915 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
916
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800917 int centerX = (int) (sTempRect.left + sTempRect.width() * scale / 2);
918 int centerY = (int) (sTempRect.top + sTempRect.height() * scale / 2);
Adam Cohen2801caf2011-05-13 20:57:39 -0700919 int centeredLeft = centerX - width / 2;
920 int centeredTop = centerY - height / 2;
Winson Chung3057b1c2013-10-10 17:35:35 -0700921 int currentPage = mLauncher.getWorkspace().getNextPage();
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800922 // In case the workspace is scrolling, we need to use the final scroll to compute
923 // the folders bounds.
924 mLauncher.getWorkspace().setFinalScrollForPageChange(currentPage);
Adam Cohen35e7e642011-07-17 14:47:18 -0700925 // We first fetch the currently visible CellLayoutChildren
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800926 CellLayout currentLayout = (CellLayout) mLauncher.getWorkspace().getChildAt(currentPage);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700927 ShortcutAndWidgetContainer boundingLayout = currentLayout.getShortcutsAndWidgets();
Adam Cohen35e7e642011-07-17 14:47:18 -0700928 Rect bounds = new Rect();
929 parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800930 // We reset the workspaces scroll
931 mLauncher.getWorkspace().resetFinalScrollForPageChange(currentPage);
Adam Cohen2801caf2011-05-13 20:57:39 -0700932
Adam Cohen35e7e642011-07-17 14:47:18 -0700933 // We need to bound the folder to the currently visible CellLayoutChildren
934 int left = Math.min(Math.max(bounds.left, centeredLeft),
935 bounds.left + bounds.width() - width);
936 int top = Math.min(Math.max(bounds.top, centeredTop),
937 bounds.top + bounds.height() - height);
Winson Chungaf40f202013-09-18 18:26:31 -0700938 if (grid.isPhone() && (grid.availableWidthPx - width) < grid.iconSizePx) {
939 // Center the folder if it is full (on phones only)
940 left = (grid.availableWidthPx - width) / 2;
941 } else if (width >= bounds.width()) {
942 // If the folder doesn't fit within the bounds, center it about the desired bounds
Adam Cohen35e7e642011-07-17 14:47:18 -0700943 left = bounds.left + (bounds.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700944 }
Adam Cohen35e7e642011-07-17 14:47:18 -0700945 if (height >= bounds.height()) {
946 top = bounds.top + (bounds.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700947 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700948
949 int folderPivotX = width / 2 + (centeredLeft - left);
950 int folderPivotY = height / 2 + (centeredTop - top);
951 setPivotX(folderPivotX);
952 setPivotY(folderPivotY);
Adam Cohen268c4752012-06-06 17:47:33 -0700953 mFolderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700954 (1.0f * folderPivotX / width));
Adam Cohen268c4752012-06-06 17:47:33 -0700955 mFolderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700956 (1.0f * folderPivotY / height));
Adam Cohen3bf84d32012-05-07 20:17:14 -0700957
Adam Cohen662b5982011-12-13 17:45:21 -0800958 lp.width = width;
959 lp.height = height;
960 lp.x = left;
961 lp.y = top;
Adam Cohen2801caf2011-05-13 20:57:39 -0700962 }
963
Adam Cohen268c4752012-06-06 17:47:33 -0700964 float getPivotXForIconAnimation() {
965 return mFolderIconPivotX;
966 }
967 float getPivotYForIconAnimation() {
968 return mFolderIconPivotY;
969 }
970
Winson Chung892c74d2013-08-22 16:15:50 -0700971 private int getContentAreaHeight() {
972 LauncherAppState app = LauncherAppState.getInstance();
973 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
974 Rect workspacePadding = grid.getWorkspacePadding(grid.isLandscape ?
975 CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
976 int maxContentAreaHeight = grid.availableHeightPx -
Winson Chung892c74d2013-08-22 16:15:50 -0700977 workspacePadding.top - workspacePadding.bottom -
Sunny Goyal290800b2015-03-05 11:33:33 -0800978 mFooterHeight;
Adam Cohen1960ea42013-11-12 11:33:14 +0000979 int height = Math.min(maxContentAreaHeight,
Winson Chung892c74d2013-08-22 16:15:50 -0700980 mContent.getDesiredHeight());
Adam Cohen1960ea42013-11-12 11:33:14 +0000981 return Math.max(height, MIN_CONTENT_DIMEN);
982 }
983
984 private int getContentAreaWidth() {
985 return Math.max(mContent.getDesiredWidth(), MIN_CONTENT_DIMEN);
Winson Chung892c74d2013-08-22 16:15:50 -0700986 }
987
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700988 private int getFolderHeight() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800989 return getFolderHeight(getContentAreaHeight());
990 }
991
992 private int getFolderHeight(int contentAreaHeight) {
Sunny Goyal290800b2015-03-05 11:33:33 -0800993 return getPaddingTop() + getPaddingBottom() + contentAreaHeight + mFooterHeight;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700994 }
Adam Cohen234c4cd2011-07-17 21:03:04 -0700995
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700996 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800997 int contentWidth = getContentAreaWidth();
998 int contentHeight = getContentAreaHeight();
Adam Cohen1960ea42013-11-12 11:33:14 +0000999
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001000 int contentAreaWidthSpec = MeasureSpec.makeMeasureSpec(contentWidth, MeasureSpec.EXACTLY);
1001 int contentAreaHeightSpec = MeasureSpec.makeMeasureSpec(contentHeight, MeasureSpec.EXACTLY);
1002
1003 mContent.setFixedSize(contentWidth, contentHeight);
1004 mContentWrapper.measure(contentAreaWidthSpec, contentAreaHeightSpec);
Sunny Goyal290800b2015-03-05 11:33:33 -08001005 mFooter.measure(contentAreaWidthSpec,
1006 MeasureSpec.makeMeasureSpec(mFooterHeight, MeasureSpec.EXACTLY));
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001007
1008 int folderWidth = getPaddingLeft() + getPaddingRight() + contentWidth;
1009 int folderHeight = getFolderHeight(contentHeight);
1010 setMeasuredDimension(folderWidth, folderHeight);
Adam Cohen234c4cd2011-07-17 21:03:04 -07001011 }
1012
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001013 /**
1014 * Rearranges the children based on their rank.
1015 */
1016 public void rearrangeChildren() {
1017 rearrangeChildren(-1);
1018 }
Adam Cohen2801caf2011-05-13 20:57:39 -07001019
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001020 /**
1021 * Rearranges the children based on their rank.
1022 * @param itemCount if greater than the total children count, empty spaces are left at the end,
1023 * otherwise it is ignored.
1024 */
1025 public void rearrangeChildren(int itemCount) {
1026 ArrayList<View> views = getItemsInReadingOrder();
1027 mContent.arrangeChildren(views, Math.max(itemCount, views.size()));
Adam Cohen7c693212011-05-18 15:26:57 -07001028 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -07001029 }
1030
Sunny Goyalc4918352015-03-10 18:15:48 -07001031 // TODO remove this once GSA code fix is submitted
1032 public ViewGroup getContent() {
1033 return (ViewGroup) mContent;
1034 }
1035
Adam Cohena9cf38f2011-05-02 15:36:58 -07001036 public int getItemCount() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001037 return mContent.getItemCount();
Adam Cohena9cf38f2011-05-02 15:36:58 -07001038 }
1039
Adam Cohen091440a2015-03-18 14:16:05 -07001040 @Thunk void onCloseComplete() {
Adam Cohen05e0f402011-08-01 12:12:49 -07001041 DragLayer parent = (DragLayer) getParent();
Michael Jurka5649c282012-06-18 10:33:21 -07001042 if (parent != null) {
1043 parent.removeView(this);
1044 }
Adam Cohen4554ee12011-08-03 16:13:21 -07001045 mDragController.removeDropTarget((DropTarget) this);
Adam Cohen05e0f402011-08-01 12:12:49 -07001046 clearFocus();
Adam Cohenac56cff2011-09-28 20:45:37 -07001047 mFolderIcon.requestFocus();
Adam Cohen05e0f402011-08-01 12:12:49 -07001048
Adam Cohen2801caf2011-05-13 20:57:39 -07001049 if (mRearrangeOnClose) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001050 rearrangeChildren();
Adam Cohen2801caf2011-05-13 20:57:39 -07001051 mRearrangeOnClose = false;
1052 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001053 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001054 if (!mDragInProgress && !mSuppressFolderDeletion) {
1055 replaceFolderWithFinalItem();
1056 } else if (mDragInProgress) {
1057 mDeleteFolderOnDropCompleted = true;
1058 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001059 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001060 mSuppressFolderDeletion = false;
Sunny Goyal31abc292015-05-01 10:42:32 -07001061 clearDragInfo();
Adam Cohenafb01ee2011-06-23 15:38:03 -07001062 }
1063
Adam Cohen091440a2015-03-18 14:16:05 -07001064 @Thunk void replaceFolderWithFinalItem() {
Adam Cohenafb01ee2011-06-23 15:38:03 -07001065 // Add the last remaining child to the workspace in place of the folder
Adam Cohenfb91f302012-06-11 15:45:18 -07001066 Runnable onCompleteRunnable = new Runnable() {
1067 @Override
1068 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -07001069 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screenId);
Adam Cohenafb01ee2011-06-23 15:38:03 -07001070
Winson Chung33231f52013-12-09 16:57:45 -08001071 View child = null;
Adam Cohenfb91f302012-06-11 15:45:18 -07001072 // Move the item from the folder to the workspace, in the position of the folder
1073 if (getItemCount() == 1) {
1074 ShortcutInfo finalItem = mInfo.contents.get(0);
Adam Cohenc5e63f32012-07-12 16:16:57 -07001075 child = mLauncher.createShortcut(R.layout.application, cellLayout,
Adam Cohenfb91f302012-06-11 15:45:18 -07001076 finalItem);
1077 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -07001078 mInfo.screenId, mInfo.cellX, mInfo.cellY);
Adam Cohenfb91f302012-06-11 15:45:18 -07001079 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001080 if (getItemCount() <= 1) {
1081 // Remove the folder
1082 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
Dan Sandler0eb687f2014-01-10 13:24:55 -05001083 if (cellLayout != null) {
1084 // b/12446428 -- sometimes the cell layout has already gone away?
1085 cellLayout.removeView(mFolderIcon);
1086 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001087 if (mFolderIcon instanceof DropTarget) {
1088 mDragController.removeDropTarget((DropTarget) mFolderIcon);
1089 }
1090 mLauncher.removeFolder(mInfo);
1091 }
Adam Cohenc5e63f32012-07-12 16:16:57 -07001092 // We add the child after removing the folder to prevent both from existing at
Winson Chung0e6a7132013-08-23 12:55:10 -07001093 // the same time in the CellLayout. We need to add the new item with addInScreenFromBind()
1094 // to ensure that hotseat items are placed correctly.
Adam Cohenc5e63f32012-07-12 16:16:57 -07001095 if (child != null) {
Winson Chung0e6a7132013-08-23 12:55:10 -07001096 mLauncher.getWorkspace().addInScreenFromBind(child, mInfo.container, mInfo.screenId,
Adam Cohenc5e63f32012-07-12 16:16:57 -07001097 mInfo.cellX, mInfo.cellY, mInfo.spanX, mInfo.spanY);
1098 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001099 }
1100 };
Sunny Goyalbc753352015-03-05 09:40:44 -08001101 View finalChild = mContent.getLastItem();
Adam Cohenfb91f302012-06-11 15:45:18 -07001102 if (finalChild != null) {
1103 mFolderIcon.performDestroyAnimation(finalChild, onCompleteRunnable);
Winson Chung33231f52013-12-09 16:57:45 -08001104 } else {
1105 onCompleteRunnable.run();
Adam Cohenafb01ee2011-06-23 15:38:03 -07001106 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001107 mDestroyed = true;
1108 }
1109
1110 boolean isDestroyed() {
1111 return mDestroyed;
Adam Cohen2801caf2011-05-13 20:57:39 -07001112 }
1113
Adam Cohenac56cff2011-09-28 20:45:37 -07001114 // This method keeps track of the last item in the folder for the purposes
1115 // of keyboard focus
Sunny Goyal290800b2015-03-05 11:33:33 -08001116 public void updateTextViewFocus() {
Sunny Goyalbc753352015-03-05 09:40:44 -08001117 View lastChild = mContent.getLastItem();
Adam Cohenac56cff2011-09-28 20:45:37 -07001118 if (lastChild != null) {
1119 mFolderName.setNextFocusDownId(lastChild.getId());
1120 mFolderName.setNextFocusRightId(lastChild.getId());
1121 mFolderName.setNextFocusLeftId(lastChild.getId());
1122 mFolderName.setNextFocusUpId(lastChild.getId());
1123 }
1124 }
1125
Adam Cohenbfbfd262011-06-13 16:55:12 -07001126 public void onDrop(DragObject d) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001127 Runnable cleanUpRunnable = null;
1128
Adam Cohen689ff162014-05-08 17:27:56 -07001129 // If we are coming from All Apps space, we defer removing the extra empty screen
1130 // until the folder closes
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001131 if (d.dragSource != mLauncher.getWorkspace() && !(d.dragSource instanceof Folder)) {
1132 cleanUpRunnable = new Runnable() {
1133 @Override
1134 public void run() {
Adam Cohen689ff162014-05-08 17:27:56 -07001135 mLauncher.exitSpringLoadedDragModeDelayed(true,
1136 Launcher.EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
1137 null);
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001138 }
1139 };
Adam Cohenbfbfd262011-06-13 16:55:12 -07001140 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001141
Sunny Goyalb8634152015-04-09 14:17:14 -07001142 // If the icon was dropped while the page was being scrolled, we need to compute
1143 // the target location again such that the icon is placed of the final page.
1144 if (!mContent.rankOnCurrentPage(mEmptyCellRank)) {
1145 // Reorder again.
1146 mTargetRank = getTargetRank(d, null);
Sunny Goyal48461932015-03-09 17:41:09 -07001147
Sunny Goyalb8634152015-04-09 14:17:14 -07001148 // Rearrange items immediately.
1149 mReorderAlarmListener.onAlarm(mReorderAlarm);
Sunny Goyal48461932015-03-09 17:41:09 -07001150
Sunny Goyalb8634152015-04-09 14:17:14 -07001151 mOnScrollHintAlarm.cancelAlarm();
1152 mScrollPauseAlarm.cancelAlarm();
Sunny Goyal48461932015-03-09 17:41:09 -07001153 }
Sunny Goyalb8634152015-04-09 14:17:14 -07001154 mContent.completePendingPageChanges();
Sunny Goyal48461932015-03-09 17:41:09 -07001155
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001156 View currentDragView;
1157 ShortcutInfo si = mCurrentDragInfo;
1158 if (mIsExternalDrag) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001159 currentDragView = mContent.createAndAddViewForRank(si, mEmptyCellRank);
Sunny Goyal95abbb32014-08-04 10:53:22 -07001160 // Actually move the item in the database if it was an external drag. Call this
1161 // before creating the view, so that ShortcutInfo is updated appropriately.
1162 LauncherModel.addOrMoveItemInDatabase(
1163 mLauncher, si, mInfo.id, 0, si.cellX, si.cellY);
1164
1165 // We only need to update the locations if it doesn't get handled in #onDropCompleted.
1166 if (d.dragSource != this) {
1167 updateItemLocationsInDatabaseBatch();
1168 }
1169 mIsExternalDrag = false;
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001170 } else {
1171 currentDragView = mCurrentDragView;
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001172 mContent.addViewForRank(currentDragView, si, mEmptyCellRank);
Adam Cohenbfbfd262011-06-13 16:55:12 -07001173 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001174
1175 if (d.dragView.hasDrawn()) {
1176
1177 // Temporarily reset the scale such that the animation target gets calculated correctly.
1178 float scaleX = getScaleX();
1179 float scaleY = getScaleY();
1180 setScaleX(1.0f);
1181 setScaleY(1.0f);
1182 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, currentDragView,
1183 cleanUpRunnable, null);
1184 setScaleX(scaleX);
1185 setScaleY(scaleY);
1186 } else {
1187 d.deferDragViewCleanupPostAnimation = false;
1188 currentDragView.setVisibility(VISIBLE);
1189 }
1190 mItemsInvalidated = true;
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001191 rearrangeChildren();
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001192
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001193 // Temporarily suppress the listener, as we did all the work already here.
1194 mSuppressOnAdd = true;
1195 mInfo.add(si);
1196 mSuppressOnAdd = false;
Sunny Goyal4b020172014-08-28 14:51:14 -07001197 // Clear the drag info, as it is no longer being dragged.
1198 mCurrentDragInfo = null;
Sunny Goyalf4066152015-04-15 09:42:19 -07001199 mDragInProgress = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001200 }
1201
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001202 // This is used so the item doesn't immediately appear in the folder when added. In one case
1203 // we need to create the illusion that the item isn't added back to the folder yet, to
1204 // to correspond to the animation of the icon back into the folder. This is
1205 public void hideItem(ShortcutInfo info) {
1206 View v = getViewForInfo(info);
1207 v.setVisibility(INVISIBLE);
1208 }
1209 public void showItem(ShortcutInfo info) {
1210 View v = getViewForInfo(info);
1211 v.setVisibility(VISIBLE);
1212 }
1213
Adam Cohenbfbfd262011-06-13 16:55:12 -07001214 public void onAdd(ShortcutInfo item) {
Adam Cohen05e0f402011-08-01 12:12:49 -07001215 // If the item was dropped onto this open folder, we have done the work associated
1216 // with adding the item to the folder, as indicated by mSuppressOnAdd being set
Adam Cohenbfbfd262011-06-13 16:55:12 -07001217 if (mSuppressOnAdd) return;
Sunny Goyal5d85c442015-03-10 13:14:47 -07001218 mContent.createAndAddViewForRank(item, mContent.allocateRankForNewItem(item));
Sunny Goyal2e688a82015-03-18 10:23:39 -07001219 mItemsInvalidated = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001220 LauncherModel.addOrMoveItemInDatabase(
1221 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
1222 }
1223
Adam Cohena9cf38f2011-05-02 15:36:58 -07001224 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -07001225 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001226 // If this item is being dragged from this open folder, we have already handled
1227 // the work associated with removing the item, so we don't have to do anything here.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001228 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001229 View v = getViewForInfo(item);
Sunny Goyal290800b2015-03-05 11:33:33 -08001230 mContent.removeItem(v);
Adam Cohen2801caf2011-05-13 20:57:39 -07001231 if (mState == STATE_ANIMATING) {
1232 mRearrangeOnClose = true;
1233 } else {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001234 rearrangeChildren();
Adam Cohen2801caf2011-05-13 20:57:39 -07001235 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001236 if (getItemCount() <= 1) {
1237 replaceFolderWithFinalItem();
1238 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001239 }
Adam Cohen7c693212011-05-18 15:26:57 -07001240
Sunny Goyalbc753352015-03-05 09:40:44 -08001241 private View getViewForInfo(final ShortcutInfo item) {
1242 return mContent.iterateOverItems(new ItemOperator() {
1243
1244 @Override
1245 public boolean evaluate(ItemInfo info, View view, View parent) {
1246 return info == item;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001247 }
Sunny Goyalbc753352015-03-05 09:40:44 -08001248 });
Adam Cohendf1e4e82011-06-24 15:57:39 -07001249 }
1250
Adam Cohen76078c42011-06-09 15:06:52 -07001251 public void onItemsChanged() {
Adam Cohenac56cff2011-09-28 20:45:37 -07001252 updateTextViewFocus();
Adam Cohen76078c42011-06-09 15:06:52 -07001253 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001254
Adam Cohen76fc0852011-06-17 13:26:23 -07001255 public void onTitleChanged(CharSequence title) {
1256 }
Adam Cohen76078c42011-06-09 15:06:52 -07001257
Adam Cohen7c693212011-05-18 15:26:57 -07001258 public ArrayList<View> getItemsInReadingOrder() {
1259 if (mItemsInvalidated) {
1260 mItemsInReadingOrder.clear();
Sunny Goyalbc753352015-03-05 09:40:44 -08001261 mContent.iterateOverItems(new ItemOperator() {
1262
1263 @Override
1264 public boolean evaluate(ItemInfo info, View view, View parent) {
1265 mItemsInReadingOrder.add(view);
1266 return false;
Adam Cohen7c693212011-05-18 15:26:57 -07001267 }
Sunny Goyalbc753352015-03-05 09:40:44 -08001268 });
Adam Cohen7c693212011-05-18 15:26:57 -07001269 mItemsInvalidated = false;
1270 }
1271 return mItemsInReadingOrder;
1272 }
Adam Cohen8dfcba42011-07-07 16:38:18 -07001273
1274 public void getLocationInDragLayer(int[] loc) {
1275 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
1276 }
Adam Cohenea0818d2011-09-30 20:06:58 -07001277
1278 public void onFocusChange(View v, boolean hasFocus) {
1279 if (v == mFolderName && hasFocus) {
1280 startEditingFolderName();
1281 }
1282 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001283
1284 @Override
1285 public void getHitRectRelativeToDragLayer(Rect outRect) {
1286 getHitRect(outRect);
Sunny Goyal48461932015-03-09 17:41:09 -07001287 outRect.left -= mScrollAreaOffset;
1288 outRect.right += mScrollAreaOffset;
1289 }
1290
1291 private class OnScrollHintListener implements OnAlarmListener {
1292
1293 private final DragObject mDragObject;
1294
1295 OnScrollHintListener(DragObject object) {
1296 mDragObject = object;
1297 }
1298
1299 /**
1300 * Scroll hint has been shown long enough. Now scroll to appropriate page.
1301 */
1302 @Override
1303 public void onAlarm(Alarm alarm) {
1304 if (mCurrentScrollDir == DragController.SCROLL_LEFT) {
Sunny Goyalb8634152015-04-09 14:17:14 -07001305 mContent.scrollLeft();
Sunny Goyal48461932015-03-09 17:41:09 -07001306 mScrollHintDir = DragController.SCROLL_NONE;
1307 } else if (mCurrentScrollDir == DragController.SCROLL_RIGHT) {
Sunny Goyalb8634152015-04-09 14:17:14 -07001308 mContent.scrollRight();
Sunny Goyal48461932015-03-09 17:41:09 -07001309 mScrollHintDir = DragController.SCROLL_NONE;
1310 } else {
1311 // This should not happen
1312 return;
1313 }
1314 mCurrentScrollDir = DragController.SCROLL_NONE;
1315
1316 // Pause drag event until the scrolling is finished
1317 mScrollPauseAlarm.setOnAlarmListener(new OnScrollFinishedListener(mDragObject));
1318 mScrollPauseAlarm.setAlarm(DragController.RESCROLL_DELAY);
1319 }
1320 }
1321
1322 private class OnScrollFinishedListener implements OnAlarmListener {
1323
1324 private final DragObject mDragObject;
1325
1326 OnScrollFinishedListener(DragObject object) {
1327 mDragObject = object;
1328 }
1329
1330 /**
1331 * Page scroll is complete.
1332 */
1333 @Override
1334 public void onAlarm(Alarm alarm) {
1335 // Reorder immediately on page change.
1336 onDragOver(mDragObject, 1);
1337 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001338 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001339}