blob: c1aa35669be996981810326af4cd01e203f99fca [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 Goyal70660032015-05-14 00:07:08 -070024import android.annotation.SuppressLint;
Sunny Goyalc46bfef2015-01-05 12:40:08 -080025import android.annotation.TargetApi;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080026import android.content.Context;
Adam Cohen76fc0852011-06-17 13:26:23 -070027import android.content.res.Resources;
Winson Chungb745afb2015-03-02 11:51:23 -080028import android.graphics.Point;
Winson Chung043f2af2012-03-01 16:09:54 -080029import android.graphics.PointF;
Romain Guyfb5411e2010-02-24 10:04:17 -080030import android.graphics.Rect;
Sunny Goyalc46bfef2015-01-05 12:40:08 -080031import android.os.Build;
Winson Chung8f1eff72015-05-28 17:33:40 -070032import android.os.Bundle;
Adam Cohen7a14d0b2011-06-24 11:36:35 -070033import android.text.InputType;
Adam Cohene601a432011-07-26 21:51:30 -070034import android.text.Selection;
35import android.text.Spannable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.util.AttributeSet;
Adam Cohen3bf84d32012-05-07 20:17:14 -070037import android.util.Log;
Adam Cohen76fc0852011-06-17 13:26:23 -070038import android.view.ActionMode;
39import android.view.KeyEvent;
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;
Sunny Goyalc1cd23b2015-06-01 17:15:34 -070048import android.view.animation.AnimationUtils;
Adam Cohen76fc0852011-06-17 13:26:23 -070049import android.view.inputmethod.EditorInfo;
50import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080051import android.widget.LinearLayout;
Adam Cohendf2cc412011-04-27 16:56:57 -070052import android.widget.TextView;
Sunny Goyal83a8f042015-05-19 12:52:12 -070053
Sunny Goyale9b651e2015-04-24 11:44:51 -070054import com.android.launcher3.CellLayout.CellInfo;
Sunny Goyalf4066152015-04-15 09:42:19 -070055import com.android.launcher3.DragController.DragListener;
Daniel Sandler325dc232013-06-05 22:57:57 -040056import com.android.launcher3.FolderInfo.FolderListener;
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;
Sunny Goyal83a8f042015-05-19 12:52:12 -070059import com.android.launcher3.accessibility.LauncherAccessibilityDelegate.AccessibilityDragSource;
Adam Cohen091440a2015-03-18 14:16:05 -070060import com.android.launcher3.util.Thunk;
Sunny Goyal2245fa22015-07-06 11:45:18 -070061import com.android.launcher3.util.UiThreadCircularReveal;
Sunny Goyal83a8f042015-05-19 12:52:12 -070062
Adam Cohenc0dcf592011-06-01 15:30:43 -070063import java.util.ArrayList;
Adam Cohen3bf84d32012-05-07 20:17:14 -070064import java.util.Collections;
Sunny Goyal1dd0f8b2015-07-06 13:04:02 -070065import java.util.Comparator;
Adam Cohenc0dcf592011-06-01 15:30:43 -070066
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067/**
68 * Represents a set of icons chosen by the user or generated by the system.
69 */
Adam Cohen8dfcba42011-07-07 16:38:18 -070070public class Folder extends LinearLayout implements DragSource, View.OnClickListener,
Adam Cohenea0818d2011-09-30 20:06:58 -070071 View.OnLongClickListener, DropTarget, FolderListener, TextView.OnEditorActionListener,
Winson Chung8f1eff72015-05-28 17:33:40 -070072 View.OnFocusChangeListener, DragListener, UninstallSource, AccessibilityDragSource,
73 Stats.LaunchSourceProvider {
Adam Cohendf2cc412011-04-27 16:56:57 -070074 private static final String TAG = "Launcher.Folder";
75
Sunny Goyalc3a609f2015-02-26 17:43:50 -080076 /**
77 * We avoid measuring {@link #mContentWrapper} with a 0 width or height, as this
78 * results in CellLayout being measured as UNSPECIFIED, which it does not support.
79 */
80 private static final int MIN_CONTENT_DIMEN = 5;
Adam Cohen4eac29a2011-07-11 17:53:37 -070081
Adam Cohendf2cc412011-04-27 16:56:57 -070082 static final int STATE_NONE = -1;
83 static final int STATE_SMALL = 0;
84 static final int STATE_ANIMATING = 1;
85 static final int STATE_OPEN = 2;
86
Sunny Goyal48461932015-03-09 17:41:09 -070087 /**
Sunny Goyal48461932015-03-09 17:41:09 -070088 * Time for which the scroll hint is shown before automatically changing page.
89 */
90 public static final int SCROLL_HINT_DURATION = DragController.SCROLL_DELAY;
91
92 /**
93 * Fraction of icon width which behave as scroll region.
94 */
95 private static final float ICON_OVERSCROLL_WIDTH_FACTOR = 0.45f;
96
Sunny Goyala07c2f52015-06-01 11:00:38 -070097 private static final int FOLDER_NAME_ANIMATION_DURATION = 633;
Sunny Goyalb7e15ad2015-05-07 14:51:31 -070098
Adam Cohenf0f4eda2013-06-06 21:27:03 -070099 private static final int REORDER_DELAY = 250;
Adam Cohen5d518fa2013-12-05 14:16:23 -0800100 private static final int ON_EXIT_CLOSE_DELAY = 400;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800101 private static final Rect sTempRect = new Rect();
102
103 private static String sDefaultFolderName;
104 private static String sHintText;
105
106 private final Alarm mReorderAlarm = new Alarm();
107 private final Alarm mOnExitAlarm = new Alarm();
Sunny Goyalb8634152015-04-09 14:17:14 -0700108 private final Alarm mOnScrollHintAlarm = new Alarm();
109 @Thunk final Alarm mScrollPauseAlarm = new Alarm();
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800110
Adam Cohen091440a2015-03-18 14:16:05 -0700111 @Thunk final ArrayList<View> mItemsInReadingOrder = new ArrayList<View>();
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800112
113 private final int mExpandDuration;
114 private final int mMaterialExpandDuration;
115 private final int mMaterialExpandStagger;
116
117 private final InputMethodManager mInputMethodManager;
118
119 protected final Launcher mLauncher;
120 protected DragController mDragController;
121 protected FolderInfo mInfo;
122
Adam Cohen091440a2015-03-18 14:16:05 -0700123 @Thunk FolderIcon mFolderIcon;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800124
Sunny Goyalb8634152015-04-09 14:17:14 -0700125 @Thunk FolderPagedView mContent;
Adam Cohen091440a2015-03-18 14:16:05 -0700126 @Thunk View mContentWrapper;
Winson97b0d082015-08-13 15:18:25 -0700127 ExtendedEditText mFolderName;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800128
Sunny Goyal290800b2015-03-05 11:33:33 -0800129 private View mFooter;
130 private int mFooterHeight;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800131
132 // Cell ranks used for drag and drop
Adam Cohen091440a2015-03-18 14:16:05 -0700133 @Thunk int mTargetRank, mPrevTargetRank, mEmptyCellRank;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800134
Adam Cohen091440a2015-03-18 14:16:05 -0700135 @Thunk int mState = STATE_NONE;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800136 private boolean mRearrangeOnClose = false;
Adam Cohen7c693212011-05-18 15:26:57 -0700137 boolean mItemsInvalidated = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700138 private ShortcutInfo mCurrentDragInfo;
139 private View mCurrentDragView;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800140 private boolean mIsExternalDrag;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700141 boolean mSuppressOnAdd = false;
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700142 private boolean mDragInProgress = false;
143 private boolean mDeleteFolderOnDropCompleted = false;
144 private boolean mSuppressFolderDeletion = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700145 private boolean mItemAddedBackToSelfViaIcon = false;
Adam Cohen091440a2015-03-18 14:16:05 -0700146 @Thunk float mFolderIconPivotX;
147 @Thunk float mFolderIconPivotY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700148 private boolean mIsEditingName = false;
Adam Cohen1960ea42013-11-12 11:33:14 +0000149
Adam Cohenfb91f302012-06-11 15:45:18 -0700150 private boolean mDestroyed;
151
Adam Cohen091440a2015-03-18 14:16:05 -0700152 @Thunk Runnable mDeferredAction;
Michael Jurka1e2f4652013-07-08 18:03:46 -0700153 private boolean mDeferDropAfterUninstall;
154 private boolean mUninstallSuccessful;
155
Sunny Goyal48461932015-03-09 17:41:09 -0700156 // Folder scrolling
157 private int mScrollAreaOffset;
Sunny Goyal48461932015-03-09 17:41:09 -0700158
Adam Cohen091440a2015-03-18 14:16:05 -0700159 @Thunk int mScrollHintDir = DragController.SCROLL_NONE;
160 @Thunk int mCurrentScrollDir = DragController.SCROLL_NONE;
Sunny Goyal48461932015-03-09 17:41:09 -0700161
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162 /**
163 * Used to inflate the Workspace from XML.
164 *
165 * @param context The application's context.
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800166 * @param attrs The attributes set containing the Workspace's customization values.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167 */
168 public Folder(Context context, AttributeSet attrs) {
169 super(context, attrs);
170 setAlwaysDrawnWithCacheEnabled(false);
Adam Cohen76fc0852011-06-17 13:26:23 -0700171 mInputMethodManager = (InputMethodManager)
Michael Jurka8b805b12012-04-18 14:23:14 -0700172 getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
Adam Cohen76fc0852011-06-17 13:26:23 -0700173
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800174 Resources res = getResources();
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700175 mExpandDuration = res.getInteger(R.integer.config_folderExpandDuration);
176 mMaterialExpandDuration = res.getInteger(R.integer.config_materialFolderExpandDuration);
177 mMaterialExpandStagger = res.getInteger(R.integer.config_materialFolderExpandStagger);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700178
179 if (sDefaultFolderName == null) {
180 sDefaultFolderName = res.getString(R.string.folder_name);
181 }
Adam Cohena65beee2011-06-27 21:32:23 -0700182 if (sHintText == null) {
183 sHintText = res.getString(R.string.folder_hint_text);
184 }
Adam Cohen4eac29a2011-07-11 17:53:37 -0700185 mLauncher = (Launcher) context;
Adam Cohenac56cff2011-09-28 20:45:37 -0700186 // We need this view to be focusable in touch mode so that when text editing of the folder
187 // name is complete, we have something to focus on, thus hiding the cursor and giving
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800188 // reliable behavior when clicking the text field (since it will always gain focus on click).
Adam Cohenac56cff2011-09-28 20:45:37 -0700189 setFocusableInTouchMode(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190 }
191
192 @Override
193 protected void onFinishInflate() {
194 super.onFinishInflate();
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800195 mContentWrapper = findViewById(R.id.folder_content_wrapper);
Sunny Goyalb8634152015-04-09 14:17:14 -0700196 mContent = (FolderPagedView) findViewById(R.id.folder_content);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800197 mContent.setFolder(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700198
Winson97b0d082015-08-13 15:18:25 -0700199 mFolderName = (ExtendedEditText) findViewById(R.id.folder_name);
200 mFolderName.setOnBackKeyListener(new ExtendedEditText.OnBackKeyListener() {
201 @Override
202 public boolean onBackKey() {
203 // Close the activity on back key press
204 doneEditingFolderName(true);
205 return false;
206 }
207 });
Adam Cohenea0818d2011-09-30 20:06:58 -0700208 mFolderName.setOnFocusChangeListener(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700209
Adam Cohen76fc0852011-06-17 13:26:23 -0700210 // We disable action mode for now since it messes up the view on phones
211 mFolderName.setCustomSelectionActionModeCallback(mActionModeCallback);
Adam Cohen76fc0852011-06-17 13:26:23 -0700212 mFolderName.setOnEditorActionListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700213 mFolderName.setSelectAllOnFocus(true);
Adam Cohen7a14d0b2011-06-24 11:36:35 -0700214 mFolderName.setInputType(mFolderName.getInputType() |
215 InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800216
Sunny Goyalb8634152015-04-09 14:17:14 -0700217 mFooter = findViewById(R.id.folder_footer);
Sunny Goyalb8634152015-04-09 14:17:14 -0700218
Sunny Goyal290800b2015-03-05 11:33:33 -0800219 // We find out how tall footer wants to be (it is set to wrap_content), so that
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800220 // we can allocate the appropriate amount of space for it.
221 int measureSpec = MeasureSpec.UNSPECIFIED;
Sunny Goyal290800b2015-03-05 11:33:33 -0800222 mFooter.measure(measureSpec, measureSpec);
223 mFooterHeight = mFooter.getMeasuredHeight();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800224 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700225
Adam Cohen76fc0852011-06-17 13:26:23 -0700226 private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
227 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
228 return false;
229 }
230
231 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
232 return false;
233 }
234
235 public void onDestroyActionMode(ActionMode mode) {
236 }
237
238 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
239 return false;
240 }
241 };
242
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800243 public void onClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700244 Object tag = v.getTag();
245 if (tag instanceof ShortcutInfo) {
Adam Cohenb5fe60c2013-06-06 22:03:51 -0700246 mLauncher.onClick(v);
Adam Cohendf2cc412011-04-27 16:56:57 -0700247 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800248 }
249
250 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -0700251 // Return if global dragging is not enabled
252 if (!mLauncher.isDraggingEnabled()) return true;
Sunny Goyale9b651e2015-04-24 11:44:51 -0700253 return beginDrag(v, false);
254 }
Winson Chung36a62fe2012-05-06 18:04:42 -0700255
Sunny Goyale9b651e2015-04-24 11:44:51 -0700256 private boolean beginDrag(View v, boolean accessible) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700257 Object tag = v.getTag();
258 if (tag instanceof ShortcutInfo) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700259 ShortcutInfo item = (ShortcutInfo) tag;
260 if (!v.isInTouchMode()) {
261 return false;
262 }
263
Sunny Goyale9b651e2015-04-24 11:44:51 -0700264 mLauncher.getWorkspace().beginDragShared(v, new Point(), this, accessible);
Adam Cohen76078c42011-06-09 15:06:52 -0700265
266 mCurrentDragInfo = item;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800267 mEmptyCellRank = item.rank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700268 mCurrentDragView = v;
Adam Cohenfc53cd22011-07-20 15:45:11 -0700269
Sunny Goyal290800b2015-03-05 11:33:33 -0800270 mContent.removeItem(mCurrentDragView);
Adam Cohenfc53cd22011-07-20 15:45:11 -0700271 mInfo.remove(mCurrentDragInfo);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700272 mDragInProgress = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700273 mItemAddedBackToSelfViaIcon = false;
Adam Cohendf2cc412011-04-27 16:56:57 -0700274 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800275 return true;
276 }
277
Sunny Goyale9b651e2015-04-24 11:44:51 -0700278 @Override
279 public void startDrag(CellInfo cellInfo, boolean accessible) {
280 beginDrag(cellInfo.cell, accessible);
281 }
282
283 @Override
284 public void enableAccessibleDrag(boolean enable) {
Winson Chung006ee262015-08-03 14:40:11 -0700285 mLauncher.getSearchDropTargetBar().enableAccessibleDrag(enable);
Sunny Goyale9b651e2015-04-24 11:44:51 -0700286 for (int i = 0; i < mContent.getChildCount(); i++) {
287 mContent.getPageAt(i).enableAccessibleDrag(enable, CellLayout.FOLDER_ACCESSIBILITY_DRAG);
288 }
Sunny Goyalccc414b2015-04-30 12:28:16 -0700289
290 mFooter.setImportantForAccessibility(enable ? IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS :
291 IMPORTANT_FOR_ACCESSIBILITY_AUTO);
Sunny Goyale9b651e2015-04-24 11:44:51 -0700292 mLauncher.getWorkspace().setAddNewPageOnDrag(!enable);
293 }
294
Adam Cohen76fc0852011-06-17 13:26:23 -0700295 public boolean isEditingName() {
296 return mIsEditingName;
297 }
298
299 public void startEditingFolderName() {
Adam Cohena65beee2011-06-27 21:32:23 -0700300 mFolderName.setHint("");
Adam Cohen76fc0852011-06-17 13:26:23 -0700301 mIsEditingName = true;
302 }
303
304 public void dismissEditingName() {
305 mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
306 doneEditingFolderName(true);
307 }
308
309 public void doneEditingFolderName(boolean commit) {
Adam Cohena65beee2011-06-27 21:32:23 -0700310 mFolderName.setHint(sHintText);
Adam Cohen1df26a32011-08-12 16:15:23 -0700311 // Convert to a string here to ensure that no other state associated with the text field
312 // gets saved.
Sunny Goyal230eade2015-06-18 12:48:51 -0700313 String newTitle = mFolderName.getText().toString();
Adam Cohen3371da02011-10-25 21:38:29 -0700314 mInfo.setTitle(newTitle);
Adam Cohen76fc0852011-06-17 13:26:23 -0700315 LauncherModel.updateItemInDatabase(mLauncher, mInfo);
Adam Cohenac56cff2011-09-28 20:45:37 -0700316
Adam Cohen3371da02011-10-25 21:38:29 -0700317 if (commit) {
318 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Sunny Goyal230eade2015-06-18 12:48:51 -0700319 String.format(getContext().getString(R.string.folder_renamed), newTitle));
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 */
Sunny Goyal70660032015-05-14 00:07:08 -0700344 @SuppressLint("ClickableViewAccessibility")
Adam Cohen0c872ba2011-05-05 10:34:16 -0700345 @Override
346 public boolean onTouchEvent(MotionEvent ev) {
347 return true;
348 }
349
Joe Onorato00acb122009-08-04 16:04:30 -0400350 public void setDragController(DragController dragController) {
351 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800352 }
353
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800354 public void setFolderIcon(FolderIcon icon) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700355 mFolderIcon = icon;
356 }
357
Adam Cohen3371da02011-10-25 21:38:29 -0700358 @Override
359 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
360 // When the folder gets focus, we don't want to announce the list of items.
361 return true;
362 }
363
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800364 /**
365 * @return the FolderInfo object associated with this folder
366 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700367 public FolderInfo getInfo() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800368 return mInfo;
369 }
370
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800371 void bind(FolderInfo info) {
372 mInfo = info;
Adam Cohendf2cc412011-04-27 16:56:57 -0700373 ArrayList<ShortcutInfo> children = info.contents;
Sunny Goyal1dd0f8b2015-07-06 13:04:02 -0700374 Collections.sort(children, ITEM_POS_COMPARATOR);
Sunny Goyal08f72612015-01-05 13:41:43 -0800375
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800376 ArrayList<ShortcutInfo> overflow = mContent.bindItems(children);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700377
Jason Monk4ff73882014-04-24 16:48:07 -0400378 // If our folder has too many items we prune them from the list. This is an issue
Adam Cohenc508b2d2011-06-28 14:41:44 -0700379 // when upgrading from the old Folders implementation which could contain an unlimited
380 // number of items.
381 for (ShortcutInfo item: overflow) {
382 mInfo.remove(item);
383 LauncherModel.deleteItemFromDatabase(mLauncher, item);
384 }
385
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800386 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
387 if (lp == null) {
388 lp = new DragLayer.LayoutParams(0, 0);
389 lp.customPosition = true;
390 setLayoutParams(lp);
391 }
392 centerAboutIcon();
393
Adam Cohen4dbe6d92011-05-18 17:14:15 -0700394 mItemsInvalidated = true;
Adam Cohenac56cff2011-09-28 20:45:37 -0700395 updateTextViewFocus();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700396 mInfo.addListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700397
Adam Cohenafb01ee2011-06-23 15:38:03 -0700398 if (!sDefaultFolderName.contentEquals(mInfo.title)) {
Adam Cohen4ef610f2011-06-21 22:37:36 -0700399 mFolderName.setText(mInfo.title);
400 } else {
401 mFolderName.setText("");
402 }
Winson Chung33231f52013-12-09 16:57:45 -0800403
404 // In case any children didn't come across during loading, clean up the folder accordingly
405 mFolderIcon.post(new Runnable() {
406 public void run() {
407 if (getItemCount() <= 1) {
408 replaceFolderWithFinalItem();
409 }
410 }
411 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700412 }
413
414 /**
415 * Creates a new UserFolder, inflated from R.layout.user_folder.
416 *
417 * @param context The application's context.
418 *
419 * @return A new UserFolder.
420 */
Sunny Goyalc23da842015-05-14 20:44:01 -0700421 @SuppressLint("InflateParams")
Sunny Goyal70660032015-05-14 00:07:08 -0700422 static Folder fromXml(Launcher launcher) {
Sunny Goyalc23da842015-05-14 20:44:01 -0700423 return (Folder) launcher.getLayoutInflater().inflate(R.layout.user_folder, null);
Adam Cohendf2cc412011-04-27 16:56:57 -0700424 }
425
426 /**
427 * This method is intended to make the UserFolder to be visually identical in size and position
428 * to its associated FolderIcon. This allows for a seamless transition into the expanded state.
429 */
430 private void positionAndSizeAsIcon() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700431 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800432 setScaleX(0.8f);
433 setScaleY(0.8f);
434 setAlpha(0f);
Adam Cohendf2cc412011-04-27 16:56:57 -0700435 mState = STATE_SMALL;
436 }
437
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700438 private void prepareReveal() {
439 setScaleX(1f);
440 setScaleY(1f);
441 setAlpha(1f);
442 mState = STATE_SMALL;
443 }
444
Adam Cohendf2cc412011-04-27 16:56:57 -0700445 public void animateOpen() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700446 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen6441de02011-12-14 14:25:32 -0800447
Sunny Goyalb8634152015-04-09 14:17:14 -0700448 mContent.completePendingPageChanges();
Sunny Goyal8167dc22015-04-27 13:44:01 -0700449 if (!mDragInProgress) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700450 // Open on the first page.
451 mContent.snapToPageImmediately(0);
Sunny Goyal48461932015-03-09 17:41:09 -0700452 }
453
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700454 Animator openFolderAnim = null;
455 final Runnable onCompleteRunnable;
Sunny Goyal9fc953b2015-08-17 12:24:25 -0700456 if (!Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700457 positionAndSizeAsIcon();
458 centerAboutIcon();
459
460 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1);
461 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
462 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
463 final ObjectAnimator oa =
464 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
465 oa.setDuration(mExpandDuration);
466 openFolderAnim = oa;
467
468 setLayerType(LAYER_TYPE_HARDWARE, null);
469 onCompleteRunnable = new Runnable() {
470 @Override
471 public void run() {
472 setLayerType(LAYER_TYPE_NONE, null);
473 }
474 };
475 } else {
476 prepareReveal();
477 centerAboutIcon();
478
Sunny Goyal2245fa22015-07-06 11:45:18 -0700479 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700480 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
481 int height = getFolderHeight();
482
483 float transX = - 0.075f * (width / 2 - getPivotX());
484 float transY = - 0.075f * (height / 2 - getPivotY());
485 setTranslationX(transX);
486 setTranslationY(transY);
487 PropertyValuesHolder tx = PropertyValuesHolder.ofFloat("translationX", transX, 0);
488 PropertyValuesHolder ty = PropertyValuesHolder.ofFloat("translationY", transY, 0);
489
Sunny Goyal2245fa22015-07-06 11:45:18 -0700490 Animator drift = ObjectAnimator.ofPropertyValuesHolder(this, tx, ty);
Sunny Goyal75deaf32015-05-07 16:13:12 -0700491 drift.setDuration(mMaterialExpandDuration);
492 drift.setStartDelay(mMaterialExpandStagger);
493 drift.setInterpolator(new LogDecelerateInterpolator(100, 0));
494
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700495 int rx = (int) Math.max(Math.max(width - getPivotX(), 0), getPivotX());
496 int ry = (int) Math.max(Math.max(height - getPivotY(), 0), getPivotY());
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700497 float radius = (float) Math.hypot(rx, ry);
Sunny Goyal75deaf32015-05-07 16:13:12 -0700498
Sunny Goyal2245fa22015-07-06 11:45:18 -0700499 Animator reveal = UiThreadCircularReveal.createCircularReveal(this, (int) getPivotX(),
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700500 (int) getPivotY(), 0, radius);
501 reveal.setDuration(mMaterialExpandDuration);
502 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
503
Sunny Goyalbc753352015-03-05 09:40:44 -0800504 mContentWrapper.setAlpha(0f);
Sunny Goyal2245fa22015-07-06 11:45:18 -0700505 Animator iconsAlpha = ObjectAnimator.ofFloat(mContentWrapper, "alpha", 0f, 1f);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700506 iconsAlpha.setDuration(mMaterialExpandDuration);
507 iconsAlpha.setStartDelay(mMaterialExpandStagger);
508 iconsAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
509
Sunny Goyal290800b2015-03-05 11:33:33 -0800510 mFooter.setAlpha(0f);
Sunny Goyal2245fa22015-07-06 11:45:18 -0700511 Animator textAlpha = ObjectAnimator.ofFloat(mFooter, "alpha", 0f, 1f);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700512 textAlpha.setDuration(mMaterialExpandDuration);
513 textAlpha.setStartDelay(mMaterialExpandStagger);
514 textAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
515
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700516 anim.play(drift);
517 anim.play(iconsAlpha);
518 anim.play(textAlpha);
519 anim.play(reveal);
520
521 openFolderAnim = anim;
522
Sunny Goyalbc753352015-03-05 09:40:44 -0800523 mContentWrapper.setLayerType(LAYER_TYPE_HARDWARE, null);
Sunny Goyal75deaf32015-05-07 16:13:12 -0700524 mFooter.setLayerType(LAYER_TYPE_HARDWARE, null);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700525 onCompleteRunnable = new Runnable() {
526 @Override
527 public void run() {
Sunny Goyalbc753352015-03-05 09:40:44 -0800528 mContentWrapper.setLayerType(LAYER_TYPE_NONE, null);
Sunny Goyal75deaf32015-05-07 16:13:12 -0700529 mContentWrapper.setLayerType(LAYER_TYPE_NONE, null);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700530 }
531 };
532 }
533 openFolderAnim.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700534 @Override
535 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700536 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Sunny Goyalbc753352015-03-05 09:40:44 -0800537 mContent.getAccessibilityDescription());
Adam Cohendf2cc412011-04-27 16:56:57 -0700538 mState = STATE_ANIMATING;
539 }
540 @Override
541 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700542 mState = STATE_OPEN;
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700543
544 if (onCompleteRunnable != null) {
545 onCompleteRunnable.run();
546 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800547
Sunny Goyalbc753352015-03-05 09:40:44 -0800548 mContent.setFocusOnFirstChild();
Adam Cohendf2cc412011-04-27 16:56:57 -0700549 }
550 });
Sunny Goyalb7e15ad2015-05-07 14:51:31 -0700551
552 // Footer animation
553 if (mContent.getPageCount() > 1 && !mInfo.hasOption(FolderInfo.FLAG_MULTI_PAGE_ANIMATION)) {
554 int footerWidth = mContent.getDesiredWidth()
555 - mFooter.getPaddingLeft() - mFooter.getPaddingRight();
556
557 float textWidth = mFolderName.getPaint().measureText(mFolderName.getText().toString());
Sunny Goyala07c2f52015-06-01 11:00:38 -0700558 float translation = (footerWidth - textWidth) / 2;
559 mFolderName.setTranslationX(mContent.mIsRtl ? -translation : translation);
Sunny Goyalb7e15ad2015-05-07 14:51:31 -0700560 mContent.setMarkerScale(0);
561
562 // Do not update the flag if we are in drag mode. The flag will be updated, when we
563 // actually drop the icon.
564 final boolean updateAnimationFlag = !mDragInProgress;
565 openFolderAnim.addListener(new AnimatorListenerAdapter() {
566
567 @Override
568 public void onAnimationEnd(Animator animation) {
Sunny Goyala07c2f52015-06-01 11:00:38 -0700569 mFolderName.animate().setDuration(FOLDER_NAME_ANIMATION_DURATION)
570 .translationX(0)
Sunny Goyal9fc953b2015-08-17 12:24:25 -0700571 .setInterpolator(Utilities.ATLEAST_LOLLIPOP ?
Sunny Goyalc1cd23b2015-06-01 17:15:34 -0700572 AnimationUtils.loadInterpolator(mLauncher,
573 android.R.interpolator.fast_out_slow_in)
574 : new LogDecelerateInterpolator(100, 0));
Sunny Goyalb7e15ad2015-05-07 14:51:31 -0700575 mContent.animateMarkers();
576
577 if (updateAnimationFlag) {
578 mInfo.setOption(FolderInfo.FLAG_MULTI_PAGE_ANIMATION, true, mLauncher);
579 }
580 }
581 });
582 } else {
583 mFolderName.setTranslationX(0);
584 mContent.setMarkerScale(1);
585 }
586
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700587 openFolderAnim.start();
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800588
589 // Make sure the folder picks up the last drag move even if the finger doesn't move.
590 if (mDragController.isDragging()) {
591 mDragController.forceTouchMove();
592 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700593
Sunny Goyalb8634152015-04-09 14:17:14 -0700594 FolderPagedView pages = (FolderPagedView) mContent;
595 pages.verifyVisibleHighResIcons(pages.getNextPage());
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800596 }
597
598 public void beginExternalDrag(ShortcutInfo item) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800599 mCurrentDragInfo = item;
Sunny Goyal5d85c442015-03-10 13:14:47 -0700600 mEmptyCellRank = mContent.allocateRankForNewItem(item);
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800601 mIsExternalDrag = true;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800602 mDragInProgress = true;
Sunny Goyalb8634152015-04-09 14:17:14 -0700603
Sunny Goyalf4066152015-04-15 09:42:19 -0700604 // Since this folder opened by another controller, it might not get onDrop or
605 // onDropComplete. Perform cleanup once drag-n-drop ends.
606 mDragController.addDragListener(this);
607 }
608
609 @Override
610 public void onDragStart(DragSource source, Object info, int dragAction) { }
611
612 @Override
613 public void onDragEnd() {
614 if (mIsExternalDrag && mDragInProgress) {
615 completeDragExit();
616 }
617 mDragController.removeDragListener(this);
Adam Cohendf2cc412011-04-27 16:56:57 -0700618 }
619
Adam Cohen091440a2015-03-18 14:16:05 -0700620 @Thunk void sendCustomAccessibilityEvent(int type, String text) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700621 AccessibilityManager accessibilityManager = (AccessibilityManager)
622 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
623 if (accessibilityManager.isEnabled()) {
Adam Cohen3371da02011-10-25 21:38:29 -0700624 AccessibilityEvent event = AccessibilityEvent.obtain(type);
625 onInitializeAccessibilityEvent(event);
626 event.getText().add(text);
Michael Jurka8b805b12012-04-18 14:23:14 -0700627 accessibilityManager.sendAccessibilityEvent(event);
Adam Cohen3371da02011-10-25 21:38:29 -0700628 }
629 }
630
Adam Cohendf2cc412011-04-27 16:56:57 -0700631 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700632 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800633 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
634 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
635 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
Michael Jurka032e6ba2013-04-22 15:08:42 +0200636 final ObjectAnimator oa =
Michael Jurka2ecf9952012-06-18 12:52:28 -0700637 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohendf2cc412011-04-27 16:56:57 -0700638
Adam Cohen2801caf2011-05-13 20:57:39 -0700639 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700640 @Override
641 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700642 onCloseComplete();
Michael Jurka0121c3e2012-05-31 08:36:04 -0700643 setLayerType(LAYER_TYPE_NONE, null);
Adam Cohen2801caf2011-05-13 20:57:39 -0700644 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700645 }
646 @Override
647 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700648 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700649 getContext().getString(R.string.folder_closed));
Adam Cohendf2cc412011-04-27 16:56:57 -0700650 mState = STATE_ANIMATING;
651 }
652 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700653 oa.setDuration(mExpandDuration);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700654 setLayerType(LAYER_TYPE_HARDWARE, null);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100655 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700656 }
657
Adam Cohencb3382b2011-05-24 14:07:08 -0700658 public boolean acceptDrop(DragObject d) {
659 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700660 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700661 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
662 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
663 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700664 }
665
Adam Cohencb3382b2011-05-24 14:07:08 -0700666 public void onDragEnter(DragObject d) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800667 mPrevTargetRank = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700668 mOnExitAlarm.cancelAlarm();
Sunny Goyalb8634152015-04-09 14:17:14 -0700669 // Get the area offset such that the folder only closes if half the drag icon width
670 // is outside the folder area
671 mScrollAreaOffset = d.dragView.getDragRegionWidth() / 2 - d.xOffset;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700672 }
673
674 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
675 public void onAlarm(Alarm alarm) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800676 mContent.realTimeReorder(mEmptyCellRank, mTargetRank);
677 mEmptyCellRank = mTargetRank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700678 }
679 };
680
Sunny Goyalc46bfef2015-01-05 12:40:08 -0800681 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohen2374abf2013-04-16 14:56:57 -0700682 public boolean isLayoutRtl() {
683 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
684 }
685
Sunny Goyal48461932015-03-09 17:41:09 -0700686 @Override
Adam Cohencb3382b2011-05-24 14:07:08 -0700687 public void onDragOver(DragObject d) {
Sunny Goyal48461932015-03-09 17:41:09 -0700688 onDragOver(d, REORDER_DELAY);
689 }
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700690
Sunny Goyal48461932015-03-09 17:41:09 -0700691 private int getTargetRank(DragObject d, float[] recycle) {
692 recycle = d.getVisualCenter(recycle);
693 return mContent.findNearestArea(
694 (int) recycle[0] - getPaddingLeft(), (int) recycle[1] - getPaddingTop());
695 }
696
Adam Cohen091440a2015-03-18 14:16:05 -0700697 @Thunk void onDragOver(DragObject d, int reorderDelay) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700698 if (mScrollPauseAlarm.alarmPending()) {
Sunny Goyal48461932015-03-09 17:41:09 -0700699 return;
700 }
701 final float[] r = new float[2];
702 mTargetRank = getTargetRank(d, r);
703
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800704 if (mTargetRank != mPrevTargetRank) {
Alan Viverette4cda5b72013-08-28 17:53:41 -0700705 mReorderAlarm.cancelAlarm();
Sunny Goyalc46bfef2015-01-05 12:40:08 -0800706 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
707 mReorderAlarm.setAlarm(REORDER_DELAY);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800708 mPrevTargetRank = mTargetRank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700709 }
Sunny Goyal48461932015-03-09 17:41:09 -0700710
Sunny Goyal48461932015-03-09 17:41:09 -0700711 float x = r[0];
Sunny Goyalb8634152015-04-09 14:17:14 -0700712 int currentPage = mContent.getNextPage();
Sunny Goyal48461932015-03-09 17:41:09 -0700713
Sunny Goyalb8634152015-04-09 14:17:14 -0700714 float cellOverlap = mContent.getCurrentCellLayout().getCellWidth()
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700715 * ICON_OVERSCROLL_WIDTH_FACTOR;
716 boolean isOutsideLeftEdge = x < cellOverlap;
717 boolean isOutsideRightEdge = x > (getWidth() - cellOverlap);
Sunny Goyal48461932015-03-09 17:41:09 -0700718
Sunny Goyal70660032015-05-14 00:07:08 -0700719 if (currentPage > 0 && (mContent.mIsRtl ? isOutsideRightEdge : isOutsideLeftEdge)) {
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700720 showScrollHint(DragController.SCROLL_LEFT, d);
Sunny Goyalb8634152015-04-09 14:17:14 -0700721 } else if (currentPage < (mContent.getPageCount() - 1)
Sunny Goyal70660032015-05-14 00:07:08 -0700722 && (mContent.mIsRtl ? isOutsideLeftEdge : isOutsideRightEdge)) {
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700723 showScrollHint(DragController.SCROLL_RIGHT, d);
Sunny Goyal48461932015-03-09 17:41:09 -0700724 } else {
725 mOnScrollHintAlarm.cancelAlarm();
726 if (mScrollHintDir != DragController.SCROLL_NONE) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700727 mContent.clearScrollHint();
Sunny Goyal48461932015-03-09 17:41:09 -0700728 mScrollHintDir = DragController.SCROLL_NONE;
729 }
730 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700731 }
732
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700733 private void showScrollHint(int direction, DragObject d) {
734 // Show scroll hint on the right
735 if (mScrollHintDir != direction) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700736 mContent.showScrollHint(direction);
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700737 mScrollHintDir = direction;
738 }
739
740 // Set alarm for when the hint is complete
741 if (!mOnScrollHintAlarm.alarmPending() || mCurrentScrollDir != direction) {
742 mCurrentScrollDir = direction;
743 mOnScrollHintAlarm.cancelAlarm();
744 mOnScrollHintAlarm.setOnAlarmListener(new OnScrollHintListener(d));
745 mOnScrollHintAlarm.setAlarm(SCROLL_HINT_DURATION);
746
747 mReorderAlarm.cancelAlarm();
748 mTargetRank = mEmptyCellRank;
749 }
750 }
751
Adam Cohenbfbfd262011-06-13 16:55:12 -0700752 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
753 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700754 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700755 }
756 };
757
Adam Cohen95bb8002011-07-03 23:40:28 -0700758 public void completeDragExit() {
Sunny Goyalf4066152015-04-15 09:42:19 -0700759 if (mInfo.opened) {
760 mLauncher.closeFolder();
761 mRearrangeOnClose = true;
Sunny Goyal31abc292015-05-01 10:42:32 -0700762 } else if (mState == STATE_ANIMATING) {
763 mRearrangeOnClose = true;
Sunny Goyalf4066152015-04-15 09:42:19 -0700764 } else {
765 rearrangeChildren();
Sunny Goyal31abc292015-05-01 10:42:32 -0700766 clearDragInfo();
Sunny Goyalf4066152015-04-15 09:42:19 -0700767 }
Sunny Goyal31abc292015-05-01 10:42:32 -0700768 }
769
770 private void clearDragInfo() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700771 mCurrentDragInfo = null;
772 mCurrentDragView = null;
773 mSuppressOnAdd = false;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800774 mIsExternalDrag = false;
Adam Cohen3e8f8112011-07-02 18:03:00 -0700775 }
776
Adam Cohencb3382b2011-05-24 14:07:08 -0700777 public void onDragExit(DragObject d) {
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700778 // We only close the folder if this is a true drag exit, ie. not because
779 // a drop has occurred above the folder.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700780 if (!d.dragComplete) {
781 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
782 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
783 }
784 mReorderAlarm.cancelAlarm();
Sunny Goyal48461932015-03-09 17:41:09 -0700785
Sunny Goyalb8634152015-04-09 14:17:14 -0700786 mOnScrollHintAlarm.cancelAlarm();
787 mScrollPauseAlarm.cancelAlarm();
788 if (mScrollHintDir != DragController.SCROLL_NONE) {
789 mContent.clearScrollHint();
790 mScrollHintDir = DragController.SCROLL_NONE;
Sunny Goyal48461932015-03-09 17:41:09 -0700791 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700792 }
793
Sunny Goyale9b651e2015-04-24 11:44:51 -0700794 /**
795 * When performing an accessibility drop, onDrop is sent immediately after onDragEnter. So we
796 * need to complete all transient states based on timers.
797 */
798 @Override
799 public void prepareAccessibilityDrop() {
800 if (mReorderAlarm.alarmPending()) {
801 mReorderAlarm.cancelAlarm();
802 mReorderAlarmListener.onAlarm(mReorderAlarm);
803 }
804 }
805
Michael Jurka1e2f4652013-07-08 18:03:46 -0700806 public void onDropCompleted(final View target, final DragObject d,
807 final boolean isFlingToDelete, final boolean success) {
808 if (mDeferDropAfterUninstall) {
Michael Jurkaf3007582013-08-21 14:33:57 +0200809 Log.d(TAG, "Deferred handling drop because waiting for uninstall.");
Michael Jurka1e2f4652013-07-08 18:03:46 -0700810 mDeferredAction = new Runnable() {
811 public void run() {
812 onDropCompleted(target, d, isFlingToDelete, success);
813 mDeferredAction = null;
814 }
815 };
816 return;
817 }
818
819 boolean beingCalledAfterUninstall = mDeferredAction != null;
820 boolean successfulDrop =
821 success && (!beingCalledAfterUninstall || mUninstallSuccessful);
Winson Chung5f8afe62013-08-12 16:19:28 -0700822
Michael Jurka1e2f4652013-07-08 18:03:46 -0700823 if (successfulDrop) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800824 if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon && target != this) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700825 replaceFolderWithFinalItem();
826 }
827 } else {
828 // The drag failed, we need to return the item to the folder
Sunny Goyalb8c663c2015-04-23 11:43:48 -0700829 ShortcutInfo info = (ShortcutInfo) d.dragInfo;
830 View icon = (mCurrentDragView != null && mCurrentDragView.getTag() == info)
831 ? mCurrentDragView : mContent.createNewView(info);
832 ArrayList<View> views = getItemsInReadingOrder();
833 views.add(info.rank, icon);
834 mContent.arrangeChildren(views, views.size());
835 mItemsInvalidated = true;
836
837 mSuppressOnAdd = true;
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700838 mFolderIcon.onDrop(d);
Sunny Goyalb8c663c2015-04-23 11:43:48 -0700839 mSuppressOnAdd = false;
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700840 }
841
842 if (target != this) {
843 if (mOnExitAlarm.alarmPending()) {
844 mOnExitAlarm.cancelAlarm();
Michael Jurka54554252013-08-01 12:52:23 +0200845 if (!successfulDrop) {
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700846 mSuppressFolderDeletion = true;
847 }
Sunny Goyal5d85c442015-03-10 13:14:47 -0700848 mScrollPauseAlarm.cancelAlarm();
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700849 completeDragExit();
850 }
Adam Cohen9c58d822013-12-13 17:18:10 -0800851 }
852
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700853 mDeleteFolderOnDropCompleted = false;
854 mDragInProgress = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700855 mItemAddedBackToSelfViaIcon = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700856 mCurrentDragInfo = null;
857 mCurrentDragView = null;
858 mSuppressOnAdd = false;
Adam Cohen4045eb72011-10-06 11:44:26 -0700859
860 // Reordering may have occured, and we need to save the new item locations. We do this once
861 // at the end to prevent unnecessary database operations.
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700862 updateItemLocationsInDatabaseBatch();
Sunny Goyalb7e15ad2015-05-07 14:51:31 -0700863
864 // Use the item count to check for multi-page as the folder UI may not have
865 // been refreshed yet.
866 if (getItemCount() <= mContent.itemsPerPage()) {
867 // Show the animation, next time something is added to the folder.
868 mInfo.setOption(FolderInfo.FLAG_MULTI_PAGE_ANIMATION, false, mLauncher);
869 }
870
Adam Cohen4045eb72011-10-06 11:44:26 -0700871 }
872
Sunny Goyalfa401a12015-04-10 13:45:42 -0700873 @Override
Michael Jurka1e2f4652013-07-08 18:03:46 -0700874 public void deferCompleteDropAfterUninstallActivity() {
875 mDeferDropAfterUninstall = true;
876 }
877
Sunny Goyalfa401a12015-04-10 13:45:42 -0700878 @Override
Michael Jurka1e2f4652013-07-08 18:03:46 -0700879 public void onUninstallActivityReturned(boolean success) {
880 mDeferDropAfterUninstall = false;
881 mUninstallSuccessful = success;
882 if (mDeferredAction != null) {
883 mDeferredAction.run();
884 }
885 }
886
Winson Chunga48487a2012-03-20 16:19:37 -0700887 @Override
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800888 public float getIntrinsicIconScaleFactor() {
889 return 1f;
890 }
891
892 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800893 public boolean supportsFlingToDelete() {
894 return true;
895 }
896
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000897 @Override
898 public boolean supportsAppInfoDropTarget() {
899 return false;
900 }
901
902 @Override
903 public boolean supportsDeleteDropTarget() {
904 return true;
905 }
906
Sunny Goyalddec7342015-04-29 18:12:37 -0700907 @Override
908 public void onFlingToDelete(DragObject d, PointF vec) {
Winson Chunga48487a2012-03-20 16:19:37 -0700909 // Do nothing
910 }
911
912 @Override
913 public void onFlingToDeleteCompleted() {
914 // Do nothing
915 }
916
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700917 private void updateItemLocationsInDatabaseBatch() {
918 ArrayList<View> list = getItemsInReadingOrder();
919 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
920 for (int i = 0; i < list.size(); i++) {
921 View v = list.get(i);
922 ItemInfo info = (ItemInfo) v.getTag();
Sunny Goyal08f72612015-01-05 13:41:43 -0800923 info.rank = i;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700924 items.add(info);
925 }
926
927 LauncherModel.moveItemsInDatabase(mLauncher, items, mInfo.id, 0);
928 }
929
Adam Cohene25af792013-06-06 23:08:25 -0700930 public void addItemLocationsInDatabase() {
931 ArrayList<View> list = getItemsInReadingOrder();
932 for (int i = 0; i < list.size(); i++) {
933 View v = list.get(i);
934 ItemInfo info = (ItemInfo) v.getTag();
935 LauncherModel.addItemToDatabase(mLauncher, info, mInfo.id, 0,
Winson Chung8f1eff72015-05-28 17:33:40 -0700936 info.cellX, info.cellY);
Adam Cohene25af792013-06-06 23:08:25 -0700937 }
938 }
939
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700940 public void notifyDrop() {
941 if (mDragInProgress) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700942 mItemAddedBackToSelfViaIcon = true;
Adam Cohen76078c42011-06-09 15:06:52 -0700943 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700944 }
945
946 public boolean isDropEnabled() {
947 return true;
948 }
949
Adam Cohen2801caf2011-05-13 20:57:39 -0700950 public boolean isFull() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800951 return mContent.isFull();
Adam Cohen2801caf2011-05-13 20:57:39 -0700952 }
953
954 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700955 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700956
Winson Chung892c74d2013-08-22 16:15:50 -0700957 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700958 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700959 int height = getFolderHeight();
Adam Cohen2801caf2011-05-13 20:57:39 -0700960
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800961 float scale = parent.getDescendantRectRelativeToSelf(mFolderIcon, sTempRect);
Adam Cohen8e776a62011-06-28 18:10:06 -0700962
Adam Cohen2e6da152015-05-06 11:42:25 -0700963 DeviceProfile grid = mLauncher.getDeviceProfile();
Winson Chungaf40f202013-09-18 18:26:31 -0700964
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800965 int centerX = (int) (sTempRect.left + sTempRect.width() * scale / 2);
966 int centerY = (int) (sTempRect.top + sTempRect.height() * scale / 2);
Adam Cohen2801caf2011-05-13 20:57:39 -0700967 int centeredLeft = centerX - width / 2;
968 int centeredTop = centerY - height / 2;
Adam Cohen2801caf2011-05-13 20:57:39 -0700969
Sunny Goyal41d84172015-07-08 23:28:51 -0700970 // We need to bound the folder to the currently visible workspace area
971 mLauncher.getWorkspace().getPageAreaRelativeToDragLayer(sTempRect);
972 int left = Math.min(Math.max(sTempRect.left, centeredLeft),
973 sTempRect.left + sTempRect.width() - width);
974 int top = Math.min(Math.max(sTempRect.top, centeredTop),
975 sTempRect.top + sTempRect.height() - height);
Sunny Goyalc6205602015-05-21 20:46:33 -0700976 if (grid.isPhone && (grid.availableWidthPx - width) < grid.iconSizePx) {
Winson Chungaf40f202013-09-18 18:26:31 -0700977 // Center the folder if it is full (on phones only)
978 left = (grid.availableWidthPx - width) / 2;
Sunny Goyal41d84172015-07-08 23:28:51 -0700979 } else if (width >= sTempRect.width()) {
Winson Chungaf40f202013-09-18 18:26:31 -0700980 // If the folder doesn't fit within the bounds, center it about the desired bounds
Sunny Goyal41d84172015-07-08 23:28:51 -0700981 left = sTempRect.left + (sTempRect.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700982 }
Sunny Goyal41d84172015-07-08 23:28:51 -0700983 if (height >= sTempRect.height()) {
984 top = sTempRect.top + (sTempRect.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700985 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700986
987 int folderPivotX = width / 2 + (centeredLeft - left);
988 int folderPivotY = height / 2 + (centeredTop - top);
989 setPivotX(folderPivotX);
990 setPivotY(folderPivotY);
Adam Cohen268c4752012-06-06 17:47:33 -0700991 mFolderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700992 (1.0f * folderPivotX / width));
Adam Cohen268c4752012-06-06 17:47:33 -0700993 mFolderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700994 (1.0f * folderPivotY / height));
Adam Cohen3bf84d32012-05-07 20:17:14 -0700995
Adam Cohen662b5982011-12-13 17:45:21 -0800996 lp.width = width;
997 lp.height = height;
998 lp.x = left;
999 lp.y = top;
Adam Cohen2801caf2011-05-13 20:57:39 -07001000 }
1001
Adam Cohen268c4752012-06-06 17:47:33 -07001002 float getPivotXForIconAnimation() {
1003 return mFolderIconPivotX;
1004 }
1005 float getPivotYForIconAnimation() {
1006 return mFolderIconPivotY;
1007 }
1008
Winson Chung892c74d2013-08-22 16:15:50 -07001009 private int getContentAreaHeight() {
Adam Cohen2e6da152015-05-06 11:42:25 -07001010 DeviceProfile grid = mLauncher.getDeviceProfile();
Sunny Goyalc6205602015-05-21 20:46:33 -07001011 Rect workspacePadding = grid.getWorkspacePadding(mContent.mIsRtl);
Winson Chung892c74d2013-08-22 16:15:50 -07001012 int maxContentAreaHeight = grid.availableHeightPx -
Winson Chung892c74d2013-08-22 16:15:50 -07001013 workspacePadding.top - workspacePadding.bottom -
Sunny Goyal290800b2015-03-05 11:33:33 -08001014 mFooterHeight;
Adam Cohen1960ea42013-11-12 11:33:14 +00001015 int height = Math.min(maxContentAreaHeight,
Winson Chung892c74d2013-08-22 16:15:50 -07001016 mContent.getDesiredHeight());
Adam Cohen1960ea42013-11-12 11:33:14 +00001017 return Math.max(height, MIN_CONTENT_DIMEN);
1018 }
1019
1020 private int getContentAreaWidth() {
1021 return Math.max(mContent.getDesiredWidth(), MIN_CONTENT_DIMEN);
Winson Chung892c74d2013-08-22 16:15:50 -07001022 }
1023
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001024 private int getFolderHeight() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001025 return getFolderHeight(getContentAreaHeight());
1026 }
1027
1028 private int getFolderHeight(int contentAreaHeight) {
Sunny Goyal290800b2015-03-05 11:33:33 -08001029 return getPaddingTop() + getPaddingBottom() + contentAreaHeight + mFooterHeight;
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001030 }
Adam Cohen234c4cd2011-07-17 21:03:04 -07001031
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001032 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001033 int contentWidth = getContentAreaWidth();
1034 int contentHeight = getContentAreaHeight();
Adam Cohen1960ea42013-11-12 11:33:14 +00001035
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001036 int contentAreaWidthSpec = MeasureSpec.makeMeasureSpec(contentWidth, MeasureSpec.EXACTLY);
1037 int contentAreaHeightSpec = MeasureSpec.makeMeasureSpec(contentHeight, MeasureSpec.EXACTLY);
1038
1039 mContent.setFixedSize(contentWidth, contentHeight);
1040 mContentWrapper.measure(contentAreaWidthSpec, contentAreaHeightSpec);
Sunny Goyala07c2f52015-06-01 11:00:38 -07001041
1042 if (mContent.getChildCount() > 0) {
1043 int cellIconGap = (mContent.getPageAt(0).getCellWidth()
1044 - mLauncher.getDeviceProfile().iconSizePx) / 2;
1045 mFooter.setPadding(mContent.getPaddingLeft() + cellIconGap,
1046 mFooter.getPaddingTop(),
1047 mContent.getPaddingRight() + cellIconGap,
1048 mFooter.getPaddingBottom());
1049 }
Sunny Goyal290800b2015-03-05 11:33:33 -08001050 mFooter.measure(contentAreaWidthSpec,
1051 MeasureSpec.makeMeasureSpec(mFooterHeight, MeasureSpec.EXACTLY));
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001052
1053 int folderWidth = getPaddingLeft() + getPaddingRight() + contentWidth;
1054 int folderHeight = getFolderHeight(contentHeight);
1055 setMeasuredDimension(folderWidth, folderHeight);
Adam Cohen234c4cd2011-07-17 21:03:04 -07001056 }
1057
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001058 /**
1059 * Rearranges the children based on their rank.
1060 */
1061 public void rearrangeChildren() {
1062 rearrangeChildren(-1);
1063 }
Adam Cohen2801caf2011-05-13 20:57:39 -07001064
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001065 /**
1066 * Rearranges the children based on their rank.
1067 * @param itemCount if greater than the total children count, empty spaces are left at the end,
1068 * otherwise it is ignored.
1069 */
1070 public void rearrangeChildren(int itemCount) {
1071 ArrayList<View> views = getItemsInReadingOrder();
1072 mContent.arrangeChildren(views, Math.max(itemCount, views.size()));
Adam Cohen7c693212011-05-18 15:26:57 -07001073 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -07001074 }
1075
Sunny Goyalc4918352015-03-10 18:15:48 -07001076 // TODO remove this once GSA code fix is submitted
1077 public ViewGroup getContent() {
1078 return (ViewGroup) mContent;
1079 }
1080
Adam Cohena9cf38f2011-05-02 15:36:58 -07001081 public int getItemCount() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001082 return mContent.getItemCount();
Adam Cohena9cf38f2011-05-02 15:36:58 -07001083 }
1084
Adam Cohen091440a2015-03-18 14:16:05 -07001085 @Thunk void onCloseComplete() {
Adam Cohen05e0f402011-08-01 12:12:49 -07001086 DragLayer parent = (DragLayer) getParent();
Michael Jurka5649c282012-06-18 10:33:21 -07001087 if (parent != null) {
1088 parent.removeView(this);
1089 }
Adam Cohen4554ee12011-08-03 16:13:21 -07001090 mDragController.removeDropTarget((DropTarget) this);
Adam Cohen05e0f402011-08-01 12:12:49 -07001091 clearFocus();
Adam Cohenac56cff2011-09-28 20:45:37 -07001092 mFolderIcon.requestFocus();
Adam Cohen05e0f402011-08-01 12:12:49 -07001093
Adam Cohen2801caf2011-05-13 20:57:39 -07001094 if (mRearrangeOnClose) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001095 rearrangeChildren();
Adam Cohen2801caf2011-05-13 20:57:39 -07001096 mRearrangeOnClose = false;
1097 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001098 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001099 if (!mDragInProgress && !mSuppressFolderDeletion) {
1100 replaceFolderWithFinalItem();
1101 } else if (mDragInProgress) {
1102 mDeleteFolderOnDropCompleted = true;
1103 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001104 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001105 mSuppressFolderDeletion = false;
Sunny Goyal31abc292015-05-01 10:42:32 -07001106 clearDragInfo();
Adam Cohenafb01ee2011-06-23 15:38:03 -07001107 }
1108
Adam Cohen091440a2015-03-18 14:16:05 -07001109 @Thunk void replaceFolderWithFinalItem() {
Adam Cohenafb01ee2011-06-23 15:38:03 -07001110 // Add the last remaining child to the workspace in place of the folder
Adam Cohenfb91f302012-06-11 15:45:18 -07001111 Runnable onCompleteRunnable = new Runnable() {
1112 @Override
1113 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -07001114 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screenId);
Adam Cohenafb01ee2011-06-23 15:38:03 -07001115
Winson Chung33231f52013-12-09 16:57:45 -08001116 View child = null;
Adam Cohenfb91f302012-06-11 15:45:18 -07001117 // Move the item from the folder to the workspace, in the position of the folder
1118 if (getItemCount() == 1) {
1119 ShortcutInfo finalItem = mInfo.contents.get(0);
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001120 child = mLauncher.createShortcut(cellLayout, finalItem);
Adam Cohenfb91f302012-06-11 15:45:18 -07001121 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -07001122 mInfo.screenId, mInfo.cellX, mInfo.cellY);
Adam Cohenfb91f302012-06-11 15:45:18 -07001123 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001124 if (getItemCount() <= 1) {
1125 // Remove the folder
1126 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
Dan Sandler0eb687f2014-01-10 13:24:55 -05001127 if (cellLayout != null) {
1128 // b/12446428 -- sometimes the cell layout has already gone away?
1129 cellLayout.removeView(mFolderIcon);
1130 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001131 if (mFolderIcon instanceof DropTarget) {
1132 mDragController.removeDropTarget((DropTarget) mFolderIcon);
1133 }
1134 mLauncher.removeFolder(mInfo);
1135 }
Adam Cohenc5e63f32012-07-12 16:16:57 -07001136 // We add the child after removing the folder to prevent both from existing at
Winson Chung0e6a7132013-08-23 12:55:10 -07001137 // the same time in the CellLayout. We need to add the new item with addInScreenFromBind()
1138 // to ensure that hotseat items are placed correctly.
Adam Cohenc5e63f32012-07-12 16:16:57 -07001139 if (child != null) {
Winson Chung0e6a7132013-08-23 12:55:10 -07001140 mLauncher.getWorkspace().addInScreenFromBind(child, mInfo.container, mInfo.screenId,
Adam Cohenc5e63f32012-07-12 16:16:57 -07001141 mInfo.cellX, mInfo.cellY, mInfo.spanX, mInfo.spanY);
1142 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001143 }
1144 };
Sunny Goyalbc753352015-03-05 09:40:44 -08001145 View finalChild = mContent.getLastItem();
Adam Cohenfb91f302012-06-11 15:45:18 -07001146 if (finalChild != null) {
1147 mFolderIcon.performDestroyAnimation(finalChild, onCompleteRunnable);
Winson Chung33231f52013-12-09 16:57:45 -08001148 } else {
1149 onCompleteRunnable.run();
Adam Cohenafb01ee2011-06-23 15:38:03 -07001150 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001151 mDestroyed = true;
1152 }
1153
1154 boolean isDestroyed() {
1155 return mDestroyed;
Adam Cohen2801caf2011-05-13 20:57:39 -07001156 }
1157
Adam Cohenac56cff2011-09-28 20:45:37 -07001158 // This method keeps track of the last item in the folder for the purposes
1159 // of keyboard focus
Sunny Goyal290800b2015-03-05 11:33:33 -08001160 public void updateTextViewFocus() {
Sunny Goyalbc753352015-03-05 09:40:44 -08001161 View lastChild = mContent.getLastItem();
Adam Cohenac56cff2011-09-28 20:45:37 -07001162 if (lastChild != null) {
1163 mFolderName.setNextFocusDownId(lastChild.getId());
1164 mFolderName.setNextFocusRightId(lastChild.getId());
1165 mFolderName.setNextFocusLeftId(lastChild.getId());
1166 mFolderName.setNextFocusUpId(lastChild.getId());
1167 }
1168 }
1169
Adam Cohenbfbfd262011-06-13 16:55:12 -07001170 public void onDrop(DragObject d) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001171 Runnable cleanUpRunnable = null;
1172
Adam Cohen689ff162014-05-08 17:27:56 -07001173 // If we are coming from All Apps space, we defer removing the extra empty screen
1174 // until the folder closes
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001175 if (d.dragSource != mLauncher.getWorkspace() && !(d.dragSource instanceof Folder)) {
1176 cleanUpRunnable = new Runnable() {
1177 @Override
1178 public void run() {
Adam Cohen689ff162014-05-08 17:27:56 -07001179 mLauncher.exitSpringLoadedDragModeDelayed(true,
1180 Launcher.EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
1181 null);
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001182 }
1183 };
Adam Cohenbfbfd262011-06-13 16:55:12 -07001184 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001185
Sunny Goyalb8634152015-04-09 14:17:14 -07001186 // If the icon was dropped while the page was being scrolled, we need to compute
1187 // the target location again such that the icon is placed of the final page.
1188 if (!mContent.rankOnCurrentPage(mEmptyCellRank)) {
1189 // Reorder again.
1190 mTargetRank = getTargetRank(d, null);
Sunny Goyal48461932015-03-09 17:41:09 -07001191
Sunny Goyalb8634152015-04-09 14:17:14 -07001192 // Rearrange items immediately.
1193 mReorderAlarmListener.onAlarm(mReorderAlarm);
Sunny Goyal48461932015-03-09 17:41:09 -07001194
Sunny Goyalb8634152015-04-09 14:17:14 -07001195 mOnScrollHintAlarm.cancelAlarm();
1196 mScrollPauseAlarm.cancelAlarm();
Sunny Goyal48461932015-03-09 17:41:09 -07001197 }
Sunny Goyalb8634152015-04-09 14:17:14 -07001198 mContent.completePendingPageChanges();
Sunny Goyal48461932015-03-09 17:41:09 -07001199
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001200 View currentDragView;
1201 ShortcutInfo si = mCurrentDragInfo;
1202 if (mIsExternalDrag) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001203 currentDragView = mContent.createAndAddViewForRank(si, mEmptyCellRank);
Sunny Goyal95abbb32014-08-04 10:53:22 -07001204 // Actually move the item in the database if it was an external drag. Call this
1205 // before creating the view, so that ShortcutInfo is updated appropriately.
1206 LauncherModel.addOrMoveItemInDatabase(
1207 mLauncher, si, mInfo.id, 0, si.cellX, si.cellY);
1208
1209 // We only need to update the locations if it doesn't get handled in #onDropCompleted.
1210 if (d.dragSource != this) {
1211 updateItemLocationsInDatabaseBatch();
1212 }
1213 mIsExternalDrag = false;
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001214 } else {
1215 currentDragView = mCurrentDragView;
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001216 mContent.addViewForRank(currentDragView, si, mEmptyCellRank);
Adam Cohenbfbfd262011-06-13 16:55:12 -07001217 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001218
1219 if (d.dragView.hasDrawn()) {
1220
1221 // Temporarily reset the scale such that the animation target gets calculated correctly.
1222 float scaleX = getScaleX();
1223 float scaleY = getScaleY();
1224 setScaleX(1.0f);
1225 setScaleY(1.0f);
1226 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, currentDragView,
1227 cleanUpRunnable, null);
1228 setScaleX(scaleX);
1229 setScaleY(scaleY);
1230 } else {
1231 d.deferDragViewCleanupPostAnimation = false;
1232 currentDragView.setVisibility(VISIBLE);
1233 }
1234 mItemsInvalidated = true;
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001235 rearrangeChildren();
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001236
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001237 // Temporarily suppress the listener, as we did all the work already here.
1238 mSuppressOnAdd = true;
1239 mInfo.add(si);
1240 mSuppressOnAdd = false;
Sunny Goyal4b020172014-08-28 14:51:14 -07001241 // Clear the drag info, as it is no longer being dragged.
1242 mCurrentDragInfo = null;
Sunny Goyalf4066152015-04-15 09:42:19 -07001243 mDragInProgress = false;
Sunny Goyalb7e15ad2015-05-07 14:51:31 -07001244
1245 if (mContent.getPageCount() > 1) {
1246 // The animation has already been shown while opening the folder.
1247 mInfo.setOption(FolderInfo.FLAG_MULTI_PAGE_ANIMATION, true, mLauncher);
1248 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001249 }
1250
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001251 // This is used so the item doesn't immediately appear in the folder when added. In one case
1252 // we need to create the illusion that the item isn't added back to the folder yet, to
1253 // to correspond to the animation of the icon back into the folder. This is
1254 public void hideItem(ShortcutInfo info) {
1255 View v = getViewForInfo(info);
1256 v.setVisibility(INVISIBLE);
1257 }
1258 public void showItem(ShortcutInfo info) {
1259 View v = getViewForInfo(info);
1260 v.setVisibility(VISIBLE);
1261 }
1262
Sunny Goyalb7e15ad2015-05-07 14:51:31 -07001263 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -07001264 public void onAdd(ShortcutInfo item) {
Adam Cohen05e0f402011-08-01 12:12:49 -07001265 // If the item was dropped onto this open folder, we have done the work associated
1266 // with adding the item to the folder, as indicated by mSuppressOnAdd being set
Adam Cohenbfbfd262011-06-13 16:55:12 -07001267 if (mSuppressOnAdd) return;
Sunny Goyal5d85c442015-03-10 13:14:47 -07001268 mContent.createAndAddViewForRank(item, mContent.allocateRankForNewItem(item));
Sunny Goyal2e688a82015-03-18 10:23:39 -07001269 mItemsInvalidated = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001270 LauncherModel.addOrMoveItemInDatabase(
1271 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
1272 }
1273
Adam Cohena9cf38f2011-05-02 15:36:58 -07001274 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -07001275 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001276 // If this item is being dragged from this open folder, we have already handled
1277 // the work associated with removing the item, so we don't have to do anything here.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001278 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001279 View v = getViewForInfo(item);
Sunny Goyal290800b2015-03-05 11:33:33 -08001280 mContent.removeItem(v);
Adam Cohen2801caf2011-05-13 20:57:39 -07001281 if (mState == STATE_ANIMATING) {
1282 mRearrangeOnClose = true;
1283 } else {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001284 rearrangeChildren();
Adam Cohen2801caf2011-05-13 20:57:39 -07001285 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001286 if (getItemCount() <= 1) {
1287 replaceFolderWithFinalItem();
1288 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001289 }
Adam Cohen7c693212011-05-18 15:26:57 -07001290
Sunny Goyalbc753352015-03-05 09:40:44 -08001291 private View getViewForInfo(final ShortcutInfo item) {
1292 return mContent.iterateOverItems(new ItemOperator() {
1293
1294 @Override
1295 public boolean evaluate(ItemInfo info, View view, View parent) {
1296 return info == item;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001297 }
Sunny Goyalbc753352015-03-05 09:40:44 -08001298 });
Adam Cohendf1e4e82011-06-24 15:57:39 -07001299 }
1300
Adam Cohen76078c42011-06-09 15:06:52 -07001301 public void onItemsChanged() {
Adam Cohenac56cff2011-09-28 20:45:37 -07001302 updateTextViewFocus();
Adam Cohen76078c42011-06-09 15:06:52 -07001303 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001304
Adam Cohen76fc0852011-06-17 13:26:23 -07001305 public void onTitleChanged(CharSequence title) {
1306 }
Adam Cohen76078c42011-06-09 15:06:52 -07001307
Adam Cohen7c693212011-05-18 15:26:57 -07001308 public ArrayList<View> getItemsInReadingOrder() {
1309 if (mItemsInvalidated) {
1310 mItemsInReadingOrder.clear();
Sunny Goyalbc753352015-03-05 09:40:44 -08001311 mContent.iterateOverItems(new ItemOperator() {
1312
1313 @Override
1314 public boolean evaluate(ItemInfo info, View view, View parent) {
1315 mItemsInReadingOrder.add(view);
1316 return false;
Adam Cohen7c693212011-05-18 15:26:57 -07001317 }
Sunny Goyalbc753352015-03-05 09:40:44 -08001318 });
Adam Cohen7c693212011-05-18 15:26:57 -07001319 mItemsInvalidated = false;
1320 }
1321 return mItemsInReadingOrder;
1322 }
Adam Cohen8dfcba42011-07-07 16:38:18 -07001323
1324 public void getLocationInDragLayer(int[] loc) {
1325 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
1326 }
Adam Cohenea0818d2011-09-30 20:06:58 -07001327
1328 public void onFocusChange(View v, boolean hasFocus) {
1329 if (v == mFolderName && hasFocus) {
1330 startEditingFolderName();
1331 }
1332 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001333
1334 @Override
1335 public void getHitRectRelativeToDragLayer(Rect outRect) {
1336 getHitRect(outRect);
Sunny Goyal48461932015-03-09 17:41:09 -07001337 outRect.left -= mScrollAreaOffset;
1338 outRect.right += mScrollAreaOffset;
1339 }
1340
Winson Chung8f1eff72015-05-28 17:33:40 -07001341 @Override
1342 public void fillInLaunchSourceData(Bundle sourceData) {
1343 // Fill in from the folder icon's launch source provider first
1344 Stats.LaunchSourceUtils.populateSourceDataFromAncestorProvider(mFolderIcon, sourceData);
1345 sourceData.putString(Stats.SOURCE_EXTRA_SUB_CONTAINER, Stats.SUB_CONTAINER_FOLDER);
1346 sourceData.putInt(Stats.SOURCE_EXTRA_SUB_CONTAINER_PAGE, mContent.getCurrentPage());
1347 }
1348
Sunny Goyal48461932015-03-09 17:41:09 -07001349 private class OnScrollHintListener implements OnAlarmListener {
1350
1351 private final DragObject mDragObject;
1352
1353 OnScrollHintListener(DragObject object) {
1354 mDragObject = object;
1355 }
1356
1357 /**
1358 * Scroll hint has been shown long enough. Now scroll to appropriate page.
1359 */
1360 @Override
1361 public void onAlarm(Alarm alarm) {
1362 if (mCurrentScrollDir == DragController.SCROLL_LEFT) {
Sunny Goyalb8634152015-04-09 14:17:14 -07001363 mContent.scrollLeft();
Sunny Goyal48461932015-03-09 17:41:09 -07001364 mScrollHintDir = DragController.SCROLL_NONE;
1365 } else if (mCurrentScrollDir == DragController.SCROLL_RIGHT) {
Sunny Goyalb8634152015-04-09 14:17:14 -07001366 mContent.scrollRight();
Sunny Goyal48461932015-03-09 17:41:09 -07001367 mScrollHintDir = DragController.SCROLL_NONE;
1368 } else {
1369 // This should not happen
1370 return;
1371 }
1372 mCurrentScrollDir = DragController.SCROLL_NONE;
1373
1374 // Pause drag event until the scrolling is finished
1375 mScrollPauseAlarm.setOnAlarmListener(new OnScrollFinishedListener(mDragObject));
1376 mScrollPauseAlarm.setAlarm(DragController.RESCROLL_DELAY);
1377 }
1378 }
1379
1380 private class OnScrollFinishedListener implements OnAlarmListener {
1381
1382 private final DragObject mDragObject;
1383
1384 OnScrollFinishedListener(DragObject object) {
1385 mDragObject = object;
1386 }
1387
1388 /**
1389 * Page scroll is complete.
1390 */
1391 @Override
1392 public void onAlarm(Alarm alarm) {
1393 // Reorder immediately on page change.
1394 onDragOver(mDragObject, 1);
1395 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001396 }
Sunny Goyal1dd0f8b2015-07-06 13:04:02 -07001397
1398 // Compares item position based on rank and position giving priority to the rank.
Sunny Goyal317698b2015-07-29 11:45:41 -07001399 public static final Comparator<ItemInfo> ITEM_POS_COMPARATOR = new Comparator<ItemInfo>() {
Sunny Goyal1dd0f8b2015-07-06 13:04:02 -07001400
1401 @Override
1402 public int compare(ItemInfo lhs, ItemInfo rhs) {
1403 if (lhs.rank != rhs.rank) {
1404 return lhs.rank - rhs.rank;
1405 } else if (lhs.cellY != rhs.cellY) {
1406 return lhs.cellY - rhs.cellY;
1407 } else {
1408 return lhs.cellX - rhs.cellX;
1409 }
1410 }
1411 };
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001412}