blob: 69254776ca1d7a0c436537f60f25dc6c02deeece [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Adam Cohendf2cc412011-04-27 16:56:57 -070019import android.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
Adam Cohenc4fe9ea2014-08-18 18:54:10 -070021import android.animation.AnimatorSet;
Adam Cohendf2cc412011-04-27 16:56:57 -070022import android.animation.ObjectAnimator;
23import android.animation.PropertyValuesHolder;
Sunny Goyalc46bfef2015-01-05 12:40:08 -080024import android.annotation.TargetApi;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080025import android.content.Context;
Adam Cohen76fc0852011-06-17 13:26:23 -070026import android.content.res.Resources;
Winson Chung043f2af2012-03-01 16:09:54 -080027import android.graphics.PointF;
Romain Guyfb5411e2010-02-24 10:04:17 -080028import android.graphics.Rect;
Sunny Goyalc46bfef2015-01-05 12:40:08 -080029import android.os.Build;
Alan Viverette4cda5b72013-08-28 17:53:41 -070030import android.os.SystemClock;
Adam Cohen7a14d0b2011-06-24 11:36:35 -070031import android.text.InputType;
Adam Cohene601a432011-07-26 21:51:30 -070032import android.text.Selection;
33import android.text.Spannable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.util.AttributeSet;
Adam Cohen3bf84d32012-05-07 20:17:14 -070035import android.util.Log;
Adam Cohen76fc0852011-06-17 13:26:23 -070036import android.view.ActionMode;
37import android.view.KeyEvent;
Adam Cohendf2cc412011-04-27 16:56:57 -070038import android.view.LayoutInflater;
Adam Cohen76fc0852011-06-17 13:26:23 -070039import android.view.Menu;
40import android.view.MenuItem;
Adam Cohen0c872ba2011-05-05 10:34:16 -070041import android.view.MotionEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.view.View;
Adam Cohen3371da02011-10-25 21:38:29 -070043import android.view.accessibility.AccessibilityEvent;
44import android.view.accessibility.AccessibilityManager;
Adam Cohenc4fe9ea2014-08-18 18:54:10 -070045import android.view.animation.AccelerateInterpolator;
Adam Cohen76fc0852011-06-17 13:26:23 -070046import android.view.inputmethod.EditorInfo;
47import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.widget.LinearLayout;
Adam Cohenf0f4eda2013-06-06 21:27:03 -070049import android.widget.ScrollView;
Adam Cohendf2cc412011-04-27 16:56:57 -070050import android.widget.TextView;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080051
Daniel Sandler325dc232013-06-05 22:57:57 -040052import com.android.launcher3.FolderInfo.FolderListener;
Romain Guyedcce092010-03-04 13:03:17 -080053
Adam Cohenc0dcf592011-06-01 15:30:43 -070054import java.util.ArrayList;
Adam Cohen3bf84d32012-05-07 20:17:14 -070055import java.util.Collections;
56import java.util.Comparator;
Adam Cohenc0dcf592011-06-01 15:30:43 -070057
The Android Open Source Project31dd5032009-03-03 19:32:27 -080058/**
59 * Represents a set of icons chosen by the user or generated by the system.
60 */
Adam Cohen8dfcba42011-07-07 16:38:18 -070061public class Folder extends LinearLayout implements DragSource, View.OnClickListener,
Adam Cohenea0818d2011-09-30 20:06:58 -070062 View.OnLongClickListener, DropTarget, FolderListener, TextView.OnEditorActionListener,
63 View.OnFocusChangeListener {
Adam Cohendf2cc412011-04-27 16:56:57 -070064 private static final String TAG = "Launcher.Folder";
65
Adam Cohen4eac29a2011-07-11 17:53:37 -070066 protected DragController mDragController;
67 protected Launcher mLauncher;
68 protected FolderInfo mInfo;
69
Adam Cohendf2cc412011-04-27 16:56:57 -070070 static final int STATE_NONE = -1;
71 static final int STATE_SMALL = 0;
72 static final int STATE_ANIMATING = 1;
73 static final int STATE_OPEN = 2;
74
75 private int mExpandDuration;
Adam Cohenc4fe9ea2014-08-18 18:54:10 -070076 private int mMaterialExpandDuration;
77 private int mMaterialExpandStagger;
Adam Cohendf2cc412011-04-27 16:56:57 -070078 protected CellLayout mContent;
Adam Cohenf0f4eda2013-06-06 21:27:03 -070079 private ScrollView mScrollView;
Adam Cohendf2cc412011-04-27 16:56:57 -070080 private final LayoutInflater mInflater;
81 private final IconCache mIconCache;
82 private int mState = STATE_NONE;
Adam Cohenbfbfd262011-06-13 16:55:12 -070083 private static final int REORDER_ANIMATION_DURATION = 230;
Adam Cohenf0f4eda2013-06-06 21:27:03 -070084 private static final int REORDER_DELAY = 250;
Adam Cohen5d518fa2013-12-05 14:16:23 -080085 private static final int ON_EXIT_CLOSE_DELAY = 400;
Adam Cohen2801caf2011-05-13 20:57:39 -070086 private boolean mRearrangeOnClose = false;
87 private FolderIcon mFolderIcon;
88 private int mMaxCountX;
89 private int mMaxCountY;
Adam Cohen78dc83e2011-11-15 17:10:00 -080090 private int mMaxNumItems;
Adam Cohen7c693212011-05-18 15:26:57 -070091 private ArrayList<View> mItemsInReadingOrder = new ArrayList<View>();
92 boolean mItemsInvalidated = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -070093 private ShortcutInfo mCurrentDragInfo;
94 private View mCurrentDragView;
Jorim Jaggi55bd9722014-01-16 15:30:42 -080095 private boolean mIsExternalDrag;
Adam Cohenbfbfd262011-06-13 16:55:12 -070096 boolean mSuppressOnAdd = false;
97 private int[] mTargetCell = new int[2];
98 private int[] mPreviousTargetCell = new int[2];
99 private int[] mEmptyCell = new int[2];
100 private Alarm mReorderAlarm = new Alarm();
101 private Alarm mOnExitAlarm = new Alarm();
Adam Cohen76fc0852011-06-17 13:26:23 -0700102 private int mFolderNameHeight;
Adam Cohen8e776a62011-06-28 18:10:06 -0700103 private Rect mTempRect = new Rect();
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700104 private boolean mDragInProgress = false;
105 private boolean mDeleteFolderOnDropCompleted = false;
106 private boolean mSuppressFolderDeletion = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700107 private boolean mItemAddedBackToSelfViaIcon = false;
Adam Cohenac56cff2011-09-28 20:45:37 -0700108 FolderEditText mFolderName;
Adam Cohen268c4752012-06-06 17:47:33 -0700109 private float mFolderIconPivotX;
110 private float mFolderIconPivotY;
Adam Cohen228da5a2011-07-27 22:23:47 -0700111
Adam Cohen76fc0852011-06-17 13:26:23 -0700112 private boolean mIsEditingName = false;
113 private InputMethodManager mInputMethodManager;
Adam Cohendf2cc412011-04-27 16:56:57 -0700114
Adam Cohena65beee2011-06-27 21:32:23 -0700115 private static String sDefaultFolderName;
116 private static String sHintText;
117
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700118 private FocusIndicatorView mFocusIndicatorHandler;
119
Adam Cohen1960ea42013-11-12 11:33:14 +0000120 // We avoid measuring the scroll view with a 0 width or height, as this
121 // results in CellLayout being measured as UNSPECIFIED, which it does
122 // not support.
123 private static final int MIN_CONTENT_DIMEN = 5;
124
Adam Cohenfb91f302012-06-11 15:45:18 -0700125 private boolean mDestroyed;
126
Michael Jurka1e2f4652013-07-08 18:03:46 -0700127 private Runnable mDeferredAction;
128 private boolean mDeferDropAfterUninstall;
129 private boolean mUninstallSuccessful;
130
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131 /**
132 * Used to inflate the Workspace from XML.
133 *
134 * @param context The application's context.
135 * @param attrs The attribtues set containing the Workspace's customization values.
136 */
137 public Folder(Context context, AttributeSet attrs) {
138 super(context, attrs);
Winson Chung5f8afe62013-08-12 16:19:28 -0700139
140 LauncherAppState app = LauncherAppState.getInstance();
141 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800142 setAlwaysDrawnWithCacheEnabled(false);
Adam Cohendf2cc412011-04-27 16:56:57 -0700143 mInflater = LayoutInflater.from(context);
Winson Chung5f8afe62013-08-12 16:19:28 -0700144 mIconCache = app.getIconCache();
Adam Cohen78dc83e2011-11-15 17:10:00 -0800145
146 Resources res = getResources();
Michael Jurka414300a2013-08-27 15:42:35 +0200147 mMaxCountX = (int) grid.numColumns;
Nilesh Agrawal5f7099a2014-01-02 15:54:57 -0800148 // Allow scrolling folders when DISABLE_ALL_APPS is true.
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800149 if (LauncherAppState.isDisableAllApps()) {
Nilesh Agrawal5f7099a2014-01-02 15:54:57 -0800150 mMaxCountY = mMaxNumItems = Integer.MAX_VALUE;
151 } else {
152 mMaxCountY = (int) grid.numRows;
153 mMaxNumItems = mMaxCountX * mMaxCountY;
154 }
Adam Cohen76fc0852011-06-17 13:26:23 -0700155
156 mInputMethodManager = (InputMethodManager)
Michael Jurka8b805b12012-04-18 14:23:14 -0700157 getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
Adam Cohen76fc0852011-06-17 13:26:23 -0700158
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700159 mExpandDuration = res.getInteger(R.integer.config_folderExpandDuration);
160 mMaterialExpandDuration = res.getInteger(R.integer.config_materialFolderExpandDuration);
161 mMaterialExpandStagger = res.getInteger(R.integer.config_materialFolderExpandStagger);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700162
163 if (sDefaultFolderName == null) {
164 sDefaultFolderName = res.getString(R.string.folder_name);
165 }
Adam Cohena65beee2011-06-27 21:32:23 -0700166 if (sHintText == null) {
167 sHintText = res.getString(R.string.folder_hint_text);
168 }
Adam Cohen4eac29a2011-07-11 17:53:37 -0700169 mLauncher = (Launcher) context;
Adam Cohenac56cff2011-09-28 20:45:37 -0700170 // We need this view to be focusable in touch mode so that when text editing of the folder
171 // name is complete, we have something to focus on, thus hiding the cursor and giving
172 // reliable behvior when clicking the text field (since it will always gain focus on click).
173 setFocusableInTouchMode(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174 }
175
176 @Override
177 protected void onFinishInflate() {
178 super.onFinishInflate();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700179 mScrollView = (ScrollView) findViewById(R.id.scroll_view);
Adam Cohendf2cc412011-04-27 16:56:57 -0700180 mContent = (CellLayout) findViewById(R.id.folder_content);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700181
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700182 mFocusIndicatorHandler = new FocusIndicatorView(getContext());
183 mContent.addView(mFocusIndicatorHandler, 0);
184 mFocusIndicatorHandler.getLayoutParams().height = FocusIndicatorView.DEFAULT_LAYOUT_SIZE;
185 mFocusIndicatorHandler.getLayoutParams().width = FocusIndicatorView.DEFAULT_LAYOUT_SIZE;
186
Winson Chung5f8afe62013-08-12 16:19:28 -0700187 LauncherAppState app = LauncherAppState.getInstance();
188 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
189
Winson Chung5f8afe62013-08-12 16:19:28 -0700190 mContent.setCellDimensions(grid.folderCellWidthPx, grid.folderCellHeightPx);
Adam Cohen2801caf2011-05-13 20:57:39 -0700191 mContent.setGridSize(0, 0);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700192 mContent.getShortcutsAndWidgets().setMotionEventSplittingEnabled(false);
Adam Cohen2374abf2013-04-16 14:56:57 -0700193 mContent.setInvertIfRtl(true);
Adam Cohenac56cff2011-09-28 20:45:37 -0700194 mFolderName = (FolderEditText) findViewById(R.id.folder_name);
195 mFolderName.setFolder(this);
Adam Cohenea0818d2011-09-30 20:06:58 -0700196 mFolderName.setOnFocusChangeListener(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700197
198 // We find out how tall the text view wants to be (it is set to wrap_content), so that
199 // we can allocate the appropriate amount of space for it.
200 int measureSpec = MeasureSpec.UNSPECIFIED;
201 mFolderName.measure(measureSpec, measureSpec);
202 mFolderNameHeight = mFolderName.getMeasuredHeight();
203
204 // We disable action mode for now since it messes up the view on phones
205 mFolderName.setCustomSelectionActionModeCallback(mActionModeCallback);
Adam Cohen76fc0852011-06-17 13:26:23 -0700206 mFolderName.setOnEditorActionListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700207 mFolderName.setSelectAllOnFocus(true);
Adam Cohen7a14d0b2011-06-24 11:36:35 -0700208 mFolderName.setInputType(mFolderName.getInputType() |
209 InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800210 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700211
Adam Cohen76fc0852011-06-17 13:26:23 -0700212 private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
213 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
214 return false;
215 }
216
217 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
218 return false;
219 }
220
221 public void onDestroyActionMode(ActionMode mode) {
222 }
223
224 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
225 return false;
226 }
227 };
228
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800229 public void onClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700230 Object tag = v.getTag();
231 if (tag instanceof ShortcutInfo) {
Adam Cohenb5fe60c2013-06-06 22:03:51 -0700232 mLauncher.onClick(v);
Adam Cohendf2cc412011-04-27 16:56:57 -0700233 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800234 }
235
236 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -0700237 // Return if global dragging is not enabled
238 if (!mLauncher.isDraggingEnabled()) return true;
239
Adam Cohendf2cc412011-04-27 16:56:57 -0700240 Object tag = v.getTag();
241 if (tag instanceof ShortcutInfo) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700242 ShortcutInfo item = (ShortcutInfo) tag;
243 if (!v.isInTouchMode()) {
244 return false;
245 }
246
Adam Cohenac8c8762011-07-13 11:15:27 -0700247 mLauncher.getWorkspace().beginDragShared(v, this);
Adam Cohen76078c42011-06-09 15:06:52 -0700248
249 mCurrentDragInfo = item;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700250 mEmptyCell[0] = item.cellX;
251 mEmptyCell[1] = item.cellY;
252 mCurrentDragView = v;
Adam Cohenfc53cd22011-07-20 15:45:11 -0700253
254 mContent.removeView(mCurrentDragView);
255 mInfo.remove(mCurrentDragInfo);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700256 mDragInProgress = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700257 mItemAddedBackToSelfViaIcon = false;
Adam Cohendf2cc412011-04-27 16:56:57 -0700258 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800259 return true;
260 }
261
Adam Cohen76fc0852011-06-17 13:26:23 -0700262 public boolean isEditingName() {
263 return mIsEditingName;
264 }
265
266 public void startEditingFolderName() {
Adam Cohena65beee2011-06-27 21:32:23 -0700267 mFolderName.setHint("");
Adam Cohen76fc0852011-06-17 13:26:23 -0700268 mIsEditingName = true;
269 }
270
271 public void dismissEditingName() {
272 mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
273 doneEditingFolderName(true);
274 }
275
276 public void doneEditingFolderName(boolean commit) {
Adam Cohena65beee2011-06-27 21:32:23 -0700277 mFolderName.setHint(sHintText);
Adam Cohen1df26a32011-08-12 16:15:23 -0700278 // Convert to a string here to ensure that no other state associated with the text field
279 // gets saved.
Adam Cohen3371da02011-10-25 21:38:29 -0700280 String newTitle = mFolderName.getText().toString();
281 mInfo.setTitle(newTitle);
Adam Cohen76fc0852011-06-17 13:26:23 -0700282 LauncherModel.updateItemInDatabase(mLauncher, mInfo);
Adam Cohenac56cff2011-09-28 20:45:37 -0700283
Adam Cohen3371da02011-10-25 21:38:29 -0700284 if (commit) {
285 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700286 String.format(getContext().getString(R.string.folder_renamed), newTitle));
Adam Cohen3371da02011-10-25 21:38:29 -0700287 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700288 // In order to clear the focus from the text field, we set the focus on ourself. This
289 // ensures that every time the field is clicked, focus is gained, giving reliable behavior.
290 requestFocus();
291
Adam Cohene601a432011-07-26 21:51:30 -0700292 Selection.setSelection((Spannable) mFolderName.getText(), 0, 0);
Adam Cohen76fc0852011-06-17 13:26:23 -0700293 mIsEditingName = false;
294 }
295
296 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
297 if (actionId == EditorInfo.IME_ACTION_DONE) {
298 dismissEditingName();
299 return true;
300 }
301 return false;
302 }
303
304 public View getEditTextRegion() {
305 return mFolderName;
306 }
307
Anjali Koppalf05545f2014-03-10 19:18:43 -0700308 public CellLayout getContent() {
309 return mContent;
310 }
311
Adam Cohen0c872ba2011-05-05 10:34:16 -0700312 /**
313 * We need to handle touch events to prevent them from falling through to the workspace below.
314 */
315 @Override
316 public boolean onTouchEvent(MotionEvent ev) {
317 return true;
318 }
319
Joe Onorato00acb122009-08-04 16:04:30 -0400320 public void setDragController(DragController dragController) {
321 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800322 }
323
Adam Cohen2801caf2011-05-13 20:57:39 -0700324 void setFolderIcon(FolderIcon icon) {
325 mFolderIcon = icon;
326 }
327
Adam Cohen3371da02011-10-25 21:38:29 -0700328 @Override
329 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
330 // When the folder gets focus, we don't want to announce the list of items.
331 return true;
332 }
333
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800334 /**
335 * @return the FolderInfo object associated with this folder
336 */
337 FolderInfo getInfo() {
338 return mInfo;
339 }
340
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800341 void bind(FolderInfo info) {
342 mInfo = info;
Adam Cohendf2cc412011-04-27 16:56:57 -0700343 ArrayList<ShortcutInfo> children = info.contents;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700344 ArrayList<ShortcutInfo> overflow = new ArrayList<ShortcutInfo>();
Sunny Goyal08f72612015-01-05 13:41:43 -0800345
346 final int totalChildren = children.size();
347 setupContentForNumItems(totalChildren);
348
349 // Arrange children in the grid based on the rank.
350 Collections.sort(children, Utilities.RANK_COMPARATOR);
351 final int countX = mContent.getCountX();
352
353 int visibleChildren = 0;
Adam Cohendf2cc412011-04-27 16:56:57 -0700354 for (int i = 0; i < children.size(); i++) {
Sunny Goyal08f72612015-01-05 13:41:43 -0800355 ShortcutInfo child = children.get(i);
356 child.cellX = i % countX;
357 child.cellY = i / countX;
358
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800359 if (createAndAddShortcut(child) == null) {
Adam Cohenc508b2d2011-06-28 14:41:44 -0700360 overflow.add(child);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700361 } else {
Sunny Goyal08f72612015-01-05 13:41:43 -0800362 visibleChildren++;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700363 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700364 }
Adam Cohenc508b2d2011-06-28 14:41:44 -0700365
Adam Cohen0057bbc2011-08-12 18:30:51 -0700366 // We rearrange the items in case there are any empty gaps
Sunny Goyal08f72612015-01-05 13:41:43 -0800367 setupContentForNumItems(visibleChildren);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700368
Jason Monk4ff73882014-04-24 16:48:07 -0400369 // If our folder has too many items we prune them from the list. This is an issue
Adam Cohenc508b2d2011-06-28 14:41:44 -0700370 // when upgrading from the old Folders implementation which could contain an unlimited
371 // number of items.
372 for (ShortcutInfo item: overflow) {
373 mInfo.remove(item);
374 LauncherModel.deleteItemFromDatabase(mLauncher, item);
375 }
376
Adam Cohen4dbe6d92011-05-18 17:14:15 -0700377 mItemsInvalidated = true;
Adam Cohenac56cff2011-09-28 20:45:37 -0700378 updateTextViewFocus();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700379 mInfo.addListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700380
Adam Cohenafb01ee2011-06-23 15:38:03 -0700381 if (!sDefaultFolderName.contentEquals(mInfo.title)) {
Adam Cohen4ef610f2011-06-21 22:37:36 -0700382 mFolderName.setText(mInfo.title);
383 } else {
384 mFolderName.setText("");
385 }
Winson Chung33231f52013-12-09 16:57:45 -0800386
387 // In case any children didn't come across during loading, clean up the folder accordingly
388 mFolderIcon.post(new Runnable() {
389 public void run() {
390 if (getItemCount() <= 1) {
391 replaceFolderWithFinalItem();
392 }
393 }
394 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700395 }
396
397 /**
398 * Creates a new UserFolder, inflated from R.layout.user_folder.
399 *
400 * @param context The application's context.
401 *
402 * @return A new UserFolder.
403 */
404 static Folder fromXml(Context context) {
405 return (Folder) LayoutInflater.from(context).inflate(R.layout.user_folder, null);
406 }
407
408 /**
409 * This method is intended to make the UserFolder to be visually identical in size and position
410 * to its associated FolderIcon. This allows for a seamless transition into the expanded state.
411 */
412 private void positionAndSizeAsIcon() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700413 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800414 setScaleX(0.8f);
415 setScaleY(0.8f);
416 setAlpha(0f);
Adam Cohendf2cc412011-04-27 16:56:57 -0700417 mState = STATE_SMALL;
418 }
419
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700420 private void prepareReveal() {
421 setScaleX(1f);
422 setScaleY(1f);
423 setAlpha(1f);
424 mState = STATE_SMALL;
425 }
426
Adam Cohendf2cc412011-04-27 16:56:57 -0700427 public void animateOpen() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700428 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen6441de02011-12-14 14:25:32 -0800429
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700430 Animator openFolderAnim = null;
431 final Runnable onCompleteRunnable;
Kenny Guyd794a3f2014-09-16 15:17:58 +0100432 if (!Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700433 positionAndSizeAsIcon();
434 centerAboutIcon();
435
436 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1);
437 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
438 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
439 final ObjectAnimator oa =
440 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
441 oa.setDuration(mExpandDuration);
442 openFolderAnim = oa;
443
444 setLayerType(LAYER_TYPE_HARDWARE, null);
445 onCompleteRunnable = new Runnable() {
446 @Override
447 public void run() {
448 setLayerType(LAYER_TYPE_NONE, null);
449 }
450 };
451 } else {
452 prepareReveal();
453 centerAboutIcon();
454
455 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
456 int height = getFolderHeight();
457
458 float transX = - 0.075f * (width / 2 - getPivotX());
459 float transY = - 0.075f * (height / 2 - getPivotY());
460 setTranslationX(transX);
461 setTranslationY(transY);
462 PropertyValuesHolder tx = PropertyValuesHolder.ofFloat("translationX", transX, 0);
463 PropertyValuesHolder ty = PropertyValuesHolder.ofFloat("translationY", transY, 0);
464
465 int rx = (int) Math.max(Math.max(width - getPivotX(), 0), getPivotX());
466 int ry = (int) Math.max(Math.max(height - getPivotY(), 0), getPivotY());
467 float radius = (float) Math.sqrt(rx * rx + ry * ry);
468 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
469 Animator reveal = LauncherAnimUtils.createCircularReveal(this, (int) getPivotX(),
470 (int) getPivotY(), 0, radius);
471 reveal.setDuration(mMaterialExpandDuration);
472 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
473
474 mContent.setAlpha(0f);
475 Animator iconsAlpha = LauncherAnimUtils.ofFloat(mContent, "alpha", 0f, 1f);
476 iconsAlpha.setDuration(mMaterialExpandDuration);
477 iconsAlpha.setStartDelay(mMaterialExpandStagger);
478 iconsAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
479
480 mFolderName.setAlpha(0f);
481 Animator textAlpha = LauncherAnimUtils.ofFloat(mFolderName, "alpha", 0f, 1f);
482 textAlpha.setDuration(mMaterialExpandDuration);
483 textAlpha.setStartDelay(mMaterialExpandStagger);
484 textAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
485
486 Animator drift = LauncherAnimUtils.ofPropertyValuesHolder(this, tx, ty);
487 drift.setDuration(mMaterialExpandDuration);
488 drift.setStartDelay(mMaterialExpandStagger);
489 drift.setInterpolator(new LogDecelerateInterpolator(60, 0));
490
491 anim.play(drift);
492 anim.play(iconsAlpha);
493 anim.play(textAlpha);
494 anim.play(reveal);
495
496 openFolderAnim = anim;
497
498 mContent.setLayerType(LAYER_TYPE_HARDWARE, null);
499 onCompleteRunnable = new Runnable() {
500 @Override
501 public void run() {
502 mContent.setLayerType(LAYER_TYPE_NONE, null);
503 }
504 };
505 }
506 openFolderAnim.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700507 @Override
508 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700509 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700510 String.format(getContext().getString(R.string.folder_opened),
Adam Cohen3371da02011-10-25 21:38:29 -0700511 mContent.getCountX(), mContent.getCountY()));
Adam Cohendf2cc412011-04-27 16:56:57 -0700512 mState = STATE_ANIMATING;
513 }
514 @Override
515 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700516 mState = STATE_OPEN;
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700517
518 if (onCompleteRunnable != null) {
519 onCompleteRunnable.run();
520 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800521
Adam Cohenac56cff2011-09-28 20:45:37 -0700522 setFocusOnFirstChild();
Adam Cohendf2cc412011-04-27 16:56:57 -0700523 }
524 });
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700525 openFolderAnim.start();
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800526
527 // Make sure the folder picks up the last drag move even if the finger doesn't move.
528 if (mDragController.isDragging()) {
529 mDragController.forceTouchMove();
530 }
531 }
532
533 public void beginExternalDrag(ShortcutInfo item) {
534 setupContentForNumItems(getItemCount() + 1);
535 findAndSetEmptyCells(item);
536
537 mCurrentDragInfo = item;
538 mEmptyCell[0] = item.cellX;
539 mEmptyCell[1] = item.cellY;
540 mIsExternalDrag = true;
541
542 mDragInProgress = true;
Adam Cohendf2cc412011-04-27 16:56:57 -0700543 }
544
Adam Cohen3371da02011-10-25 21:38:29 -0700545 private void sendCustomAccessibilityEvent(int type, String text) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700546 AccessibilityManager accessibilityManager = (AccessibilityManager)
547 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
548 if (accessibilityManager.isEnabled()) {
Adam Cohen3371da02011-10-25 21:38:29 -0700549 AccessibilityEvent event = AccessibilityEvent.obtain(type);
550 onInitializeAccessibilityEvent(event);
551 event.getText().add(text);
Michael Jurka8b805b12012-04-18 14:23:14 -0700552 accessibilityManager.sendAccessibilityEvent(event);
Adam Cohen3371da02011-10-25 21:38:29 -0700553 }
554 }
555
Adam Cohenac56cff2011-09-28 20:45:37 -0700556 private void setFocusOnFirstChild() {
557 View firstChild = mContent.getChildAt(0, 0);
558 if (firstChild != null) {
559 firstChild.requestFocus();
560 }
561 }
562
Adam Cohendf2cc412011-04-27 16:56:57 -0700563 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700564 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800565 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
566 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
567 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
Michael Jurka032e6ba2013-04-22 15:08:42 +0200568 final ObjectAnimator oa =
Michael Jurka2ecf9952012-06-18 12:52:28 -0700569 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohendf2cc412011-04-27 16:56:57 -0700570
Adam Cohen2801caf2011-05-13 20:57:39 -0700571 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700572 @Override
573 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700574 onCloseComplete();
Michael Jurka0121c3e2012-05-31 08:36:04 -0700575 setLayerType(LAYER_TYPE_NONE, null);
Adam Cohen2801caf2011-05-13 20:57:39 -0700576 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700577 }
578 @Override
579 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700580 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700581 getContext().getString(R.string.folder_closed));
Adam Cohendf2cc412011-04-27 16:56:57 -0700582 mState = STATE_ANIMATING;
583 }
584 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700585 oa.setDuration(mExpandDuration);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700586 setLayerType(LAYER_TYPE_HARDWARE, null);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100587 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700588 }
589
Adam Cohencb3382b2011-05-24 14:07:08 -0700590 public boolean acceptDrop(DragObject d) {
591 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700592 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700593 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
594 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
595 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700596 }
597
Adam Cohendf2cc412011-04-27 16:56:57 -0700598 protected boolean findAndSetEmptyCells(ShortcutInfo item) {
599 int[] emptyCell = new int[2];
600 if (mContent.findCellForSpan(emptyCell, item.spanX, item.spanY)) {
601 item.cellX = emptyCell[0];
602 item.cellY = emptyCell[1];
Adam Cohendf2cc412011-04-27 16:56:57 -0700603 return true;
604 } else {
605 return false;
606 }
607 }
608
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800609 protected View createAndAddShortcut(ShortcutInfo item) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700610 final BubbleTextView textView =
611 (BubbleTextView) mInflater.inflate(R.layout.folder_application, this, false);
612 textView.applyFromShortcutInfo(item, mIconCache, false);
613
Adam Cohendf2cc412011-04-27 16:56:57 -0700614 textView.setOnClickListener(this);
615 textView.setOnLongClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700616 textView.setOnFocusChangeListener(mFocusIndicatorHandler);
Adam Cohendf2cc412011-04-27 16:56:57 -0700617
Adam Cohenc508b2d2011-06-28 14:41:44 -0700618 // We need to check here to verify that the given item's location isn't already occupied
Adam Cohen3bf84d32012-05-07 20:17:14 -0700619 // by another item.
Adam Cohen0057bbc2011-08-12 18:30:51 -0700620 if (mContent.getChildAt(item.cellX, item.cellY) != null || item.cellX < 0 || item.cellY < 0
621 || item.cellX >= mContent.getCountX() || item.cellY >= mContent.getCountY()) {
Jason Monk4ff73882014-04-24 16:48:07 -0400622 // This shouldn't happen, log it.
Adam Cohen3bf84d32012-05-07 20:17:14 -0700623 Log.e(TAG, "Folder order not properly persisted during bind");
Adam Cohenc508b2d2011-06-28 14:41:44 -0700624 if (!findAndSetEmptyCells(item)) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800625 return null;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700626 }
627 }
628
Adam Cohendf2cc412011-04-27 16:56:57 -0700629 CellLayout.LayoutParams lp =
630 new CellLayout.LayoutParams(item.cellX, item.cellY, item.spanX, item.spanY);
631 boolean insert = false;
Adam Cohenac56cff2011-09-28 20:45:37 -0700632 textView.setOnKeyListener(new FolderKeyEventListener());
Adam Cohendf2cc412011-04-27 16:56:57 -0700633 mContent.addViewToCellLayout(textView, insert ? 0 : -1, (int)item.id, lp, true);
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800634 return textView;
Adam Cohendf2cc412011-04-27 16:56:57 -0700635 }
636
Adam Cohencb3382b2011-05-24 14:07:08 -0700637 public void onDragEnter(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700638 mPreviousTargetCell[0] = -1;
639 mPreviousTargetCell[1] = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700640 mOnExitAlarm.cancelAlarm();
641 }
642
643 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
644 public void onAlarm(Alarm alarm) {
645 realTimeReorder(mEmptyCell, mTargetCell);
646 }
647 };
648
649 boolean readingOrderGreaterThan(int[] v1, int[] v2) {
650 if (v1[1] > v2[1] || (v1[1] == v2[1] && v1[0] > v2[0])) {
651 return true;
652 } else {
653 return false;
654 }
655 }
656
657 private void realTimeReorder(int[] empty, int[] target) {
658 boolean wrap;
659 int startX;
660 int endX;
661 int startY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700662 int delay = 0;
663 float delayAmount = 30;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700664 if (readingOrderGreaterThan(target, empty)) {
665 wrap = empty[0] >= mContent.getCountX() - 1;
666 startY = wrap ? empty[1] + 1 : empty[1];
667 for (int y = startY; y <= target[1]; y++) {
668 startX = y == empty[1] ? empty[0] + 1 : 0;
669 endX = y < target[1] ? mContent.getCountX() - 1 : target[0];
670 for (int x = startX; x <= endX; x++) {
671 View v = mContent.getChildAt(x,y);
672 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen482ed822012-03-02 14:15:13 -0800673 REORDER_ANIMATION_DURATION, delay, true, true)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700674 empty[0] = x;
675 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700676 delay += delayAmount;
677 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700678 }
679 }
680 }
681 } else {
682 wrap = empty[0] == 0;
683 startY = wrap ? empty[1] - 1 : empty[1];
684 for (int y = startY; y >= target[1]; y--) {
685 startX = y == empty[1] ? empty[0] - 1 : mContent.getCountX() - 1;
686 endX = y > target[1] ? 0 : target[0];
687 for (int x = startX; x >= endX; x--) {
688 View v = mContent.getChildAt(x,y);
689 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen482ed822012-03-02 14:15:13 -0800690 REORDER_ANIMATION_DURATION, delay, true, true)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700691 empty[0] = x;
692 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700693 delay += delayAmount;
694 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700695 }
696 }
697 }
698 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700699 }
700
Sunny Goyalc46bfef2015-01-05 12:40:08 -0800701 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohen2374abf2013-04-16 14:56:57 -0700702 public boolean isLayoutRtl() {
703 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
704 }
705
Adam Cohencb3382b2011-05-24 14:07:08 -0700706 public void onDragOver(DragObject d) {
Alan Viverette4cda5b72013-08-28 17:53:41 -0700707 final DragView dragView = d.dragView;
708 final int scrollOffset = mScrollView.getScrollY();
709 final float[] r = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, dragView, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700710 r[0] -= getPaddingLeft();
711 r[1] -= getPaddingTop();
712
Alan Viverette4cda5b72013-08-28 17:53:41 -0700713 final long downTime = SystemClock.uptimeMillis();
714 final MotionEvent translatedEv = MotionEvent.obtain(
715 downTime, downTime, MotionEvent.ACTION_MOVE, d.x, d.y, 0);
716
Alan Viverette4cda5b72013-08-28 17:53:41 -0700717 translatedEv.recycle();
Sunny Goyalc46bfef2015-01-05 12:40:08 -0800718 mTargetCell = mContent.findNearestArea(
719 (int) r[0], (int) r[1] + scrollOffset, 1, 1, mTargetCell);
720 if (isLayoutRtl()) {
721 mTargetCell[0] = mContent.getCountX() - mTargetCell[0] - 1;
722 }
723 if (mTargetCell[0] != mPreviousTargetCell[0]
724 || mTargetCell[1] != mPreviousTargetCell[1]) {
Alan Viverette4cda5b72013-08-28 17:53:41 -0700725 mReorderAlarm.cancelAlarm();
Sunny Goyalc46bfef2015-01-05 12:40:08 -0800726 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
727 mReorderAlarm.setAlarm(REORDER_DELAY);
728 mPreviousTargetCell[0] = mTargetCell[0];
729 mPreviousTargetCell[1] = mTargetCell[1];
Adam Cohenbfbfd262011-06-13 16:55:12 -0700730 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700731 }
732
Adam Cohenbfbfd262011-06-13 16:55:12 -0700733 // This is used to compute the visual center of the dragView. The idea is that
734 // the visual center represents the user's interpretation of where the item is, and hence
735 // is the appropriate point to use when determining drop location.
736 private float[] getDragViewVisualCenter(int x, int y, int xOffset, int yOffset,
737 DragView dragView, float[] recycle) {
738 float res[];
739 if (recycle == null) {
740 res = new float[2];
741 } else {
742 res = recycle;
743 }
744
745 // These represent the visual top and left of drag view if a dragRect was provided.
746 // If a dragRect was not provided, then they correspond to the actual view left and
747 // top, as the dragRect is in that case taken to be the entire dragView.
748 // R.dimen.dragViewOffsetY.
749 int left = x - xOffset;
750 int top = y - yOffset;
751
752 // In order to find the visual center, we shift by half the dragRect
753 res[0] = left + dragView.getDragRegion().width() / 2;
754 res[1] = top + dragView.getDragRegion().height() / 2;
755
756 return res;
757 }
758
759 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
760 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700761 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700762 }
763 };
764
Adam Cohen95bb8002011-07-03 23:40:28 -0700765 public void completeDragExit() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700766 mLauncher.closeFolder();
767 mCurrentDragInfo = null;
768 mCurrentDragView = null;
769 mSuppressOnAdd = false;
770 mRearrangeOnClose = true;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800771 mIsExternalDrag = false;
Adam Cohen3e8f8112011-07-02 18:03:00 -0700772 }
773
Adam Cohencb3382b2011-05-24 14:07:08 -0700774 public void onDragExit(DragObject d) {
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700775 // We only close the folder if this is a true drag exit, ie. not because
776 // a drop has occurred above the folder.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700777 if (!d.dragComplete) {
778 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
779 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
780 }
781 mReorderAlarm.cancelAlarm();
Adam Cohen2801caf2011-05-13 20:57:39 -0700782 }
783
Michael Jurka1e2f4652013-07-08 18:03:46 -0700784 public void onDropCompleted(final View target, final DragObject d,
785 final boolean isFlingToDelete, final boolean success) {
786 if (mDeferDropAfterUninstall) {
Michael Jurkaf3007582013-08-21 14:33:57 +0200787 Log.d(TAG, "Deferred handling drop because waiting for uninstall.");
Michael Jurka1e2f4652013-07-08 18:03:46 -0700788 mDeferredAction = new Runnable() {
789 public void run() {
790 onDropCompleted(target, d, isFlingToDelete, success);
791 mDeferredAction = null;
792 }
793 };
794 return;
795 }
796
797 boolean beingCalledAfterUninstall = mDeferredAction != null;
798 boolean successfulDrop =
799 success && (!beingCalledAfterUninstall || mUninstallSuccessful);
Winson Chung5f8afe62013-08-12 16:19:28 -0700800
Michael Jurka1e2f4652013-07-08 18:03:46 -0700801 if (successfulDrop) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800802 if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon && target != this) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700803 replaceFolderWithFinalItem();
804 }
805 } else {
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700806 setupContentForNumItems(getItemCount());
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700807 // The drag failed, we need to return the item to the folder
808 mFolderIcon.onDrop(d);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700809 }
810
811 if (target != this) {
812 if (mOnExitAlarm.alarmPending()) {
813 mOnExitAlarm.cancelAlarm();
Michael Jurka54554252013-08-01 12:52:23 +0200814 if (!successfulDrop) {
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700815 mSuppressFolderDeletion = true;
816 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700817 completeDragExit();
818 }
Adam Cohen9c58d822013-12-13 17:18:10 -0800819 }
820
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700821 mDeleteFolderOnDropCompleted = false;
822 mDragInProgress = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700823 mItemAddedBackToSelfViaIcon = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700824 mCurrentDragInfo = null;
825 mCurrentDragView = null;
826 mSuppressOnAdd = false;
Adam Cohen4045eb72011-10-06 11:44:26 -0700827
828 // Reordering may have occured, and we need to save the new item locations. We do this once
829 // at the end to prevent unnecessary database operations.
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700830 updateItemLocationsInDatabaseBatch();
Adam Cohen4045eb72011-10-06 11:44:26 -0700831 }
832
Michael Jurka1e2f4652013-07-08 18:03:46 -0700833 public void deferCompleteDropAfterUninstallActivity() {
834 mDeferDropAfterUninstall = true;
835 }
836
837 public void onUninstallActivityReturned(boolean success) {
838 mDeferDropAfterUninstall = false;
839 mUninstallSuccessful = success;
840 if (mDeferredAction != null) {
841 mDeferredAction.run();
842 }
843 }
844
Winson Chunga48487a2012-03-20 16:19:37 -0700845 @Override
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800846 public float getIntrinsicIconScaleFactor() {
847 return 1f;
848 }
849
850 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800851 public boolean supportsFlingToDelete() {
852 return true;
853 }
854
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000855 @Override
856 public boolean supportsAppInfoDropTarget() {
857 return false;
858 }
859
860 @Override
861 public boolean supportsDeleteDropTarget() {
862 return true;
863 }
864
Winson Chunga48487a2012-03-20 16:19:37 -0700865 public void onFlingToDelete(DragObject d, int x, int y, PointF vec) {
866 // Do nothing
867 }
868
869 @Override
870 public void onFlingToDeleteCompleted() {
871 // Do nothing
872 }
873
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700874 private void updateItemLocationsInDatabaseBatch() {
875 ArrayList<View> list = getItemsInReadingOrder();
876 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
877 for (int i = 0; i < list.size(); i++) {
878 View v = list.get(i);
879 ItemInfo info = (ItemInfo) v.getTag();
Sunny Goyal08f72612015-01-05 13:41:43 -0800880 info.rank = i;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700881 items.add(info);
882 }
883
884 LauncherModel.moveItemsInDatabase(mLauncher, items, mInfo.id, 0);
885 }
886
Adam Cohene25af792013-06-06 23:08:25 -0700887 public void addItemLocationsInDatabase() {
888 ArrayList<View> list = getItemsInReadingOrder();
889 for (int i = 0; i < list.size(); i++) {
890 View v = list.get(i);
891 ItemInfo info = (ItemInfo) v.getTag();
892 LauncherModel.addItemToDatabase(mLauncher, info, mInfo.id, 0,
893 info.cellX, info.cellY, false);
894 }
895 }
896
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700897 public void notifyDrop() {
898 if (mDragInProgress) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700899 mItemAddedBackToSelfViaIcon = true;
Adam Cohen76078c42011-06-09 15:06:52 -0700900 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700901 }
902
903 public boolean isDropEnabled() {
904 return true;
905 }
906
Adam Cohen4045eb72011-10-06 11:44:26 -0700907 private void setupContentDimensions(int count) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700908 ArrayList<View> list = getItemsInReadingOrder();
909
910 int countX = mContent.getCountX();
911 int countY = mContent.getCountY();
Adam Cohen7c693212011-05-18 15:26:57 -0700912 boolean done = false;
Adam Cohen2801caf2011-05-13 20:57:39 -0700913
Adam Cohen7c693212011-05-18 15:26:57 -0700914 while (!done) {
915 int oldCountX = countX;
916 int oldCountY = countY;
917 if (countX * countY < count) {
918 // Current grid is too small, expand it
Adam Cohen78dc83e2011-11-15 17:10:00 -0800919 if ((countX <= countY || countY == mMaxCountY) && countX < mMaxCountX) {
Adam Cohen7c693212011-05-18 15:26:57 -0700920 countX++;
921 } else if (countY < mMaxCountY) {
922 countY++;
923 }
924 if (countY == 0) countY++;
925 } else if ((countY - 1) * countX >= count && countY >= countX) {
926 countY = Math.max(0, countY - 1);
927 } else if ((countX - 1) * countY >= count) {
928 countX = Math.max(0, countX - 1);
Adam Cohen2801caf2011-05-13 20:57:39 -0700929 }
Adam Cohen7c693212011-05-18 15:26:57 -0700930 done = countX == oldCountX && countY == oldCountY;
Adam Cohen2801caf2011-05-13 20:57:39 -0700931 }
Adam Cohen7c693212011-05-18 15:26:57 -0700932 mContent.setGridSize(countX, countY);
Adam Cohen2801caf2011-05-13 20:57:39 -0700933 arrangeChildren(list);
934 }
935
936 public boolean isFull() {
Adam Cohen78dc83e2011-11-15 17:10:00 -0800937 return getItemCount() >= mMaxNumItems;
Adam Cohen2801caf2011-05-13 20:57:39 -0700938 }
939
940 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700941 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700942
Winson Chung892c74d2013-08-22 16:15:50 -0700943 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700944 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700945 int height = getFolderHeight();
Adam Cohen2801caf2011-05-13 20:57:39 -0700946
Adam Cohen307fe232012-08-16 17:55:58 -0700947 float scale = parent.getDescendantRectRelativeToSelf(mFolderIcon, mTempRect);
Adam Cohen8e776a62011-06-28 18:10:06 -0700948
Winson Chungaf40f202013-09-18 18:26:31 -0700949 LauncherAppState app = LauncherAppState.getInstance();
950 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
951
Adam Cohen307fe232012-08-16 17:55:58 -0700952 int centerX = (int) (mTempRect.left + mTempRect.width() * scale / 2);
953 int centerY = (int) (mTempRect.top + mTempRect.height() * scale / 2);
Adam Cohen2801caf2011-05-13 20:57:39 -0700954 int centeredLeft = centerX - width / 2;
955 int centeredTop = centerY - height / 2;
Winson Chung3057b1c2013-10-10 17:35:35 -0700956 int currentPage = mLauncher.getWorkspace().getNextPage();
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800957 // In case the workspace is scrolling, we need to use the final scroll to compute
958 // the folders bounds.
959 mLauncher.getWorkspace().setFinalScrollForPageChange(currentPage);
Adam Cohen35e7e642011-07-17 14:47:18 -0700960 // We first fetch the currently visible CellLayoutChildren
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800961 CellLayout currentLayout = (CellLayout) mLauncher.getWorkspace().getChildAt(currentPage);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700962 ShortcutAndWidgetContainer boundingLayout = currentLayout.getShortcutsAndWidgets();
Adam Cohen35e7e642011-07-17 14:47:18 -0700963 Rect bounds = new Rect();
964 parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800965 // We reset the workspaces scroll
966 mLauncher.getWorkspace().resetFinalScrollForPageChange(currentPage);
Adam Cohen2801caf2011-05-13 20:57:39 -0700967
Adam Cohen35e7e642011-07-17 14:47:18 -0700968 // We need to bound the folder to the currently visible CellLayoutChildren
969 int left = Math.min(Math.max(bounds.left, centeredLeft),
970 bounds.left + bounds.width() - width);
971 int top = Math.min(Math.max(bounds.top, centeredTop),
972 bounds.top + bounds.height() - height);
Winson Chungaf40f202013-09-18 18:26:31 -0700973 if (grid.isPhone() && (grid.availableWidthPx - width) < grid.iconSizePx) {
974 // Center the folder if it is full (on phones only)
975 left = (grid.availableWidthPx - width) / 2;
976 } else if (width >= bounds.width()) {
977 // If the folder doesn't fit within the bounds, center it about the desired bounds
Adam Cohen35e7e642011-07-17 14:47:18 -0700978 left = bounds.left + (bounds.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700979 }
Adam Cohen35e7e642011-07-17 14:47:18 -0700980 if (height >= bounds.height()) {
981 top = bounds.top + (bounds.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700982 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700983
984 int folderPivotX = width / 2 + (centeredLeft - left);
985 int folderPivotY = height / 2 + (centeredTop - top);
986 setPivotX(folderPivotX);
987 setPivotY(folderPivotY);
Adam Cohen268c4752012-06-06 17:47:33 -0700988 mFolderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700989 (1.0f * folderPivotX / width));
Adam Cohen268c4752012-06-06 17:47:33 -0700990 mFolderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700991 (1.0f * folderPivotY / height));
Adam Cohen3bf84d32012-05-07 20:17:14 -0700992
Adam Cohen662b5982011-12-13 17:45:21 -0800993 lp.width = width;
994 lp.height = height;
995 lp.x = left;
996 lp.y = top;
Adam Cohen2801caf2011-05-13 20:57:39 -0700997 }
998
Adam Cohen268c4752012-06-06 17:47:33 -0700999 float getPivotXForIconAnimation() {
1000 return mFolderIconPivotX;
1001 }
1002 float getPivotYForIconAnimation() {
1003 return mFolderIconPivotY;
1004 }
1005
Adam Cohen2801caf2011-05-13 20:57:39 -07001006 private void setupContentForNumItems(int count) {
Adam Cohen4045eb72011-10-06 11:44:26 -07001007 setupContentDimensions(count);
Adam Cohen2801caf2011-05-13 20:57:39 -07001008
Adam Cohen8e776a62011-06-28 18:10:06 -07001009 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -07001010 if (lp == null) {
Adam Cohen8e776a62011-06-28 18:10:06 -07001011 lp = new DragLayer.LayoutParams(0, 0);
1012 lp.customPosition = true;
Adam Cohen2801caf2011-05-13 20:57:39 -07001013 setLayoutParams(lp);
1014 }
1015 centerAboutIcon();
1016 }
1017
Winson Chung892c74d2013-08-22 16:15:50 -07001018 private int getContentAreaHeight() {
1019 LauncherAppState app = LauncherAppState.getInstance();
1020 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1021 Rect workspacePadding = grid.getWorkspacePadding(grid.isLandscape ?
1022 CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
1023 int maxContentAreaHeight = grid.availableHeightPx -
Winson Chung892c74d2013-08-22 16:15:50 -07001024 workspacePadding.top - workspacePadding.bottom -
Winson Chung892c74d2013-08-22 16:15:50 -07001025 mFolderNameHeight;
Adam Cohen1960ea42013-11-12 11:33:14 +00001026 int height = Math.min(maxContentAreaHeight,
Winson Chung892c74d2013-08-22 16:15:50 -07001027 mContent.getDesiredHeight());
Adam Cohen1960ea42013-11-12 11:33:14 +00001028 return Math.max(height, MIN_CONTENT_DIMEN);
1029 }
1030
1031 private int getContentAreaWidth() {
1032 return Math.max(mContent.getDesiredWidth(), MIN_CONTENT_DIMEN);
Winson Chung892c74d2013-08-22 16:15:50 -07001033 }
1034
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001035 private int getFolderHeight() {
Winson Chung892c74d2013-08-22 16:15:50 -07001036 int height = getPaddingTop() + getPaddingBottom()
1037 + getContentAreaHeight() + mFolderNameHeight;
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001038 return height;
1039 }
Adam Cohen234c4cd2011-07-17 21:03:04 -07001040
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001041 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001042 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
1043 int height = getFolderHeight();
Adam Cohen1960ea42013-11-12 11:33:14 +00001044 int contentAreaWidthSpec = MeasureSpec.makeMeasureSpec(getContentAreaWidth(),
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001045 MeasureSpec.EXACTLY);
Winson Chung892c74d2013-08-22 16:15:50 -07001046 int contentAreaHeightSpec = MeasureSpec.makeMeasureSpec(getContentAreaHeight(),
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001047 MeasureSpec.EXACTLY);
Adam Cohen1960ea42013-11-12 11:33:14 +00001048
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08001049 if (LauncherAppState.isDisableAllApps()) {
Nilesh Agrawal5f7099a2014-01-02 15:54:57 -08001050 // Don't cap the height of the content to allow scrolling.
1051 mContent.setFixedSize(getContentAreaWidth(), mContent.getDesiredHeight());
1052 } else {
1053 mContent.setFixedSize(getContentAreaWidth(), getContentAreaHeight());
1054 }
1055
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001056 mScrollView.measure(contentAreaWidthSpec, contentAreaHeightSpec);
1057 mFolderName.measure(contentAreaWidthSpec,
Adam Cohen234c4cd2011-07-17 21:03:04 -07001058 MeasureSpec.makeMeasureSpec(mFolderNameHeight, MeasureSpec.EXACTLY));
1059 setMeasuredDimension(width, height);
1060 }
1061
Adam Cohen2801caf2011-05-13 20:57:39 -07001062 private void arrangeChildren(ArrayList<View> list) {
1063 int[] vacant = new int[2];
1064 if (list == null) {
1065 list = getItemsInReadingOrder();
1066 }
1067 mContent.removeAllViews();
1068
1069 for (int i = 0; i < list.size(); i++) {
1070 View v = list.get(i);
1071 mContent.getVacantCell(vacant, 1, 1);
1072 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) v.getLayoutParams();
1073 lp.cellX = vacant[0];
1074 lp.cellY = vacant[1];
1075 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohen2792a332011-09-26 21:09:47 -07001076 if (info.cellX != vacant[0] || info.cellY != vacant[1]) {
1077 info.cellX = vacant[0];
1078 info.cellY = vacant[1];
1079 LauncherModel.addOrMoveItemInDatabase(mLauncher, info, mInfo.id, 0,
1080 info.cellX, info.cellY);
1081 }
Adam Cohen2801caf2011-05-13 20:57:39 -07001082 boolean insert = false;
1083 mContent.addViewToCellLayout(v, insert ? 0 : -1, (int)info.id, lp, true);
1084 }
Adam Cohen7c693212011-05-18 15:26:57 -07001085 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -07001086 }
1087
Adam Cohena9cf38f2011-05-02 15:36:58 -07001088 public int getItemCount() {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001089 return mContent.getShortcutsAndWidgets().getChildCount();
Adam Cohena9cf38f2011-05-02 15:36:58 -07001090 }
1091
1092 public View getItemAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001093 return mContent.getShortcutsAndWidgets().getChildAt(index);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001094 }
1095
Adam Cohen2801caf2011-05-13 20:57:39 -07001096 private void onCloseComplete() {
Adam Cohen05e0f402011-08-01 12:12:49 -07001097 DragLayer parent = (DragLayer) getParent();
Michael Jurka5649c282012-06-18 10:33:21 -07001098 if (parent != null) {
1099 parent.removeView(this);
1100 }
Adam Cohen4554ee12011-08-03 16:13:21 -07001101 mDragController.removeDropTarget((DropTarget) this);
Adam Cohen05e0f402011-08-01 12:12:49 -07001102 clearFocus();
Adam Cohenac56cff2011-09-28 20:45:37 -07001103 mFolderIcon.requestFocus();
Adam Cohen05e0f402011-08-01 12:12:49 -07001104
Adam Cohen2801caf2011-05-13 20:57:39 -07001105 if (mRearrangeOnClose) {
1106 setupContentForNumItems(getItemCount());
1107 mRearrangeOnClose = false;
1108 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001109 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001110 if (!mDragInProgress && !mSuppressFolderDeletion) {
1111 replaceFolderWithFinalItem();
1112 } else if (mDragInProgress) {
1113 mDeleteFolderOnDropCompleted = true;
1114 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001115 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001116 mSuppressFolderDeletion = false;
Adam Cohenafb01ee2011-06-23 15:38:03 -07001117 }
1118
1119 private void replaceFolderWithFinalItem() {
Adam Cohenafb01ee2011-06-23 15:38:03 -07001120 // Add the last remaining child to the workspace in place of the folder
Adam Cohenfb91f302012-06-11 15:45:18 -07001121 Runnable onCompleteRunnable = new Runnable() {
1122 @Override
1123 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -07001124 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screenId);
Adam Cohenafb01ee2011-06-23 15:38:03 -07001125
Winson Chung33231f52013-12-09 16:57:45 -08001126 View child = null;
Adam Cohenfb91f302012-06-11 15:45:18 -07001127 // Move the item from the folder to the workspace, in the position of the folder
1128 if (getItemCount() == 1) {
1129 ShortcutInfo finalItem = mInfo.contents.get(0);
Adam Cohenc5e63f32012-07-12 16:16:57 -07001130 child = mLauncher.createShortcut(R.layout.application, cellLayout,
Adam Cohenfb91f302012-06-11 15:45:18 -07001131 finalItem);
1132 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -07001133 mInfo.screenId, mInfo.cellX, mInfo.cellY);
Adam Cohenfb91f302012-06-11 15:45:18 -07001134 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001135 if (getItemCount() <= 1) {
1136 // Remove the folder
1137 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
Dan Sandler0eb687f2014-01-10 13:24:55 -05001138 if (cellLayout != null) {
1139 // b/12446428 -- sometimes the cell layout has already gone away?
1140 cellLayout.removeView(mFolderIcon);
1141 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001142 if (mFolderIcon instanceof DropTarget) {
1143 mDragController.removeDropTarget((DropTarget) mFolderIcon);
1144 }
1145 mLauncher.removeFolder(mInfo);
1146 }
Adam Cohenc5e63f32012-07-12 16:16:57 -07001147 // We add the child after removing the folder to prevent both from existing at
Winson Chung0e6a7132013-08-23 12:55:10 -07001148 // the same time in the CellLayout. We need to add the new item with addInScreenFromBind()
1149 // to ensure that hotseat items are placed correctly.
Adam Cohenc5e63f32012-07-12 16:16:57 -07001150 if (child != null) {
Winson Chung0e6a7132013-08-23 12:55:10 -07001151 mLauncher.getWorkspace().addInScreenFromBind(child, mInfo.container, mInfo.screenId,
Adam Cohenc5e63f32012-07-12 16:16:57 -07001152 mInfo.cellX, mInfo.cellY, mInfo.spanX, mInfo.spanY);
1153 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001154 }
1155 };
1156 View finalChild = getItemAt(0);
1157 if (finalChild != null) {
1158 mFolderIcon.performDestroyAnimation(finalChild, onCompleteRunnable);
Winson Chung33231f52013-12-09 16:57:45 -08001159 } else {
1160 onCompleteRunnable.run();
Adam Cohenafb01ee2011-06-23 15:38:03 -07001161 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001162 mDestroyed = true;
1163 }
1164
1165 boolean isDestroyed() {
1166 return mDestroyed;
Adam Cohen2801caf2011-05-13 20:57:39 -07001167 }
1168
Adam Cohenac56cff2011-09-28 20:45:37 -07001169 // This method keeps track of the last item in the folder for the purposes
1170 // of keyboard focus
1171 private void updateTextViewFocus() {
1172 View lastChild = getItemAt(getItemCount() - 1);
1173 getItemAt(getItemCount() - 1);
1174 if (lastChild != null) {
1175 mFolderName.setNextFocusDownId(lastChild.getId());
1176 mFolderName.setNextFocusRightId(lastChild.getId());
1177 mFolderName.setNextFocusLeftId(lastChild.getId());
1178 mFolderName.setNextFocusUpId(lastChild.getId());
1179 }
1180 }
1181
Adam Cohenbfbfd262011-06-13 16:55:12 -07001182 public void onDrop(DragObject d) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001183 Runnable cleanUpRunnable = null;
1184
Adam Cohen689ff162014-05-08 17:27:56 -07001185 // If we are coming from All Apps space, we defer removing the extra empty screen
1186 // until the folder closes
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001187 if (d.dragSource != mLauncher.getWorkspace() && !(d.dragSource instanceof Folder)) {
1188 cleanUpRunnable = new Runnable() {
1189 @Override
1190 public void run() {
Adam Cohen689ff162014-05-08 17:27:56 -07001191 mLauncher.exitSpringLoadedDragModeDelayed(true,
1192 Launcher.EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
1193 null);
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001194 }
1195 };
Adam Cohenbfbfd262011-06-13 16:55:12 -07001196 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001197
1198 View currentDragView;
1199 ShortcutInfo si = mCurrentDragInfo;
1200 if (mIsExternalDrag) {
1201 si.cellX = mEmptyCell[0];
1202 si.cellY = mEmptyCell[1];
Sunny Goyal95abbb32014-08-04 10:53:22 -07001203
1204 // Actually move the item in the database if it was an external drag. Call this
1205 // before creating the view, so that ShortcutInfo is updated appropriately.
1206 LauncherModel.addOrMoveItemInDatabase(
1207 mLauncher, si, mInfo.id, 0, si.cellX, si.cellY);
1208
1209 // We only need to update the locations if it doesn't get handled in #onDropCompleted.
1210 if (d.dragSource != this) {
1211 updateItemLocationsInDatabaseBatch();
1212 }
1213 mIsExternalDrag = false;
1214
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001215 currentDragView = createAndAddShortcut(si);
1216 } else {
1217 currentDragView = mCurrentDragView;
1218 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) currentDragView.getLayoutParams();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001219 si.cellX = lp.cellX = mEmptyCell[0];
1220 si.cellX = lp.cellY = mEmptyCell[1];
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001221 mContent.addViewToCellLayout(currentDragView, -1, (int) si.id, lp, true);
Adam Cohenbfbfd262011-06-13 16:55:12 -07001222 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001223
1224 if (d.dragView.hasDrawn()) {
1225
1226 // Temporarily reset the scale such that the animation target gets calculated correctly.
1227 float scaleX = getScaleX();
1228 float scaleY = getScaleY();
1229 setScaleX(1.0f);
1230 setScaleY(1.0f);
1231 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, currentDragView,
1232 cleanUpRunnable, null);
1233 setScaleX(scaleX);
1234 setScaleY(scaleY);
1235 } else {
1236 d.deferDragViewCleanupPostAnimation = false;
1237 currentDragView.setVisibility(VISIBLE);
1238 }
1239 mItemsInvalidated = true;
1240 setupContentDimensions(getItemCount());
1241
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001242 // Temporarily suppress the listener, as we did all the work already here.
1243 mSuppressOnAdd = true;
1244 mInfo.add(si);
1245 mSuppressOnAdd = false;
Sunny Goyal4b020172014-08-28 14:51:14 -07001246 // Clear the drag info, as it is no longer being dragged.
1247 mCurrentDragInfo = null;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001248 }
1249
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001250 // This is used so the item doesn't immediately appear in the folder when added. In one case
1251 // we need to create the illusion that the item isn't added back to the folder yet, to
1252 // to correspond to the animation of the icon back into the folder. This is
1253 public void hideItem(ShortcutInfo info) {
1254 View v = getViewForInfo(info);
1255 v.setVisibility(INVISIBLE);
1256 }
1257 public void showItem(ShortcutInfo info) {
1258 View v = getViewForInfo(info);
1259 v.setVisibility(VISIBLE);
1260 }
1261
Adam Cohenbfbfd262011-06-13 16:55:12 -07001262 public void onAdd(ShortcutInfo item) {
1263 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001264 // If the item was dropped onto this open folder, we have done the work associated
1265 // with adding the item to the folder, as indicated by mSuppressOnAdd being set
Adam Cohenbfbfd262011-06-13 16:55:12 -07001266 if (mSuppressOnAdd) return;
1267 if (!findAndSetEmptyCells(item)) {
1268 // The current layout is full, can we expand it?
1269 setupContentForNumItems(getItemCount() + 1);
1270 findAndSetEmptyCells(item);
1271 }
1272 createAndAddShortcut(item);
1273 LauncherModel.addOrMoveItemInDatabase(
1274 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
1275 }
1276
Adam Cohena9cf38f2011-05-02 15:36:58 -07001277 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -07001278 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001279 // If this item is being dragged from this open folder, we have already handled
1280 // the work associated with removing the item, so we don't have to do anything here.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001281 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001282 View v = getViewForInfo(item);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001283 mContent.removeView(v);
Adam Cohen2801caf2011-05-13 20:57:39 -07001284 if (mState == STATE_ANIMATING) {
1285 mRearrangeOnClose = true;
1286 } else {
1287 setupContentForNumItems(getItemCount());
1288 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001289 if (getItemCount() <= 1) {
1290 replaceFolderWithFinalItem();
1291 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001292 }
Adam Cohen7c693212011-05-18 15:26:57 -07001293
Adam Cohendf1e4e82011-06-24 15:57:39 -07001294 private View getViewForInfo(ShortcutInfo item) {
1295 for (int j = 0; j < mContent.getCountY(); j++) {
1296 for (int i = 0; i < mContent.getCountX(); i++) {
1297 View v = mContent.getChildAt(i, j);
1298 if (v.getTag() == item) {
1299 return v;
1300 }
1301 }
1302 }
1303 return null;
1304 }
1305
Adam Cohen76078c42011-06-09 15:06:52 -07001306 public void onItemsChanged() {
Adam Cohenac56cff2011-09-28 20:45:37 -07001307 updateTextViewFocus();
Adam Cohen76078c42011-06-09 15:06:52 -07001308 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001309
Adam Cohen76fc0852011-06-17 13:26:23 -07001310 public void onTitleChanged(CharSequence title) {
1311 }
Adam Cohen76078c42011-06-09 15:06:52 -07001312
Adam Cohen7c693212011-05-18 15:26:57 -07001313 public ArrayList<View> getItemsInReadingOrder() {
1314 if (mItemsInvalidated) {
1315 mItemsInReadingOrder.clear();
1316 for (int j = 0; j < mContent.getCountY(); j++) {
1317 for (int i = 0; i < mContent.getCountX(); i++) {
1318 View v = mContent.getChildAt(i, j);
1319 if (v != null) {
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001320 mItemsInReadingOrder.add(v);
Adam Cohen7c693212011-05-18 15:26:57 -07001321 }
1322 }
1323 }
1324 mItemsInvalidated = false;
1325 }
1326 return mItemsInReadingOrder;
1327 }
Adam Cohen8dfcba42011-07-07 16:38:18 -07001328
1329 public void getLocationInDragLayer(int[] loc) {
1330 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
1331 }
Adam Cohenea0818d2011-09-30 20:06:58 -07001332
1333 public void onFocusChange(View v, boolean hasFocus) {
1334 if (v == mFolderName && hasFocus) {
1335 startEditingFolderName();
1336 }
1337 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001338
1339 @Override
1340 public void getHitRectRelativeToDragLayer(Rect outRect) {
1341 getHitRect(outRect);
1342 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001343}