blob: 4414672cc4e8e689d1604924a86304a371e5332f [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 Cohendf2cc412011-04-27 16:56:57 -070048import android.widget.TextView;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049
Daniel Sandler325dc232013-06-05 22:57:57 -040050import com.android.launcher3.FolderInfo.FolderListener;
Sunny Goyalbc753352015-03-05 09:40:44 -080051import com.android.launcher3.Workspace.ItemOperator;
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
Sunny Goyalc3a609f2015-02-26 17:43:50 -080064 /**
65 * We avoid measuring {@link #mContentWrapper} with a 0 width or height, as this
66 * results in CellLayout being measured as UNSPECIFIED, which it does not support.
67 */
68 private static final int MIN_CONTENT_DIMEN = 5;
Adam Cohen4eac29a2011-07-11 17:53:37 -070069
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
Adam Cohenf0f4eda2013-06-06 21:27:03 -070075 private static final int REORDER_DELAY = 250;
Adam Cohen5d518fa2013-12-05 14:16:23 -080076 private static final int ON_EXIT_CLOSE_DELAY = 400;
Sunny Goyalc3a609f2015-02-26 17:43:50 -080077 private static final Rect sTempRect = new Rect();
78
79 private static String sDefaultFolderName;
80 private static String sHintText;
81
82 private final Alarm mReorderAlarm = new Alarm();
83 private final Alarm mOnExitAlarm = new Alarm();
84
85 private final ArrayList<View> mItemsInReadingOrder = new ArrayList<View>();
86
87 private final int mExpandDuration;
88 private final int mMaterialExpandDuration;
89 private final int mMaterialExpandStagger;
90
91 private final InputMethodManager mInputMethodManager;
92
93 protected final Launcher mLauncher;
94 protected DragController mDragController;
95 protected FolderInfo mInfo;
96
Adam Cohen2801caf2011-05-13 20:57:39 -070097 private FolderIcon mFolderIcon;
Sunny Goyalc3a609f2015-02-26 17:43:50 -080098
Sunny Goyalbc753352015-03-05 09:40:44 -080099 private FolderContent mContent;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800100 private View mContentWrapper;
101 FolderEditText mFolderName;
102
103 private View mBottomContent;
104 private int mBottomContentHeight;
105
106 // Cell ranks used for drag and drop
107 private int mTargetRank, mPrevTargetRank, mEmptyCellRank;
108
109 private int mState = STATE_NONE;
110 private boolean mRearrangeOnClose = false;
Adam Cohen7c693212011-05-18 15:26:57 -0700111 boolean mItemsInvalidated = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700112 private ShortcutInfo mCurrentDragInfo;
113 private View mCurrentDragView;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800114 private boolean mIsExternalDrag;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700115 boolean mSuppressOnAdd = false;
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700116 private boolean mDragInProgress = false;
117 private boolean mDeleteFolderOnDropCompleted = false;
118 private boolean mSuppressFolderDeletion = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700119 private boolean mItemAddedBackToSelfViaIcon = false;
Adam Cohen268c4752012-06-06 17:47:33 -0700120 private float mFolderIconPivotX;
121 private float mFolderIconPivotY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700122 private boolean mIsEditingName = false;
Adam Cohen1960ea42013-11-12 11:33:14 +0000123
Adam Cohenfb91f302012-06-11 15:45:18 -0700124 private boolean mDestroyed;
125
Michael Jurka1e2f4652013-07-08 18:03:46 -0700126 private Runnable mDeferredAction;
127 private boolean mDeferDropAfterUninstall;
128 private boolean mUninstallSuccessful;
129
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130 /**
131 * Used to inflate the Workspace from XML.
132 *
133 * @param context The application's context.
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800134 * @param attrs The attributes set containing the Workspace's customization values.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135 */
136 public Folder(Context context, AttributeSet attrs) {
137 super(context, attrs);
138 setAlwaysDrawnWithCacheEnabled(false);
Adam Cohen76fc0852011-06-17 13:26:23 -0700139 mInputMethodManager = (InputMethodManager)
Michael Jurka8b805b12012-04-18 14:23:14 -0700140 getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
Adam Cohen76fc0852011-06-17 13:26:23 -0700141
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800142 Resources res = getResources();
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700143 mExpandDuration = res.getInteger(R.integer.config_folderExpandDuration);
144 mMaterialExpandDuration = res.getInteger(R.integer.config_materialFolderExpandDuration);
145 mMaterialExpandStagger = res.getInteger(R.integer.config_materialFolderExpandStagger);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700146
147 if (sDefaultFolderName == null) {
148 sDefaultFolderName = res.getString(R.string.folder_name);
149 }
Adam Cohena65beee2011-06-27 21:32:23 -0700150 if (sHintText == null) {
151 sHintText = res.getString(R.string.folder_hint_text);
152 }
Adam Cohen4eac29a2011-07-11 17:53:37 -0700153 mLauncher = (Launcher) context;
Adam Cohenac56cff2011-09-28 20:45:37 -0700154 // We need this view to be focusable in touch mode so that when text editing of the folder
155 // name is complete, we have something to focus on, thus hiding the cursor and giving
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800156 // reliable behavior when clicking the text field (since it will always gain focus on click).
Adam Cohenac56cff2011-09-28 20:45:37 -0700157 setFocusableInTouchMode(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158 }
159
160 @Override
161 protected void onFinishInflate() {
162 super.onFinishInflate();
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800163 mContentWrapper = findViewById(R.id.folder_content_wrapper);
Sunny Goyalbc753352015-03-05 09:40:44 -0800164 mContent = (FolderContent) findViewById(R.id.folder_content);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800165 mContent.setFolder(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700166
Adam Cohenac56cff2011-09-28 20:45:37 -0700167 mFolderName = (FolderEditText) findViewById(R.id.folder_name);
168 mFolderName.setFolder(this);
Adam Cohenea0818d2011-09-30 20:06:58 -0700169 mFolderName.setOnFocusChangeListener(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700170
Adam Cohen76fc0852011-06-17 13:26:23 -0700171 // We disable action mode for now since it messes up the view on phones
172 mFolderName.setCustomSelectionActionModeCallback(mActionModeCallback);
Adam Cohen76fc0852011-06-17 13:26:23 -0700173 mFolderName.setOnEditorActionListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700174 mFolderName.setSelectAllOnFocus(true);
Adam Cohen7a14d0b2011-06-24 11:36:35 -0700175 mFolderName.setInputType(mFolderName.getInputType() |
176 InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800177
178 // We only have the folder name at the bottom for now
179 mBottomContent = mFolderName;
180 // We find out how tall the bottom content wants to be (it is set to wrap_content), so that
181 // we can allocate the appropriate amount of space for it.
182 int measureSpec = MeasureSpec.UNSPECIFIED;
183 mBottomContent.measure(measureSpec, measureSpec);
184 mBottomContentHeight = mBottomContent.getMeasuredHeight();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700186
Adam Cohen76fc0852011-06-17 13:26:23 -0700187 private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
188 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
189 return false;
190 }
191
192 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
193 return false;
194 }
195
196 public void onDestroyActionMode(ActionMode mode) {
197 }
198
199 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
200 return false;
201 }
202 };
203
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800204 public void onClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700205 Object tag = v.getTag();
206 if (tag instanceof ShortcutInfo) {
Adam Cohenb5fe60c2013-06-06 22:03:51 -0700207 mLauncher.onClick(v);
Adam Cohendf2cc412011-04-27 16:56:57 -0700208 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800209 }
210
211 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -0700212 // Return if global dragging is not enabled
213 if (!mLauncher.isDraggingEnabled()) return true;
214
Adam Cohendf2cc412011-04-27 16:56:57 -0700215 Object tag = v.getTag();
216 if (tag instanceof ShortcutInfo) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700217 ShortcutInfo item = (ShortcutInfo) tag;
218 if (!v.isInTouchMode()) {
219 return false;
220 }
221
Adam Cohenac8c8762011-07-13 11:15:27 -0700222 mLauncher.getWorkspace().beginDragShared(v, this);
Adam Cohen76078c42011-06-09 15:06:52 -0700223
224 mCurrentDragInfo = item;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800225 mEmptyCellRank = item.rank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700226 mCurrentDragView = v;
Adam Cohenfc53cd22011-07-20 15:45:11 -0700227
228 mContent.removeView(mCurrentDragView);
229 mInfo.remove(mCurrentDragInfo);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700230 mDragInProgress = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700231 mItemAddedBackToSelfViaIcon = false;
Adam Cohendf2cc412011-04-27 16:56:57 -0700232 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800233 return true;
234 }
235
Adam Cohen76fc0852011-06-17 13:26:23 -0700236 public boolean isEditingName() {
237 return mIsEditingName;
238 }
239
240 public void startEditingFolderName() {
Adam Cohena65beee2011-06-27 21:32:23 -0700241 mFolderName.setHint("");
Adam Cohen76fc0852011-06-17 13:26:23 -0700242 mIsEditingName = true;
243 }
244
245 public void dismissEditingName() {
246 mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
247 doneEditingFolderName(true);
248 }
249
250 public void doneEditingFolderName(boolean commit) {
Adam Cohena65beee2011-06-27 21:32:23 -0700251 mFolderName.setHint(sHintText);
Adam Cohen1df26a32011-08-12 16:15:23 -0700252 // Convert to a string here to ensure that no other state associated with the text field
253 // gets saved.
Adam Cohen3371da02011-10-25 21:38:29 -0700254 String newTitle = mFolderName.getText().toString();
255 mInfo.setTitle(newTitle);
Adam Cohen76fc0852011-06-17 13:26:23 -0700256 LauncherModel.updateItemInDatabase(mLauncher, mInfo);
Adam Cohenac56cff2011-09-28 20:45:37 -0700257
Adam Cohen3371da02011-10-25 21:38:29 -0700258 if (commit) {
259 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700260 String.format(getContext().getString(R.string.folder_renamed), newTitle));
Adam Cohen3371da02011-10-25 21:38:29 -0700261 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700262 // In order to clear the focus from the text field, we set the focus on ourself. This
263 // ensures that every time the field is clicked, focus is gained, giving reliable behavior.
264 requestFocus();
265
Adam Cohene601a432011-07-26 21:51:30 -0700266 Selection.setSelection((Spannable) mFolderName.getText(), 0, 0);
Adam Cohen76fc0852011-06-17 13:26:23 -0700267 mIsEditingName = false;
268 }
269
270 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
271 if (actionId == EditorInfo.IME_ACTION_DONE) {
272 dismissEditingName();
273 return true;
274 }
275 return false;
276 }
277
278 public View getEditTextRegion() {
279 return mFolderName;
280 }
281
Adam Cohen0c872ba2011-05-05 10:34:16 -0700282 /**
283 * We need to handle touch events to prevent them from falling through to the workspace below.
284 */
285 @Override
286 public boolean onTouchEvent(MotionEvent ev) {
287 return true;
288 }
289
Joe Onorato00acb122009-08-04 16:04:30 -0400290 public void setDragController(DragController dragController) {
291 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800292 }
293
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800294 public void setFolderIcon(FolderIcon icon) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700295 mFolderIcon = icon;
296 }
297
Adam Cohen3371da02011-10-25 21:38:29 -0700298 @Override
299 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
300 // When the folder gets focus, we don't want to announce the list of items.
301 return true;
302 }
303
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800304 /**
305 * @return the FolderInfo object associated with this folder
306 */
307 FolderInfo getInfo() {
308 return mInfo;
309 }
310
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800311 void bind(FolderInfo info) {
312 mInfo = info;
Adam Cohendf2cc412011-04-27 16:56:57 -0700313 ArrayList<ShortcutInfo> children = info.contents;
Sunny Goyal08f72612015-01-05 13:41:43 -0800314 Collections.sort(children, Utilities.RANK_COMPARATOR);
Sunny Goyal08f72612015-01-05 13:41:43 -0800315
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800316 ArrayList<ShortcutInfo> overflow = mContent.bindItems(children);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700317
Jason Monk4ff73882014-04-24 16:48:07 -0400318 // If our folder has too many items we prune them from the list. This is an issue
Adam Cohenc508b2d2011-06-28 14:41:44 -0700319 // when upgrading from the old Folders implementation which could contain an unlimited
320 // number of items.
321 for (ShortcutInfo item: overflow) {
322 mInfo.remove(item);
323 LauncherModel.deleteItemFromDatabase(mLauncher, item);
324 }
325
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800326 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
327 if (lp == null) {
328 lp = new DragLayer.LayoutParams(0, 0);
329 lp.customPosition = true;
330 setLayoutParams(lp);
331 }
332 centerAboutIcon();
333
Adam Cohen4dbe6d92011-05-18 17:14:15 -0700334 mItemsInvalidated = true;
Adam Cohenac56cff2011-09-28 20:45:37 -0700335 updateTextViewFocus();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700336 mInfo.addListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700337
Adam Cohenafb01ee2011-06-23 15:38:03 -0700338 if (!sDefaultFolderName.contentEquals(mInfo.title)) {
Adam Cohen4ef610f2011-06-21 22:37:36 -0700339 mFolderName.setText(mInfo.title);
340 } else {
341 mFolderName.setText("");
342 }
Winson Chung33231f52013-12-09 16:57:45 -0800343
344 // In case any children didn't come across during loading, clean up the folder accordingly
345 mFolderIcon.post(new Runnable() {
346 public void run() {
347 if (getItemCount() <= 1) {
348 replaceFolderWithFinalItem();
349 }
350 }
351 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700352 }
353
354 /**
355 * Creates a new UserFolder, inflated from R.layout.user_folder.
356 *
357 * @param context The application's context.
358 *
359 * @return A new UserFolder.
360 */
361 static Folder fromXml(Context context) {
362 return (Folder) LayoutInflater.from(context).inflate(R.layout.user_folder, null);
363 }
364
365 /**
366 * This method is intended to make the UserFolder to be visually identical in size and position
367 * to its associated FolderIcon. This allows for a seamless transition into the expanded state.
368 */
369 private void positionAndSizeAsIcon() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700370 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800371 setScaleX(0.8f);
372 setScaleY(0.8f);
373 setAlpha(0f);
Adam Cohendf2cc412011-04-27 16:56:57 -0700374 mState = STATE_SMALL;
375 }
376
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700377 private void prepareReveal() {
378 setScaleX(1f);
379 setScaleY(1f);
380 setAlpha(1f);
381 mState = STATE_SMALL;
382 }
383
Adam Cohendf2cc412011-04-27 16:56:57 -0700384 public void animateOpen() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700385 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen6441de02011-12-14 14:25:32 -0800386
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700387 Animator openFolderAnim = null;
388 final Runnable onCompleteRunnable;
Kenny Guyd794a3f2014-09-16 15:17:58 +0100389 if (!Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700390 positionAndSizeAsIcon();
391 centerAboutIcon();
392
393 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1);
394 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
395 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
396 final ObjectAnimator oa =
397 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
398 oa.setDuration(mExpandDuration);
399 openFolderAnim = oa;
400
401 setLayerType(LAYER_TYPE_HARDWARE, null);
402 onCompleteRunnable = new Runnable() {
403 @Override
404 public void run() {
405 setLayerType(LAYER_TYPE_NONE, null);
406 }
407 };
408 } else {
409 prepareReveal();
410 centerAboutIcon();
411
412 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
413 int height = getFolderHeight();
414
415 float transX = - 0.075f * (width / 2 - getPivotX());
416 float transY = - 0.075f * (height / 2 - getPivotY());
417 setTranslationX(transX);
418 setTranslationY(transY);
419 PropertyValuesHolder tx = PropertyValuesHolder.ofFloat("translationX", transX, 0);
420 PropertyValuesHolder ty = PropertyValuesHolder.ofFloat("translationY", transY, 0);
421
422 int rx = (int) Math.max(Math.max(width - getPivotX(), 0), getPivotX());
423 int ry = (int) Math.max(Math.max(height - getPivotY(), 0), getPivotY());
424 float radius = (float) Math.sqrt(rx * rx + ry * ry);
425 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
426 Animator reveal = LauncherAnimUtils.createCircularReveal(this, (int) getPivotX(),
427 (int) getPivotY(), 0, radius);
428 reveal.setDuration(mMaterialExpandDuration);
429 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
430
Sunny Goyalbc753352015-03-05 09:40:44 -0800431 mContentWrapper.setAlpha(0f);
432 Animator iconsAlpha = LauncherAnimUtils.ofFloat(mContentWrapper, "alpha", 0f, 1f);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700433 iconsAlpha.setDuration(mMaterialExpandDuration);
434 iconsAlpha.setStartDelay(mMaterialExpandStagger);
435 iconsAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
436
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800437 mBottomContent.setAlpha(0f);
438 Animator textAlpha = LauncherAnimUtils.ofFloat(mBottomContent, "alpha", 0f, 1f);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700439 textAlpha.setDuration(mMaterialExpandDuration);
440 textAlpha.setStartDelay(mMaterialExpandStagger);
441 textAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
442
443 Animator drift = LauncherAnimUtils.ofPropertyValuesHolder(this, tx, ty);
444 drift.setDuration(mMaterialExpandDuration);
445 drift.setStartDelay(mMaterialExpandStagger);
446 drift.setInterpolator(new LogDecelerateInterpolator(60, 0));
447
448 anim.play(drift);
449 anim.play(iconsAlpha);
450 anim.play(textAlpha);
451 anim.play(reveal);
452
453 openFolderAnim = anim;
454
Sunny Goyalbc753352015-03-05 09:40:44 -0800455 mContentWrapper.setLayerType(LAYER_TYPE_HARDWARE, null);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700456 onCompleteRunnable = new Runnable() {
457 @Override
458 public void run() {
Sunny Goyalbc753352015-03-05 09:40:44 -0800459 mContentWrapper.setLayerType(LAYER_TYPE_NONE, null);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700460 }
461 };
462 }
463 openFolderAnim.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700464 @Override
465 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700466 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Sunny Goyalbc753352015-03-05 09:40:44 -0800467 mContent.getAccessibilityDescription());
Adam Cohendf2cc412011-04-27 16:56:57 -0700468 mState = STATE_ANIMATING;
469 }
470 @Override
471 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700472 mState = STATE_OPEN;
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700473
474 if (onCompleteRunnable != null) {
475 onCompleteRunnable.run();
476 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800477
Sunny Goyalbc753352015-03-05 09:40:44 -0800478 mContent.setFocusOnFirstChild();
Adam Cohendf2cc412011-04-27 16:56:57 -0700479 }
480 });
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700481 openFolderAnim.start();
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800482
483 // Make sure the folder picks up the last drag move even if the finger doesn't move.
484 if (mDragController.isDragging()) {
485 mDragController.forceTouchMove();
486 }
487 }
488
489 public void beginExternalDrag(ShortcutInfo item) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800490 mCurrentDragInfo = item;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800491 mEmptyCellRank = mContent.allocateNewLastItemRank();
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800492 mIsExternalDrag = true;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800493 mDragInProgress = true;
Adam Cohendf2cc412011-04-27 16:56:57 -0700494 }
495
Adam Cohen3371da02011-10-25 21:38:29 -0700496 private void sendCustomAccessibilityEvent(int type, String text) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700497 AccessibilityManager accessibilityManager = (AccessibilityManager)
498 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
499 if (accessibilityManager.isEnabled()) {
Adam Cohen3371da02011-10-25 21:38:29 -0700500 AccessibilityEvent event = AccessibilityEvent.obtain(type);
501 onInitializeAccessibilityEvent(event);
502 event.getText().add(text);
Michael Jurka8b805b12012-04-18 14:23:14 -0700503 accessibilityManager.sendAccessibilityEvent(event);
Adam Cohen3371da02011-10-25 21:38:29 -0700504 }
505 }
506
Adam Cohendf2cc412011-04-27 16:56:57 -0700507 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700508 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800509 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
510 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
511 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
Michael Jurka032e6ba2013-04-22 15:08:42 +0200512 final ObjectAnimator oa =
Michael Jurka2ecf9952012-06-18 12:52:28 -0700513 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohendf2cc412011-04-27 16:56:57 -0700514
Adam Cohen2801caf2011-05-13 20:57:39 -0700515 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700516 @Override
517 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700518 onCloseComplete();
Michael Jurka0121c3e2012-05-31 08:36:04 -0700519 setLayerType(LAYER_TYPE_NONE, null);
Adam Cohen2801caf2011-05-13 20:57:39 -0700520 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700521 }
522 @Override
523 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700524 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700525 getContext().getString(R.string.folder_closed));
Adam Cohendf2cc412011-04-27 16:56:57 -0700526 mState = STATE_ANIMATING;
527 }
528 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700529 oa.setDuration(mExpandDuration);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700530 setLayerType(LAYER_TYPE_HARDWARE, null);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100531 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700532 }
533
Adam Cohencb3382b2011-05-24 14:07:08 -0700534 public boolean acceptDrop(DragObject d) {
535 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700536 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700537 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
538 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
539 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700540 }
541
Adam Cohencb3382b2011-05-24 14:07:08 -0700542 public void onDragEnter(DragObject d) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800543 mPrevTargetRank = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700544 mOnExitAlarm.cancelAlarm();
545 }
546
547 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
548 public void onAlarm(Alarm alarm) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800549 mContent.realTimeReorder(mEmptyCellRank, mTargetRank);
550 mEmptyCellRank = mTargetRank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700551 }
552 };
553
Sunny Goyalc46bfef2015-01-05 12:40:08 -0800554 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohen2374abf2013-04-16 14:56:57 -0700555 public boolean isLayoutRtl() {
556 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
557 }
558
Adam Cohencb3382b2011-05-24 14:07:08 -0700559 public void onDragOver(DragObject d) {
Sunny Goyal1587d532015-01-29 09:57:16 -0800560 final float[] r = d.getVisualCenter(null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700561 r[0] -= getPaddingLeft();
562 r[1] -= getPaddingTop();
563
Sunny Goyalbc753352015-03-05 09:40:44 -0800564 mTargetRank = mContent.findNearestArea((int) r[0], (int) r[1]);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800565 if (mTargetRank != mPrevTargetRank) {
Alan Viverette4cda5b72013-08-28 17:53:41 -0700566 mReorderAlarm.cancelAlarm();
Sunny Goyalc46bfef2015-01-05 12:40:08 -0800567 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
568 mReorderAlarm.setAlarm(REORDER_DELAY);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800569 mPrevTargetRank = mTargetRank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700570 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700571 }
572
Adam Cohenbfbfd262011-06-13 16:55:12 -0700573 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
574 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700575 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700576 }
577 };
578
Adam Cohen95bb8002011-07-03 23:40:28 -0700579 public void completeDragExit() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700580 mLauncher.closeFolder();
581 mCurrentDragInfo = null;
582 mCurrentDragView = null;
583 mSuppressOnAdd = false;
584 mRearrangeOnClose = true;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800585 mIsExternalDrag = false;
Adam Cohen3e8f8112011-07-02 18:03:00 -0700586 }
587
Adam Cohencb3382b2011-05-24 14:07:08 -0700588 public void onDragExit(DragObject d) {
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700589 // We only close the folder if this is a true drag exit, ie. not because
590 // a drop has occurred above the folder.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700591 if (!d.dragComplete) {
592 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
593 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
594 }
595 mReorderAlarm.cancelAlarm();
Adam Cohen2801caf2011-05-13 20:57:39 -0700596 }
597
Michael Jurka1e2f4652013-07-08 18:03:46 -0700598 public void onDropCompleted(final View target, final DragObject d,
599 final boolean isFlingToDelete, final boolean success) {
600 if (mDeferDropAfterUninstall) {
Michael Jurkaf3007582013-08-21 14:33:57 +0200601 Log.d(TAG, "Deferred handling drop because waiting for uninstall.");
Michael Jurka1e2f4652013-07-08 18:03:46 -0700602 mDeferredAction = new Runnable() {
603 public void run() {
604 onDropCompleted(target, d, isFlingToDelete, success);
605 mDeferredAction = null;
606 }
607 };
608 return;
609 }
610
611 boolean beingCalledAfterUninstall = mDeferredAction != null;
612 boolean successfulDrop =
613 success && (!beingCalledAfterUninstall || mUninstallSuccessful);
Winson Chung5f8afe62013-08-12 16:19:28 -0700614
Michael Jurka1e2f4652013-07-08 18:03:46 -0700615 if (successfulDrop) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800616 if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon && target != this) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700617 replaceFolderWithFinalItem();
618 }
619 } else {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800620 rearrangeChildren();
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700621 // The drag failed, we need to return the item to the folder
622 mFolderIcon.onDrop(d);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700623 }
624
625 if (target != this) {
626 if (mOnExitAlarm.alarmPending()) {
627 mOnExitAlarm.cancelAlarm();
Michael Jurka54554252013-08-01 12:52:23 +0200628 if (!successfulDrop) {
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700629 mSuppressFolderDeletion = true;
630 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700631 completeDragExit();
632 }
Adam Cohen9c58d822013-12-13 17:18:10 -0800633 }
634
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700635 mDeleteFolderOnDropCompleted = false;
636 mDragInProgress = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700637 mItemAddedBackToSelfViaIcon = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700638 mCurrentDragInfo = null;
639 mCurrentDragView = null;
640 mSuppressOnAdd = false;
Adam Cohen4045eb72011-10-06 11:44:26 -0700641
642 // Reordering may have occured, and we need to save the new item locations. We do this once
643 // at the end to prevent unnecessary database operations.
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700644 updateItemLocationsInDatabaseBatch();
Adam Cohen4045eb72011-10-06 11:44:26 -0700645 }
646
Michael Jurka1e2f4652013-07-08 18:03:46 -0700647 public void deferCompleteDropAfterUninstallActivity() {
648 mDeferDropAfterUninstall = true;
649 }
650
651 public void onUninstallActivityReturned(boolean success) {
652 mDeferDropAfterUninstall = false;
653 mUninstallSuccessful = success;
654 if (mDeferredAction != null) {
655 mDeferredAction.run();
656 }
657 }
658
Winson Chunga48487a2012-03-20 16:19:37 -0700659 @Override
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800660 public float getIntrinsicIconScaleFactor() {
661 return 1f;
662 }
663
664 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800665 public boolean supportsFlingToDelete() {
666 return true;
667 }
668
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000669 @Override
670 public boolean supportsAppInfoDropTarget() {
671 return false;
672 }
673
674 @Override
675 public boolean supportsDeleteDropTarget() {
676 return true;
677 }
678
Winson Chunga48487a2012-03-20 16:19:37 -0700679 public void onFlingToDelete(DragObject d, int x, int y, PointF vec) {
680 // Do nothing
681 }
682
683 @Override
684 public void onFlingToDeleteCompleted() {
685 // Do nothing
686 }
687
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700688 private void updateItemLocationsInDatabaseBatch() {
689 ArrayList<View> list = getItemsInReadingOrder();
690 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
691 for (int i = 0; i < list.size(); i++) {
692 View v = list.get(i);
693 ItemInfo info = (ItemInfo) v.getTag();
Sunny Goyal08f72612015-01-05 13:41:43 -0800694 info.rank = i;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700695 items.add(info);
696 }
697
698 LauncherModel.moveItemsInDatabase(mLauncher, items, mInfo.id, 0);
699 }
700
Adam Cohene25af792013-06-06 23:08:25 -0700701 public void addItemLocationsInDatabase() {
702 ArrayList<View> list = getItemsInReadingOrder();
703 for (int i = 0; i < list.size(); i++) {
704 View v = list.get(i);
705 ItemInfo info = (ItemInfo) v.getTag();
706 LauncherModel.addItemToDatabase(mLauncher, info, mInfo.id, 0,
707 info.cellX, info.cellY, false);
708 }
709 }
710
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700711 public void notifyDrop() {
712 if (mDragInProgress) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700713 mItemAddedBackToSelfViaIcon = true;
Adam Cohen76078c42011-06-09 15:06:52 -0700714 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700715 }
716
717 public boolean isDropEnabled() {
718 return true;
719 }
720
Adam Cohen2801caf2011-05-13 20:57:39 -0700721 public boolean isFull() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800722 return mContent.isFull();
Adam Cohen2801caf2011-05-13 20:57:39 -0700723 }
724
725 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700726 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700727
Winson Chung892c74d2013-08-22 16:15:50 -0700728 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700729 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700730 int height = getFolderHeight();
Adam Cohen2801caf2011-05-13 20:57:39 -0700731
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800732 float scale = parent.getDescendantRectRelativeToSelf(mFolderIcon, sTempRect);
Adam Cohen8e776a62011-06-28 18:10:06 -0700733
Winson Chungaf40f202013-09-18 18:26:31 -0700734 LauncherAppState app = LauncherAppState.getInstance();
735 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
736
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800737 int centerX = (int) (sTempRect.left + sTempRect.width() * scale / 2);
738 int centerY = (int) (sTempRect.top + sTempRect.height() * scale / 2);
Adam Cohen2801caf2011-05-13 20:57:39 -0700739 int centeredLeft = centerX - width / 2;
740 int centeredTop = centerY - height / 2;
Winson Chung3057b1c2013-10-10 17:35:35 -0700741 int currentPage = mLauncher.getWorkspace().getNextPage();
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800742 // In case the workspace is scrolling, we need to use the final scroll to compute
743 // the folders bounds.
744 mLauncher.getWorkspace().setFinalScrollForPageChange(currentPage);
Adam Cohen35e7e642011-07-17 14:47:18 -0700745 // We first fetch the currently visible CellLayoutChildren
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800746 CellLayout currentLayout = (CellLayout) mLauncher.getWorkspace().getChildAt(currentPage);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700747 ShortcutAndWidgetContainer boundingLayout = currentLayout.getShortcutsAndWidgets();
Adam Cohen35e7e642011-07-17 14:47:18 -0700748 Rect bounds = new Rect();
749 parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800750 // We reset the workspaces scroll
751 mLauncher.getWorkspace().resetFinalScrollForPageChange(currentPage);
Adam Cohen2801caf2011-05-13 20:57:39 -0700752
Adam Cohen35e7e642011-07-17 14:47:18 -0700753 // We need to bound the folder to the currently visible CellLayoutChildren
754 int left = Math.min(Math.max(bounds.left, centeredLeft),
755 bounds.left + bounds.width() - width);
756 int top = Math.min(Math.max(bounds.top, centeredTop),
757 bounds.top + bounds.height() - height);
Winson Chungaf40f202013-09-18 18:26:31 -0700758 if (grid.isPhone() && (grid.availableWidthPx - width) < grid.iconSizePx) {
759 // Center the folder if it is full (on phones only)
760 left = (grid.availableWidthPx - width) / 2;
761 } else if (width >= bounds.width()) {
762 // If the folder doesn't fit within the bounds, center it about the desired bounds
Adam Cohen35e7e642011-07-17 14:47:18 -0700763 left = bounds.left + (bounds.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700764 }
Adam Cohen35e7e642011-07-17 14:47:18 -0700765 if (height >= bounds.height()) {
766 top = bounds.top + (bounds.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700767 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700768
769 int folderPivotX = width / 2 + (centeredLeft - left);
770 int folderPivotY = height / 2 + (centeredTop - top);
771 setPivotX(folderPivotX);
772 setPivotY(folderPivotY);
Adam Cohen268c4752012-06-06 17:47:33 -0700773 mFolderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700774 (1.0f * folderPivotX / width));
Adam Cohen268c4752012-06-06 17:47:33 -0700775 mFolderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700776 (1.0f * folderPivotY / height));
Adam Cohen3bf84d32012-05-07 20:17:14 -0700777
Adam Cohen662b5982011-12-13 17:45:21 -0800778 lp.width = width;
779 lp.height = height;
780 lp.x = left;
781 lp.y = top;
Adam Cohen2801caf2011-05-13 20:57:39 -0700782 }
783
Adam Cohen268c4752012-06-06 17:47:33 -0700784 float getPivotXForIconAnimation() {
785 return mFolderIconPivotX;
786 }
787 float getPivotYForIconAnimation() {
788 return mFolderIconPivotY;
789 }
790
Winson Chung892c74d2013-08-22 16:15:50 -0700791 private int getContentAreaHeight() {
792 LauncherAppState app = LauncherAppState.getInstance();
793 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
794 Rect workspacePadding = grid.getWorkspacePadding(grid.isLandscape ?
795 CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
796 int maxContentAreaHeight = grid.availableHeightPx -
Winson Chung892c74d2013-08-22 16:15:50 -0700797 workspacePadding.top - workspacePadding.bottom -
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800798 mBottomContentHeight;
Adam Cohen1960ea42013-11-12 11:33:14 +0000799 int height = Math.min(maxContentAreaHeight,
Winson Chung892c74d2013-08-22 16:15:50 -0700800 mContent.getDesiredHeight());
Adam Cohen1960ea42013-11-12 11:33:14 +0000801 return Math.max(height, MIN_CONTENT_DIMEN);
802 }
803
804 private int getContentAreaWidth() {
805 return Math.max(mContent.getDesiredWidth(), MIN_CONTENT_DIMEN);
Winson Chung892c74d2013-08-22 16:15:50 -0700806 }
807
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700808 private int getFolderHeight() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800809 return getFolderHeight(getContentAreaHeight());
810 }
811
812 private int getFolderHeight(int contentAreaHeight) {
813 return getPaddingTop() + getPaddingBottom() + contentAreaHeight + mBottomContentHeight;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700814 }
Adam Cohen234c4cd2011-07-17 21:03:04 -0700815
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700816 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800817 int contentWidth = getContentAreaWidth();
818 int contentHeight = getContentAreaHeight();
Adam Cohen1960ea42013-11-12 11:33:14 +0000819
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800820 int contentAreaWidthSpec = MeasureSpec.makeMeasureSpec(contentWidth, MeasureSpec.EXACTLY);
821 int contentAreaHeightSpec = MeasureSpec.makeMeasureSpec(contentHeight, MeasureSpec.EXACTLY);
822
823 mContent.setFixedSize(contentWidth, contentHeight);
824 mContentWrapper.measure(contentAreaWidthSpec, contentAreaHeightSpec);
825
826 // Move the bottom content below mContent
827 mBottomContent.measure(contentAreaWidthSpec,
828 MeasureSpec.makeMeasureSpec(mBottomContentHeight, MeasureSpec.EXACTLY));
829
830 int folderWidth = getPaddingLeft() + getPaddingRight() + contentWidth;
831 int folderHeight = getFolderHeight(contentHeight);
832 setMeasuredDimension(folderWidth, folderHeight);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700833 }
834
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800835 /**
836 * Rearranges the children based on their rank.
837 */
838 public void rearrangeChildren() {
839 rearrangeChildren(-1);
840 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700841
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800842 /**
843 * Rearranges the children based on their rank.
844 * @param itemCount if greater than the total children count, empty spaces are left at the end,
845 * otherwise it is ignored.
846 */
847 public void rearrangeChildren(int itemCount) {
848 ArrayList<View> views = getItemsInReadingOrder();
849 mContent.arrangeChildren(views, Math.max(itemCount, views.size()));
Adam Cohen7c693212011-05-18 15:26:57 -0700850 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700851 }
852
Adam Cohena9cf38f2011-05-02 15:36:58 -0700853 public int getItemCount() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800854 return mContent.getItemCount();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700855 }
856
Adam Cohen2801caf2011-05-13 20:57:39 -0700857 private void onCloseComplete() {
Adam Cohen05e0f402011-08-01 12:12:49 -0700858 DragLayer parent = (DragLayer) getParent();
Michael Jurka5649c282012-06-18 10:33:21 -0700859 if (parent != null) {
860 parent.removeView(this);
861 }
Adam Cohen4554ee12011-08-03 16:13:21 -0700862 mDragController.removeDropTarget((DropTarget) this);
Adam Cohen05e0f402011-08-01 12:12:49 -0700863 clearFocus();
Adam Cohenac56cff2011-09-28 20:45:37 -0700864 mFolderIcon.requestFocus();
Adam Cohen05e0f402011-08-01 12:12:49 -0700865
Adam Cohen2801caf2011-05-13 20:57:39 -0700866 if (mRearrangeOnClose) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800867 rearrangeChildren();
Adam Cohen2801caf2011-05-13 20:57:39 -0700868 mRearrangeOnClose = false;
869 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700870 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700871 if (!mDragInProgress && !mSuppressFolderDeletion) {
872 replaceFolderWithFinalItem();
873 } else if (mDragInProgress) {
874 mDeleteFolderOnDropCompleted = true;
875 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700876 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700877 mSuppressFolderDeletion = false;
Adam Cohenafb01ee2011-06-23 15:38:03 -0700878 }
879
880 private void replaceFolderWithFinalItem() {
Adam Cohenafb01ee2011-06-23 15:38:03 -0700881 // Add the last remaining child to the workspace in place of the folder
Adam Cohenfb91f302012-06-11 15:45:18 -0700882 Runnable onCompleteRunnable = new Runnable() {
883 @Override
884 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -0700885 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screenId);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700886
Winson Chung33231f52013-12-09 16:57:45 -0800887 View child = null;
Adam Cohenfb91f302012-06-11 15:45:18 -0700888 // Move the item from the folder to the workspace, in the position of the folder
889 if (getItemCount() == 1) {
890 ShortcutInfo finalItem = mInfo.contents.get(0);
Adam Cohenc5e63f32012-07-12 16:16:57 -0700891 child = mLauncher.createShortcut(R.layout.application, cellLayout,
Adam Cohenfb91f302012-06-11 15:45:18 -0700892 finalItem);
893 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700894 mInfo.screenId, mInfo.cellX, mInfo.cellY);
Adam Cohenfb91f302012-06-11 15:45:18 -0700895 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700896 if (getItemCount() <= 1) {
897 // Remove the folder
898 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
Dan Sandler0eb687f2014-01-10 13:24:55 -0500899 if (cellLayout != null) {
900 // b/12446428 -- sometimes the cell layout has already gone away?
901 cellLayout.removeView(mFolderIcon);
902 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700903 if (mFolderIcon instanceof DropTarget) {
904 mDragController.removeDropTarget((DropTarget) mFolderIcon);
905 }
906 mLauncher.removeFolder(mInfo);
907 }
Adam Cohenc5e63f32012-07-12 16:16:57 -0700908 // We add the child after removing the folder to prevent both from existing at
Winson Chung0e6a7132013-08-23 12:55:10 -0700909 // the same time in the CellLayout. We need to add the new item with addInScreenFromBind()
910 // to ensure that hotseat items are placed correctly.
Adam Cohenc5e63f32012-07-12 16:16:57 -0700911 if (child != null) {
Winson Chung0e6a7132013-08-23 12:55:10 -0700912 mLauncher.getWorkspace().addInScreenFromBind(child, mInfo.container, mInfo.screenId,
Adam Cohenc5e63f32012-07-12 16:16:57 -0700913 mInfo.cellX, mInfo.cellY, mInfo.spanX, mInfo.spanY);
914 }
Adam Cohenfb91f302012-06-11 15:45:18 -0700915 }
916 };
Sunny Goyalbc753352015-03-05 09:40:44 -0800917 View finalChild = mContent.getLastItem();
Adam Cohenfb91f302012-06-11 15:45:18 -0700918 if (finalChild != null) {
919 mFolderIcon.performDestroyAnimation(finalChild, onCompleteRunnable);
Winson Chung33231f52013-12-09 16:57:45 -0800920 } else {
921 onCompleteRunnable.run();
Adam Cohenafb01ee2011-06-23 15:38:03 -0700922 }
Adam Cohenfb91f302012-06-11 15:45:18 -0700923 mDestroyed = true;
924 }
925
926 boolean isDestroyed() {
927 return mDestroyed;
Adam Cohen2801caf2011-05-13 20:57:39 -0700928 }
929
Adam Cohenac56cff2011-09-28 20:45:37 -0700930 // This method keeps track of the last item in the folder for the purposes
931 // of keyboard focus
932 private void updateTextViewFocus() {
Sunny Goyalbc753352015-03-05 09:40:44 -0800933 View lastChild = mContent.getLastItem();
Adam Cohenac56cff2011-09-28 20:45:37 -0700934 if (lastChild != null) {
935 mFolderName.setNextFocusDownId(lastChild.getId());
936 mFolderName.setNextFocusRightId(lastChild.getId());
937 mFolderName.setNextFocusLeftId(lastChild.getId());
938 mFolderName.setNextFocusUpId(lastChild.getId());
939 }
940 }
941
Adam Cohenbfbfd262011-06-13 16:55:12 -0700942 public void onDrop(DragObject d) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800943 Runnable cleanUpRunnable = null;
944
Adam Cohen689ff162014-05-08 17:27:56 -0700945 // If we are coming from All Apps space, we defer removing the extra empty screen
946 // until the folder closes
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800947 if (d.dragSource != mLauncher.getWorkspace() && !(d.dragSource instanceof Folder)) {
948 cleanUpRunnable = new Runnable() {
949 @Override
950 public void run() {
Adam Cohen689ff162014-05-08 17:27:56 -0700951 mLauncher.exitSpringLoadedDragModeDelayed(true,
952 Launcher.EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
953 null);
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800954 }
955 };
Adam Cohenbfbfd262011-06-13 16:55:12 -0700956 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800957
958 View currentDragView;
959 ShortcutInfo si = mCurrentDragInfo;
960 if (mIsExternalDrag) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800961 currentDragView = mContent.createAndAddViewForRank(si, mEmptyCellRank);
Sunny Goyal95abbb32014-08-04 10:53:22 -0700962 // Actually move the item in the database if it was an external drag. Call this
963 // before creating the view, so that ShortcutInfo is updated appropriately.
964 LauncherModel.addOrMoveItemInDatabase(
965 mLauncher, si, mInfo.id, 0, si.cellX, si.cellY);
966
967 // We only need to update the locations if it doesn't get handled in #onDropCompleted.
968 if (d.dragSource != this) {
969 updateItemLocationsInDatabaseBatch();
970 }
971 mIsExternalDrag = false;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800972 } else {
973 currentDragView = mCurrentDragView;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800974 mContent.addViewForRank(currentDragView, si, mEmptyCellRank);
Adam Cohenbfbfd262011-06-13 16:55:12 -0700975 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800976
977 if (d.dragView.hasDrawn()) {
978
979 // Temporarily reset the scale such that the animation target gets calculated correctly.
980 float scaleX = getScaleX();
981 float scaleY = getScaleY();
982 setScaleX(1.0f);
983 setScaleY(1.0f);
984 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, currentDragView,
985 cleanUpRunnable, null);
986 setScaleX(scaleX);
987 setScaleY(scaleY);
988 } else {
989 d.deferDragViewCleanupPostAnimation = false;
990 currentDragView.setVisibility(VISIBLE);
991 }
992 mItemsInvalidated = true;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800993 rearrangeChildren();
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800994
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800995 // Temporarily suppress the listener, as we did all the work already here.
996 mSuppressOnAdd = true;
997 mInfo.add(si);
998 mSuppressOnAdd = false;
Sunny Goyal4b020172014-08-28 14:51:14 -0700999 // Clear the drag info, as it is no longer being dragged.
1000 mCurrentDragInfo = null;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001001 }
1002
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001003 // This is used so the item doesn't immediately appear in the folder when added. In one case
1004 // we need to create the illusion that the item isn't added back to the folder yet, to
1005 // to correspond to the animation of the icon back into the folder. This is
1006 public void hideItem(ShortcutInfo info) {
1007 View v = getViewForInfo(info);
1008 v.setVisibility(INVISIBLE);
1009 }
1010 public void showItem(ShortcutInfo info) {
1011 View v = getViewForInfo(info);
1012 v.setVisibility(VISIBLE);
1013 }
1014
Adam Cohenbfbfd262011-06-13 16:55:12 -07001015 public void onAdd(ShortcutInfo item) {
1016 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001017 // If the item was dropped onto this open folder, we have done the work associated
1018 // with adding the item to the folder, as indicated by mSuppressOnAdd being set
Adam Cohenbfbfd262011-06-13 16:55:12 -07001019 if (mSuppressOnAdd) return;
Sunny Goyalbc753352015-03-05 09:40:44 -08001020 mContent.createAndAddViewForRank(item, mContent.allocateNewLastItemRank());
Adam Cohenbfbfd262011-06-13 16:55:12 -07001021 LauncherModel.addOrMoveItemInDatabase(
1022 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
1023 }
1024
Adam Cohena9cf38f2011-05-02 15:36:58 -07001025 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -07001026 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001027 // If this item is being dragged from this open folder, we have already handled
1028 // the work associated with removing the item, so we don't have to do anything here.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001029 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001030 View v = getViewForInfo(item);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001031 mContent.removeView(v);
Adam Cohen2801caf2011-05-13 20:57:39 -07001032 if (mState == STATE_ANIMATING) {
1033 mRearrangeOnClose = true;
1034 } else {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001035 rearrangeChildren();
Adam Cohen2801caf2011-05-13 20:57:39 -07001036 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001037 if (getItemCount() <= 1) {
1038 replaceFolderWithFinalItem();
1039 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001040 }
Adam Cohen7c693212011-05-18 15:26:57 -07001041
Sunny Goyalbc753352015-03-05 09:40:44 -08001042 private View getViewForInfo(final ShortcutInfo item) {
1043 return mContent.iterateOverItems(new ItemOperator() {
1044
1045 @Override
1046 public boolean evaluate(ItemInfo info, View view, View parent) {
1047 return info == item;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001048 }
Sunny Goyalbc753352015-03-05 09:40:44 -08001049 });
Adam Cohendf1e4e82011-06-24 15:57:39 -07001050 }
1051
Adam Cohen76078c42011-06-09 15:06:52 -07001052 public void onItemsChanged() {
Adam Cohenac56cff2011-09-28 20:45:37 -07001053 updateTextViewFocus();
Adam Cohen76078c42011-06-09 15:06:52 -07001054 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001055
Adam Cohen76fc0852011-06-17 13:26:23 -07001056 public void onTitleChanged(CharSequence title) {
1057 }
Adam Cohen76078c42011-06-09 15:06:52 -07001058
Adam Cohen7c693212011-05-18 15:26:57 -07001059 public ArrayList<View> getItemsInReadingOrder() {
1060 if (mItemsInvalidated) {
1061 mItemsInReadingOrder.clear();
Sunny Goyalbc753352015-03-05 09:40:44 -08001062 mContent.iterateOverItems(new ItemOperator() {
1063
1064 @Override
1065 public boolean evaluate(ItemInfo info, View view, View parent) {
1066 mItemsInReadingOrder.add(view);
1067 return false;
Adam Cohen7c693212011-05-18 15:26:57 -07001068 }
Sunny Goyalbc753352015-03-05 09:40:44 -08001069 });
Adam Cohen7c693212011-05-18 15:26:57 -07001070 mItemsInvalidated = false;
1071 }
1072 return mItemsInReadingOrder;
1073 }
Adam Cohen8dfcba42011-07-07 16:38:18 -07001074
1075 public void getLocationInDragLayer(int[] loc) {
1076 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
1077 }
Adam Cohenea0818d2011-09-30 20:06:58 -07001078
1079 public void onFocusChange(View v, boolean hasFocus) {
1080 if (v == mFolderName && hasFocus) {
1081 startEditingFolderName();
1082 }
1083 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001084
1085 @Override
1086 public void getHitRectRelativeToDragLayer(Rect outRect) {
1087 getHitRect(outRect);
1088 }
Sunny Goyalbc753352015-03-05 09:40:44 -08001089
1090 public static interface FolderContent {
1091 void setFolder(Folder f);
1092
1093 void removeView(View v);
1094
1095 boolean isFull();
1096 int getItemCount();
1097
1098 int getDesiredWidth();
1099 int getDesiredHeight();
1100 void setFixedSize(int width, int height);
1101
1102 /**
1103 * Iterates over all its items in a reading order.
1104 * @return the view for which the operator returned true.
1105 */
1106 View iterateOverItems(ItemOperator op);
1107 View getLastItem();
1108
1109 String getAccessibilityDescription();
1110
1111 /**
1112 * Binds items to the layout.
1113 * @return list of items that could not be bound, probably because we hit the max size limit.
1114 */
1115 ArrayList<ShortcutInfo> bindItems(ArrayList<ShortcutInfo> children);
1116
1117 /**
1118 * Create space for a new item at the end, and returns the rank for that item.
1119 * Resizes the content if necessary.
1120 */
1121 int allocateNewLastItemRank();
1122
1123 View createAndAddViewForRank(ShortcutInfo item, int rank);
1124
1125 /**
1126 * Adds the {@param view} to the layout based on {@param rank} and updated the position
1127 * related attributes. It assumes that {@param item} is already attached to the view.
1128 */
1129 void addViewForRank(View view, ShortcutInfo item, int rank);
1130
1131 /**
1132 * Reorders the items such that the {@param empty} spot moves to {@param target}
1133 */
1134 void realTimeReorder(int empty, int target);
1135
1136 /**
1137 * @return the rank of the cell nearest to the provided pixel position.
1138 */
1139 int findNearestArea(int pixelX, int pixelY);
1140
1141 /**
1142 * Updates position and rank of all the children in the view based.
1143 * @param list the ordered list of children.
1144 * @param itemCount if greater than the total children count, empty spaces are left
1145 * at the end.
1146 */
1147 void arrangeChildren(ArrayList<View> list, int itemCount);
1148
1149 /**
1150 * Sets the focus on the first visible child.
1151 */
1152 void setFocusOnFirstChild();
1153 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001154}