blob: 66b656882f8984671f28c8d1cfd8428d58b56f50 [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;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080024import android.content.Context;
Adam Cohen76fc0852011-06-17 13:26:23 -070025import android.content.res.Resources;
Winson Chung043f2af2012-03-01 16:09:54 -080026import android.graphics.PointF;
Romain Guyfb5411e2010-02-24 10:04:17 -080027import android.graphics.Rect;
Alan Viverette4cda5b72013-08-28 17:53:41 -070028import android.os.SystemClock;
Bjorn Bringertb66773c2013-09-25 16:12:24 +010029import android.support.v4.widget.AutoScrollHelper;
Adam Cohen7a14d0b2011-06-24 11:36:35 -070030import android.text.InputType;
Adam Cohene601a432011-07-26 21:51:30 -070031import android.text.Selection;
32import android.text.Spannable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.util.AttributeSet;
Adam Cohen3bf84d32012-05-07 20:17:14 -070034import android.util.Log;
Adam Cohen76fc0852011-06-17 13:26:23 -070035import android.view.ActionMode;
36import android.view.KeyEvent;
Adam Cohendf2cc412011-04-27 16:56:57 -070037import android.view.LayoutInflater;
Adam Cohen76fc0852011-06-17 13:26:23 -070038import android.view.Menu;
39import android.view.MenuItem;
Adam Cohen0c872ba2011-05-05 10:34:16 -070040import android.view.MotionEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.view.View;
Adam Cohen3371da02011-10-25 21:38:29 -070042import android.view.accessibility.AccessibilityEvent;
43import android.view.accessibility.AccessibilityManager;
Adam Cohenc4fe9ea2014-08-18 18:54:10 -070044import android.view.animation.AccelerateInterpolator;
Adam Cohen76fc0852011-06-17 13:26:23 -070045import android.view.inputmethod.EditorInfo;
46import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.widget.LinearLayout;
Adam Cohenf0f4eda2013-06-06 21:27:03 -070048import android.widget.ScrollView;
Adam Cohendf2cc412011-04-27 16:56:57 -070049import android.widget.TextView;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050
Daniel Sandler325dc232013-06-05 22:57:57 -040051import com.android.launcher3.FolderInfo.FolderListener;
Romain Guyedcce092010-03-04 13:03:17 -080052
Adam Cohenc0dcf592011-06-01 15:30:43 -070053import java.util.ArrayList;
Adam Cohen3bf84d32012-05-07 20:17:14 -070054import java.util.Collections;
55import java.util.Comparator;
Adam Cohenc0dcf592011-06-01 15:30:43 -070056
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057/**
58 * Represents a set of icons chosen by the user or generated by the system.
59 */
Adam Cohen8dfcba42011-07-07 16:38:18 -070060public class Folder extends LinearLayout implements DragSource, View.OnClickListener,
Adam Cohenea0818d2011-09-30 20:06:58 -070061 View.OnLongClickListener, DropTarget, FolderListener, TextView.OnEditorActionListener,
62 View.OnFocusChangeListener {
Adam Cohendf2cc412011-04-27 16:56:57 -070063 private static final String TAG = "Launcher.Folder";
64
Adam Cohen4eac29a2011-07-11 17:53:37 -070065 protected DragController mDragController;
66 protected Launcher mLauncher;
67 protected FolderInfo mInfo;
68
Adam Cohendf2cc412011-04-27 16:56:57 -070069 static final int STATE_NONE = -1;
70 static final int STATE_SMALL = 0;
71 static final int STATE_ANIMATING = 1;
72 static final int STATE_OPEN = 2;
73
74 private int mExpandDuration;
Adam Cohenc4fe9ea2014-08-18 18:54:10 -070075 private int mMaterialExpandDuration;
76 private int mMaterialExpandStagger;
Adam Cohendf2cc412011-04-27 16:56:57 -070077 protected CellLayout mContent;
Adam Cohenf0f4eda2013-06-06 21:27:03 -070078 private ScrollView mScrollView;
Adam Cohendf2cc412011-04-27 16:56:57 -070079 private final LayoutInflater mInflater;
80 private final IconCache mIconCache;
81 private int mState = STATE_NONE;
Adam Cohenbfbfd262011-06-13 16:55:12 -070082 private static final int REORDER_ANIMATION_DURATION = 230;
Adam Cohenf0f4eda2013-06-06 21:27:03 -070083 private static final int REORDER_DELAY = 250;
Adam Cohen5d518fa2013-12-05 14:16:23 -080084 private static final int ON_EXIT_CLOSE_DELAY = 400;
Adam Cohen2801caf2011-05-13 20:57:39 -070085 private boolean mRearrangeOnClose = false;
86 private FolderIcon mFolderIcon;
87 private int mMaxCountX;
88 private int mMaxCountY;
Adam Cohen78dc83e2011-11-15 17:10:00 -080089 private int mMaxNumItems;
Adam Cohen7c693212011-05-18 15:26:57 -070090 private ArrayList<View> mItemsInReadingOrder = new ArrayList<View>();
91 boolean mItemsInvalidated = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -070092 private ShortcutInfo mCurrentDragInfo;
93 private View mCurrentDragView;
Jorim Jaggi55bd9722014-01-16 15:30:42 -080094 private boolean mIsExternalDrag;
Adam Cohenbfbfd262011-06-13 16:55:12 -070095 boolean mSuppressOnAdd = false;
96 private int[] mTargetCell = new int[2];
97 private int[] mPreviousTargetCell = new int[2];
98 private int[] mEmptyCell = new int[2];
99 private Alarm mReorderAlarm = new Alarm();
100 private Alarm mOnExitAlarm = new Alarm();
Adam Cohen76fc0852011-06-17 13:26:23 -0700101 private int mFolderNameHeight;
Adam Cohen8e776a62011-06-28 18:10:06 -0700102 private Rect mTempRect = new Rect();
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700103 private boolean mDragInProgress = false;
104 private boolean mDeleteFolderOnDropCompleted = false;
105 private boolean mSuppressFolderDeletion = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700106 private boolean mItemAddedBackToSelfViaIcon = false;
Adam Cohenac56cff2011-09-28 20:45:37 -0700107 FolderEditText mFolderName;
Adam Cohen268c4752012-06-06 17:47:33 -0700108 private float mFolderIconPivotX;
109 private float mFolderIconPivotY;
Adam Cohen228da5a2011-07-27 22:23:47 -0700110
Adam Cohen76fc0852011-06-17 13:26:23 -0700111 private boolean mIsEditingName = false;
112 private InputMethodManager mInputMethodManager;
Adam Cohendf2cc412011-04-27 16:56:57 -0700113
Adam Cohena65beee2011-06-27 21:32:23 -0700114 private static String sDefaultFolderName;
115 private static String sHintText;
116
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700117 private FocusIndicatorView mFocusIndicatorHandler;
118
Adam Cohen1960ea42013-11-12 11:33:14 +0000119 // We avoid measuring the scroll view with a 0 width or height, as this
120 // results in CellLayout being measured as UNSPECIFIED, which it does
121 // not support.
122 private static final int MIN_CONTENT_DIMEN = 5;
123
Adam Cohenfb91f302012-06-11 15:45:18 -0700124 private boolean mDestroyed;
125
Alan Viverette4cda5b72013-08-28 17:53:41 -0700126 private AutoScrollHelper mAutoScrollHelper;
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700127
Michael Jurka1e2f4652013-07-08 18:03:46 -0700128 private Runnable mDeferredAction;
129 private boolean mDeferDropAfterUninstall;
130 private boolean mUninstallSuccessful;
131
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132 /**
133 * Used to inflate the Workspace from XML.
134 *
135 * @param context The application's context.
136 * @param attrs The attribtues set containing the Workspace's customization values.
137 */
138 public Folder(Context context, AttributeSet attrs) {
139 super(context, attrs);
Winson Chung5f8afe62013-08-12 16:19:28 -0700140
141 LauncherAppState app = LauncherAppState.getInstance();
142 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143 setAlwaysDrawnWithCacheEnabled(false);
Adam Cohendf2cc412011-04-27 16:56:57 -0700144 mInflater = LayoutInflater.from(context);
Winson Chung5f8afe62013-08-12 16:19:28 -0700145 mIconCache = app.getIconCache();
Adam Cohen78dc83e2011-11-15 17:10:00 -0800146
147 Resources res = getResources();
Michael Jurka414300a2013-08-27 15:42:35 +0200148 mMaxCountX = (int) grid.numColumns;
Nilesh Agrawal5f7099a2014-01-02 15:54:57 -0800149 // Allow scrolling folders when DISABLE_ALL_APPS is true.
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800150 if (LauncherAppState.isDisableAllApps()) {
Nilesh Agrawal5f7099a2014-01-02 15:54:57 -0800151 mMaxCountY = mMaxNumItems = Integer.MAX_VALUE;
152 } else {
153 mMaxCountY = (int) grid.numRows;
154 mMaxNumItems = mMaxCountX * mMaxCountY;
155 }
Adam Cohen76fc0852011-06-17 13:26:23 -0700156
157 mInputMethodManager = (InputMethodManager)
Michael Jurka8b805b12012-04-18 14:23:14 -0700158 getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
Adam Cohen76fc0852011-06-17 13:26:23 -0700159
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700160 mExpandDuration = res.getInteger(R.integer.config_folderExpandDuration);
161 mMaterialExpandDuration = res.getInteger(R.integer.config_materialFolderExpandDuration);
162 mMaterialExpandStagger = res.getInteger(R.integer.config_materialFolderExpandStagger);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700163
164 if (sDefaultFolderName == null) {
165 sDefaultFolderName = res.getString(R.string.folder_name);
166 }
Adam Cohena65beee2011-06-27 21:32:23 -0700167 if (sHintText == null) {
168 sHintText = res.getString(R.string.folder_hint_text);
169 }
Adam Cohen4eac29a2011-07-11 17:53:37 -0700170 mLauncher = (Launcher) context;
Adam Cohenac56cff2011-09-28 20:45:37 -0700171 // We need this view to be focusable in touch mode so that when text editing of the folder
172 // name is complete, we have something to focus on, thus hiding the cursor and giving
173 // reliable behvior when clicking the text field (since it will always gain focus on click).
174 setFocusableInTouchMode(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175 }
176
177 @Override
178 protected void onFinishInflate() {
179 super.onFinishInflate();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700180 mScrollView = (ScrollView) findViewById(R.id.scroll_view);
Adam Cohendf2cc412011-04-27 16:56:57 -0700181 mContent = (CellLayout) findViewById(R.id.folder_content);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700182
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700183 mFocusIndicatorHandler = new FocusIndicatorView(getContext());
184 mContent.addView(mFocusIndicatorHandler, 0);
185 mFocusIndicatorHandler.getLayoutParams().height = FocusIndicatorView.DEFAULT_LAYOUT_SIZE;
186 mFocusIndicatorHandler.getLayoutParams().width = FocusIndicatorView.DEFAULT_LAYOUT_SIZE;
187
Winson Chung5f8afe62013-08-12 16:19:28 -0700188 LauncherAppState app = LauncherAppState.getInstance();
189 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
190
Winson Chung5f8afe62013-08-12 16:19:28 -0700191 mContent.setCellDimensions(grid.folderCellWidthPx, grid.folderCellHeightPx);
Adam Cohen2801caf2011-05-13 20:57:39 -0700192 mContent.setGridSize(0, 0);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700193 mContent.getShortcutsAndWidgets().setMotionEventSplittingEnabled(false);
Adam Cohen2374abf2013-04-16 14:56:57 -0700194 mContent.setInvertIfRtl(true);
Adam Cohenac56cff2011-09-28 20:45:37 -0700195 mFolderName = (FolderEditText) findViewById(R.id.folder_name);
196 mFolderName.setFolder(this);
Adam Cohenea0818d2011-09-30 20:06:58 -0700197 mFolderName.setOnFocusChangeListener(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700198
199 // We find out how tall the text view wants to be (it is set to wrap_content), so that
200 // we can allocate the appropriate amount of space for it.
201 int measureSpec = MeasureSpec.UNSPECIFIED;
202 mFolderName.measure(measureSpec, measureSpec);
203 mFolderNameHeight = mFolderName.getMeasuredHeight();
204
205 // We disable action mode for now since it messes up the view on phones
206 mFolderName.setCustomSelectionActionModeCallback(mActionModeCallback);
Adam Cohen76fc0852011-06-17 13:26:23 -0700207 mFolderName.setOnEditorActionListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700208 mFolderName.setSelectAllOnFocus(true);
Adam Cohen7a14d0b2011-06-24 11:36:35 -0700209 mFolderName.setInputType(mFolderName.getInputType() |
210 InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
Alan Viverette4cda5b72013-08-28 17:53:41 -0700211 mAutoScrollHelper = new FolderAutoScrollHelper(mScrollView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800212 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700213
Adam Cohen76fc0852011-06-17 13:26:23 -0700214 private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
215 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
216 return false;
217 }
218
219 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
220 return false;
221 }
222
223 public void onDestroyActionMode(ActionMode mode) {
224 }
225
226 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
227 return false;
228 }
229 };
230
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800231 public void onClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700232 Object tag = v.getTag();
233 if (tag instanceof ShortcutInfo) {
Adam Cohenb5fe60c2013-06-06 22:03:51 -0700234 mLauncher.onClick(v);
Adam Cohendf2cc412011-04-27 16:56:57 -0700235 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800236 }
237
238 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -0700239 // Return if global dragging is not enabled
240 if (!mLauncher.isDraggingEnabled()) return true;
241
Adam Cohendf2cc412011-04-27 16:56:57 -0700242 Object tag = v.getTag();
243 if (tag instanceof ShortcutInfo) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700244 ShortcutInfo item = (ShortcutInfo) tag;
245 if (!v.isInTouchMode()) {
246 return false;
247 }
248
Adam Cohenac8c8762011-07-13 11:15:27 -0700249 mLauncher.getWorkspace().beginDragShared(v, this);
Adam Cohen76078c42011-06-09 15:06:52 -0700250
251 mCurrentDragInfo = item;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700252 mEmptyCell[0] = item.cellX;
253 mEmptyCell[1] = item.cellY;
254 mCurrentDragView = v;
Adam Cohenfc53cd22011-07-20 15:45:11 -0700255
256 mContent.removeView(mCurrentDragView);
257 mInfo.remove(mCurrentDragInfo);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700258 mDragInProgress = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700259 mItemAddedBackToSelfViaIcon = false;
Adam Cohendf2cc412011-04-27 16:56:57 -0700260 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800261 return true;
262 }
263
Adam Cohen76fc0852011-06-17 13:26:23 -0700264 public boolean isEditingName() {
265 return mIsEditingName;
266 }
267
268 public void startEditingFolderName() {
Adam Cohena65beee2011-06-27 21:32:23 -0700269 mFolderName.setHint("");
Adam Cohen76fc0852011-06-17 13:26:23 -0700270 mIsEditingName = true;
271 }
272
273 public void dismissEditingName() {
274 mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
275 doneEditingFolderName(true);
276 }
277
278 public void doneEditingFolderName(boolean commit) {
Adam Cohena65beee2011-06-27 21:32:23 -0700279 mFolderName.setHint(sHintText);
Adam Cohen1df26a32011-08-12 16:15:23 -0700280 // Convert to a string here to ensure that no other state associated with the text field
281 // gets saved.
Adam Cohen3371da02011-10-25 21:38:29 -0700282 String newTitle = mFolderName.getText().toString();
283 mInfo.setTitle(newTitle);
Adam Cohen76fc0852011-06-17 13:26:23 -0700284 LauncherModel.updateItemInDatabase(mLauncher, mInfo);
Adam Cohenac56cff2011-09-28 20:45:37 -0700285
Adam Cohen3371da02011-10-25 21:38:29 -0700286 if (commit) {
287 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700288 String.format(getContext().getString(R.string.folder_renamed), newTitle));
Adam Cohen3371da02011-10-25 21:38:29 -0700289 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700290 // In order to clear the focus from the text field, we set the focus on ourself. This
291 // ensures that every time the field is clicked, focus is gained, giving reliable behavior.
292 requestFocus();
293
Adam Cohene601a432011-07-26 21:51:30 -0700294 Selection.setSelection((Spannable) mFolderName.getText(), 0, 0);
Adam Cohen76fc0852011-06-17 13:26:23 -0700295 mIsEditingName = false;
296 }
297
298 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
299 if (actionId == EditorInfo.IME_ACTION_DONE) {
300 dismissEditingName();
301 return true;
302 }
303 return false;
304 }
305
306 public View getEditTextRegion() {
307 return mFolderName;
308 }
309
Anjali Koppalf05545f2014-03-10 19:18:43 -0700310 public CellLayout getContent() {
311 return mContent;
312 }
313
Adam Cohen0c872ba2011-05-05 10:34:16 -0700314 /**
315 * We need to handle touch events to prevent them from falling through to the workspace below.
316 */
317 @Override
318 public boolean onTouchEvent(MotionEvent ev) {
319 return true;
320 }
321
Joe Onorato00acb122009-08-04 16:04:30 -0400322 public void setDragController(DragController dragController) {
323 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800324 }
325
Adam Cohen2801caf2011-05-13 20:57:39 -0700326 void setFolderIcon(FolderIcon icon) {
327 mFolderIcon = icon;
328 }
329
Adam Cohen3371da02011-10-25 21:38:29 -0700330 @Override
331 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
332 // When the folder gets focus, we don't want to announce the list of items.
333 return true;
334 }
335
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800336 /**
337 * @return the FolderInfo object associated with this folder
338 */
339 FolderInfo getInfo() {
340 return mInfo;
341 }
342
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800343 void bind(FolderInfo info) {
344 mInfo = info;
Adam Cohendf2cc412011-04-27 16:56:57 -0700345 ArrayList<ShortcutInfo> children = info.contents;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700346 ArrayList<ShortcutInfo> overflow = new ArrayList<ShortcutInfo>();
Sunny Goyal08f72612015-01-05 13:41:43 -0800347
348 final int totalChildren = children.size();
349 setupContentForNumItems(totalChildren);
350
351 // Arrange children in the grid based on the rank.
352 Collections.sort(children, Utilities.RANK_COMPARATOR);
353 final int countX = mContent.getCountX();
354
355 int visibleChildren = 0;
Adam Cohendf2cc412011-04-27 16:56:57 -0700356 for (int i = 0; i < children.size(); i++) {
Sunny Goyal08f72612015-01-05 13:41:43 -0800357 ShortcutInfo child = children.get(i);
358 child.cellX = i % countX;
359 child.cellY = i / countX;
360
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800361 if (createAndAddShortcut(child) == null) {
Adam Cohenc508b2d2011-06-28 14:41:44 -0700362 overflow.add(child);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700363 } else {
Sunny Goyal08f72612015-01-05 13:41:43 -0800364 visibleChildren++;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700365 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700366 }
Adam Cohenc508b2d2011-06-28 14:41:44 -0700367
Adam Cohen0057bbc2011-08-12 18:30:51 -0700368 // We rearrange the items in case there are any empty gaps
Sunny Goyal08f72612015-01-05 13:41:43 -0800369 setupContentForNumItems(visibleChildren);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700370
Jason Monk4ff73882014-04-24 16:48:07 -0400371 // If our folder has too many items we prune them from the list. This is an issue
Adam Cohenc508b2d2011-06-28 14:41:44 -0700372 // when upgrading from the old Folders implementation which could contain an unlimited
373 // number of items.
374 for (ShortcutInfo item: overflow) {
375 mInfo.remove(item);
376 LauncherModel.deleteItemFromDatabase(mLauncher, item);
377 }
378
Adam Cohen4dbe6d92011-05-18 17:14:15 -0700379 mItemsInvalidated = true;
Adam Cohenac56cff2011-09-28 20:45:37 -0700380 updateTextViewFocus();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700381 mInfo.addListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700382
Adam Cohenafb01ee2011-06-23 15:38:03 -0700383 if (!sDefaultFolderName.contentEquals(mInfo.title)) {
Adam Cohen4ef610f2011-06-21 22:37:36 -0700384 mFolderName.setText(mInfo.title);
385 } else {
386 mFolderName.setText("");
387 }
Winson Chung33231f52013-12-09 16:57:45 -0800388
389 // In case any children didn't come across during loading, clean up the folder accordingly
390 mFolderIcon.post(new Runnable() {
391 public void run() {
392 if (getItemCount() <= 1) {
393 replaceFolderWithFinalItem();
394 }
395 }
396 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700397 }
398
399 /**
400 * Creates a new UserFolder, inflated from R.layout.user_folder.
401 *
402 * @param context The application's context.
403 *
404 * @return A new UserFolder.
405 */
406 static Folder fromXml(Context context) {
407 return (Folder) LayoutInflater.from(context).inflate(R.layout.user_folder, null);
408 }
409
410 /**
411 * This method is intended to make the UserFolder to be visually identical in size and position
412 * to its associated FolderIcon. This allows for a seamless transition into the expanded state.
413 */
414 private void positionAndSizeAsIcon() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700415 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800416 setScaleX(0.8f);
417 setScaleY(0.8f);
418 setAlpha(0f);
Adam Cohendf2cc412011-04-27 16:56:57 -0700419 mState = STATE_SMALL;
420 }
421
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700422 private void prepareReveal() {
423 setScaleX(1f);
424 setScaleY(1f);
425 setAlpha(1f);
426 mState = STATE_SMALL;
427 }
428
Adam Cohendf2cc412011-04-27 16:56:57 -0700429 public void animateOpen() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700430 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen6441de02011-12-14 14:25:32 -0800431
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700432 Animator openFolderAnim = null;
433 final Runnable onCompleteRunnable;
Kenny Guyd794a3f2014-09-16 15:17:58 +0100434 if (!Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700435 positionAndSizeAsIcon();
436 centerAboutIcon();
437
438 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1);
439 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
440 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
441 final ObjectAnimator oa =
442 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
443 oa.setDuration(mExpandDuration);
444 openFolderAnim = oa;
445
446 setLayerType(LAYER_TYPE_HARDWARE, null);
447 onCompleteRunnable = new Runnable() {
448 @Override
449 public void run() {
450 setLayerType(LAYER_TYPE_NONE, null);
451 }
452 };
453 } else {
454 prepareReveal();
455 centerAboutIcon();
456
457 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
458 int height = getFolderHeight();
459
460 float transX = - 0.075f * (width / 2 - getPivotX());
461 float transY = - 0.075f * (height / 2 - getPivotY());
462 setTranslationX(transX);
463 setTranslationY(transY);
464 PropertyValuesHolder tx = PropertyValuesHolder.ofFloat("translationX", transX, 0);
465 PropertyValuesHolder ty = PropertyValuesHolder.ofFloat("translationY", transY, 0);
466
467 int rx = (int) Math.max(Math.max(width - getPivotX(), 0), getPivotX());
468 int ry = (int) Math.max(Math.max(height - getPivotY(), 0), getPivotY());
469 float radius = (float) Math.sqrt(rx * rx + ry * ry);
470 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
471 Animator reveal = LauncherAnimUtils.createCircularReveal(this, (int) getPivotX(),
472 (int) getPivotY(), 0, radius);
473 reveal.setDuration(mMaterialExpandDuration);
474 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
475
476 mContent.setAlpha(0f);
477 Animator iconsAlpha = LauncherAnimUtils.ofFloat(mContent, "alpha", 0f, 1f);
478 iconsAlpha.setDuration(mMaterialExpandDuration);
479 iconsAlpha.setStartDelay(mMaterialExpandStagger);
480 iconsAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
481
482 mFolderName.setAlpha(0f);
483 Animator textAlpha = LauncherAnimUtils.ofFloat(mFolderName, "alpha", 0f, 1f);
484 textAlpha.setDuration(mMaterialExpandDuration);
485 textAlpha.setStartDelay(mMaterialExpandStagger);
486 textAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
487
488 Animator drift = LauncherAnimUtils.ofPropertyValuesHolder(this, tx, ty);
489 drift.setDuration(mMaterialExpandDuration);
490 drift.setStartDelay(mMaterialExpandStagger);
491 drift.setInterpolator(new LogDecelerateInterpolator(60, 0));
492
493 anim.play(drift);
494 anim.play(iconsAlpha);
495 anim.play(textAlpha);
496 anim.play(reveal);
497
498 openFolderAnim = anim;
499
500 mContent.setLayerType(LAYER_TYPE_HARDWARE, null);
501 onCompleteRunnable = new Runnable() {
502 @Override
503 public void run() {
504 mContent.setLayerType(LAYER_TYPE_NONE, null);
505 }
506 };
507 }
508 openFolderAnim.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700509 @Override
510 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700511 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700512 String.format(getContext().getString(R.string.folder_opened),
Adam Cohen3371da02011-10-25 21:38:29 -0700513 mContent.getCountX(), mContent.getCountY()));
Adam Cohendf2cc412011-04-27 16:56:57 -0700514 mState = STATE_ANIMATING;
515 }
516 @Override
517 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700518 mState = STATE_OPEN;
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700519
520 if (onCompleteRunnable != null) {
521 onCompleteRunnable.run();
522 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800523
Adam Cohenac56cff2011-09-28 20:45:37 -0700524 setFocusOnFirstChild();
Adam Cohendf2cc412011-04-27 16:56:57 -0700525 }
526 });
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700527 openFolderAnim.start();
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800528
529 // Make sure the folder picks up the last drag move even if the finger doesn't move.
530 if (mDragController.isDragging()) {
531 mDragController.forceTouchMove();
532 }
533 }
534
535 public void beginExternalDrag(ShortcutInfo item) {
536 setupContentForNumItems(getItemCount() + 1);
537 findAndSetEmptyCells(item);
538
539 mCurrentDragInfo = item;
540 mEmptyCell[0] = item.cellX;
541 mEmptyCell[1] = item.cellY;
542 mIsExternalDrag = true;
543
544 mDragInProgress = true;
Adam Cohendf2cc412011-04-27 16:56:57 -0700545 }
546
Adam Cohen3371da02011-10-25 21:38:29 -0700547 private void sendCustomAccessibilityEvent(int type, String text) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700548 AccessibilityManager accessibilityManager = (AccessibilityManager)
549 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
550 if (accessibilityManager.isEnabled()) {
Adam Cohen3371da02011-10-25 21:38:29 -0700551 AccessibilityEvent event = AccessibilityEvent.obtain(type);
552 onInitializeAccessibilityEvent(event);
553 event.getText().add(text);
Michael Jurka8b805b12012-04-18 14:23:14 -0700554 accessibilityManager.sendAccessibilityEvent(event);
Adam Cohen3371da02011-10-25 21:38:29 -0700555 }
556 }
557
Adam Cohenac56cff2011-09-28 20:45:37 -0700558 private void setFocusOnFirstChild() {
559 View firstChild = mContent.getChildAt(0, 0);
560 if (firstChild != null) {
561 firstChild.requestFocus();
562 }
563 }
564
Adam Cohendf2cc412011-04-27 16:56:57 -0700565 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700566 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800567 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
568 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
569 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
Michael Jurka032e6ba2013-04-22 15:08:42 +0200570 final ObjectAnimator oa =
Michael Jurka2ecf9952012-06-18 12:52:28 -0700571 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohendf2cc412011-04-27 16:56:57 -0700572
Adam Cohen2801caf2011-05-13 20:57:39 -0700573 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700574 @Override
575 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700576 onCloseComplete();
Michael Jurka0121c3e2012-05-31 08:36:04 -0700577 setLayerType(LAYER_TYPE_NONE, null);
Adam Cohen2801caf2011-05-13 20:57:39 -0700578 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700579 }
580 @Override
581 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700582 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700583 getContext().getString(R.string.folder_closed));
Adam Cohendf2cc412011-04-27 16:56:57 -0700584 mState = STATE_ANIMATING;
585 }
586 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700587 oa.setDuration(mExpandDuration);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700588 setLayerType(LAYER_TYPE_HARDWARE, null);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100589 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700590 }
591
Adam Cohencb3382b2011-05-24 14:07:08 -0700592 public boolean acceptDrop(DragObject d) {
593 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700594 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700595 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
596 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
597 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700598 }
599
Adam Cohendf2cc412011-04-27 16:56:57 -0700600 protected boolean findAndSetEmptyCells(ShortcutInfo item) {
601 int[] emptyCell = new int[2];
602 if (mContent.findCellForSpan(emptyCell, item.spanX, item.spanY)) {
603 item.cellX = emptyCell[0];
604 item.cellY = emptyCell[1];
Adam Cohendf2cc412011-04-27 16:56:57 -0700605 return true;
606 } else {
607 return false;
608 }
609 }
610
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800611 protected View createAndAddShortcut(ShortcutInfo item) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700612 final BubbleTextView textView =
613 (BubbleTextView) mInflater.inflate(R.layout.folder_application, this, false);
614 textView.applyFromShortcutInfo(item, mIconCache, false);
615
Adam Cohendf2cc412011-04-27 16:56:57 -0700616 textView.setOnClickListener(this);
617 textView.setOnLongClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700618 textView.setOnFocusChangeListener(mFocusIndicatorHandler);
Adam Cohendf2cc412011-04-27 16:56:57 -0700619
Adam Cohenc508b2d2011-06-28 14:41:44 -0700620 // We need to check here to verify that the given item's location isn't already occupied
Adam Cohen3bf84d32012-05-07 20:17:14 -0700621 // by another item.
Adam Cohen0057bbc2011-08-12 18:30:51 -0700622 if (mContent.getChildAt(item.cellX, item.cellY) != null || item.cellX < 0 || item.cellY < 0
623 || item.cellX >= mContent.getCountX() || item.cellY >= mContent.getCountY()) {
Jason Monk4ff73882014-04-24 16:48:07 -0400624 // This shouldn't happen, log it.
Adam Cohen3bf84d32012-05-07 20:17:14 -0700625 Log.e(TAG, "Folder order not properly persisted during bind");
Adam Cohenc508b2d2011-06-28 14:41:44 -0700626 if (!findAndSetEmptyCells(item)) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800627 return null;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700628 }
629 }
630
Adam Cohendf2cc412011-04-27 16:56:57 -0700631 CellLayout.LayoutParams lp =
632 new CellLayout.LayoutParams(item.cellX, item.cellY, item.spanX, item.spanY);
633 boolean insert = false;
Adam Cohenac56cff2011-09-28 20:45:37 -0700634 textView.setOnKeyListener(new FolderKeyEventListener());
Adam Cohendf2cc412011-04-27 16:56:57 -0700635 mContent.addViewToCellLayout(textView, insert ? 0 : -1, (int)item.id, lp, true);
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800636 return textView;
Adam Cohendf2cc412011-04-27 16:56:57 -0700637 }
638
Adam Cohencb3382b2011-05-24 14:07:08 -0700639 public void onDragEnter(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700640 mPreviousTargetCell[0] = -1;
641 mPreviousTargetCell[1] = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700642 mOnExitAlarm.cancelAlarm();
643 }
644
645 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
646 public void onAlarm(Alarm alarm) {
647 realTimeReorder(mEmptyCell, mTargetCell);
648 }
649 };
650
651 boolean readingOrderGreaterThan(int[] v1, int[] v2) {
652 if (v1[1] > v2[1] || (v1[1] == v2[1] && v1[0] > v2[0])) {
653 return true;
654 } else {
655 return false;
656 }
657 }
658
659 private void realTimeReorder(int[] empty, int[] target) {
660 boolean wrap;
661 int startX;
662 int endX;
663 int startY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700664 int delay = 0;
665 float delayAmount = 30;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700666 if (readingOrderGreaterThan(target, empty)) {
667 wrap = empty[0] >= mContent.getCountX() - 1;
668 startY = wrap ? empty[1] + 1 : empty[1];
669 for (int y = startY; y <= target[1]; y++) {
670 startX = y == empty[1] ? empty[0] + 1 : 0;
671 endX = y < target[1] ? mContent.getCountX() - 1 : target[0];
672 for (int x = startX; x <= endX; x++) {
673 View v = mContent.getChildAt(x,y);
674 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen482ed822012-03-02 14:15:13 -0800675 REORDER_ANIMATION_DURATION, delay, true, true)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700676 empty[0] = x;
677 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700678 delay += delayAmount;
679 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700680 }
681 }
682 }
683 } else {
684 wrap = empty[0] == 0;
685 startY = wrap ? empty[1] - 1 : empty[1];
686 for (int y = startY; y >= target[1]; y--) {
687 startX = y == empty[1] ? empty[0] - 1 : mContent.getCountX() - 1;
688 endX = y > target[1] ? 0 : target[0];
689 for (int x = startX; x >= endX; x--) {
690 View v = mContent.getChildAt(x,y);
691 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen482ed822012-03-02 14:15:13 -0800692 REORDER_ANIMATION_DURATION, delay, true, true)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700693 empty[0] = x;
694 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700695 delay += delayAmount;
696 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700697 }
698 }
699 }
700 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700701 }
702
Adam Cohen2374abf2013-04-16 14:56:57 -0700703 public boolean isLayoutRtl() {
704 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
705 }
706
Adam Cohencb3382b2011-05-24 14:07:08 -0700707 public void onDragOver(DragObject d) {
Alan Viverette4cda5b72013-08-28 17:53:41 -0700708 final DragView dragView = d.dragView;
709 final int scrollOffset = mScrollView.getScrollY();
710 final float[] r = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, dragView, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700711 r[0] -= getPaddingLeft();
712 r[1] -= getPaddingTop();
713
Alan Viverette4cda5b72013-08-28 17:53:41 -0700714 final long downTime = SystemClock.uptimeMillis();
715 final MotionEvent translatedEv = MotionEvent.obtain(
716 downTime, downTime, MotionEvent.ACTION_MOVE, d.x, d.y, 0);
717
718 if (!mAutoScrollHelper.isEnabled()) {
719 mAutoScrollHelper.setEnabled(true);
720 }
721
722 final boolean handled = mAutoScrollHelper.onTouch(this, translatedEv);
723 translatedEv.recycle();
724
725 if (handled) {
726 mReorderAlarm.cancelAlarm();
727 } else {
728 mTargetCell = mContent.findNearestArea(
729 (int) r[0], (int) r[1] + scrollOffset, 1, 1, mTargetCell);
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700730 if (isLayoutRtl()) {
731 mTargetCell[0] = mContent.getCountX() - mTargetCell[0] - 1;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700732 }
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700733 if (mTargetCell[0] != mPreviousTargetCell[0]
734 || mTargetCell[1] != mPreviousTargetCell[1]) {
735 mReorderAlarm.cancelAlarm();
736 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
737 mReorderAlarm.setAlarm(REORDER_DELAY);
738 mPreviousTargetCell[0] = mTargetCell[0];
739 mPreviousTargetCell[1] = mTargetCell[1];
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700740 }
Adam Cohenbfbfd262011-06-13 16:55:12 -0700741 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700742 }
743
Adam Cohenbfbfd262011-06-13 16:55:12 -0700744 // This is used to compute the visual center of the dragView. The idea is that
745 // the visual center represents the user's interpretation of where the item is, and hence
746 // is the appropriate point to use when determining drop location.
747 private float[] getDragViewVisualCenter(int x, int y, int xOffset, int yOffset,
748 DragView dragView, float[] recycle) {
749 float res[];
750 if (recycle == null) {
751 res = new float[2];
752 } else {
753 res = recycle;
754 }
755
756 // These represent the visual top and left of drag view if a dragRect was provided.
757 // If a dragRect was not provided, then they correspond to the actual view left and
758 // top, as the dragRect is in that case taken to be the entire dragView.
759 // R.dimen.dragViewOffsetY.
760 int left = x - xOffset;
761 int top = y - yOffset;
762
763 // In order to find the visual center, we shift by half the dragRect
764 res[0] = left + dragView.getDragRegion().width() / 2;
765 res[1] = top + dragView.getDragRegion().height() / 2;
766
767 return res;
768 }
769
770 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
771 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700772 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700773 }
774 };
775
Adam Cohen95bb8002011-07-03 23:40:28 -0700776 public void completeDragExit() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700777 mLauncher.closeFolder();
778 mCurrentDragInfo = null;
779 mCurrentDragView = null;
780 mSuppressOnAdd = false;
781 mRearrangeOnClose = true;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800782 mIsExternalDrag = false;
Adam Cohen3e8f8112011-07-02 18:03:00 -0700783 }
784
Adam Cohencb3382b2011-05-24 14:07:08 -0700785 public void onDragExit(DragObject d) {
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700786 // Exiting folder; stop the auto scroller.
Alan Viverette4cda5b72013-08-28 17:53:41 -0700787 mAutoScrollHelper.setEnabled(false);
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700788 // We only close the folder if this is a true drag exit, ie. not because
789 // a drop has occurred above the folder.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700790 if (!d.dragComplete) {
791 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
792 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
793 }
794 mReorderAlarm.cancelAlarm();
Adam Cohen2801caf2011-05-13 20:57:39 -0700795 }
796
Michael Jurka1e2f4652013-07-08 18:03:46 -0700797 public void onDropCompleted(final View target, final DragObject d,
798 final boolean isFlingToDelete, final boolean success) {
799 if (mDeferDropAfterUninstall) {
Michael Jurkaf3007582013-08-21 14:33:57 +0200800 Log.d(TAG, "Deferred handling drop because waiting for uninstall.");
Michael Jurka1e2f4652013-07-08 18:03:46 -0700801 mDeferredAction = new Runnable() {
802 public void run() {
803 onDropCompleted(target, d, isFlingToDelete, success);
804 mDeferredAction = null;
805 }
806 };
807 return;
808 }
809
810 boolean beingCalledAfterUninstall = mDeferredAction != null;
811 boolean successfulDrop =
812 success && (!beingCalledAfterUninstall || mUninstallSuccessful);
Winson Chung5f8afe62013-08-12 16:19:28 -0700813
Michael Jurka1e2f4652013-07-08 18:03:46 -0700814 if (successfulDrop) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800815 if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon && target != this) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700816 replaceFolderWithFinalItem();
817 }
818 } else {
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700819 setupContentForNumItems(getItemCount());
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700820 // The drag failed, we need to return the item to the folder
821 mFolderIcon.onDrop(d);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700822 }
823
824 if (target != this) {
825 if (mOnExitAlarm.alarmPending()) {
826 mOnExitAlarm.cancelAlarm();
Michael Jurka54554252013-08-01 12:52:23 +0200827 if (!successfulDrop) {
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700828 mSuppressFolderDeletion = true;
829 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700830 completeDragExit();
831 }
Adam Cohen9c58d822013-12-13 17:18:10 -0800832 }
833
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700834 mDeleteFolderOnDropCompleted = false;
835 mDragInProgress = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700836 mItemAddedBackToSelfViaIcon = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700837 mCurrentDragInfo = null;
838 mCurrentDragView = null;
839 mSuppressOnAdd = false;
Adam Cohen4045eb72011-10-06 11:44:26 -0700840
841 // Reordering may have occured, and we need to save the new item locations. We do this once
842 // at the end to prevent unnecessary database operations.
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700843 updateItemLocationsInDatabaseBatch();
Adam Cohen4045eb72011-10-06 11:44:26 -0700844 }
845
Michael Jurka1e2f4652013-07-08 18:03:46 -0700846 public void deferCompleteDropAfterUninstallActivity() {
847 mDeferDropAfterUninstall = true;
848 }
849
850 public void onUninstallActivityReturned(boolean success) {
851 mDeferDropAfterUninstall = false;
852 mUninstallSuccessful = success;
853 if (mDeferredAction != null) {
854 mDeferredAction.run();
855 }
856 }
857
Winson Chunga48487a2012-03-20 16:19:37 -0700858 @Override
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800859 public float getIntrinsicIconScaleFactor() {
860 return 1f;
861 }
862
863 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800864 public boolean supportsFlingToDelete() {
865 return true;
866 }
867
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000868 @Override
869 public boolean supportsAppInfoDropTarget() {
870 return false;
871 }
872
873 @Override
874 public boolean supportsDeleteDropTarget() {
875 return true;
876 }
877
Winson Chunga48487a2012-03-20 16:19:37 -0700878 public void onFlingToDelete(DragObject d, int x, int y, PointF vec) {
879 // Do nothing
880 }
881
882 @Override
883 public void onFlingToDeleteCompleted() {
884 // Do nothing
885 }
886
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700887 private void updateItemLocationsInDatabaseBatch() {
888 ArrayList<View> list = getItemsInReadingOrder();
889 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
890 for (int i = 0; i < list.size(); i++) {
891 View v = list.get(i);
892 ItemInfo info = (ItemInfo) v.getTag();
Sunny Goyal08f72612015-01-05 13:41:43 -0800893 info.rank = i;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700894 items.add(info);
895 }
896
897 LauncherModel.moveItemsInDatabase(mLauncher, items, mInfo.id, 0);
898 }
899
Adam Cohene25af792013-06-06 23:08:25 -0700900 public void addItemLocationsInDatabase() {
901 ArrayList<View> list = getItemsInReadingOrder();
902 for (int i = 0; i < list.size(); i++) {
903 View v = list.get(i);
904 ItemInfo info = (ItemInfo) v.getTag();
905 LauncherModel.addItemToDatabase(mLauncher, info, mInfo.id, 0,
906 info.cellX, info.cellY, false);
907 }
908 }
909
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700910 public void notifyDrop() {
911 if (mDragInProgress) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700912 mItemAddedBackToSelfViaIcon = true;
Adam Cohen76078c42011-06-09 15:06:52 -0700913 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700914 }
915
916 public boolean isDropEnabled() {
917 return true;
918 }
919
Adam Cohen4045eb72011-10-06 11:44:26 -0700920 private void setupContentDimensions(int count) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700921 ArrayList<View> list = getItemsInReadingOrder();
922
923 int countX = mContent.getCountX();
924 int countY = mContent.getCountY();
Adam Cohen7c693212011-05-18 15:26:57 -0700925 boolean done = false;
Adam Cohen2801caf2011-05-13 20:57:39 -0700926
Adam Cohen7c693212011-05-18 15:26:57 -0700927 while (!done) {
928 int oldCountX = countX;
929 int oldCountY = countY;
930 if (countX * countY < count) {
931 // Current grid is too small, expand it
Adam Cohen78dc83e2011-11-15 17:10:00 -0800932 if ((countX <= countY || countY == mMaxCountY) && countX < mMaxCountX) {
Adam Cohen7c693212011-05-18 15:26:57 -0700933 countX++;
934 } else if (countY < mMaxCountY) {
935 countY++;
936 }
937 if (countY == 0) countY++;
938 } else if ((countY - 1) * countX >= count && countY >= countX) {
939 countY = Math.max(0, countY - 1);
940 } else if ((countX - 1) * countY >= count) {
941 countX = Math.max(0, countX - 1);
Adam Cohen2801caf2011-05-13 20:57:39 -0700942 }
Adam Cohen7c693212011-05-18 15:26:57 -0700943 done = countX == oldCountX && countY == oldCountY;
Adam Cohen2801caf2011-05-13 20:57:39 -0700944 }
Adam Cohen7c693212011-05-18 15:26:57 -0700945 mContent.setGridSize(countX, countY);
Adam Cohen2801caf2011-05-13 20:57:39 -0700946 arrangeChildren(list);
947 }
948
949 public boolean isFull() {
Adam Cohen78dc83e2011-11-15 17:10:00 -0800950 return getItemCount() >= mMaxNumItems;
Adam Cohen2801caf2011-05-13 20:57:39 -0700951 }
952
953 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700954 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700955
Winson Chung892c74d2013-08-22 16:15:50 -0700956 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700957 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700958 int height = getFolderHeight();
Adam Cohen2801caf2011-05-13 20:57:39 -0700959
Adam Cohen307fe232012-08-16 17:55:58 -0700960 float scale = parent.getDescendantRectRelativeToSelf(mFolderIcon, mTempRect);
Adam Cohen8e776a62011-06-28 18:10:06 -0700961
Winson Chungaf40f202013-09-18 18:26:31 -0700962 LauncherAppState app = LauncherAppState.getInstance();
963 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
964
Adam Cohen307fe232012-08-16 17:55:58 -0700965 int centerX = (int) (mTempRect.left + mTempRect.width() * scale / 2);
966 int centerY = (int) (mTempRect.top + mTempRect.height() * scale / 2);
Adam Cohen2801caf2011-05-13 20:57:39 -0700967 int centeredLeft = centerX - width / 2;
968 int centeredTop = centerY - height / 2;
Winson Chung3057b1c2013-10-10 17:35:35 -0700969 int currentPage = mLauncher.getWorkspace().getNextPage();
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800970 // In case the workspace is scrolling, we need to use the final scroll to compute
971 // the folders bounds.
972 mLauncher.getWorkspace().setFinalScrollForPageChange(currentPage);
Adam Cohen35e7e642011-07-17 14:47:18 -0700973 // We first fetch the currently visible CellLayoutChildren
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800974 CellLayout currentLayout = (CellLayout) mLauncher.getWorkspace().getChildAt(currentPage);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700975 ShortcutAndWidgetContainer boundingLayout = currentLayout.getShortcutsAndWidgets();
Adam Cohen35e7e642011-07-17 14:47:18 -0700976 Rect bounds = new Rect();
977 parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800978 // We reset the workspaces scroll
979 mLauncher.getWorkspace().resetFinalScrollForPageChange(currentPage);
Adam Cohen2801caf2011-05-13 20:57:39 -0700980
Adam Cohen35e7e642011-07-17 14:47:18 -0700981 // We need to bound the folder to the currently visible CellLayoutChildren
982 int left = Math.min(Math.max(bounds.left, centeredLeft),
983 bounds.left + bounds.width() - width);
984 int top = Math.min(Math.max(bounds.top, centeredTop),
985 bounds.top + bounds.height() - height);
Winson Chungaf40f202013-09-18 18:26:31 -0700986 if (grid.isPhone() && (grid.availableWidthPx - width) < grid.iconSizePx) {
987 // Center the folder if it is full (on phones only)
988 left = (grid.availableWidthPx - width) / 2;
989 } else if (width >= bounds.width()) {
990 // If the folder doesn't fit within the bounds, center it about the desired bounds
Adam Cohen35e7e642011-07-17 14:47:18 -0700991 left = bounds.left + (bounds.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700992 }
Adam Cohen35e7e642011-07-17 14:47:18 -0700993 if (height >= bounds.height()) {
994 top = bounds.top + (bounds.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700995 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700996
997 int folderPivotX = width / 2 + (centeredLeft - left);
998 int folderPivotY = height / 2 + (centeredTop - top);
999 setPivotX(folderPivotX);
1000 setPivotY(folderPivotY);
Adam Cohen268c4752012-06-06 17:47:33 -07001001 mFolderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
Adam Cohen2801caf2011-05-13 20:57:39 -07001002 (1.0f * folderPivotX / width));
Adam Cohen268c4752012-06-06 17:47:33 -07001003 mFolderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
Adam Cohen2801caf2011-05-13 20:57:39 -07001004 (1.0f * folderPivotY / height));
Adam Cohen3bf84d32012-05-07 20:17:14 -07001005
Adam Cohen662b5982011-12-13 17:45:21 -08001006 lp.width = width;
1007 lp.height = height;
1008 lp.x = left;
1009 lp.y = top;
Adam Cohen2801caf2011-05-13 20:57:39 -07001010 }
1011
Adam Cohen268c4752012-06-06 17:47:33 -07001012 float getPivotXForIconAnimation() {
1013 return mFolderIconPivotX;
1014 }
1015 float getPivotYForIconAnimation() {
1016 return mFolderIconPivotY;
1017 }
1018
Adam Cohen2801caf2011-05-13 20:57:39 -07001019 private void setupContentForNumItems(int count) {
Adam Cohen4045eb72011-10-06 11:44:26 -07001020 setupContentDimensions(count);
Adam Cohen2801caf2011-05-13 20:57:39 -07001021
Adam Cohen8e776a62011-06-28 18:10:06 -07001022 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -07001023 if (lp == null) {
Adam Cohen8e776a62011-06-28 18:10:06 -07001024 lp = new DragLayer.LayoutParams(0, 0);
1025 lp.customPosition = true;
Adam Cohen2801caf2011-05-13 20:57:39 -07001026 setLayoutParams(lp);
1027 }
1028 centerAboutIcon();
1029 }
1030
Winson Chung892c74d2013-08-22 16:15:50 -07001031 private int getContentAreaHeight() {
1032 LauncherAppState app = LauncherAppState.getInstance();
1033 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1034 Rect workspacePadding = grid.getWorkspacePadding(grid.isLandscape ?
1035 CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
1036 int maxContentAreaHeight = grid.availableHeightPx -
Winson Chung892c74d2013-08-22 16:15:50 -07001037 workspacePadding.top - workspacePadding.bottom -
Winson Chung892c74d2013-08-22 16:15:50 -07001038 mFolderNameHeight;
Adam Cohen1960ea42013-11-12 11:33:14 +00001039 int height = Math.min(maxContentAreaHeight,
Winson Chung892c74d2013-08-22 16:15:50 -07001040 mContent.getDesiredHeight());
Adam Cohen1960ea42013-11-12 11:33:14 +00001041 return Math.max(height, MIN_CONTENT_DIMEN);
1042 }
1043
1044 private int getContentAreaWidth() {
1045 return Math.max(mContent.getDesiredWidth(), MIN_CONTENT_DIMEN);
Winson Chung892c74d2013-08-22 16:15:50 -07001046 }
1047
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001048 private int getFolderHeight() {
Winson Chung892c74d2013-08-22 16:15:50 -07001049 int height = getPaddingTop() + getPaddingBottom()
1050 + getContentAreaHeight() + mFolderNameHeight;
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001051 return height;
1052 }
Adam Cohen234c4cd2011-07-17 21:03:04 -07001053
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001054 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001055 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
1056 int height = getFolderHeight();
Adam Cohen1960ea42013-11-12 11:33:14 +00001057 int contentAreaWidthSpec = MeasureSpec.makeMeasureSpec(getContentAreaWidth(),
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001058 MeasureSpec.EXACTLY);
Winson Chung892c74d2013-08-22 16:15:50 -07001059 int contentAreaHeightSpec = MeasureSpec.makeMeasureSpec(getContentAreaHeight(),
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001060 MeasureSpec.EXACTLY);
Adam Cohen1960ea42013-11-12 11:33:14 +00001061
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08001062 if (LauncherAppState.isDisableAllApps()) {
Nilesh Agrawal5f7099a2014-01-02 15:54:57 -08001063 // Don't cap the height of the content to allow scrolling.
1064 mContent.setFixedSize(getContentAreaWidth(), mContent.getDesiredHeight());
1065 } else {
1066 mContent.setFixedSize(getContentAreaWidth(), getContentAreaHeight());
1067 }
1068
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001069 mScrollView.measure(contentAreaWidthSpec, contentAreaHeightSpec);
1070 mFolderName.measure(contentAreaWidthSpec,
Adam Cohen234c4cd2011-07-17 21:03:04 -07001071 MeasureSpec.makeMeasureSpec(mFolderNameHeight, MeasureSpec.EXACTLY));
1072 setMeasuredDimension(width, height);
1073 }
1074
Adam Cohen2801caf2011-05-13 20:57:39 -07001075 private void arrangeChildren(ArrayList<View> list) {
1076 int[] vacant = new int[2];
1077 if (list == null) {
1078 list = getItemsInReadingOrder();
1079 }
1080 mContent.removeAllViews();
1081
1082 for (int i = 0; i < list.size(); i++) {
1083 View v = list.get(i);
1084 mContent.getVacantCell(vacant, 1, 1);
1085 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) v.getLayoutParams();
1086 lp.cellX = vacant[0];
1087 lp.cellY = vacant[1];
1088 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohen2792a332011-09-26 21:09:47 -07001089 if (info.cellX != vacant[0] || info.cellY != vacant[1]) {
1090 info.cellX = vacant[0];
1091 info.cellY = vacant[1];
1092 LauncherModel.addOrMoveItemInDatabase(mLauncher, info, mInfo.id, 0,
1093 info.cellX, info.cellY);
1094 }
Adam Cohen2801caf2011-05-13 20:57:39 -07001095 boolean insert = false;
1096 mContent.addViewToCellLayout(v, insert ? 0 : -1, (int)info.id, lp, true);
1097 }
Adam Cohen7c693212011-05-18 15:26:57 -07001098 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -07001099 }
1100
Adam Cohena9cf38f2011-05-02 15:36:58 -07001101 public int getItemCount() {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001102 return mContent.getShortcutsAndWidgets().getChildCount();
Adam Cohena9cf38f2011-05-02 15:36:58 -07001103 }
1104
1105 public View getItemAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001106 return mContent.getShortcutsAndWidgets().getChildAt(index);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001107 }
1108
Adam Cohen2801caf2011-05-13 20:57:39 -07001109 private void onCloseComplete() {
Adam Cohen05e0f402011-08-01 12:12:49 -07001110 DragLayer parent = (DragLayer) getParent();
Michael Jurka5649c282012-06-18 10:33:21 -07001111 if (parent != null) {
1112 parent.removeView(this);
1113 }
Adam Cohen4554ee12011-08-03 16:13:21 -07001114 mDragController.removeDropTarget((DropTarget) this);
Adam Cohen05e0f402011-08-01 12:12:49 -07001115 clearFocus();
Adam Cohenac56cff2011-09-28 20:45:37 -07001116 mFolderIcon.requestFocus();
Adam Cohen05e0f402011-08-01 12:12:49 -07001117
Adam Cohen2801caf2011-05-13 20:57:39 -07001118 if (mRearrangeOnClose) {
1119 setupContentForNumItems(getItemCount());
1120 mRearrangeOnClose = false;
1121 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001122 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001123 if (!mDragInProgress && !mSuppressFolderDeletion) {
1124 replaceFolderWithFinalItem();
1125 } else if (mDragInProgress) {
1126 mDeleteFolderOnDropCompleted = true;
1127 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001128 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001129 mSuppressFolderDeletion = false;
Adam Cohenafb01ee2011-06-23 15:38:03 -07001130 }
1131
1132 private void replaceFolderWithFinalItem() {
Adam Cohenafb01ee2011-06-23 15:38:03 -07001133 // Add the last remaining child to the workspace in place of the folder
Adam Cohenfb91f302012-06-11 15:45:18 -07001134 Runnable onCompleteRunnable = new Runnable() {
1135 @Override
1136 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -07001137 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screenId);
Adam Cohenafb01ee2011-06-23 15:38:03 -07001138
Winson Chung33231f52013-12-09 16:57:45 -08001139 View child = null;
Adam Cohenfb91f302012-06-11 15:45:18 -07001140 // Move the item from the folder to the workspace, in the position of the folder
1141 if (getItemCount() == 1) {
1142 ShortcutInfo finalItem = mInfo.contents.get(0);
Adam Cohenc5e63f32012-07-12 16:16:57 -07001143 child = mLauncher.createShortcut(R.layout.application, cellLayout,
Adam Cohenfb91f302012-06-11 15:45:18 -07001144 finalItem);
1145 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -07001146 mInfo.screenId, mInfo.cellX, mInfo.cellY);
Adam Cohenfb91f302012-06-11 15:45:18 -07001147 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001148 if (getItemCount() <= 1) {
1149 // Remove the folder
1150 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
Dan Sandler0eb687f2014-01-10 13:24:55 -05001151 if (cellLayout != null) {
1152 // b/12446428 -- sometimes the cell layout has already gone away?
1153 cellLayout.removeView(mFolderIcon);
1154 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001155 if (mFolderIcon instanceof DropTarget) {
1156 mDragController.removeDropTarget((DropTarget) mFolderIcon);
1157 }
1158 mLauncher.removeFolder(mInfo);
1159 }
Adam Cohenc5e63f32012-07-12 16:16:57 -07001160 // We add the child after removing the folder to prevent both from existing at
Winson Chung0e6a7132013-08-23 12:55:10 -07001161 // the same time in the CellLayout. We need to add the new item with addInScreenFromBind()
1162 // to ensure that hotseat items are placed correctly.
Adam Cohenc5e63f32012-07-12 16:16:57 -07001163 if (child != null) {
Winson Chung0e6a7132013-08-23 12:55:10 -07001164 mLauncher.getWorkspace().addInScreenFromBind(child, mInfo.container, mInfo.screenId,
Adam Cohenc5e63f32012-07-12 16:16:57 -07001165 mInfo.cellX, mInfo.cellY, mInfo.spanX, mInfo.spanY);
1166 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001167 }
1168 };
1169 View finalChild = getItemAt(0);
1170 if (finalChild != null) {
1171 mFolderIcon.performDestroyAnimation(finalChild, onCompleteRunnable);
Winson Chung33231f52013-12-09 16:57:45 -08001172 } else {
1173 onCompleteRunnable.run();
Adam Cohenafb01ee2011-06-23 15:38:03 -07001174 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001175 mDestroyed = true;
1176 }
1177
1178 boolean isDestroyed() {
1179 return mDestroyed;
Adam Cohen2801caf2011-05-13 20:57:39 -07001180 }
1181
Adam Cohenac56cff2011-09-28 20:45:37 -07001182 // This method keeps track of the last item in the folder for the purposes
1183 // of keyboard focus
1184 private void updateTextViewFocus() {
1185 View lastChild = getItemAt(getItemCount() - 1);
1186 getItemAt(getItemCount() - 1);
1187 if (lastChild != null) {
1188 mFolderName.setNextFocusDownId(lastChild.getId());
1189 mFolderName.setNextFocusRightId(lastChild.getId());
1190 mFolderName.setNextFocusLeftId(lastChild.getId());
1191 mFolderName.setNextFocusUpId(lastChild.getId());
1192 }
1193 }
1194
Adam Cohenbfbfd262011-06-13 16:55:12 -07001195 public void onDrop(DragObject d) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001196 Runnable cleanUpRunnable = null;
1197
Adam Cohen689ff162014-05-08 17:27:56 -07001198 // If we are coming from All Apps space, we defer removing the extra empty screen
1199 // until the folder closes
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001200 if (d.dragSource != mLauncher.getWorkspace() && !(d.dragSource instanceof Folder)) {
1201 cleanUpRunnable = new Runnable() {
1202 @Override
1203 public void run() {
Adam Cohen689ff162014-05-08 17:27:56 -07001204 mLauncher.exitSpringLoadedDragModeDelayed(true,
1205 Launcher.EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
1206 null);
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001207 }
1208 };
Adam Cohenbfbfd262011-06-13 16:55:12 -07001209 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001210
1211 View currentDragView;
1212 ShortcutInfo si = mCurrentDragInfo;
1213 if (mIsExternalDrag) {
1214 si.cellX = mEmptyCell[0];
1215 si.cellY = mEmptyCell[1];
Sunny Goyal95abbb32014-08-04 10:53:22 -07001216
1217 // Actually move the item in the database if it was an external drag. Call this
1218 // before creating the view, so that ShortcutInfo is updated appropriately.
1219 LauncherModel.addOrMoveItemInDatabase(
1220 mLauncher, si, mInfo.id, 0, si.cellX, si.cellY);
1221
1222 // We only need to update the locations if it doesn't get handled in #onDropCompleted.
1223 if (d.dragSource != this) {
1224 updateItemLocationsInDatabaseBatch();
1225 }
1226 mIsExternalDrag = false;
1227
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001228 currentDragView = createAndAddShortcut(si);
1229 } else {
1230 currentDragView = mCurrentDragView;
1231 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) currentDragView.getLayoutParams();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001232 si.cellX = lp.cellX = mEmptyCell[0];
1233 si.cellX = lp.cellY = mEmptyCell[1];
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001234 mContent.addViewToCellLayout(currentDragView, -1, (int) si.id, lp, true);
Adam Cohenbfbfd262011-06-13 16:55:12 -07001235 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001236
1237 if (d.dragView.hasDrawn()) {
1238
1239 // Temporarily reset the scale such that the animation target gets calculated correctly.
1240 float scaleX = getScaleX();
1241 float scaleY = getScaleY();
1242 setScaleX(1.0f);
1243 setScaleY(1.0f);
1244 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, currentDragView,
1245 cleanUpRunnable, null);
1246 setScaleX(scaleX);
1247 setScaleY(scaleY);
1248 } else {
1249 d.deferDragViewCleanupPostAnimation = false;
1250 currentDragView.setVisibility(VISIBLE);
1251 }
1252 mItemsInvalidated = true;
1253 setupContentDimensions(getItemCount());
1254
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001255 // Temporarily suppress the listener, as we did all the work already here.
1256 mSuppressOnAdd = true;
1257 mInfo.add(si);
1258 mSuppressOnAdd = false;
Sunny Goyal4b020172014-08-28 14:51:14 -07001259 // Clear the drag info, as it is no longer being dragged.
1260 mCurrentDragInfo = null;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001261 }
1262
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001263 // This is used so the item doesn't immediately appear in the folder when added. In one case
1264 // we need to create the illusion that the item isn't added back to the folder yet, to
1265 // to correspond to the animation of the icon back into the folder. This is
1266 public void hideItem(ShortcutInfo info) {
1267 View v = getViewForInfo(info);
1268 v.setVisibility(INVISIBLE);
1269 }
1270 public void showItem(ShortcutInfo info) {
1271 View v = getViewForInfo(info);
1272 v.setVisibility(VISIBLE);
1273 }
1274
Adam Cohenbfbfd262011-06-13 16:55:12 -07001275 public void onAdd(ShortcutInfo item) {
1276 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001277 // If the item was dropped onto this open folder, we have done the work associated
1278 // with adding the item to the folder, as indicated by mSuppressOnAdd being set
Adam Cohenbfbfd262011-06-13 16:55:12 -07001279 if (mSuppressOnAdd) return;
1280 if (!findAndSetEmptyCells(item)) {
1281 // The current layout is full, can we expand it?
1282 setupContentForNumItems(getItemCount() + 1);
1283 findAndSetEmptyCells(item);
1284 }
1285 createAndAddShortcut(item);
1286 LauncherModel.addOrMoveItemInDatabase(
1287 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
1288 }
1289
Adam Cohena9cf38f2011-05-02 15:36:58 -07001290 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -07001291 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001292 // If this item is being dragged from this open folder, we have already handled
1293 // the work associated with removing the item, so we don't have to do anything here.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001294 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001295 View v = getViewForInfo(item);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001296 mContent.removeView(v);
Adam Cohen2801caf2011-05-13 20:57:39 -07001297 if (mState == STATE_ANIMATING) {
1298 mRearrangeOnClose = true;
1299 } else {
1300 setupContentForNumItems(getItemCount());
1301 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001302 if (getItemCount() <= 1) {
1303 replaceFolderWithFinalItem();
1304 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001305 }
Adam Cohen7c693212011-05-18 15:26:57 -07001306
Adam Cohendf1e4e82011-06-24 15:57:39 -07001307 private View getViewForInfo(ShortcutInfo item) {
1308 for (int j = 0; j < mContent.getCountY(); j++) {
1309 for (int i = 0; i < mContent.getCountX(); i++) {
1310 View v = mContent.getChildAt(i, j);
1311 if (v.getTag() == item) {
1312 return v;
1313 }
1314 }
1315 }
1316 return null;
1317 }
1318
Adam Cohen76078c42011-06-09 15:06:52 -07001319 public void onItemsChanged() {
Adam Cohenac56cff2011-09-28 20:45:37 -07001320 updateTextViewFocus();
Adam Cohen76078c42011-06-09 15:06:52 -07001321 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001322
Adam Cohen76fc0852011-06-17 13:26:23 -07001323 public void onTitleChanged(CharSequence title) {
1324 }
Adam Cohen76078c42011-06-09 15:06:52 -07001325
Adam Cohen7c693212011-05-18 15:26:57 -07001326 public ArrayList<View> getItemsInReadingOrder() {
1327 if (mItemsInvalidated) {
1328 mItemsInReadingOrder.clear();
1329 for (int j = 0; j < mContent.getCountY(); j++) {
1330 for (int i = 0; i < mContent.getCountX(); i++) {
1331 View v = mContent.getChildAt(i, j);
1332 if (v != null) {
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001333 mItemsInReadingOrder.add(v);
Adam Cohen7c693212011-05-18 15:26:57 -07001334 }
1335 }
1336 }
1337 mItemsInvalidated = false;
1338 }
1339 return mItemsInReadingOrder;
1340 }
Adam Cohen8dfcba42011-07-07 16:38:18 -07001341
1342 public void getLocationInDragLayer(int[] loc) {
1343 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
1344 }
Adam Cohenea0818d2011-09-30 20:06:58 -07001345
1346 public void onFocusChange(View v, boolean hasFocus) {
1347 if (v == mFolderName && hasFocus) {
1348 startEditingFolderName();
1349 }
1350 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001351
1352 @Override
1353 public void getHitRectRelativeToDragLayer(Rect outRect) {
1354 getHitRect(outRect);
1355 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001356}