blob: 0a65579071dbf49a70aa600ad0de72ec2fd28d81 [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;
Sunny Goyal290800b2015-03-05 11:33:33 -080069 private static final boolean ALLOW_FOLDER_SCROLL = true;
Adam Cohen4eac29a2011-07-11 17:53:37 -070070
Adam Cohendf2cc412011-04-27 16:56:57 -070071 static final int STATE_NONE = -1;
72 static final int STATE_SMALL = 0;
73 static final int STATE_ANIMATING = 1;
74 static final int STATE_OPEN = 2;
75
Adam Cohenf0f4eda2013-06-06 21:27:03 -070076 private static final int REORDER_DELAY = 250;
Adam Cohen5d518fa2013-12-05 14:16:23 -080077 private static final int ON_EXIT_CLOSE_DELAY = 400;
Sunny Goyalc3a609f2015-02-26 17:43:50 -080078 private static final Rect sTempRect = new Rect();
79
80 private static String sDefaultFolderName;
81 private static String sHintText;
82
83 private final Alarm mReorderAlarm = new Alarm();
84 private final Alarm mOnExitAlarm = new Alarm();
85
86 private final ArrayList<View> mItemsInReadingOrder = new ArrayList<View>();
87
88 private final int mExpandDuration;
89 private final int mMaterialExpandDuration;
90 private final int mMaterialExpandStagger;
91
92 private final InputMethodManager mInputMethodManager;
93
94 protected final Launcher mLauncher;
95 protected DragController mDragController;
96 protected FolderInfo mInfo;
97
Adam Cohen2801caf2011-05-13 20:57:39 -070098 private FolderIcon mFolderIcon;
Sunny Goyalc3a609f2015-02-26 17:43:50 -080099
Sunny Goyalbc753352015-03-05 09:40:44 -0800100 private FolderContent mContent;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800101 private View mContentWrapper;
102 FolderEditText mFolderName;
103
Sunny Goyal290800b2015-03-05 11:33:33 -0800104 private View mFooter;
105 private int mFooterHeight;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800106
107 // Cell ranks used for drag and drop
108 private int mTargetRank, mPrevTargetRank, mEmptyCellRank;
109
110 private int mState = STATE_NONE;
111 private boolean mRearrangeOnClose = false;
Adam Cohen7c693212011-05-18 15:26:57 -0700112 boolean mItemsInvalidated = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700113 private ShortcutInfo mCurrentDragInfo;
114 private View mCurrentDragView;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800115 private boolean mIsExternalDrag;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700116 boolean mSuppressOnAdd = false;
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700117 private boolean mDragInProgress = false;
118 private boolean mDeleteFolderOnDropCompleted = false;
119 private boolean mSuppressFolderDeletion = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700120 private boolean mItemAddedBackToSelfViaIcon = false;
Adam Cohen268c4752012-06-06 17:47:33 -0700121 private float mFolderIconPivotX;
122 private float mFolderIconPivotY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700123 private boolean mIsEditingName = false;
Adam Cohen1960ea42013-11-12 11:33:14 +0000124
Adam Cohenfb91f302012-06-11 15:45:18 -0700125 private boolean mDestroyed;
126
Michael Jurka1e2f4652013-07-08 18:03:46 -0700127 private Runnable mDeferredAction;
128 private boolean mDeferDropAfterUninstall;
129 private boolean mUninstallSuccessful;
130
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131 /**
132 * Used to inflate the Workspace from XML.
133 *
134 * @param context The application's context.
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800135 * @param attrs The attributes set containing the Workspace's customization values.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136 */
137 public Folder(Context context, AttributeSet attrs) {
138 super(context, attrs);
139 setAlwaysDrawnWithCacheEnabled(false);
Adam Cohen76fc0852011-06-17 13:26:23 -0700140 mInputMethodManager = (InputMethodManager)
Michael Jurka8b805b12012-04-18 14:23:14 -0700141 getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
Adam Cohen76fc0852011-06-17 13:26:23 -0700142
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800143 Resources res = getResources();
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700144 mExpandDuration = res.getInteger(R.integer.config_folderExpandDuration);
145 mMaterialExpandDuration = res.getInteger(R.integer.config_materialFolderExpandDuration);
146 mMaterialExpandStagger = res.getInteger(R.integer.config_materialFolderExpandStagger);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700147
148 if (sDefaultFolderName == null) {
149 sDefaultFolderName = res.getString(R.string.folder_name);
150 }
Adam Cohena65beee2011-06-27 21:32:23 -0700151 if (sHintText == null) {
152 sHintText = res.getString(R.string.folder_hint_text);
153 }
Adam Cohen4eac29a2011-07-11 17:53:37 -0700154 mLauncher = (Launcher) context;
Adam Cohenac56cff2011-09-28 20:45:37 -0700155 // We need this view to be focusable in touch mode so that when text editing of the folder
156 // name is complete, we have something to focus on, thus hiding the cursor and giving
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800157 // reliable behavior when clicking the text field (since it will always gain focus on click).
Adam Cohenac56cff2011-09-28 20:45:37 -0700158 setFocusableInTouchMode(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159 }
160
161 @Override
162 protected void onFinishInflate() {
163 super.onFinishInflate();
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800164 mContentWrapper = findViewById(R.id.folder_content_wrapper);
Sunny Goyalbc753352015-03-05 09:40:44 -0800165 mContent = (FolderContent) findViewById(R.id.folder_content);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800166 mContent.setFolder(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700167
Adam Cohenac56cff2011-09-28 20:45:37 -0700168 mFolderName = (FolderEditText) findViewById(R.id.folder_name);
169 mFolderName.setFolder(this);
Adam Cohenea0818d2011-09-30 20:06:58 -0700170 mFolderName.setOnFocusChangeListener(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700171
Adam Cohen76fc0852011-06-17 13:26:23 -0700172 // We disable action mode for now since it messes up the view on phones
173 mFolderName.setCustomSelectionActionModeCallback(mActionModeCallback);
Adam Cohen76fc0852011-06-17 13:26:23 -0700174 mFolderName.setOnEditorActionListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700175 mFolderName.setSelectAllOnFocus(true);
Adam Cohen7a14d0b2011-06-24 11:36:35 -0700176 mFolderName.setInputType(mFolderName.getInputType() |
177 InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800178
Sunny Goyal290800b2015-03-05 11:33:33 -0800179 mFooter = ALLOW_FOLDER_SCROLL ? findViewById(R.id.folder_footer) : mFolderName;
180 // We find out how tall footer wants to be (it is set to wrap_content), so that
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800181 // we can allocate the appropriate amount of space for it.
182 int measureSpec = MeasureSpec.UNSPECIFIED;
Sunny Goyal290800b2015-03-05 11:33:33 -0800183 mFooter.measure(measureSpec, measureSpec);
184 mFooterHeight = mFooter.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
Sunny Goyal290800b2015-03-05 11:33:33 -0800228 mContent.removeItem(mCurrentDragView);
Adam Cohenfc53cd22011-07-20 15:45:11 -0700229 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) {
Sunny Goyal290800b2015-03-05 11:33:33 -0800362 return (Folder) LayoutInflater.from(context).inflate(
363 ALLOW_FOLDER_SCROLL ? R.layout.user_folder_scroll : R.layout.user_folder, null);
Adam Cohendf2cc412011-04-27 16:56:57 -0700364 }
365
366 /**
367 * This method is intended to make the UserFolder to be visually identical in size and position
368 * to its associated FolderIcon. This allows for a seamless transition into the expanded state.
369 */
370 private void positionAndSizeAsIcon() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700371 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800372 setScaleX(0.8f);
373 setScaleY(0.8f);
374 setAlpha(0f);
Adam Cohendf2cc412011-04-27 16:56:57 -0700375 mState = STATE_SMALL;
376 }
377
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700378 private void prepareReveal() {
379 setScaleX(1f);
380 setScaleY(1f);
381 setAlpha(1f);
382 mState = STATE_SMALL;
383 }
384
Adam Cohendf2cc412011-04-27 16:56:57 -0700385 public void animateOpen() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700386 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen6441de02011-12-14 14:25:32 -0800387
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700388 Animator openFolderAnim = null;
389 final Runnable onCompleteRunnable;
Kenny Guyd794a3f2014-09-16 15:17:58 +0100390 if (!Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700391 positionAndSizeAsIcon();
392 centerAboutIcon();
393
394 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1);
395 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
396 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
397 final ObjectAnimator oa =
398 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
399 oa.setDuration(mExpandDuration);
400 openFolderAnim = oa;
401
402 setLayerType(LAYER_TYPE_HARDWARE, null);
403 onCompleteRunnable = new Runnable() {
404 @Override
405 public void run() {
406 setLayerType(LAYER_TYPE_NONE, null);
407 }
408 };
409 } else {
410 prepareReveal();
411 centerAboutIcon();
412
413 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
414 int height = getFolderHeight();
415
416 float transX = - 0.075f * (width / 2 - getPivotX());
417 float transY = - 0.075f * (height / 2 - getPivotY());
418 setTranslationX(transX);
419 setTranslationY(transY);
420 PropertyValuesHolder tx = PropertyValuesHolder.ofFloat("translationX", transX, 0);
421 PropertyValuesHolder ty = PropertyValuesHolder.ofFloat("translationY", transY, 0);
422
423 int rx = (int) Math.max(Math.max(width - getPivotX(), 0), getPivotX());
424 int ry = (int) Math.max(Math.max(height - getPivotY(), 0), getPivotY());
425 float radius = (float) Math.sqrt(rx * rx + ry * ry);
426 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
427 Animator reveal = LauncherAnimUtils.createCircularReveal(this, (int) getPivotX(),
428 (int) getPivotY(), 0, radius);
429 reveal.setDuration(mMaterialExpandDuration);
430 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
431
Sunny Goyalbc753352015-03-05 09:40:44 -0800432 mContentWrapper.setAlpha(0f);
433 Animator iconsAlpha = LauncherAnimUtils.ofFloat(mContentWrapper, "alpha", 0f, 1f);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700434 iconsAlpha.setDuration(mMaterialExpandDuration);
435 iconsAlpha.setStartDelay(mMaterialExpandStagger);
436 iconsAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
437
Sunny Goyal290800b2015-03-05 11:33:33 -0800438 mFooter.setAlpha(0f);
439 Animator textAlpha = LauncherAnimUtils.ofFloat(mFooter, "alpha", 0f, 1f);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700440 textAlpha.setDuration(mMaterialExpandDuration);
441 textAlpha.setStartDelay(mMaterialExpandStagger);
442 textAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
443
444 Animator drift = LauncherAnimUtils.ofPropertyValuesHolder(this, tx, ty);
445 drift.setDuration(mMaterialExpandDuration);
446 drift.setStartDelay(mMaterialExpandStagger);
447 drift.setInterpolator(new LogDecelerateInterpolator(60, 0));
448
449 anim.play(drift);
450 anim.play(iconsAlpha);
451 anim.play(textAlpha);
452 anim.play(reveal);
453
454 openFolderAnim = anim;
455
Sunny Goyalbc753352015-03-05 09:40:44 -0800456 mContentWrapper.setLayerType(LAYER_TYPE_HARDWARE, null);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700457 onCompleteRunnable = new Runnable() {
458 @Override
459 public void run() {
Sunny Goyalbc753352015-03-05 09:40:44 -0800460 mContentWrapper.setLayerType(LAYER_TYPE_NONE, null);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700461 }
462 };
463 }
464 openFolderAnim.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700465 @Override
466 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700467 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Sunny Goyalbc753352015-03-05 09:40:44 -0800468 mContent.getAccessibilityDescription());
Adam Cohendf2cc412011-04-27 16:56:57 -0700469 mState = STATE_ANIMATING;
470 }
471 @Override
472 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700473 mState = STATE_OPEN;
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700474
475 if (onCompleteRunnable != null) {
476 onCompleteRunnable.run();
477 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800478
Sunny Goyalbc753352015-03-05 09:40:44 -0800479 mContent.setFocusOnFirstChild();
Adam Cohendf2cc412011-04-27 16:56:57 -0700480 }
481 });
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700482 openFolderAnim.start();
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800483
484 // Make sure the folder picks up the last drag move even if the finger doesn't move.
485 if (mDragController.isDragging()) {
486 mDragController.forceTouchMove();
487 }
488 }
489
490 public void beginExternalDrag(ShortcutInfo item) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800491 mCurrentDragInfo = item;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800492 mEmptyCellRank = mContent.allocateNewLastItemRank();
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800493 mIsExternalDrag = true;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800494 mDragInProgress = true;
Adam Cohendf2cc412011-04-27 16:56:57 -0700495 }
496
Adam Cohen3371da02011-10-25 21:38:29 -0700497 private void sendCustomAccessibilityEvent(int type, String text) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700498 AccessibilityManager accessibilityManager = (AccessibilityManager)
499 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
500 if (accessibilityManager.isEnabled()) {
Adam Cohen3371da02011-10-25 21:38:29 -0700501 AccessibilityEvent event = AccessibilityEvent.obtain(type);
502 onInitializeAccessibilityEvent(event);
503 event.getText().add(text);
Michael Jurka8b805b12012-04-18 14:23:14 -0700504 accessibilityManager.sendAccessibilityEvent(event);
Adam Cohen3371da02011-10-25 21:38:29 -0700505 }
506 }
507
Adam Cohendf2cc412011-04-27 16:56:57 -0700508 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700509 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800510 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
511 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
512 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
Michael Jurka032e6ba2013-04-22 15:08:42 +0200513 final ObjectAnimator oa =
Michael Jurka2ecf9952012-06-18 12:52:28 -0700514 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohendf2cc412011-04-27 16:56:57 -0700515
Adam Cohen2801caf2011-05-13 20:57:39 -0700516 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700517 @Override
518 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700519 onCloseComplete();
Michael Jurka0121c3e2012-05-31 08:36:04 -0700520 setLayerType(LAYER_TYPE_NONE, null);
Adam Cohen2801caf2011-05-13 20:57:39 -0700521 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700522 }
523 @Override
524 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700525 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700526 getContext().getString(R.string.folder_closed));
Adam Cohendf2cc412011-04-27 16:56:57 -0700527 mState = STATE_ANIMATING;
528 }
529 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700530 oa.setDuration(mExpandDuration);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700531 setLayerType(LAYER_TYPE_HARDWARE, null);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100532 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700533 }
534
Adam Cohencb3382b2011-05-24 14:07:08 -0700535 public boolean acceptDrop(DragObject d) {
536 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700537 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700538 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
539 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
540 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700541 }
542
Adam Cohencb3382b2011-05-24 14:07:08 -0700543 public void onDragEnter(DragObject d) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800544 mPrevTargetRank = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700545 mOnExitAlarm.cancelAlarm();
546 }
547
548 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
549 public void onAlarm(Alarm alarm) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800550 mContent.realTimeReorder(mEmptyCellRank, mTargetRank);
551 mEmptyCellRank = mTargetRank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700552 }
553 };
554
Sunny Goyalc46bfef2015-01-05 12:40:08 -0800555 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohen2374abf2013-04-16 14:56:57 -0700556 public boolean isLayoutRtl() {
557 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
558 }
559
Adam Cohencb3382b2011-05-24 14:07:08 -0700560 public void onDragOver(DragObject d) {
Sunny Goyal1587d532015-01-29 09:57:16 -0800561 final float[] r = d.getVisualCenter(null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700562 r[0] -= getPaddingLeft();
563 r[1] -= getPaddingTop();
564
Sunny Goyalbc753352015-03-05 09:40:44 -0800565 mTargetRank = mContent.findNearestArea((int) r[0], (int) r[1]);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800566 if (mTargetRank != mPrevTargetRank) {
Alan Viverette4cda5b72013-08-28 17:53:41 -0700567 mReorderAlarm.cancelAlarm();
Sunny Goyalc46bfef2015-01-05 12:40:08 -0800568 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
569 mReorderAlarm.setAlarm(REORDER_DELAY);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800570 mPrevTargetRank = mTargetRank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700571 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700572 }
573
Adam Cohenbfbfd262011-06-13 16:55:12 -0700574 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
575 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700576 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700577 }
578 };
579
Adam Cohen95bb8002011-07-03 23:40:28 -0700580 public void completeDragExit() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700581 mLauncher.closeFolder();
582 mCurrentDragInfo = null;
583 mCurrentDragView = null;
584 mSuppressOnAdd = false;
585 mRearrangeOnClose = true;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800586 mIsExternalDrag = false;
Adam Cohen3e8f8112011-07-02 18:03:00 -0700587 }
588
Adam Cohencb3382b2011-05-24 14:07:08 -0700589 public void onDragExit(DragObject d) {
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700590 // We only close the folder if this is a true drag exit, ie. not because
591 // a drop has occurred above the folder.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700592 if (!d.dragComplete) {
593 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
594 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
595 }
596 mReorderAlarm.cancelAlarm();
Adam Cohen2801caf2011-05-13 20:57:39 -0700597 }
598
Michael Jurka1e2f4652013-07-08 18:03:46 -0700599 public void onDropCompleted(final View target, final DragObject d,
600 final boolean isFlingToDelete, final boolean success) {
601 if (mDeferDropAfterUninstall) {
Michael Jurkaf3007582013-08-21 14:33:57 +0200602 Log.d(TAG, "Deferred handling drop because waiting for uninstall.");
Michael Jurka1e2f4652013-07-08 18:03:46 -0700603 mDeferredAction = new Runnable() {
604 public void run() {
605 onDropCompleted(target, d, isFlingToDelete, success);
606 mDeferredAction = null;
607 }
608 };
609 return;
610 }
611
612 boolean beingCalledAfterUninstall = mDeferredAction != null;
613 boolean successfulDrop =
614 success && (!beingCalledAfterUninstall || mUninstallSuccessful);
Winson Chung5f8afe62013-08-12 16:19:28 -0700615
Michael Jurka1e2f4652013-07-08 18:03:46 -0700616 if (successfulDrop) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800617 if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon && target != this) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700618 replaceFolderWithFinalItem();
619 }
620 } else {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800621 rearrangeChildren();
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700622 // The drag failed, we need to return the item to the folder
623 mFolderIcon.onDrop(d);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700624 }
625
626 if (target != this) {
627 if (mOnExitAlarm.alarmPending()) {
628 mOnExitAlarm.cancelAlarm();
Michael Jurka54554252013-08-01 12:52:23 +0200629 if (!successfulDrop) {
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700630 mSuppressFolderDeletion = true;
631 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700632 completeDragExit();
633 }
Adam Cohen9c58d822013-12-13 17:18:10 -0800634 }
635
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700636 mDeleteFolderOnDropCompleted = false;
637 mDragInProgress = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700638 mItemAddedBackToSelfViaIcon = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700639 mCurrentDragInfo = null;
640 mCurrentDragView = null;
641 mSuppressOnAdd = false;
Adam Cohen4045eb72011-10-06 11:44:26 -0700642
643 // Reordering may have occured, and we need to save the new item locations. We do this once
644 // at the end to prevent unnecessary database operations.
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700645 updateItemLocationsInDatabaseBatch();
Adam Cohen4045eb72011-10-06 11:44:26 -0700646 }
647
Michael Jurka1e2f4652013-07-08 18:03:46 -0700648 public void deferCompleteDropAfterUninstallActivity() {
649 mDeferDropAfterUninstall = true;
650 }
651
652 public void onUninstallActivityReturned(boolean success) {
653 mDeferDropAfterUninstall = false;
654 mUninstallSuccessful = success;
655 if (mDeferredAction != null) {
656 mDeferredAction.run();
657 }
658 }
659
Winson Chunga48487a2012-03-20 16:19:37 -0700660 @Override
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800661 public float getIntrinsicIconScaleFactor() {
662 return 1f;
663 }
664
665 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800666 public boolean supportsFlingToDelete() {
667 return true;
668 }
669
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000670 @Override
671 public boolean supportsAppInfoDropTarget() {
672 return false;
673 }
674
675 @Override
676 public boolean supportsDeleteDropTarget() {
677 return true;
678 }
679
Winson Chunga48487a2012-03-20 16:19:37 -0700680 public void onFlingToDelete(DragObject d, int x, int y, PointF vec) {
681 // Do nothing
682 }
683
684 @Override
685 public void onFlingToDeleteCompleted() {
686 // Do nothing
687 }
688
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700689 private void updateItemLocationsInDatabaseBatch() {
690 ArrayList<View> list = getItemsInReadingOrder();
691 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
692 for (int i = 0; i < list.size(); i++) {
693 View v = list.get(i);
694 ItemInfo info = (ItemInfo) v.getTag();
Sunny Goyal08f72612015-01-05 13:41:43 -0800695 info.rank = i;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700696 items.add(info);
697 }
698
699 LauncherModel.moveItemsInDatabase(mLauncher, items, mInfo.id, 0);
700 }
701
Adam Cohene25af792013-06-06 23:08:25 -0700702 public void addItemLocationsInDatabase() {
703 ArrayList<View> list = getItemsInReadingOrder();
704 for (int i = 0; i < list.size(); i++) {
705 View v = list.get(i);
706 ItemInfo info = (ItemInfo) v.getTag();
707 LauncherModel.addItemToDatabase(mLauncher, info, mInfo.id, 0,
708 info.cellX, info.cellY, false);
709 }
710 }
711
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700712 public void notifyDrop() {
713 if (mDragInProgress) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700714 mItemAddedBackToSelfViaIcon = true;
Adam Cohen76078c42011-06-09 15:06:52 -0700715 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700716 }
717
718 public boolean isDropEnabled() {
719 return true;
720 }
721
Adam Cohen2801caf2011-05-13 20:57:39 -0700722 public boolean isFull() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800723 return mContent.isFull();
Adam Cohen2801caf2011-05-13 20:57:39 -0700724 }
725
726 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700727 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700728
Winson Chung892c74d2013-08-22 16:15:50 -0700729 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700730 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700731 int height = getFolderHeight();
Adam Cohen2801caf2011-05-13 20:57:39 -0700732
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800733 float scale = parent.getDescendantRectRelativeToSelf(mFolderIcon, sTempRect);
Adam Cohen8e776a62011-06-28 18:10:06 -0700734
Winson Chungaf40f202013-09-18 18:26:31 -0700735 LauncherAppState app = LauncherAppState.getInstance();
736 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
737
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800738 int centerX = (int) (sTempRect.left + sTempRect.width() * scale / 2);
739 int centerY = (int) (sTempRect.top + sTempRect.height() * scale / 2);
Adam Cohen2801caf2011-05-13 20:57:39 -0700740 int centeredLeft = centerX - width / 2;
741 int centeredTop = centerY - height / 2;
Winson Chung3057b1c2013-10-10 17:35:35 -0700742 int currentPage = mLauncher.getWorkspace().getNextPage();
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800743 // In case the workspace is scrolling, we need to use the final scroll to compute
744 // the folders bounds.
745 mLauncher.getWorkspace().setFinalScrollForPageChange(currentPage);
Adam Cohen35e7e642011-07-17 14:47:18 -0700746 // We first fetch the currently visible CellLayoutChildren
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800747 CellLayout currentLayout = (CellLayout) mLauncher.getWorkspace().getChildAt(currentPage);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700748 ShortcutAndWidgetContainer boundingLayout = currentLayout.getShortcutsAndWidgets();
Adam Cohen35e7e642011-07-17 14:47:18 -0700749 Rect bounds = new Rect();
750 parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800751 // We reset the workspaces scroll
752 mLauncher.getWorkspace().resetFinalScrollForPageChange(currentPage);
Adam Cohen2801caf2011-05-13 20:57:39 -0700753
Adam Cohen35e7e642011-07-17 14:47:18 -0700754 // We need to bound the folder to the currently visible CellLayoutChildren
755 int left = Math.min(Math.max(bounds.left, centeredLeft),
756 bounds.left + bounds.width() - width);
757 int top = Math.min(Math.max(bounds.top, centeredTop),
758 bounds.top + bounds.height() - height);
Winson Chungaf40f202013-09-18 18:26:31 -0700759 if (grid.isPhone() && (grid.availableWidthPx - width) < grid.iconSizePx) {
760 // Center the folder if it is full (on phones only)
761 left = (grid.availableWidthPx - width) / 2;
762 } else if (width >= bounds.width()) {
763 // If the folder doesn't fit within the bounds, center it about the desired bounds
Adam Cohen35e7e642011-07-17 14:47:18 -0700764 left = bounds.left + (bounds.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700765 }
Adam Cohen35e7e642011-07-17 14:47:18 -0700766 if (height >= bounds.height()) {
767 top = bounds.top + (bounds.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700768 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700769
770 int folderPivotX = width / 2 + (centeredLeft - left);
771 int folderPivotY = height / 2 + (centeredTop - top);
772 setPivotX(folderPivotX);
773 setPivotY(folderPivotY);
Adam Cohen268c4752012-06-06 17:47:33 -0700774 mFolderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700775 (1.0f * folderPivotX / width));
Adam Cohen268c4752012-06-06 17:47:33 -0700776 mFolderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700777 (1.0f * folderPivotY / height));
Adam Cohen3bf84d32012-05-07 20:17:14 -0700778
Adam Cohen662b5982011-12-13 17:45:21 -0800779 lp.width = width;
780 lp.height = height;
781 lp.x = left;
782 lp.y = top;
Adam Cohen2801caf2011-05-13 20:57:39 -0700783 }
784
Adam Cohen268c4752012-06-06 17:47:33 -0700785 float getPivotXForIconAnimation() {
786 return mFolderIconPivotX;
787 }
788 float getPivotYForIconAnimation() {
789 return mFolderIconPivotY;
790 }
791
Winson Chung892c74d2013-08-22 16:15:50 -0700792 private int getContentAreaHeight() {
793 LauncherAppState app = LauncherAppState.getInstance();
794 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
795 Rect workspacePadding = grid.getWorkspacePadding(grid.isLandscape ?
796 CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
797 int maxContentAreaHeight = grid.availableHeightPx -
Winson Chung892c74d2013-08-22 16:15:50 -0700798 workspacePadding.top - workspacePadding.bottom -
Sunny Goyal290800b2015-03-05 11:33:33 -0800799 mFooterHeight;
Adam Cohen1960ea42013-11-12 11:33:14 +0000800 int height = Math.min(maxContentAreaHeight,
Winson Chung892c74d2013-08-22 16:15:50 -0700801 mContent.getDesiredHeight());
Adam Cohen1960ea42013-11-12 11:33:14 +0000802 return Math.max(height, MIN_CONTENT_DIMEN);
803 }
804
805 private int getContentAreaWidth() {
806 return Math.max(mContent.getDesiredWidth(), MIN_CONTENT_DIMEN);
Winson Chung892c74d2013-08-22 16:15:50 -0700807 }
808
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700809 private int getFolderHeight() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800810 return getFolderHeight(getContentAreaHeight());
811 }
812
813 private int getFolderHeight(int contentAreaHeight) {
Sunny Goyal290800b2015-03-05 11:33:33 -0800814 return getPaddingTop() + getPaddingBottom() + contentAreaHeight + mFooterHeight;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700815 }
Adam Cohen234c4cd2011-07-17 21:03:04 -0700816
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700817 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800818 int contentWidth = getContentAreaWidth();
819 int contentHeight = getContentAreaHeight();
Adam Cohen1960ea42013-11-12 11:33:14 +0000820
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800821 int contentAreaWidthSpec = MeasureSpec.makeMeasureSpec(contentWidth, MeasureSpec.EXACTLY);
822 int contentAreaHeightSpec = MeasureSpec.makeMeasureSpec(contentHeight, MeasureSpec.EXACTLY);
823
824 mContent.setFixedSize(contentWidth, contentHeight);
825 mContentWrapper.measure(contentAreaWidthSpec, contentAreaHeightSpec);
Sunny Goyal290800b2015-03-05 11:33:33 -0800826 mFooter.measure(contentAreaWidthSpec,
827 MeasureSpec.makeMeasureSpec(mFooterHeight, MeasureSpec.EXACTLY));
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800828
829 int folderWidth = getPaddingLeft() + getPaddingRight() + contentWidth;
830 int folderHeight = getFolderHeight(contentHeight);
831 setMeasuredDimension(folderWidth, folderHeight);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700832 }
833
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800834 /**
835 * Rearranges the children based on their rank.
836 */
837 public void rearrangeChildren() {
838 rearrangeChildren(-1);
839 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700840
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800841 /**
842 * Rearranges the children based on their rank.
843 * @param itemCount if greater than the total children count, empty spaces are left at the end,
844 * otherwise it is ignored.
845 */
846 public void rearrangeChildren(int itemCount) {
847 ArrayList<View> views = getItemsInReadingOrder();
848 mContent.arrangeChildren(views, Math.max(itemCount, views.size()));
Adam Cohen7c693212011-05-18 15:26:57 -0700849 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700850 }
851
Adam Cohena9cf38f2011-05-02 15:36:58 -0700852 public int getItemCount() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800853 return mContent.getItemCount();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700854 }
855
Adam Cohen2801caf2011-05-13 20:57:39 -0700856 private void onCloseComplete() {
Adam Cohen05e0f402011-08-01 12:12:49 -0700857 DragLayer parent = (DragLayer) getParent();
Michael Jurka5649c282012-06-18 10:33:21 -0700858 if (parent != null) {
859 parent.removeView(this);
860 }
Adam Cohen4554ee12011-08-03 16:13:21 -0700861 mDragController.removeDropTarget((DropTarget) this);
Adam Cohen05e0f402011-08-01 12:12:49 -0700862 clearFocus();
Adam Cohenac56cff2011-09-28 20:45:37 -0700863 mFolderIcon.requestFocus();
Adam Cohen05e0f402011-08-01 12:12:49 -0700864
Adam Cohen2801caf2011-05-13 20:57:39 -0700865 if (mRearrangeOnClose) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800866 rearrangeChildren();
Adam Cohen2801caf2011-05-13 20:57:39 -0700867 mRearrangeOnClose = false;
868 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700869 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700870 if (!mDragInProgress && !mSuppressFolderDeletion) {
871 replaceFolderWithFinalItem();
872 } else if (mDragInProgress) {
873 mDeleteFolderOnDropCompleted = true;
874 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700875 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700876 mSuppressFolderDeletion = false;
Adam Cohenafb01ee2011-06-23 15:38:03 -0700877 }
878
879 private void replaceFolderWithFinalItem() {
Adam Cohenafb01ee2011-06-23 15:38:03 -0700880 // Add the last remaining child to the workspace in place of the folder
Adam Cohenfb91f302012-06-11 15:45:18 -0700881 Runnable onCompleteRunnable = new Runnable() {
882 @Override
883 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -0700884 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screenId);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700885
Winson Chung33231f52013-12-09 16:57:45 -0800886 View child = null;
Adam Cohenfb91f302012-06-11 15:45:18 -0700887 // Move the item from the folder to the workspace, in the position of the folder
888 if (getItemCount() == 1) {
889 ShortcutInfo finalItem = mInfo.contents.get(0);
Adam Cohenc5e63f32012-07-12 16:16:57 -0700890 child = mLauncher.createShortcut(R.layout.application, cellLayout,
Adam Cohenfb91f302012-06-11 15:45:18 -0700891 finalItem);
892 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700893 mInfo.screenId, mInfo.cellX, mInfo.cellY);
Adam Cohenfb91f302012-06-11 15:45:18 -0700894 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700895 if (getItemCount() <= 1) {
896 // Remove the folder
897 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
Dan Sandler0eb687f2014-01-10 13:24:55 -0500898 if (cellLayout != null) {
899 // b/12446428 -- sometimes the cell layout has already gone away?
900 cellLayout.removeView(mFolderIcon);
901 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700902 if (mFolderIcon instanceof DropTarget) {
903 mDragController.removeDropTarget((DropTarget) mFolderIcon);
904 }
905 mLauncher.removeFolder(mInfo);
906 }
Adam Cohenc5e63f32012-07-12 16:16:57 -0700907 // We add the child after removing the folder to prevent both from existing at
Winson Chung0e6a7132013-08-23 12:55:10 -0700908 // the same time in the CellLayout. We need to add the new item with addInScreenFromBind()
909 // to ensure that hotseat items are placed correctly.
Adam Cohenc5e63f32012-07-12 16:16:57 -0700910 if (child != null) {
Winson Chung0e6a7132013-08-23 12:55:10 -0700911 mLauncher.getWorkspace().addInScreenFromBind(child, mInfo.container, mInfo.screenId,
Adam Cohenc5e63f32012-07-12 16:16:57 -0700912 mInfo.cellX, mInfo.cellY, mInfo.spanX, mInfo.spanY);
913 }
Adam Cohenfb91f302012-06-11 15:45:18 -0700914 }
915 };
Sunny Goyalbc753352015-03-05 09:40:44 -0800916 View finalChild = mContent.getLastItem();
Adam Cohenfb91f302012-06-11 15:45:18 -0700917 if (finalChild != null) {
918 mFolderIcon.performDestroyAnimation(finalChild, onCompleteRunnable);
Winson Chung33231f52013-12-09 16:57:45 -0800919 } else {
920 onCompleteRunnable.run();
Adam Cohenafb01ee2011-06-23 15:38:03 -0700921 }
Adam Cohenfb91f302012-06-11 15:45:18 -0700922 mDestroyed = true;
923 }
924
925 boolean isDestroyed() {
926 return mDestroyed;
Adam Cohen2801caf2011-05-13 20:57:39 -0700927 }
928
Adam Cohenac56cff2011-09-28 20:45:37 -0700929 // This method keeps track of the last item in the folder for the purposes
930 // of keyboard focus
Sunny Goyal290800b2015-03-05 11:33:33 -0800931 public void updateTextViewFocus() {
Sunny Goyalbc753352015-03-05 09:40:44 -0800932 View lastChild = mContent.getLastItem();
Adam Cohenac56cff2011-09-28 20:45:37 -0700933 if (lastChild != null) {
934 mFolderName.setNextFocusDownId(lastChild.getId());
935 mFolderName.setNextFocusRightId(lastChild.getId());
936 mFolderName.setNextFocusLeftId(lastChild.getId());
937 mFolderName.setNextFocusUpId(lastChild.getId());
938 }
939 }
940
Adam Cohenbfbfd262011-06-13 16:55:12 -0700941 public void onDrop(DragObject d) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800942 Runnable cleanUpRunnable = null;
943
Adam Cohen689ff162014-05-08 17:27:56 -0700944 // If we are coming from All Apps space, we defer removing the extra empty screen
945 // until the folder closes
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800946 if (d.dragSource != mLauncher.getWorkspace() && !(d.dragSource instanceof Folder)) {
947 cleanUpRunnable = new Runnable() {
948 @Override
949 public void run() {
Adam Cohen689ff162014-05-08 17:27:56 -0700950 mLauncher.exitSpringLoadedDragModeDelayed(true,
951 Launcher.EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
952 null);
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800953 }
954 };
Adam Cohenbfbfd262011-06-13 16:55:12 -0700955 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800956
957 View currentDragView;
958 ShortcutInfo si = mCurrentDragInfo;
959 if (mIsExternalDrag) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800960 currentDragView = mContent.createAndAddViewForRank(si, mEmptyCellRank);
Sunny Goyal95abbb32014-08-04 10:53:22 -0700961 // Actually move the item in the database if it was an external drag. Call this
962 // before creating the view, so that ShortcutInfo is updated appropriately.
963 LauncherModel.addOrMoveItemInDatabase(
964 mLauncher, si, mInfo.id, 0, si.cellX, si.cellY);
965
966 // We only need to update the locations if it doesn't get handled in #onDropCompleted.
967 if (d.dragSource != this) {
968 updateItemLocationsInDatabaseBatch();
969 }
970 mIsExternalDrag = false;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800971 } else {
972 currentDragView = mCurrentDragView;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800973 mContent.addViewForRank(currentDragView, si, mEmptyCellRank);
Adam Cohenbfbfd262011-06-13 16:55:12 -0700974 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800975
976 if (d.dragView.hasDrawn()) {
977
978 // Temporarily reset the scale such that the animation target gets calculated correctly.
979 float scaleX = getScaleX();
980 float scaleY = getScaleY();
981 setScaleX(1.0f);
982 setScaleY(1.0f);
983 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, currentDragView,
984 cleanUpRunnable, null);
985 setScaleX(scaleX);
986 setScaleY(scaleY);
987 } else {
988 d.deferDragViewCleanupPostAnimation = false;
989 currentDragView.setVisibility(VISIBLE);
990 }
991 mItemsInvalidated = true;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800992 rearrangeChildren();
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800993
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800994 // Temporarily suppress the listener, as we did all the work already here.
995 mSuppressOnAdd = true;
996 mInfo.add(si);
997 mSuppressOnAdd = false;
Sunny Goyal4b020172014-08-28 14:51:14 -0700998 // Clear the drag info, as it is no longer being dragged.
999 mCurrentDragInfo = null;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001000 }
1001
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001002 // This is used so the item doesn't immediately appear in the folder when added. In one case
1003 // we need to create the illusion that the item isn't added back to the folder yet, to
1004 // to correspond to the animation of the icon back into the folder. This is
1005 public void hideItem(ShortcutInfo info) {
1006 View v = getViewForInfo(info);
1007 v.setVisibility(INVISIBLE);
1008 }
1009 public void showItem(ShortcutInfo info) {
1010 View v = getViewForInfo(info);
1011 v.setVisibility(VISIBLE);
1012 }
1013
Adam Cohenbfbfd262011-06-13 16:55:12 -07001014 public void onAdd(ShortcutInfo item) {
1015 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001016 // If the item was dropped onto this open folder, we have done the work associated
1017 // with adding the item to the folder, as indicated by mSuppressOnAdd being set
Adam Cohenbfbfd262011-06-13 16:55:12 -07001018 if (mSuppressOnAdd) return;
Sunny Goyalbc753352015-03-05 09:40:44 -08001019 mContent.createAndAddViewForRank(item, mContent.allocateNewLastItemRank());
Adam Cohenbfbfd262011-06-13 16:55:12 -07001020 LauncherModel.addOrMoveItemInDatabase(
1021 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
1022 }
1023
Adam Cohena9cf38f2011-05-02 15:36:58 -07001024 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -07001025 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001026 // If this item is being dragged from this open folder, we have already handled
1027 // the work associated with removing the item, so we don't have to do anything here.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001028 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001029 View v = getViewForInfo(item);
Sunny Goyal290800b2015-03-05 11:33:33 -08001030 mContent.removeItem(v);
Adam Cohen2801caf2011-05-13 20:57:39 -07001031 if (mState == STATE_ANIMATING) {
1032 mRearrangeOnClose = true;
1033 } else {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001034 rearrangeChildren();
Adam Cohen2801caf2011-05-13 20:57:39 -07001035 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001036 if (getItemCount() <= 1) {
1037 replaceFolderWithFinalItem();
1038 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001039 }
Adam Cohen7c693212011-05-18 15:26:57 -07001040
Sunny Goyalbc753352015-03-05 09:40:44 -08001041 private View getViewForInfo(final ShortcutInfo item) {
1042 return mContent.iterateOverItems(new ItemOperator() {
1043
1044 @Override
1045 public boolean evaluate(ItemInfo info, View view, View parent) {
1046 return info == item;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001047 }
Sunny Goyalbc753352015-03-05 09:40:44 -08001048 });
Adam Cohendf1e4e82011-06-24 15:57:39 -07001049 }
1050
Adam Cohen76078c42011-06-09 15:06:52 -07001051 public void onItemsChanged() {
Adam Cohenac56cff2011-09-28 20:45:37 -07001052 updateTextViewFocus();
Adam Cohen76078c42011-06-09 15:06:52 -07001053 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001054
Adam Cohen76fc0852011-06-17 13:26:23 -07001055 public void onTitleChanged(CharSequence title) {
1056 }
Adam Cohen76078c42011-06-09 15:06:52 -07001057
Adam Cohen7c693212011-05-18 15:26:57 -07001058 public ArrayList<View> getItemsInReadingOrder() {
1059 if (mItemsInvalidated) {
1060 mItemsInReadingOrder.clear();
Sunny Goyalbc753352015-03-05 09:40:44 -08001061 mContent.iterateOverItems(new ItemOperator() {
1062
1063 @Override
1064 public boolean evaluate(ItemInfo info, View view, View parent) {
1065 mItemsInReadingOrder.add(view);
1066 return false;
Adam Cohen7c693212011-05-18 15:26:57 -07001067 }
Sunny Goyalbc753352015-03-05 09:40:44 -08001068 });
Adam Cohen7c693212011-05-18 15:26:57 -07001069 mItemsInvalidated = false;
1070 }
1071 return mItemsInReadingOrder;
1072 }
Adam Cohen8dfcba42011-07-07 16:38:18 -07001073
1074 public void getLocationInDragLayer(int[] loc) {
1075 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
1076 }
Adam Cohenea0818d2011-09-30 20:06:58 -07001077
1078 public void onFocusChange(View v, boolean hasFocus) {
1079 if (v == mFolderName && hasFocus) {
1080 startEditingFolderName();
1081 }
1082 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001083
1084 @Override
1085 public void getHitRectRelativeToDragLayer(Rect outRect) {
1086 getHitRect(outRect);
1087 }
Sunny Goyalbc753352015-03-05 09:40:44 -08001088
1089 public static interface FolderContent {
1090 void setFolder(Folder f);
1091
Sunny Goyal290800b2015-03-05 11:33:33 -08001092 void removeItem(View v);
Sunny Goyalbc753352015-03-05 09:40:44 -08001093
1094 boolean isFull();
1095 int getItemCount();
1096
1097 int getDesiredWidth();
1098 int getDesiredHeight();
1099 void setFixedSize(int width, int height);
1100
1101 /**
1102 * Iterates over all its items in a reading order.
1103 * @return the view for which the operator returned true.
1104 */
1105 View iterateOverItems(ItemOperator op);
1106 View getLastItem();
1107
1108 String getAccessibilityDescription();
1109
1110 /**
1111 * Binds items to the layout.
1112 * @return list of items that could not be bound, probably because we hit the max size limit.
1113 */
1114 ArrayList<ShortcutInfo> bindItems(ArrayList<ShortcutInfo> children);
1115
1116 /**
1117 * Create space for a new item at the end, and returns the rank for that item.
1118 * Resizes the content if necessary.
1119 */
1120 int allocateNewLastItemRank();
1121
1122 View createAndAddViewForRank(ShortcutInfo item, int rank);
1123
1124 /**
1125 * Adds the {@param view} to the layout based on {@param rank} and updated the position
1126 * related attributes. It assumes that {@param item} is already attached to the view.
1127 */
1128 void addViewForRank(View view, ShortcutInfo item, int rank);
1129
1130 /**
1131 * Reorders the items such that the {@param empty} spot moves to {@param target}
1132 */
1133 void realTimeReorder(int empty, int target);
1134
1135 /**
1136 * @return the rank of the cell nearest to the provided pixel position.
1137 */
1138 int findNearestArea(int pixelX, int pixelY);
1139
1140 /**
1141 * Updates position and rank of all the children in the view based.
1142 * @param list the ordered list of children.
1143 * @param itemCount if greater than the total children count, empty spaces are left
1144 * at the end.
1145 */
1146 void arrangeChildren(ArrayList<View> list, int itemCount);
1147
1148 /**
1149 * Sets the focus on the first visible child.
1150 */
1151 void setFocusOnFirstChild();
1152 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001153}