blob: 4af56ac6e611d96f5fb42c606727fdb1d77d4f66 [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
Sunny Goyal5d2fc322015-07-06 22:52:49 -0700460 final ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(this, 1, 1, 1);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700461 oa.setDuration(mExpandDuration);
462 openFolderAnim = oa;
463
464 setLayerType(LAYER_TYPE_HARDWARE, null);
465 onCompleteRunnable = new Runnable() {
466 @Override
467 public void run() {
468 setLayerType(LAYER_TYPE_NONE, null);
469 }
470 };
471 } else {
472 prepareReveal();
473 centerAboutIcon();
474
Sunny Goyal2245fa22015-07-06 11:45:18 -0700475 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700476 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
477 int height = getFolderHeight();
478
479 float transX = - 0.075f * (width / 2 - getPivotX());
480 float transY = - 0.075f * (height / 2 - getPivotY());
481 setTranslationX(transX);
482 setTranslationY(transY);
Sunny Goyal5d2fc322015-07-06 22:52:49 -0700483 PropertyValuesHolder tx = PropertyValuesHolder.ofFloat(TRANSLATION_X, transX, 0);
484 PropertyValuesHolder ty = PropertyValuesHolder.ofFloat(TRANSLATION_Y, transY, 0);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700485
Sunny Goyal2245fa22015-07-06 11:45:18 -0700486 Animator drift = ObjectAnimator.ofPropertyValuesHolder(this, tx, ty);
Sunny Goyal75deaf32015-05-07 16:13:12 -0700487 drift.setDuration(mMaterialExpandDuration);
488 drift.setStartDelay(mMaterialExpandStagger);
489 drift.setInterpolator(new LogDecelerateInterpolator(100, 0));
490
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700491 int rx = (int) Math.max(Math.max(width - getPivotX(), 0), getPivotX());
492 int ry = (int) Math.max(Math.max(height - getPivotY(), 0), getPivotY());
Sunny Goyalf7a29e82015-04-24 15:20:43 -0700493 float radius = (float) Math.hypot(rx, ry);
Sunny Goyal75deaf32015-05-07 16:13:12 -0700494
Sunny Goyal2245fa22015-07-06 11:45:18 -0700495 Animator reveal = UiThreadCircularReveal.createCircularReveal(this, (int) getPivotX(),
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700496 (int) getPivotY(), 0, radius);
497 reveal.setDuration(mMaterialExpandDuration);
498 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
499
Sunny Goyalbc753352015-03-05 09:40:44 -0800500 mContentWrapper.setAlpha(0f);
Sunny Goyal2245fa22015-07-06 11:45:18 -0700501 Animator iconsAlpha = ObjectAnimator.ofFloat(mContentWrapper, "alpha", 0f, 1f);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700502 iconsAlpha.setDuration(mMaterialExpandDuration);
503 iconsAlpha.setStartDelay(mMaterialExpandStagger);
504 iconsAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
505
Sunny Goyal290800b2015-03-05 11:33:33 -0800506 mFooter.setAlpha(0f);
Sunny Goyal2245fa22015-07-06 11:45:18 -0700507 Animator textAlpha = ObjectAnimator.ofFloat(mFooter, "alpha", 0f, 1f);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700508 textAlpha.setDuration(mMaterialExpandDuration);
509 textAlpha.setStartDelay(mMaterialExpandStagger);
510 textAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
511
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700512 anim.play(drift);
513 anim.play(iconsAlpha);
514 anim.play(textAlpha);
515 anim.play(reveal);
516
517 openFolderAnim = anim;
518
Sunny Goyalbc753352015-03-05 09:40:44 -0800519 mContentWrapper.setLayerType(LAYER_TYPE_HARDWARE, null);
Sunny Goyal75deaf32015-05-07 16:13:12 -0700520 mFooter.setLayerType(LAYER_TYPE_HARDWARE, null);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700521 onCompleteRunnable = new Runnable() {
522 @Override
523 public void run() {
Sunny Goyalbc753352015-03-05 09:40:44 -0800524 mContentWrapper.setLayerType(LAYER_TYPE_NONE, null);
Sunny Goyal75deaf32015-05-07 16:13:12 -0700525 mContentWrapper.setLayerType(LAYER_TYPE_NONE, null);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700526 }
527 };
528 }
529 openFolderAnim.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700530 @Override
531 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700532 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Sunny Goyalbc753352015-03-05 09:40:44 -0800533 mContent.getAccessibilityDescription());
Adam Cohendf2cc412011-04-27 16:56:57 -0700534 mState = STATE_ANIMATING;
535 }
536 @Override
537 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700538 mState = STATE_OPEN;
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700539
540 if (onCompleteRunnable != null) {
541 onCompleteRunnable.run();
542 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800543
Sunny Goyalbc753352015-03-05 09:40:44 -0800544 mContent.setFocusOnFirstChild();
Adam Cohendf2cc412011-04-27 16:56:57 -0700545 }
546 });
Sunny Goyalb7e15ad2015-05-07 14:51:31 -0700547
548 // Footer animation
549 if (mContent.getPageCount() > 1 && !mInfo.hasOption(FolderInfo.FLAG_MULTI_PAGE_ANIMATION)) {
550 int footerWidth = mContent.getDesiredWidth()
551 - mFooter.getPaddingLeft() - mFooter.getPaddingRight();
552
553 float textWidth = mFolderName.getPaint().measureText(mFolderName.getText().toString());
Sunny Goyala07c2f52015-06-01 11:00:38 -0700554 float translation = (footerWidth - textWidth) / 2;
555 mFolderName.setTranslationX(mContent.mIsRtl ? -translation : translation);
Sunny Goyalb7e15ad2015-05-07 14:51:31 -0700556 mContent.setMarkerScale(0);
557
558 // Do not update the flag if we are in drag mode. The flag will be updated, when we
559 // actually drop the icon.
560 final boolean updateAnimationFlag = !mDragInProgress;
561 openFolderAnim.addListener(new AnimatorListenerAdapter() {
562
Sunny Goyal5d2fc322015-07-06 22:52:49 -0700563 @SuppressLint("InlinedApi")
Sunny Goyalb7e15ad2015-05-07 14:51:31 -0700564 @Override
565 public void onAnimationEnd(Animator animation) {
Sunny Goyala07c2f52015-06-01 11:00:38 -0700566 mFolderName.animate().setDuration(FOLDER_NAME_ANIMATION_DURATION)
567 .translationX(0)
Sunny Goyal9fc953b2015-08-17 12:24:25 -0700568 .setInterpolator(Utilities.ATLEAST_LOLLIPOP ?
Sunny Goyalc1cd23b2015-06-01 17:15:34 -0700569 AnimationUtils.loadInterpolator(mLauncher,
570 android.R.interpolator.fast_out_slow_in)
571 : new LogDecelerateInterpolator(100, 0));
Sunny Goyalb7e15ad2015-05-07 14:51:31 -0700572 mContent.animateMarkers();
573
574 if (updateAnimationFlag) {
575 mInfo.setOption(FolderInfo.FLAG_MULTI_PAGE_ANIMATION, true, mLauncher);
576 }
577 }
578 });
579 } else {
580 mFolderName.setTranslationX(0);
581 mContent.setMarkerScale(1);
582 }
583
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700584 openFolderAnim.start();
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800585
586 // Make sure the folder picks up the last drag move even if the finger doesn't move.
587 if (mDragController.isDragging()) {
588 mDragController.forceTouchMove();
589 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700590
Sunny Goyalb8634152015-04-09 14:17:14 -0700591 FolderPagedView pages = (FolderPagedView) mContent;
592 pages.verifyVisibleHighResIcons(pages.getNextPage());
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800593 }
594
595 public void beginExternalDrag(ShortcutInfo item) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800596 mCurrentDragInfo = item;
Sunny Goyal5d85c442015-03-10 13:14:47 -0700597 mEmptyCellRank = mContent.allocateRankForNewItem(item);
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800598 mIsExternalDrag = true;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800599 mDragInProgress = true;
Sunny Goyalb8634152015-04-09 14:17:14 -0700600
Sunny Goyalf4066152015-04-15 09:42:19 -0700601 // Since this folder opened by another controller, it might not get onDrop or
602 // onDropComplete. Perform cleanup once drag-n-drop ends.
603 mDragController.addDragListener(this);
604 }
605
606 @Override
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700607 public void onDragStart(DragSource source, ItemInfo info, int dragAction) { }
Sunny Goyalf4066152015-04-15 09:42:19 -0700608
609 @Override
610 public void onDragEnd() {
611 if (mIsExternalDrag && mDragInProgress) {
612 completeDragExit();
613 }
614 mDragController.removeDragListener(this);
Adam Cohendf2cc412011-04-27 16:56:57 -0700615 }
616
Adam Cohen091440a2015-03-18 14:16:05 -0700617 @Thunk void sendCustomAccessibilityEvent(int type, String text) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700618 AccessibilityManager accessibilityManager = (AccessibilityManager)
619 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
620 if (accessibilityManager.isEnabled()) {
Adam Cohen3371da02011-10-25 21:38:29 -0700621 AccessibilityEvent event = AccessibilityEvent.obtain(type);
622 onInitializeAccessibilityEvent(event);
623 event.getText().add(text);
Michael Jurka8b805b12012-04-18 14:23:14 -0700624 accessibilityManager.sendAccessibilityEvent(event);
Adam Cohen3371da02011-10-25 21:38:29 -0700625 }
626 }
627
Adam Cohendf2cc412011-04-27 16:56:57 -0700628 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700629 if (!(getParent() instanceof DragLayer)) return;
Sunny Goyal5d2fc322015-07-06 22:52:49 -0700630 final ObjectAnimator oa = LauncherAnimUtils.ofViewAlphaAndScale(this, 0, 0.9f, 0.9f);
Adam Cohen2801caf2011-05-13 20:57:39 -0700631 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700632 @Override
633 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700634 onCloseComplete();
Michael Jurka0121c3e2012-05-31 08:36:04 -0700635 setLayerType(LAYER_TYPE_NONE, null);
Adam Cohen2801caf2011-05-13 20:57:39 -0700636 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700637 }
638 @Override
639 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700640 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700641 getContext().getString(R.string.folder_closed));
Adam Cohendf2cc412011-04-27 16:56:57 -0700642 mState = STATE_ANIMATING;
643 }
644 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700645 oa.setDuration(mExpandDuration);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700646 setLayerType(LAYER_TYPE_HARDWARE, null);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100647 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700648 }
649
Adam Cohencb3382b2011-05-24 14:07:08 -0700650 public boolean acceptDrop(DragObject d) {
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700651 final ItemInfo item = d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700652 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700653 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
654 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
655 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700656 }
657
Adam Cohencb3382b2011-05-24 14:07:08 -0700658 public void onDragEnter(DragObject d) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800659 mPrevTargetRank = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700660 mOnExitAlarm.cancelAlarm();
Sunny Goyalb8634152015-04-09 14:17:14 -0700661 // Get the area offset such that the folder only closes if half the drag icon width
662 // is outside the folder area
663 mScrollAreaOffset = d.dragView.getDragRegionWidth() / 2 - d.xOffset;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700664 }
665
666 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
667 public void onAlarm(Alarm alarm) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800668 mContent.realTimeReorder(mEmptyCellRank, mTargetRank);
669 mEmptyCellRank = mTargetRank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700670 }
671 };
672
Sunny Goyalc46bfef2015-01-05 12:40:08 -0800673 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohen2374abf2013-04-16 14:56:57 -0700674 public boolean isLayoutRtl() {
675 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
676 }
677
Sunny Goyal48461932015-03-09 17:41:09 -0700678 @Override
Adam Cohencb3382b2011-05-24 14:07:08 -0700679 public void onDragOver(DragObject d) {
Sunny Goyal48461932015-03-09 17:41:09 -0700680 onDragOver(d, REORDER_DELAY);
681 }
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700682
Sunny Goyal48461932015-03-09 17:41:09 -0700683 private int getTargetRank(DragObject d, float[] recycle) {
684 recycle = d.getVisualCenter(recycle);
685 return mContent.findNearestArea(
686 (int) recycle[0] - getPaddingLeft(), (int) recycle[1] - getPaddingTop());
687 }
688
Adam Cohen091440a2015-03-18 14:16:05 -0700689 @Thunk void onDragOver(DragObject d, int reorderDelay) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700690 if (mScrollPauseAlarm.alarmPending()) {
Sunny Goyal48461932015-03-09 17:41:09 -0700691 return;
692 }
693 final float[] r = new float[2];
694 mTargetRank = getTargetRank(d, r);
695
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800696 if (mTargetRank != mPrevTargetRank) {
Alan Viverette4cda5b72013-08-28 17:53:41 -0700697 mReorderAlarm.cancelAlarm();
Sunny Goyalc46bfef2015-01-05 12:40:08 -0800698 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
699 mReorderAlarm.setAlarm(REORDER_DELAY);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800700 mPrevTargetRank = mTargetRank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700701 }
Sunny Goyal48461932015-03-09 17:41:09 -0700702
Sunny Goyal48461932015-03-09 17:41:09 -0700703 float x = r[0];
Sunny Goyalb8634152015-04-09 14:17:14 -0700704 int currentPage = mContent.getNextPage();
Sunny Goyal48461932015-03-09 17:41:09 -0700705
Sunny Goyalb8634152015-04-09 14:17:14 -0700706 float cellOverlap = mContent.getCurrentCellLayout().getCellWidth()
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700707 * ICON_OVERSCROLL_WIDTH_FACTOR;
708 boolean isOutsideLeftEdge = x < cellOverlap;
709 boolean isOutsideRightEdge = x > (getWidth() - cellOverlap);
Sunny Goyal48461932015-03-09 17:41:09 -0700710
Sunny Goyal70660032015-05-14 00:07:08 -0700711 if (currentPage > 0 && (mContent.mIsRtl ? isOutsideRightEdge : isOutsideLeftEdge)) {
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700712 showScrollHint(DragController.SCROLL_LEFT, d);
Sunny Goyalb8634152015-04-09 14:17:14 -0700713 } else if (currentPage < (mContent.getPageCount() - 1)
Sunny Goyal70660032015-05-14 00:07:08 -0700714 && (mContent.mIsRtl ? isOutsideLeftEdge : isOutsideRightEdge)) {
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700715 showScrollHint(DragController.SCROLL_RIGHT, d);
Sunny Goyal48461932015-03-09 17:41:09 -0700716 } else {
717 mOnScrollHintAlarm.cancelAlarm();
718 if (mScrollHintDir != DragController.SCROLL_NONE) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700719 mContent.clearScrollHint();
Sunny Goyal48461932015-03-09 17:41:09 -0700720 mScrollHintDir = DragController.SCROLL_NONE;
721 }
722 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700723 }
724
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700725 private void showScrollHint(int direction, DragObject d) {
726 // Show scroll hint on the right
727 if (mScrollHintDir != direction) {
Sunny Goyalb8634152015-04-09 14:17:14 -0700728 mContent.showScrollHint(direction);
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700729 mScrollHintDir = direction;
730 }
731
732 // Set alarm for when the hint is complete
733 if (!mOnScrollHintAlarm.alarmPending() || mCurrentScrollDir != direction) {
734 mCurrentScrollDir = direction;
735 mOnScrollHintAlarm.cancelAlarm();
736 mOnScrollHintAlarm.setOnAlarmListener(new OnScrollHintListener(d));
737 mOnScrollHintAlarm.setAlarm(SCROLL_HINT_DURATION);
738
739 mReorderAlarm.cancelAlarm();
740 mTargetRank = mEmptyCellRank;
741 }
742 }
743
Adam Cohenbfbfd262011-06-13 16:55:12 -0700744 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
745 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700746 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700747 }
748 };
749
Adam Cohen95bb8002011-07-03 23:40:28 -0700750 public void completeDragExit() {
Sunny Goyalf4066152015-04-15 09:42:19 -0700751 if (mInfo.opened) {
752 mLauncher.closeFolder();
753 mRearrangeOnClose = true;
Sunny Goyal31abc292015-05-01 10:42:32 -0700754 } else if (mState == STATE_ANIMATING) {
755 mRearrangeOnClose = true;
Sunny Goyalf4066152015-04-15 09:42:19 -0700756 } else {
757 rearrangeChildren();
Sunny Goyal31abc292015-05-01 10:42:32 -0700758 clearDragInfo();
Sunny Goyalf4066152015-04-15 09:42:19 -0700759 }
Sunny Goyal31abc292015-05-01 10:42:32 -0700760 }
761
762 private void clearDragInfo() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700763 mCurrentDragInfo = null;
764 mCurrentDragView = null;
765 mSuppressOnAdd = false;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800766 mIsExternalDrag = false;
Adam Cohen3e8f8112011-07-02 18:03:00 -0700767 }
768
Adam Cohencb3382b2011-05-24 14:07:08 -0700769 public void onDragExit(DragObject d) {
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700770 // We only close the folder if this is a true drag exit, ie. not because
771 // a drop has occurred above the folder.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700772 if (!d.dragComplete) {
773 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
774 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
775 }
776 mReorderAlarm.cancelAlarm();
Sunny Goyal48461932015-03-09 17:41:09 -0700777
Sunny Goyalb8634152015-04-09 14:17:14 -0700778 mOnScrollHintAlarm.cancelAlarm();
779 mScrollPauseAlarm.cancelAlarm();
780 if (mScrollHintDir != DragController.SCROLL_NONE) {
781 mContent.clearScrollHint();
782 mScrollHintDir = DragController.SCROLL_NONE;
Sunny Goyal48461932015-03-09 17:41:09 -0700783 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700784 }
785
Sunny Goyale9b651e2015-04-24 11:44:51 -0700786 /**
787 * When performing an accessibility drop, onDrop is sent immediately after onDragEnter. So we
788 * need to complete all transient states based on timers.
789 */
790 @Override
791 public void prepareAccessibilityDrop() {
792 if (mReorderAlarm.alarmPending()) {
793 mReorderAlarm.cancelAlarm();
794 mReorderAlarmListener.onAlarm(mReorderAlarm);
795 }
796 }
797
Michael Jurka1e2f4652013-07-08 18:03:46 -0700798 public void onDropCompleted(final View target, final DragObject d,
799 final boolean isFlingToDelete, final boolean success) {
800 if (mDeferDropAfterUninstall) {
Michael Jurkaf3007582013-08-21 14:33:57 +0200801 Log.d(TAG, "Deferred handling drop because waiting for uninstall.");
Michael Jurka1e2f4652013-07-08 18:03:46 -0700802 mDeferredAction = new Runnable() {
803 public void run() {
804 onDropCompleted(target, d, isFlingToDelete, success);
805 mDeferredAction = null;
806 }
807 };
808 return;
809 }
810
811 boolean beingCalledAfterUninstall = mDeferredAction != null;
812 boolean successfulDrop =
813 success && (!beingCalledAfterUninstall || mUninstallSuccessful);
Winson Chung5f8afe62013-08-12 16:19:28 -0700814
Michael Jurka1e2f4652013-07-08 18:03:46 -0700815 if (successfulDrop) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800816 if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon && target != this) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700817 replaceFolderWithFinalItem();
818 }
819 } else {
820 // The drag failed, we need to return the item to the folder
Sunny Goyalb8c663c2015-04-23 11:43:48 -0700821 ShortcutInfo info = (ShortcutInfo) d.dragInfo;
822 View icon = (mCurrentDragView != null && mCurrentDragView.getTag() == info)
823 ? mCurrentDragView : mContent.createNewView(info);
824 ArrayList<View> views = getItemsInReadingOrder();
825 views.add(info.rank, icon);
826 mContent.arrangeChildren(views, views.size());
827 mItemsInvalidated = true;
828
829 mSuppressOnAdd = true;
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700830 mFolderIcon.onDrop(d);
Sunny Goyalb8c663c2015-04-23 11:43:48 -0700831 mSuppressOnAdd = false;
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700832 }
833
834 if (target != this) {
835 if (mOnExitAlarm.alarmPending()) {
836 mOnExitAlarm.cancelAlarm();
Michael Jurka54554252013-08-01 12:52:23 +0200837 if (!successfulDrop) {
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700838 mSuppressFolderDeletion = true;
839 }
Sunny Goyal5d85c442015-03-10 13:14:47 -0700840 mScrollPauseAlarm.cancelAlarm();
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700841 completeDragExit();
842 }
Adam Cohen9c58d822013-12-13 17:18:10 -0800843 }
844
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700845 mDeleteFolderOnDropCompleted = false;
846 mDragInProgress = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700847 mItemAddedBackToSelfViaIcon = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700848 mCurrentDragInfo = null;
849 mCurrentDragView = null;
850 mSuppressOnAdd = false;
Adam Cohen4045eb72011-10-06 11:44:26 -0700851
852 // Reordering may have occured, and we need to save the new item locations. We do this once
853 // at the end to prevent unnecessary database operations.
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700854 updateItemLocationsInDatabaseBatch();
Sunny Goyalb7e15ad2015-05-07 14:51:31 -0700855
856 // Use the item count to check for multi-page as the folder UI may not have
857 // been refreshed yet.
858 if (getItemCount() <= mContent.itemsPerPage()) {
859 // Show the animation, next time something is added to the folder.
860 mInfo.setOption(FolderInfo.FLAG_MULTI_PAGE_ANIMATION, false, mLauncher);
861 }
862
Adam Cohen4045eb72011-10-06 11:44:26 -0700863 }
864
Sunny Goyalfa401a12015-04-10 13:45:42 -0700865 @Override
Michael Jurka1e2f4652013-07-08 18:03:46 -0700866 public void deferCompleteDropAfterUninstallActivity() {
867 mDeferDropAfterUninstall = true;
868 }
869
Sunny Goyalfa401a12015-04-10 13:45:42 -0700870 @Override
Michael Jurka1e2f4652013-07-08 18:03:46 -0700871 public void onUninstallActivityReturned(boolean success) {
872 mDeferDropAfterUninstall = false;
873 mUninstallSuccessful = success;
874 if (mDeferredAction != null) {
875 mDeferredAction.run();
876 }
877 }
878
Winson Chunga48487a2012-03-20 16:19:37 -0700879 @Override
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800880 public float getIntrinsicIconScaleFactor() {
881 return 1f;
882 }
883
884 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800885 public boolean supportsFlingToDelete() {
886 return true;
887 }
888
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000889 @Override
890 public boolean supportsAppInfoDropTarget() {
891 return false;
892 }
893
894 @Override
895 public boolean supportsDeleteDropTarget() {
896 return true;
897 }
898
Sunny Goyalddec7342015-04-29 18:12:37 -0700899 @Override
900 public void onFlingToDelete(DragObject d, PointF vec) {
Winson Chunga48487a2012-03-20 16:19:37 -0700901 // Do nothing
902 }
903
904 @Override
905 public void onFlingToDeleteCompleted() {
906 // Do nothing
907 }
908
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700909 private void updateItemLocationsInDatabaseBatch() {
910 ArrayList<View> list = getItemsInReadingOrder();
911 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
912 for (int i = 0; i < list.size(); i++) {
913 View v = list.get(i);
914 ItemInfo info = (ItemInfo) v.getTag();
Sunny Goyal08f72612015-01-05 13:41:43 -0800915 info.rank = i;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700916 items.add(info);
917 }
918
919 LauncherModel.moveItemsInDatabase(mLauncher, items, mInfo.id, 0);
920 }
921
Adam Cohene25af792013-06-06 23:08:25 -0700922 public void addItemLocationsInDatabase() {
923 ArrayList<View> list = getItemsInReadingOrder();
924 for (int i = 0; i < list.size(); i++) {
925 View v = list.get(i);
926 ItemInfo info = (ItemInfo) v.getTag();
927 LauncherModel.addItemToDatabase(mLauncher, info, mInfo.id, 0,
Winson Chung8f1eff72015-05-28 17:33:40 -0700928 info.cellX, info.cellY);
Adam Cohene25af792013-06-06 23:08:25 -0700929 }
930 }
931
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700932 public void notifyDrop() {
933 if (mDragInProgress) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700934 mItemAddedBackToSelfViaIcon = true;
Adam Cohen76078c42011-06-09 15:06:52 -0700935 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700936 }
937
938 public boolean isDropEnabled() {
939 return true;
940 }
941
Adam Cohen2801caf2011-05-13 20:57:39 -0700942 public boolean isFull() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800943 return mContent.isFull();
Adam Cohen2801caf2011-05-13 20:57:39 -0700944 }
945
946 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700947 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700948
Winson Chung892c74d2013-08-22 16:15:50 -0700949 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700950 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700951 int height = getFolderHeight();
Adam Cohen2801caf2011-05-13 20:57:39 -0700952
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800953 float scale = parent.getDescendantRectRelativeToSelf(mFolderIcon, sTempRect);
Adam Cohen8e776a62011-06-28 18:10:06 -0700954
Adam Cohen2e6da152015-05-06 11:42:25 -0700955 DeviceProfile grid = mLauncher.getDeviceProfile();
Winson Chungaf40f202013-09-18 18:26:31 -0700956
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800957 int centerX = (int) (sTempRect.left + sTempRect.width() * scale / 2);
958 int centerY = (int) (sTempRect.top + sTempRect.height() * scale / 2);
Adam Cohen2801caf2011-05-13 20:57:39 -0700959 int centeredLeft = centerX - width / 2;
960 int centeredTop = centerY - height / 2;
Adam Cohen2801caf2011-05-13 20:57:39 -0700961
Sunny Goyal41d84172015-07-08 23:28:51 -0700962 // We need to bound the folder to the currently visible workspace area
963 mLauncher.getWorkspace().getPageAreaRelativeToDragLayer(sTempRect);
964 int left = Math.min(Math.max(sTempRect.left, centeredLeft),
965 sTempRect.left + sTempRect.width() - width);
966 int top = Math.min(Math.max(sTempRect.top, centeredTop),
967 sTempRect.top + sTempRect.height() - height);
Sunny Goyalc6205602015-05-21 20:46:33 -0700968 if (grid.isPhone && (grid.availableWidthPx - width) < grid.iconSizePx) {
Winson Chungaf40f202013-09-18 18:26:31 -0700969 // Center the folder if it is full (on phones only)
970 left = (grid.availableWidthPx - width) / 2;
Sunny Goyal41d84172015-07-08 23:28:51 -0700971 } else if (width >= sTempRect.width()) {
Winson Chungaf40f202013-09-18 18:26:31 -0700972 // If the folder doesn't fit within the bounds, center it about the desired bounds
Sunny Goyal41d84172015-07-08 23:28:51 -0700973 left = sTempRect.left + (sTempRect.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700974 }
Sunny Goyal41d84172015-07-08 23:28:51 -0700975 if (height >= sTempRect.height()) {
976 top = sTempRect.top + (sTempRect.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700977 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700978
979 int folderPivotX = width / 2 + (centeredLeft - left);
980 int folderPivotY = height / 2 + (centeredTop - top);
981 setPivotX(folderPivotX);
982 setPivotY(folderPivotY);
Adam Cohen268c4752012-06-06 17:47:33 -0700983 mFolderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700984 (1.0f * folderPivotX / width));
Adam Cohen268c4752012-06-06 17:47:33 -0700985 mFolderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700986 (1.0f * folderPivotY / height));
Adam Cohen3bf84d32012-05-07 20:17:14 -0700987
Adam Cohen662b5982011-12-13 17:45:21 -0800988 lp.width = width;
989 lp.height = height;
990 lp.x = left;
991 lp.y = top;
Adam Cohen2801caf2011-05-13 20:57:39 -0700992 }
993
Adam Cohen268c4752012-06-06 17:47:33 -0700994 float getPivotXForIconAnimation() {
995 return mFolderIconPivotX;
996 }
997 float getPivotYForIconAnimation() {
998 return mFolderIconPivotY;
999 }
1000
Winson Chung892c74d2013-08-22 16:15:50 -07001001 private int getContentAreaHeight() {
Adam Cohen2e6da152015-05-06 11:42:25 -07001002 DeviceProfile grid = mLauncher.getDeviceProfile();
Sunny Goyalc6205602015-05-21 20:46:33 -07001003 Rect workspacePadding = grid.getWorkspacePadding(mContent.mIsRtl);
Winson Chung892c74d2013-08-22 16:15:50 -07001004 int maxContentAreaHeight = grid.availableHeightPx -
Winson Chung892c74d2013-08-22 16:15:50 -07001005 workspacePadding.top - workspacePadding.bottom -
Sunny Goyal290800b2015-03-05 11:33:33 -08001006 mFooterHeight;
Adam Cohen1960ea42013-11-12 11:33:14 +00001007 int height = Math.min(maxContentAreaHeight,
Winson Chung892c74d2013-08-22 16:15:50 -07001008 mContent.getDesiredHeight());
Adam Cohen1960ea42013-11-12 11:33:14 +00001009 return Math.max(height, MIN_CONTENT_DIMEN);
1010 }
1011
1012 private int getContentAreaWidth() {
1013 return Math.max(mContent.getDesiredWidth(), MIN_CONTENT_DIMEN);
Winson Chung892c74d2013-08-22 16:15:50 -07001014 }
1015
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001016 private int getFolderHeight() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001017 return getFolderHeight(getContentAreaHeight());
1018 }
1019
1020 private int getFolderHeight(int contentAreaHeight) {
Sunny Goyal290800b2015-03-05 11:33:33 -08001021 return getPaddingTop() + getPaddingBottom() + contentAreaHeight + mFooterHeight;
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001022 }
Adam Cohen234c4cd2011-07-17 21:03:04 -07001023
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001024 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001025 int contentWidth = getContentAreaWidth();
1026 int contentHeight = getContentAreaHeight();
Adam Cohen1960ea42013-11-12 11:33:14 +00001027
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001028 int contentAreaWidthSpec = MeasureSpec.makeMeasureSpec(contentWidth, MeasureSpec.EXACTLY);
1029 int contentAreaHeightSpec = MeasureSpec.makeMeasureSpec(contentHeight, MeasureSpec.EXACTLY);
1030
1031 mContent.setFixedSize(contentWidth, contentHeight);
1032 mContentWrapper.measure(contentAreaWidthSpec, contentAreaHeightSpec);
Sunny Goyala07c2f52015-06-01 11:00:38 -07001033
1034 if (mContent.getChildCount() > 0) {
1035 int cellIconGap = (mContent.getPageAt(0).getCellWidth()
1036 - mLauncher.getDeviceProfile().iconSizePx) / 2;
1037 mFooter.setPadding(mContent.getPaddingLeft() + cellIconGap,
1038 mFooter.getPaddingTop(),
1039 mContent.getPaddingRight() + cellIconGap,
1040 mFooter.getPaddingBottom());
1041 }
Sunny Goyal290800b2015-03-05 11:33:33 -08001042 mFooter.measure(contentAreaWidthSpec,
1043 MeasureSpec.makeMeasureSpec(mFooterHeight, MeasureSpec.EXACTLY));
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001044
1045 int folderWidth = getPaddingLeft() + getPaddingRight() + contentWidth;
1046 int folderHeight = getFolderHeight(contentHeight);
1047 setMeasuredDimension(folderWidth, folderHeight);
Adam Cohen234c4cd2011-07-17 21:03:04 -07001048 }
1049
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001050 /**
1051 * Rearranges the children based on their rank.
1052 */
1053 public void rearrangeChildren() {
1054 rearrangeChildren(-1);
1055 }
Adam Cohen2801caf2011-05-13 20:57:39 -07001056
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001057 /**
1058 * Rearranges the children based on their rank.
1059 * @param itemCount if greater than the total children count, empty spaces are left at the end,
1060 * otherwise it is ignored.
1061 */
1062 public void rearrangeChildren(int itemCount) {
1063 ArrayList<View> views = getItemsInReadingOrder();
1064 mContent.arrangeChildren(views, Math.max(itemCount, views.size()));
Adam Cohen7c693212011-05-18 15:26:57 -07001065 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -07001066 }
1067
Sunny Goyalc4918352015-03-10 18:15:48 -07001068 // TODO remove this once GSA code fix is submitted
1069 public ViewGroup getContent() {
1070 return (ViewGroup) mContent;
1071 }
1072
Adam Cohena9cf38f2011-05-02 15:36:58 -07001073 public int getItemCount() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001074 return mContent.getItemCount();
Adam Cohena9cf38f2011-05-02 15:36:58 -07001075 }
1076
Adam Cohen091440a2015-03-18 14:16:05 -07001077 @Thunk void onCloseComplete() {
Adam Cohen05e0f402011-08-01 12:12:49 -07001078 DragLayer parent = (DragLayer) getParent();
Michael Jurka5649c282012-06-18 10:33:21 -07001079 if (parent != null) {
1080 parent.removeView(this);
1081 }
Adam Cohen4554ee12011-08-03 16:13:21 -07001082 mDragController.removeDropTarget((DropTarget) this);
Adam Cohen05e0f402011-08-01 12:12:49 -07001083 clearFocus();
Adam Cohenac56cff2011-09-28 20:45:37 -07001084 mFolderIcon.requestFocus();
Adam Cohen05e0f402011-08-01 12:12:49 -07001085
Adam Cohen2801caf2011-05-13 20:57:39 -07001086 if (mRearrangeOnClose) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001087 rearrangeChildren();
Adam Cohen2801caf2011-05-13 20:57:39 -07001088 mRearrangeOnClose = false;
1089 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001090 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001091 if (!mDragInProgress && !mSuppressFolderDeletion) {
1092 replaceFolderWithFinalItem();
1093 } else if (mDragInProgress) {
1094 mDeleteFolderOnDropCompleted = true;
1095 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001096 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001097 mSuppressFolderDeletion = false;
Sunny Goyal31abc292015-05-01 10:42:32 -07001098 clearDragInfo();
Adam Cohenafb01ee2011-06-23 15:38:03 -07001099 }
1100
Adam Cohen091440a2015-03-18 14:16:05 -07001101 @Thunk void replaceFolderWithFinalItem() {
Adam Cohenafb01ee2011-06-23 15:38:03 -07001102 // Add the last remaining child to the workspace in place of the folder
Adam Cohenfb91f302012-06-11 15:45:18 -07001103 Runnable onCompleteRunnable = new Runnable() {
1104 @Override
1105 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -07001106 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screenId);
Adam Cohenafb01ee2011-06-23 15:38:03 -07001107
Winson Chung33231f52013-12-09 16:57:45 -08001108 View child = null;
Adam Cohenfb91f302012-06-11 15:45:18 -07001109 // Move the item from the folder to the workspace, in the position of the folder
1110 if (getItemCount() == 1) {
1111 ShortcutInfo finalItem = mInfo.contents.get(0);
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001112 child = mLauncher.createShortcut(cellLayout, finalItem);
Adam Cohenfb91f302012-06-11 15:45:18 -07001113 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -07001114 mInfo.screenId, mInfo.cellX, mInfo.cellY);
Adam Cohenfb91f302012-06-11 15:45:18 -07001115 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001116 if (getItemCount() <= 1) {
1117 // Remove the folder
1118 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
Dan Sandler0eb687f2014-01-10 13:24:55 -05001119 if (cellLayout != null) {
1120 // b/12446428 -- sometimes the cell layout has already gone away?
1121 cellLayout.removeView(mFolderIcon);
1122 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001123 if (mFolderIcon instanceof DropTarget) {
1124 mDragController.removeDropTarget((DropTarget) mFolderIcon);
1125 }
1126 mLauncher.removeFolder(mInfo);
1127 }
Adam Cohenc5e63f32012-07-12 16:16:57 -07001128 // We add the child after removing the folder to prevent both from existing at
Winson Chung0e6a7132013-08-23 12:55:10 -07001129 // the same time in the CellLayout. We need to add the new item with addInScreenFromBind()
1130 // to ensure that hotseat items are placed correctly.
Adam Cohenc5e63f32012-07-12 16:16:57 -07001131 if (child != null) {
Winson Chung0e6a7132013-08-23 12:55:10 -07001132 mLauncher.getWorkspace().addInScreenFromBind(child, mInfo.container, mInfo.screenId,
Adam Cohenc5e63f32012-07-12 16:16:57 -07001133 mInfo.cellX, mInfo.cellY, mInfo.spanX, mInfo.spanY);
1134 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001135 }
1136 };
Sunny Goyalbc753352015-03-05 09:40:44 -08001137 View finalChild = mContent.getLastItem();
Adam Cohenfb91f302012-06-11 15:45:18 -07001138 if (finalChild != null) {
1139 mFolderIcon.performDestroyAnimation(finalChild, onCompleteRunnable);
Winson Chung33231f52013-12-09 16:57:45 -08001140 } else {
1141 onCompleteRunnable.run();
Adam Cohenafb01ee2011-06-23 15:38:03 -07001142 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001143 mDestroyed = true;
1144 }
1145
1146 boolean isDestroyed() {
1147 return mDestroyed;
Adam Cohen2801caf2011-05-13 20:57:39 -07001148 }
1149
Adam Cohenac56cff2011-09-28 20:45:37 -07001150 // This method keeps track of the last item in the folder for the purposes
1151 // of keyboard focus
Sunny Goyal290800b2015-03-05 11:33:33 -08001152 public void updateTextViewFocus() {
Sunny Goyalbc753352015-03-05 09:40:44 -08001153 View lastChild = mContent.getLastItem();
Adam Cohenac56cff2011-09-28 20:45:37 -07001154 if (lastChild != null) {
1155 mFolderName.setNextFocusDownId(lastChild.getId());
1156 mFolderName.setNextFocusRightId(lastChild.getId());
1157 mFolderName.setNextFocusLeftId(lastChild.getId());
1158 mFolderName.setNextFocusUpId(lastChild.getId());
1159 }
1160 }
1161
Adam Cohenbfbfd262011-06-13 16:55:12 -07001162 public void onDrop(DragObject d) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001163 Runnable cleanUpRunnable = null;
1164
Adam Cohen689ff162014-05-08 17:27:56 -07001165 // If we are coming from All Apps space, we defer removing the extra empty screen
1166 // until the folder closes
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001167 if (d.dragSource != mLauncher.getWorkspace() && !(d.dragSource instanceof Folder)) {
1168 cleanUpRunnable = new Runnable() {
1169 @Override
1170 public void run() {
Adam Cohen689ff162014-05-08 17:27:56 -07001171 mLauncher.exitSpringLoadedDragModeDelayed(true,
1172 Launcher.EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
1173 null);
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001174 }
1175 };
Adam Cohenbfbfd262011-06-13 16:55:12 -07001176 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001177
Sunny Goyalb8634152015-04-09 14:17:14 -07001178 // If the icon was dropped while the page was being scrolled, we need to compute
1179 // the target location again such that the icon is placed of the final page.
1180 if (!mContent.rankOnCurrentPage(mEmptyCellRank)) {
1181 // Reorder again.
1182 mTargetRank = getTargetRank(d, null);
Sunny Goyal48461932015-03-09 17:41:09 -07001183
Sunny Goyalb8634152015-04-09 14:17:14 -07001184 // Rearrange items immediately.
1185 mReorderAlarmListener.onAlarm(mReorderAlarm);
Sunny Goyal48461932015-03-09 17:41:09 -07001186
Sunny Goyalb8634152015-04-09 14:17:14 -07001187 mOnScrollHintAlarm.cancelAlarm();
1188 mScrollPauseAlarm.cancelAlarm();
Sunny Goyal48461932015-03-09 17:41:09 -07001189 }
Sunny Goyalb8634152015-04-09 14:17:14 -07001190 mContent.completePendingPageChanges();
Sunny Goyal48461932015-03-09 17:41:09 -07001191
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001192 View currentDragView;
1193 ShortcutInfo si = mCurrentDragInfo;
1194 if (mIsExternalDrag) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001195 currentDragView = mContent.createAndAddViewForRank(si, mEmptyCellRank);
Sunny Goyal95abbb32014-08-04 10:53:22 -07001196 // Actually move the item in the database if it was an external drag. Call this
1197 // before creating the view, so that ShortcutInfo is updated appropriately.
1198 LauncherModel.addOrMoveItemInDatabase(
1199 mLauncher, si, mInfo.id, 0, si.cellX, si.cellY);
1200
1201 // We only need to update the locations if it doesn't get handled in #onDropCompleted.
1202 if (d.dragSource != this) {
1203 updateItemLocationsInDatabaseBatch();
1204 }
1205 mIsExternalDrag = false;
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001206 } else {
1207 currentDragView = mCurrentDragView;
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001208 mContent.addViewForRank(currentDragView, si, mEmptyCellRank);
Adam Cohenbfbfd262011-06-13 16:55:12 -07001209 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001210
1211 if (d.dragView.hasDrawn()) {
1212
1213 // Temporarily reset the scale such that the animation target gets calculated correctly.
1214 float scaleX = getScaleX();
1215 float scaleY = getScaleY();
1216 setScaleX(1.0f);
1217 setScaleY(1.0f);
1218 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, currentDragView,
1219 cleanUpRunnable, null);
1220 setScaleX(scaleX);
1221 setScaleY(scaleY);
1222 } else {
1223 d.deferDragViewCleanupPostAnimation = false;
1224 currentDragView.setVisibility(VISIBLE);
1225 }
1226 mItemsInvalidated = true;
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001227 rearrangeChildren();
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001228
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001229 // Temporarily suppress the listener, as we did all the work already here.
1230 mSuppressOnAdd = true;
1231 mInfo.add(si);
1232 mSuppressOnAdd = false;
Sunny Goyal4b020172014-08-28 14:51:14 -07001233 // Clear the drag info, as it is no longer being dragged.
1234 mCurrentDragInfo = null;
Sunny Goyalf4066152015-04-15 09:42:19 -07001235 mDragInProgress = false;
Sunny Goyalb7e15ad2015-05-07 14:51:31 -07001236
1237 if (mContent.getPageCount() > 1) {
1238 // The animation has already been shown while opening the folder.
1239 mInfo.setOption(FolderInfo.FLAG_MULTI_PAGE_ANIMATION, true, mLauncher);
1240 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001241 }
1242
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001243 // This is used so the item doesn't immediately appear in the folder when added. In one case
1244 // we need to create the illusion that the item isn't added back to the folder yet, to
1245 // to correspond to the animation of the icon back into the folder. This is
1246 public void hideItem(ShortcutInfo info) {
1247 View v = getViewForInfo(info);
1248 v.setVisibility(INVISIBLE);
1249 }
1250 public void showItem(ShortcutInfo info) {
1251 View v = getViewForInfo(info);
1252 v.setVisibility(VISIBLE);
1253 }
1254
Sunny Goyalb7e15ad2015-05-07 14:51:31 -07001255 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -07001256 public void onAdd(ShortcutInfo item) {
Adam Cohen05e0f402011-08-01 12:12:49 -07001257 // If the item was dropped onto this open folder, we have done the work associated
1258 // with adding the item to the folder, as indicated by mSuppressOnAdd being set
Adam Cohenbfbfd262011-06-13 16:55:12 -07001259 if (mSuppressOnAdd) return;
Sunny Goyal5d85c442015-03-10 13:14:47 -07001260 mContent.createAndAddViewForRank(item, mContent.allocateRankForNewItem(item));
Sunny Goyal2e688a82015-03-18 10:23:39 -07001261 mItemsInvalidated = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001262 LauncherModel.addOrMoveItemInDatabase(
1263 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
1264 }
1265
Adam Cohena9cf38f2011-05-02 15:36:58 -07001266 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -07001267 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001268 // If this item is being dragged from this open folder, we have already handled
1269 // the work associated with removing the item, so we don't have to do anything here.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001270 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001271 View v = getViewForInfo(item);
Sunny Goyal290800b2015-03-05 11:33:33 -08001272 mContent.removeItem(v);
Adam Cohen2801caf2011-05-13 20:57:39 -07001273 if (mState == STATE_ANIMATING) {
1274 mRearrangeOnClose = true;
1275 } else {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001276 rearrangeChildren();
Adam Cohen2801caf2011-05-13 20:57:39 -07001277 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001278 if (getItemCount() <= 1) {
1279 replaceFolderWithFinalItem();
1280 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001281 }
Adam Cohen7c693212011-05-18 15:26:57 -07001282
Sunny Goyalbc753352015-03-05 09:40:44 -08001283 private View getViewForInfo(final ShortcutInfo item) {
1284 return mContent.iterateOverItems(new ItemOperator() {
1285
1286 @Override
1287 public boolean evaluate(ItemInfo info, View view, View parent) {
1288 return info == item;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001289 }
Sunny Goyalbc753352015-03-05 09:40:44 -08001290 });
Adam Cohendf1e4e82011-06-24 15:57:39 -07001291 }
1292
Adam Cohen76078c42011-06-09 15:06:52 -07001293 public void onItemsChanged() {
Adam Cohenac56cff2011-09-28 20:45:37 -07001294 updateTextViewFocus();
Adam Cohen76078c42011-06-09 15:06:52 -07001295 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001296
Adam Cohen76fc0852011-06-17 13:26:23 -07001297 public void onTitleChanged(CharSequence title) {
1298 }
Adam Cohen76078c42011-06-09 15:06:52 -07001299
Adam Cohen7c693212011-05-18 15:26:57 -07001300 public ArrayList<View> getItemsInReadingOrder() {
1301 if (mItemsInvalidated) {
1302 mItemsInReadingOrder.clear();
Sunny Goyalbc753352015-03-05 09:40:44 -08001303 mContent.iterateOverItems(new ItemOperator() {
1304
1305 @Override
1306 public boolean evaluate(ItemInfo info, View view, View parent) {
1307 mItemsInReadingOrder.add(view);
1308 return false;
Adam Cohen7c693212011-05-18 15:26:57 -07001309 }
Sunny Goyalbc753352015-03-05 09:40:44 -08001310 });
Adam Cohen7c693212011-05-18 15:26:57 -07001311 mItemsInvalidated = false;
1312 }
1313 return mItemsInReadingOrder;
1314 }
Adam Cohen8dfcba42011-07-07 16:38:18 -07001315
1316 public void getLocationInDragLayer(int[] loc) {
1317 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
1318 }
Adam Cohenea0818d2011-09-30 20:06:58 -07001319
1320 public void onFocusChange(View v, boolean hasFocus) {
1321 if (v == mFolderName && hasFocus) {
1322 startEditingFolderName();
1323 }
1324 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001325
1326 @Override
1327 public void getHitRectRelativeToDragLayer(Rect outRect) {
1328 getHitRect(outRect);
Sunny Goyal48461932015-03-09 17:41:09 -07001329 outRect.left -= mScrollAreaOffset;
1330 outRect.right += mScrollAreaOffset;
1331 }
1332
Winson Chung8f1eff72015-05-28 17:33:40 -07001333 @Override
1334 public void fillInLaunchSourceData(Bundle sourceData) {
1335 // Fill in from the folder icon's launch source provider first
1336 Stats.LaunchSourceUtils.populateSourceDataFromAncestorProvider(mFolderIcon, sourceData);
1337 sourceData.putString(Stats.SOURCE_EXTRA_SUB_CONTAINER, Stats.SUB_CONTAINER_FOLDER);
1338 sourceData.putInt(Stats.SOURCE_EXTRA_SUB_CONTAINER_PAGE, mContent.getCurrentPage());
1339 }
1340
Sunny Goyal48461932015-03-09 17:41:09 -07001341 private class OnScrollHintListener implements OnAlarmListener {
1342
1343 private final DragObject mDragObject;
1344
1345 OnScrollHintListener(DragObject object) {
1346 mDragObject = object;
1347 }
1348
1349 /**
1350 * Scroll hint has been shown long enough. Now scroll to appropriate page.
1351 */
1352 @Override
1353 public void onAlarm(Alarm alarm) {
1354 if (mCurrentScrollDir == DragController.SCROLL_LEFT) {
Sunny Goyalb8634152015-04-09 14:17:14 -07001355 mContent.scrollLeft();
Sunny Goyal48461932015-03-09 17:41:09 -07001356 mScrollHintDir = DragController.SCROLL_NONE;
1357 } else if (mCurrentScrollDir == DragController.SCROLL_RIGHT) {
Sunny Goyalb8634152015-04-09 14:17:14 -07001358 mContent.scrollRight();
Sunny Goyal48461932015-03-09 17:41:09 -07001359 mScrollHintDir = DragController.SCROLL_NONE;
1360 } else {
1361 // This should not happen
1362 return;
1363 }
1364 mCurrentScrollDir = DragController.SCROLL_NONE;
1365
1366 // Pause drag event until the scrolling is finished
1367 mScrollPauseAlarm.setOnAlarmListener(new OnScrollFinishedListener(mDragObject));
1368 mScrollPauseAlarm.setAlarm(DragController.RESCROLL_DELAY);
1369 }
1370 }
1371
1372 private class OnScrollFinishedListener implements OnAlarmListener {
1373
1374 private final DragObject mDragObject;
1375
1376 OnScrollFinishedListener(DragObject object) {
1377 mDragObject = object;
1378 }
1379
1380 /**
1381 * Page scroll is complete.
1382 */
1383 @Override
1384 public void onAlarm(Alarm alarm) {
1385 // Reorder immediately on page change.
1386 onDragOver(mDragObject, 1);
1387 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001388 }
Sunny Goyal1dd0f8b2015-07-06 13:04:02 -07001389
1390 // Compares item position based on rank and position giving priority to the rank.
Sunny Goyal317698b2015-07-29 11:45:41 -07001391 public static final Comparator<ItemInfo> ITEM_POS_COMPARATOR = new Comparator<ItemInfo>() {
Sunny Goyal1dd0f8b2015-07-06 13:04:02 -07001392
1393 @Override
1394 public int compare(ItemInfo lhs, ItemInfo rhs) {
1395 if (lhs.rank != rhs.rank) {
1396 return lhs.rank - rhs.rank;
1397 } else if (lhs.cellY != rhs.cellY) {
1398 return lhs.cellY - rhs.cellY;
1399 } else {
1400 return lhs.cellX - rhs.cellX;
1401 }
1402 }
1403 };
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001404}