blob: 11e9835e869885ccfe01d9c741174d4f06848c0a [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;
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;
Adam Cohenc0dcf592011-06-01 15:30:43 -070055
The Android Open Source Project31dd5032009-03-03 19:32:27 -080056/**
57 * Represents a set of icons chosen by the user or generated by the system.
58 */
Adam Cohen8dfcba42011-07-07 16:38:18 -070059public class Folder extends LinearLayout implements DragSource, View.OnClickListener,
Adam Cohenea0818d2011-09-30 20:06:58 -070060 View.OnLongClickListener, DropTarget, FolderListener, TextView.OnEditorActionListener,
61 View.OnFocusChangeListener {
Adam Cohendf2cc412011-04-27 16:56:57 -070062 private static final String TAG = "Launcher.Folder";
63
Adam Cohen4eac29a2011-07-11 17:53:37 -070064 protected DragController mDragController;
65 protected Launcher mLauncher;
66 protected FolderInfo mInfo;
67
Adam Cohendf2cc412011-04-27 16:56:57 -070068 static final int STATE_NONE = -1;
69 static final int STATE_SMALL = 0;
70 static final int STATE_ANIMATING = 1;
71 static final int STATE_OPEN = 2;
72
73 private int mExpandDuration;
Adam Cohenc4fe9ea2014-08-18 18:54:10 -070074 private int mMaterialExpandDuration;
75 private int mMaterialExpandStagger;
Adam Cohendf2cc412011-04-27 16:56:57 -070076 protected CellLayout mContent;
Adam Cohenf0f4eda2013-06-06 21:27:03 -070077 private ScrollView mScrollView;
Adam Cohendf2cc412011-04-27 16:56:57 -070078 private final LayoutInflater mInflater;
79 private final IconCache mIconCache;
80 private int mState = STATE_NONE;
Adam Cohenbfbfd262011-06-13 16:55:12 -070081 private static final int REORDER_ANIMATION_DURATION = 230;
Adam Cohenf0f4eda2013-06-06 21:27:03 -070082 private static final int REORDER_DELAY = 250;
Adam Cohen5d518fa2013-12-05 14:16:23 -080083 private static final int ON_EXIT_CLOSE_DELAY = 400;
Adam Cohen2801caf2011-05-13 20:57:39 -070084 private boolean mRearrangeOnClose = false;
85 private FolderIcon mFolderIcon;
86 private int mMaxCountX;
87 private int mMaxCountY;
Adam Cohen78dc83e2011-11-15 17:10:00 -080088 private int mMaxNumItems;
Adam Cohen7c693212011-05-18 15:26:57 -070089 private ArrayList<View> mItemsInReadingOrder = new ArrayList<View>();
90 boolean mItemsInvalidated = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -070091 private ShortcutInfo mCurrentDragInfo;
92 private View mCurrentDragView;
Jorim Jaggi55bd9722014-01-16 15:30:42 -080093 private boolean mIsExternalDrag;
Adam Cohenbfbfd262011-06-13 16:55:12 -070094 boolean mSuppressOnAdd = false;
95 private int[] mTargetCell = new int[2];
96 private int[] mPreviousTargetCell = new int[2];
97 private int[] mEmptyCell = new int[2];
98 private Alarm mReorderAlarm = new Alarm();
99 private Alarm mOnExitAlarm = new Alarm();
Adam Cohen76fc0852011-06-17 13:26:23 -0700100 private int mFolderNameHeight;
Adam Cohen8e776a62011-06-28 18:10:06 -0700101 private Rect mTempRect = new Rect();
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700102 private boolean mDragInProgress = false;
103 private boolean mDeleteFolderOnDropCompleted = false;
104 private boolean mSuppressFolderDeletion = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700105 private boolean mItemAddedBackToSelfViaIcon = false;
Adam Cohenac56cff2011-09-28 20:45:37 -0700106 FolderEditText mFolderName;
Adam Cohen268c4752012-06-06 17:47:33 -0700107 private float mFolderIconPivotX;
108 private float mFolderIconPivotY;
Adam Cohen228da5a2011-07-27 22:23:47 -0700109
Adam Cohen76fc0852011-06-17 13:26:23 -0700110 private boolean mIsEditingName = false;
111 private InputMethodManager mInputMethodManager;
Adam Cohendf2cc412011-04-27 16:56:57 -0700112
Adam Cohena65beee2011-06-27 21:32:23 -0700113 private static String sDefaultFolderName;
114 private static String sHintText;
115
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700116 private FocusIndicatorView mFocusIndicatorHandler;
117
Adam Cohen1960ea42013-11-12 11:33:14 +0000118 // We avoid measuring the scroll view with a 0 width or height, as this
119 // results in CellLayout being measured as UNSPECIFIED, which it does
120 // not support.
121 private static final int MIN_CONTENT_DIMEN = 5;
122
Adam Cohenfb91f302012-06-11 15:45:18 -0700123 private boolean mDestroyed;
124
Michael Jurka1e2f4652013-07-08 18:03:46 -0700125 private Runnable mDeferredAction;
126 private boolean mDeferDropAfterUninstall;
127 private boolean mUninstallSuccessful;
128
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129 /**
130 * Used to inflate the Workspace from XML.
131 *
132 * @param context The application's context.
133 * @param attrs The attribtues set containing the Workspace's customization values.
134 */
135 public Folder(Context context, AttributeSet attrs) {
136 super(context, attrs);
Winson Chung5f8afe62013-08-12 16:19:28 -0700137
138 LauncherAppState app = LauncherAppState.getInstance();
139 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140 setAlwaysDrawnWithCacheEnabled(false);
Adam Cohendf2cc412011-04-27 16:56:57 -0700141 mInflater = LayoutInflater.from(context);
Winson Chung5f8afe62013-08-12 16:19:28 -0700142 mIconCache = app.getIconCache();
Adam Cohen78dc83e2011-11-15 17:10:00 -0800143
144 Resources res = getResources();
Michael Jurka414300a2013-08-27 15:42:35 +0200145 mMaxCountX = (int) grid.numColumns;
Sunny Goyalc9acdd52015-02-26 12:34:42 -0800146 mMaxCountY = (int) grid.numRows;
147 mMaxNumItems = mMaxCountX * mMaxCountY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700148
149 mInputMethodManager = (InputMethodManager)
Michael Jurka8b805b12012-04-18 14:23:14 -0700150 getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
Adam Cohen76fc0852011-06-17 13:26:23 -0700151
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700152 mExpandDuration = res.getInteger(R.integer.config_folderExpandDuration);
153 mMaterialExpandDuration = res.getInteger(R.integer.config_materialFolderExpandDuration);
154 mMaterialExpandStagger = res.getInteger(R.integer.config_materialFolderExpandStagger);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700155
156 if (sDefaultFolderName == null) {
157 sDefaultFolderName = res.getString(R.string.folder_name);
158 }
Adam Cohena65beee2011-06-27 21:32:23 -0700159 if (sHintText == null) {
160 sHintText = res.getString(R.string.folder_hint_text);
161 }
Adam Cohen4eac29a2011-07-11 17:53:37 -0700162 mLauncher = (Launcher) context;
Adam Cohenac56cff2011-09-28 20:45:37 -0700163 // We need this view to be focusable in touch mode so that when text editing of the folder
164 // name is complete, we have something to focus on, thus hiding the cursor and giving
165 // reliable behvior when clicking the text field (since it will always gain focus on click).
166 setFocusableInTouchMode(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167 }
168
169 @Override
170 protected void onFinishInflate() {
171 super.onFinishInflate();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700172 mScrollView = (ScrollView) findViewById(R.id.scroll_view);
Adam Cohendf2cc412011-04-27 16:56:57 -0700173 mContent = (CellLayout) findViewById(R.id.folder_content);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700174
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700175 mFocusIndicatorHandler = new FocusIndicatorView(getContext());
176 mContent.addView(mFocusIndicatorHandler, 0);
177 mFocusIndicatorHandler.getLayoutParams().height = FocusIndicatorView.DEFAULT_LAYOUT_SIZE;
178 mFocusIndicatorHandler.getLayoutParams().width = FocusIndicatorView.DEFAULT_LAYOUT_SIZE;
179
Winson Chung5f8afe62013-08-12 16:19:28 -0700180 LauncherAppState app = LauncherAppState.getInstance();
181 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
182
Winson Chung5f8afe62013-08-12 16:19:28 -0700183 mContent.setCellDimensions(grid.folderCellWidthPx, grid.folderCellHeightPx);
Adam Cohen2801caf2011-05-13 20:57:39 -0700184 mContent.setGridSize(0, 0);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700185 mContent.getShortcutsAndWidgets().setMotionEventSplittingEnabled(false);
Adam Cohen2374abf2013-04-16 14:56:57 -0700186 mContent.setInvertIfRtl(true);
Adam Cohenac56cff2011-09-28 20:45:37 -0700187 mFolderName = (FolderEditText) findViewById(R.id.folder_name);
188 mFolderName.setFolder(this);
Adam Cohenea0818d2011-09-30 20:06:58 -0700189 mFolderName.setOnFocusChangeListener(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700190
191 // We find out how tall the text view wants to be (it is set to wrap_content), so that
192 // we can allocate the appropriate amount of space for it.
193 int measureSpec = MeasureSpec.UNSPECIFIED;
194 mFolderName.measure(measureSpec, measureSpec);
195 mFolderNameHeight = mFolderName.getMeasuredHeight();
196
197 // We disable action mode for now since it messes up the view on phones
198 mFolderName.setCustomSelectionActionModeCallback(mActionModeCallback);
Adam Cohen76fc0852011-06-17 13:26:23 -0700199 mFolderName.setOnEditorActionListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700200 mFolderName.setSelectAllOnFocus(true);
Adam Cohen7a14d0b2011-06-24 11:36:35 -0700201 mFolderName.setInputType(mFolderName.getInputType() |
202 InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800203 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700204
Adam Cohen76fc0852011-06-17 13:26:23 -0700205 private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
206 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
207 return false;
208 }
209
210 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
211 return false;
212 }
213
214 public void onDestroyActionMode(ActionMode mode) {
215 }
216
217 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
218 return false;
219 }
220 };
221
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800222 public void onClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700223 Object tag = v.getTag();
224 if (tag instanceof ShortcutInfo) {
Adam Cohenb5fe60c2013-06-06 22:03:51 -0700225 mLauncher.onClick(v);
Adam Cohendf2cc412011-04-27 16:56:57 -0700226 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800227 }
228
229 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -0700230 // Return if global dragging is not enabled
231 if (!mLauncher.isDraggingEnabled()) return true;
232
Adam Cohendf2cc412011-04-27 16:56:57 -0700233 Object tag = v.getTag();
234 if (tag instanceof ShortcutInfo) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700235 ShortcutInfo item = (ShortcutInfo) tag;
236 if (!v.isInTouchMode()) {
237 return false;
238 }
239
Adam Cohenac8c8762011-07-13 11:15:27 -0700240 mLauncher.getWorkspace().beginDragShared(v, this);
Adam Cohen76078c42011-06-09 15:06:52 -0700241
242 mCurrentDragInfo = item;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700243 mEmptyCell[0] = item.cellX;
244 mEmptyCell[1] = item.cellY;
245 mCurrentDragView = v;
Adam Cohenfc53cd22011-07-20 15:45:11 -0700246
247 mContent.removeView(mCurrentDragView);
248 mInfo.remove(mCurrentDragInfo);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700249 mDragInProgress = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700250 mItemAddedBackToSelfViaIcon = false;
Adam Cohendf2cc412011-04-27 16:56:57 -0700251 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800252 return true;
253 }
254
Adam Cohen76fc0852011-06-17 13:26:23 -0700255 public boolean isEditingName() {
256 return mIsEditingName;
257 }
258
259 public void startEditingFolderName() {
Adam Cohena65beee2011-06-27 21:32:23 -0700260 mFolderName.setHint("");
Adam Cohen76fc0852011-06-17 13:26:23 -0700261 mIsEditingName = true;
262 }
263
264 public void dismissEditingName() {
265 mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
266 doneEditingFolderName(true);
267 }
268
269 public void doneEditingFolderName(boolean commit) {
Adam Cohena65beee2011-06-27 21:32:23 -0700270 mFolderName.setHint(sHintText);
Adam Cohen1df26a32011-08-12 16:15:23 -0700271 // Convert to a string here to ensure that no other state associated with the text field
272 // gets saved.
Adam Cohen3371da02011-10-25 21:38:29 -0700273 String newTitle = mFolderName.getText().toString();
274 mInfo.setTitle(newTitle);
Adam Cohen76fc0852011-06-17 13:26:23 -0700275 LauncherModel.updateItemInDatabase(mLauncher, mInfo);
Adam Cohenac56cff2011-09-28 20:45:37 -0700276
Adam Cohen3371da02011-10-25 21:38:29 -0700277 if (commit) {
278 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700279 String.format(getContext().getString(R.string.folder_renamed), newTitle));
Adam Cohen3371da02011-10-25 21:38:29 -0700280 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700281 // In order to clear the focus from the text field, we set the focus on ourself. This
282 // ensures that every time the field is clicked, focus is gained, giving reliable behavior.
283 requestFocus();
284
Adam Cohene601a432011-07-26 21:51:30 -0700285 Selection.setSelection((Spannable) mFolderName.getText(), 0, 0);
Adam Cohen76fc0852011-06-17 13:26:23 -0700286 mIsEditingName = false;
287 }
288
289 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
290 if (actionId == EditorInfo.IME_ACTION_DONE) {
291 dismissEditingName();
292 return true;
293 }
294 return false;
295 }
296
297 public View getEditTextRegion() {
298 return mFolderName;
299 }
300
Anjali Koppalf05545f2014-03-10 19:18:43 -0700301 public CellLayout getContent() {
302 return mContent;
303 }
304
Adam Cohen0c872ba2011-05-05 10:34:16 -0700305 /**
306 * We need to handle touch events to prevent them from falling through to the workspace below.
307 */
308 @Override
309 public boolean onTouchEvent(MotionEvent ev) {
310 return true;
311 }
312
Joe Onorato00acb122009-08-04 16:04:30 -0400313 public void setDragController(DragController dragController) {
314 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800315 }
316
Adam Cohen2801caf2011-05-13 20:57:39 -0700317 void setFolderIcon(FolderIcon icon) {
318 mFolderIcon = icon;
319 }
320
Adam Cohen3371da02011-10-25 21:38:29 -0700321 @Override
322 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
323 // When the folder gets focus, we don't want to announce the list of items.
324 return true;
325 }
326
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800327 /**
328 * @return the FolderInfo object associated with this folder
329 */
330 FolderInfo getInfo() {
331 return mInfo;
332 }
333
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800334 void bind(FolderInfo info) {
335 mInfo = info;
Adam Cohendf2cc412011-04-27 16:56:57 -0700336 ArrayList<ShortcutInfo> children = info.contents;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700337 ArrayList<ShortcutInfo> overflow = new ArrayList<ShortcutInfo>();
Sunny Goyal08f72612015-01-05 13:41:43 -0800338
339 final int totalChildren = children.size();
340 setupContentForNumItems(totalChildren);
341
342 // Arrange children in the grid based on the rank.
343 Collections.sort(children, Utilities.RANK_COMPARATOR);
344 final int countX = mContent.getCountX();
345
346 int visibleChildren = 0;
Adam Cohendf2cc412011-04-27 16:56:57 -0700347 for (int i = 0; i < children.size(); i++) {
Sunny Goyal08f72612015-01-05 13:41:43 -0800348 ShortcutInfo child = children.get(i);
349 child.cellX = i % countX;
350 child.cellY = i / countX;
351
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800352 if (createAndAddShortcut(child) == null) {
Adam Cohenc508b2d2011-06-28 14:41:44 -0700353 overflow.add(child);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700354 } else {
Sunny Goyal08f72612015-01-05 13:41:43 -0800355 visibleChildren++;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700356 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700357 }
Adam Cohenc508b2d2011-06-28 14:41:44 -0700358
Adam Cohen0057bbc2011-08-12 18:30:51 -0700359 // We rearrange the items in case there are any empty gaps
Sunny Goyal08f72612015-01-05 13:41:43 -0800360 setupContentForNumItems(visibleChildren);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700361
Jason Monk4ff73882014-04-24 16:48:07 -0400362 // If our folder has too many items we prune them from the list. This is an issue
Adam Cohenc508b2d2011-06-28 14:41:44 -0700363 // when upgrading from the old Folders implementation which could contain an unlimited
364 // number of items.
365 for (ShortcutInfo item: overflow) {
366 mInfo.remove(item);
367 LauncherModel.deleteItemFromDatabase(mLauncher, item);
368 }
369
Adam Cohen4dbe6d92011-05-18 17:14:15 -0700370 mItemsInvalidated = true;
Adam Cohenac56cff2011-09-28 20:45:37 -0700371 updateTextViewFocus();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700372 mInfo.addListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700373
Adam Cohenafb01ee2011-06-23 15:38:03 -0700374 if (!sDefaultFolderName.contentEquals(mInfo.title)) {
Adam Cohen4ef610f2011-06-21 22:37:36 -0700375 mFolderName.setText(mInfo.title);
376 } else {
377 mFolderName.setText("");
378 }
Winson Chung33231f52013-12-09 16:57:45 -0800379
380 // In case any children didn't come across during loading, clean up the folder accordingly
381 mFolderIcon.post(new Runnable() {
382 public void run() {
383 if (getItemCount() <= 1) {
384 replaceFolderWithFinalItem();
385 }
386 }
387 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700388 }
389
390 /**
391 * Creates a new UserFolder, inflated from R.layout.user_folder.
392 *
393 * @param context The application's context.
394 *
395 * @return A new UserFolder.
396 */
397 static Folder fromXml(Context context) {
398 return (Folder) LayoutInflater.from(context).inflate(R.layout.user_folder, null);
399 }
400
401 /**
402 * This method is intended to make the UserFolder to be visually identical in size and position
403 * to its associated FolderIcon. This allows for a seamless transition into the expanded state.
404 */
405 private void positionAndSizeAsIcon() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700406 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800407 setScaleX(0.8f);
408 setScaleY(0.8f);
409 setAlpha(0f);
Adam Cohendf2cc412011-04-27 16:56:57 -0700410 mState = STATE_SMALL;
411 }
412
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700413 private void prepareReveal() {
414 setScaleX(1f);
415 setScaleY(1f);
416 setAlpha(1f);
417 mState = STATE_SMALL;
418 }
419
Adam Cohendf2cc412011-04-27 16:56:57 -0700420 public void animateOpen() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700421 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen6441de02011-12-14 14:25:32 -0800422
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700423 Animator openFolderAnim = null;
424 final Runnable onCompleteRunnable;
Kenny Guyd794a3f2014-09-16 15:17:58 +0100425 if (!Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700426 positionAndSizeAsIcon();
427 centerAboutIcon();
428
429 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1);
430 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
431 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
432 final ObjectAnimator oa =
433 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
434 oa.setDuration(mExpandDuration);
435 openFolderAnim = oa;
436
437 setLayerType(LAYER_TYPE_HARDWARE, null);
438 onCompleteRunnable = new Runnable() {
439 @Override
440 public void run() {
441 setLayerType(LAYER_TYPE_NONE, null);
442 }
443 };
444 } else {
445 prepareReveal();
446 centerAboutIcon();
447
448 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
449 int height = getFolderHeight();
450
451 float transX = - 0.075f * (width / 2 - getPivotX());
452 float transY = - 0.075f * (height / 2 - getPivotY());
453 setTranslationX(transX);
454 setTranslationY(transY);
455 PropertyValuesHolder tx = PropertyValuesHolder.ofFloat("translationX", transX, 0);
456 PropertyValuesHolder ty = PropertyValuesHolder.ofFloat("translationY", transY, 0);
457
458 int rx = (int) Math.max(Math.max(width - getPivotX(), 0), getPivotX());
459 int ry = (int) Math.max(Math.max(height - getPivotY(), 0), getPivotY());
460 float radius = (float) Math.sqrt(rx * rx + ry * ry);
461 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
462 Animator reveal = LauncherAnimUtils.createCircularReveal(this, (int) getPivotX(),
463 (int) getPivotY(), 0, radius);
464 reveal.setDuration(mMaterialExpandDuration);
465 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
466
467 mContent.setAlpha(0f);
468 Animator iconsAlpha = LauncherAnimUtils.ofFloat(mContent, "alpha", 0f, 1f);
469 iconsAlpha.setDuration(mMaterialExpandDuration);
470 iconsAlpha.setStartDelay(mMaterialExpandStagger);
471 iconsAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
472
473 mFolderName.setAlpha(0f);
474 Animator textAlpha = LauncherAnimUtils.ofFloat(mFolderName, "alpha", 0f, 1f);
475 textAlpha.setDuration(mMaterialExpandDuration);
476 textAlpha.setStartDelay(mMaterialExpandStagger);
477 textAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
478
479 Animator drift = LauncherAnimUtils.ofPropertyValuesHolder(this, tx, ty);
480 drift.setDuration(mMaterialExpandDuration);
481 drift.setStartDelay(mMaterialExpandStagger);
482 drift.setInterpolator(new LogDecelerateInterpolator(60, 0));
483
484 anim.play(drift);
485 anim.play(iconsAlpha);
486 anim.play(textAlpha);
487 anim.play(reveal);
488
489 openFolderAnim = anim;
490
491 mContent.setLayerType(LAYER_TYPE_HARDWARE, null);
492 onCompleteRunnable = new Runnable() {
493 @Override
494 public void run() {
495 mContent.setLayerType(LAYER_TYPE_NONE, null);
496 }
497 };
498 }
499 openFolderAnim.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700500 @Override
501 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700502 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700503 String.format(getContext().getString(R.string.folder_opened),
Adam Cohen3371da02011-10-25 21:38:29 -0700504 mContent.getCountX(), mContent.getCountY()));
Adam Cohendf2cc412011-04-27 16:56:57 -0700505 mState = STATE_ANIMATING;
506 }
507 @Override
508 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700509 mState = STATE_OPEN;
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700510
511 if (onCompleteRunnable != null) {
512 onCompleteRunnable.run();
513 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800514
Adam Cohenac56cff2011-09-28 20:45:37 -0700515 setFocusOnFirstChild();
Adam Cohendf2cc412011-04-27 16:56:57 -0700516 }
517 });
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700518 openFolderAnim.start();
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800519
520 // Make sure the folder picks up the last drag move even if the finger doesn't move.
521 if (mDragController.isDragging()) {
522 mDragController.forceTouchMove();
523 }
524 }
525
526 public void beginExternalDrag(ShortcutInfo item) {
527 setupContentForNumItems(getItemCount() + 1);
528 findAndSetEmptyCells(item);
529
530 mCurrentDragInfo = item;
531 mEmptyCell[0] = item.cellX;
532 mEmptyCell[1] = item.cellY;
533 mIsExternalDrag = true;
534
535 mDragInProgress = true;
Adam Cohendf2cc412011-04-27 16:56:57 -0700536 }
537
Adam Cohen3371da02011-10-25 21:38:29 -0700538 private void sendCustomAccessibilityEvent(int type, String text) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700539 AccessibilityManager accessibilityManager = (AccessibilityManager)
540 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
541 if (accessibilityManager.isEnabled()) {
Adam Cohen3371da02011-10-25 21:38:29 -0700542 AccessibilityEvent event = AccessibilityEvent.obtain(type);
543 onInitializeAccessibilityEvent(event);
544 event.getText().add(text);
Michael Jurka8b805b12012-04-18 14:23:14 -0700545 accessibilityManager.sendAccessibilityEvent(event);
Adam Cohen3371da02011-10-25 21:38:29 -0700546 }
547 }
548
Adam Cohenac56cff2011-09-28 20:45:37 -0700549 private void setFocusOnFirstChild() {
550 View firstChild = mContent.getChildAt(0, 0);
551 if (firstChild != null) {
552 firstChild.requestFocus();
553 }
554 }
555
Adam Cohendf2cc412011-04-27 16:56:57 -0700556 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700557 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800558 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
559 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
560 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
Michael Jurka032e6ba2013-04-22 15:08:42 +0200561 final ObjectAnimator oa =
Michael Jurka2ecf9952012-06-18 12:52:28 -0700562 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohendf2cc412011-04-27 16:56:57 -0700563
Adam Cohen2801caf2011-05-13 20:57:39 -0700564 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700565 @Override
566 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700567 onCloseComplete();
Michael Jurka0121c3e2012-05-31 08:36:04 -0700568 setLayerType(LAYER_TYPE_NONE, null);
Adam Cohen2801caf2011-05-13 20:57:39 -0700569 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700570 }
571 @Override
572 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700573 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700574 getContext().getString(R.string.folder_closed));
Adam Cohendf2cc412011-04-27 16:56:57 -0700575 mState = STATE_ANIMATING;
576 }
577 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700578 oa.setDuration(mExpandDuration);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700579 setLayerType(LAYER_TYPE_HARDWARE, null);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100580 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700581 }
582
Adam Cohencb3382b2011-05-24 14:07:08 -0700583 public boolean acceptDrop(DragObject d) {
584 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700585 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700586 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
587 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
588 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700589 }
590
Adam Cohendf2cc412011-04-27 16:56:57 -0700591 protected boolean findAndSetEmptyCells(ShortcutInfo item) {
592 int[] emptyCell = new int[2];
593 if (mContent.findCellForSpan(emptyCell, item.spanX, item.spanY)) {
594 item.cellX = emptyCell[0];
595 item.cellY = emptyCell[1];
Adam Cohendf2cc412011-04-27 16:56:57 -0700596 return true;
597 } else {
598 return false;
599 }
600 }
601
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800602 protected View createAndAddShortcut(ShortcutInfo item) {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700603 final BubbleTextView textView =
604 (BubbleTextView) mInflater.inflate(R.layout.folder_application, this, false);
605 textView.applyFromShortcutInfo(item, mIconCache, false);
606
Adam Cohendf2cc412011-04-27 16:56:57 -0700607 textView.setOnClickListener(this);
608 textView.setOnLongClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700609 textView.setOnFocusChangeListener(mFocusIndicatorHandler);
Adam Cohendf2cc412011-04-27 16:56:57 -0700610
Adam Cohenc508b2d2011-06-28 14:41:44 -0700611 // We need to check here to verify that the given item's location isn't already occupied
Adam Cohen3bf84d32012-05-07 20:17:14 -0700612 // by another item.
Adam Cohen0057bbc2011-08-12 18:30:51 -0700613 if (mContent.getChildAt(item.cellX, item.cellY) != null || item.cellX < 0 || item.cellY < 0
614 || item.cellX >= mContent.getCountX() || item.cellY >= mContent.getCountY()) {
Jason Monk4ff73882014-04-24 16:48:07 -0400615 // This shouldn't happen, log it.
Adam Cohen3bf84d32012-05-07 20:17:14 -0700616 Log.e(TAG, "Folder order not properly persisted during bind");
Adam Cohenc508b2d2011-06-28 14:41:44 -0700617 if (!findAndSetEmptyCells(item)) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800618 return null;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700619 }
620 }
621
Adam Cohendf2cc412011-04-27 16:56:57 -0700622 CellLayout.LayoutParams lp =
623 new CellLayout.LayoutParams(item.cellX, item.cellY, item.spanX, item.spanY);
624 boolean insert = false;
Adam Cohenac56cff2011-09-28 20:45:37 -0700625 textView.setOnKeyListener(new FolderKeyEventListener());
Adam Cohendf2cc412011-04-27 16:56:57 -0700626 mContent.addViewToCellLayout(textView, insert ? 0 : -1, (int)item.id, lp, true);
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800627 return textView;
Adam Cohendf2cc412011-04-27 16:56:57 -0700628 }
629
Adam Cohencb3382b2011-05-24 14:07:08 -0700630 public void onDragEnter(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700631 mPreviousTargetCell[0] = -1;
632 mPreviousTargetCell[1] = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700633 mOnExitAlarm.cancelAlarm();
634 }
635
636 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
637 public void onAlarm(Alarm alarm) {
638 realTimeReorder(mEmptyCell, mTargetCell);
639 }
640 };
641
642 boolean readingOrderGreaterThan(int[] v1, int[] v2) {
643 if (v1[1] > v2[1] || (v1[1] == v2[1] && v1[0] > v2[0])) {
644 return true;
645 } else {
646 return false;
647 }
648 }
649
650 private void realTimeReorder(int[] empty, int[] target) {
651 boolean wrap;
652 int startX;
653 int endX;
654 int startY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700655 int delay = 0;
656 float delayAmount = 30;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700657 if (readingOrderGreaterThan(target, empty)) {
658 wrap = empty[0] >= mContent.getCountX() - 1;
659 startY = wrap ? empty[1] + 1 : empty[1];
660 for (int y = startY; y <= target[1]; y++) {
661 startX = y == empty[1] ? empty[0] + 1 : 0;
662 endX = y < target[1] ? mContent.getCountX() - 1 : target[0];
663 for (int x = startX; x <= endX; x++) {
664 View v = mContent.getChildAt(x,y);
665 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen482ed822012-03-02 14:15:13 -0800666 REORDER_ANIMATION_DURATION, delay, true, true)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700667 empty[0] = x;
668 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700669 delay += delayAmount;
670 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700671 }
672 }
673 }
674 } else {
675 wrap = empty[0] == 0;
676 startY = wrap ? empty[1] - 1 : empty[1];
677 for (int y = startY; y >= target[1]; y--) {
678 startX = y == empty[1] ? empty[0] - 1 : mContent.getCountX() - 1;
679 endX = y > target[1] ? 0 : target[0];
680 for (int x = startX; x >= endX; x--) {
681 View v = mContent.getChildAt(x,y);
682 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen482ed822012-03-02 14:15:13 -0800683 REORDER_ANIMATION_DURATION, delay, true, true)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700684 empty[0] = x;
685 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700686 delay += delayAmount;
687 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700688 }
689 }
690 }
691 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700692 }
693
Sunny Goyalc46bfef2015-01-05 12:40:08 -0800694 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohen2374abf2013-04-16 14:56:57 -0700695 public boolean isLayoutRtl() {
696 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
697 }
698
Adam Cohencb3382b2011-05-24 14:07:08 -0700699 public void onDragOver(DragObject d) {
Alan Viverette4cda5b72013-08-28 17:53:41 -0700700 final int scrollOffset = mScrollView.getScrollY();
Sunny Goyal1587d532015-01-29 09:57:16 -0800701 final float[] r = d.getVisualCenter(null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700702 r[0] -= getPaddingLeft();
703 r[1] -= getPaddingTop();
704
Sunny Goyalc46bfef2015-01-05 12:40:08 -0800705 mTargetCell = mContent.findNearestArea(
706 (int) r[0], (int) r[1] + scrollOffset, 1, 1, mTargetCell);
707 if (isLayoutRtl()) {
708 mTargetCell[0] = mContent.getCountX() - mTargetCell[0] - 1;
709 }
710 if (mTargetCell[0] != mPreviousTargetCell[0]
711 || mTargetCell[1] != mPreviousTargetCell[1]) {
Alan Viverette4cda5b72013-08-28 17:53:41 -0700712 mReorderAlarm.cancelAlarm();
Sunny Goyalc46bfef2015-01-05 12:40:08 -0800713 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
714 mReorderAlarm.setAlarm(REORDER_DELAY);
715 mPreviousTargetCell[0] = mTargetCell[0];
716 mPreviousTargetCell[1] = mTargetCell[1];
Adam Cohenbfbfd262011-06-13 16:55:12 -0700717 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700718 }
719
Adam Cohenbfbfd262011-06-13 16:55:12 -0700720 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
721 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700722 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700723 }
724 };
725
Adam Cohen95bb8002011-07-03 23:40:28 -0700726 public void completeDragExit() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700727 mLauncher.closeFolder();
728 mCurrentDragInfo = null;
729 mCurrentDragView = null;
730 mSuppressOnAdd = false;
731 mRearrangeOnClose = true;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800732 mIsExternalDrag = false;
Adam Cohen3e8f8112011-07-02 18:03:00 -0700733 }
734
Adam Cohencb3382b2011-05-24 14:07:08 -0700735 public void onDragExit(DragObject d) {
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700736 // We only close the folder if this is a true drag exit, ie. not because
737 // a drop has occurred above the folder.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700738 if (!d.dragComplete) {
739 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
740 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
741 }
742 mReorderAlarm.cancelAlarm();
Adam Cohen2801caf2011-05-13 20:57:39 -0700743 }
744
Michael Jurka1e2f4652013-07-08 18:03:46 -0700745 public void onDropCompleted(final View target, final DragObject d,
746 final boolean isFlingToDelete, final boolean success) {
747 if (mDeferDropAfterUninstall) {
Michael Jurkaf3007582013-08-21 14:33:57 +0200748 Log.d(TAG, "Deferred handling drop because waiting for uninstall.");
Michael Jurka1e2f4652013-07-08 18:03:46 -0700749 mDeferredAction = new Runnable() {
750 public void run() {
751 onDropCompleted(target, d, isFlingToDelete, success);
752 mDeferredAction = null;
753 }
754 };
755 return;
756 }
757
758 boolean beingCalledAfterUninstall = mDeferredAction != null;
759 boolean successfulDrop =
760 success && (!beingCalledAfterUninstall || mUninstallSuccessful);
Winson Chung5f8afe62013-08-12 16:19:28 -0700761
Michael Jurka1e2f4652013-07-08 18:03:46 -0700762 if (successfulDrop) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800763 if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon && target != this) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700764 replaceFolderWithFinalItem();
765 }
766 } else {
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700767 setupContentForNumItems(getItemCount());
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700768 // The drag failed, we need to return the item to the folder
769 mFolderIcon.onDrop(d);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700770 }
771
772 if (target != this) {
773 if (mOnExitAlarm.alarmPending()) {
774 mOnExitAlarm.cancelAlarm();
Michael Jurka54554252013-08-01 12:52:23 +0200775 if (!successfulDrop) {
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700776 mSuppressFolderDeletion = true;
777 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700778 completeDragExit();
779 }
Adam Cohen9c58d822013-12-13 17:18:10 -0800780 }
781
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700782 mDeleteFolderOnDropCompleted = false;
783 mDragInProgress = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700784 mItemAddedBackToSelfViaIcon = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700785 mCurrentDragInfo = null;
786 mCurrentDragView = null;
787 mSuppressOnAdd = false;
Adam Cohen4045eb72011-10-06 11:44:26 -0700788
789 // Reordering may have occured, and we need to save the new item locations. We do this once
790 // at the end to prevent unnecessary database operations.
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700791 updateItemLocationsInDatabaseBatch();
Adam Cohen4045eb72011-10-06 11:44:26 -0700792 }
793
Michael Jurka1e2f4652013-07-08 18:03:46 -0700794 public void deferCompleteDropAfterUninstallActivity() {
795 mDeferDropAfterUninstall = true;
796 }
797
798 public void onUninstallActivityReturned(boolean success) {
799 mDeferDropAfterUninstall = false;
800 mUninstallSuccessful = success;
801 if (mDeferredAction != null) {
802 mDeferredAction.run();
803 }
804 }
805
Winson Chunga48487a2012-03-20 16:19:37 -0700806 @Override
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800807 public float getIntrinsicIconScaleFactor() {
808 return 1f;
809 }
810
811 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800812 public boolean supportsFlingToDelete() {
813 return true;
814 }
815
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000816 @Override
817 public boolean supportsAppInfoDropTarget() {
818 return false;
819 }
820
821 @Override
822 public boolean supportsDeleteDropTarget() {
823 return true;
824 }
825
Winson Chunga48487a2012-03-20 16:19:37 -0700826 public void onFlingToDelete(DragObject d, int x, int y, PointF vec) {
827 // Do nothing
828 }
829
830 @Override
831 public void onFlingToDeleteCompleted() {
832 // Do nothing
833 }
834
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700835 private void updateItemLocationsInDatabaseBatch() {
836 ArrayList<View> list = getItemsInReadingOrder();
837 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
838 for (int i = 0; i < list.size(); i++) {
839 View v = list.get(i);
840 ItemInfo info = (ItemInfo) v.getTag();
Sunny Goyal08f72612015-01-05 13:41:43 -0800841 info.rank = i;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700842 items.add(info);
843 }
844
845 LauncherModel.moveItemsInDatabase(mLauncher, items, mInfo.id, 0);
846 }
847
Adam Cohene25af792013-06-06 23:08:25 -0700848 public void addItemLocationsInDatabase() {
849 ArrayList<View> list = getItemsInReadingOrder();
850 for (int i = 0; i < list.size(); i++) {
851 View v = list.get(i);
852 ItemInfo info = (ItemInfo) v.getTag();
853 LauncherModel.addItemToDatabase(mLauncher, info, mInfo.id, 0,
854 info.cellX, info.cellY, false);
855 }
856 }
857
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700858 public void notifyDrop() {
859 if (mDragInProgress) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700860 mItemAddedBackToSelfViaIcon = true;
Adam Cohen76078c42011-06-09 15:06:52 -0700861 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700862 }
863
864 public boolean isDropEnabled() {
865 return true;
866 }
867
Adam Cohen4045eb72011-10-06 11:44:26 -0700868 private void setupContentDimensions(int count) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700869 ArrayList<View> list = getItemsInReadingOrder();
870
871 int countX = mContent.getCountX();
872 int countY = mContent.getCountY();
Adam Cohen7c693212011-05-18 15:26:57 -0700873 boolean done = false;
Adam Cohen2801caf2011-05-13 20:57:39 -0700874
Adam Cohen7c693212011-05-18 15:26:57 -0700875 while (!done) {
876 int oldCountX = countX;
877 int oldCountY = countY;
878 if (countX * countY < count) {
879 // Current grid is too small, expand it
Adam Cohen78dc83e2011-11-15 17:10:00 -0800880 if ((countX <= countY || countY == mMaxCountY) && countX < mMaxCountX) {
Adam Cohen7c693212011-05-18 15:26:57 -0700881 countX++;
882 } else if (countY < mMaxCountY) {
883 countY++;
884 }
885 if (countY == 0) countY++;
886 } else if ((countY - 1) * countX >= count && countY >= countX) {
887 countY = Math.max(0, countY - 1);
888 } else if ((countX - 1) * countY >= count) {
889 countX = Math.max(0, countX - 1);
Adam Cohen2801caf2011-05-13 20:57:39 -0700890 }
Adam Cohen7c693212011-05-18 15:26:57 -0700891 done = countX == oldCountX && countY == oldCountY;
Adam Cohen2801caf2011-05-13 20:57:39 -0700892 }
Adam Cohen7c693212011-05-18 15:26:57 -0700893 mContent.setGridSize(countX, countY);
Adam Cohen2801caf2011-05-13 20:57:39 -0700894 arrangeChildren(list);
895 }
896
897 public boolean isFull() {
Adam Cohen78dc83e2011-11-15 17:10:00 -0800898 return getItemCount() >= mMaxNumItems;
Adam Cohen2801caf2011-05-13 20:57:39 -0700899 }
900
901 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700902 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700903
Winson Chung892c74d2013-08-22 16:15:50 -0700904 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700905 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700906 int height = getFolderHeight();
Adam Cohen2801caf2011-05-13 20:57:39 -0700907
Adam Cohen307fe232012-08-16 17:55:58 -0700908 float scale = parent.getDescendantRectRelativeToSelf(mFolderIcon, mTempRect);
Adam Cohen8e776a62011-06-28 18:10:06 -0700909
Winson Chungaf40f202013-09-18 18:26:31 -0700910 LauncherAppState app = LauncherAppState.getInstance();
911 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
912
Adam Cohen307fe232012-08-16 17:55:58 -0700913 int centerX = (int) (mTempRect.left + mTempRect.width() * scale / 2);
914 int centerY = (int) (mTempRect.top + mTempRect.height() * scale / 2);
Adam Cohen2801caf2011-05-13 20:57:39 -0700915 int centeredLeft = centerX - width / 2;
916 int centeredTop = centerY - height / 2;
Winson Chung3057b1c2013-10-10 17:35:35 -0700917 int currentPage = mLauncher.getWorkspace().getNextPage();
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800918 // In case the workspace is scrolling, we need to use the final scroll to compute
919 // the folders bounds.
920 mLauncher.getWorkspace().setFinalScrollForPageChange(currentPage);
Adam Cohen35e7e642011-07-17 14:47:18 -0700921 // We first fetch the currently visible CellLayoutChildren
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800922 CellLayout currentLayout = (CellLayout) mLauncher.getWorkspace().getChildAt(currentPage);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700923 ShortcutAndWidgetContainer boundingLayout = currentLayout.getShortcutsAndWidgets();
Adam Cohen35e7e642011-07-17 14:47:18 -0700924 Rect bounds = new Rect();
925 parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800926 // We reset the workspaces scroll
927 mLauncher.getWorkspace().resetFinalScrollForPageChange(currentPage);
Adam Cohen2801caf2011-05-13 20:57:39 -0700928
Adam Cohen35e7e642011-07-17 14:47:18 -0700929 // We need to bound the folder to the currently visible CellLayoutChildren
930 int left = Math.min(Math.max(bounds.left, centeredLeft),
931 bounds.left + bounds.width() - width);
932 int top = Math.min(Math.max(bounds.top, centeredTop),
933 bounds.top + bounds.height() - height);
Winson Chungaf40f202013-09-18 18:26:31 -0700934 if (grid.isPhone() && (grid.availableWidthPx - width) < grid.iconSizePx) {
935 // Center the folder if it is full (on phones only)
936 left = (grid.availableWidthPx - width) / 2;
937 } else if (width >= bounds.width()) {
938 // If the folder doesn't fit within the bounds, center it about the desired bounds
Adam Cohen35e7e642011-07-17 14:47:18 -0700939 left = bounds.left + (bounds.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700940 }
Adam Cohen35e7e642011-07-17 14:47:18 -0700941 if (height >= bounds.height()) {
942 top = bounds.top + (bounds.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700943 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700944
945 int folderPivotX = width / 2 + (centeredLeft - left);
946 int folderPivotY = height / 2 + (centeredTop - top);
947 setPivotX(folderPivotX);
948 setPivotY(folderPivotY);
Adam Cohen268c4752012-06-06 17:47:33 -0700949 mFolderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700950 (1.0f * folderPivotX / width));
Adam Cohen268c4752012-06-06 17:47:33 -0700951 mFolderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700952 (1.0f * folderPivotY / height));
Adam Cohen3bf84d32012-05-07 20:17:14 -0700953
Adam Cohen662b5982011-12-13 17:45:21 -0800954 lp.width = width;
955 lp.height = height;
956 lp.x = left;
957 lp.y = top;
Adam Cohen2801caf2011-05-13 20:57:39 -0700958 }
959
Adam Cohen268c4752012-06-06 17:47:33 -0700960 float getPivotXForIconAnimation() {
961 return mFolderIconPivotX;
962 }
963 float getPivotYForIconAnimation() {
964 return mFolderIconPivotY;
965 }
966
Adam Cohen2801caf2011-05-13 20:57:39 -0700967 private void setupContentForNumItems(int count) {
Adam Cohen4045eb72011-10-06 11:44:26 -0700968 setupContentDimensions(count);
Adam Cohen2801caf2011-05-13 20:57:39 -0700969
Adam Cohen8e776a62011-06-28 18:10:06 -0700970 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700971 if (lp == null) {
Adam Cohen8e776a62011-06-28 18:10:06 -0700972 lp = new DragLayer.LayoutParams(0, 0);
973 lp.customPosition = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700974 setLayoutParams(lp);
975 }
976 centerAboutIcon();
977 }
978
Winson Chung892c74d2013-08-22 16:15:50 -0700979 private int getContentAreaHeight() {
980 LauncherAppState app = LauncherAppState.getInstance();
981 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
982 Rect workspacePadding = grid.getWorkspacePadding(grid.isLandscape ?
983 CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
984 int maxContentAreaHeight = grid.availableHeightPx -
Winson Chung892c74d2013-08-22 16:15:50 -0700985 workspacePadding.top - workspacePadding.bottom -
Winson Chung892c74d2013-08-22 16:15:50 -0700986 mFolderNameHeight;
Adam Cohen1960ea42013-11-12 11:33:14 +0000987 int height = Math.min(maxContentAreaHeight,
Winson Chung892c74d2013-08-22 16:15:50 -0700988 mContent.getDesiredHeight());
Adam Cohen1960ea42013-11-12 11:33:14 +0000989 return Math.max(height, MIN_CONTENT_DIMEN);
990 }
991
992 private int getContentAreaWidth() {
993 return Math.max(mContent.getDesiredWidth(), MIN_CONTENT_DIMEN);
Winson Chung892c74d2013-08-22 16:15:50 -0700994 }
995
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700996 private int getFolderHeight() {
Winson Chung892c74d2013-08-22 16:15:50 -0700997 int height = getPaddingTop() + getPaddingBottom()
998 + getContentAreaHeight() + mFolderNameHeight;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700999 return height;
1000 }
Adam Cohen234c4cd2011-07-17 21:03:04 -07001001
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001002 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001003 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
1004 int height = getFolderHeight();
Adam Cohen1960ea42013-11-12 11:33:14 +00001005 int contentAreaWidthSpec = MeasureSpec.makeMeasureSpec(getContentAreaWidth(),
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001006 MeasureSpec.EXACTLY);
Winson Chung892c74d2013-08-22 16:15:50 -07001007 int contentAreaHeightSpec = MeasureSpec.makeMeasureSpec(getContentAreaHeight(),
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001008 MeasureSpec.EXACTLY);
Adam Cohen1960ea42013-11-12 11:33:14 +00001009
Sunny Goyalc9acdd52015-02-26 12:34:42 -08001010 mContent.setFixedSize(getContentAreaWidth(), getContentAreaHeight());
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001011 mScrollView.measure(contentAreaWidthSpec, contentAreaHeightSpec);
1012 mFolderName.measure(contentAreaWidthSpec,
Adam Cohen234c4cd2011-07-17 21:03:04 -07001013 MeasureSpec.makeMeasureSpec(mFolderNameHeight, MeasureSpec.EXACTLY));
1014 setMeasuredDimension(width, height);
1015 }
1016
Adam Cohen2801caf2011-05-13 20:57:39 -07001017 private void arrangeChildren(ArrayList<View> list) {
1018 int[] vacant = new int[2];
1019 if (list == null) {
1020 list = getItemsInReadingOrder();
1021 }
1022 mContent.removeAllViews();
1023
1024 for (int i = 0; i < list.size(); i++) {
1025 View v = list.get(i);
1026 mContent.getVacantCell(vacant, 1, 1);
1027 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) v.getLayoutParams();
1028 lp.cellX = vacant[0];
1029 lp.cellY = vacant[1];
1030 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohen2792a332011-09-26 21:09:47 -07001031 if (info.cellX != vacant[0] || info.cellY != vacant[1]) {
1032 info.cellX = vacant[0];
1033 info.cellY = vacant[1];
1034 LauncherModel.addOrMoveItemInDatabase(mLauncher, info, mInfo.id, 0,
1035 info.cellX, info.cellY);
1036 }
Adam Cohen2801caf2011-05-13 20:57:39 -07001037 boolean insert = false;
1038 mContent.addViewToCellLayout(v, insert ? 0 : -1, (int)info.id, lp, true);
1039 }
Adam Cohen7c693212011-05-18 15:26:57 -07001040 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -07001041 }
1042
Adam Cohena9cf38f2011-05-02 15:36:58 -07001043 public int getItemCount() {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001044 return mContent.getShortcutsAndWidgets().getChildCount();
Adam Cohena9cf38f2011-05-02 15:36:58 -07001045 }
1046
1047 public View getItemAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001048 return mContent.getShortcutsAndWidgets().getChildAt(index);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001049 }
1050
Adam Cohen2801caf2011-05-13 20:57:39 -07001051 private void onCloseComplete() {
Adam Cohen05e0f402011-08-01 12:12:49 -07001052 DragLayer parent = (DragLayer) getParent();
Michael Jurka5649c282012-06-18 10:33:21 -07001053 if (parent != null) {
1054 parent.removeView(this);
1055 }
Adam Cohen4554ee12011-08-03 16:13:21 -07001056 mDragController.removeDropTarget((DropTarget) this);
Adam Cohen05e0f402011-08-01 12:12:49 -07001057 clearFocus();
Adam Cohenac56cff2011-09-28 20:45:37 -07001058 mFolderIcon.requestFocus();
Adam Cohen05e0f402011-08-01 12:12:49 -07001059
Adam Cohen2801caf2011-05-13 20:57:39 -07001060 if (mRearrangeOnClose) {
1061 setupContentForNumItems(getItemCount());
1062 mRearrangeOnClose = false;
1063 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001064 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001065 if (!mDragInProgress && !mSuppressFolderDeletion) {
1066 replaceFolderWithFinalItem();
1067 } else if (mDragInProgress) {
1068 mDeleteFolderOnDropCompleted = true;
1069 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001070 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001071 mSuppressFolderDeletion = false;
Adam Cohenafb01ee2011-06-23 15:38:03 -07001072 }
1073
1074 private void replaceFolderWithFinalItem() {
Adam Cohenafb01ee2011-06-23 15:38:03 -07001075 // Add the last remaining child to the workspace in place of the folder
Adam Cohenfb91f302012-06-11 15:45:18 -07001076 Runnable onCompleteRunnable = new Runnable() {
1077 @Override
1078 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -07001079 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screenId);
Adam Cohenafb01ee2011-06-23 15:38:03 -07001080
Winson Chung33231f52013-12-09 16:57:45 -08001081 View child = null;
Adam Cohenfb91f302012-06-11 15:45:18 -07001082 // Move the item from the folder to the workspace, in the position of the folder
1083 if (getItemCount() == 1) {
1084 ShortcutInfo finalItem = mInfo.contents.get(0);
Adam Cohenc5e63f32012-07-12 16:16:57 -07001085 child = mLauncher.createShortcut(R.layout.application, cellLayout,
Adam Cohenfb91f302012-06-11 15:45:18 -07001086 finalItem);
1087 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -07001088 mInfo.screenId, mInfo.cellX, mInfo.cellY);
Adam Cohenfb91f302012-06-11 15:45:18 -07001089 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001090 if (getItemCount() <= 1) {
1091 // Remove the folder
1092 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
Dan Sandler0eb687f2014-01-10 13:24:55 -05001093 if (cellLayout != null) {
1094 // b/12446428 -- sometimes the cell layout has already gone away?
1095 cellLayout.removeView(mFolderIcon);
1096 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001097 if (mFolderIcon instanceof DropTarget) {
1098 mDragController.removeDropTarget((DropTarget) mFolderIcon);
1099 }
1100 mLauncher.removeFolder(mInfo);
1101 }
Adam Cohenc5e63f32012-07-12 16:16:57 -07001102 // We add the child after removing the folder to prevent both from existing at
Winson Chung0e6a7132013-08-23 12:55:10 -07001103 // the same time in the CellLayout. We need to add the new item with addInScreenFromBind()
1104 // to ensure that hotseat items are placed correctly.
Adam Cohenc5e63f32012-07-12 16:16:57 -07001105 if (child != null) {
Winson Chung0e6a7132013-08-23 12:55:10 -07001106 mLauncher.getWorkspace().addInScreenFromBind(child, mInfo.container, mInfo.screenId,
Adam Cohenc5e63f32012-07-12 16:16:57 -07001107 mInfo.cellX, mInfo.cellY, mInfo.spanX, mInfo.spanY);
1108 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001109 }
1110 };
1111 View finalChild = getItemAt(0);
1112 if (finalChild != null) {
1113 mFolderIcon.performDestroyAnimation(finalChild, onCompleteRunnable);
Winson Chung33231f52013-12-09 16:57:45 -08001114 } else {
1115 onCompleteRunnable.run();
Adam Cohenafb01ee2011-06-23 15:38:03 -07001116 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001117 mDestroyed = true;
1118 }
1119
1120 boolean isDestroyed() {
1121 return mDestroyed;
Adam Cohen2801caf2011-05-13 20:57:39 -07001122 }
1123
Adam Cohenac56cff2011-09-28 20:45:37 -07001124 // This method keeps track of the last item in the folder for the purposes
1125 // of keyboard focus
1126 private void updateTextViewFocus() {
1127 View lastChild = getItemAt(getItemCount() - 1);
1128 getItemAt(getItemCount() - 1);
1129 if (lastChild != null) {
1130 mFolderName.setNextFocusDownId(lastChild.getId());
1131 mFolderName.setNextFocusRightId(lastChild.getId());
1132 mFolderName.setNextFocusLeftId(lastChild.getId());
1133 mFolderName.setNextFocusUpId(lastChild.getId());
1134 }
1135 }
1136
Adam Cohenbfbfd262011-06-13 16:55:12 -07001137 public void onDrop(DragObject d) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001138 Runnable cleanUpRunnable = null;
1139
Adam Cohen689ff162014-05-08 17:27:56 -07001140 // If we are coming from All Apps space, we defer removing the extra empty screen
1141 // until the folder closes
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001142 if (d.dragSource != mLauncher.getWorkspace() && !(d.dragSource instanceof Folder)) {
1143 cleanUpRunnable = new Runnable() {
1144 @Override
1145 public void run() {
Adam Cohen689ff162014-05-08 17:27:56 -07001146 mLauncher.exitSpringLoadedDragModeDelayed(true,
1147 Launcher.EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
1148 null);
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001149 }
1150 };
Adam Cohenbfbfd262011-06-13 16:55:12 -07001151 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001152
1153 View currentDragView;
1154 ShortcutInfo si = mCurrentDragInfo;
1155 if (mIsExternalDrag) {
1156 si.cellX = mEmptyCell[0];
1157 si.cellY = mEmptyCell[1];
Sunny Goyal95abbb32014-08-04 10:53:22 -07001158
1159 // Actually move the item in the database if it was an external drag. Call this
1160 // before creating the view, so that ShortcutInfo is updated appropriately.
1161 LauncherModel.addOrMoveItemInDatabase(
1162 mLauncher, si, mInfo.id, 0, si.cellX, si.cellY);
1163
1164 // We only need to update the locations if it doesn't get handled in #onDropCompleted.
1165 if (d.dragSource != this) {
1166 updateItemLocationsInDatabaseBatch();
1167 }
1168 mIsExternalDrag = false;
1169
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001170 currentDragView = createAndAddShortcut(si);
1171 } else {
1172 currentDragView = mCurrentDragView;
1173 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) currentDragView.getLayoutParams();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001174 si.cellX = lp.cellX = mEmptyCell[0];
1175 si.cellX = lp.cellY = mEmptyCell[1];
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001176 mContent.addViewToCellLayout(currentDragView, -1, (int) si.id, lp, true);
Adam Cohenbfbfd262011-06-13 16:55:12 -07001177 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001178
1179 if (d.dragView.hasDrawn()) {
1180
1181 // Temporarily reset the scale such that the animation target gets calculated correctly.
1182 float scaleX = getScaleX();
1183 float scaleY = getScaleY();
1184 setScaleX(1.0f);
1185 setScaleY(1.0f);
1186 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, currentDragView,
1187 cleanUpRunnable, null);
1188 setScaleX(scaleX);
1189 setScaleY(scaleY);
1190 } else {
1191 d.deferDragViewCleanupPostAnimation = false;
1192 currentDragView.setVisibility(VISIBLE);
1193 }
1194 mItemsInvalidated = true;
1195 setupContentDimensions(getItemCount());
1196
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001197 // Temporarily suppress the listener, as we did all the work already here.
1198 mSuppressOnAdd = true;
1199 mInfo.add(si);
1200 mSuppressOnAdd = false;
Sunny Goyal4b020172014-08-28 14:51:14 -07001201 // Clear the drag info, as it is no longer being dragged.
1202 mCurrentDragInfo = null;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001203 }
1204
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001205 // This is used so the item doesn't immediately appear in the folder when added. In one case
1206 // we need to create the illusion that the item isn't added back to the folder yet, to
1207 // to correspond to the animation of the icon back into the folder. This is
1208 public void hideItem(ShortcutInfo info) {
1209 View v = getViewForInfo(info);
1210 v.setVisibility(INVISIBLE);
1211 }
1212 public void showItem(ShortcutInfo info) {
1213 View v = getViewForInfo(info);
1214 v.setVisibility(VISIBLE);
1215 }
1216
Adam Cohenbfbfd262011-06-13 16:55:12 -07001217 public void onAdd(ShortcutInfo item) {
1218 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001219 // If the item was dropped onto this open folder, we have done the work associated
1220 // with adding the item to the folder, as indicated by mSuppressOnAdd being set
Adam Cohenbfbfd262011-06-13 16:55:12 -07001221 if (mSuppressOnAdd) return;
1222 if (!findAndSetEmptyCells(item)) {
1223 // The current layout is full, can we expand it?
1224 setupContentForNumItems(getItemCount() + 1);
1225 findAndSetEmptyCells(item);
1226 }
1227 createAndAddShortcut(item);
1228 LauncherModel.addOrMoveItemInDatabase(
1229 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
1230 }
1231
Adam Cohena9cf38f2011-05-02 15:36:58 -07001232 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -07001233 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001234 // If this item is being dragged from this open folder, we have already handled
1235 // the work associated with removing the item, so we don't have to do anything here.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001236 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001237 View v = getViewForInfo(item);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001238 mContent.removeView(v);
Adam Cohen2801caf2011-05-13 20:57:39 -07001239 if (mState == STATE_ANIMATING) {
1240 mRearrangeOnClose = true;
1241 } else {
1242 setupContentForNumItems(getItemCount());
1243 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001244 if (getItemCount() <= 1) {
1245 replaceFolderWithFinalItem();
1246 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001247 }
Adam Cohen7c693212011-05-18 15:26:57 -07001248
Adam Cohendf1e4e82011-06-24 15:57:39 -07001249 private View getViewForInfo(ShortcutInfo item) {
1250 for (int j = 0; j < mContent.getCountY(); j++) {
1251 for (int i = 0; i < mContent.getCountX(); i++) {
1252 View v = mContent.getChildAt(i, j);
1253 if (v.getTag() == item) {
1254 return v;
1255 }
1256 }
1257 }
1258 return null;
1259 }
1260
Adam Cohen76078c42011-06-09 15:06:52 -07001261 public void onItemsChanged() {
Adam Cohenac56cff2011-09-28 20:45:37 -07001262 updateTextViewFocus();
Adam Cohen76078c42011-06-09 15:06:52 -07001263 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001264
Adam Cohen76fc0852011-06-17 13:26:23 -07001265 public void onTitleChanged(CharSequence title) {
1266 }
Adam Cohen76078c42011-06-09 15:06:52 -07001267
Adam Cohen7c693212011-05-18 15:26:57 -07001268 public ArrayList<View> getItemsInReadingOrder() {
1269 if (mItemsInvalidated) {
1270 mItemsInReadingOrder.clear();
1271 for (int j = 0; j < mContent.getCountY(); j++) {
1272 for (int i = 0; i < mContent.getCountX(); i++) {
1273 View v = mContent.getChildAt(i, j);
1274 if (v != null) {
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001275 mItemsInReadingOrder.add(v);
Adam Cohen7c693212011-05-18 15:26:57 -07001276 }
1277 }
1278 }
1279 mItemsInvalidated = false;
1280 }
1281 return mItemsInReadingOrder;
1282 }
Adam Cohen8dfcba42011-07-07 16:38:18 -07001283
1284 public void getLocationInDragLayer(int[] loc) {
1285 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
1286 }
Adam Cohenea0818d2011-09-30 20:06:58 -07001287
1288 public void onFocusChange(View v, boolean hasFocus) {
1289 if (v == mFolderName && hasFocus) {
1290 startEditingFolderName();
1291 }
1292 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001293
1294 @Override
1295 public void getHitRectRelativeToDragLayer(Rect outRect) {
1296 getHitRect(outRect);
1297 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001298}